Skip to content

Commit

Permalink
Problem: no api to convert native events to logs (#343)
Browse files Browse the repository at this point in the history
* Problem: no api to convert native events to logs

Solution:
- add event converters

* convert after commit

* Update CHANGELOG.md

Signed-off-by: mmsqe <mavis@crypto.com>

---------

Signed-off-by: mmsqe <mavis@crypto.com>
Co-authored-by: mmsqe <mavis@crypto.com>
  • Loading branch information
yihuang and mmsqe committed Sep 12, 2023
1 parent 7f03e19 commit 81c398f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ func NewEthermintApp(
tracer,
evmSs, nil,
allKeys,
nil,
)

// Create IBC Keeper
Expand Down
7 changes: 7 additions & 0 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -117,6 +123,7 @@ func NewKeeper(
ss: ss,
customContracts: customContracts,
keys: keys,
eventConverters: eventConverters,
}
}

Expand Down
22 changes: 22 additions & 0 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
}
}

0 comments on commit 81c398f

Please sign in to comment.