From 11c57943e2f7f715cd7774e3360f78e8b1ded902 Mon Sep 17 00:00:00 2001 From: Cal Bera Date: Mon, 25 Sep 2023 11:29:58 -0400 Subject: [PATCH] fix(evm): revive #1129 (missing things when init or export genesis) (#1138) --- cosmos/x/evm/plugins/state/genesis.go | 21 +++++++++++++++++++ cosmos/x/evm/plugins/state/plugin.go | 21 +++++++++++++++++++ .../get-account-proof-with-storage.io | 2 +- .../tests/eth_getProof/get-account-proof.io | 2 +- .../get-account-nonce.io | 2 +- eth/common/imported.go | 1 + 6 files changed, 46 insertions(+), 3 deletions(-) diff --git a/cosmos/x/evm/plugins/state/genesis.go b/cosmos/x/evm/plugins/state/genesis.go index b456587eb..66720f9e3 100644 --- a/cosmos/x/evm/plugins/state/genesis.go +++ b/cosmos/x/evm/plugins/state/genesis.go @@ -48,6 +48,9 @@ func (p *plugin) InitGenesis(ctx sdk.Context, ethGen *core.Genesis) { p.SetState(address, k, v) } } + if account.Nonce != 0 { + p.SetNonce(address, account.Nonce) + } } p.Finalize() } @@ -68,6 +71,7 @@ func (p *plugin) ExportGenesis(ctx sdk.Context, ethGen *core.Genesis) { account.Storage = make(map[common.Hash]common.Hash) } account.Balance = p.GetBalance(address) + account.Nonce = p.GetNonce(address) ethGen.Alloc[address] = account return false }) @@ -85,6 +89,23 @@ func (p *plugin) ExportGenesis(ctx sdk.Context, ethGen *core.Genesis) { account.Code = p.GetCode(address) account.Balance = p.GetBalance(address) + account.Nonce = p.GetNonce(address) + ethGen.Alloc[address] = account + + return false + }) + + // Iterate Code and set the genesis accounts. + p.IterateCode(func(address common.Address, codeHash common.Hash) bool { + account, ok := ethGen.Alloc[address] + if !ok { + account = core.GenesisAccount{} + } + account.Code = p.GetCode(address) + account.Nonce = p.GetNonce(address) + if account.Balance == nil { + account.Balance = big.NewInt(0) + } ethGen.Alloc[address] = account return false diff --git a/cosmos/x/evm/plugins/state/plugin.go b/cosmos/x/evm/plugins/state/plugin.go index aa298bbc2..bf6504c8f 100644 --- a/cosmos/x/evm/plugins/state/plugin.go +++ b/cosmos/x/evm/plugins/state/plugin.go @@ -362,6 +362,27 @@ func (p *plugin) SetCode(addr common.Address, code []byte) { } } +// IterateCode iterates over all the contract code, and calls the given function. +func (p *plugin) IterateCode(fn func(addr common.Address, value common.Hash) bool) { + it := storetypes.KVStorePrefixIterator( + p.cms.GetKVStore(p.storeKey), + []byte{types.CodeHashKeyPrefix}, + ) + defer func() { + if err := it.Close(); err != nil { + p.savedErr = err + } + }() + + for ; it.Valid(); it.Next() { + k := it.Key() + addr := AddressFromCodeHashKey(k) + if fn(addr, p.GetCodeHash(addr)) { + break + } + } +} + // ============================================================================= // Storage // ============================================================================= diff --git a/e2e/hive/simulators/rpc-compat/tests/eth_getProof/get-account-proof-with-storage.io b/e2e/hive/simulators/rpc-compat/tests/eth_getProof/get-account-proof-with-storage.io index d051ce621..3dd98c33f 100644 --- a/e2e/hive/simulators/rpc-compat/tests/eth_getProof/get-account-proof-with-storage.io +++ b/e2e/hive/simulators/rpc-compat/tests/eth_getProof/get-account-proof-with-storage.io @@ -1,2 +1,2 @@ >> {"jsonrpc":"2.0","id":1,"method":"eth_getProof","params":["0xaa00000000000000000000000000000000000000",["0x1"],"0x3"]} -<< {"jsonrpc":"2.0","id":1,"result":{"address":"0xaa00000000000000000000000000000000000000","accountProof":[],"balance":"0x1","codeHash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470","nonce":"0x0","storageHash":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","storageProof":[{"key":"0x1","value":"0x0","proof":[]}]}} +<< {"jsonrpc":"2.0","id":1,"result":{"address":"0xaa00000000000000000000000000000000000000","accountProof":[],"balance":"0x1","codeHash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470","nonce":"0x1","storageHash":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","storageProof":[{"key":"0x1","value":"0x0","proof":[]}]}} diff --git a/e2e/hive/simulators/rpc-compat/tests/eth_getProof/get-account-proof.io b/e2e/hive/simulators/rpc-compat/tests/eth_getProof/get-account-proof.io index 7bf3353ad..295782b89 100644 --- a/e2e/hive/simulators/rpc-compat/tests/eth_getProof/get-account-proof.io +++ b/e2e/hive/simulators/rpc-compat/tests/eth_getProof/get-account-proof.io @@ -1,2 +1,2 @@ >> {"jsonrpc":"2.0","id":1,"method":"eth_getProof","params":["0xaa00000000000000000000000000000000000000",[],"0x3"]} -<< {"jsonrpc":"2.0","id":1,"result":{"address":"0xaa00000000000000000000000000000000000000","accountProof":[],"balance":"0x1","codeHash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470","nonce":"0x0","storageHash":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","storageProof":[]}} +<< {"jsonrpc":"2.0","id":1,"result":{"address":"0xaa00000000000000000000000000000000000000","accountProof":[],"balance":"0x1","codeHash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470","nonce":"0x1","storageHash":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","storageProof":[]}} diff --git a/e2e/hive/simulators/rpc-compat/tests/eth_getTransactionCount/get-account-nonce.io b/e2e/hive/simulators/rpc-compat/tests/eth_getTransactionCount/get-account-nonce.io index 956051ede..4edec43e9 100644 --- a/e2e/hive/simulators/rpc-compat/tests/eth_getTransactionCount/get-account-nonce.io +++ b/e2e/hive/simulators/rpc-compat/tests/eth_getTransactionCount/get-account-nonce.io @@ -1,2 +1,2 @@ >> {"jsonrpc":"2.0","id":1,"method":"eth_getTransactionCount","params":["0xaa00000000000000000000000000000000000000","latest"]} -<< {"jsonrpc":"2.0","id":1,"result":"0x0"} +<< {"jsonrpc":"2.0","id":1,"result":"0x1"} diff --git a/eth/common/imported.go b/eth/common/imported.go index f9b01534c..a1904ac3e 100644 --- a/eth/common/imported.go +++ b/eth/common/imported.go @@ -39,6 +39,7 @@ var ( Bytes2Hex = common.Bytes2Hex FromHex = common.FromHex HexToAddress = common.HexToAddress + IsHexAddress = common.IsHexAddress Hex2Bytes = common.Hex2Bytes HexToHash = common.HexToHash LeftPadBytes = common.LeftPadBytes