2022-11-23 16:11:38 +00:00
|
|
|
# nimbus_verified_proxy
|
2023-01-31 12:38:08 +00:00
|
|
|
# Copyright (c) 2022-2023 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
|
2022-09-09 13:59:36 +00:00
|
|
|
std/[sequtils, typetraits, options],
|
2022-09-06 16:14:50 +00:00
|
|
|
stint,
|
2022-09-09 13:59:36 +00:00
|
|
|
stew/results,
|
2022-09-06 16:14:50 +00:00
|
|
|
eth/common/eth_types as etypes,
|
|
|
|
eth/common/eth_types_rlp,
|
|
|
|
eth/rlp,
|
2022-09-15 11:04:41 +00:00
|
|
|
eth/trie/[hexary, hexary_proof_verification],
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/eth_api_types
|
2022-09-06 16:14:50 +00:00
|
|
|
|
2022-09-09 13:59:36 +00:00
|
|
|
export results
|
|
|
|
|
2022-09-06 16:14:50 +00:00
|
|
|
func toMDigest(arg: FixedBytes[32]): MDigest[256] =
|
|
|
|
MDigest[256](data: distinctBase(arg))
|
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
func emptyAccount(): etypes.Account =
|
|
|
|
return etypes.Account(
|
|
|
|
nonce: uint64(0),
|
|
|
|
balance: UInt256.zero,
|
|
|
|
storageRoot: etypes.EMPTY_ROOT_HASH,
|
|
|
|
codeHash: etypes.EMPTY_CODE_HASH
|
|
|
|
)
|
|
|
|
|
2022-09-09 13:59:36 +00:00
|
|
|
proc isValidProof(
|
|
|
|
branch: seq[seq[byte]],
|
|
|
|
rootHash: KeccakHash,
|
|
|
|
key, value: seq[byte]): bool =
|
|
|
|
try:
|
|
|
|
# TODO: Investigate if this handles proof of non-existence.
|
2022-11-24 13:41:00 +00:00
|
|
|
# Probably not as bool is not expressive enough to say if proof is valid,
|
|
|
|
# but key actually does not exists in MPT
|
2022-09-09 13:59:36 +00:00
|
|
|
return isValidBranch(branch, rootHash, key, value)
|
|
|
|
except RlpError:
|
|
|
|
return false
|
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
proc getAccountFromProof*(
|
2022-09-06 16:14:50 +00:00
|
|
|
stateRoot: FixedBytes[32],
|
|
|
|
accountAddress: Address,
|
|
|
|
accountBalance: UInt256,
|
|
|
|
accountNonce: Quantity,
|
|
|
|
accountCodeHash: CodeHash,
|
|
|
|
accountStorageRootHash: StorageHash,
|
|
|
|
mptNodes: seq[RlpEncodedBytes]
|
2022-09-15 11:04:41 +00:00
|
|
|
): Result[etypes.Account, string] =
|
2022-09-06 16:14:50 +00:00
|
|
|
let
|
|
|
|
mptNodesBytes = mptNodes.mapIt(distinctBase(it))
|
|
|
|
keccakStateRootHash = toMDigest(stateRoot)
|
|
|
|
acc = etypes.Account(
|
|
|
|
nonce: distinctBase(accountNonce),
|
|
|
|
balance: accountBalance,
|
|
|
|
storageRoot: toMDigest(accountStorageRootHash),
|
|
|
|
codeHash: toMDigest(accountCodeHash)
|
|
|
|
)
|
|
|
|
accountEncoded = rlp.encode(acc)
|
|
|
|
accountKey = toSeq(keccakHash(distinctBase(accountAddress)).data)
|
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
let proofResult = verifyMptProof(
|
2022-09-09 13:59:36 +00:00
|
|
|
mptNodesBytes,
|
|
|
|
keccakStateRootHash,
|
|
|
|
accountKey,
|
|
|
|
accountEncoded
|
|
|
|
)
|
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
case proofResult.kind
|
|
|
|
of MissingKey:
|
|
|
|
return ok(emptyAccount())
|
|
|
|
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(
|
2022-09-09 13:59:36 +00:00
|
|
|
account: etypes.Account,
|
2022-09-15 11:04:41 +00:00
|
|
|
storageProof: StorageProof): Result[UInt256, string] =
|
2022-09-09 13:59:36 +00:00
|
|
|
let
|
|
|
|
storageMptNodes = storageProof.proof.mapIt(distinctBase(it))
|
|
|
|
key = toSeq(keccakHash(toBytesBE(storageProof.key)).data)
|
|
|
|
encodedValue = rlp.encode(storageProof.value)
|
2022-11-24 13:41:00 +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*(
|
|
|
|
stateRoot: FixedBytes[32],
|
|
|
|
requestedSlot: UInt256,
|
|
|
|
proof: ProofResponse): Result[UInt256, string] =
|
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
let account = ?getAccountFromProof(
|
2022-09-09 13:59:36 +00:00
|
|
|
stateRoot,
|
|
|
|
proof.address,
|
|
|
|
proof.balance,
|
|
|
|
proof.nonce,
|
|
|
|
proof.codeHash,
|
|
|
|
proof.storageHash,
|
|
|
|
proof.accountProof
|
|
|
|
)
|
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
if account.storageRoot == etypes.EMPTY_ROOT_HASH:
|
|
|
|
# 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
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
let sproof = proof.storageProof[0]
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
if len(sproof.proof) == 0:
|
|
|
|
return err("empty mpt proof for account with not empty storage")
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
if sproof.key != requestedSlot:
|
|
|
|
return err("received proof for invalid slot")
|
2022-09-06 16:14:50 +00:00
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
return getStorageData(account, sproof)
|
2022-11-21 12:28:15 +00:00
|
|
|
|
|
|
|
func isValidCode*(account: etypes.Account, code: openArray[byte]): bool =
|
|
|
|
return account.codeHash == keccakHash(code)
|