Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
stargate: fix ethereum transaction (#604)
Browse files Browse the repository at this point in the history
* bump cosmos-sdk dependency

* fix tx encoding

* fix eth tx

* update cosmos-sdk to latest version

* use cryptotypes.PrivKey
  • Loading branch information
chengwenxi authored Dec 10, 2020
1 parent 189d6c7 commit 4ee7eea
Show file tree
Hide file tree
Showing 33 changed files with 265 additions and 118 deletions.
5 changes: 3 additions & 2 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ func NewAnteHandler(
authante.NewRejectExtensionOptionsDecorator(),
NewEthMempoolFeeDecorator(evmKeeper),
authante.NewValidateBasicDecorator(),
authante.TxTimeoutHeightDecorator{},
authante.NewValidateMemoDecorator(ak),
// TODO: add timeout for MsgEthereumTx
// authante.TxTimeoutHeightDecorator{},
// authante.NewValidateMemoDecorator(ak),
NewEthSigVerificationDecorator(),
NewAccountVerificationDecorator(ak, bankKeeper, evmKeeper),
NewNonceVerificationDecorator(ak),
Expand Down
12 changes: 6 additions & 6 deletions app/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

ethcmn "github.com/ethereum/go-ethereum/common"

tmcrypto "github.com/tendermint/tendermint/crypto"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ethermint/app"
Expand Down Expand Up @@ -83,7 +83,7 @@ func (suite *AnteTestSuite) TestValidTx() {
msg1 := newTestMsg(addr1, addr2)
msgs := []sdk.Msg{msg1}

privKeys := []tmcrypto.PrivKey{priv1, priv2}
privKeys := []cryptotypes.PrivKey{priv1, priv2}
accNums := []uint64{acc1.GetAccountNumber(), acc2.GetAccountNumber()}
accSeqs := []uint64{acc1.GetSequence(), acc2.GetSequence()}

Expand Down Expand Up @@ -115,7 +115,7 @@ func (suite *AnteTestSuite) TestSDKInvalidSigs() {
// require validation failure with no signers
msgs := []sdk.Msg{msg1}

privKeys := []tmcrypto.PrivKey{}
privKeys := []cryptotypes.PrivKey{}
accNums := []uint64{acc1.GetAccountNumber(), acc2.GetAccountNumber()}
accSeqs := []uint64{acc1.GetSequence(), acc2.GetSequence()}

Expand All @@ -125,7 +125,7 @@ func (suite *AnteTestSuite) TestSDKInvalidSigs() {
// require validation failure with invalid number of signers
msgs = []sdk.Msg{msg1}

privKeys = []tmcrypto.PrivKey{priv1}
privKeys = []cryptotypes.PrivKey{priv1}
accNums = []uint64{acc1.GetAccountNumber(), acc2.GetAccountNumber()}
accSeqs = []uint64{acc1.GetSequence(), acc2.GetSequence()}

Expand All @@ -136,7 +136,7 @@ func (suite *AnteTestSuite) TestSDKInvalidSigs() {
msg2 := newTestMsg(addr1, addr3)
msgs = []sdk.Msg{msg1, msg2}

privKeys = []tmcrypto.PrivKey{priv1, priv2, priv3}
privKeys = []cryptotypes.PrivKey{priv1, priv2, priv3}
accNums = []uint64{acc1.GetAccountNumber(), acc2.GetAccountNumber(), 0}
accSeqs = []uint64{acc1.GetSequence(), acc2.GetSequence(), 0}

Expand All @@ -157,7 +157,7 @@ func (suite *AnteTestSuite) TestSDKInvalidAcc() {
fee := newTestStdFee()
msg1 := newTestMsg(addr1)
msgs := []sdk.Msg{msg1}
privKeys := []tmcrypto.PrivKey{priv1}
privKeys := []cryptotypes.PrivKey{priv1}

// require validation failure with invalid account number
accNums := []uint64{1}
Expand Down
9 changes: 5 additions & 4 deletions app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (
ethermint "github.com/cosmos/ethermint/types"
evmtypes "github.com/cosmos/ethermint/x/evm/types"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"

ethcrypto "github.com/ethereum/go-ethereum/crypto"

tmcrypto "github.com/tendermint/tendermint/crypto"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

Expand Down Expand Up @@ -66,15 +67,15 @@ func newTestStdFee() legacytx.StdFee {
}

// GenerateAddress generates an Ethereum address.
func newTestAddrKey() (sdk.AccAddress, tmcrypto.PrivKey) {
func newTestAddrKey() (sdk.AccAddress, cryptotypes.PrivKey) {
privkey, _ := ethsecp256k1.GenerateKey()
addr := ethcrypto.PubkeyToAddress(privkey.ToECDSA().PublicKey)

return sdk.AccAddress(addr.Bytes()), privkey
}

func newTestSDKTx(
ctx sdk.Context, msgs []sdk.Msg, privs []tmcrypto.PrivKey,
ctx sdk.Context, msgs []sdk.Msg, privs []cryptotypes.PrivKey,
accNums []uint64, seqs []uint64, fee legacytx.StdFee,
) sdk.Tx {

Expand All @@ -96,7 +97,7 @@ func newTestSDKTx(
return legacytx.NewStdTx(msgs, fee, sigs, "")
}

func newTestEthTx(ctx sdk.Context, msg *evmtypes.MsgEthereumTx, priv tmcrypto.PrivKey) (sdk.Tx, error) {
func newTestEthTx(ctx sdk.Context, msg *evmtypes.MsgEthereumTx, priv cryptotypes.PrivKey) (sdk.Tx, error) {
chainIDEpoch, err := ethermint.ParseChainID(ctx.ChainID())
if err != nil {
return nil, err
Expand Down
57 changes: 56 additions & 1 deletion app/encoding.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,72 @@
package app

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/x/auth/tx"

"github.com/cosmos/ethermint/codec"
evmtypes "github.com/cosmos/ethermint/x/evm/types"
)

// MakeEncodingConfig creates an EncodingConfig for testing
func MakeEncodingConfig() params.EncodingConfig {
encodingConfig := params.MakeTestEncodingConfig()
cdc := amino.NewLegacyAmino()
interfaceRegistry := types.NewInterfaceRegistry()
marshaler := amino.NewProtoCodec(interfaceRegistry)

encodingConfig := params.EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: marshaler,
TxConfig: NewTxConfig(marshaler),
Amino: cdc,
}

codec.RegisterLegacyAminoCodec(encodingConfig.Amino)
ModuleBasics.RegisterLegacyAminoCodec(encodingConfig.Amino)
codec.RegisterInterfaces(encodingConfig.InterfaceRegistry)
ModuleBasics.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 overwites sdk.TxEncoder to support MsgEthereumTx
func (g txConfig) TxEncoder() sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
ethtx, ok := tx.(*evmtypes.MsgEthereumTx)
if ok {
return g.cdc.MarshalBinaryBare(ethtx)
}
return g.TxConfig.TxEncoder()(tx)
}
}

// TxDecoder overwites sdk.TxDecoder to support MsgEthereumTx
func (g txConfig) TxDecoder() sdk.TxDecoder {
return func(txBytes []byte) (sdk.Tx, error) {
var ethtx evmtypes.MsgEthereumTx

err := g.cdc.UnmarshalBinaryBare(txBytes, &ethtx)
if err == nil {
return &ethtx, nil
}

return g.TxConfig.TxDecoder()(txBytes)
}
}
14 changes: 11 additions & 3 deletions app/ethermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -86,6 +87,7 @@ import (
_ "github.com/cosmos/cosmos-sdk/client/docs/statik"

"github.com/cosmos/ethermint/app/ante"
ethermintrpc "github.com/cosmos/ethermint/rpc"
"github.com/cosmos/ethermint/server"
"github.com/cosmos/ethermint/server/api"
"github.com/cosmos/ethermint/server/config"
Expand Down Expand Up @@ -314,7 +316,7 @@ func NewEthermintApp(

// Create IBC Keeper
app.IBCKeeper = ibckeeper.NewKeeper(
appCodec, keys[ibchost.StoreKey], app.StakingKeeper, scopedIBCKeeper,
appCodec, keys[ibchost.StoreKey], app.GetSubspace(ibchost.ModuleName), app.StakingKeeper, scopedIBCKeeper,
)

// register the proposal types
Expand Down Expand Up @@ -601,10 +603,10 @@ func (app *EthermintApp) RegisterAPIRoutes(apiSvr *sdkapi.Server, apiConfig sdkc
authrest.RegisterTxRoutes(clientCtx, apiSvr.Router)

ModuleBasics.RegisterRESTRoutes(clientCtx, apiSvr.Router)
ModuleBasics.RegisterGRPCGatewayRoutes(apiSvr.ClientCtx, apiSvr.GRPCRouter)
ModuleBasics.RegisterGRPCGatewayRoutes(apiSvr.ClientCtx, apiSvr.GRPCGatewayRouter)

// Register Ethereum namespaces
// ethermintrpc.RegisterRoutes(clientCtx, apiSvr.Router)
ethermintrpc.RegisterEthereum(clientCtx, apiSvr.Router)

// register swagger API from root so that other applications can override easily
if apiConfig.Swagger {
Expand All @@ -617,6 +619,11 @@ func (app *EthermintApp) RegisterTxService(clientCtx client.Context) {
authtx.RegisterTxService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.BaseApp.Simulate, app.interfaceRegistry)
}

// RegisterTendermintService implements the Application.RegisterTendermintService method.
func (app *EthermintApp) RegisterTendermintService(clientCtx client.Context) {
tmservice.RegisterTendermintService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.interfaceRegistry)
}

// RegisterEthereumServers registers all application ethereum routes with the provided
// API server.
func (app *EthermintApp) RegisterEthereumServers(apiSvr *api.Server, apiConfig config.EthereumConfig) {
Expand Down Expand Up @@ -661,6 +668,7 @@ func initParamsKeeper(
paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govtypes.ParamKeyTable())
paramsKeeper.Subspace(crisistypes.ModuleName)
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(ibchost.ModuleName)
// ethermint subspaces
paramsKeeper.Subspace(evmtypes.ModuleName)

Expand Down
3 changes: 2 additions & 1 deletion app/ethermint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"os"
"testing"

"github.com/cosmos/cosmos-sdk/simapp"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/simapp"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
Expand Down
1 change: 1 addition & 0 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/cosmos/cosmos-sdk/simapp"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
Expand Down
6 changes: 3 additions & 3 deletions client/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/spf13/cobra"

tmconfig "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto"
tmos "github.com/tendermint/tendermint/libs/os"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/types"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/server"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -145,7 +145,7 @@ func InitTestnet(
}

nodeIDs := make([]string, numValidators)
valPubKeys := make([]crypto.PubKey, numValidators)
valPubKeys := make([]cryptotypes.PubKey, numValidators)

appConfig := srvconfig.DefaultConfig()
appConfig.MinGasPrices = minGasPrices
Expand Down Expand Up @@ -392,7 +392,7 @@ func initGenFiles(

func collectGenFiles(
clientCtx client.Context, nodeConfig *tmconfig.Config, chainID string,
nodeIDs []string, valPubKeys []crypto.PubKey, numValidators int,
nodeIDs []string, valPubKeys []cryptotypes.PubKey, numValidators int,
outputDir, nodeDirPrefix, nodeDaemonHome string, genBalIterator banktypes.GenesisBalancesIterator,
) error {

Expand Down
4 changes: 2 additions & 2 deletions cmd/ethermintd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
addr = info.GetAddress()
}

coins, err := sdk.ParseCoins(args[1])
coins, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return fmt.Errorf("failed to parse coins: %w", err)
}
Expand All @@ -88,7 +88,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
vestingEnd, _ := cmd.Flags().GetInt64(flagVestingEnd)
vestingAmtStr, _ := cmd.Flags().GetString(flagVestingAmt)

vestingAmt, err := sdk.ParseCoins(vestingAmtStr)
vestingAmt, err := sdk.ParseCoinsNormalized(vestingAmtStr)
if err != nil {
return fmt.Errorf("failed to parse vesting amount: %w", err)
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/ethermintd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"os"
"path/filepath"

"github.com/rs/zerolog"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/snapshots"

"github.com/spf13/cast"
"github.com/spf13/cobra"

tmcfg "github.com/tendermint/tendermint/config"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
Expand Down Expand Up @@ -82,9 +85,10 @@ func Execute(rootCmd *cobra.Command) error {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &client.Context{})
ctx = context.WithValue(ctx, sdkserver.ServerContextKey, sdkserver.NewDefaultContext())
ctx = context.WithValue(ctx, sdkserver.ServerContextKey, srvCtx)

rootCmd.PersistentFlags().String("log_level", srvCtx.Config.LogLevel, "The logging level in the format of <module>:<level>,...")
rootCmd.PersistentFlags().String(flags.FlagLogLevel, zerolog.InfoLevel.String(), "The logging level (trace|debug|info|warn|error|fatal|panic)")
rootCmd.PersistentFlags().String(flags.FlagLogFormat, tmcfg.LogFormatJSON, "The logging format (json|plain)")

executor := tmcli.PrepareBaseCmd(rootCmd, "", app.DefaultNodeHome)
return executor.ExecuteContext(ctx)
Expand Down
3 changes: 2 additions & 1 deletion codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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) {
std.RegisterLegacyAminoCodec(cdc)
sdk.RegisterLegacyAminoCodec(cdc)
cryptocodec.RegisterCrypto(cdc)
}

Expand Down
7 changes: 5 additions & 2 deletions crypto/codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package codec

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"

"github.com/cosmos/ethermint/crypto/ethsecp256k1"
Expand All @@ -17,12 +19,13 @@ func init() {
// RegisterCrypto registers all crypto dependency types with the provided Amino
// codec.
func RegisterCrypto(cdc *codec.LegacyAmino) {
keyring.RegisterLegacyAminoCodec(cdc)
cryptocodec.RegisterCrypto(cdc)
cdc.RegisterConcrete(&ethsecp256k1.PubKey{},
ethsecp256k1.PubKeyName, nil)
cdc.RegisterConcrete(&ethsecp256k1.PrivKey{},
ethsecp256k1.PrivKeyName, nil)

keyring.RegisterLegacyAminoCodec(cdc)
// update SDK's key codec to include the ethsecp256k1 keys.
keyring.CryptoCdc = cdc
legacy.Cdc = cdc
}
Loading

0 comments on commit 4ee7eea

Please sign in to comment.