encoding: tests (#63)

* encoding: tests

* transaction hashing test
This commit is contained in:
Federico Kunze 2021-06-04 09:14:45 -04:00 committed by GitHub
parent bf8e4b404e
commit 475c61851c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 12 deletions

View File

@ -12,11 +12,13 @@ import (
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db" dbm "github.com/tendermint/tm-db"
"github.com/cosmos/ethermint/encoding"
) )
func TestEthermintAppExport(t *testing.T) { func TestEthermintAppExport(t *testing.T) {
db := dbm.NewMemDB() 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() genesisState := NewDefaultGenesisState()
stateBytes, err := json.MarshalIndent(genesisState, "", " ") stateBytes, err := json.MarshalIndent(genesisState, "", " ")
@ -33,7 +35,7 @@ func TestEthermintAppExport(t *testing.T) {
app.Commit() app.Commit()
// Making a new app object with the db, so that initchain hasn't been called // 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{}) _, err = app2.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error") require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
} }

View File

@ -12,11 +12,13 @@ import (
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking" "github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/ethermint/encoding"
) )
// NewDefaultGenesisState generates the default state for the application. // NewDefaultGenesisState generates the default state for the application.
func NewDefaultGenesisState() simapp.GenesisState { func NewDefaultGenesisState() simapp.GenesisState {
encCfg := MakeEncodingConfig() encCfg := encoding.MakeConfig(ModuleBasics)
return ModuleBasics.DefaultGenesis(encCfg.Marshaler) return ModuleBasics.DefaultGenesis(encCfg.Marshaler)
} }

View File

@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/ethermint/encoding"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log" "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. // Setup initializes a new EthermintApp. A Nop logger is set in EthermintApp.
func Setup(isCheckTx bool) *EthermintApp { func Setup(isCheckTx bool) *EthermintApp {
db := dbm.NewMemDB() 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 { if !isCheckTx {
// init chain must be called to stop deliverState from being nil // init chain must be called to stop deliverState from being nil
genesisState := NewDefaultGenesisState() genesisState := NewDefaultGenesisState()

View File

@ -1,4 +1,4 @@
package codec package encoding
import ( import (
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"

View File

@ -1,4 +1,4 @@
package app package encoding
import ( import (
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
@ -6,16 +6,16 @@ import (
"github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/simapp/params" "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth/tx" "github.com/cosmos/cosmos-sdk/x/auth/tx"
ethtypes "github.com/ethereum/go-ethereum/core/types" ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/cosmos/ethermint/codec"
evmtypes "github.com/cosmos/ethermint/x/evm/types" evmtypes "github.com/cosmos/ethermint/x/evm/types"
) )
// MakeEncodingConfig creates an EncodingConfig for testing // MakeEncodingConfig creates an EncodingConfig for testing
func MakeEncodingConfig() params.EncodingConfig { func MakeConfig(mb module.BasicManager) params.EncodingConfig {
cdc := amino.NewLegacyAmino() cdc := amino.NewLegacyAmino()
interfaceRegistry := types.NewInterfaceRegistry() interfaceRegistry := types.NewInterfaceRegistry()
marshaler := amino.NewProtoCodec(interfaceRegistry) marshaler := amino.NewProtoCodec(interfaceRegistry)
@ -27,10 +27,10 @@ func MakeEncodingConfig() params.EncodingConfig {
Amino: cdc, Amino: cdc,
} }
codec.RegisterLegacyAminoCodec(encodingConfig.Amino) RegisterLegacyAminoCodec(encodingConfig.Amino)
ModuleBasics.RegisterLegacyAminoCodec(encodingConfig.Amino) mb.RegisterLegacyAminoCodec(encodingConfig.Amino)
codec.RegisterInterfaces(encodingConfig.InterfaceRegistry) RegisterInterfaces(encodingConfig.InterfaceRegistry)
ModuleBasics.RegisterInterfaces(encodingConfig.InterfaceRegistry) mb.RegisterInterfaces(encodingConfig.InterfaceRegistry)
return encodingConfig return encodingConfig
} }

44
encoding/config_test.go Normal file
View File

@ -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())
}