From 81c398f2ed6b44ad4736e788bb227290e169de2f Mon Sep 17 00:00:00 2001 From: yihuang Date: Tue, 12 Sep 2023 12:34:01 +0800 Subject: [PATCH] Problem: no api to convert native events to logs (#343) * Problem: no api to convert native events to logs Solution: - add event converters * convert after commit * Update CHANGELOG.md Signed-off-by: mmsqe --------- Signed-off-by: mmsqe Co-authored-by: mmsqe --- 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..c22c1fdfaf 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) [#343](https://github.com/crypto-org-chain/ethermint/pull/343) 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..3cf291cbfb 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -422,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 @@ -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) + } + } + } +}