2023-12-19 19:59:38 +01:00
|
|
|
# Fluffy
|
2024-01-09 12:32:29 +03:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2021-06-18 19:20:48 +02: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 17:11:17 +01:00
|
|
|
# As per spec:
|
|
|
|
# https://github.com/ethereum/portal-network-specs/blob/master/state-network.md#content-keys-and-content-ids
|
2021-06-18 19:20:48 +02:00
|
|
|
|
2023-01-31 13:38:08 +01:00
|
|
|
{.push raises: [].}
|
2021-06-18 19:20:48 +02:00
|
|
|
|
|
|
|
import
|
2024-01-19 20:18:57 +03:00
|
|
|
nimcrypto/[hash, sha2, keccak], stew/results, stint,
|
|
|
|
eth/common/eth_types,
|
2021-10-23 14:28:12 +02:00
|
|
|
ssz_serialization,
|
2021-09-24 17:18:54 +02:00
|
|
|
../../common/common_types
|
2021-07-09 16:15:10 +02:00
|
|
|
|
2022-12-09 17:59:36 +01:00
|
|
|
export ssz_serialization, common_types, hash, results
|
2021-06-18 19:20:48 +02:00
|
|
|
|
|
|
|
type
|
2024-01-19 20:18:57 +03:00
|
|
|
NodeHash* = KeccakHash
|
|
|
|
CodeHash* = KeccakHash
|
|
|
|
Address* = EthAddress
|
2021-06-18 19:20:48 +02:00
|
|
|
|
2021-11-17 17:11:17 +01:00
|
|
|
ContentType* = enum
|
2023-12-19 19:59:38 +01: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 20:18:57 +03:00
|
|
|
contractTrieNode = 0x21
|
|
|
|
contractCode = 0x22
|
|
|
|
|
|
|
|
NibblePair* = byte
|
|
|
|
Nibbles* = object
|
|
|
|
isOddLength*: bool
|
|
|
|
packedNibbles*: List[NibblePair, 32]
|
|
|
|
|
|
|
|
WitnessNode* = List[byte, 1024]
|
|
|
|
Witness* = List[WitnessNode, 1024]
|
|
|
|
|
|
|
|
StateWitness* = object
|
|
|
|
key*: Nibbles
|
|
|
|
proof*: Witness
|
|
|
|
|
|
|
|
StorageWitness* = object
|
|
|
|
key*: Nibbles
|
|
|
|
proof*: Witness
|
|
|
|
stateWitness*: StateWitness
|
2021-11-17 17:11:17 +01:00
|
|
|
|
|
|
|
AccountTrieNodeKey* = object
|
2024-01-19 20:18:57 +03:00
|
|
|
path*: Nibbles
|
2021-07-09 16:15:10 +02:00
|
|
|
nodeHash*: NodeHash
|
2021-06-18 19:20:48 +02:00
|
|
|
|
2024-01-19 20:18:57 +03:00
|
|
|
ContractTrieNodeKey* = object
|
2021-11-17 17:11:17 +01:00
|
|
|
address*: Address
|
2024-01-19 20:18:57 +03:00
|
|
|
path*: Nibbles
|
2021-11-17 17:11:17 +01:00
|
|
|
nodeHash*: NodeHash
|
2021-07-09 16:15:10 +02:00
|
|
|
|
2024-01-19 20:18:57 +03:00
|
|
|
ContractCodeKey* = object
|
2021-11-17 17:11:17 +01:00
|
|
|
address*: Address
|
|
|
|
codeHash*: CodeHash
|
2021-07-09 16:15:10 +02:00
|
|
|
|
2021-11-17 17:11:17 +01:00
|
|
|
ContentKey* = object
|
|
|
|
case contentType*: ContentType
|
2023-12-19 19:59:38 +01:00
|
|
|
of unused:
|
|
|
|
discard
|
2021-11-17 17:11:17 +01:00
|
|
|
of accountTrieNode:
|
|
|
|
accountTrieNodeKey*: AccountTrieNodeKey
|
2024-01-19 20:18:57 +03:00
|
|
|
of contractTrieNode:
|
|
|
|
contractTrieNodeKey*: ContractTrieNodeKey
|
|
|
|
of contractCode:
|
|
|
|
contractCodeKey*: ContractCodeKey
|
|
|
|
|
|
|
|
AccountTrieNodeOffer* = object
|
|
|
|
proof*: StateWitness
|
|
|
|
blockHash*: BlockHash
|
|
|
|
|
|
|
|
AccountTrieNodeRetrieval* = object
|
|
|
|
node*: WitnessNode
|
|
|
|
|
|
|
|
ContractTrieNodeOffer* = object
|
|
|
|
proof*: StorageWitness
|
|
|
|
blockHash*: BlockHash
|
|
|
|
|
|
|
|
ContractTrieNodeRetrieval* = object
|
|
|
|
node*: WitnessNode
|
|
|
|
|
|
|
|
ContractCodeOffer* = object
|
|
|
|
code*: ByteList
|
|
|
|
accountProof*: StateWitness
|
|
|
|
blockHash*: BlockHash
|
|
|
|
|
|
|
|
ContractCodeRetrieval* = object
|
|
|
|
code*: ByteList
|
2021-06-18 19:20:48 +02:00
|
|
|
|
2021-09-24 11:22:07 +02:00
|
|
|
func encode*(contentKey: ContentKey): ByteList =
|
2023-12-19 19:59:38 +01:00
|
|
|
doAssert(contentKey.contentType != unused)
|
2021-10-22 17:49:23 +02:00
|
|
|
ByteList.init(SSZ.encode(contentKey))
|
2021-08-18 09:23:57 +02:00
|
|
|
|
2023-12-19 19:59:38 +01: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 17:59:36 +01:00
|
|
|
func decode*(contentKey: ByteList): Opt[ContentKey] =
|
2021-08-18 09:23:57 +02:00
|
|
|
try:
|
2022-12-09 17:59:36 +01:00
|
|
|
Opt.some(SSZ.decode(contentKey.asSeq(), ContentKey))
|
2023-10-17 14:19:50 +02:00
|
|
|
except SerializationError:
|
2022-12-09 17:59:36 +01:00
|
|
|
return Opt.none(ContentKey)
|
2021-08-18 09:23:57 +02:00
|
|
|
|
2024-01-19 20:18:57 +03: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 11:22:07 +02:00
|
|
|
readUintBE[256](idHash.data)
|
2021-07-13 15:15:33 +02:00
|
|
|
|
2021-12-08 11:54:22 +01:00
|
|
|
func toContentId*(contentKey: ContentKey): ContentId =
|
2024-01-19 20:18:57 +03:00
|
|
|
toContentId(encode(contentKey))
|