From 853e1e5b3475c832f6674e039e6a26eba15f93ab Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 4 Oct 2024 15:46:23 +0800 Subject: [PATCH] Problem: opBlockhash broke after sdk50 (#534) * Problem: opBlockhash broke after sdk50 * cleanup * Apply suggestions from code review * DeleteHeaderHash * add header_hash_num in param * Apply suggestions from code review Signed-off-by: yihuang * Apply suggestions from code review Signed-off-by: yihuang * Update x/evm/types/key.go Signed-off-by: yihuang * Apply suggestions from code review * fix build * lint --------- Signed-off-by: yihuang Co-authored-by: yihuang --- CHANGELOG.md | 1 + app/upgrades.go | 16 ++- client/docs/swagger-ui/swagger.yaml | 120 ++++++++++++++---- proto/ethermint/evm/v1/params.proto | 2 + .../configs/cosmovisor.jsonnet | 5 + .../integration_tests/configs/default.jsonnet | 1 + .../contracts/TestBlockTxProperties.sol | 8 ++ tests/integration_tests/test_block.py | 14 ++ tests/integration_tests/utils.py | 1 + x/evm/keeper/abci.go | 17 ++- x/evm/keeper/keeper.go | 22 ++++ x/evm/keeper/state_transition.go | 45 +------ x/evm/keeper/state_transition_test.go | 111 +++++++--------- x/evm/types/key.go | 15 ++- x/evm/types/params.go | 3 + x/evm/types/params.pb.go | 88 +++++++++---- 16 files changed, 310 insertions(+), 159 deletions(-) create mode 100644 tests/integration_tests/hardhat/contracts/TestBlockTxProperties.sol create mode 100644 tests/integration_tests/test_block.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eba179458..f593c90c74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (rpc) [#516](https://github.com/crypto-org-chain/ethermint/pull/516) Avoid method eth_chainId crashed due to nil pointer on IsEIP155 check. * (cli) [#524](https://github.com/crypto-org-chain/ethermint/pull/524) Allow tx evm raw run for generate only when offline with evm-denom flag. * (rpc) [#527](https://github.com/crypto-org-chain/ethermint/pull/527) Fix balance consistency between trace-block and state machine. +* (rpc) [#534](https://github.com/crypto-org-chain/ethermint/pull/534) Fix opBlockhash when no block header in abci request. * (rpc) [#536](https://github.com/crypto-org-chain/ethermint/pull/536) Fix validate basic after transaction conversion with raw field. * (cli) [#537](https://github.com/crypto-org-chain/ethermint/pull/537) Fix unsuppored sign mode SIGN_MODE_TEXTUAL for bank transfer. diff --git a/app/upgrades.go b/app/upgrades.go index f6289b9e20..bc1f660a43 100644 --- a/app/upgrades.go +++ b/app/upgrades.go @@ -19,14 +19,28 @@ import ( "context" upgradetypes "cosmossdk.io/x/upgrade/types" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" + evmtypes "github.com/evmos/ethermint/x/evm/types" ) func (app *EthermintApp) RegisterUpgradeHandlers() { planName := "sdk50" app.UpgradeKeeper.SetUpgradeHandler(planName, func(ctx context.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { - return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM) + m, err := app.ModuleManager.RunMigrations(ctx, app.configurator, fromVM) + if err != nil { + return m, err + } + sdkCtx := sdk.UnwrapSDKContext(ctx) + { + params := app.EvmKeeper.GetParams(sdkCtx) + params.HeaderHashNum = evmtypes.DefaultHeaderHashNum + if err := app.EvmKeeper.SetParams(sdkCtx, params); err != nil { + return m, err + } + } + return m, nil }, ) } diff --git a/client/docs/swagger-ui/swagger.yaml b/client/docs/swagger-ui/swagger.yaml index e8c1d48c18..eaa4cc6a98 100644 --- a/client/docs/swagger-ui/swagger.yaml +++ b/client/docs/swagger-ui/swagger.yaml @@ -140,6 +140,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -179,7 +183,6 @@ paths: name "y.z". - JSON @@ -357,6 +360,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -396,7 +403,6 @@ paths: name "y.z". - JSON @@ -574,6 +580,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -613,7 +623,6 @@ paths: name "y.z". - JSON @@ -784,6 +793,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -823,7 +836,6 @@ paths: name "y.z". - JSON @@ -1009,6 +1021,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -1048,7 +1064,6 @@ paths: name "y.z". - JSON @@ -1234,6 +1249,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -1273,7 +1292,6 @@ paths: name "y.z". - JSON @@ -1572,6 +1590,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -1611,7 +1633,6 @@ paths: name "y.z". - JSON @@ -1828,7 +1849,7 @@ paths: prague) description: >- ChainConfig defines the Ethereum ChainConfig parameters - using *sdk.Int values + using *sdkmath.Int values instead of *big.Int. allow_unprotected_txs: @@ -1838,6 +1859,10 @@ paths: EIP155 signed) transactions can be executed on the state machine. + header_hash_num: + type: string + format: uint64 + description: header_hash_num is the number of header hash to persist. title: Params defines the EVM module parameters description: >- QueryParamsResponse defines the response type for querying x/evm @@ -1955,6 +1980,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -1994,7 +2023,6 @@ paths: name "y.z". - JSON @@ -2168,6 +2196,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -2207,7 +2239,6 @@ paths: name "y.z". - JSON @@ -2389,6 +2420,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -2428,7 +2463,6 @@ paths: name "y.z". - JSON @@ -2833,6 +2867,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -2872,7 +2910,6 @@ paths: name "y.z". - JSON @@ -3289,6 +3326,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -3328,7 +3369,6 @@ paths: name "y.z". - JSON @@ -3434,7 +3474,7 @@ paths: required: false type: number format: double - - name: msg.hash + - name: msg.deprecated_hash description: hash of the transaction in hex format. in: query required: false @@ -3455,6 +3495,12 @@ paths: required: false type: string format: byte + - name: msg.raw + description: raw is the raw bytes of the ethereum transaction. + in: query + required: false + type: string + format: byte - name: trace_config.tracer description: tracer is a custom javascript tracer. in: query @@ -3835,6 +3881,10 @@ paths: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -3874,7 +3924,6 @@ paths: name "y.z". - JSON @@ -4014,7 +4063,7 @@ definitions: type: string title: prague switch time (nil = no fork, 0 = already on prague) description: >- - ChainConfig defines the Ethereum ChainConfig parameters using *sdk.Int + ChainConfig defines the Ethereum ChainConfig parameters using *sdkmath.Int values instead of *big.Int. @@ -4187,6 +4236,10 @@ definitions: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -4222,7 +4275,6 @@ definitions: name "y.z". - JSON @@ -4261,7 +4313,7 @@ definitions: type: number format: double title: size is the encoded storage size of the transaction (DEPRECATED) - hash: + deprecated_hash: type: string title: hash of the transaction in hex format deprecated_from: @@ -4276,6 +4328,10 @@ definitions: against the address derived from the signature (V, R, S) using the secp256k1 elliptic curve + raw: + type: string + format: byte + title: raw is the raw bytes of the ethereum transaction description: MsgEthereumTx encapsulates an Ethereum transaction as an SDK message. ethermint.evm.v1.MsgEthereumTxResponse: type: object @@ -4488,8 +4544,8 @@ definitions: type: string title: prague switch time (nil = no fork, 0 = already on prague) description: >- - ChainConfig defines the Ethereum ChainConfig parameters using *sdk.Int - values + ChainConfig defines the Ethereum ChainConfig parameters using + *sdkmath.Int values instead of *big.Int. allow_unprotected_txs: @@ -4497,6 +4553,10 @@ definitions: description: |- allow_unprotected_txs defines if replay-protected (i.e non EIP155 signed) transactions can be executed on the state machine. + header_hash_num: + type: string + format: uint64 + description: header_hash_num is the number of header hash to persist. title: Params defines the EVM module parameters ethermint.evm.v1.QueryAccountResponse: type: object @@ -4685,7 +4745,7 @@ definitions: title: prague switch time (nil = no fork, 0 = already on prague) description: >- ChainConfig defines the Ethereum ChainConfig parameters using - *sdk.Int values + *sdkmath.Int values instead of *big.Int. allow_unprotected_txs: @@ -4693,6 +4753,10 @@ definitions: description: |- allow_unprotected_txs defines if replay-protected (i.e non EIP155 signed) transactions can be executed on the state machine. + header_hash_num: + type: string + format: uint64 + description: header_hash_num is the number of header hash to persist. title: Params defines the EVM module parameters description: >- QueryParamsResponse defines the response type for querying x/evm @@ -4874,8 +4938,8 @@ definitions: type: string title: prague switch time (nil = no fork, 0 = already on prague) description: >- - ChainConfig defines the Ethereum ChainConfig parameters using *sdk.Int - values + ChainConfig defines the Ethereum ChainConfig parameters using + *sdkmath.Int values instead of *big.Int. enable_memory: @@ -4986,6 +5050,10 @@ definitions: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -5021,7 +5089,6 @@ definitions: name "y.z". - JSON @@ -5162,6 +5229,10 @@ definitions: if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } + // or ... + if (any.isSameTypeAs(Foo.getDefaultInstance())) { + foo = any.unpack(Foo.getDefaultInstance()); + } Example 3: Pack and unpack a message in Python. @@ -5197,7 +5268,6 @@ definitions: name "y.z". - JSON diff --git a/proto/ethermint/evm/v1/params.proto b/proto/ethermint/evm/v1/params.proto index c4c64323aa..48c0c944ce 100644 --- a/proto/ethermint/evm/v1/params.proto +++ b/proto/ethermint/evm/v1/params.proto @@ -22,4 +22,6 @@ message Params { // allow_unprotected_txs defines if replay-protected (i.e non EIP155 // signed) transactions can be executed on the state machine. bool allow_unprotected_txs = 6; + // header_hash_num is the number of header hash to persist. + uint64 header_hash_num = 7; } \ No newline at end of file diff --git a/tests/integration_tests/configs/cosmovisor.jsonnet b/tests/integration_tests/configs/cosmovisor.jsonnet index 4630f9a5ab..34ec86b27c 100644 --- a/tests/integration_tests/configs/cosmovisor.jsonnet +++ b/tests/integration_tests/configs/cosmovisor.jsonnet @@ -13,6 +13,11 @@ config { }, }, app_state+: { + evm+: { + params+: { + header_hash_num:: super.header_hash_num, + }, + }, feemarket+: { params+: { base_fee:: super.base_fee, diff --git a/tests/integration_tests/configs/default.jsonnet b/tests/integration_tests/configs/default.jsonnet index 1345ee682a..c90f910848 100644 --- a/tests/integration_tests/configs/default.jsonnet +++ b/tests/integration_tests/configs/default.jsonnet @@ -63,6 +63,7 @@ evm: { params: { evm_denom: 'aphoton', + header_hash_num: 2, }, }, gov: { diff --git a/tests/integration_tests/hardhat/contracts/TestBlockTxProperties.sol b/tests/integration_tests/hardhat/contracts/TestBlockTxProperties.sol new file mode 100644 index 0000000000..07d5b0d20d --- /dev/null +++ b/tests/integration_tests/hardhat/contracts/TestBlockTxProperties.sol @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract TestBlockTxProperties { + function getBlockHash(uint256 blockNumber) public view returns (bytes32) { + return blockhash(blockNumber); + } +} diff --git a/tests/integration_tests/test_block.py b/tests/integration_tests/test_block.py new file mode 100644 index 0000000000..f19b026e57 --- /dev/null +++ b/tests/integration_tests/test_block.py @@ -0,0 +1,14 @@ +from .utils import CONTRACTS, deploy_contract, w3_wait_for_new_blocks + + +def test_call(ethermint): + w3 = ethermint.w3 + contract, res = deploy_contract(w3, CONTRACTS["TestBlockTxProperties"]) + height = w3.eth.get_block_number() + w3_wait_for_new_blocks(w3, 1) + res = contract.caller.getBlockHash(height).hex() + blk = w3.eth.get_block(height) + assert f"0x{res}" == blk.hash.hex(), res + w3_wait_for_new_blocks(w3, 1) + res = contract.caller.getBlockHash(height).hex() + assert f"0x{res}" == "0x" + "0" * 64, res diff --git a/tests/integration_tests/utils.py b/tests/integration_tests/utils.py index 4eb1fdcfa3..b450c99eeb 100644 --- a/tests/integration_tests/utils.py +++ b/tests/integration_tests/utils.py @@ -43,6 +43,7 @@ "Calculator": "Calculator.sol", "Caller": "Caller.sol", "Random": "Random.sol", + "TestBlockTxProperties": "TestBlockTxProperties.sol", } diff --git a/x/evm/keeper/abci.go b/x/evm/keeper/abci.go index d890eb53f3..04c6d1442d 100644 --- a/x/evm/keeper/abci.go +++ b/x/evm/keeper/abci.go @@ -17,6 +17,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" + ethermint "github.com/evmos/ethermint/types" ) // BeginBlock sets the sdk Context and EIP155 chain id to the Keeper. @@ -24,10 +25,22 @@ 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 { + cfg, err := k.EVMBlockConfig(ctx, k.ChainID()) + if err != nil { return err } - + k.SetHeaderHash(ctx) + headerHashNum, err := ethermint.SafeInt64(cfg.Params.GetHeaderHashNum()) + if err != nil { + panic(err) + } + if i := ctx.BlockHeight() - headerHashNum; i > 0 { + h, err := ethermint.SafeUint64(i) + if err != nil { + panic(err) + } + k.DeleteHeaderHash(ctx, h) + } return nil } diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index 39997b685b..8689485435 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -305,3 +305,25 @@ func (k Keeper) AddTransientGasUsed(ctx sdk.Context, gasUsed uint64) (uint64, er k.SetTransientGasUsed(ctx, result) return result, nil } + +// 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) + } + 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)) +} + +// DeleteHeaderHash removes the hash of a block header from the store by height +func (k Keeper) DeleteHeaderHash(ctx sdk.Context, height uint64) { + store := ctx.KVStore(k.storeKey) + store.Delete(types.GetHeaderHashKey(height)) +} diff --git a/x/evm/keeper/state_transition.go b/x/evm/keeper/state_transition.go index d8eaccc49c..c6d493f56a 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -21,8 +21,6 @@ import ( "math/big" "sort" - cmttypes "github.com/cometbft/cometbft/types" - errorsmod "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" @@ -100,51 +98,18 @@ func (k Keeper) GetHashFn(ctx sdk.Context) vm.GetHashFunc { return func(height uint64) common.Hash { h, err := ethermint.SafeInt64(height) if err != nil { - k.Logger(ctx).Error("failed to cast height to int64", "error", err) return common.Hash{} } - - switch { - case ctx.BlockHeight() == h: - // Case 1: The requested height matches the one from the context so we can retrieve the header - // hash directly from the context. - // Note: The headerHash is only set at begin block, it will be nil in case of a query context + if ctx.BlockHeight() < h { + return common.Hash{} + } + if ctx.BlockHeight() == h { headerHash := ctx.HeaderHash() if len(headerHash) != 0 { return common.BytesToHash(headerHash) } - - // only recompute the hash if not set (eg: checkTxState) - contextBlockHeader := ctx.BlockHeader() - header, err := cmttypes.HeaderFromProto(&contextBlockHeader) - if err != nil { - k.Logger(ctx).Error("failed to cast tendermint header from proto", "error", err) - return common.Hash{} - } - - headerHash = header.Hash() - return common.BytesToHash(headerHash) - - case ctx.BlockHeight() > h: - // Case 2: if the chain is not the current height we need to retrieve the hash from the store for the - // current chain epoch. This only applies if the current height is greater than the requested height. - 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()) - default: - // Case 3: heights greater than the current one returns an empty hash. - return common.Hash{} } + return common.BytesToHash(k.GetHeaderHash(ctx, height)) } } diff --git a/x/evm/keeper/state_transition_test.go b/x/evm/keeper/state_transition_test.go index bdc95a1f87..cad82800be 100644 --- a/x/evm/keeper/state_transition_test.go +++ b/x/evm/keeper/state_transition_test.go @@ -5,12 +5,16 @@ import ( "math" "math/big" "testing" + "time" sdkmath "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" + cmtcrypto "github.com/cometbft/cometbft/crypto" "github.com/cometbft/cometbft/crypto/tmhash" - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + cmtrand "github.com/cometbft/cometbft/libs/rand" + cmtversion "github.com/cometbft/cometbft/proto/tendermint/version" tmtypes "github.com/cometbft/cometbft/types" + "github.com/cometbft/cometbft/version" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -84,86 +88,67 @@ func TestStateTransitionTestSuite(t *testing.T) { suite.Run(t, new(StateTransitionTestSuite)) } +func makeRandHeader(height uint64) tmtypes.Header { + chainID := "test" + t := time.Now() + randBytes := cmtrand.Bytes(tmhash.Size) + randAddress := cmtrand.Bytes(cmtcrypto.AddressSize) + h := tmtypes.Header{ + Version: cmtversion.Consensus{Block: version.BlockProtocol, App: 1}, + ChainID: chainID, + Height: int64(height), + Time: t, + LastBlockID: tmtypes.BlockID{}, + LastCommitHash: randBytes, + DataHash: randBytes, + ValidatorsHash: randBytes, + NextValidatorsHash: randBytes, + ConsensusHash: randBytes, + AppHash: randBytes, + LastResultsHash: randBytes, + EvidenceHash: randBytes, + ProposerAddress: randAddress, + } + return h +} + +func (suite *StateTransitionTestSuite) registerHeader(header tmtypes.Header) { + suite.Ctx.WithBlockHeight(header.Height) + suite.Ctx.WithHeaderHash(header.Hash()) + suite.App.EvmKeeper.SetHeaderHash(suite.Ctx) +} + func (suite *StateTransitionTestSuite) TestGetHashFn() { - header := suite.Ctx.BlockHeader() - h, _ := tmtypes.HeaderFromProto(&header) - hash := h.Hash() + height := uint64(10) + header := makeRandHeader(height) + hash := header.Hash() testCases := []struct { msg string height uint64 - malleate func() + malleate func(height uint64) expHash common.Hash }{ { - "case 1.1: context hash cached", - uint64(suite.Ctx.BlockHeight()), - func() { - suite.Ctx = suite.Ctx.WithHeaderHash(tmhash.Sum([]byte("header"))).WithConsensusParams(*testutil.DefaultConsensusParams) - }, - common.BytesToHash(tmhash.Sum([]byte("header"))), - }, - { - "case 1.2: failed to cast Tendermint header", - uint64(suite.Ctx.BlockHeight()), - func() { - header := tmproto.Header{} - header.Height = suite.Ctx.BlockHeight() - suite.Ctx = suite.Ctx.WithBlockHeader(header).WithConsensusParams(*testutil.DefaultConsensusParams) - }, + "header not found", + height, + func(height uint64) {}, common.Hash{}, }, { - "case 1.3: hash calculated from Tendermint header", - uint64(suite.Ctx.BlockHeight()), - func() { - suite.Ctx = suite.Ctx.WithBlockHeader(header).WithConsensusParams(*testutil.DefaultConsensusParams) + "header found", + height, + func(height uint64) { + suite.Ctx = suite.Ctx.WithBlockHeight(header.Height).WithHeaderHash(header.Hash()) + suite.App.EvmKeeper.SetHeaderHash(suite.Ctx) }, common.BytesToHash(hash), }, - { - "case 2.1: height lower than current one, hist info not found", - 1, - func() { - suite.Ctx = suite.Ctx.WithBlockHeight(10).WithConsensusParams(*testutil.DefaultConsensusParams) - }, - common.Hash{}, - }, - { - "case 2.2: height lower than current one, invalid hist info header", - 1, - func() { - suite.App.StakingKeeper.SetHistoricalInfo(suite.Ctx, 1, &stakingtypes.HistoricalInfo{}) - suite.Ctx = suite.Ctx.WithBlockHeight(10).WithConsensusParams(*testutil.DefaultConsensusParams) - }, - common.Hash{}, - }, - { - "case 2.3: height lower than current one, calculated from hist info header", - 1, - func() { - histInfo := &stakingtypes.HistoricalInfo{ - Header: header, - } - suite.App.StakingKeeper.SetHistoricalInfo(suite.Ctx, 1, histInfo) - suite.Ctx = suite.Ctx.WithBlockHeight(10).WithConsensusParams(*testutil.DefaultConsensusParams) - }, - common.BytesToHash(hash), - }, - { - "case 3: height greater than current one", - 200, - func() {}, - common.Hash{}, - }, } - for _, tc := range testCases { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset - - tc.malleate() - + tc.malleate(tc.height) hash := suite.App.EvmKeeper.GetHashFn(suite.Ctx)(tc.height) suite.Require().Equal(tc.expHash, hash) }) diff --git a/x/evm/types/key.go b/x/evm/types/key.go index b3b7173ba9..569ce7bb90 100644 --- a/x/evm/types/key.go +++ b/x/evm/types/key.go @@ -44,6 +44,7 @@ const ( prefixCode = iota + 1 prefixStorage prefixParams + prefixHeaderHash ) // prefix bytes for the EVM object store @@ -55,9 +56,10 @@ const ( // KVStore key prefixes var ( - KeyPrefixCode = []byte{prefixCode} - KeyPrefixStorage = []byte{prefixStorage} - KeyPrefixParams = []byte{prefixParams} + KeyPrefixCode = []byte{prefixCode} + KeyPrefixStorage = []byte{prefixStorage} + KeyPrefixParams = []byte{prefixParams} + KeyPrefixHeaderHash = []byte{prefixHeaderHash} ) // Object Store key prefixes @@ -110,3 +112,10 @@ func ObjectBloomKey(txIndex, msgIndex int) []byte { binary.BigEndian.PutUint64(key[9:], value) return key[:] } + +func GetHeaderHashKey(height uint64) []byte { + var key [1 + 8]byte + key[0] = prefixHeaderHash + binary.BigEndian.PutUint64(key[1:], height) + return key[:] +} diff --git a/x/evm/types/params.go b/x/evm/types/params.go index 27d84155cd..f18b65f4e5 100644 --- a/x/evm/types/params.go +++ b/x/evm/types/params.go @@ -34,6 +34,8 @@ var ( DefaultEnableCreate = true // DefaultEnableCall enables contract calls (i.e true) DefaultEnableCall = true + // DefaultHeaderHashNum defines the default number of header hash to persist. + DefaultHeaderHashNum = uint64(10000) ) // NewParams creates a new Params instance @@ -58,6 +60,7 @@ func DefaultParams() Params { EnableCall: DefaultEnableCall, ChainConfig: config, AllowUnprotectedTxs: DefaultAllowUnprotectedTxs, + HeaderHashNum: DefaultHeaderHashNum, } } diff --git a/x/evm/types/params.pb.go b/x/evm/types/params.pb.go index ed314c775e..165d458898 100644 --- a/x/evm/types/params.pb.go +++ b/x/evm/types/params.pb.go @@ -39,6 +39,8 @@ type Params struct { // allow_unprotected_txs defines if replay-protected (i.e non EIP155 // signed) transactions can be executed on the state machine. 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"` } func (m *Params) Reset() { *m = Params{} } @@ -116,6 +118,13 @@ func (m *Params) GetAllowUnprotectedTxs() bool { return false } +func (m *Params) GetHeaderHashNum() uint64 { + if m != nil { + return m.HeaderHashNum + } + return 0 +} + func init() { proto.RegisterType((*Params)(nil), "ethermint.evm.v1.Params") } @@ -123,32 +132,34 @@ func init() { func init() { proto.RegisterFile("ethermint/evm/v1/params.proto", fileDescriptor_e7d3c06c1322f20f) } var fileDescriptor_e7d3c06c1322f20f = []byte{ - // 391 bytes of a gzipped FileDescriptorProto + // 423 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x64, 0x91, 0x41, 0x8b, 0xd3, 0x40, - 0x18, 0x86, 0x93, 0xed, 0x5a, 0x36, 0xd3, 0x15, 0xd6, 0xb1, 0x4a, 0x58, 0xd8, 0x24, 0x44, 0x90, - 0x9c, 0x12, 0xba, 0x1e, 0x04, 0x41, 0xd0, 0xd4, 0x0a, 0xde, 0x96, 0xa0, 0x17, 0x2f, 0x61, 0x9a, - 0x7e, 0xa6, 0x81, 0x99, 0x4c, 0xc8, 0x4c, 0x63, 0xfa, 0x2f, 0xfc, 0x59, 0x3d, 0xf6, 0xe8, 0x29, - 0x48, 0xfa, 0x0f, 0xf2, 0x0b, 0x24, 0xd3, 0xda, 0x56, 0x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f5, 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, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -171,6 +182,11 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.HeaderHashNum != 0 { + i = encodeVarintParams(dAtA, i, uint64(m.HeaderHashNum)) + i-- + dAtA[i] = 0x38 + } if m.AllowUnprotectedTxs { i-- if m.AllowUnprotectedTxs { @@ -279,6 +295,9 @@ func (m *Params) Size() (n int) { if m.AllowUnprotectedTxs { n += 2 } + if m.HeaderHashNum != 0 { + n += 1 + sovParams(uint64(m.HeaderHashNum)) + } return n } @@ -518,6 +537,25 @@ func (m *Params) Unmarshal(dAtA []byte) error { } } m.AllowUnprotectedTxs = bool(v != 0) + case 7: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field HeaderHashNum", wireType) + } + m.HeaderHashNum = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParams + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.HeaderHashNum |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipParams(dAtA[iNdEx:])