From cf82835a046f755f49df670bd2696c7790e99f1e Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Fri, 5 Sep 2025 12:01:33 +0800 Subject: [PATCH 01/17] eip-2935 support # Conflicts: # x/evm/migrations/v4/types/params_v4.pb.go --- proto/ethermint/evm/v1/params.proto | 3 + x/evm/keeper/keeper.go | 50 ++++++++---- x/evm/keeper/state_transition.go | 4 +- x/evm/migrations/v4/types/params_v4.pb.go | 2 +- x/evm/types/params.go | 3 + x/evm/types/params.pb.go | 94 ++++++++++++++++------- 6 files changed, 112 insertions(+), 44 deletions(-) diff --git a/proto/ethermint/evm/v1/params.proto b/proto/ethermint/evm/v1/params.proto index 48c0c944ce..f4e8482f7a 100644 --- a/proto/ethermint/evm/v1/params.proto +++ b/proto/ethermint/evm/v1/params.proto @@ -24,4 +24,7 @@ message Params { bool allow_unprotected_txs = 6; // header_hash_num is the number of header hash to persist. uint64 header_hash_num = 7; + // historyServeWindow for EIP 2935 + uint64 history_serve_window = 8; + } \ No newline at end of file diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index d41f613a5b..aaf6cb47af 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -16,6 +16,7 @@ package keeper import ( + "encoding/binary" "math/big" "github.com/ethereum/go-ethereum/crypto" @@ -31,7 +32,7 @@ import ( "github.com/ethereum/go-ethereum/core/tracing" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" - "github.com/ethereum/go-ethereum/params" + ethparams "github.com/ethereum/go-ethereum/params" ethermint "github.com/evmos/ethermint/types" "github.com/evmos/ethermint/x/evm/statedb" "github.com/evmos/ethermint/x/evm/types" @@ -39,7 +40,7 @@ import ( ) // CustomContractFn defines a custom precompiled contract generator with ctx, rules and returns a precompiled contract. -type CustomContractFn func(sdk.Context, params.Rules) vm.PrecompiledContract +type CustomContractFn func(sdk.Context, ethparams.Rules) vm.PrecompiledContract // GasNoLimit is the value for keeper.queryMaxGasLimit in case there is no limit const GasNoLimit = 0 @@ -216,7 +217,7 @@ func (k *Keeper) PostTxProcessing(ctx sdk.Context, msg *core.Message, receipt *e } // Tracer return a default vm.Tracer based on current keeper state -func (k Keeper) Tracer(ctx sdk.Context, msg core.Message, ethCfg *params.ChainConfig) *tracing.Hooks { +func (k Keeper) Tracer(ctx sdk.Context, msg core.Message, ethCfg *ethparams.ChainConfig) *tracing.Hooks { return types.NewTracer(k.tracer, msg, ethCfg, ctx.BlockHeight(), uint64(ctx.BlockTime().Unix())) //#nosec G115 -- int overflow is not a concern here } @@ -278,7 +279,7 @@ func (k *Keeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) // - `nil`: london hardfork not enabled. // - `0`: london hardfork enabled but feemarket is not enabled. // - `n`: both london hardfork and feemarket are enabled. -func (k Keeper) GetBaseFee(ctx sdk.Context, ethCfg *params.ChainConfig) *big.Int { +func (k Keeper) GetBaseFee(ctx sdk.Context, ethCfg *ethparams.ChainConfig) *big.Int { return k.getBaseFee(ctx, types.IsLondon(ethCfg, ctx.BlockHeight())) } @@ -322,18 +323,41 @@ func (k Keeper) AddTransientGasUsed(ctx sdk.Context, gasUsed uint64) (uint64, er // SetHeaderHash stores the hash of the current block header in the store. func (k Keeper) SetHeaderHash(ctx sdk.Context) { - store := ctx.KVStore(k.storeKey) - height, err := ethermint.SafeUint64(ctx.BlockHeight()) - if err != nil { - panic(err) + window := types.DefaultHistoryServeWindow + params := k.GetParams(ctx) + if params.HistoryServeWindow > 0 { + window = params.HistoryServeWindow + } + + acct := k.GetAccount(ctx, ethparams.HistoryStorageAddress) + if acct != nil && acct.IsContract() { + // set current block hash in the contract storage, compatible with EIP-2935 + ringIndex := uint64(ctx.BlockHeight()) % window //nolint:gosec // G115 // won't exceed uint64 + var key common.Hash + binary.BigEndian.PutUint64(key[24:], ringIndex) + k.SetState(ctx, ethparams.HistoryStorageAddress, key, ctx.HeaderHash()) } - store.Set(types.GetHeaderHashKey(height), ctx.HeaderHash()) } -// GetHeaderHash retrieves the hash of a block header from the store by height. -func (k Keeper) GetHeaderHash(ctx sdk.Context, height uint64) []byte { - store := ctx.KVStore(k.storeKey) - return store.Get(types.GetHeaderHashKey(height)) +// GetHeaderHash sets block hash into EIP-2935 compatible storage contract. +func (k Keeper) GetHeaderHash(ctx sdk.Context, height uint64) common.Hash { + window := types.DefaultHistoryServeWindow + params := k.GetParams(ctx) + if params.HistoryServeWindow > 0 { + window = params.HistoryServeWindow + } + + ringIndex := height % window + var key common.Hash + binary.BigEndian.PutUint64(key[24:], ringIndex) + hash := k.GetState(ctx, ethparams.HistoryStorageAddress, key) + + if hash == (common.Hash{}) { + // fall back to old behavior for retro compability + store := ctx.KVStore(k.storeKey) + return common.Hash(store.Get(types.GetHeaderHashKey(height))) + } + return common.Hash{} } // DeleteHeaderHash removes the hash of a block header from the store by height diff --git a/x/evm/keeper/state_transition.go b/x/evm/keeper/state_transition.go index 938efebabb..1a3302e885 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -125,8 +125,8 @@ func (k Keeper) GetHashFn(ctx sdk.Context) vm.GetHashFunc { return common.Hash{} } hash := k.GetHeaderHash(ctx, num64) - if len(hash) > 0 { - return common.BytesToHash(hash) + if hash != (common.Hash{}) { + return hash } histInfo, err := k.stakingKeeper.GetHistoricalInfo(ctx, h) if err != nil { diff --git a/x/evm/migrations/v4/types/params_v4.pb.go b/x/evm/migrations/v4/types/params_v4.pb.go index 2d74ed4b13..211e519444 100644 --- a/x/evm/migrations/v4/types/params_v4.pb.go +++ b/x/evm/migrations/v4/types/params_v4.pb.go @@ -32,7 +32,7 @@ type V4Params struct { EvmDenom string `protobuf:"bytes,1,opt,name=evm_denom,json=evmDenom,proto3" json:"evm_denom,omitempty" yaml:"evm_denom"` // enable_create toggles state transitions that use the vm.Create function EnableCreate bool `protobuf:"varint,2,opt,name=enable_create,json=enableCreate,proto3" json:"enable_create,omitempty" yaml:"enable_create"` - // enable_call toggles state transitions that use the vm.Call function + // enable_call toggles state tran dsitions that use the vm.Call function EnableCall bool `protobuf:"varint,3,opt,name=enable_call,json=enableCall,proto3" json:"enable_call,omitempty" yaml:"enable_call"` // extra_eips defines the additional EIPs for the vm.Config ExtraEIPs ExtraEIPs `protobuf:"bytes,4,opt,name=extra_eips,json=extraEips,proto3" json:"extra_eips"` diff --git a/x/evm/types/params.go b/x/evm/types/params.go index 2dd5563988..29c0f30ae6 100644 --- a/x/evm/types/params.go +++ b/x/evm/types/params.go @@ -36,6 +36,8 @@ var ( DefaultEnableCall = true // DefaultHeaderHashNum defines the default number of header hash to persist. DefaultHeaderHashNum = uint64(256) + // DefaultHistoryServeWindow DefaultHeaderHashNum defines the default number of hystorical value to serve for EIP2935. + DefaultHistoryServeWindow = uint64(8192) // same as EIP-2935 ) // NewParams creates a new Params instance @@ -61,6 +63,7 @@ func DefaultParams() Params { ChainConfig: config, AllowUnprotectedTxs: DefaultAllowUnprotectedTxs, HeaderHashNum: DefaultHeaderHashNum, + HistoryServeWindow: DefaultHistoryServeWindow, } } diff --git a/x/evm/types/params.pb.go b/x/evm/types/params.pb.go index 165d458898..3329c1cac6 100644 --- a/x/evm/types/params.pb.go +++ b/x/evm/types/params.pb.go @@ -41,6 +41,8 @@ type Params struct { AllowUnprotectedTxs bool `protobuf:"varint,6,opt,name=allow_unprotected_txs,json=allowUnprotectedTxs,proto3" json:"allow_unprotected_txs,omitempty"` // header_hash_num is the number of header hash to persist. HeaderHashNum uint64 `protobuf:"varint,7,opt,name=header_hash_num,json=headerHashNum,proto3" json:"header_hash_num,omitempty"` + // historyServeWindow for EIP 2935 + HistoryServeWindow uint64 `protobuf:"varint,8,opt,name=history_serve_window,json=historyServeWindow,proto3" json:"history_serve_window,omitempty"` } func (m *Params) Reset() { *m = Params{} } @@ -125,6 +127,13 @@ func (m *Params) GetHeaderHashNum() uint64 { return 0 } +func (m *Params) GetHistoryServeWindow() uint64 { + if m != nil { + return m.HistoryServeWindow + } + return 0 +} + func init() { proto.RegisterType((*Params)(nil), "ethermint.evm.v1.Params") } @@ -132,34 +141,36 @@ func init() { func init() { proto.RegisterFile("ethermint/evm/v1/params.proto", fileDescriptor_e7d3c06c1322f20f) } var fileDescriptor_e7d3c06c1322f20f = []byte{ - // 423 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x64, 0x91, 0x41, 0x8b, 0xd3, 0x40, - 0x18, 0x86, 0x1b, 0x5b, 0xeb, 0x76, 0xba, 0x8b, 0xeb, 0x58, 0x65, 0x58, 0xd8, 0x24, 0x44, 0x58, - 0x72, 0x4a, 0xe8, 0x7a, 0x10, 0x04, 0x41, 0x53, 0x2b, 0x7a, 0x91, 0x25, 0xe8, 0xc5, 0xcb, 0x30, - 0x4d, 0x3f, 0x9b, 0xc0, 0x4c, 0x26, 0x64, 0xa6, 0xb1, 0xfb, 0x2f, 0x3c, 0xfb, 0x8b, 0xf6, 0xb8, - 0x47, 0x4f, 0x41, 0xda, 0x7f, 0xd0, 0x5f, 0x20, 0x99, 0xd4, 0x76, 0xd5, 0xdb, 0x7c, 0xdf, 0xf3, - 0xbe, 0x1f, 0xcc, 0xfb, 0xa2, 0x73, 0xd0, 0x29, 0x94, 0x22, 0xcb, 0x75, 0x08, 0x95, 0x08, 0xab, - 0x71, 0x58, 0xb0, 0x92, 0x09, 0x15, 0x14, 0xa5, 0xd4, 0x12, 0x9f, 0xee, 0x71, 0x00, 0x95, 0x08, - 0xaa, 0xf1, 0xd9, 0x68, 0x21, 0x17, 0xd2, 0xc0, 0xb0, 0x79, 0xb5, 0xba, 0xb3, 0x67, 0xff, 0x9d, - 0x49, 0x52, 0x96, 0xe5, 0x34, 0x91, 0xf9, 0xd7, 0x6c, 0xd1, 0x8a, 0xbc, 0x1f, 0x5d, 0xd4, 0xbf, - 0x32, 0xd7, 0xf1, 0x18, 0x0d, 0xa0, 0x12, 0x74, 0x0e, 0xb9, 0x14, 0xc4, 0x72, 0x2d, 0x7f, 0x10, - 0x8d, 0xb6, 0xb5, 0x73, 0x7a, 0xcd, 0x04, 0x7f, 0xe9, 0xed, 0x91, 0x17, 0x1f, 0x41, 0x25, 0xde, - 0x36, 0x4f, 0xfc, 0x0a, 0x9d, 0x40, 0xce, 0x66, 0x1c, 0x68, 0x52, 0x02, 0xd3, 0x40, 0xee, 0xb9, - 0x96, 0x7f, 0x14, 0x91, 0x6d, 0xed, 0x8c, 0x76, 0xb6, 0xbb, 0xd8, 0x8b, 0x8f, 0xdb, 0x79, 0x62, - 0x46, 0xfc, 0x02, 0x0d, 0xff, 0x70, 0xc6, 0x39, 0xe9, 0x1a, 0xf3, 0xd3, 0x6d, 0xed, 0xe0, 0xbf, - 0xcd, 0x8c, 0x73, 0x2f, 0x46, 0x3b, 0x2b, 0xe3, 0x1c, 0xbf, 0x41, 0x08, 0x56, 0xba, 0x64, 0x14, - 0xb2, 0x42, 0x91, 0x9e, 0xdb, 0xf5, 0xbb, 0x91, 0xb7, 0xae, 0x9d, 0xc1, 0xb4, 0xd9, 0x4e, 0x3f, - 0x5c, 0xa9, 0x6d, 0xed, 0x3c, 0xda, 0x1d, 0xd9, 0x0b, 0xbd, 0x78, 0x60, 0x86, 0x69, 0x56, 0x28, - 0xfc, 0x0e, 0x1d, 0xdf, 0x8d, 0x83, 0xdc, 0x77, 0x2d, 0x7f, 0x78, 0x79, 0x1e, 0xfc, 0x1b, 0x6e, - 0x30, 0x69, 0x54, 0x13, 0x23, 0x8a, 0x7a, 0x37, 0xb5, 0xd3, 0x89, 0x87, 0xc9, 0x61, 0x85, 0x2f, - 0xd1, 0x13, 0xc6, 0xb9, 0xfc, 0x46, 0x97, 0x79, 0x93, 0x28, 0x24, 0x1a, 0xe6, 0x54, 0xaf, 0x14, - 0xe9, 0x37, 0xbf, 0x89, 0x1f, 0x1b, 0xf8, 0xf9, 0xc0, 0x3e, 0xad, 0x14, 0xbe, 0x40, 0x0f, 0x53, - 0x60, 0x73, 0x28, 0x69, 0xca, 0x54, 0x4a, 0xf3, 0xa5, 0x20, 0x0f, 0x5c, 0xcb, 0xef, 0xc5, 0x27, - 0xed, 0xfa, 0x3d, 0x53, 0xe9, 0xc7, 0xa5, 0x88, 0x5e, 0xdf, 0xac, 0x6d, 0xeb, 0x76, 0x6d, 0x5b, - 0xbf, 0xd6, 0xb6, 0xf5, 0x7d, 0x63, 0x77, 0x6e, 0x37, 0x76, 0xe7, 0xe7, 0xc6, 0xee, 0x7c, 0xb9, - 0x58, 0x64, 0x3a, 0x5d, 0xce, 0x82, 0x44, 0x8a, 0xa6, 0x5c, 0xa9, 0xc2, 0x43, 0xd9, 0x2b, 0x53, - 0xb7, 0xbe, 0x2e, 0x40, 0xcd, 0xfa, 0xa6, 0xe5, 0xe7, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0x01, - 0xa5, 0x92, 0x40, 0x53, 0x02, 0x00, 0x00, + // 451 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x64, 0x92, 0x41, 0x8b, 0xd3, 0x40, + 0x18, 0x86, 0x1b, 0x5b, 0x6b, 0x3b, 0xdd, 0xc5, 0x75, 0xac, 0x12, 0x16, 0x36, 0x09, 0x11, 0x96, + 0x9c, 0x12, 0xbb, 0x1e, 0x04, 0x41, 0xd0, 0xd4, 0x8a, 0x5e, 0x64, 0x89, 0x8a, 0xe0, 0x65, 0x98, + 0xa6, 0x9f, 0x4d, 0x60, 0x26, 0x13, 0x32, 0xd3, 0xb4, 0xfd, 0x17, 0xfe, 0xac, 0x3d, 0x78, 0xd8, + 0xa3, 0xa7, 0x20, 0xed, 0x3f, 0xe8, 0x2f, 0x90, 0x4c, 0x6b, 0x5b, 0xf5, 0x36, 0xdf, 0xf7, 0xbc, + 0xef, 0x07, 0xf3, 0xf2, 0xa2, 0x0b, 0x50, 0x09, 0x14, 0x3c, 0xcd, 0x54, 0x00, 0x25, 0x0f, 0xca, + 0x41, 0x90, 0xd3, 0x82, 0x72, 0xe9, 0xe7, 0x85, 0x50, 0x02, 0x9f, 0xed, 0xb1, 0x0f, 0x25, 0xf7, + 0xcb, 0xc1, 0x79, 0x7f, 0x2a, 0xa6, 0x42, 0xc3, 0xa0, 0x7e, 0x6d, 0x75, 0xe7, 0x4f, 0xfe, 0x3b, + 0x13, 0x27, 0x34, 0xcd, 0x48, 0x2c, 0xb2, 0x6f, 0xe9, 0x74, 0x2b, 0x72, 0x7f, 0x34, 0x51, 0xfb, + 0x5a, 0x5f, 0xc7, 0x03, 0xd4, 0x85, 0x92, 0x93, 0x09, 0x64, 0x82, 0x9b, 0x86, 0x63, 0x78, 0xdd, + 0xb0, 0xbf, 0xa9, 0xec, 0xb3, 0x25, 0xe5, 0xec, 0x85, 0xbb, 0x47, 0x6e, 0xd4, 0x81, 0x92, 0xbf, + 0xa9, 0x9f, 0xf8, 0x25, 0x3a, 0x85, 0x8c, 0x8e, 0x19, 0x90, 0xb8, 0x00, 0xaa, 0xc0, 0xbc, 0xe3, + 0x18, 0x5e, 0x27, 0x34, 0x37, 0x95, 0xdd, 0xdf, 0xd9, 0x8e, 0xb1, 0x1b, 0x9d, 0x6c, 0xe7, 0xa1, + 0x1e, 0xf1, 0x73, 0xd4, 0xfb, 0xc3, 0x29, 0x63, 0x66, 0x53, 0x9b, 0x1f, 0x6f, 0x2a, 0x1b, 0xff, + 0x6d, 0xa6, 0x8c, 0xb9, 0x11, 0xda, 0x59, 0x29, 0x63, 0xf8, 0x35, 0x42, 0xb0, 0x50, 0x05, 0x25, + 0x90, 0xe6, 0xd2, 0x6c, 0x39, 0x4d, 0xaf, 0x19, 0xba, 0xab, 0xca, 0xee, 0x8e, 0xea, 0xed, 0xe8, + 0xfd, 0xb5, 0xdc, 0x54, 0xf6, 0x83, 0xdd, 0x91, 0xbd, 0xd0, 0x8d, 0xba, 0x7a, 0x18, 0xa5, 0xb9, + 0xc4, 0x6f, 0xd1, 0xc9, 0x71, 0x1c, 0xe6, 0x5d, 0xc7, 0xf0, 0x7a, 0x57, 0x17, 0xfe, 0xbf, 0xe1, + 0xfa, 0xc3, 0x5a, 0x35, 0xd4, 0xa2, 0xb0, 0x75, 0x53, 0xd9, 0x8d, 0xa8, 0x17, 0x1f, 0x56, 0xf8, + 0x0a, 0x3d, 0xa2, 0x8c, 0x89, 0x39, 0x99, 0x65, 0x75, 0xa2, 0x10, 0x2b, 0x98, 0x10, 0xb5, 0x90, + 0x66, 0xbb, 0xfe, 0x4d, 0xf4, 0x50, 0xc3, 0xcf, 0x07, 0xf6, 0x69, 0x21, 0xf1, 0x25, 0xba, 0x9f, + 0x00, 0x9d, 0x40, 0x41, 0x12, 0x2a, 0x13, 0x92, 0xcd, 0xb8, 0x79, 0xcf, 0x31, 0xbc, 0x56, 0x74, + 0xba, 0x5d, 0xbf, 0xa3, 0x32, 0xf9, 0x30, 0xe3, 0xf8, 0x29, 0xea, 0x27, 0xa9, 0x54, 0xa2, 0x58, + 0x12, 0x09, 0x45, 0x09, 0x64, 0x9e, 0x66, 0x13, 0x31, 0x37, 0x3b, 0x5a, 0x8c, 0x77, 0xec, 0x63, + 0x8d, 0xbe, 0x68, 0x12, 0xbe, 0xba, 0x59, 0x59, 0xc6, 0xed, 0xca, 0x32, 0x7e, 0xad, 0x2c, 0xe3, + 0xfb, 0xda, 0x6a, 0xdc, 0xae, 0xad, 0xc6, 0xcf, 0xb5, 0xd5, 0xf8, 0x7a, 0x39, 0x4d, 0x55, 0x32, + 0x1b, 0xfb, 0xb1, 0xe0, 0x75, 0x1d, 0x84, 0x0c, 0x0e, 0xf5, 0x58, 0xe8, 0x82, 0xa8, 0x65, 0x0e, + 0x72, 0xdc, 0xd6, 0xbd, 0x78, 0xf6, 0x3b, 0x00, 0x00, 0xff, 0xff, 0x94, 0xe9, 0x25, 0xb7, 0x85, + 0x02, 0x00, 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -182,6 +193,11 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.HistoryServeWindow != 0 { + i = encodeVarintParams(dAtA, i, uint64(m.HistoryServeWindow)) + i-- + dAtA[i] = 0x40 + } if m.HeaderHashNum != 0 { i = encodeVarintParams(dAtA, i, uint64(m.HeaderHashNum)) i-- @@ -298,6 +314,9 @@ func (m *Params) Size() (n int) { if m.HeaderHashNum != 0 { n += 1 + sovParams(uint64(m.HeaderHashNum)) } + if m.HistoryServeWindow != 0 { + n += 1 + sovParams(uint64(m.HistoryServeWindow)) + } return n } @@ -556,6 +575,25 @@ func (m *Params) Unmarshal(dAtA []byte) error { break } } + case 8: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field HistoryServeWindow", wireType) + } + m.HistoryServeWindow = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParams + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.HistoryServeWindow |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipParams(dAtA[iNdEx:]) From 2249cba86e7463521387ddf765adaf7c504b23d9 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Fri, 5 Sep 2025 12:37:11 +0800 Subject: [PATCH 02/17] revert proto --- x/evm/migrations/v4/types/params_v4.pb.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/evm/migrations/v4/types/params_v4.pb.go b/x/evm/migrations/v4/types/params_v4.pb.go index 211e519444..a9168b28e8 100644 --- a/x/evm/migrations/v4/types/params_v4.pb.go +++ b/x/evm/migrations/v4/types/params_v4.pb.go @@ -8,6 +8,7 @@ import ( _ "github.com/cosmos/gogoproto/gogoproto" v0types "github.com/evmos/ethermint/x/evm/migrations/v0/types" proto "github.com/cosmos/gogoproto/proto" + v0types "github.com/evmos/ethermint/x/evm/migrations/v0/types" io "io" math "math" math_bits "math/bits" From c868d6b817efb001bcfd44b17f9c9530931eb99f Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Fri, 5 Sep 2025 12:46:08 +0800 Subject: [PATCH 03/17] add more comments --- x/evm/keeper/keeper.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index aaf6cb47af..13b9d71b50 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -352,12 +352,14 @@ func (k Keeper) GetHeaderHash(ctx sdk.Context, height uint64) common.Hash { binary.BigEndian.PutUint64(key[24:], ringIndex) hash := k.GetState(ctx, ethparams.HistoryStorageAddress, key) - if hash == (common.Hash{}) { - // fall back to old behavior for retro compability - store := ctx.KVStore(k.storeKey) - return common.Hash(store.Get(types.GetHeaderHashKey(height))) + if hash != (common.Hash{}) { + return hash } - return common.Hash{} + + // fall back to old behavior for retro compatibility + // TODO can be removed along with DeleteHeaderHash once HistoryStorage has been filled up in next protocol upgrade + store := ctx.KVStore(k.storeKey) + return common.Hash(store.Get(types.GetHeaderHashKey(height))) } // DeleteHeaderHash removes the hash of a block header from the store by height From 0e8354bef6fdc1c275691bcd967d18f4d740bfbc Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Fri, 5 Sep 2025 13:09:45 +0800 Subject: [PATCH 04/17] fix conversion --- x/evm/keeper/keeper.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index 13b9d71b50..5ed9654dc5 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -359,7 +359,11 @@ func (k Keeper) GetHeaderHash(ctx sdk.Context, height uint64) common.Hash { // fall back to old behavior for retro compatibility // TODO can be removed along with DeleteHeaderHash once HistoryStorage has been filled up in next protocol upgrade store := ctx.KVStore(k.storeKey) - return common.Hash(store.Get(types.GetHeaderHashKey(height))) + hashByte := store.Get(types.GetHeaderHashKey(height)) + if len(hashByte) > 0 { + return common.BytesToHash(hashByte) + } + return common.Hash{} } // DeleteHeaderHash removes the hash of a block header from the store by height From 9b1798acc3720312b45aace7eabe7dc2ac066da8 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Fri, 5 Sep 2025 13:12:12 +0800 Subject: [PATCH 05/17] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a831e32b58..266da2de92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (evm) [#740](https://github.com/crypto-org-chain/ethermint/pull/740) fix: missing tx context during vm initialisation * (evm) [#742](https://github.com/crypto-org-chain/ethermint/pull/742) fix: prevent nil pointer dereference in tracer hooks * (evm) [#728](https://github.com/crypto-org-chain/ethermint/pull/728) feat: support preinstalls +* (evm) [#722](https://github.com/crypto-org-chain/ethermint/pull/722) Support EIP-2935 ## [v0.22.0] - 2025-08-12 From cff94aaed6857dd82652410e5721b4c5daf31959 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Mon, 8 Sep 2025 14:16:51 +0900 Subject: [PATCH 06/17] fix test --- x/evm/keeper/keeper.go | 2 +- x/evm/keeper/state_transition.go | 2 +- x/evm/keeper/state_transition_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index 5ed9654dc5..b6c2d0f012 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -352,7 +352,7 @@ func (k Keeper) GetHeaderHash(ctx sdk.Context, height uint64) common.Hash { binary.BigEndian.PutUint64(key[24:], ringIndex) hash := k.GetState(ctx, ethparams.HistoryStorageAddress, key) - if hash != (common.Hash{}) { + if hash.Cmp(common.Hash{}) != 0 { return hash } diff --git a/x/evm/keeper/state_transition.go b/x/evm/keeper/state_transition.go index 1a3302e885..0ae5f10e3e 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -125,7 +125,7 @@ func (k Keeper) GetHashFn(ctx sdk.Context) vm.GetHashFunc { return common.Hash{} } hash := k.GetHeaderHash(ctx, num64) - if hash != (common.Hash{}) { + if hash.Cmp(common.Hash{}) != 0 { return hash } histInfo, err := k.stakingKeeper.GetHistoricalInfo(ctx, h) diff --git a/x/evm/keeper/state_transition_test.go b/x/evm/keeper/state_transition_test.go index 2a525158b7..3e6e250902 100644 --- a/x/evm/keeper/state_transition_test.go +++ b/x/evm/keeper/state_transition_test.go @@ -140,7 +140,7 @@ func (suite *StateTransitionTestSuite) TestGetHashFn() { }, { "header after sdk50 found", - height - 1, + height, func(height int64) { suite.Ctx = suite.Ctx.WithBlockHeight(height).WithHeaderHash(header.Hash()) suite.App.EvmKeeper.SetHeaderHash(suite.Ctx) From 09823e1d324d9f6753dff3f749abfeb343b13138 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Mon, 8 Sep 2025 15:46:51 +0900 Subject: [PATCH 07/17] add fallback old behavior --- x/evm/keeper/keeper.go | 7 +++++++ x/evm/keeper/state_transition_test.go | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index b6c2d0f012..e4e3ad346d 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -336,6 +336,13 @@ func (k Keeper) SetHeaderHash(ctx sdk.Context) { var key common.Hash binary.BigEndian.PutUint64(key[24:], ringIndex) k.SetState(ctx, ethparams.HistoryStorageAddress, key, ctx.HeaderHash()) + } else { + store := ctx.KVStore(k.storeKey) + height, err := ethermint.SafeUint64(ctx.BlockHeight()) + if err != nil { + panic(err) + } + store.Set(types.GetHeaderHashKey(height), ctx.HeaderHash()) } } diff --git a/x/evm/keeper/state_transition_test.go b/x/evm/keeper/state_transition_test.go index 3e6e250902..2a525158b7 100644 --- a/x/evm/keeper/state_transition_test.go +++ b/x/evm/keeper/state_transition_test.go @@ -140,7 +140,7 @@ func (suite *StateTransitionTestSuite) TestGetHashFn() { }, { "header after sdk50 found", - height, + height - 1, func(height int64) { suite.Ctx = suite.Ctx.WithBlockHeight(height).WithHeaderHash(header.Hash()) suite.App.EvmKeeper.SetHeaderHash(suite.Ctx) From 2ed846a886a1b00359f618eef713dfca6ce35874 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Mon, 8 Sep 2025 15:48:12 +0900 Subject: [PATCH 08/17] add more comments --- x/evm/keeper/keeper.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index e4e3ad346d..68b8870400 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -337,6 +337,7 @@ func (k Keeper) SetHeaderHash(ctx sdk.Context) { binary.BigEndian.PutUint64(key[24:], ringIndex) k.SetState(ctx, ethparams.HistoryStorageAddress, key, ctx.HeaderHash()) } else { + // fallback old implementation store := ctx.KVStore(k.storeKey) height, err := ethermint.SafeUint64(ctx.BlockHeight()) if err != nil { From 3583ef9c51b13b0aed70e8c3858e506db840b2b7 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Fri, 3 Oct 2025 10:24:29 +0900 Subject: [PATCH 09/17] fix proto --- x/evm/migrations/v4/types/params_v4.pb.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/x/evm/migrations/v4/types/params_v4.pb.go b/x/evm/migrations/v4/types/params_v4.pb.go index a9168b28e8..3a93df5dd6 100644 --- a/x/evm/migrations/v4/types/params_v4.pb.go +++ b/x/evm/migrations/v4/types/params_v4.pb.go @@ -6,13 +6,11 @@ package types import ( fmt "fmt" _ "github.com/cosmos/gogoproto/gogoproto" - v0types "github.com/evmos/ethermint/x/evm/migrations/v0/types" proto "github.com/cosmos/gogoproto/proto" v0types "github.com/evmos/ethermint/x/evm/migrations/v0/types" io "io" math "math" math_bits "math/bits" - ) // Reference imports to suppress errors if they are not otherwise used. @@ -33,7 +31,7 @@ type V4Params struct { EvmDenom string `protobuf:"bytes,1,opt,name=evm_denom,json=evmDenom,proto3" json:"evm_denom,omitempty" yaml:"evm_denom"` // enable_create toggles state transitions that use the vm.Create function EnableCreate bool `protobuf:"varint,2,opt,name=enable_create,json=enableCreate,proto3" json:"enable_create,omitempty" yaml:"enable_create"` - // enable_call toggles state tran dsitions that use the vm.Call function + // enable_call toggles state transitions that use the vm.Call function EnableCall bool `protobuf:"varint,3,opt,name=enable_call,json=enableCall,proto3" json:"enable_call,omitempty" yaml:"enable_call"` // extra_eips defines the additional EIPs for the vm.Config ExtraEIPs ExtraEIPs `protobuf:"bytes,4,opt,name=extra_eips,json=extraEips,proto3" json:"extra_eips"` From 12f1d7fd8be48fe7ca278d1d5e44876e1a6b0834 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Fri, 3 Oct 2025 23:11:37 +0900 Subject: [PATCH 10/17] add logs --- x/evm/keeper/keeper.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index 68b8870400..d9026d7ede 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -335,6 +335,11 @@ func (k Keeper) SetHeaderHash(ctx sdk.Context) { ringIndex := uint64(ctx.BlockHeight()) % window //nolint:gosec // G115 // won't exceed uint64 var key common.Hash binary.BigEndian.PutUint64(key[24:], ringIndex) + k.Logger(ctx).Error("set in contract", + "address", ethparams.HistoryStorageAddress, + "key", key, + "hash", ctx.HeaderHash(), + ) k.SetState(ctx, ethparams.HistoryStorageAddress, key, ctx.HeaderHash()) } else { // fallback old implementation @@ -343,6 +348,11 @@ func (k Keeper) SetHeaderHash(ctx sdk.Context) { if err != nil { panic(err) } + k.Logger(ctx).Error("allback old implementation", + "address", ethparams.HistoryStorageAddress, + "height", types.GetHeaderHashKey(height), + "hash", ctx.HeaderHash(), + ) store.Set(types.GetHeaderHashKey(height), ctx.HeaderHash()) } } @@ -354,13 +364,19 @@ func (k Keeper) GetHeaderHash(ctx sdk.Context, height uint64) common.Hash { if params.HistoryServeWindow > 0 { window = params.HistoryServeWindow } - ringIndex := height % window var key common.Hash binary.BigEndian.PutUint64(key[24:], ringIndex) + k.Logger(ctx).Error("query header hash", + "index", ringIndex, + "key", key, + ) hash := k.GetState(ctx, ethparams.HistoryStorageAddress, key) if hash.Cmp(common.Hash{}) != 0 { + k.Logger(ctx).Error("get from contract", + "hash", hash, + ) return hash } @@ -369,6 +385,9 @@ func (k Keeper) GetHeaderHash(ctx sdk.Context, height uint64) common.Hash { store := ctx.KVStore(k.storeKey) hashByte := store.Get(types.GetHeaderHashKey(height)) if len(hashByte) > 0 { + k.Logger(ctx).Error("get from state", + "hash", common.BytesToHash(hashByte), + ) return common.BytesToHash(hashByte) } return common.Hash{} From 7847a6e9c1aae5d52fc9306f94a4764a62813857 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Fri, 3 Oct 2025 23:57:06 +0900 Subject: [PATCH 11/17] fix GetHashFn --- x/evm/keeper/state_transition.go | 36 +++++++------------------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/x/evm/keeper/state_transition.go b/x/evm/keeper/state_transition.go index 0ae5f10e3e..87f8e7829d 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -29,7 +29,6 @@ import ( "github.com/evmos/ethermint/x/evm/types" "github.com/holiman/uint256" - cmttypes "github.com/cometbft/cometbft/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/tracing" @@ -95,11 +94,11 @@ func (k *Keeper) NewEVM( // GetHashFn implements vm.GetHashFunc for Ethermint. It returns hash for 3 cases: // 1. The requested height matches current block height from the context. -// 2. The requested height is within the valid range, retrieve the hash from GetHeaderHash for heights after sdk50. -// 3. The requested height is within the valid range, retrieve the hash from GetHistoricalInfo for heights before sdk50. +// 2. The requested height is below current block height, follow EIP-2935. +// 3. The requested height is above current block height, return empty func (k Keeper) GetHashFn(ctx sdk.Context) vm.GetHashFunc { return func(num64 uint64) common.Hash { - h, err := ethermint.SafeInt64(num64) + _, err := ethermint.SafeInt64(num64) if err != nil { return common.Hash{} } @@ -113,32 +112,11 @@ func (k Keeper) GetHashFn(ctx sdk.Context) vm.GetHashFunc { return common.BytesToHash(headerHash) } } - // Align check with https://github.com/ethereum/go-ethereum/blob/release/1.11/core/vm/instructions.go#L433 - headerNum := k.GetParams(ctx).HeaderHashNum - var lower uint64 - if upper <= headerNum { - lower = 0 - } else { - lower = upper - headerNum + if upper > num64 { + // The requested height is historical, query EIP-2935 contract storage + return k.GetHeaderHash(ctx, num64) } - if num64 < lower || num64 >= upper { - return common.Hash{} - } - hash := k.GetHeaderHash(ctx, num64) - if hash.Cmp(common.Hash{}) != 0 { - return hash - } - histInfo, err := k.stakingKeeper.GetHistoricalInfo(ctx, h) - if err != nil { - k.Logger(ctx).Debug("historical info not found", "height", h, "err", err.Error()) - return common.Hash{} - } - header, err := cmttypes.HeaderFromProto(&histInfo.Header) - if err != nil { - k.Logger(ctx).Error("failed to cast tendermint header from proto", "error", err) - return common.Hash{} - } - return common.BytesToHash(header.Hash()) + return common.Hash{} } } From f1fa86be261c0b7cdb7447d80ff18633a8f3a600 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Sat, 4 Oct 2025 01:36:31 +0900 Subject: [PATCH 12/17] remove log --- x/evm/keeper/keeper.go | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index d9026d7ede..68b8870400 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -335,11 +335,6 @@ func (k Keeper) SetHeaderHash(ctx sdk.Context) { ringIndex := uint64(ctx.BlockHeight()) % window //nolint:gosec // G115 // won't exceed uint64 var key common.Hash binary.BigEndian.PutUint64(key[24:], ringIndex) - k.Logger(ctx).Error("set in contract", - "address", ethparams.HistoryStorageAddress, - "key", key, - "hash", ctx.HeaderHash(), - ) k.SetState(ctx, ethparams.HistoryStorageAddress, key, ctx.HeaderHash()) } else { // fallback old implementation @@ -348,11 +343,6 @@ func (k Keeper) SetHeaderHash(ctx sdk.Context) { if err != nil { panic(err) } - k.Logger(ctx).Error("allback old implementation", - "address", ethparams.HistoryStorageAddress, - "height", types.GetHeaderHashKey(height), - "hash", ctx.HeaderHash(), - ) store.Set(types.GetHeaderHashKey(height), ctx.HeaderHash()) } } @@ -364,19 +354,13 @@ func (k Keeper) GetHeaderHash(ctx sdk.Context, height uint64) common.Hash { if params.HistoryServeWindow > 0 { window = params.HistoryServeWindow } + ringIndex := height % window var key common.Hash binary.BigEndian.PutUint64(key[24:], ringIndex) - k.Logger(ctx).Error("query header hash", - "index", ringIndex, - "key", key, - ) hash := k.GetState(ctx, ethparams.HistoryStorageAddress, key) if hash.Cmp(common.Hash{}) != 0 { - k.Logger(ctx).Error("get from contract", - "hash", hash, - ) return hash } @@ -385,9 +369,6 @@ func (k Keeper) GetHeaderHash(ctx sdk.Context, height uint64) common.Hash { store := ctx.KVStore(k.storeKey) hashByte := store.Get(types.GetHeaderHashKey(height)) if len(hashByte) > 0 { - k.Logger(ctx).Error("get from state", - "hash", common.BytesToHash(hashByte), - ) return common.BytesToHash(hashByte) } return common.Hash{} From d6c0c69ff2eeb135f0680dbe8ac16cb639a9a204 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Sat, 4 Oct 2025 15:51:28 +0900 Subject: [PATCH 13/17] fix historical data --- x/evm/keeper/keeper.go | 27 +++++++++++++++------------ x/evm/keeper/state_transition.go | 31 +++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index 68b8870400..2300a1f820 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -349,21 +349,24 @@ func (k Keeper) SetHeaderHash(ctx sdk.Context) { // GetHeaderHash sets block hash into EIP-2935 compatible storage contract. func (k Keeper) GetHeaderHash(ctx sdk.Context, height uint64) common.Hash { - window := types.DefaultHistoryServeWindow - params := k.GetParams(ctx) - if params.HistoryServeWindow > 0 { - window = params.HistoryServeWindow - } + // check if history contract has been deployed + acct := k.GetAccount(ctx, ethparams.HistoryStorageAddress) + if acct != nil && acct.IsContract() { + window := types.DefaultHistoryServeWindow + params := k.GetParams(ctx) + if params.HistoryServeWindow > 0 { + window = params.HistoryServeWindow + } - ringIndex := height % window - var key common.Hash - binary.BigEndian.PutUint64(key[24:], ringIndex) - hash := k.GetState(ctx, ethparams.HistoryStorageAddress, key) + ringIndex := height % window + var key common.Hash + binary.BigEndian.PutUint64(key[24:], ringIndex) + hash := k.GetState(ctx, ethparams.HistoryStorageAddress, key) - if hash.Cmp(common.Hash{}) != 0 { - return hash + if hash.Cmp(common.Hash{}) != 0 { + return hash + } } - // fall back to old behavior for retro compatibility // TODO can be removed along with DeleteHeaderHash once HistoryStorage has been filled up in next protocol upgrade store := ctx.KVStore(k.storeKey) diff --git a/x/evm/keeper/state_transition.go b/x/evm/keeper/state_transition.go index 87f8e7829d..162892efcf 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -18,6 +18,7 @@ package keeper import ( "bytes" "fmt" + cmttypes "github.com/cometbft/cometbft/types" "math/big" "sort" @@ -98,7 +99,7 @@ func (k *Keeper) NewEVM( // 3. The requested height is above current block height, return empty func (k Keeper) GetHashFn(ctx sdk.Context) vm.GetHashFunc { return func(num64 uint64) common.Hash { - _, err := ethermint.SafeInt64(num64) + h, err := ethermint.SafeInt64(num64) if err != nil { return common.Hash{} } @@ -112,9 +113,35 @@ func (k Keeper) GetHashFn(ctx sdk.Context) vm.GetHashFunc { return common.BytesToHash(headerHash) } } + // Align check with https://github.com/ethereum/go-ethereum/blob/release/1.11/core/vm/instructions.go#L433 + var lower uint64 + headerNum := k.GetParams(ctx).HeaderHashNum + if upper <= headerNum { + lower = 0 + } else { + lower = upper - headerNum + } + if upper > num64 { // The requested height is historical, query EIP-2935 contract storage - return k.GetHeaderHash(ctx, num64) + headerHash := k.GetHeaderHash(ctx, num64) + if headerHash.Cmp(common.Hash{}) != 0 { + return headerHash + } else if num64 >= lower { + // Pre upgrade case + // In case EIP-2935 is not supported and data cannot be found, we fetch historical info + histInfo, err := k.stakingKeeper.GetHistoricalInfo(ctx, h) + if err != nil { + k.Logger(ctx).Debug("historical info not found", "height", h, "err", err.Error()) + return common.Hash{} + } + header, err := cmttypes.HeaderFromProto(&histInfo.Header) + if err != nil { + k.Logger(ctx).Error("failed to cast tendermint header from proto", "error", err) + return common.Hash{} + } + return common.BytesToHash(header.Hash()) + } } return common.Hash{} } From f87236ac314039e58ca9abad89f87535ff7953a6 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Sat, 4 Oct 2025 16:24:01 +0900 Subject: [PATCH 14/17] fix lint --- x/evm/keeper/state_transition.go | 3 ++- x/evm/keeper/state_transition_test.go | 6 ------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/x/evm/keeper/state_transition.go b/x/evm/keeper/state_transition.go index 162892efcf..22835f08f5 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -18,10 +18,11 @@ package keeper import ( "bytes" "fmt" - cmttypes "github.com/cometbft/cometbft/types" "math/big" "sort" + cmttypes "github.com/cometbft/cometbft/types" + errorsmod "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" diff --git a/x/evm/keeper/state_transition_test.go b/x/evm/keeper/state_transition_test.go index 2a525158b7..5bcc3c6f7b 100644 --- a/x/evm/keeper/state_transition_test.go +++ b/x/evm/keeper/state_transition_test.go @@ -169,12 +169,6 @@ func (suite *StateTransitionTestSuite) TestGetHashFn() { func(_ int64) {}, common.Hash{}, }, - { - "height less than header hash num range", - height - evmtypes.DefaultHeaderHashNum - 1, - func(_ int64) {}, - common.Hash{}, - }, { "header not found in stores", height - 1, From d1f9dacdbfc887f75a8fd0d5a2ef9b91e28a60b8 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Sat, 4 Oct 2025 17:37:46 +0900 Subject: [PATCH 15/17] small optimize --- x/evm/keeper/keeper.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index 2300a1f820..603f0b9edf 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -323,14 +323,13 @@ func (k Keeper) AddTransientGasUsed(ctx sdk.Context, gasUsed uint64) (uint64, er // SetHeaderHash stores the hash of the current block header in the store. func (k Keeper) SetHeaderHash(ctx sdk.Context) { - window := types.DefaultHistoryServeWindow - params := k.GetParams(ctx) - if params.HistoryServeWindow > 0 { - window = params.HistoryServeWindow - } - acct := k.GetAccount(ctx, ethparams.HistoryStorageAddress) if acct != nil && acct.IsContract() { + window := types.DefaultHistoryServeWindow + params := k.GetParams(ctx) + if params.HistoryServeWindow > 0 { + window = params.HistoryServeWindow + } // set current block hash in the contract storage, compatible with EIP-2935 ringIndex := uint64(ctx.BlockHeight()) % window //nolint:gosec // G115 // won't exceed uint64 var key common.Hash From cd58ddef822bdd078690fe763d906a24c225f69c Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Sat, 4 Oct 2025 21:41:31 +0900 Subject: [PATCH 16/17] fix tests --- x/evm/keeper/keeper_test.go | 45 ++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/x/evm/keeper/keeper_test.go b/x/evm/keeper/keeper_test.go index bde3244214..67f820c128 100644 --- a/x/evm/keeper/keeper_test.go +++ b/x/evm/keeper/keeper_test.go @@ -4,6 +4,7 @@ import ( _ "embed" "math" "math/big" + "strings" "testing" sdkmath "cosmossdk.io/math" @@ -127,7 +128,11 @@ func (suite *KeeperTestSuite) TestGetAccountStorage() { if tc.malleate != nil { contractAddr = tc.malleate() } - i := 0 + + var results []struct { + addr common.Address + storage types.Storage + } suite.App.AccountKeeper.IterateAccounts(suite.Ctx, func(account sdk.AccountI) bool { ethAccount, ok := account.(ethermint.EthAccountI) if !ok { @@ -137,21 +142,39 @@ func (suite *KeeperTestSuite) TestGetAccountStorage() { addr := ethAccount.EthAddress() storage := suite.App.EvmKeeper.GetAccountStorage(suite.Ctx, addr) + results = append(results, struct { + addr common.Address + storage types.Storage + }{addr, storage}) + return false + }) + + isPreinstall := func(addr common.Address) bool { + for _, p := range types.DefaultPreinstalls { + if strings.EqualFold(addr.Hex(), p.Address) { + return true + } + } + return false + } - if addr == contractAddr { - s.Require().NotEqual(0, len(storage), - "expected account %d to have non-zero amount of storage slots, got %d", - i, len(storage), + for _, r := range results { + if isPreinstall(r.addr) { + // skip preinstall + continue + } + if r.addr == contractAddr { + suite.Require().NotEqual(0, len(r.storage), + "expected account address %s to have non-zero amount of storage slots, got %d", + r.addr.Hex(), len(r.storage), ) } else { - s.Require().Len(storage, 0, - "expected account %d to have %d storage slots, got %d", - i, 0, len(storage), + suite.Require().Len(r.storage, 0, + "expected account address %s to have %d storage slots, got %d", + r.addr.Hex(), 0, len(r.storage), ) } - i++ - return false - }) + } }) } } From ab83b1776e82659ae9d9b7cbccebe48450f9cd2a Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Tue, 7 Oct 2025 09:47:44 +0900 Subject: [PATCH 17/17] fix changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 266da2de92..ba97e44ae6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,7 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (evm) [#740](https://github.com/crypto-org-chain/ethermint/pull/740) fix: missing tx context during vm initialisation * (evm) [#742](https://github.com/crypto-org-chain/ethermint/pull/742) fix: prevent nil pointer dereference in tracer hooks * (evm) [#728](https://github.com/crypto-org-chain/ethermint/pull/728) feat: support preinstalls -* (evm) [#722](https://github.com/crypto-org-chain/ethermint/pull/722) Support EIP-2935 +* (evm) [#722](https://github.com/crypto-org-chain/ethermint/pull/722) feat: support EIP-2935 ## [v0.22.0] - 2025-08-12