2024-05-06 15:13:35 +00:00
|
|
|
# Fluffy
|
|
|
|
# Copyright (c) 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-10-04 21:21:26 +00:00
|
|
|
import results, eth/rlp, eth/common/hashes, ./state_content, ./state_utils
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
export results, state_content, hashes
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
from eth/common/eth_types_rlp import rlpHash
|
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
template hashEquals(value: TrieNode | Bytecode, expectedHash: Hash32): bool =
|
2024-10-04 21:21:26 +00:00
|
|
|
keccak256(value.asSeq()) == expectedHash
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
func isValidNextNode(
|
2024-06-14 05:38:24 +00:00
|
|
|
thisNodeRlp: Rlp, rlpIdx: int, nextNode: TrieNode
|
|
|
|
): bool {.raises: RlpError.} =
|
2024-05-16 08:16:16 +00:00
|
|
|
let hashOrShortRlp = thisNodeRlp.listElem(rlpIdx)
|
|
|
|
if hashOrShortRlp.isEmpty():
|
|
|
|
return false
|
|
|
|
|
|
|
|
let nextHash =
|
|
|
|
if hashOrShortRlp.isList():
|
|
|
|
# is a short node
|
2024-06-14 10:42:31 +00:00
|
|
|
rlpHash(hashOrShortRlp)
|
2024-05-16 08:16:16 +00:00
|
|
|
else:
|
|
|
|
let hash = hashOrShortRlp.toBytes()
|
|
|
|
if hash.len() != 32:
|
|
|
|
return false
|
2024-10-04 21:21:26 +00:00
|
|
|
Hash32.fromBytes(hash)
|
2024-05-16 08:16:16 +00:00
|
|
|
|
|
|
|
nextNode.hashEquals(nextHash)
|
|
|
|
|
2024-06-20 15:41:34 +00:00
|
|
|
# TODO: Refactor this function to improve maintainability
|
2024-10-10 13:24:39 +00:00
|
|
|
func validateTrieProof*(
|
2024-10-04 21:21:26 +00:00
|
|
|
expectedRootHash: Opt[Hash32],
|
2024-05-24 07:49:51 +00:00
|
|
|
path: Nibbles,
|
|
|
|
proof: TrieProof,
|
|
|
|
allowKeyEndInPathForLeafs = false,
|
2024-05-16 08:16:16 +00:00
|
|
|
): Result[void, string] =
|
|
|
|
if proof.len() == 0:
|
|
|
|
return err("proof is empty")
|
|
|
|
|
2024-06-11 13:01:35 +00:00
|
|
|
if expectedRootHash.isSome():
|
|
|
|
if not proof[0].hashEquals(expectedRootHash.get()):
|
|
|
|
return err("hash of proof root node doesn't match the expected root hash")
|
2024-05-16 08:16:16 +00:00
|
|
|
|
|
|
|
let nibbles = path.unpackNibbles()
|
|
|
|
if nibbles.len() == 0:
|
|
|
|
if proof.len() == 1:
|
|
|
|
return ok() # root node case, already validated above
|
|
|
|
else:
|
|
|
|
return err("empty path, only one node expected in proof")
|
|
|
|
|
|
|
|
var nibbleIdx = 0
|
|
|
|
for proofIdx, p in proof:
|
|
|
|
let
|
|
|
|
thisNodeRlp = rlpFromBytes(p.asSeq())
|
|
|
|
remainingNibbles = nibbles.len() - nibbleIdx
|
|
|
|
isLastNode = proofIdx == proof.high
|
|
|
|
|
|
|
|
if remainingNibbles == 0:
|
|
|
|
if isLastNode:
|
|
|
|
break
|
|
|
|
else:
|
2024-05-28 00:45:30 +00:00
|
|
|
return err("proof has more nodes then expected for given path")
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
try:
|
|
|
|
case thisNodeRlp.listLen()
|
|
|
|
of 2:
|
|
|
|
let nodePrefixRlp = thisNodeRlp.listElem(0)
|
|
|
|
if nodePrefixRlp.isEmpty():
|
|
|
|
return err("node prefix is empty")
|
|
|
|
|
|
|
|
let (prefix, isLeaf, prefixNibbles) = decodePrefix(nodePrefixRlp)
|
|
|
|
if prefix >= 4:
|
|
|
|
return err("invalid prefix in node")
|
|
|
|
|
|
|
|
if not isLastNode or (isLeaf and allowKeyEndInPathForLeafs):
|
|
|
|
let unpackedPrefix = prefixNibbles.unpackNibbles()
|
|
|
|
if remainingNibbles < unpackedPrefix.len():
|
|
|
|
return err("not enough nibbles to validate node prefix")
|
|
|
|
|
|
|
|
let nibbleEndIdx = nibbleIdx + unpackedPrefix.len()
|
|
|
|
if nibbles[nibbleIdx ..< nibbleEndIdx] != unpackedPrefix:
|
|
|
|
return err("nibbles don't match node prefix")
|
|
|
|
nibbleIdx += unpackedPrefix.len()
|
|
|
|
|
|
|
|
if not isLastNode:
|
|
|
|
if isLeaf:
|
|
|
|
return err("leaf node must be last node in the proof")
|
|
|
|
else: # is extension node
|
|
|
|
if not isValidNextNode(thisNodeRlp, 1, proof[proofIdx + 1]):
|
|
|
|
return
|
|
|
|
err("hash of next node doesn't match the expected extension node hash")
|
|
|
|
of 17:
|
|
|
|
if not isLastNode:
|
|
|
|
let nextNibble = nibbles[nibbleIdx]
|
|
|
|
if nextNibble >= 16:
|
|
|
|
return err("invalid next nibble for branch node")
|
|
|
|
|
|
|
|
if not isValidNextNode(thisNodeRlp, nextNibble.int, proof[proofIdx + 1]):
|
|
|
|
return err("hash of next node doesn't match the expected branch node hash")
|
|
|
|
|
|
|
|
inc nibbleIdx
|
|
|
|
else:
|
|
|
|
return err("invalid rlp node, expected 2 or 17 elements")
|
|
|
|
except RlpError as e:
|
|
|
|
return err(e.msg)
|
2024-05-16 08:16:16 +00:00
|
|
|
|
|
|
|
if nibbleIdx < nibbles.len():
|
|
|
|
err("path contains more nibbles than expected for proof")
|
|
|
|
else:
|
|
|
|
ok()
|
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
func validateRetrieval*(
|
2024-05-24 07:49:51 +00:00
|
|
|
key: AccountTrieNodeKey, value: AccountTrieNodeRetrieval
|
2024-05-16 08:16:16 +00:00
|
|
|
): Result[void, string] =
|
2024-05-24 07:49:51 +00:00
|
|
|
if value.node.hashEquals(key.nodeHash):
|
2024-05-16 08:16:16 +00:00
|
|
|
ok()
|
|
|
|
else:
|
2024-05-24 07:49:51 +00:00
|
|
|
err("hash of account trie node doesn't match the expected node hash")
|
2024-05-06 15:13:35 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
func validateRetrieval*(
|
2024-05-24 07:49:51 +00:00
|
|
|
key: ContractTrieNodeKey, value: ContractTrieNodeRetrieval
|
2024-05-16 08:16:16 +00:00
|
|
|
): Result[void, string] =
|
2024-05-24 07:49:51 +00:00
|
|
|
if value.node.hashEquals(key.nodeHash):
|
2024-05-16 08:16:16 +00:00
|
|
|
ok()
|
|
|
|
else:
|
2024-05-24 07:49:51 +00:00
|
|
|
err("hash of contract trie node doesn't match the expected node hash")
|
2024-05-06 15:13:35 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
func validateRetrieval*(
|
2024-05-24 07:49:51 +00:00
|
|
|
key: ContractCodeKey, value: ContractCodeRetrieval
|
2024-05-16 08:16:16 +00:00
|
|
|
): Result[void, string] =
|
2024-05-24 07:49:51 +00:00
|
|
|
if value.code.hashEquals(key.codeHash):
|
2024-05-16 08:16:16 +00:00
|
|
|
ok()
|
|
|
|
else:
|
2024-05-24 07:49:51 +00:00
|
|
|
err("hash of bytecode doesn't match the expected code hash")
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
func validateOffer*(
|
2024-10-04 21:21:26 +00:00
|
|
|
trustedStateRoot: Opt[Hash32], key: AccountTrieNodeKey, offer: AccountTrieNodeOffer
|
2024-05-16 08:16:16 +00:00
|
|
|
): Result[void, string] =
|
2024-05-24 07:49:51 +00:00
|
|
|
?validateTrieProof(trustedStateRoot, key.path, offer.proof)
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
validateRetrieval(key, offer.toRetrieval())
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
func validateOffer*(
|
2024-10-04 21:21:26 +00:00
|
|
|
trustedStateRoot: Opt[Hash32],
|
2024-06-11 13:01:35 +00:00
|
|
|
key: ContractTrieNodeKey,
|
|
|
|
offer: ContractTrieNodeOffer,
|
2024-05-16 08:16:16 +00:00
|
|
|
): Result[void, string] =
|
|
|
|
?validateTrieProof(
|
2024-05-24 07:49:51 +00:00
|
|
|
trustedStateRoot,
|
2024-08-07 16:01:30 +00:00
|
|
|
key.addressHash.toPath(),
|
2024-05-24 07:49:51 +00:00
|
|
|
offer.accountProof,
|
|
|
|
allowKeyEndInPathForLeafs = true,
|
2024-05-16 08:16:16 +00:00
|
|
|
)
|
|
|
|
|
2024-06-10 10:47:09 +00:00
|
|
|
let account = ?offer.accountProof.toAccount()
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2024-06-11 13:01:35 +00:00
|
|
|
?validateTrieProof(Opt.some(account.storageRoot), key.path, offer.storageProof)
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
validateRetrieval(key, offer.toRetrieval())
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
func validateOffer*(
|
2024-10-04 21:21:26 +00:00
|
|
|
trustedStateRoot: Opt[Hash32], key: ContractCodeKey, offer: ContractCodeOffer
|
2024-05-16 08:16:16 +00:00
|
|
|
): Result[void, string] =
|
|
|
|
?validateTrieProof(
|
2024-05-24 07:49:51 +00:00
|
|
|
trustedStateRoot,
|
2024-08-07 16:01:30 +00:00
|
|
|
key.addressHash.toPath(),
|
2024-05-24 07:49:51 +00:00
|
|
|
offer.accountProof,
|
|
|
|
allowKeyEndInPathForLeafs = true,
|
2024-05-16 08:16:16 +00:00
|
|
|
)
|
|
|
|
|
2024-06-10 10:47:09 +00:00
|
|
|
let account = ?offer.accountProof.toAccount()
|
2024-05-24 07:49:51 +00:00
|
|
|
if not offer.code.hashEquals(account.codeHash):
|
|
|
|
return err("hash of bytecode doesn't match the code hash in the account proof")
|
2024-05-06 15:13:35 +00:00
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
validateRetrieval(key, offer.toRetrieval())
|
|
|
|
|
|
|
|
# Local validations that check the structure of the content keys and values.
|
|
|
|
# None of the validations below check if the data is canonical or not
|
2024-10-10 13:24:39 +00:00
|
|
|
|
|
|
|
func validateGetContentKey*(
|
|
|
|
keyBytes: ContentKeyByteList
|
|
|
|
): Result[(ContentKey, ContentId), string] =
|
|
|
|
let key = ?ContentKey.decode(keyBytes)
|
|
|
|
ok((key, toContentId(keyBytes)))
|
|
|
|
|
|
|
|
func validateRetrieval*(
|
|
|
|
key: ContentKey, contentBytes: seq[byte]
|
|
|
|
): Result[void, string] =
|
|
|
|
case key.contentType
|
|
|
|
of unused:
|
|
|
|
raiseAssert("ContentKey contentType: unused")
|
|
|
|
of accountTrieNode:
|
|
|
|
let retrieval = ?AccountTrieNodeRetrieval.decode(contentBytes)
|
2024-10-12 16:51:50 +00:00
|
|
|
validateRetrieval(key.accountTrieNodeKey, retrieval)
|
2024-10-10 13:24:39 +00:00
|
|
|
of contractTrieNode:
|
|
|
|
let retrieval = ?ContractTrieNodeRetrieval.decode(contentBytes)
|
2024-10-12 16:51:50 +00:00
|
|
|
validateRetrieval(key.contractTrieNodeKey, retrieval)
|
2024-10-10 13:24:39 +00:00
|
|
|
of contractCode:
|
|
|
|
let retrieval = ?ContractCodeRetrieval.decode(contentBytes)
|
2024-10-12 16:51:50 +00:00
|
|
|
validateRetrieval(key.contractCodeKey, retrieval)
|
2024-10-10 13:24:39 +00:00
|
|
|
|
2024-11-28 12:33:57 +00:00
|
|
|
func validateRetrievalGetOffer*(
|
|
|
|
key: ContentKey, contentBytes: seq[byte], parentContentBytes: seq[byte]
|
|
|
|
): Result[seq[byte], string] =
|
|
|
|
case key.contentType
|
|
|
|
of unused:
|
|
|
|
raiseAssert("ContentKey contentType: unused")
|
|
|
|
of accountTrieNode:
|
|
|
|
let
|
|
|
|
retrieval = ?AccountTrieNodeRetrieval.decode(contentBytes)
|
|
|
|
parentOffer = ?AccountTrieNodeOffer.decode(parentContentBytes)
|
|
|
|
offer = retrieval.toOffer(parentOffer)
|
|
|
|
?validateRetrieval(key.accountTrieNodeKey, retrieval)
|
|
|
|
?validateOffer(Opt.none(Hash32), key.accountTrieNodeKey, offer)
|
|
|
|
ok(offer.encode())
|
|
|
|
of contractTrieNode:
|
|
|
|
let
|
|
|
|
retrieval = ?ContractTrieNodeRetrieval.decode(contentBytes)
|
|
|
|
parentOffer = ?ContractTrieNodeOffer.decode(parentContentBytes)
|
|
|
|
offer = retrieval.toOffer(parentOffer)
|
|
|
|
?validateRetrieval(key.contractTrieNodeKey, retrieval)
|
|
|
|
?validateOffer(Opt.none(Hash32), key.contractTrieNodeKey, offer)
|
|
|
|
ok(offer.encode())
|
|
|
|
of contractCode:
|
|
|
|
let
|
|
|
|
retrieval = ?ContractCodeRetrieval.decode(contentBytes)
|
|
|
|
parentOffer = ?ContractCodeOffer.decode(parentContentBytes)
|
|
|
|
offer = retrieval.toOffer(parentOffer)
|
|
|
|
?validateRetrieval(key.contractCodeKey, retrieval)
|
|
|
|
?validateOffer(Opt.none(Hash32), key.contractCodeKey, offer)
|
|
|
|
ok(offer.encode())
|
|
|
|
|
|
|
|
func validateOfferGetRetrieval*(
|
|
|
|
key: ContentKey, contentBytes: seq[byte]
|
2024-10-10 13:24:39 +00:00
|
|
|
): Result[seq[byte], string] =
|
2024-11-28 12:33:57 +00:00
|
|
|
case key.contentType
|
|
|
|
of unused:
|
|
|
|
raiseAssert("ContentKey contentType: unused")
|
|
|
|
of accountTrieNode:
|
|
|
|
let offer = ?AccountTrieNodeOffer.decode(contentBytes)
|
|
|
|
?validateOffer(Opt.none(Hash32), key.accountTrieNodeKey, offer)
|
|
|
|
ok(offer.toRetrieval.encode())
|
|
|
|
of contractTrieNode:
|
|
|
|
let offer = ?ContractTrieNodeOffer.decode(contentBytes)
|
|
|
|
?validateOffer(Opt.none(Hash32), key.contractTrieNodeKey, offer)
|
|
|
|
ok(offer.toRetrieval.encode())
|
|
|
|
of contractCode:
|
|
|
|
let offer = ?ContractCodeOffer.decode(contentBytes)
|
|
|
|
?validateOffer(Opt.none(Hash32), key.contractCodeKey, offer)
|
|
|
|
ok(offer.toRetrieval.encode())
|