2023-12-19 18:59:38 +00:00
|
|
|
# Fluffy
|
2024-01-09 09:32:29 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2021-06-18 17:20:48 +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.
|
|
|
|
|
2021-11-17 16:11:17 +00:00
|
|
|
# As per spec:
|
|
|
|
# https://github.com/ethereum/portal-network-specs/blob/master/state-network.md#content-keys-and-content-ids
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2021-06-18 17:20:48 +00:00
|
|
|
|
|
|
|
import
|
2024-01-19 17:18:57 +00:00
|
|
|
nimcrypto/[hash, sha2, keccak], stew/results, stint,
|
|
|
|
eth/common/eth_types,
|
2021-10-23 12:28:12 +00:00
|
|
|
ssz_serialization,
|
2021-09-24 15:18:54 +00:00
|
|
|
../../common/common_types
|
2021-07-09 14:15:10 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
export ssz_serialization, common_types, hash, results
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2024-02-02 21:12:36 +00:00
|
|
|
const
|
2024-02-12 12:39:01 +00:00
|
|
|
MAX_PACKED_NIBBLES_LEN = 33
|
2024-02-02 21:12:36 +00:00
|
|
|
MAX_UNPACKED_NIBBLES_LEN = 64
|
|
|
|
|
|
|
|
MAX_TRIE_NODE_LEN = 1024
|
|
|
|
MAX_TRIE_PROOF_LEN = 65
|
|
|
|
MAX_BYTECODE_LEN = 32768
|
|
|
|
|
2021-06-18 17:20:48 +00:00
|
|
|
type
|
2024-01-19 17:18:57 +00:00
|
|
|
NodeHash* = KeccakHash
|
|
|
|
CodeHash* = KeccakHash
|
|
|
|
Address* = EthAddress
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2021-11-17 16:11:17 +00:00
|
|
|
ContentType* = enum
|
2023-12-19 18:59:38 +00:00
|
|
|
# Note: Need to add this unused value as a case object with an enum without
|
|
|
|
# a 0 valueis not allowed: "low(contentType) must be 0 for discriminant".
|
|
|
|
# For prefix values that are in the enum gap, the deserialization will fail
|
|
|
|
# at runtime as is wanted.
|
|
|
|
# In the future it might be possible that this will fail at compile time for
|
|
|
|
# the SSZ Union type, but currently it is allowed in the implementation, and
|
|
|
|
# the SSZ spec is not explicit about disallowing this.
|
|
|
|
unused = 0x00
|
|
|
|
accountTrieNode = 0x20
|
2024-01-19 17:18:57 +00:00
|
|
|
contractTrieNode = 0x21
|
|
|
|
contractCode = 0x22
|
|
|
|
|
2024-02-12 12:39:01 +00:00
|
|
|
Nibbles* = List[byte, MAX_PACKED_NIBBLES_LEN]
|
2024-01-19 17:18:57 +00:00
|
|
|
|
2024-02-02 21:12:36 +00:00
|
|
|
TrieNode* = List[byte, MAX_TRIE_NODE_LEN]
|
|
|
|
TrieProof* = List[TrieNode, MAX_TRIE_PROOF_LEN]
|
2024-01-19 17:18:57 +00:00
|
|
|
|
2024-02-02 21:12:36 +00:00
|
|
|
Bytecode* = List[byte, MAX_BYTECODE_LEN]
|
2021-11-17 16:11:17 +00:00
|
|
|
|
|
|
|
AccountTrieNodeKey* = object
|
2024-01-19 17:18:57 +00:00
|
|
|
path*: Nibbles
|
2021-07-09 14:15:10 +00:00
|
|
|
nodeHash*: NodeHash
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2024-01-19 17:18:57 +00:00
|
|
|
ContractTrieNodeKey* = object
|
2021-11-17 16:11:17 +00:00
|
|
|
address*: Address
|
2024-01-19 17:18:57 +00:00
|
|
|
path*: Nibbles
|
2021-11-17 16:11:17 +00:00
|
|
|
nodeHash*: NodeHash
|
2021-07-09 14:15:10 +00:00
|
|
|
|
2024-01-19 17:18:57 +00:00
|
|
|
ContractCodeKey* = object
|
2021-11-17 16:11:17 +00:00
|
|
|
address*: Address
|
|
|
|
codeHash*: CodeHash
|
2021-07-09 14:15:10 +00:00
|
|
|
|
2021-11-17 16:11:17 +00:00
|
|
|
ContentKey* = object
|
|
|
|
case contentType*: ContentType
|
2023-12-19 18:59:38 +00:00
|
|
|
of unused:
|
|
|
|
discard
|
2021-11-17 16:11:17 +00:00
|
|
|
of accountTrieNode:
|
|
|
|
accountTrieNodeKey*: AccountTrieNodeKey
|
2024-01-19 17:18:57 +00:00
|
|
|
of contractTrieNode:
|
|
|
|
contractTrieNodeKey*: ContractTrieNodeKey
|
|
|
|
of contractCode:
|
|
|
|
contractCodeKey*: ContractCodeKey
|
|
|
|
|
|
|
|
AccountTrieNodeOffer* = object
|
2024-02-02 21:12:36 +00:00
|
|
|
proof*: TrieProof
|
2024-01-19 17:18:57 +00:00
|
|
|
blockHash*: BlockHash
|
|
|
|
|
|
|
|
AccountTrieNodeRetrieval* = object
|
2024-02-02 21:12:36 +00:00
|
|
|
node*: TrieNode
|
2024-01-19 17:18:57 +00:00
|
|
|
|
|
|
|
ContractTrieNodeOffer* = object
|
2024-02-02 21:12:36 +00:00
|
|
|
storageProof*: TrieProof
|
|
|
|
accountProof*: TrieProof
|
2024-01-19 17:18:57 +00:00
|
|
|
blockHash*: BlockHash
|
|
|
|
|
|
|
|
ContractTrieNodeRetrieval* = object
|
2024-02-02 21:12:36 +00:00
|
|
|
node*: TrieNode
|
2024-01-19 17:18:57 +00:00
|
|
|
|
|
|
|
ContractCodeOffer* = object
|
2024-02-02 21:12:36 +00:00
|
|
|
code*: Bytecode
|
|
|
|
accountProof*: TrieProof
|
2024-01-19 17:18:57 +00:00
|
|
|
blockHash*: BlockHash
|
|
|
|
|
|
|
|
ContractCodeRetrieval* = object
|
2024-02-02 21:12:36 +00:00
|
|
|
code*: Bytecode
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2021-09-24 09:22:07 +00:00
|
|
|
func encode*(contentKey: ContentKey): ByteList =
|
2023-12-19 18:59:38 +00:00
|
|
|
doAssert(contentKey.contentType != unused)
|
2021-10-22 15:49:23 +00:00
|
|
|
ByteList.init(SSZ.encode(contentKey))
|
2021-08-18 07:23:57 +00:00
|
|
|
|
2023-12-19 18:59:38 +00:00
|
|
|
proc readSszBytes*(
|
|
|
|
data: openArray[byte], val: var ContentKey
|
|
|
|
) {.raises: [SszError].} =
|
|
|
|
mixin readSszValue
|
|
|
|
if data.len() > 0 and data[0] == ord(unused):
|
|
|
|
raise newException(MalformedSszError, "SSZ selector is unused value")
|
|
|
|
|
|
|
|
readSszValue(data, val)
|
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
func decode*(contentKey: ByteList): Opt[ContentKey] =
|
2021-08-18 07:23:57 +00:00
|
|
|
try:
|
2022-12-09 16:59:36 +00:00
|
|
|
Opt.some(SSZ.decode(contentKey.asSeq(), ContentKey))
|
2023-10-17 12:19:50 +00:00
|
|
|
except SerializationError:
|
2022-12-09 16:59:36 +00:00
|
|
|
return Opt.none(ContentKey)
|
2021-08-18 07:23:57 +00:00
|
|
|
|
2024-01-19 17:18:57 +00:00
|
|
|
func toContentId*(contentKey: ByteList): ContentId =
|
|
|
|
# TODO: Should we try to parse the content key here for invalid ones?
|
|
|
|
let idHash = sha2.sha256.digest(contentKey.asSeq())
|
2021-09-24 09:22:07 +00:00
|
|
|
readUintBE[256](idHash.data)
|
2021-07-13 13:15:33 +00:00
|
|
|
|
2021-12-08 10:54:22 +00:00
|
|
|
func toContentId*(contentKey: ContentKey): ContentId =
|
2024-01-19 17:18:57 +00:00
|
|
|
toContentId(encode(contentKey))
|
2024-02-02 21:12:36 +00:00
|
|
|
|
|
|
|
func packNibbles*(nibbles: seq[byte]): Nibbles =
|
|
|
|
doAssert(nibbles.len() <= MAX_UNPACKED_NIBBLES_LEN, "Can't pack more than 64 nibbles")
|
2024-02-12 12:39:01 +00:00
|
|
|
|
|
|
|
if nibbles.len() == 0:
|
|
|
|
return Nibbles(@[byte(0x00)])
|
2024-02-02 21:12:36 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
isOddLength = (nibbles.len() %% 2 == 1)
|
|
|
|
outputLength = (nibbles.len() + 1) div 2
|
|
|
|
|
|
|
|
var
|
2024-02-12 12:39:01 +00:00
|
|
|
output = newSeq[byte]()
|
2024-02-02 21:12:36 +00:00
|
|
|
highNibble = not isOddLength
|
|
|
|
currentByte: byte = 0
|
|
|
|
|
2024-02-12 12:39:01 +00:00
|
|
|
if isOddLength:
|
|
|
|
currentByte = 0x10
|
|
|
|
|
|
|
|
if not isOddLength:
|
|
|
|
output.add(0x00)
|
|
|
|
|
|
|
|
for i, nibble in nibbles:
|
2024-02-02 21:12:36 +00:00
|
|
|
if highNibble:
|
|
|
|
currentByte = nibble shl 4
|
|
|
|
else:
|
2024-02-12 12:39:01 +00:00
|
|
|
output.add(currentByte or nibble)
|
2024-02-02 21:12:36 +00:00
|
|
|
currentByte = 0
|
|
|
|
highNibble = not highNibble
|
|
|
|
|
2024-02-12 12:39:01 +00:00
|
|
|
Nibbles(output)
|
2024-02-02 21:12:36 +00:00
|
|
|
|
|
|
|
func unpackNibbles*(nibbles: Nibbles): seq[byte] =
|
2024-02-15 15:37:05 +00:00
|
|
|
doAssert(nibbles.len() <= MAX_PACKED_NIBBLES_LEN, "Packed nibbles length is too long")
|
2024-02-02 21:12:36 +00:00
|
|
|
|
|
|
|
var output = newSeq[byte]()
|
|
|
|
|
2024-02-12 12:39:01 +00:00
|
|
|
for i, pair in nibbles:
|
|
|
|
if i == 0 and pair == 0x00:
|
|
|
|
continue
|
|
|
|
|
2024-02-02 21:12:36 +00:00
|
|
|
let
|
|
|
|
first = (pair and 0xF0) shr 4
|
|
|
|
second = pair and 0x0F
|
2024-02-12 12:39:01 +00:00
|
|
|
|
|
|
|
if i == 0 and first == 0x01:
|
|
|
|
output.add(second)
|
|
|
|
else:
|
|
|
|
output.add(first)
|
|
|
|
output.add(second)
|
2024-02-02 21:12:36 +00:00
|
|
|
|
|
|
|
output
|
|
|
|
|