diff --git a/app/app_test.go b/app/app_test.go index a2806357..d9c63244 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -12,11 +12,13 @@ import ( abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" dbm "github.com/tendermint/tm-db" + + "github.com/cosmos/ethermint/encoding" ) func TestEthermintAppExport(t *testing.T) { db := dbm.NewMemDB() - app := NewEthermintApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, MakeEncodingConfig(), simapp.EmptyAppOptions{}) + app := NewEthermintApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encoding.MakeConfig(ModuleBasics), simapp.EmptyAppOptions{}) genesisState := NewDefaultGenesisState() stateBytes, err := json.MarshalIndent(genesisState, "", " ") @@ -33,7 +35,7 @@ func TestEthermintAppExport(t *testing.T) { app.Commit() // Making a new app object with the db, so that initchain hasn't been called - app2 := NewEthermintApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, MakeEncodingConfig(), simapp.EmptyAppOptions{}) + app2 := NewEthermintApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encoding.MakeConfig(ModuleBasics), simapp.EmptyAppOptions{}) _, err = app2.ExportAppStateAndValidators(false, []string{}) require.NoError(t, err, "ExportAppStateAndValidators should not have an error") } diff --git a/app/export.go b/app/export.go index 23ba86fe..54ea669a 100644 --- a/app/export.go +++ b/app/export.go @@ -12,11 +12,13 @@ import ( slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" "github.com/cosmos/cosmos-sdk/x/staking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + + "github.com/cosmos/ethermint/encoding" ) // NewDefaultGenesisState generates the default state for the application. func NewDefaultGenesisState() simapp.GenesisState { - encCfg := MakeEncodingConfig() + encCfg := encoding.MakeConfig(ModuleBasics) return ModuleBasics.DefaultGenesis(encCfg.Marshaler) } diff --git a/app/test_helpers.go b/app/test_helpers.go index 05f5bb64..47d62188 100644 --- a/app/test_helpers.go +++ b/app/test_helpers.go @@ -5,6 +5,7 @@ import ( "time" "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/ethermint/encoding" abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" @@ -35,7 +36,7 @@ var DefaultConsensusParams = &abci.ConsensusParams{ // Setup initializes a new EthermintApp. A Nop logger is set in EthermintApp. func Setup(isCheckTx bool) *EthermintApp { db := dbm.NewMemDB() - app := NewEthermintApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 5, MakeEncodingConfig(), simapp.EmptyAppOptions{}) + app := NewEthermintApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 5, encoding.MakeConfig(ModuleBasics), simapp.EmptyAppOptions{}) if !isCheckTx { // init chain must be called to stop deliverState from being nil genesisState := NewDefaultGenesisState() diff --git a/codec/codec.go b/encoding/codec.go similarity index 97% rename from codec/codec.go rename to encoding/codec.go index 64782242..81540aa2 100644 --- a/codec/codec.go +++ b/encoding/codec.go @@ -1,4 +1,4 @@ -package codec +package encoding import ( "github.com/cosmos/cosmos-sdk/codec" diff --git a/app/encoding.go b/encoding/config.go similarity index 84% rename from app/encoding.go rename to encoding/config.go index 1ea6ca44..0744cd83 100644 --- a/app/encoding.go +++ b/encoding/config.go @@ -1,4 +1,4 @@ -package app +package encoding import ( "github.com/cosmos/cosmos-sdk/client" @@ -6,16 +6,16 @@ import ( "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/simapp/params" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/auth/tx" ethtypes "github.com/ethereum/go-ethereum/core/types" - "github.com/cosmos/ethermint/codec" evmtypes "github.com/cosmos/ethermint/x/evm/types" ) // MakeEncodingConfig creates an EncodingConfig for testing -func MakeEncodingConfig() params.EncodingConfig { +func MakeConfig(mb module.BasicManager) params.EncodingConfig { cdc := amino.NewLegacyAmino() interfaceRegistry := types.NewInterfaceRegistry() marshaler := amino.NewProtoCodec(interfaceRegistry) @@ -27,10 +27,10 @@ func MakeEncodingConfig() params.EncodingConfig { Amino: cdc, } - codec.RegisterLegacyAminoCodec(encodingConfig.Amino) - ModuleBasics.RegisterLegacyAminoCodec(encodingConfig.Amino) - codec.RegisterInterfaces(encodingConfig.InterfaceRegistry) - ModuleBasics.RegisterInterfaces(encodingConfig.InterfaceRegistry) + RegisterLegacyAminoCodec(encodingConfig.Amino) + mb.RegisterLegacyAminoCodec(encodingConfig.Amino) + RegisterInterfaces(encodingConfig.InterfaceRegistry) + mb.RegisterInterfaces(encodingConfig.InterfaceRegistry) return encodingConfig } diff --git a/encoding/config_test.go b/encoding/config_test.go new file mode 100644 index 00000000..a6a862ea --- /dev/null +++ b/encoding/config_test.go @@ -0,0 +1,44 @@ +package encoding_test + +import ( + "math/big" + "testing" + + "github.com/stretchr/testify/require" + + ethtypes "github.com/ethereum/go-ethereum/core/types" + + "github.com/cosmos/ethermint/app" + "github.com/cosmos/ethermint/encoding" + "github.com/cosmos/ethermint/tests" + evmtypes "github.com/cosmos/ethermint/x/evm/types" +) + +func TestTxEncoding(t *testing.T) { + addr, key := tests.NewAddrKey() + signer := tests.NewSigner(key) + + msg := evmtypes.NewMsgEthereumTxContract(big.NewInt(1), 1, big.NewInt(10), 100000, big.NewInt(1), []byte{}, nil) + msg.From = addr.Hex() + + ethSigner := ethtypes.LatestSignerForChainID(big.NewInt(1)) + err := msg.Sign(ethSigner, signer) + require.NoError(t, err) + + cfg := encoding.MakeConfig(app.ModuleBasics) + + bz, err := cfg.TxConfig.TxEncoder()(msg) + require.NoError(t, err, "encoding failed") + + tx, err := cfg.TxConfig.TxDecoder()(bz) + require.NoError(t, err, "decoding failed") + require.IsType(t, &evmtypes.MsgEthereumTx{}, tx) + require.Equal(t, msg.Data, tx.(*evmtypes.MsgEthereumTx).Data) + + // FIXME: transaction hashing is hardcoded on Terndermint: + // See https://github.com/tendermint/tendermint/issues/6539 for reference + // txHash := msg.AsTransaction().Hash() + // tmTx := tmtypes.Tx(bz) + + // require.Equal(t, txHash.Bytes(), tmTx.Hash()) +}