2022-11-23 16:11:38 +00:00
|
|
|
# nimbus_verified_proxy
|
2024-02-15 13:50:25 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-09-06 16:14:50 +00:00
|
|
|
# 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.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-09-06 16:14:50 +00:00
|
|
|
|
|
|
|
import
|
2024-10-16 12:18:02 +00:00
|
|
|
std/sequtils,
|
2022-09-06 16:14:50 +00:00
|
|
|
stint,
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
2024-10-16 12:18:02 +00:00
|
|
|
eth/common/[base_rlp, accounts_rlp, hashes_rlp],
|
|
|
|
eth/trie/[hexary_proof_verification],
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/eth_api_types
|
2022-09-06 16:14:50 +00:00
|
|
|
|
2024-10-16 12:18:02 +00:00
|
|
|
export results, stint, hashes_rlp, accounts_rlp, eth_api_types
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
proc getAccountFromProof*(
|
2024-10-02 16:22:35 +00:00
|
|
|
stateRoot: Hash32,
|
2022-09-06 16:14:50 +00:00
|
|
|
accountAddress: Address,
|
|
|
|
accountBalance: UInt256,
|
|
|
|
accountNonce: Quantity,
|
2024-10-16 12:18:02 +00:00
|
|
|
accountCodeHash: Hash32,
|
|
|
|
accountStorageRoot: Hash32,
|
2024-02-15 13:50:25 +00:00
|
|
|
mptNodes: seq[RlpEncodedBytes],
|
2024-10-16 12:18:02 +00:00
|
|
|
): Result[Account, string] =
|
2022-09-06 16:14:50 +00:00
|
|
|
let
|
|
|
|
mptNodesBytes = mptNodes.mapIt(distinctBase(it))
|
2024-10-16 12:18:02 +00:00
|
|
|
acc = Account(
|
2022-09-06 16:14:50 +00:00
|
|
|
nonce: distinctBase(accountNonce),
|
|
|
|
balance: accountBalance,
|
2024-10-16 12:18:02 +00:00
|
|
|
storageRoot: accountStorageRoot,
|
|
|
|
codeHash: accountCodeHash,
|
2022-09-06 16:14:50 +00:00
|
|
|
)
|
|
|
|
accountEncoded = rlp.encode(acc)
|
2024-10-16 12:18:02 +00:00
|
|
|
accountKey = toSeq(keccak256((accountAddress.data)).data)
|
2022-09-06 16:14:50 +00:00
|
|
|
|
2024-10-16 12:18:02 +00:00
|
|
|
let proofResult = verifyMptProof(mptNodesBytes, stateRoot, accountKey, accountEncoded)
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
case proofResult.kind
|
|
|
|
of MissingKey:
|
2024-10-16 12:18:02 +00:00
|
|
|
return ok(EMPTY_ACCOUNT)
|
2022-09-15 11:04:41 +00:00
|
|
|
of ValidProof:
|
|
|
|
return ok(acc)
|
|
|
|
of InvalidProof:
|
|
|
|
return err(proofResult.errorMsg)
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
proc getStorageData(
|
2024-10-16 12:18:02 +00:00
|
|
|
account: Account, storageProof: StorageProof
|
2024-02-15 13:50:25 +00:00
|
|
|
): Result[UInt256, string] =
|
2022-09-09 13:59:36 +00:00
|
|
|
let
|
|
|
|
storageMptNodes = storageProof.proof.mapIt(distinctBase(it))
|
2024-10-16 12:18:02 +00:00
|
|
|
key = toSeq(keccak256(toBytesBE(storageProof.key)).data)
|
2022-09-09 13:59:36 +00:00
|
|
|
encodedValue = rlp.encode(storageProof.value)
|
2024-02-15 13:50:25 +00:00
|
|
|
proofResult =
|
|
|
|
verifyMptProof(storageMptNodes, account.storageRoot, key, encodedValue)
|
2022-09-15 11:04:41 +00:00
|
|
|
|
|
|
|
case proofResult.kind
|
|
|
|
of MissingKey:
|
|
|
|
return ok(UInt256.zero)
|
|
|
|
of ValidProof:
|
|
|
|
return ok(storageProof.value)
|
|
|
|
of InvalidProof:
|
|
|
|
return err(proofResult.errorMsg)
|
2022-09-09 13:59:36 +00:00
|
|
|
|
|
|
|
proc getStorageData*(
|
2024-10-02 16:22:35 +00:00
|
|
|
stateRoot: Hash32, requestedSlot: UInt256, proof: ProofResponse
|
2024-02-15 13:50:25 +00:00
|
|
|
): Result[UInt256, string] =
|
|
|
|
let account =
|
|
|
|
?getAccountFromProof(
|
|
|
|
stateRoot, proof.address, proof.balance, proof.nonce, proof.codeHash,
|
2024-02-29 17:56:59 +00:00
|
|
|
proof.storageHash, proof.accountProof,
|
2024-02-15 13:50:25 +00:00
|
|
|
)
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2024-10-16 12:18:02 +00:00
|
|
|
if account.storageRoot == EMPTY_ROOT_HASH:
|
2022-09-15 11:04:41 +00:00
|
|
|
# valid account with empty storage, in that case getStorageAt
|
|
|
|
# return 0 value
|
|
|
|
return ok(u256(0))
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
if len(proof.storageProof) != 1:
|
|
|
|
return err("no storage proof for requested slot")
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2024-10-16 12:18:02 +00:00
|
|
|
let storageProof = proof.storageProof[0]
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2024-10-16 12:18:02 +00:00
|
|
|
if len(storageProof.proof) == 0:
|
2022-09-15 11:04:41 +00:00
|
|
|
return err("empty mpt proof for account with not empty storage")
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2024-10-16 12:18:02 +00:00
|
|
|
if storageProof.key != requestedSlot:
|
2022-09-15 11:04:41 +00:00
|
|
|
return err("received proof for invalid slot")
|
2022-09-06 16:14:50 +00:00
|
|
|
|
2024-10-16 12:18:02 +00:00
|
|
|
getStorageData(account, storageProof)
|
2022-11-21 12:28:15 +00:00
|
|
|
|
2024-10-16 12:18:02 +00:00
|
|
|
func isValidCode*(account: Account, code: openArray[byte]): bool =
|
|
|
|
account.codeHash == keccak256(code)
|