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-05-24 07:49:51 +00:00
|
|
|
import results, eth/common, ./state_content
|
|
|
|
|
|
|
|
export results, common
|
|
|
|
|
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
|
|
|
|
|
|
|
func toAccount*(accountProof: TrieProof): Result[Account, string] {.inline.} =
|
2024-08-23 07:46:23 +00:00
|
|
|
doAssert(accountProof.len() > 0)
|
2024-06-10 10:47:09 +00:00
|
|
|
|
|
|
|
rlpDecodeAccountTrieNode(accountProof[^1])
|
|
|
|
|
|
|
|
func toSlot*(storageProof: TrieProof): Result[UInt256, string] {.inline.} =
|
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-06-10 10:47:09 +00:00
|
|
|
func toPath*(hash: KeccakHash): Nibbles {.inline.} =
|
|
|
|
Nibbles.init(hash.data, isEven = true)
|
|
|
|
|
2024-08-07 16:01:30 +00:00
|
|
|
func toPath*(address: EthAddress): Nibbles {.inline.} =
|
2024-06-10 10:47:09 +00:00
|
|
|
keccakHash(address).toPath()
|
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
func toPath*(slotKey: UInt256): Nibbles {.inline.} =
|
2024-06-10 10:47:09 +00:00
|
|
|
keccakHash(toBytesBE(slotKey)).toPath()
|