From bf7358bae637650f4d31ec51a460bdfe2ddfb5c2 Mon Sep 17 00:00:00 2001 From: Loredana Date: Mon, 4 Apr 2022 12:16:20 +0300 Subject: [PATCH 1/3] x/evm - PostTxProcessing use message as callback input --- x/evm/handler_test.go | 4 ++-- x/evm/keeper/hooks.go | 5 ++--- x/evm/keeper/hooks_test.go | 6 +++--- x/evm/keeper/keeper.go | 4 ++-- x/evm/keeper/state_transition.go | 2 +- x/evm/types/interfaces.go | 3 +-- 6 files changed, 11 insertions(+), 13 deletions(-) diff --git a/x/evm/handler_test.go b/x/evm/handler_test.go index e80791bea9..068d87d43f 100644 --- a/x/evm/handler_test.go +++ b/x/evm/handler_test.go @@ -679,13 +679,13 @@ func (suite *EvmTestSuite) TestContractDeploymentRevert() { // DummyHook implements EvmHooks interface type DummyHook struct{} -func (dh *DummyHook) PostTxProcessing(ctx sdk.Context, from common.Address, to *common.Address, receipt *ethtypes.Receipt) error { +func (dh *DummyHook) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error { return nil } // FailureHook implements EvmHooks interface type FailureHook struct{} -func (dh *FailureHook) PostTxProcessing(ctx sdk.Context, from common.Address, to *common.Address, receipt *ethtypes.Receipt) error { +func (dh *FailureHook) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error { return errors.New("mock error") } diff --git a/x/evm/keeper/hooks.go b/x/evm/keeper/hooks.go index 5bb0952e3b..8df2537209 100644 --- a/x/evm/keeper/hooks.go +++ b/x/evm/keeper/hooks.go @@ -3,7 +3,6 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/tharsis/ethermint/x/evm/types" ) @@ -19,9 +18,9 @@ func NewMultiEvmHooks(hooks ...types.EvmHooks) MultiEvmHooks { } // PostTxProcessing delegate the call to underlying hooks -func (mh MultiEvmHooks) PostTxProcessing(ctx sdk.Context, from common.Address, to *common.Address, receipt *ethtypes.Receipt) error { +func (mh MultiEvmHooks) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error { for i := range mh { - if err := mh[i].PostTxProcessing(ctx, from, to, receipt); err != nil { + if err := mh[i].PostTxProcessing(ctx, msg, receipt); err != nil { return sdkerrors.Wrapf(err, "EVM hook %T failed", mh[i]) } } diff --git a/x/evm/keeper/hooks_test.go b/x/evm/keeper/hooks_test.go index 3dc5a83e32..ed9b5902ff 100644 --- a/x/evm/keeper/hooks_test.go +++ b/x/evm/keeper/hooks_test.go @@ -18,7 +18,7 @@ type LogRecordHook struct { Logs []*ethtypes.Log } -func (dh *LogRecordHook) PostTxProcessing(ctx sdk.Context, from common.Address, to *common.Address, receipt *ethtypes.Receipt) error { +func (dh *LogRecordHook) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error { dh.Logs = receipt.Logs return nil } @@ -26,7 +26,7 @@ func (dh *LogRecordHook) PostTxProcessing(ctx sdk.Context, from common.Address, // FailureHook always fail type FailureHook struct{} -func (dh FailureHook) PostTxProcessing(ctx sdk.Context, from common.Address, to *common.Address, receipt *ethtypes.Receipt) error { +func (dh FailureHook) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error { return errors.New("post tx processing failed") } @@ -81,7 +81,7 @@ func (suite *KeeperTestSuite) TestEvmHooks() { TxHash: txHash, Logs: logs, } - result := k.PostTxProcessing(ctx, common.Address{}, nil, receipt) + result := k.PostTxProcessing(ctx, ethtypes.Message{}, receipt) tc.expFunc(hook, result) } diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index 13010e0ee8..c8bc03f2e5 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -221,11 +221,11 @@ func (k *Keeper) SetHooks(eh types.EvmHooks) *Keeper { } // PostTxProcessing delegate the call to the hooks. If no hook has been registered, this function returns with a `nil` error -func (k *Keeper) PostTxProcessing(ctx sdk.Context, from common.Address, to *common.Address, receipt *ethtypes.Receipt) error { +func (k *Keeper) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error { if k.hooks == nil { return nil } - return k.hooks.PostTxProcessing(ctx, from, to, receipt) + return k.hooks.PostTxProcessing(ctx, msg, receipt) } // Tracer return a default vm.Tracer based on current keeper state diff --git a/x/evm/keeper/state_transition.go b/x/evm/keeper/state_transition.go index fb9a082e50..310bd33659 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -264,7 +264,7 @@ func (k *Keeper) ApplyTransaction(ctx sdk.Context, tx *ethtypes.Transaction) (*t } // Only call hooks if tx executed successfully. - if err = k.PostTxProcessing(tmpCtx, msg.From(), tx.To(), receipt); err != nil { + if err = k.PostTxProcessing(tmpCtx, msg, receipt); err != nil { // If hooks return error, revert the whole tx. res.VmError = types.ErrPostTxProcessing.Error() k.Logger(ctx).Error("tx post processing failed", "error", err) diff --git a/x/evm/types/interfaces.go b/x/evm/types/interfaces.go index 9664cbf4be..9151f1169e 100644 --- a/x/evm/types/interfaces.go +++ b/x/evm/types/interfaces.go @@ -6,7 +6,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" feemarkettypes "github.com/tharsis/ethermint/x/feemarket/types" @@ -53,5 +52,5 @@ type FeeMarketKeeper interface { // EvmHooks event hooks for evm tx processing type EvmHooks interface { // Must be called after tx is processed successfully, if return an error, the whole transaction is reverted. - PostTxProcessing(ctx sdk.Context, from common.Address, to *common.Address, receipt *ethtypes.Receipt) error + PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error } From c8630e14a1542e18f27d67bfaa4ce0d570414e88 Mon Sep 17 00:00:00 2001 From: Loredana Date: Mon, 4 Apr 2022 18:47:09 +0300 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3bbf38f9c..ef63ba0da1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking +* (evm) [tharsis#1027](https://github.com/tharsis/ethermint/pull/1027) Change the `PostTxProcessing` hook interface to include the full message data. * (feemarket) [tharsis#1026](https://github.com/tharsis/ethermint/pull/1026) Fix REST endpoints to use `/ethermint/feemarket/*` instead of `/feemarket/evm/*`. ### Bug Fixes From 3fc39073c9092797fe2afdb452ddbe7d79835dfc Mon Sep 17 00:00:00 2001 From: Loredana Date: Mon, 4 Apr 2022 20:19:41 +0300 Subject: [PATCH 3/3] Use core.Message for the PostTxProcessing hook (PR review) --- x/evm/handler_test.go | 5 +++-- x/evm/keeper/hooks.go | 3 ++- x/evm/keeper/hooks_test.go | 5 +++-- x/evm/keeper/keeper.go | 2 +- x/evm/types/interfaces.go | 3 ++- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/x/evm/handler_test.go b/x/evm/handler_test.go index 068d87d43f..4839de45ce 100644 --- a/x/evm/handler_test.go +++ b/x/evm/handler_test.go @@ -18,6 +18,7 @@ import ( stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/require" @@ -679,13 +680,13 @@ func (suite *EvmTestSuite) TestContractDeploymentRevert() { // DummyHook implements EvmHooks interface type DummyHook struct{} -func (dh *DummyHook) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error { +func (dh *DummyHook) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error { return nil } // FailureHook implements EvmHooks interface type FailureHook struct{} -func (dh *FailureHook) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error { +func (dh *FailureHook) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error { return errors.New("mock error") } diff --git a/x/evm/keeper/hooks.go b/x/evm/keeper/hooks.go index 8df2537209..6499655574 100644 --- a/x/evm/keeper/hooks.go +++ b/x/evm/keeper/hooks.go @@ -3,6 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/ethereum/go-ethereum/core" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/tharsis/ethermint/x/evm/types" ) @@ -18,7 +19,7 @@ func NewMultiEvmHooks(hooks ...types.EvmHooks) MultiEvmHooks { } // PostTxProcessing delegate the call to underlying hooks -func (mh MultiEvmHooks) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error { +func (mh MultiEvmHooks) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error { for i := range mh { if err := mh[i].PostTxProcessing(ctx, msg, receipt); err != nil { return sdkerrors.Wrapf(err, "EVM hook %T failed", mh[i]) diff --git a/x/evm/keeper/hooks_test.go b/x/evm/keeper/hooks_test.go index ed9b5902ff..b471f13a2e 100644 --- a/x/evm/keeper/hooks_test.go +++ b/x/evm/keeper/hooks_test.go @@ -6,6 +6,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/tharsis/ethermint/x/evm/keeper" @@ -18,7 +19,7 @@ type LogRecordHook struct { Logs []*ethtypes.Log } -func (dh *LogRecordHook) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error { +func (dh *LogRecordHook) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error { dh.Logs = receipt.Logs return nil } @@ -26,7 +27,7 @@ func (dh *LogRecordHook) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, // FailureHook always fail type FailureHook struct{} -func (dh FailureHook) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error { +func (dh FailureHook) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error { return errors.New("post tx processing failed") } diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index c8bc03f2e5..ca112bc97d 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -221,7 +221,7 @@ func (k *Keeper) SetHooks(eh types.EvmHooks) *Keeper { } // PostTxProcessing delegate the call to the hooks. If no hook has been registered, this function returns with a `nil` error -func (k *Keeper) PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error { +func (k *Keeper) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error { if k.hooks == nil { return nil } diff --git a/x/evm/types/interfaces.go b/x/evm/types/interfaces.go index 9151f1169e..9518c92d00 100644 --- a/x/evm/types/interfaces.go +++ b/x/evm/types/interfaces.go @@ -7,6 +7,7 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/ethereum/go-ethereum/core" ethtypes "github.com/ethereum/go-ethereum/core/types" feemarkettypes "github.com/tharsis/ethermint/x/feemarket/types" ) @@ -52,5 +53,5 @@ type FeeMarketKeeper interface { // EvmHooks event hooks for evm tx processing type EvmHooks interface { // Must be called after tx is processed successfully, if return an error, the whole transaction is reverted. - PostTxProcessing(ctx sdk.Context, msg ethtypes.Message, receipt *ethtypes.Receipt) error + PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error }