From 3f946920ad235d810eddff81388735baaabb4e9f Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Thu, 12 May 2022 19:28:21 +0800 Subject: [PATCH 01/11] middleware refactor --- app/app.go | 5 +- app/middleware/middleware.go | 141 +++++++++++++---------------------- app/middleware/options.go | 45 +++++------ app/middleware/utils_test.go | 10 +-- 4 files changed, 81 insertions(+), 120 deletions(-) diff --git a/app/app.go b/app/app.go index ff69c9db..4bb8121b 100644 --- a/app/app.go +++ b/app/app.go @@ -712,10 +712,7 @@ func (app *EthermintApp) setTxHandler(options middleware.HandlerOptions, txConfi options.IndexEvents = indexEvents - txHandler, err := middleware.NewMiddleware(options) - if err != nil { - panic(err) - } + txHandler := middleware.NewTxHandler(options) app.SetTxHandler(txHandler) } diff --git a/app/middleware/middleware.go b/app/middleware/middleware.go index 78425fbe..6d241dd5 100644 --- a/app/middleware/middleware.go +++ b/app/middleware/middleware.go @@ -7,7 +7,7 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/types/tx/signing" - authante "github.com/cosmos/cosmos-sdk/x/auth/middleware" + authmiddleware "github.com/cosmos/cosmos-sdk/x/auth/middleware" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/tharsis/ethermint/crypto/ethsecp256k1" ) @@ -16,128 +16,91 @@ const ( secp256k1VerifyCost uint64 = 21000 ) -type MD struct { - ethMiddleware tx.Handler - cosmosMiddleware tx.Handler - cosmoseip712 tx.Handler +type txRouter struct { + eth, cosmos, eip712 tx.Handler } -var _ tx.Handler = MD{} +var _ tx.Handler = txRouter{} -func NewMiddleware(options HandlerOptions) (tx.Handler, error) { - ethMiddleware, err := newEthAuthMiddleware(options) - if err != nil { - return nil, err +func NewTxHandler(options HandlerOptions) tx.Handler { + return authmiddleware.ComposeMiddlewares( + authmiddleware.NewRunMsgsTxHandler(options.MsgServiceRouter, options.LegacyRouter), + authmiddleware.NewTxDecoderMiddleware(options.TxDecoder), + NewTxRouterMiddleware(options), + ) +} + +func NewTxRouterMiddleware(options HandlerOptions) tx.Middleware { + ethMiddleware := newEthAuthMiddleware(options) + cosmoseip712 := newCosmosMiddlewareEip712(options) + cosmosMiddleware := newCosmosAuthMiddleware(options) + return func(txh tx.Handler) tx.Handler { + return txRouter{ + eth: ethMiddleware(txh), + cosmos: cosmosMiddleware(txh), + eip712: cosmoseip712(txh), + } } - cosmoseip712, err := newCosmosAnteHandlerEip712(options) - if err != nil { - return nil, err - } - cosmosMiddleware, err := newCosmosAuthMiddleware(options) - if err != nil { - return nil, err - } - return MD{ - ethMiddleware: ethMiddleware, - cosmosMiddleware: cosmosMiddleware, - cosmoseip712: cosmoseip712, - }, nil } // CheckTx implements tx.Handler -func (md MD) CheckTx(ctx context.Context, req tx.Request, checkReq tx.RequestCheckTx) (tx.Response, tx.ResponseCheckTx, error) { - var anteHandler tx.Handler - reqTx := req.Tx - txWithExtensions, ok := reqTx.(authante.HasExtensionOptionsTx) +func (txh txRouter) route(req tx.Request) (tx.Handler, error) { + txWithExtensions, ok := req.Tx.(authmiddleware.HasExtensionOptionsTx) if ok { opts := txWithExtensions.GetExtensionOptions() if len(opts) > 0 { + var next tx.Handler switch typeURL := opts[0].GetTypeUrl(); typeURL { case "/ethermint.evm.v1.ExtensionOptionsEthereumTx": // handle as *evmtypes.MsgEthereumTx - anteHandler = md.ethMiddleware + next = txh.eth case "/ethermint.types.v1.ExtensionOptionsWeb3Tx": // handle as normal Cosmos SDK tx, except signature is checked for EIP712 representation - anteHandler = md.cosmoseip712 + next = txh.eip712 default: - return tx.Response{}, tx.ResponseCheckTx{}, sdkerrors.Wrapf( + return nil, sdkerrors.Wrapf( sdkerrors.ErrUnknownExtensionOptions, "rejecting tx with unsupported extension option: %s", typeURL, ) } - - return anteHandler.CheckTx(ctx, req, checkReq) + return next, nil } } - // // handle as totally normal Cosmos SDK tx - // _, ok = reqTx.(sdk.Tx) - // if !ok { - // return tx.Response{}, tx.ResponseCheckTx{}, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", reqTx) + // if _, ok = reqTx.(sdk.Tx); !ok { + // return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", reqTx) // } + return txh.cosmos, nil +} - return md.cosmosMiddleware.CheckTx(ctx, req, checkReq) +// CheckTx implements tx.Handler +func (txh txRouter) CheckTx(ctx context.Context, req tx.Request, checkReq tx.RequestCheckTx) (res tx.Response, rct tx.ResponseCheckTx, err error) { + next, err := txh.route(req) + if err != nil { + return + } + return next.CheckTx(ctx, req, checkReq) } // DeliverTx implements tx.Handler -func (md MD) DeliverTx(ctx context.Context, req tx.Request) (tx.Response, error) { - var anteHandler tx.Handler - reqTx := req.Tx - txWithExtensions, ok := reqTx.(authante.HasExtensionOptionsTx) - if ok { - opts := txWithExtensions.GetExtensionOptions() - if len(opts) > 0 { - switch typeURL := opts[0].GetTypeUrl(); typeURL { - case "/ethermint.evm.v1.ExtensionOptionsEthereumTx": - // handle as *evmtypes.MsgEthereumTx - anteHandler = md.ethMiddleware - case "/ethermint.types.v1.ExtensionOptionsWeb3Tx": - // handle as normal Cosmos SDK tx, except signature is checked for EIP712 representation - anteHandler = md.cosmoseip712 - default: - return tx.Response{}, sdkerrors.Wrapf( - sdkerrors.ErrUnknownExtensionOptions, - "rejecting tx with unsupported extension option: %s", typeURL, - ) - } - - return anteHandler.DeliverTx(ctx, req) - } +func (txh txRouter) DeliverTx(ctx context.Context, req tx.Request) (res tx.Response, err error) { + next, err := txh.route(req) + if err != nil { + return } - - return md.cosmosMiddleware.DeliverTx(ctx, req) + return next.DeliverTx(ctx, req) } // SimulateTx implements tx.Handler -func (md MD) SimulateTx(ctx context.Context, req tx.Request) (tx.Response, error) { - var anteHandler tx.Handler - reqTx := req.Tx - txWithExtensions, ok := reqTx.(authante.HasExtensionOptionsTx) - if ok { - opts := txWithExtensions.GetExtensionOptions() - if len(opts) > 0 { - switch typeURL := opts[0].GetTypeUrl(); typeURL { - case "/ethermint.evm.v1.ExtensionOptionsEthereumTx": - // handle as *evmtypes.MsgEthereumTx - anteHandler = md.ethMiddleware - case "/ethermint.types.v1.ExtensionOptionsWeb3Tx": - // handle as normal Cosmos SDK tx, except signature is checked for EIP712 representation - anteHandler = md.cosmoseip712 - default: - return tx.Response{}, sdkerrors.Wrapf( - sdkerrors.ErrUnknownExtensionOptions, - "rejecting tx with unsupported extension option: %s", typeURL, - ) - } - - return anteHandler.SimulateTx(ctx, req) - } +func (txh txRouter) SimulateTx(ctx context.Context, req tx.Request) (res tx.Response, err error) { + next, err := txh.route(req) + if err != nil { + return } - - return md.cosmosMiddleware.SimulateTx(ctx, req) + return next.SimulateTx(ctx, req) } -var _ authante.SignatureVerificationGasConsumer = DefaultSigVerificationGasConsumer +var _ authmiddleware.SignatureVerificationGasConsumer = DefaultSigVerificationGasConsumer // DefaultSigVerificationGasConsumer is the default implementation of SignatureVerificationGasConsumer. It consumes gas // for signature verification based upon the public key type. The cost is fetched from the given params and is matched @@ -152,5 +115,5 @@ func DefaultSigVerificationGasConsumer( return nil } - return authante.DefaultSigVerificationGasConsumer(meter, sig, params) + return authmiddleware.DefaultSigVerificationGasConsumer(meter, sig, params) } diff --git a/app/middleware/options.go b/app/middleware/options.go index fd64a789..a6fe6da2 100644 --- a/app/middleware/options.go +++ b/app/middleware/options.go @@ -13,7 +13,7 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) -// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC +// HandlerOptions extend the SDK's TxHandler options by requiring the IBC // channel keeper, EVM Keeper and Fee Market Keeper. type HandlerOptions struct { Debug bool @@ -50,26 +50,25 @@ func (options HandlerOptions) Validate() error { return sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for middlewares") } if options.AccountKeeper == nil { - return sdkerrors.Wrap(sdkerrors.ErrLogic, "account keeper is required for AnteHandler") + return sdkerrors.Wrap(sdkerrors.ErrLogic, "account keeper is required for middlewares") } if options.BankKeeper == nil { - return sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for AnteHandler") + return sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for middlewares") } if options.SignModeHandler == nil { - return sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder") + return sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for middlewares") } if options.FeeMarketKeeper == nil { - return sdkerrors.Wrap(sdkerrors.ErrLogic, "fee market keeper is required for AnteHandler") + return sdkerrors.Wrap(sdkerrors.ErrLogic, "fee market keeper is required for middlewares") } if options.EvmKeeper == nil { - return sdkerrors.Wrap(sdkerrors.ErrLogic, "evm keeper is required for AnteHandler") + return sdkerrors.Wrap(sdkerrors.ErrLogic, "evm keeper is required for middlewares") } return nil } -func newEthAuthMiddleware(options HandlerOptions) (tx.Handler, error) { - return authmiddleware.ComposeMiddlewares( - authmiddleware.NewRunMsgsTxHandler(options.MsgServiceRouter, options.LegacyRouter), +func newEthAuthMiddleware(options HandlerOptions) tx.Middleware { + stack := []tx.Middleware{ NewEthSetUpContextMiddleware(options.EvmKeeper), NewEthMempoolFeeMiddleware(options.EvmKeeper), NewEthValidateBasicMiddleware(options.EvmKeeper), @@ -78,13 +77,14 @@ func newEthAuthMiddleware(options HandlerOptions) (tx.Handler, error) { NewEthGasConsumeMiddleware(options.EvmKeeper, options.MaxTxGasWanted), NewCanTransferMiddleware(options.EvmKeeper), NewEthIncrementSenderSequenceMiddleware(options.AccountKeeper), - ), nil + } + return func(txh tx.Handler) tx.Handler { + return authmiddleware.ComposeMiddlewares(txh, stack...) + } } -func newCosmosAuthMiddleware(options HandlerOptions) (tx.Handler, error) { - return authmiddleware.ComposeMiddlewares( - authmiddleware.NewRunMsgsTxHandler(options.MsgServiceRouter, options.LegacyRouter), - authmiddleware.NewTxDecoderMiddleware(options.TxDecoder), +func newCosmosAuthMiddleware(options HandlerOptions) tx.Middleware { + stack := []tx.Middleware{ NewRejectMessagesMiddleware, // Set a new GasMeter on sdk.Context. // @@ -123,15 +123,15 @@ func newCosmosAuthMiddleware(options HandlerOptions) (tx.Handler, error) { // should be accounted for, should go below this middleware. authmiddleware.ConsumeBlockGasMiddleware, authmiddleware.NewTipMiddleware(options.BankKeeper), - ), nil + } + return func(txh tx.Handler) tx.Handler { + return authmiddleware.ComposeMiddlewares(txh, stack...) + } } -func newCosmosAnteHandlerEip712(options HandlerOptions) (tx.Handler, error) { - - return authmiddleware.ComposeMiddlewares( - authmiddleware.NewRunMsgsTxHandler(options.MsgServiceRouter, options.LegacyRouter), +func newCosmosMiddlewareEip712(options HandlerOptions) tx.Middleware { + stack := []tx.Middleware{ NewRejectMessagesMiddleware, - authmiddleware.NewTxDecoderMiddleware(options.TxDecoder), // Set a new GasMeter on sdk.Context. // // Make sure the Gas middleware is outside of all other middlewares @@ -170,5 +170,8 @@ func newCosmosAnteHandlerEip712(options HandlerOptions) (tx.Handler, error) { // should be accounted for, should go below this middleware. authmiddleware.ConsumeBlockGasMiddleware, authmiddleware.NewTipMiddleware(options.BankKeeper), - ), nil + } + return func(txh tx.Handler) tx.Handler { + return authmiddleware.ComposeMiddlewares(txh, stack...) + } } diff --git a/app/middleware/utils_test.go b/app/middleware/utils_test.go index ca90436b..923405b5 100644 --- a/app/middleware/utils_test.go +++ b/app/middleware/utils_test.go @@ -13,7 +13,7 @@ import ( types3 "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/ethereum/go-ethereum/crypto" "github.com/spf13/cast" - ante "github.com/tharsis/ethermint/app/middleware" + "github.com/tharsis/ethermint/app/middleware" "github.com/tharsis/ethermint/ethereum/eip712" "github.com/tharsis/ethermint/server/config" "github.com/tharsis/ethermint/types" @@ -123,7 +123,7 @@ func (suite *MiddlewareTestSuite) SetupTest() { suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig) maxGasWanted := cast.ToUint64(config.DefaultMaxTxGasWanted) - options := ante.HandlerOptions{ + options := middleware.HandlerOptions{ TxDecoder: suite.clientCtx.TxConfig.TxDecoder(), AccountKeeper: suite.app.AccountKeeper, BankKeeper: suite.app.BankKeeper, @@ -135,14 +135,12 @@ func (suite *MiddlewareTestSuite) SetupTest() { MsgServiceRouter: suite.app.MsgSvcRouter, FeeMarketKeeper: suite.app.FeeMarketKeeper, SignModeHandler: suite.clientCtx.TxConfig.SignModeHandler(), - SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + SigGasConsumer: middleware.DefaultSigVerificationGasConsumer, MaxTxGasWanted: maxGasWanted, } suite.Require().NoError(options.Validate()) - middleware, err := ante.NewMiddleware(options) - suite.Require().NoError(err) - suite.anteHandler = middleware + suite.anteHandler = middleware.NewTxHandler(options) suite.ethSigner = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID()) } From 00814e27144b465c98a4f7e63a9840cb91379ae0 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Thu, 12 May 2022 19:45:01 +0800 Subject: [PATCH 02/11] TxExtensionOptionI for ExtensionOptionsEthereumTx --- app/app.go | 2 ++ x/evm/types/codec.go | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/app/app.go b/app/app.go index 4bb8121b..f6a358f4 100644 --- a/app/app.go +++ b/app/app.go @@ -354,6 +354,8 @@ func NewEthermintApp( ) bApp.SetCommitMultiStoreTracer(traceStore) bApp.SetVersion(version.Version) + + evmtypes.RegisterInterfaces(interfaceRegistry) bApp.SetInterfaceRegistry(interfaceRegistry) // configure state listening capabilities using AppOptions diff --git a/x/evm/types/codec.go b/x/evm/types/codec.go index 4452bac4..b44db514 100644 --- a/x/evm/types/codec.go +++ b/x/evm/types/codec.go @@ -6,6 +6,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/msgservice" + "github.com/cosmos/cosmos-sdk/types/tx" proto "github.com/gogo/protobuf/proto" ) @@ -26,6 +27,10 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { (*ExtensionOptionsEthereumTxI)(nil), &ExtensionOptionsEthereumTx{}, ) + registry.RegisterImplementations( + (*tx.TxExtensionOptionI)(nil), + &ExtensionOptionsEthereumTx{}, + ) registry.RegisterInterface( "ethermint.evm.v1.TxData", (*TxData)(nil), From 264aa4549231b73b547f98f88fae8c689f6c7c97 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Thu, 12 May 2022 20:26:04 +0800 Subject: [PATCH 03/11] cleanup --- app/app.go | 3 ++- app/middleware/eth.go | 2 +- scripts/integration-test-all.sh | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/app.go b/app/app.go index f6a358f4..1db4865c 100644 --- a/app/app.go +++ b/app/app.go @@ -29,6 +29,7 @@ import ( simappparams "github.com/cosmos/cosmos-sdk/simapp/params" "github.com/cosmos/cosmos-sdk/store/streaming" + storetypes "github.com/cosmos/cosmos-sdk/store/v2alpha1" "github.com/cosmos/cosmos-sdk/store/v2alpha1/multi" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" @@ -88,6 +89,7 @@ import ( upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client" upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper" upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" tmos "github.com/tendermint/tendermint/libs/os" @@ -113,7 +115,6 @@ import ( "github.com/tharsis/ethermint/x/evm" // evmrest "github.com/tharsis/ethermint/x/evm/client/rest" - storetypes "github.com/cosmos/cosmos-sdk/store/v2alpha1" evmkeeper "github.com/tharsis/ethermint/x/evm/keeper" "github.com/tharsis/ethermint/x/feemarket" feemarketkeeper "github.com/tharsis/ethermint/x/feemarket/keeper" diff --git a/app/middleware/eth.go b/app/middleware/eth.go index 951b6842..fecb0585 100644 --- a/app/middleware/eth.go +++ b/app/middleware/eth.go @@ -180,7 +180,7 @@ func (vbd EthValidateBasicMiddleware) CheckTx(cx context.Context, req tx.Request return tx.Response{}, tx.ResponseCheckTx{}, sdkerrors.Wrap(err, "tx basic validation failed") } - // For eth type cosmos tx, some fields should be veified as zero values, + // For eth type cosmos tx, some fields should be verified as zero values, // since we will only verify the signature against the hash of the MsgEthereumTx.Data if wrapperTx, ok := reqTx.(protoTxProvider); ok { protoTx := wrapperTx.GetProtoTx() diff --git a/scripts/integration-test-all.sh b/scripts/integration-test-all.sh index 5e71ded1..b8074073 100755 --- a/scripts/integration-test-all.sh +++ b/scripts/integration-test-all.sh @@ -32,6 +32,7 @@ usage() { echo "-q -- Quantity of nodes to run. eg: 3" echo "-z -- Quantity of nodes to run tests against eg: 3" echo "-s -- Sleep between operations in secs. eg: 5" + echo "-m -- Mode for testing. eg: rpc" echo "-r -- Remove test dir after, eg: true, default is false" exit 1 } From a7aff81d32da71eb691d5b52903a63c4ccdf90b8 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Fri, 13 May 2022 00:38:19 +0800 Subject: [PATCH 04/11] fix gas limit panic --- app/middleware/eth.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/middleware/eth.go b/app/middleware/eth.go index fecb0585..bdd42e1c 100644 --- a/app/middleware/eth.go +++ b/app/middleware/eth.go @@ -46,7 +46,7 @@ func setGasMeter(ctx sdk.Context, gasLimit uint64, simulate bool) sdk.Context { return ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) } - return ctx.WithGasMeter(sdk.NewGasMeter(gasLimit)) + return ctx.WithGasMeter(ethermint.NewInfiniteGasMeterWithLimit(gasLimit)) } // CheckTx implements tx.Handler From 9de27fb24f9a8c92b307b4c55e96abb860b3d26c Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Fri, 13 May 2022 17:53:31 +0800 Subject: [PATCH 05/11] test script cleanup --- scripts/integration-test-all.sh | 36 ++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/scripts/integration-test-all.sh b/scripts/integration-test-all.sh index b8074073..3c729f88 100755 --- a/scripts/integration-test-all.sh +++ b/scripts/integration-test-all.sh @@ -17,10 +17,10 @@ RPC_PORT="854" IP_ADDR="0.0.0.0" KEY="mykey" -CHAINID="ethermint_9000-1" +CHAINID="chibaclonk_9000-1" MONIKER="mymoniker" -## default port prefixes for ethermintd +## default port prefixes for chibaclonkd NODE_P2P_PORT="2660" NODE_PORT="2663" NODE_RPC_PORT="2666" @@ -55,6 +55,7 @@ set -euxo pipefail DATA_DIR=$(mktemp -d -t ethermint-datadir.XXXXX) +DATA_DIR=$(mktemp -d -t chibaclonk-datadir.XXXXX) if [[ ! "$DATA_DIR" ]]; then echo "Could not create $DATA_DIR" exit 1 @@ -105,17 +106,21 @@ init_func() { start_func() { echo "starting chibaclonk node $i in background ..." - "$PWD"/build/chibaclonkd start --pruning=nothing --rpc.unsafe \ - --p2p.laddr tcp://$IP_ADDR:$NODE_P2P_PORT"$i" --address tcp://$IP_ADDR:$NODE_PORT"$i" --rpc.laddr tcp://$IP_ADDR:$NODE_RPC_PORT"$i" \ - --json-rpc.address=$IP_ADDR:$RPC_PORT"$i" \ - --json-rpc.api="eth,txpool,personal,net,debug,web3" \ - --keyring-backend test --mode validator --home "$DATA_DIR$i" \ + "$PWD"/build/chibaclonkd start \ + --pruning=nothing --rpc.unsafe \ + --p2p.laddr tcp://$IP_ADDR:$NODE_P2P_PORT"$i" \ + --address tcp://$IP_ADDR:$NODE_PORT"$i" \ + --rpc.laddr tcp://$IP_ADDR:$NODE_RPC_PORT"$i" \ + --json-rpc.address=$IP_ADDR:$RPC_PORT"$i" \ + --json-rpc.api="eth,txpool,personal,net,debug,web3" \ + --keyring-backend test --mode validator --home "$DATA_DIR$i" \ + --log_level debug \ >"$DATA_DIR"/node"$i".log 2>&1 & disown - ETHERMINT_PID=$! - echo "started chibaclonk node, pid=$ETHERMINT_PID" + CHIBACLONK_PID=$! + echo "started chibaclonk node, pid=$CHIBACLONK_PID" # add PID to array - arr+=("$ETHERMINT_PID") + arr+=("$CHIBACLONK_PID") if [[ $MODE == "pending" ]]; then echo "waiting for the first block..." @@ -164,16 +169,15 @@ if [[ -z $TEST || $TEST == "rpc" || $TEST == "pending" ]]; then TEST_FAIL=$? done - fi stop_func() { - ETHERMINT_PID=$i - echo "shutting down node, pid=$ETHERMINT_PID ..." + CHIBACLONK_PID=$i + echo "shutting down node, pid=$CHIBACLONK_PID ..." - # Shutdown ethermint node - kill -9 "$ETHERMINT_PID" - wait "$ETHERMINT_PID" + # Shutdown chibaclonk node + kill -9 "$CHIBACLONK_PID" + wait "$CHIBACLONK_PID" if [ $REMOVE_DATA_DIR == "true" ] then From 4040c3425fc68c2b370db6d6527510b8cf9e9c69 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Fri, 13 May 2022 18:48:39 +0800 Subject: [PATCH 06/11] fix build flags for debugging --- Makefile | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 4a1ee042..f344e753 100755 --- a/Makefile +++ b/Makefile @@ -88,23 +88,22 @@ ifeq (boltdb,$(findstring boltdb,$(COSMOS_BUILD_OPTIONS))) ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=boltdb endif +BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags)' + +# Check for debug option +ifeq (debug,$(findstring debug,$(COSMOS_BUILD_OPTIONS))) + BUILD_FLAGS += -gcflags 'all=-N -l' + COSMOS_BUILD_OPTIONS += nostrip +endif + +# check for nostrip option ifeq (,$(findstring nostrip,$(COSMOS_BUILD_OPTIONS))) + BUILD_FLAGS += -trimpath ldflags += -w -s endif ldflags += $(LDFLAGS) ldflags := $(strip $(ldflags)) -BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags)' -# check for nostrip option -ifeq (,$(findstring nostrip,$(COSMOS_BUILD_OPTIONS))) - BUILD_FLAGS += -trimpath -endif - -# Check for debug option -ifeq (debug,$(findstring debug,$(COSMOS_BUILD_OPTIONS))) - BUILD_FLAGS += -gcflags "all=-N -l" -endif - all: tools build lint test # # The below include contains the tools and runsim targets. # include contrib/devtools/Makefile From 68187bc75b28306bbae079841e58ee03afedd157 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Fri, 13 May 2022 18:51:50 +0800 Subject: [PATCH 07/11] misc cleanup --- tests/rpc/rpc_test.go | 13 +------------ types/gasmeter.go | 2 +- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/tests/rpc/rpc_test.go b/tests/rpc/rpc_test.go index 302c3513..9af82ceb 100644 --- a/tests/rpc/rpc_test.go +++ b/tests/rpc/rpc_test.go @@ -301,17 +301,6 @@ func deployTestContract(t *testing.T) (hexutil.Bytes, map[string]interface{}) { return hash, receipt } -func getTransactionReceipt(t *testing.T, hash hexutil.Bytes) map[string]interface{} { - param := []string{hash.String()} - rpcRes := call(t, "eth_getTransactionReceipt", param) - - receipt := make(map[string]interface{}) - err := json.Unmarshal(rpcRes.Result, &receipt) - require.NoError(t, err) - - return receipt -} - func waitForReceipt(t *testing.T, hash hexutil.Bytes) map[string]interface{} { timeout := time.After(12 * time.Second) ticker := time.Tick(500 * time.Millisecond) @@ -321,7 +310,7 @@ func waitForReceipt(t *testing.T, hash hexutil.Bytes) map[string]interface{} { case <-timeout: return nil case <-ticker: - receipt := getTransactionReceipt(t, hash) + receipt := GetTransactionReceipt(t, hash) if receipt != nil { return receipt } diff --git a/types/gasmeter.go b/types/gasmeter.go index 751de082..65c29c62 100644 --- a/types/gasmeter.go +++ b/types/gasmeter.go @@ -75,7 +75,7 @@ func (g *infiniteGasMeterWithLimit) ConsumeGas(amount sdk.Gas, descriptor string // RefundGas will deduct the given amount from the gas consumed. If the amount is greater than the // gas consumed, the function will panic. // -// Use case: This functionality enables refunding gas to the trasaction or block gas pools so that +// Use case: This functionality enables refunding gas to the transaction or block gas pools so that // EVM-compatible chains can fully support the go-ethereum StateDb interface. // See https://github.com/cosmos/cosmos-sdk/pull/9403 for reference. func (g *infiniteGasMeterWithLimit) RefundGas(amount sdk.Gas, descriptor string) { From 54e49b83e7eced19a820ea0b7bb2939781f17491 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Sat, 14 May 2022 03:02:24 +0800 Subject: [PATCH 08/11] ethereum needs "index events middleware" --- app/middleware/options.go | 1 + 1 file changed, 1 insertion(+) diff --git a/app/middleware/options.go b/app/middleware/options.go index a6fe6da2..6a410627 100644 --- a/app/middleware/options.go +++ b/app/middleware/options.go @@ -71,6 +71,7 @@ func newEthAuthMiddleware(options HandlerOptions) tx.Middleware { stack := []tx.Middleware{ NewEthSetUpContextMiddleware(options.EvmKeeper), NewEthMempoolFeeMiddleware(options.EvmKeeper), + authmiddleware.NewIndexEventsTxMiddleware(options.IndexEvents), NewEthValidateBasicMiddleware(options.EvmKeeper), NewEthSigVerificationMiddleware(options.EvmKeeper), NewEthAccountVerificationMiddleware(options.AccountKeeper, options.BankKeeper, options.EvmKeeper), From bfb702009a6434f657500262a1f71e3d4acfe80d Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Mon, 16 May 2022 22:14:11 +0800 Subject: [PATCH 09/11] fix tx response decoding --- rpc/ethereum/namespaces/eth/filters/api.go | 4 +-- rpc/websockets.go | 2 +- x/evm/types/codec.go | 20 +++++++----- x/evm/types/utils.go | 36 +++++++++------------- x/evm/types/utils_test.go | 33 ++++++++++++-------- 5 files changed, 50 insertions(+), 45 deletions(-) diff --git a/rpc/ethereum/namespaces/eth/filters/api.go b/rpc/ethereum/namespaces/eth/filters/api.go index 00d6defe..9bd9eafb 100644 --- a/rpc/ethereum/namespaces/eth/filters/api.go +++ b/rpc/ethereum/namespaces/eth/filters/api.go @@ -391,7 +391,7 @@ func (api *PublicFilterAPI) Logs(ctx context.Context, crit filters.FilterCriteri continue } - txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data) + txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data, api.clientCtx.Codec) if err != nil { return } @@ -467,7 +467,7 @@ func (api *PublicFilterAPI) NewFilter(criteria filters.FilterCriteria) (rpc.ID, continue } - txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data) + txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data, api.clientCtx.Codec) if err != nil { return } diff --git a/rpc/websockets.go b/rpc/websockets.go index c23f5944..e2d76abc 100644 --- a/rpc/websockets.go +++ b/rpc/websockets.go @@ -536,7 +536,7 @@ func (api *pubSubAPI) subscribeLogs(wsConn *wsConn, subID rpc.ID, extra interfac continue } - txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data) + txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data, api.clientCtx.Codec) if err != nil { api.logger.Error("failed to decode tx response", "error", err.Error()) return diff --git a/x/evm/types/codec.go b/x/evm/types/codec.go index b44db514..793dd29a 100644 --- a/x/evm/types/codec.go +++ b/x/evm/types/codec.go @@ -1,7 +1,6 @@ package types import ( - "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -10,9 +9,8 @@ import ( proto "github.com/gogo/protobuf/proto" ) -var ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) - type ( + TxResponse interface{} ExtensionOptionsEthereumTxI interface{} ) @@ -22,15 +20,11 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { (*sdk.Msg)(nil), &MsgEthereumTx{}, ) - registry.RegisterInterface( - "ethermint.evm.v1.ExtensionOptionsEthereumTx", - (*ExtensionOptionsEthereumTxI)(nil), - &ExtensionOptionsEthereumTx{}, - ) registry.RegisterImplementations( (*tx.TxExtensionOptionI)(nil), &ExtensionOptionsEthereumTx{}, ) + registry.RegisterInterface( "ethermint.evm.v1.TxData", (*TxData)(nil), @@ -38,6 +32,16 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { &AccessListTx{}, &LegacyTx{}, ) + registry.RegisterInterface( + "ethermint.evm.v1.MsgEthereumTxResponse", + (*TxResponse)(nil), + &MsgEthereumTxResponse{}, + ) + registry.RegisterInterface( + "ethermint.evm.v1.ExtensionOptionsEthereumTx", + (*ExtensionOptionsEthereumTxI)(nil), + &ExtensionOptionsEthereumTx{}, + ) msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } diff --git a/x/evm/types/utils.go b/x/evm/types/utils.go index 16b1c780..5df470b9 100644 --- a/x/evm/types/utils.go +++ b/x/evm/types/utils.go @@ -6,8 +6,8 @@ import ( "github.com/gogo/protobuf/proto" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -18,25 +18,27 @@ const maxBitLen = 256 var EmptyCodeHash = crypto.Keccak256(nil) // DecodeTxResponse decodes an protobuf-encoded byte slice into TxResponse -func DecodeTxResponse(in []byte) (*MsgEthereumTxResponse, error) { +func DecodeTxResponse(in []byte, cdc codec.Codec) (*MsgEthereumTxResponse, error) { var txMsgData sdk.TxMsgData - if err := proto.Unmarshal(in, &txMsgData); err != nil { + if err := txMsgData.Unmarshal(in); err != nil { return nil, err } - data := txMsgData.GetData() - if len(data) == 0 { - return &MsgEthereumTxResponse{}, nil + responses := txMsgData.GetMsgResponses() + if len(responses) == 0 { + return nil, nil } - var res MsgEthereumTxResponse - - err := proto.Unmarshal(data[0].GetData(), &res) - if err != nil { - return nil, sdkerrors.Wrap(err, "failed to unmarshal tx response message data") + if err := cdc.UnpackAny(responses[0], new(TxResponse)); err != nil { + return nil, fmt.Errorf("failed to unmarshal tx response message: %w", err) } - return &res, nil + msgval := responses[0].GetCachedValue() + res, ok := msgval.(*MsgEthereumTxResponse) + if !ok { + return nil, fmt.Errorf("tx response message has invalid type: %T", msgval) + } + return res, nil } // EncodeTransactionLogs encodes TransactionLogs slice into a protobuf-encoded byte slice. @@ -44,16 +46,6 @@ func EncodeTransactionLogs(res *TransactionLogs) ([]byte, error) { return proto.Marshal(res) } -// DecodeTxResponse decodes an protobuf-encoded byte slice into TransactionLogs -func DecodeTransactionLogs(data []byte) (TransactionLogs, error) { - var logs TransactionLogs - err := proto.Unmarshal(data, &logs) - if err != nil { - return TransactionLogs{}, err - } - return logs, nil -} - // UnwrapEthereumMsg extract MsgEthereumTx from wrapping sdk.Tx func UnwrapEthereumMsg(tx *sdk.Tx, ethHash common.Hash) (*MsgEthereumTx, error) { if tx == nil { diff --git a/x/evm/types/utils_test.go b/x/evm/types/utils_test.go index d3ec00e5..d48a3c27 100644 --- a/x/evm/types/utils_test.go +++ b/x/evm/types/utils_test.go @@ -5,23 +5,33 @@ import ( "math/big" "testing" + "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" - proto "github.com/gogo/protobuf/proto" + "github.com/tharsis/ethermint/app" "github.com/tharsis/ethermint/encoding" evmtypes "github.com/tharsis/ethermint/x/evm/types" - "github.com/stretchr/testify/require" - "github.com/ethereum/go-ethereum/common" ) +var testCodec codec.Codec + +func init() { + registry := codectypes.NewInterfaceRegistry() + evmtypes.RegisterInterfaces(registry) + testCodec = codec.NewProtoCodec(registry) +} + func TestEvmDataEncoding(t *testing.T) { ret := []byte{0x5, 0x8} - data := &evmtypes.MsgEthereumTxResponse{ + resp := &evmtypes.MsgEthereumTxResponse{ Hash: common.BytesToHash([]byte("hash")).String(), Logs: []*evmtypes.Log{{ Data: []byte{1, 2, 3, 4}, @@ -30,21 +40,20 @@ func TestEvmDataEncoding(t *testing.T) { Ret: ret, } - enc, err := proto.Marshal(data) + any, err := codectypes.NewAnyWithValue(resp) require.NoError(t, err) - txData := &sdk.TxMsgData{ - Data: []*sdk.MsgData{{MsgType: evmtypes.TypeMsgEthereumTx, Data: enc}}, + MsgResponses: []*codectypes.Any{any}, } - txDataBz, err := proto.Marshal(txData) + txDataBz, err := txData.Marshal() require.NoError(t, err) - res, err := evmtypes.DecodeTxResponse(txDataBz) + decoded, err := evmtypes.DecodeTxResponse(txDataBz, testCodec) require.NoError(t, err) - require.NotNil(t, res) - require.Equal(t, data.Logs, res.Logs) - require.Equal(t, ret, res.Ret) + require.NotNil(t, decoded) + require.Equal(t, resp.Logs, decoded.Logs) + require.Equal(t, ret, decoded.Ret) } func TestUnwrapEthererumMsg(t *testing.T) { From b1f755db7bc2f6ca2c0e523be24dd1ac148d7711 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Mon, 16 May 2022 22:14:58 +0800 Subject: [PATCH 10/11] fix tx event query --- rpc/ethereum/namespaces/eth/filters/filter_system.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rpc/ethereum/namespaces/eth/filters/filter_system.go b/rpc/ethereum/namespaces/eth/filters/filter_system.go index f75a0a53..89f17bb8 100644 --- a/rpc/ethereum/namespaces/eth/filters/filter_system.go +++ b/rpc/ethereum/namespaces/eth/filters/filter_system.go @@ -10,6 +10,7 @@ import ( tmjson "github.com/tendermint/tendermint/libs/json" "github.com/tendermint/tendermint/libs/log" + tmquery "github.com/tendermint/tendermint/libs/pubsub/query" coretypes "github.com/tendermint/tendermint/rpc/coretypes" rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client" tmtypes "github.com/tendermint/tendermint/types" @@ -27,7 +28,7 @@ import ( var ( txEvents = tmtypes.QueryForEvent(tmtypes.EventTxValue).String() - evmEvents = fmt.Sprintf("%s='%s' AND %s.%s='%s'", tmtypes.EventTypeKey, tmtypes.EventTx, sdk.EventTypeMessage, sdk.AttributeKeyModule, evmtypes.ModuleName) + evmEvents = tmquery.MustCompile(fmt.Sprintf("%s='%s' AND %s.%s='%s'", tmtypes.EventTypeKey, tmtypes.EventTxValue, sdk.EventTypeMessage, sdk.AttributeKeyModule, evmtypes.ModuleName)).String() headerEvents = tmtypes.QueryForEvent(tmtypes.EventNewBlockHeaderValue).String() ) From 2c9d18fd93009209fc4f3b9a9c2aa545c1a888d5 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Mon, 16 May 2022 22:15:28 +0800 Subject: [PATCH 11/11] misc clean up --- app/middleware/eip792.go | 9 --------- encoding/codec/codec.go | 7 +------ x/evm/keeper/msg_server.go | 3 +-- 3 files changed, 2 insertions(+), 17 deletions(-) diff --git a/app/middleware/eip792.go b/app/middleware/eip792.go index 9dcd92cd..d140a3f4 100644 --- a/app/middleware/eip792.go +++ b/app/middleware/eip792.go @@ -17,20 +17,11 @@ import ( "github.com/tharsis/ethermint/ethereum/eip712" ethermint "github.com/tharsis/ethermint/types" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" evmtypes "github.com/tharsis/ethermint/x/evm/types" ) -var ethermintCodec codec.ProtoCodecMarshaler - -func init() { - registry := codectypes.NewInterfaceRegistry() - ethermint.RegisterInterfaces(registry) - ethermintCodec = codec.NewProtoCodec(registry) -} - // Eip712SigVerificationMiddleware Verify all signatures for a tx and return an error if any are invalid. Note, // the Eip712SigVerificationMiddleware middleware will not get executed on ReCheck. // diff --git a/encoding/codec/codec.go b/encoding/codec/codec.go index 591f0318..6a6a9bdd 100644 --- a/encoding/codec/codec.go +++ b/encoding/codec/codec.go @@ -5,22 +5,17 @@ import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/std" - sdk "github.com/cosmos/cosmos-sdk/types" - cryptocodec "github.com/tharsis/ethermint/crypto/codec" ethermint "github.com/tharsis/ethermint/types" ) // RegisterLegacyAminoCodec registers Interfaces from types, crypto, and SDK std. func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - sdk.RegisterLegacyAminoCodec(cdc) - cryptocodec.RegisterCrypto(cdc) - codec.RegisterEvidences(cdc) + std.RegisterLegacyAminoCodec(cdc) } // RegisterInterfaces registers Interfaces from types, crypto, and SDK std. func RegisterInterfaces(interfaceRegistry codectypes.InterfaceRegistry) { - sdk.RegisterInterfaces(interfaceRegistry) std.RegisterInterfaces(interfaceRegistry) cryptocodec.RegisterInterfaces(interfaceRegistry) ethermint.RegisterInterfaces(interfaceRegistry) diff --git a/x/evm/keeper/msg_server.go b/x/evm/keeper/msg_server.go index 50c49afc..54ebb762 100644 --- a/x/evm/keeper/msg_server.go +++ b/x/evm/keeper/msg_server.go @@ -19,8 +19,7 @@ var _ types.MsgServer = &Keeper{} // EthereumTx implements the gRPC MsgServer interface. It receives a transaction which is then // executed (i.e applied) against the go-ethereum EVM. The provided SDK Context is set to the Keeper -// so that it can implements and call the StateDB methods without receiving it as a function -// parameter. +// so that it can call the StateDB methods without receiving it as a function parameter. func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*types.MsgEthereumTxResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx)