2021-06-18 17:20:48 +00:00
|
|
|
# Nimbus
|
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
|
2022-11-08 17:31:45 +00:00
|
|
|
nimcrypto/[hash, sha2, keccak], stew/[objects, results], 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
|
|
|
|
accountTrieNode = 0x00
|
|
|
|
contractStorageTrieNode = 0x01
|
|
|
|
accountTrieProof = 0x02
|
|
|
|
contractStorageTrieProof = 0x03
|
|
|
|
contractBytecode = 0x04
|
|
|
|
|
|
|
|
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
|
|
|
|
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 =
|
2021-10-22 15:49:23 +00:00
|
|
|
ByteList.init(SSZ.encode(contentKey))
|
2021-08-18 07:23:57 +00:00
|
|
|
|
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))
|
2021-08-18 07:23:57 +00:00
|
|
|
except SszError:
|
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:
|
|
|
|
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)
|