proto, evm: use string for address and hash protobuf message fields (#23)

* proto, evm: use string for address and hash protobuf message fields

* fix tests

* msg test
This commit is contained in:
Federico Kunze
2021-05-14 02:52:18 -04:00
committed by GitHub
parent a952bc36d1
commit ba5bf33f19
18 changed files with 536 additions and 447 deletions
+164
View File
@@ -0,0 +1,164 @@
package importer
import (
"math/big"
ethcmn "github.com/ethereum/go-ethereum/common"
ethcons "github.com/ethereum/go-ethereum/consensus"
ethstate "github.com/ethereum/go-ethereum/core/state"
ethtypes "github.com/ethereum/go-ethereum/core/types"
ethrpc "github.com/ethereum/go-ethereum/rpc"
)
// ChainContext implements Ethereum's core.ChainContext and consensus.Engine
// interfaces. It is needed in order to apply and process Ethereum
// transactions. There should only be a single implementation in Ethermint. For
// the purposes of Ethermint, it should be support retrieving headers and
// consensus parameters from the current blockchain to be used during
// transaction processing.
//
// NOTE: Ethermint will distribute the fees out to validators, so the structure
// and functionality of this is a WIP and subject to change.
type ChainContext struct {
Coinbase ethcmn.Address
headersByNumber map[uint64]*ethtypes.Header
}
// NewChainContext generates new ChainContext based on Ethereum's core.ChainContext and
// consensus.Engine interfaces in order to process Ethereum transactions.
func NewChainContext() *ChainContext {
return &ChainContext{
headersByNumber: make(map[uint64]*ethtypes.Header),
}
}
// Engine implements Ethereum's core.ChainContext interface. As a ChainContext
// implements the consensus.Engine interface, it is simply returned.
func (cc *ChainContext) Engine() ethcons.Engine {
return cc
}
// SetHeader implements Ethereum's core.ChainContext interface. It sets the
// header for the given block number.
func (cc *ChainContext) SetHeader(number uint64, header *ethtypes.Header) {
cc.headersByNumber[number] = header
}
// GetHeader implements Ethereum's core.ChainContext interface.
//
// TODO: The Cosmos SDK supports retreiving such information in contexts and
// multi-store, so this will be need to be integrated.
func (cc *ChainContext) GetHeader(_ ethcmn.Hash, number uint64) *ethtypes.Header {
if header, ok := cc.headersByNumber[number]; ok {
return header
}
return nil
}
// Author implements Ethereum's consensus.Engine interface. It is responsible
// for returned the address of the validtor to receive any fees. This function
// is only invoked if the given author in the ApplyTransaction call is nil.
//
// NOTE: Ethermint will distribute the fees out to validators, so the structure
// and functionality of this is a WIP and subject to change.
func (cc *ChainContext) Author(_ *ethtypes.Header) (ethcmn.Address, error) {
return cc.Coinbase, nil
}
// APIs implements Ethereum's consensus.Engine interface. It currently performs
// a no-op.
//
// TODO: Do we need to support such RPC APIs? This will tie into a bigger
// discussion on if we want to support web3.
func (cc *ChainContext) APIs(_ ethcons.ChainHeaderReader) []ethrpc.API {
return nil
}
// CalcDifficulty implements Ethereum's consensus.Engine interface. It currently
// performs a no-op.
func (cc *ChainContext) CalcDifficulty(_ ethcons.ChainHeaderReader, _ uint64, _ *ethtypes.Header) *big.Int {
return nil
}
// Finalize implements Ethereum's consensus.Engine interface. It currently
// performs a no-op.
//
// TODO: Figure out if this needs to be hooked up to any part of the ABCI?
func (cc *ChainContext) Finalize(
_ ethcons.ChainHeaderReader, _ *ethtypes.Header, _ *ethstate.StateDB,
_ []*ethtypes.Transaction, _ []*ethtypes.Header) {
}
// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
// rewards) and assembles the final block.
//
// Note: The block header and state database might be updated to reflect any
// consensus rules that happen at finalization (e.g. block rewards).
// TODO: Figure out if this needs to be hooked up to any part of the ABCI?
func (cc *ChainContext) FinalizeAndAssemble(_ ethcons.ChainHeaderReader, _ *ethtypes.Header, _ *ethstate.StateDB, _ []*ethtypes.Transaction,
_ []*ethtypes.Header, _ []*ethtypes.Receipt) (*ethtypes.Block, error) {
return nil, nil
}
// Prepare implements Ethereum's consensus.Engine interface. It currently
// performs a no-op.
//
// TODO: Figure out if this needs to be hooked up to any part of the ABCI?
func (cc *ChainContext) Prepare(_ ethcons.ChainHeaderReader, _ *ethtypes.Header) error {
return nil
}
// Seal implements Ethereum's consensus.Engine interface. It currently
// performs a no-op.
//
// TODO: Figure out if this needs to be hooked up to any part of the ABCI?
func (cc *ChainContext) Seal(_ ethcons.ChainHeaderReader, _ *ethtypes.Block, _ chan<- *ethtypes.Block, _ <-chan struct{}) error {
return nil
}
// SealHash implements Ethereum's consensus.Engine interface. It returns the
// hash of a block prior to it being sealed.
func (cc *ChainContext) SealHash(header *ethtypes.Header) ethcmn.Hash {
return ethcmn.Hash{}
}
// VerifyHeader implements Ethereum's consensus.Engine interface. It currently
// performs a no-op.
//
// TODO: Figure out if this needs to be hooked up to any part of the Cosmos SDK
// handlers?
func (cc *ChainContext) VerifyHeader(_ ethcons.ChainHeaderReader, _ *ethtypes.Header, _ bool) error {
return nil
}
// VerifyHeaders implements Ethereum's consensus.Engine interface. It
// currently performs a no-op.
//
// TODO: Figure out if this needs to be hooked up to any part of the Cosmos SDK
// handlers?
func (cc *ChainContext) VerifyHeaders(_ ethcons.ChainHeaderReader, _ []*ethtypes.Header, _ []bool) (chan<- struct{}, <-chan error) {
return nil, nil
}
// VerifySeal implements Ethereum's consensus.Engine interface. It currently
// performs a no-op.
//
// TODO: Figure out if this needs to be hooked up to any part of the Cosmos SDK
// handlers?
func (cc *ChainContext) VerifySeal(_ ethcons.ChainHeaderReader, _ *ethtypes.Header) error {
return nil
}
// VerifyUncles implements Ethereum's consensus.Engine interface. It currently
// performs a no-op.
func (cc *ChainContext) VerifyUncles(_ ethcons.ChainReader, _ *ethtypes.Block) error {
return nil
}
// Close implements Ethereum's consensus.Engine interface. It terminates any
// background threads maintained by the consensus engine. It currently performs
// a no-op.
func (cc *ChainContext) Close() error {
return nil
}
+119
View File
@@ -0,0 +1,119 @@
package importer
// NOTE: A bulk of these unit tests will change and evolve as the context and
// implementation of ChainConext evolves.
import (
"math/big"
"testing"
"github.com/stretchr/testify/require"
ethcmn "github.com/ethereum/go-ethereum/common"
ethcons "github.com/ethereum/go-ethereum/consensus"
ethcore "github.com/ethereum/go-ethereum/core"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)
func TestChainContextInterface(t *testing.T) {
require.Implements(t, (*ethcore.ChainContext)(nil), new(ChainContext))
require.Implements(t, (*ethcons.Engine)(nil), new(ChainContext))
}
func TestNewChainContext(t *testing.T) {
cc := NewChainContext()
require.NotNil(t, cc.headersByNumber)
}
func TestChainContextEngine(t *testing.T) {
cc := NewChainContext()
require.Equal(t, cc, cc.Engine())
}
func TestChainContextSetHeader(t *testing.T) {
cc := NewChainContext()
header := &ethtypes.Header{
Number: big.NewInt(64),
}
cc.SetHeader(uint64(header.Number.Int64()), header)
require.Equal(t, header, cc.headersByNumber[uint64(header.Number.Int64())])
}
func TestChainContextGetHeader(t *testing.T) {
cc := NewChainContext()
header := &ethtypes.Header{
Number: big.NewInt(64),
}
cc.SetHeader(uint64(header.Number.Int64()), header)
require.Equal(t, header, cc.GetHeader(ethcmn.Hash{}, uint64(header.Number.Int64())))
require.Nil(t, cc.GetHeader(ethcmn.Hash{}, 0))
}
func TestChainContextAuthor(t *testing.T) {
cc := NewChainContext()
cb, err := cc.Author(nil)
require.Nil(t, err)
require.Equal(t, cc.Coinbase, cb)
}
func TestChainContextAPIs(t *testing.T) {
cc := NewChainContext()
require.Nil(t, cc.APIs(nil))
}
func TestChainContextCalcDifficulty(t *testing.T) {
cc := NewChainContext()
require.Nil(t, cc.CalcDifficulty(nil, 0, nil))
}
func TestChainContextFinalize(t *testing.T) {
cc := NewChainContext()
cc.Finalize(nil, nil, nil, nil, nil)
}
func TestChainContextPrepare(t *testing.T) {
cc := NewChainContext()
err := cc.Prepare(nil, nil)
require.Nil(t, err)
}
func TestChainContextSeal(t *testing.T) {
cc := NewChainContext()
err := cc.Seal(nil, nil, nil, nil)
require.Nil(t, err)
}
func TestChainContextVerifyHeader(t *testing.T) {
cc := NewChainContext()
err := cc.VerifyHeader(nil, nil, false)
require.Nil(t, err)
}
func TestChainContextVerifyHeaders(t *testing.T) {
cc := NewChainContext()
ch, err := cc.VerifyHeaders(nil, nil, []bool{false})
require.Nil(t, err)
require.Nil(t, ch)
}
func TestChainContextVerifySeal(t *testing.T) {
cc := NewChainContext()
err := cc.VerifySeal(nil, nil)
require.Nil(t, err)
}
func TestChainContextVerifyUncles(t *testing.T) {
cc := NewChainContext()
err := cc.VerifyUncles(nil, nil)
require.Nil(t, err)
}
+1 -2
View File
@@ -29,7 +29,6 @@ import (
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/ethermint/codec"
"github.com/cosmos/ethermint/core"
"github.com/cosmos/ethermint/types"
evmkeeper "github.com/cosmos/ethermint/x/evm/keeper"
evmtypes "github.com/cosmos/ethermint/x/evm/types"
@@ -226,7 +225,7 @@ func TestImportBlocks(t *testing.T) {
}()
// ethereum mainnet config
chainContext := core.NewChainContext()
chainContext := NewChainContext()
vmConfig := ethvm.Config{}
chainConfig := ethparams.MainnetChainConfig