2021-10-09 11:22:03 +00:00
|
|
|
# Nimbus
|
2022-06-14 21:38:34 +00:00
|
|
|
# Copyright (c) 2021-2022 Status Research & Development GmbH
|
2021-10-09 11:22:03 +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
|
2022-01-05 08:49:49 +00:00
|
|
|
unittest2, stew/byteutils, stint,
|
2021-10-09 11:22:03 +00:00
|
|
|
../network/history/history_content
|
|
|
|
|
2021-12-20 08:53:00 +00:00
|
|
|
# According to test vectors:
|
2022-06-14 21:38:34 +00:00
|
|
|
# https://github.com/ethereum/portal-network-specs/blob/master/content-keys-test-vectors.md#history-network-keys
|
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
suite "History ContentKey Encodings":
|
2021-12-20 08:53:00 +00:00
|
|
|
test "BlockHeader":
|
2022-01-05 08:49:49 +00:00
|
|
|
# Input
|
2022-06-16 06:50:29 +00:00
|
|
|
const blockHash = BlockHash.fromHex(
|
2021-12-20 08:53:00 +00:00
|
|
|
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
|
2022-01-05 08:49:49 +00:00
|
|
|
|
|
|
|
# Output
|
|
|
|
const
|
|
|
|
contentKeyHex =
|
2022-09-28 07:09:38 +00:00
|
|
|
"00d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
2022-01-05 08:49:49 +00:00
|
|
|
contentId =
|
2022-09-28 07:09:38 +00:00
|
|
|
"28281392725701906550238743427348001871342819822834514257505083923073246729726"
|
2022-01-05 08:49:49 +00:00
|
|
|
# or
|
|
|
|
contentIdHexBE =
|
2022-09-28 07:09:38 +00:00
|
|
|
"3e86b3767b57402ea72e369ae0496ce47cc15be685bec3b4726b9f316e3895fe"
|
2022-01-05 08:49:49 +00:00
|
|
|
|
|
|
|
let contentKey = ContentKey(
|
|
|
|
contentType: blockHeader,
|
2022-09-28 07:09:38 +00:00
|
|
|
blockHeaderKey: BlockKey(blockHash: blockHash))
|
2021-10-09 11:22:03 +00:00
|
|
|
|
|
|
|
let encoded = encode(contentKey)
|
2022-01-05 08:49:49 +00:00
|
|
|
check encoded.asSeq.toHex == contentKeyHex
|
2021-10-09 11:22:03 +00:00
|
|
|
let decoded = decode(encoded)
|
|
|
|
check decoded.isSome()
|
|
|
|
|
2021-12-15 07:48:56 +00:00
|
|
|
let contentKeyDecoded = decoded.get()
|
2021-10-09 11:22:03 +00:00
|
|
|
check:
|
2021-12-15 07:48:56 +00:00
|
|
|
contentKeyDecoded.contentType == contentKey.contentType
|
2022-01-05 08:49:49 +00:00
|
|
|
contentKeyDecoded.blockHeaderKey == contentKey.blockHeaderKey
|
2021-12-15 07:48:56 +00:00
|
|
|
|
2022-09-10 19:00:27 +00:00
|
|
|
toContentId(contentKey) == parse(contentId, StUint[256], 10)
|
2022-01-05 08:49:49 +00:00
|
|
|
# In stint this does BE hex string
|
|
|
|
toContentId(contentKey).toHex() == contentIdHexBE
|
2021-12-20 08:53:00 +00:00
|
|
|
|
|
|
|
test "BlockBody":
|
2022-01-05 08:49:49 +00:00
|
|
|
# Input
|
2022-06-16 06:50:29 +00:00
|
|
|
const blockHash = BlockHash.fromHex(
|
2021-12-20 08:53:00 +00:00
|
|
|
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
|
2022-01-05 08:49:49 +00:00
|
|
|
|
|
|
|
# Output
|
|
|
|
const
|
|
|
|
contentKeyHex =
|
2022-09-28 07:09:38 +00:00
|
|
|
"01d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
2022-01-05 08:49:49 +00:00
|
|
|
contentId =
|
2022-09-28 07:09:38 +00:00
|
|
|
"106696502175825986237944249828698290888857178633945273402044845898673345165419"
|
2022-01-05 08:49:49 +00:00
|
|
|
# or
|
|
|
|
contentIdHexBE =
|
2022-09-28 07:09:38 +00:00
|
|
|
"ebe414854629d60c58ddd5bf60fd72e41760a5f7a463fdcb169f13ee4a26786b"
|
2022-01-05 08:49:49 +00:00
|
|
|
|
|
|
|
let contentKey = ContentKey(
|
|
|
|
contentType: blockBody,
|
2022-09-28 07:09:38 +00:00
|
|
|
blockBodyKey: BlockKey(blockHash: blockHash))
|
2021-12-20 08:53:00 +00:00
|
|
|
|
|
|
|
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
|
2022-01-05 08:49:49 +00:00
|
|
|
contentKeyDecoded.blockBodyKey == contentKey.blockBodyKey
|
2021-12-20 08:53:00 +00:00
|
|
|
|
2022-09-10 19:00:27 +00:00
|
|
|
toContentId(contentKey) == parse(contentId, StUint[256], 10)
|
2022-01-05 08:49:49 +00:00
|
|
|
# In stint this does BE hex string
|
|
|
|
toContentId(contentKey).toHex() == contentIdHexBE
|
2021-12-20 08:53:00 +00:00
|
|
|
|
|
|
|
test "Receipts":
|
2022-06-16 06:50:29 +00:00
|
|
|
# Input
|
|
|
|
const blockHash = BlockHash.fromHex(
|
2021-12-20 08:53:00 +00:00
|
|
|
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
|
2022-01-05 08:49:49 +00:00
|
|
|
|
|
|
|
# Output
|
|
|
|
const
|
|
|
|
contentKeyHex =
|
2022-09-28 07:09:38 +00:00
|
|
|
"02d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
2022-01-05 08:49:49 +00:00
|
|
|
contentId =
|
2022-09-28 07:09:38 +00:00
|
|
|
"76230538398907151249589044529104962263309222250374376758768131420767496438948"
|
2022-01-05 08:49:49 +00:00
|
|
|
# or
|
|
|
|
contentIdHexBE =
|
2022-09-28 07:09:38 +00:00
|
|
|
"a888f4aafe9109d495ac4d4774a6277c1ada42035e3da5e10a04cc93247c04a4"
|
2022-01-05 08:49:49 +00:00
|
|
|
|
|
|
|
let contentKey = ContentKey(
|
|
|
|
contentType: receipts,
|
2022-09-28 07:09:38 +00:00
|
|
|
receiptsKey: BlockKey(blockHash: blockHash))
|
2021-12-20 08:53:00 +00:00
|
|
|
|
|
|
|
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
|
2022-01-05 08:49:49 +00:00
|
|
|
contentKeyDecoded.receiptsKey == contentKey.receiptsKey
|
2021-12-20 08:53:00 +00:00
|
|
|
|
2022-09-10 19:00:27 +00:00
|
|
|
toContentId(contentKey) == parse(contentId, StUint[256], 10)
|
2022-01-05 08:49:49 +00:00
|
|
|
# In stint this does BE hex string
|
|
|
|
toContentId(contentKey).toHex() == contentIdHexBE
|
2022-06-14 21:38:34 +00:00
|
|
|
|
|
|
|
test "Epoch Accumulator":
|
2022-06-16 06:50:29 +00:00
|
|
|
# Input
|
|
|
|
const epochHash = Digest.fromHex(
|
2022-06-14 21:38:34 +00:00
|
|
|
"0xe242814b90ed3950e13aac7e56ce116540c71b41d1516605aada26c6c07cc491")
|
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
# Output
|
2022-06-14 21:38:34 +00:00
|
|
|
const
|
|
|
|
contentKeyHex =
|
|
|
|
"03e242814b90ed3950e13aac7e56ce116540c71b41d1516605aada26c6c07cc491"
|
|
|
|
contentId =
|
|
|
|
"72232402989179419196382321898161638871438419016077939952896528930608027961710"
|
|
|
|
# or
|
|
|
|
contentIdHexBE =
|
|
|
|
"9fb2175e76c6989e0fdac3ee10c40d2a81eb176af32e1c16193e3904fe56896e"
|
|
|
|
|
|
|
|
let contentKey = ContentKey(
|
|
|
|
contentType: epochAccumulator,
|
|
|
|
epochAccumulatorKey: EpochAccumulatorKey(epochHash: epochHash))
|
|
|
|
|
|
|
|
let encoded = encode(contentKey)
|
|
|
|
check encoded.asSeq.toHex == contentKeyHex
|
|
|
|
let decoded = decode(encoded)
|
|
|
|
check decoded.isSome()
|
|
|
|
|
|
|
|
let contentKeyDecoded = decoded.get()
|
|
|
|
check:
|
|
|
|
contentKeyDecoded.contentType == contentKey.contentType
|
|
|
|
contentKeyDecoded.epochAccumulatorKey == contentKey.epochAccumulatorKey
|
|
|
|
|
2022-09-10 19:00:27 +00:00
|
|
|
toContentId(contentKey) == parse(contentId, StUint[256], 10)
|
2022-06-14 21:38:34 +00:00
|
|
|
# In stint this does BE hex string
|
|
|
|
toContentId(contentKey).toHex() == contentIdHexBE
|
|
|
|
|
|
|
|
test "Master Accumulator - Latest":
|
2022-06-16 06:50:29 +00:00
|
|
|
# Output
|
2022-06-14 21:38:34 +00:00
|
|
|
const
|
|
|
|
contentKeyHex =
|
|
|
|
"0400"
|
|
|
|
contentId =
|
|
|
|
"87173654316145541646904042090629917349369185510102051783618763191692466404071"
|
|
|
|
# or
|
|
|
|
contentIdHexBE =
|
|
|
|
"c0ba8a33ac67f44abff5984dfbb6f56c46b880ac2b86e1f23e7fa9c402c53ae7"
|
|
|
|
|
|
|
|
let contentKey = ContentKey(
|
|
|
|
contentType: masterAccumulator,
|
|
|
|
masterAccumulatorKey: MasterAccumulatorKey(accumulaterKeyType: latest))
|
|
|
|
|
|
|
|
let encoded = encode(contentKey)
|
|
|
|
check encoded.asSeq.toHex == contentKeyHex
|
|
|
|
let decoded = decode(encoded)
|
|
|
|
check decoded.isSome()
|
|
|
|
|
|
|
|
let contentKeyDecoded = decoded.get()
|
|
|
|
check:
|
|
|
|
contentKeyDecoded.contentType == contentKey.contentType
|
|
|
|
contentKeyDecoded.masterAccumulatorKey.accumulaterKeyType ==
|
|
|
|
contentKey.masterAccumulatorKey.accumulaterKeyType
|
|
|
|
|
2022-09-10 19:00:27 +00:00
|
|
|
toContentId(contentKey) == parse(contentId, StUint[256], 10)
|
2022-06-14 21:38:34 +00:00
|
|
|
# In stint this does BE hex string
|
|
|
|
toContentId(contentKey).toHex() == contentIdHexBE
|
|
|
|
|
|
|
|
test "Master Accumulator - Hash":
|
2022-06-16 06:50:29 +00:00
|
|
|
# Input
|
|
|
|
const accumulatorHash = Digest.fromHex(
|
2022-06-14 21:38:34 +00:00
|
|
|
"0x88cce8439ebc0c1d007177ffb6831c15c07b4361984cc52235b6fd728434f0c7")
|
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
# Output
|
2022-06-14 21:38:34 +00:00
|
|
|
const
|
|
|
|
contentKeyHex =
|
|
|
|
"040188cce8439ebc0c1d007177ffb6831c15c07b4361984cc52235b6fd728434f0c7"
|
|
|
|
contentId =
|
|
|
|
"79362820890138237094338894474079140563693945795365426184460738681339857347750"
|
|
|
|
# or
|
|
|
|
contentIdHexBE =
|
|
|
|
"af75c3c9d0e89a5083361a3334a9c5583955f0dbe9a413eb79ba26400d1824a6"
|
|
|
|
|
|
|
|
let contentKey = ContentKey(
|
|
|
|
contentType: masterAccumulator,
|
|
|
|
masterAccumulatorKey: MasterAccumulatorKey(
|
|
|
|
accumulaterKeyType: masterHash, masterHashKey: accumulatorHash))
|
|
|
|
|
|
|
|
let encoded = encode(contentKey)
|
|
|
|
check encoded.asSeq.toHex == contentKeyHex
|
|
|
|
let decoded = decode(encoded)
|
|
|
|
check decoded.isSome()
|
|
|
|
|
|
|
|
let contentKeyDecoded = decoded.get()
|
|
|
|
check:
|
|
|
|
contentKeyDecoded.contentType == contentKey.contentType
|
|
|
|
contentKeyDecoded.masterAccumulatorKey.accumulaterKeyType ==
|
|
|
|
contentKey.masterAccumulatorKey.accumulaterKeyType
|
|
|
|
contentKeyDecoded.masterAccumulatorKey.masterHashKey ==
|
|
|
|
contentKey.masterAccumulatorKey.masterHashKey
|
|
|
|
|
2022-09-10 19:00:27 +00:00
|
|
|
toContentId(contentKey) == parse(contentId, StUint[256], 10)
|
2022-06-14 21:38:34 +00:00
|
|
|
# In stint this does BE hex string
|
|
|
|
toContentId(contentKey).toHex() == contentIdHexBE
|