From 46ad912258b02812c9237bd4a4fa40d4866cc298 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Tue, 12 Sep 2023 11:42:17 +0800 Subject: [PATCH 1/3] Problem: no api to convert native events to logs Solution: - add event converters --- CHANGELOG.md | 1 + app/app.go | 1 + x/evm/keeper/keeper.go | 7 +++++++ x/evm/keeper/state_transition.go | 22 ++++++++++++++++++++++ 4 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e6195704f..882b293d78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ - (cli) [#242](https://github.com/crypto-org-chain/ethermint/pull/242) Integrate tendermint bootstrap cmd. - (cli) [#246](https://github.com/crypto-org-chain/ethermint/pull/246) Call app.Close to cleanup resource on graceful shutdown. * (cli) [#288](https://github.com/crypto-org-chain/ethermint/pull/288) make abci handshake shutdown gracefully. +- (evm) [#]() Add native event converter APIs. ## [v0.21.0] - 2023-01-26 diff --git a/app/app.go b/app/app.go index 32fe518b13..f5bfd907f5 100644 --- a/app/app.go +++ b/app/app.go @@ -472,6 +472,7 @@ func NewEthermintApp( tracer, evmSs, nil, allKeys, + nil, ) // Create IBC Keeper diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index abe4b55600..ffba366d6b 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -19,6 +19,7 @@ import ( "math/big" errorsmod "cosmossdk.io/errors" + abci "github.com/cometbft/cometbft/abci/types" "github.com/cometbft/cometbft/libs/log" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store/prefix" @@ -37,6 +38,8 @@ import ( "github.com/evmos/ethermint/x/evm/types" ) +type EventConverter = func([]abci.EventAttribute) []*ethtypes.Log + // Keeper grants access to the EVM module state and implements the go-ethereum StateDB interface. type Keeper struct { // Protobuf codec @@ -77,6 +80,8 @@ type Keeper struct { // a set of store keys that should cover all the precompile use cases, // or ideally just pass the application's all stores. keys map[string]storetypes.StoreKey + + eventConverters map[string]EventConverter } // NewKeeper generates new evm module keeper @@ -92,6 +97,7 @@ func NewKeeper( ss paramstypes.Subspace, customContracts []precompiles.StatefulPrecompiledContract, keys map[string]storetypes.StoreKey, + eventConverters map[string]EventConverter, ) *Keeper { // ensure evm module account is set if addr := ak.GetModuleAddress(types.ModuleName); addr == nil { @@ -117,6 +123,7 @@ func NewKeeper( ss: ss, customContracts: customContracts, keys: keys, + eventConverters: eventConverters, } } diff --git a/x/evm/keeper/state_transition.go b/x/evm/keeper/state_transition.go index 59d2992dee..ea833e244e 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -415,6 +415,9 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context, vmError = vmErr.Error() } + // convert native events to ethereum logs + convertNativeEvents(stateDB, k.eventConverters) + // The dirty states in `StateDB` is either committed or discarded after return if commit { if err := stateDB.Commit(); err != nil { @@ -445,3 +448,22 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context, Hash: txConfig.TxHash.Hex(), }, nil } + +func convertNativeEvents(stateDB *statedb.StateDB, converters map[string]EventConverter) { + if len(converters) == 0 { + return + } + + events := stateDB.NativeEvents() + if len(events) == 0 { + return + } + + for _, event := range events { + if converter, ok := converters[event.Type]; ok { + for _, log := range converter(event.Attributes) { + stateDB.AddLog(log) + } + } + } +} From 1b4d7252a879f443473f4b0acb0e545112a18985 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Tue, 12 Sep 2023 11:45:11 +0800 Subject: [PATCH 2/3] convert after commit --- x/evm/keeper/state_transition.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/evm/keeper/state_transition.go b/x/evm/keeper/state_transition.go index ea833e244e..3cf291cbfb 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -415,9 +415,6 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context, vmError = vmErr.Error() } - // convert native events to ethereum logs - convertNativeEvents(stateDB, k.eventConverters) - // The dirty states in `StateDB` is either committed or discarded after return if commit { if err := stateDB.Commit(); err != nil { @@ -425,6 +422,9 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context, } } + // convert native events to ethereum logs + convertNativeEvents(stateDB, k.eventConverters) + // calculate a minimum amount of gas to be charged to sender if GasLimit // is considerably higher than GasUsed to stay more aligned with Tendermint gas mechanics // for more info https://github.com/evmos/ethermint/issues/1085 From 398b136f6dac634d6c1e30aee766f944e25c0fb1 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Tue, 12 Sep 2023 12:02:31 +0800 Subject: [PATCH 3/3] Update CHANGELOG.md Signed-off-by: mmsqe --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 882b293d78..c22c1fdfaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,7 +68,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ - (cli) [#242](https://github.com/crypto-org-chain/ethermint/pull/242) Integrate tendermint bootstrap cmd. - (cli) [#246](https://github.com/crypto-org-chain/ethermint/pull/246) Call app.Close to cleanup resource on graceful shutdown. * (cli) [#288](https://github.com/crypto-org-chain/ethermint/pull/288) make abci handshake shutdown gracefully. -- (evm) [#]() Add native event converter APIs. +- (evm) [#343](https://github.com/crypto-org-chain/ethermint/pull/343) Add native event converter APIs. ## [v0.21.0] - 2023-01-26