nimbus-eth1/fluffy/tests/test_history_content.nim

216 lines
7.3 KiB
Nim

# Nimbus
# Copyright (c) 2021-2022 Status Research & Development GmbH
# 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, stint,
../network/history/history_content
# According to test vectors:
# https://github.com/ethereum/portal-network-specs/blob/master/content-keys-test-vectors.md#history-network-keys
suite "History ContentKey Encodings":
test "BlockHeader":
# Input
const blockHash = BlockHash.fromHex(
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
# Output
const
contentKeyHex =
"00d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
contentId =
"28281392725701906550238743427348001871342819822834514257505083923073246729726"
# or
contentIdHexBE =
"3e86b3767b57402ea72e369ae0496ce47cc15be685bec3b4726b9f316e3895fe"
let contentKey = ContentKey(
contentType: blockHeader,
blockHeaderKey: BlockKey(blockHash: blockHash))
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.blockHeaderKey == contentKey.blockHeaderKey
toContentId(contentKey) == parse(contentId, StUint[256], 10)
# In stint this does BE hex string
toContentId(contentKey).toHex() == contentIdHexBE
test "BlockBody":
# Input
const blockHash = BlockHash.fromHex(
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
# Output
const
contentKeyHex =
"01d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
contentId =
"106696502175825986237944249828698290888857178633945273402044845898673345165419"
# or
contentIdHexBE =
"ebe414854629d60c58ddd5bf60fd72e41760a5f7a463fdcb169f13ee4a26786b"
let contentKey = ContentKey(
contentType: blockBody,
blockBodyKey: BlockKey(blockHash: blockHash))
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.blockBodyKey == contentKey.blockBodyKey
toContentId(contentKey) == parse(contentId, StUint[256], 10)
# In stint this does BE hex string
toContentId(contentKey).toHex() == contentIdHexBE
test "Receipts":
# Input
const blockHash = BlockHash.fromHex(
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
# Output
const
contentKeyHex =
"02d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
contentId =
"76230538398907151249589044529104962263309222250374376758768131420767496438948"
# or
contentIdHexBE =
"a888f4aafe9109d495ac4d4774a6277c1ada42035e3da5e10a04cc93247c04a4"
let contentKey = ContentKey(
contentType: receipts,
receiptsKey: BlockKey(blockHash: blockHash))
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.receiptsKey == contentKey.receiptsKey
toContentId(contentKey) == parse(contentId, StUint[256], 10)
# In stint this does BE hex string
toContentId(contentKey).toHex() == contentIdHexBE
test "Epoch Accumulator":
# Input
const epochHash = Digest.fromHex(
"0xe242814b90ed3950e13aac7e56ce116540c71b41d1516605aada26c6c07cc491")
# Output
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
toContentId(contentKey) == parse(contentId, StUint[256], 10)
# In stint this does BE hex string
toContentId(contentKey).toHex() == contentIdHexBE
test "Master Accumulator - Latest":
# Output
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
toContentId(contentKey) == parse(contentId, StUint[256], 10)
# In stint this does BE hex string
toContentId(contentKey).toHex() == contentIdHexBE
test "Master Accumulator - Hash":
# Input
const accumulatorHash = Digest.fromHex(
"0x88cce8439ebc0c1d007177ffb6831c15c07b4361984cc52235b6fd728434f0c7")
# Output
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
toContentId(contentKey) == parse(contentId, StUint[256], 10)
# In stint this does BE hex string
toContentId(contentKey).toHex() == contentIdHexBE