2024-05-24 07:49:51 +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,
|
|
|
|
stew/arrayops,
|
|
|
|
eth/rlp,
|
|
|
|
eth/common/[hashes, addresses, base_rlp, accounts_rlp],
|
|
|
|
./state_content
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
export results, hashes, accounts, addresses, rlp
|
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
template fromBytes*(T: type Hash32, hash: openArray[byte]): T =
|
2024-10-04 21:21:26 +00:00
|
|
|
doAssert(hash.len() == 32)
|
|
|
|
Hash32(array[32, byte].initCopyFrom(hash))
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
func decodePrefix*(nodePrefixRlp: Rlp): (byte, bool, Nibbles) {.raises: RlpError.} =
|
2024-05-24 07:49:51 +00:00
|
|
|
doAssert(not nodePrefixRlp.isEmpty())
|
|
|
|
|
|
|
|
let
|
|
|
|
rlpBytes = nodePrefixRlp.toBytes()
|
|
|
|
firstNibble = (rlpBytes[0] and 0xF0) shr 4
|
|
|
|
isLeaf = firstNibble == 2 or firstNibble == 3
|
|
|
|
isEven = firstNibble == 0 or firstNibble == 2
|
|
|
|
startIdx = if isEven: 1 else: 0
|
|
|
|
nibbles = Nibbles.init(rlpBytes[startIdx .. ^1], isEven)
|
|
|
|
|
|
|
|
(firstNibble.byte, isLeaf, nibbles)
|
|
|
|
|
2024-06-10 10:47:09 +00:00
|
|
|
func rlpDecodeAccountTrieNode*(accountTrieNode: TrieNode): Result[Account, string] =
|
2024-06-14 05:38:24 +00:00
|
|
|
try:
|
|
|
|
let accNodeRlp = rlpFromBytes(accountTrieNode.asSeq())
|
|
|
|
if accNodeRlp.isEmpty() or accNodeRlp.listLen() != 2:
|
|
|
|
return err("invalid account trie node - malformed")
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
let accNodePrefixRlp = accNodeRlp.listElem(0)
|
|
|
|
if accNodePrefixRlp.isEmpty():
|
|
|
|
return err("invalid account trie node - empty prefix")
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
let (_, isLeaf, _) = decodePrefix(accNodePrefixRlp)
|
|
|
|
if not isLeaf:
|
|
|
|
return err("invalid account trie node - leaf prefix expected")
|
2024-06-10 10:47:09 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
decodeRlp(accNodeRlp.listElem(1).toBytes(), Account)
|
|
|
|
except RlpError as e:
|
|
|
|
err(e.msg)
|
2024-06-10 10:47:09 +00:00
|
|
|
|
|
|
|
func rlpDecodeContractTrieNode*(contractTrieNode: TrieNode): Result[UInt256, string] =
|
2024-06-14 05:38:24 +00:00
|
|
|
try:
|
|
|
|
let storageNodeRlp = rlpFromBytes(contractTrieNode.asSeq())
|
|
|
|
if storageNodeRlp.isEmpty() or storageNodeRlp.listLen() != 2:
|
|
|
|
return err("invalid contract trie node - malformed")
|
2024-06-10 10:47:09 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
let storageNodePrefixRlp = storageNodeRlp.listElem(0)
|
|
|
|
if storageNodePrefixRlp.isEmpty():
|
|
|
|
return err("invalid contract trie node - empty prefix")
|
2024-06-10 10:47:09 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
let (_, isLeaf, _) = decodePrefix(storageNodePrefixRlp)
|
|
|
|
if not isLeaf:
|
|
|
|
return err("invalid contract trie node - leaf prefix expected")
|
2024-06-10 10:47:09 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
decodeRlp(storageNodeRlp.listElem(1).toBytes(), UInt256)
|
|
|
|
except RlpError as e:
|
|
|
|
err(e.msg)
|
2024-06-10 10:47:09 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
template toAccount*(accountProof: TrieProof): Result[Account, string] =
|
2024-08-23 07:46:23 +00:00
|
|
|
doAssert(accountProof.len() > 0)
|
2024-06-10 10:47:09 +00:00
|
|
|
rlpDecodeAccountTrieNode(accountProof[^1])
|
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
template toSlot*(storageProof: TrieProof): Result[UInt256, string] =
|
2024-08-23 07:46:23 +00:00
|
|
|
doAssert(storageProof.len() > 0)
|
2024-06-10 10:47:09 +00:00
|
|
|
rlpDecodeContractTrieNode(storageProof[^1])
|
|
|
|
|
2024-07-30 14:56:21 +00:00
|
|
|
func removeLeafKeyEndNibbles*(
|
|
|
|
nibbles: Nibbles, leafNode: TrieNode
|
|
|
|
): Nibbles {.raises: RlpError.} =
|
|
|
|
let nodeRlp = rlpFromBytes(leafNode.asSeq())
|
|
|
|
doAssert(nodeRlp.listLen() == 2)
|
|
|
|
let (_, isLeaf, prefix) = decodePrefix(nodeRlp.listElem(0))
|
|
|
|
doAssert(isLeaf)
|
|
|
|
|
|
|
|
let leafPrefix = prefix.unpackNibbles()
|
|
|
|
var unpackedNibbles = nibbles.unpackNibbles()
|
|
|
|
doAssert(unpackedNibbles[^leafPrefix.len() .. ^1] == leafPrefix)
|
|
|
|
|
|
|
|
unpackedNibbles.dropN(leafPrefix.len()).packNibbles()
|
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
template toPath*(hash: Hash32): Nibbles =
|
2024-06-10 10:47:09 +00:00
|
|
|
Nibbles.init(hash.data, isEven = true)
|
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
template toPath*(address: Address): Nibbles =
|
2024-10-04 21:21:26 +00:00
|
|
|
keccak256(address.data).toPath()
|
2024-06-10 10:47:09 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
template toPath*(slotKey: UInt256): Nibbles =
|
2024-10-04 21:21:26 +00:00
|
|
|
keccak256(toBytesBE(slotKey)).toPath()
|