2021-12-15 07:48:56 +00:00
|
|
|
# Nimbus
|
2022-06-16 06:50:29 +00:00
|
|
|
# Copyright (c) 2021-2022 Status Research & Development GmbH
|
2021-12-15 07:48:56 +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.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
unittest2, stew/byteutils,
|
|
|
|
../network/state/state_content
|
|
|
|
|
2021-12-20 08:53:00 +00:00
|
|
|
# According to test vectors:
|
2022-06-16 06:50:29 +00:00
|
|
|
# https://github.com/ethereum/portal-network-specs/blob/master/content-keys-test-vectors.md#state-network-keys
|
2021-12-20 08:53:00 +00:00
|
|
|
|
2021-12-15 07:48:56 +00:00
|
|
|
suite "State ContentKey Encodings":
|
2022-01-05 08:49:49 +00:00
|
|
|
# Common input
|
2021-12-20 08:53:00 +00:00
|
|
|
const
|
|
|
|
stateRoot = hexToByteArray[sizeof(Bytes32)](
|
|
|
|
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
|
|
|
|
address = hexToByteArray[sizeof(Address)](
|
|
|
|
"0x829bd824b016326a401d083b33d092293333a830")
|
|
|
|
|
|
|
|
test "AccountTrieNode":
|
2022-01-05 08:49:49 +00:00
|
|
|
# Input
|
|
|
|
const
|
2022-06-16 06:50:29 +00:00
|
|
|
nodeHash = NodeHash.fromHex(
|
|
|
|
"0xb8be7903aee73b8f6a59cd44a1f52c62148e1f376c0dfa1f5f773a98666efc2b")
|
2022-01-05 08:49:49 +00:00
|
|
|
path = ByteList.init(@[byte 1, 2, 0, 1])
|
|
|
|
|
|
|
|
# Output
|
|
|
|
contentKeyHex =
|
|
|
|
"0044000000b8be7903aee73b8f6a59cd44a1f52c62148e1f376c0dfa1f5f773a98666efc2bd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d01020001"
|
|
|
|
contentId =
|
|
|
|
"41237096982860596884042712109427867048220765019203857308279863638242761605893"
|
|
|
|
# or
|
|
|
|
contentIdHexBE =
|
|
|
|
"5b2b5ea9a7384491010c1aa459a0f967dcf8b69988adbfe7e0bed513e9bb8305"
|
2021-12-15 07:48:56 +00:00
|
|
|
|
2021-12-20 08:53:00 +00:00
|
|
|
let
|
|
|
|
accountTrieNodeKey = AccountTrieNodeKey(
|
|
|
|
path: path, nodeHash: nodeHash, stateRoot: stateRoot)
|
|
|
|
contentKey = ContentKey(
|
|
|
|
contentType: accountTrieNode, accountTrieNodeKey: accountTrieNodeKey)
|
2021-12-15 07:48:56 +00:00
|
|
|
|
|
|
|
let encoded = encode(contentKey)
|
2022-01-05 08:49:49 +00:00
|
|
|
check encoded.asSeq.toHex == contentKeyHex
|
2021-12-15 07:48:56 +00:00
|
|
|
let decoded = decode(encoded)
|
|
|
|
check decoded.isSome()
|
|
|
|
|
|
|
|
let contentKeyDecoded = decoded.get()
|
|
|
|
check:
|
|
|
|
contentKeyDecoded.contentType == contentKey.contentType
|
|
|
|
contentKeyDecoded.accountTrieNodeKey == contentKey.accountTrieNodeKey
|
|
|
|
|
2022-01-05 08:49:49 +00:00
|
|
|
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
|
|
|
# In stint this does BE hex string
|
|
|
|
toContentId(contentKey).toHex() == contentIdHexBE
|
2021-12-20 08:53:00 +00:00
|
|
|
|
|
|
|
test "ContractStorageTrieNode":
|
2022-01-05 08:49:49 +00:00
|
|
|
# Input
|
|
|
|
const
|
2022-06-16 06:50:29 +00:00
|
|
|
nodeHash = NodeHash.fromHex(
|
|
|
|
"0x3e190b68719aecbcb28ed2271014dd25f2aa633184988eb414189ce0899cade5")
|
2022-01-05 08:49:49 +00:00
|
|
|
path = ByteList.init(@[byte 1, 0, 15, 14, 12, 0])
|
|
|
|
|
|
|
|
# Output
|
|
|
|
contentKeyHex =
|
|
|
|
"01829bd824b016326a401d083b33d092293333a830580000003e190b68719aecbcb28ed2271014dd25f2aa633184988eb414189ce0899cade5d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d01000f0e0c00"
|
|
|
|
contentId =
|
|
|
|
"43529358882110548041037387588279806363134301284609868141745095118932570363585"
|
|
|
|
# or
|
|
|
|
contentIdHexBE =
|
|
|
|
"603cbe7902925ce359822378a4cb1b4b53e1bf19d003de2c26e55812d76956c1"
|
2021-12-20 08:53:00 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
contractStorageTrieNodeKey = ContractStorageTrieNodeKey(
|
|
|
|
address: address, path: path, nodeHash: nodeHash, stateRoot: stateRoot)
|
|
|
|
contentKey = ContentKey(
|
|
|
|
contentType: contractStorageTrieNode,
|
|
|
|
contractStorageTrieNodeKey: contractStorageTrieNodeKey)
|
|
|
|
|
|
|
|
let encoded = encode(contentKey)
|
2022-01-05 08:49:49 +00:00
|
|
|
check encoded.asSeq.toHex == contentKeyHex
|
2021-12-20 08:53:00 +00:00
|
|
|
let decoded = decode(encoded)
|
|
|
|
check decoded.isSome()
|
|
|
|
|
|
|
|
let contentKeyDecoded = decoded.get()
|
|
|
|
check:
|
|
|
|
contentKeyDecoded.contentType == contentKey.contentType
|
|
|
|
contentKeyDecoded.contractStorageTrieNodeKey ==
|
|
|
|
contentKey.contractStorageTrieNodeKey
|
|
|
|
|
2022-01-05 08:49:49 +00:00
|
|
|
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
|
|
|
# In stint this does BE hex string
|
|
|
|
toContentId(contentKey).toHex() == contentIdHexBE
|
2021-12-20 08:53:00 +00:00
|
|
|
|
|
|
|
test "AccountTrieProof":
|
2022-01-05 08:49:49 +00:00
|
|
|
# Output
|
|
|
|
const
|
|
|
|
contentKeyHex =
|
|
|
|
"02829bd824b016326a401d083b33d092293333a830d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
|
|
|
contentId =
|
|
|
|
"45301550050471302973396879294932122279426162994178563319590607565171451545101"
|
|
|
|
# or
|
|
|
|
contentIdHexBE =
|
|
|
|
"6427c4c8d42db15c2aca8dfc7dff7ce2c8c835441b566424fa3377dd031cc60d"
|
|
|
|
|
2021-12-20 08:53:00 +00:00
|
|
|
let
|
|
|
|
accountTrieProofKey = AccountTrieProofKey(
|
|
|
|
address: address, stateRoot: stateRoot)
|
|
|
|
contentKey = ContentKey(
|
|
|
|
contentType: accountTrieProof,
|
|
|
|
accountTrieProofKey: accountTrieProofKey)
|
|
|
|
|
|
|
|
let encoded = encode(contentKey)
|
2022-01-05 08:49:49 +00:00
|
|
|
check encoded.asSeq.toHex == contentKeyHex
|
2021-12-20 08:53:00 +00:00
|
|
|
let decoded = decode(encoded)
|
|
|
|
check decoded.isSome()
|
|
|
|
|
|
|
|
let contentKeyDecoded = decoded.get()
|
|
|
|
check:
|
|
|
|
contentKeyDecoded.contentType == contentKey.contentType
|
|
|
|
contentKeyDecoded.accountTrieProofKey == contentKey.accountTrieProofKey
|
|
|
|
|
2022-01-05 08:49:49 +00:00
|
|
|
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
|
|
|
# In stint this does BE hex string
|
|
|
|
toContentId(contentKey).toHex() == contentIdHexBE
|
2021-12-20 08:53:00 +00:00
|
|
|
|
|
|
|
test "ContractStorageTrieProof":
|
2022-01-05 08:49:49 +00:00
|
|
|
# Input
|
|
|
|
const
|
|
|
|
slot = 239304.stuint(256)
|
|
|
|
|
|
|
|
# Output
|
|
|
|
contentKeyHex =
|
|
|
|
"03829bd824b016326a401d083b33d092293333a830c8a6030000000000000000000000000000000000000000000000000000000000d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
|
|
|
contentId =
|
|
|
|
"80413803151602881485894828440259195604313253842905231566803078625935967002376"
|
|
|
|
# or
|
|
|
|
contentIdHexBE =
|
|
|
|
"b1c89984803cebd325303ba035f9c4ca0d0d91b2cbfef84d455e7a847ade1f08"
|
2021-12-20 08:53:00 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
contractStorageTrieProofKey = ContractStorageTrieProofKey(
|
|
|
|
address: address, slot: slot, stateRoot: stateRoot)
|
|
|
|
contentKey = ContentKey(
|
|
|
|
contentType: contractStorageTrieProof,
|
|
|
|
contractStorageTrieProofKey: contractStorageTrieProofKey)
|
|
|
|
|
|
|
|
let encoded = encode(contentKey)
|
2022-01-05 08:49:49 +00:00
|
|
|
check encoded.asSeq.toHex == contentKeyHex
|
2021-12-20 08:53:00 +00:00
|
|
|
let decoded = decode(encoded)
|
|
|
|
check decoded.isSome()
|
2021-12-15 07:48:56 +00:00
|
|
|
|
2021-12-20 08:53:00 +00:00
|
|
|
let contentKeyDecoded = decoded.get()
|
|
|
|
check:
|
|
|
|
contentKeyDecoded.contentType == contentKey.contentType
|
|
|
|
contentKeyDecoded.contractStorageTrieProofKey ==
|
|
|
|
contentKey.contractStorageTrieProofKey
|
|
|
|
|
2022-01-05 08:49:49 +00:00
|
|
|
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
|
|
|
# In stint this does BE hex string
|
|
|
|
toContentId(contentKey).toHex() == contentIdHexBE
|
2021-12-20 08:53:00 +00:00
|
|
|
|
|
|
|
test "ContractBytecode":
|
2022-01-05 08:49:49 +00:00
|
|
|
# Input
|
2022-06-16 06:50:29 +00:00
|
|
|
const codeHash = CodeHash.fromHex(
|
2021-12-20 08:53:00 +00:00
|
|
|
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
|
|
|
|
|
2022-01-05 08:49:49 +00:00
|
|
|
# Output
|
|
|
|
const
|
|
|
|
contentKeyHex =
|
|
|
|
"04829bd824b016326a401d083b33d092293333a830d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
|
|
|
contentId =
|
|
|
|
"9243655320250466575533858917172702581481192615849913473767356296630272634800"
|
|
|
|
# or
|
|
|
|
contentIdHexBE =
|
|
|
|
"146fb937afe42bcf11d25ad57d67734b9a7138677d59eeec3f402908f54dafb0"
|
|
|
|
|
2021-12-20 08:53:00 +00:00
|
|
|
let
|
|
|
|
contractBytecodeKey = ContractBytecodeKey(
|
|
|
|
address: address, codeHash: codeHash)
|
|
|
|
contentKey = ContentKey(
|
|
|
|
contentType: contractBytecode,
|
|
|
|
contractBytecodeKey: contractBytecodeKey)
|
|
|
|
|
|
|
|
let encoded = encode(contentKey)
|
2022-01-05 08:49:49 +00:00
|
|
|
check encoded.asSeq.toHex == contentKeyHex
|
2021-12-20 08:53:00 +00:00
|
|
|
let decoded = decode(encoded)
|
|
|
|
check decoded.isSome()
|
|
|
|
|
|
|
|
let contentKeyDecoded = decoded.get()
|
|
|
|
check:
|
|
|
|
contentKeyDecoded.contentType == contentKey.contentType
|
|
|
|
contentKeyDecoded.contractBytecodeKey == contentKey.contractBytecodeKey
|
|
|
|
|
2022-01-05 08:49:49 +00:00
|
|
|
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
|
|
|
# In stint this does BE hex string
|
|
|
|
toContentId(contentKey).toHex() == contentIdHexBE
|