2023-12-19 18:59:38 +00:00
|
|
|
# Fluffy
|
2023-01-31 12:38:08 +00:00
|
|
|
# Copyright (c) 2021-2023 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
|
2023-09-13 02:32:38 +00:00
|
|
|
nimcrypto/[hash, sha2, keccak], stew/[objects, results, endians2], stint,
|
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
|
|
|
|
|
|
|
type
|
2021-07-20 12:04:57 +00:00
|
|
|
NodeHash* = MDigest[32 * 8] # keccak256
|
|
|
|
CodeHash* = MDigest[32 * 8] # keccak256
|
|
|
|
Address* = array[20, byte]
|
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
|
|
|
|
contractStorageTrieNode = 0x21
|
|
|
|
accountTrieProof = 0x22
|
|
|
|
contractStorageTrieProof = 0x23
|
|
|
|
contractBytecode = 0x24
|
2021-11-17 16:11:17 +00:00
|
|
|
|
|
|
|
AccountTrieNodeKey* = object
|
|
|
|
path*: ByteList
|
2021-07-09 14:15:10 +00:00
|
|
|
nodeHash*: NodeHash
|
2021-11-17 16:11:17 +00:00
|
|
|
stateRoot*: Bytes32
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2021-11-17 16:11:17 +00:00
|
|
|
ContractStorageTrieNodeKey* = object
|
|
|
|
address*: Address
|
|
|
|
path*: ByteList
|
|
|
|
nodeHash*: NodeHash
|
|
|
|
stateRoot*: Bytes32
|
2021-07-09 14:15:10 +00:00
|
|
|
|
2021-11-17 16:11:17 +00:00
|
|
|
AccountTrieProofKey* = object
|
|
|
|
address*: Address
|
|
|
|
stateRoot*: Bytes32
|
2021-07-09 14:15:10 +00:00
|
|
|
|
2021-11-17 16:11:17 +00:00
|
|
|
ContractStorageTrieProofKey* = object
|
|
|
|
address*: Address
|
|
|
|
slot*: UInt256
|
|
|
|
stateRoot*: Bytes32
|
2021-07-09 14:15:10 +00:00
|
|
|
|
2021-11-17 16:11:17 +00:00
|
|
|
ContractBytecodeKey* = object
|
|
|
|
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
|
|
|
|
of contractStorageTrieNode:
|
|
|
|
contractStorageTrieNodeKey*: ContractStorageTrieNodeKey
|
|
|
|
of accountTrieProof:
|
|
|
|
accountTrieProofKey*: AccountTrieProofKey
|
|
|
|
of contractStorageTrieProof:
|
|
|
|
contractStorageTrieProofKey*: ContractStorageTrieProofKey
|
|
|
|
of contractBytecode:
|
|
|
|
contractBytecodeKey*: ContractBytecodeKey
|
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
|
|
|
|
2021-11-17 16:11:17 +00:00
|
|
|
template computeContentId*(digestCtxType: type, body: untyped): ContentId =
|
|
|
|
var h {.inject.}: digestCtxType
|
|
|
|
init(h)
|
|
|
|
body
|
|
|
|
let idHash = finish(h)
|
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 =
|
2021-11-17 16:11:17 +00:00
|
|
|
case contentKey.contentType:
|
2023-12-19 18:59:38 +00:00
|
|
|
of unused:
|
|
|
|
raiseAssert "Should not be used and fail at decoding"
|
2021-11-17 16:11:17 +00:00
|
|
|
of accountTrieNode: # sha256(path | node_hash)
|
|
|
|
let key = contentKey.accountTrieNodeKey
|
|
|
|
computeContentId sha256:
|
|
|
|
h.update(key.path.asSeq())
|
|
|
|
h.update(key.nodeHash.data)
|
|
|
|
of contractStorageTrieNode: # sha256(address | path | node_hash)
|
|
|
|
let key = contentKey.contractStorageTrieNodeKey
|
|
|
|
computeContentId sha256:
|
|
|
|
h.update(key.address)
|
|
|
|
h.update(key.path.asSeq())
|
|
|
|
h.update(key.nodeHash.data)
|
|
|
|
of accountTrieProof: # keccak(address)
|
|
|
|
let key = contentKey.accountTrieProofKey
|
|
|
|
computeContentId keccak256:
|
|
|
|
h.update(key.address)
|
|
|
|
of contractStorageTrieProof: # (keccak(address) + keccak(slot)) % 2**256
|
|
|
|
# TODO: Why is keccak run on slot, when it can be used directly?
|
|
|
|
# Also, value to LE or BE? Not mentioned in specification.
|
|
|
|
let key = contentKey.contractStorageTrieProofKey
|
|
|
|
let n1 =
|
|
|
|
block: computeContentId keccak256:
|
|
|
|
h.update(key.address)
|
|
|
|
let n2 =
|
|
|
|
block: computeContentId keccak256:
|
2022-01-05 08:49:49 +00:00
|
|
|
h.update(toBytesBE(key.slot))
|
2021-11-17 16:11:17 +00:00
|
|
|
|
|
|
|
n1 + n2 # uint256 will wrap arround, practically applying the modulo 256
|
|
|
|
of contractBytecode: # sha256(address | code_hash)
|
|
|
|
let key = contentKey.contractBytecodeKey
|
|
|
|
computeContentId sha256:
|
|
|
|
h.update(key.address)
|
|
|
|
h.update(key.codeHash.data)
|
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
func toContentId*(contentKey: ByteList): results.Opt[ContentId] =
|
2021-11-17 16:11:17 +00:00
|
|
|
let key = decode(contentKey)
|
|
|
|
if key.isSome():
|
2022-11-08 17:31:45 +00:00
|
|
|
ok(key.get().toContentId())
|
2021-11-17 16:11:17 +00:00
|
|
|
else:
|
2022-11-08 17:31:45 +00:00
|
|
|
Opt.none(ContentId)
|