@@ -0,0 +1,24 @@
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
"github.com/cosmos/cosmos-sdk/std"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
|
||||
cryptocodec "github.com/cosmos/ethermint/crypto/codec"
|
||||
ethermint "github.com/cosmos/ethermint/types"
|
||||
)
|
||||
|
||||
// RegisterLegacyAminoCodec registers Interfaces from types, crypto, and SDK std.
|
||||
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
|
||||
sdk.RegisterLegacyAminoCodec(cdc)
|
||||
cryptocodec.RegisterCrypto(cdc)
|
||||
}
|
||||
|
||||
// RegisterInterfaces registers Interfaces from types, crypto, and SDK std.
|
||||
func RegisterInterfaces(interfaceRegistry codectypes.InterfaceRegistry) {
|
||||
std.RegisterInterfaces(interfaceRegistry)
|
||||
cryptocodec.RegisterInterfaces(interfaceRegistry)
|
||||
ethermint.RegisterInterfaces(interfaceRegistry)
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
amino "github.com/cosmos/cosmos-sdk/codec"
|
||||
"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"
|
||||
|
||||
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
||||
)
|
||||
|
||||
// MakeEncodingConfig creates an EncodingConfig for testing
|
||||
func MakeConfig(mb module.BasicManager) params.EncodingConfig {
|
||||
cdc := amino.NewLegacyAmino()
|
||||
interfaceRegistry := types.NewInterfaceRegistry()
|
||||
marshaler := amino.NewProtoCodec(interfaceRegistry)
|
||||
|
||||
encodingConfig := params.EncodingConfig{
|
||||
InterfaceRegistry: interfaceRegistry,
|
||||
Marshaler: marshaler,
|
||||
TxConfig: NewTxConfig(marshaler),
|
||||
Amino: cdc,
|
||||
}
|
||||
|
||||
RegisterLegacyAminoCodec(encodingConfig.Amino)
|
||||
mb.RegisterLegacyAminoCodec(encodingConfig.Amino)
|
||||
RegisterInterfaces(encodingConfig.InterfaceRegistry)
|
||||
mb.RegisterInterfaces(encodingConfig.InterfaceRegistry)
|
||||
return encodingConfig
|
||||
}
|
||||
|
||||
type txConfig struct {
|
||||
cdc amino.ProtoCodecMarshaler
|
||||
client.TxConfig
|
||||
}
|
||||
|
||||
// NewTxConfig returns a new protobuf TxConfig using the provided ProtoCodec and sign modes. The
|
||||
// first enabled sign mode will become the default sign mode.
|
||||
func NewTxConfig(marshaler amino.ProtoCodecMarshaler) client.TxConfig {
|
||||
return &txConfig{
|
||||
marshaler,
|
||||
tx.NewTxConfig(marshaler, tx.DefaultSignModes),
|
||||
}
|
||||
}
|
||||
|
||||
// TxEncoder overwrites sdk.TxEncoder to support MsgEthereumTx
|
||||
func (g txConfig) TxEncoder() sdk.TxEncoder {
|
||||
return func(tx sdk.Tx) ([]byte, error) {
|
||||
msg, ok := tx.(*evmtypes.MsgEthereumTx)
|
||||
if ok {
|
||||
return msg.AsTransaction().MarshalBinary()
|
||||
}
|
||||
return g.TxConfig.TxEncoder()(tx)
|
||||
}
|
||||
}
|
||||
|
||||
// TxDecoder overwrites sdk.TxDecoder to support MsgEthereumTx
|
||||
func (g txConfig) TxDecoder() sdk.TxDecoder {
|
||||
return func(txBytes []byte) (sdk.Tx, error) {
|
||||
tx := ðtypes.Transaction{}
|
||||
|
||||
err := tx.UnmarshalBinary(txBytes)
|
||||
if err == nil {
|
||||
msg := &evmtypes.MsgEthereumTx{}
|
||||
msg.FromEthereumTx(tx)
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
return g.TxConfig.TxDecoder()(txBytes)
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
Reference in New Issue
Block a user