2024-06-10 10:47:09 +00:00
|
|
|
# Fluffy
|
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2024-06-10 10:47:09 +00:00
|
|
|
import
|
|
|
|
results,
|
|
|
|
chronos,
|
|
|
|
chronicles,
|
2024-10-04 21:21:26 +00:00
|
|
|
eth/rlp,
|
|
|
|
eth/common/[hashes, accounts, addresses],
|
2024-06-10 10:47:09 +00:00
|
|
|
./state_network,
|
|
|
|
./state_utils
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
from eth/common/eth_types_rlp import rlpHash
|
|
|
|
|
|
|
|
export results, state_network, hashes, addresses
|
2024-06-10 10:47:09 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "portal_state"
|
|
|
|
|
|
|
|
proc getNextNodeHash(
|
|
|
|
trieNode: TrieNode, nibbles: UnpackedNibbles, nibbleIdx: var int
|
2024-10-07 20:39:07 +00:00
|
|
|
): Opt[(Nibbles, Hash32)] =
|
2024-06-14 05:38:24 +00:00
|
|
|
# the trie node should have already been validated against the lookup hash
|
|
|
|
# so we expect that no rlp errors should be possible
|
|
|
|
try:
|
|
|
|
doAssert(nibbles.len() > 0)
|
|
|
|
doAssert(nibbleIdx < nibbles.len())
|
|
|
|
|
|
|
|
let trieNodeRlp = rlpFromBytes(trieNode.asSeq())
|
|
|
|
|
|
|
|
doAssert(not trieNodeRlp.isEmpty())
|
|
|
|
doAssert(trieNodeRlp.listLen() == 2 or trieNodeRlp.listLen() == 17)
|
|
|
|
|
|
|
|
if trieNodeRlp.listLen() == 17:
|
|
|
|
let nextNibble = nibbles[nibbleIdx]
|
|
|
|
doAssert(nextNibble < 16)
|
|
|
|
|
2024-06-19 16:21:23 +00:00
|
|
|
let nextHashBytes = trieNodeRlp.listElem(nextNibble.int).toBytes()
|
|
|
|
if nextHashBytes.len() == 0:
|
2024-10-07 20:39:07 +00:00
|
|
|
return Opt.none((Nibbles, Hash32))
|
2024-06-14 05:38:24 +00:00
|
|
|
|
|
|
|
nibbleIdx += 1
|
|
|
|
return Opt.some(
|
2024-10-04 21:21:26 +00:00
|
|
|
(nibbles[0 ..< nibbleIdx].packNibbles(), Hash32.fromBytes(nextHashBytes))
|
2024-06-14 05:38:24 +00:00
|
|
|
)
|
2024-06-10 10:47:09 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
# leaf or extension node
|
2024-08-23 07:46:23 +00:00
|
|
|
let
|
|
|
|
(_, isLeaf, prefix) = decodePrefix(trieNodeRlp.listElem(0))
|
|
|
|
unpackedPrefix = prefix.unpackNibbles()
|
|
|
|
|
|
|
|
if unpackedPrefix != nibbles[nibbleIdx ..< nibbleIdx + unpackedPrefix.len()]:
|
|
|
|
# The nibbles don't match so we stop the search and don't increment
|
|
|
|
# the nibble index to indicate how many nibbles were consumed
|
2024-10-07 20:39:07 +00:00
|
|
|
return Opt.none((Nibbles, Hash32))
|
2024-08-23 07:46:23 +00:00
|
|
|
|
|
|
|
nibbleIdx += prefix.unpackNibbles().len()
|
2024-06-14 05:38:24 +00:00
|
|
|
if isLeaf:
|
2024-10-07 20:39:07 +00:00
|
|
|
return Opt.none((Nibbles, Hash32))
|
2024-06-10 10:47:09 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
# extension node
|
2024-06-19 16:21:23 +00:00
|
|
|
let nextHashBytes = trieNodeRlp.listElem(1).toBytes()
|
|
|
|
if nextHashBytes.len() == 0:
|
2024-10-07 20:39:07 +00:00
|
|
|
return Opt.none((Nibbles, Hash32))
|
2024-06-10 10:47:09 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
Opt.some((nibbles[0 ..< nibbleIdx].packNibbles(), Hash32.fromBytes(nextHashBytes)))
|
2024-06-14 05:38:24 +00:00
|
|
|
except RlpError as e:
|
|
|
|
raiseAssert(e.msg)
|
2024-06-10 10:47:09 +00:00
|
|
|
|
|
|
|
proc getAccountProof(
|
2024-11-28 12:33:57 +00:00
|
|
|
n: StateNetwork,
|
|
|
|
stateRoot: Hash32,
|
|
|
|
address: Address,
|
|
|
|
maybeBlockHash: Opt[Hash32], # required for poke
|
2024-09-13 03:46:35 +00:00
|
|
|
): Future[Result[(TrieProof, bool), string]] {.async: (raises: [CancelledError]).} =
|
2024-06-10 10:47:09 +00:00
|
|
|
let nibbles = address.toPath().unpackNibbles()
|
|
|
|
|
|
|
|
var
|
|
|
|
nibblesIdx = 0
|
|
|
|
key = AccountTrieNodeKey.init(Nibbles.empty(), stateRoot)
|
|
|
|
proof = TrieProof.empty()
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
# Construct the parent offer which is used to provide the data needed to
|
|
|
|
# implement poke after the account trie node has been retrieved
|
|
|
|
maybeParentOffer =
|
|
|
|
if maybeBlockHash.isSome():
|
|
|
|
Opt.some(AccountTrieNodeOffer.init(proof, maybeBlockHash.get()))
|
|
|
|
else:
|
|
|
|
Opt.none(AccountTrieNodeOffer)
|
2024-08-28 08:27:36 +00:00
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
while nibblesIdx < nibbles.len():
|
2024-06-10 10:47:09 +00:00
|
|
|
let
|
2024-11-28 12:33:57 +00:00
|
|
|
accountTrieNode = (await n.getAccountTrieNode(key, maybeParentOffer)).valueOr:
|
|
|
|
return err("Failed to get account trie node when building account proof")
|
2024-06-10 10:47:09 +00:00
|
|
|
trieNode = accountTrieNode.node
|
2024-08-28 08:27:36 +00:00
|
|
|
added = proof.add(trieNode)
|
2024-06-10 10:47:09 +00:00
|
|
|
doAssert(added)
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
if maybeParentOffer.isSome():
|
|
|
|
let added = maybeParentOffer.get().proof.add(trieNode)
|
|
|
|
doAssert(added)
|
|
|
|
|
2024-06-10 10:47:09 +00:00
|
|
|
let (nextPath, nextNodeHash) = trieNode.getNextNodeHash(nibbles, nibblesIdx).valueOr:
|
|
|
|
break
|
|
|
|
|
|
|
|
key = AccountTrieNodeKey.init(nextPath, nextNodeHash)
|
|
|
|
|
2024-09-13 03:46:35 +00:00
|
|
|
doAssert(nibblesIdx <= nibbles.len())
|
|
|
|
ok((proof, nibblesIdx == nibbles.len()))
|
2024-06-10 10:47:09 +00:00
|
|
|
|
|
|
|
proc getStorageProof(
|
2024-11-28 12:33:57 +00:00
|
|
|
n: StateNetwork,
|
|
|
|
storageRoot: Hash32,
|
|
|
|
address: Address,
|
|
|
|
slotKey: UInt256,
|
|
|
|
maybeBlockHash: Opt[Hash32],
|
|
|
|
maybeAccProof: Opt[TrieProof],
|
2024-09-13 03:46:35 +00:00
|
|
|
): Future[Result[(TrieProof, bool), string]] {.async: (raises: [CancelledError]).} =
|
|
|
|
let nibbles = slotKey.toPath().unpackNibbles()
|
2024-06-10 10:47:09 +00:00
|
|
|
|
|
|
|
var
|
2024-10-04 21:21:26 +00:00
|
|
|
addressHash = keccak256(address.data)
|
2024-06-10 10:47:09 +00:00
|
|
|
nibblesIdx = 0
|
2024-08-07 16:01:30 +00:00
|
|
|
key = ContractTrieNodeKey.init(addressHash, Nibbles.empty(), storageRoot)
|
2024-06-10 10:47:09 +00:00
|
|
|
proof = TrieProof.empty()
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
# Construct the parent offer which is used to provide the data needed to
|
|
|
|
# implement poke after the contract trie node has been retrieved
|
|
|
|
maybeParentOffer =
|
|
|
|
if maybeBlockHash.isSome():
|
|
|
|
doAssert maybeAccProof.isSome()
|
|
|
|
Opt.some(
|
|
|
|
ContractTrieNodeOffer.init(proof, maybeAccProof.get(), maybeBlockHash.get())
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
Opt.none(ContractTrieNodeOffer)
|
2024-08-28 08:27:36 +00:00
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
while nibblesIdx < nibbles.len():
|
2024-06-10 10:47:09 +00:00
|
|
|
let
|
2024-11-28 12:33:57 +00:00
|
|
|
contractTrieNode = (await n.getContractTrieNode(key, maybeParentOffer)).valueOr:
|
|
|
|
return err("Failed to get contract trie node when building storage proof")
|
2024-06-10 10:47:09 +00:00
|
|
|
trieNode = contractTrieNode.node
|
2024-08-28 08:27:36 +00:00
|
|
|
added = proof.add(trieNode)
|
2024-06-10 10:47:09 +00:00
|
|
|
doAssert(added)
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
if maybeParentOffer.isSome():
|
|
|
|
let added = maybeParentOffer.get().storageProof.add(trieNode)
|
|
|
|
doAssert(added)
|
|
|
|
|
2024-06-10 10:47:09 +00:00
|
|
|
let (nextPath, nextNodeHash) = trieNode.getNextNodeHash(nibbles, nibblesIdx).valueOr:
|
|
|
|
break
|
|
|
|
|
2024-08-07 16:01:30 +00:00
|
|
|
key = ContractTrieNodeKey.init(addressHash, nextPath, nextNodeHash)
|
2024-06-10 10:47:09 +00:00
|
|
|
|
2024-09-13 03:46:35 +00:00
|
|
|
doAssert(nibblesIdx <= nibbles.len())
|
|
|
|
ok((proof, nibblesIdx == nibbles.len()))
|
2024-06-10 10:47:09 +00:00
|
|
|
|
|
|
|
proc getAccount(
|
2024-11-28 12:33:57 +00:00
|
|
|
n: StateNetwork, stateRoot: Hash32, address: Address, maybeBlockHash: Opt[Hash32]
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[Account]] {.async: (raises: [CancelledError]).} =
|
2024-11-28 12:33:57 +00:00
|
|
|
let (accountProof, exists) = (
|
|
|
|
await n.getAccountProof(stateRoot, address, maybeBlockHash)
|
|
|
|
).valueOr:
|
2024-09-13 03:46:35 +00:00
|
|
|
warn "Failed to get account proof", error = error
|
|
|
|
return Opt.none(Account)
|
|
|
|
|
|
|
|
if not exists:
|
|
|
|
info "Account doesn't exist, returning default account"
|
|
|
|
# return an empty account if the account doesn't exist
|
2024-10-04 21:21:26 +00:00
|
|
|
return Opt.some(EMPTY_ACCOUNT)
|
2024-09-13 03:46:35 +00:00
|
|
|
|
|
|
|
let account = accountProof.toAccount().valueOr:
|
|
|
|
error "Failed to get account from accountProof"
|
|
|
|
return Opt.none(Account)
|
2024-06-10 10:47:09 +00:00
|
|
|
|
|
|
|
Opt.some(account)
|
|
|
|
|
2024-08-27 12:35:27 +00:00
|
|
|
proc getBalanceByStateRoot*(
|
2024-11-28 12:33:57 +00:00
|
|
|
n: StateNetwork,
|
|
|
|
stateRoot: Hash32,
|
|
|
|
address: Address,
|
|
|
|
maybeBlockHash = Opt.none(Hash32),
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[UInt256]] {.async: (raises: [CancelledError]).} =
|
2024-11-28 12:33:57 +00:00
|
|
|
let account = (await n.getAccount(stateRoot, address, maybeBlockHash)).valueOr:
|
2024-06-10 10:47:09 +00:00
|
|
|
return Opt.none(UInt256)
|
|
|
|
|
|
|
|
Opt.some(account.balance)
|
|
|
|
|
2024-08-27 12:35:27 +00:00
|
|
|
proc getTransactionCountByStateRoot*(
|
2024-11-28 12:33:57 +00:00
|
|
|
n: StateNetwork,
|
|
|
|
stateRoot: Hash32,
|
|
|
|
address: Address,
|
|
|
|
maybeBlockHash = Opt.none(Hash32),
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[AccountNonce]] {.async: (raises: [CancelledError]).} =
|
2024-11-28 12:33:57 +00:00
|
|
|
let account = (await n.getAccount(stateRoot, address, maybeBlockHash)).valueOr:
|
2024-06-10 10:47:09 +00:00
|
|
|
return Opt.none(AccountNonce)
|
|
|
|
|
|
|
|
Opt.some(account.nonce)
|
|
|
|
|
2024-08-27 12:35:27 +00:00
|
|
|
proc getStorageAtByStateRoot*(
|
2024-11-28 12:33:57 +00:00
|
|
|
n: StateNetwork,
|
|
|
|
stateRoot: Hash32,
|
|
|
|
address: Address,
|
|
|
|
slotKey: UInt256,
|
|
|
|
maybeBlockHash = Opt.none(Hash32),
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[UInt256]] {.async: (raises: [CancelledError]).} =
|
2024-11-28 12:33:57 +00:00
|
|
|
let (accountProof, exists) = (
|
|
|
|
await n.getAccountProof(stateRoot, address, maybeBlockHash)
|
|
|
|
).valueOr:
|
|
|
|
warn "Failed to get account proof", error = error
|
2024-08-28 08:27:36 +00:00
|
|
|
return Opt.none(UInt256)
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
let account =
|
|
|
|
if exists:
|
|
|
|
accountProof.toAccount().valueOr:
|
|
|
|
error "Failed to get account from accountProof"
|
|
|
|
return Opt.none(UInt256)
|
|
|
|
else:
|
|
|
|
info "Account doesn't exist, returning default account"
|
|
|
|
# return an empty account if the account doesn't exist
|
|
|
|
EMPTY_ACCOUNT
|
|
|
|
|
2024-08-28 08:27:36 +00:00
|
|
|
if account.storageRoot == EMPTY_ROOT_HASH:
|
|
|
|
info "Storage doesn't exist, returning default storage value"
|
|
|
|
# return zero if the storage doesn't exist
|
|
|
|
return Opt.some(0.u256)
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
let (storageProof, slotExists) = (
|
|
|
|
await n.getStorageProof(
|
|
|
|
account.storageRoot, address, slotKey, maybeBlockHash, Opt.some(accountProof)
|
|
|
|
)
|
2024-09-13 03:46:35 +00:00
|
|
|
).valueOr:
|
|
|
|
warn "Failed to get storage proof", error = error
|
|
|
|
return Opt.none(UInt256)
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
if not slotExists:
|
2024-09-13 03:46:35 +00:00
|
|
|
info "Slot doesn't exist, returning default storage value"
|
|
|
|
# return zero if the slot doesn't exist
|
|
|
|
return Opt.some(0.u256)
|
|
|
|
|
|
|
|
let slotValue = storageProof.toSlot().valueOr:
|
|
|
|
error "Failed to get slot from storageProof"
|
|
|
|
return Opt.none(UInt256)
|
2024-06-10 10:47:09 +00:00
|
|
|
|
|
|
|
Opt.some(slotValue)
|
|
|
|
|
2024-08-27 12:35:27 +00:00
|
|
|
proc getCodeByStateRoot*(
|
2024-11-28 12:33:57 +00:00
|
|
|
n: StateNetwork,
|
|
|
|
stateRoot: Hash32,
|
|
|
|
address: Address,
|
|
|
|
maybeBlockHash = Opt.none(Hash32),
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[Bytecode]] {.async: (raises: [CancelledError]).} =
|
2024-11-28 12:33:57 +00:00
|
|
|
let (accountProof, exists) = (
|
|
|
|
await n.getAccountProof(stateRoot, address, maybeBlockHash)
|
|
|
|
).valueOr:
|
|
|
|
warn "Failed to get account proof", error = error
|
2024-08-28 08:27:36 +00:00
|
|
|
return Opt.none(Bytecode)
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
let account =
|
|
|
|
if exists:
|
|
|
|
accountProof.toAccount().valueOr:
|
|
|
|
error "Failed to get account from accountProof"
|
|
|
|
return Opt.none(Bytecode)
|
|
|
|
else:
|
|
|
|
info "Account doesn't exist, returning default account"
|
|
|
|
# return an empty account if the account doesn't exist
|
|
|
|
EMPTY_ACCOUNT
|
|
|
|
|
2024-08-28 08:27:36 +00:00
|
|
|
if account.codeHash == EMPTY_CODE_HASH:
|
|
|
|
info "Code doesn't exist, returning default code value"
|
|
|
|
# return empty bytecode if the code doesn't exist
|
|
|
|
return Opt.some(Bytecode.empty())
|
|
|
|
|
2024-06-10 10:47:09 +00:00
|
|
|
let
|
2024-10-04 21:21:26 +00:00
|
|
|
contractCodeKey = ContractCodeKey.init(keccak256(address.data), account.codeHash)
|
2024-11-28 12:33:57 +00:00
|
|
|
maybeParentOffer =
|
|
|
|
if maybeBlockHash.isSome():
|
|
|
|
Opt.some(
|
|
|
|
ContractCodeOffer.init(Bytecode.empty(), accountProof, maybeBlockHash.get())
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
Opt.none(ContractCodeOffer)
|
|
|
|
contractCodeRetrieval = (await n.getContractCode(contractCodeKey, maybeParentOffer)).valueOr:
|
2024-08-28 08:27:36 +00:00
|
|
|
warn "Failed to get contract code"
|
|
|
|
return Opt.none(Bytecode)
|
2024-06-10 10:47:09 +00:00
|
|
|
|
|
|
|
Opt.some(contractCodeRetrieval.code)
|
2024-08-27 12:35:27 +00:00
|
|
|
|
2024-09-13 03:46:35 +00:00
|
|
|
type Proofs* = ref object
|
|
|
|
account*: Account
|
|
|
|
accountProof*: TrieProof
|
|
|
|
slots*: seq[(UInt256, UInt256)]
|
|
|
|
slotProofs*: seq[TrieProof]
|
|
|
|
|
|
|
|
proc getProofsByStateRoot*(
|
2024-11-28 12:33:57 +00:00
|
|
|
n: StateNetwork,
|
|
|
|
stateRoot: Hash32,
|
|
|
|
address: Address,
|
|
|
|
slotKeys: seq[UInt256],
|
|
|
|
maybeBlockHash = Opt.none(Hash32),
|
2024-09-13 03:46:35 +00:00
|
|
|
): Future[Opt[Proofs]] {.async: (raises: [CancelledError]).} =
|
|
|
|
let
|
2024-11-28 12:33:57 +00:00
|
|
|
(accountProof, accountExists) = (
|
|
|
|
await n.getAccountProof(stateRoot, address, maybeBlockHash)
|
|
|
|
).valueOr:
|
2024-09-13 03:46:35 +00:00
|
|
|
warn "Failed to get account proof", error = error
|
|
|
|
return Opt.none(Proofs)
|
|
|
|
account =
|
|
|
|
if accountExists:
|
|
|
|
accountProof.toAccount().valueOr:
|
|
|
|
error "Failed to get account from accountProof"
|
|
|
|
return Opt.none(Proofs)
|
|
|
|
else:
|
2024-10-04 21:21:26 +00:00
|
|
|
EMPTY_ACCOUNT
|
2024-09-13 03:46:35 +00:00
|
|
|
storageExists = account.storageRoot != EMPTY_ROOT_HASH
|
|
|
|
|
|
|
|
var
|
|
|
|
slots = newSeqOfCap[(UInt256, UInt256)](slotKeys.len)
|
|
|
|
slotProofs = newSeqOfCap[TrieProof](slotKeys.len)
|
|
|
|
|
|
|
|
for slotKey in slotKeys:
|
|
|
|
if not storageExists:
|
|
|
|
slots.add((slotKey, 0.u256))
|
|
|
|
slotProofs.add(TrieProof.empty())
|
|
|
|
continue
|
|
|
|
|
|
|
|
let
|
|
|
|
(storageProof, slotExists) = (
|
2024-11-28 12:33:57 +00:00
|
|
|
await n.getStorageProof(
|
|
|
|
account.storageRoot, address, slotKey, maybeBlockHash, Opt.some(accountProof)
|
|
|
|
)
|
2024-09-13 03:46:35 +00:00
|
|
|
).valueOr:
|
|
|
|
warn "Failed to get storage proof", error = error
|
|
|
|
return Opt.none(Proofs)
|
|
|
|
slotValue =
|
|
|
|
if slotExists:
|
|
|
|
storageProof.toSlot().valueOr:
|
|
|
|
error "Failed to get slot from storageProof"
|
|
|
|
return Opt.none(Proofs)
|
|
|
|
else:
|
|
|
|
0.u256
|
|
|
|
slots.add((slotKey, slotValue))
|
|
|
|
slotProofs.add(storageProof)
|
|
|
|
|
|
|
|
return Opt.some(
|
|
|
|
Proofs(
|
|
|
|
account: account, accountProof: accountProof, slots: slots, slotProofs: slotProofs
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-08-27 12:35:27 +00:00
|
|
|
# Used by: eth_getBalance,
|
|
|
|
proc getBalance*(
|
2024-10-07 20:39:07 +00:00
|
|
|
n: StateNetwork, blockNumOrHash: uint64 | Hash32, address: Address
|
2024-08-27 12:35:27 +00:00
|
|
|
): Future[Opt[UInt256]] {.async: (raises: [CancelledError]).} =
|
2024-11-28 12:33:57 +00:00
|
|
|
let header = (await n.getBlockHeaderByBlockNumOrHash(blockNumOrHash)).valueOr:
|
|
|
|
warn "Failed to get block header by block number or hash", blockNumOrHash
|
2024-08-27 12:35:27 +00:00
|
|
|
return Opt.none(UInt256)
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
await n.getBalanceByStateRoot(header.stateRoot, address, Opt.some(header.rlpHash()))
|
2024-08-27 12:35:27 +00:00
|
|
|
|
|
|
|
# Used by: eth_getTransactionCount
|
|
|
|
proc getTransactionCount*(
|
2024-10-07 20:39:07 +00:00
|
|
|
n: StateNetwork, blockNumOrHash: uint64 | Hash32, address: Address
|
2024-08-27 12:35:27 +00:00
|
|
|
): Future[Opt[AccountNonce]] {.async: (raises: [CancelledError]).} =
|
2024-11-28 12:33:57 +00:00
|
|
|
let header = (await n.getBlockHeaderByBlockNumOrHash(blockNumOrHash)).valueOr:
|
|
|
|
warn "Failed to get block header by block number or hash", blockNumOrHash
|
2024-08-27 12:35:27 +00:00
|
|
|
return Opt.none(AccountNonce)
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
await n.getTransactionCountByStateRoot(
|
|
|
|
header.stateRoot, address, Opt.some(header.rlpHash())
|
|
|
|
)
|
2024-08-27 12:35:27 +00:00
|
|
|
|
|
|
|
# Used by: eth_getStorageAt
|
|
|
|
proc getStorageAt*(
|
2024-10-07 20:39:07 +00:00
|
|
|
n: StateNetwork, blockNumOrHash: uint64 | Hash32, address: Address, slotKey: UInt256
|
2024-08-27 12:35:27 +00:00
|
|
|
): Future[Opt[UInt256]] {.async: (raises: [CancelledError]).} =
|
2024-11-28 12:33:57 +00:00
|
|
|
let header = (await n.getBlockHeaderByBlockNumOrHash(blockNumOrHash)).valueOr:
|
|
|
|
warn "Failed to get block header by block number or hash", blockNumOrHash
|
2024-08-27 12:35:27 +00:00
|
|
|
return Opt.none(UInt256)
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
await n.getStorageAtByStateRoot(
|
|
|
|
header.stateRoot, address, slotKey, Opt.some(header.rlpHash())
|
|
|
|
)
|
2024-08-27 12:35:27 +00:00
|
|
|
|
|
|
|
# Used by: eth_getCode
|
|
|
|
proc getCode*(
|
2024-10-07 20:39:07 +00:00
|
|
|
n: StateNetwork, blockNumOrHash: uint64 | Hash32, address: Address
|
2024-08-27 12:35:27 +00:00
|
|
|
): Future[Opt[Bytecode]] {.async: (raises: [CancelledError]).} =
|
2024-11-28 12:33:57 +00:00
|
|
|
let header = (await n.getBlockHeaderByBlockNumOrHash(blockNumOrHash)).valueOr:
|
|
|
|
warn "Failed to get block header by block number or hash", blockNumOrHash
|
2024-08-27 12:35:27 +00:00
|
|
|
return Opt.none(Bytecode)
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
await n.getCodeByStateRoot(header.stateRoot, address, Opt.some(header.rlpHash()))
|
2024-09-13 03:46:35 +00:00
|
|
|
|
|
|
|
# Used by: eth_getProof
|
|
|
|
proc getProofs*(
|
2024-09-13 06:51:16 +00:00
|
|
|
n: StateNetwork,
|
2024-10-07 20:39:07 +00:00
|
|
|
blockNumOrHash: uint64 | Hash32,
|
2024-10-04 21:21:26 +00:00
|
|
|
address: Address,
|
2024-09-13 06:51:16 +00:00
|
|
|
slotKeys: seq[UInt256],
|
2024-09-13 03:46:35 +00:00
|
|
|
): Future[Opt[Proofs]] {.async: (raises: [CancelledError]).} =
|
2024-11-28 12:33:57 +00:00
|
|
|
let header = (await n.getBlockHeaderByBlockNumOrHash(blockNumOrHash)).valueOr:
|
|
|
|
warn "Failed to get block header by block number or hash", blockNumOrHash
|
2024-09-13 03:46:35 +00:00
|
|
|
return Opt.none(Proofs)
|
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
await n.getProofsByStateRoot(
|
|
|
|
header.stateRoot, address, slotKeys, Opt.some(header.rlpHash())
|
|
|
|
)
|