Skip to content

Commit

Permalink
Update tracer with custom OnCosmosBlockStart and OnCosmosBlockEnd hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard-Voiculescu committed Oct 9, 2024
1 parent 7821ea6 commit 5f5cd2c
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 46 deletions.
42 changes: 13 additions & 29 deletions x/evm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,27 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"math/big"
)

// BeginBlock sets the sdk Context and EIP155 chain id to the Keeper.
func (k *Keeper) BeginBlock(ctx sdk.Context) error {
k.WithChainID(ctx)

// cache parameters that's common for the whole block.
if _, err := k.EVMBlockConfig(ctx, k.ChainID()); err != nil {
evmBlockConfig, err := k.EVMBlockConfig(ctx, k.ChainID())
if err != nil {
return err
}

if k.evmTracer != nil && k.evmTracer.OnBlockStart != nil {
b := types.NewBlock(&types.Header{
Number: big.NewInt(ctx.BlockHeight()),
Time: uint64(ctx.BlockTime().Unix()),
ParentHash: ethcommon.BytesToHash(ctx.BlockHeader().LastBlockId.Hash),
Coinbase: ethcommon.BytesToAddress(ctx.BlockHeader().ProposerAddress),
}, nil, nil, nil)

finalizedHeaderNumber := ctx.BlockHeight() - 1
if ctx.BlockHeight() == 0 {
finalizedHeaderNumber = 0
}

finalizedHeader := &types.Header{
Number: big.NewInt(finalizedHeaderNumber),
}

k.evmTracer.OnBlockStart(tracing.BlockEvent{
Block: b,
TD: big.NewInt(0),
Finalized: finalizedHeader,
})
if k.evmTracer != nil && k.evmTracer.OnCosmosBlockStart != nil {
k.evmTracer.OnCosmosBlockStart(
ToCosmosStartBlockEvent(
k,
ctx,
evmBlockConfig.CoinBase.Bytes(),
ctx.BlockHeader(),
),
)
}

return nil
Expand All @@ -66,9 +50,9 @@ func (k *Keeper) EndBlock(ctx sdk.Context) error {
k.CollectTxBloom(ctx)
k.RemoveParamsCache(ctx)

if k.evmTracer != nil && k.evmTracer.OnBlockEnd != nil {
if k.evmTracer != nil && k.evmTracer.OnCosmosBlockEnd != nil {
defer func() {
k.evmTracer.OnBlockEnd(nil)
k.evmTracer.OnCosmosBlockEnd(ToCosmosEndBlockEvent(k, ctx), nil)
}()
}

Expand Down
9 changes: 7 additions & 2 deletions x/evm/keeper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
package keeper

import (
"github.com/ethereum/go-ethereum/eth/tracers"
"math/big"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/params"
rpctypes "github.com/evmos/ethermint/rpc/types"
"github.com/evmos/ethermint/x/evm/statedb"
Expand Down Expand Up @@ -132,7 +132,12 @@ func (k *Keeper) EVMConfig(ctx sdk.Context, chainID *big.Int, txHash common.Hash
}

if k.evmTracer != nil {
cfg.Tracer = k.evmTracer
t := &tracers.Tracer{
Hooks: k.evmTracer.Hooks,
GetResult: nil,
Stop: nil,
}
cfg.Tracer = t
}

return cfg, nil
Expand Down
6 changes: 3 additions & 3 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package keeper

import (
cosmostracing "github.com/evmos/ethermint/x/evm/tracing"
"math/big"

errorsmod "cosmossdk.io/errors"
Expand All @@ -28,7 +29,6 @@ import (
"github.com/ethereum/go-ethereum/core"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/params"
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/statedb"
Expand Down Expand Up @@ -67,7 +67,7 @@ type Keeper struct {
eip155ChainID *big.Int

// EVM Tracer
evmTracer *tracers.Tracer
evmTracer *cosmostracing.Hooks

// EVM Hooks for tx post-processing
hooks types.EvmHooks
Expand Down Expand Up @@ -204,7 +204,7 @@ func (k *Keeper) PostTxProcessing(ctx sdk.Context, msg *core.Message, receipt *e
return k.hooks.PostTxProcessing(ctx, msg, receipt)
}

func (k *Keeper) SetTracer(tracer *tracers.Tracer) {
func (k *Keeper) SetTracer(tracer *cosmostracing.Hooks) {
k.evmTracer = tracer
}

Expand Down
58 changes: 58 additions & 0 deletions x/evm/keeper/keeper_firehose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package keeper

import (
"cosmossdk.io/store/prefix"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
cosmostypes "github.com/cometbft/cometbft/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/evmos/ethermint/x/evm/tracing"
"github.com/evmos/ethermint/x/evm/types"
"math/big"
)

func BlocksBloom(k *Keeper, ctx sdk.Context) *big.Int {
store := prefix.NewObjStore(ctx.ObjectStore(k.objectKey), types.KeyPrefixObjectBloom)
it := store.Iterator(nil, nil)
defer it.Close()

bloom := new(big.Int)
for ; it.Valid(); it.Next() {
bloom.Or(bloom, it.Value().(*big.Int))
}
return bloom
}

func ToCosmosStartBlockEvent(k *Keeper, ctx sdk.Context, coinbaseBytes []byte, blockHeader cmtproto.Header) tracing.CosmosStartBlockEvent {
// ignore the errors as we are sure that the block header is valid
h, _ := cosmostypes.HeaderFromProto(&blockHeader)
h.ValidatorsHash = ctx.CometInfo().GetValidatorsHash()

keeperParams := k.GetParams(ctx)
ethCfg := keeperParams.ChainConfig.EthereumConfig(k.ChainID())
baseFee := k.GetBaseFee(ctx, ethCfg)
gasLimit := uint64(ctx.ConsensusParams().Block.MaxGas)

finalizedHeaderNumber := h.Height - 1
if h.Height == 0 {
finalizedHeaderNumber = 0
}

finalizedHeader := &ethtypes.Header{
Number: big.NewInt(finalizedHeaderNumber),
}

return tracing.CosmosStartBlockEvent{
CosmosHeader: h,
BaseFee: baseFee,
GasLimit: gasLimit,
Coinbase: coinbaseBytes,
Finalized: finalizedHeader,
}
}

func ToCosmosEndBlockEvent(k *Keeper, ctx sdk.Context) tracing.CosmosEndBlockEvent {
return tracing.CosmosEndBlockEvent{
LogsBloom: BlocksBloom(k, ctx).Bytes(),
}
}
11 changes: 8 additions & 3 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import (
// beneficiary of the coinbase transaction (since we're not mining).
//
// NOTE: the RANDOM opcode is currently not supported since it requires
// RANDAO implementation. See https://github.com/evmos/ethermint/pull/1520#pullrequestreview-1200504697
// RANDOM implementation. See https://github.com/evmos/ethermint/pull/1520#pullrequestreview-1200504697
// for more information.
func (k *Keeper) NewEVM(
ctx sdk.Context,
Expand All @@ -74,7 +74,12 @@ func (k *Keeper) NewEVM(

// Set Config Tracer if it was not already initialized
if k.evmTracer != nil {
cfg.Tracer = k.evmTracer
t := &tracers.Tracer{
Hooks: k.evmTracer.Hooks,
GetResult: nil,
Stop: nil,
}
cfg.Tracer = t
}

vmConfig := k.VMConfig(ctx, cfg)
Expand Down Expand Up @@ -179,7 +184,7 @@ func (k *Keeper) ApplyTransaction(ctx sdk.Context, msgEth *types.MsgEthereumTx)
}

msg := msgEth.AsMessage(cfg.BaseFee)
// snapshot to contain the tx processing and post processing in same scope
// snapshot to contain the tx processing and post-processing in same scope
var commit func()
tmpCtx := ctx
if k.hooks != nil {
Expand Down
Loading

0 comments on commit 5f5cd2c

Please sign in to comment.