Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
fix(evm): revive #1129 (missing things when init or export genesis) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
calbera authored Sep 25, 2023
1 parent bc36040 commit 11c5794
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
21 changes: 21 additions & 0 deletions cosmos/x/evm/plugins/state/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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
})
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions cosmos/x/evm/plugins/state/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// =============================================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -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":[]}]}}
Original file line number Diff line number Diff line change
@@ -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":[]}}
Original file line number Diff line number Diff line change
@@ -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"}
1 change: 1 addition & 0 deletions eth/common/imported.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 11c5794

Please sign in to comment.