From 326426fa61f6424d809a933ea287e46967e04b80 Mon Sep 17 00:00:00 2001 From: Taeguk Kwon Date: Mon, 25 Sep 2023 23:23:47 +0900 Subject: [PATCH] fix(evm): missing things when init or export genesis (#1129) Co-authored-by: steve806 --- cosmos/x/evm/plugins/state/genesis.go | 21 +++++++++++++++++++++ cosmos/x/evm/plugins/state/plugin.go | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) 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 // =============================================================================