Add state ContentKey test and adjust history ContentKey test (#914)
This commit is contained in:
parent
6a7803a9e4
commit
092d946c72
|
@ -35,9 +35,6 @@ type
|
||||||
template toSszType*(x: ContentType): uint8 =
|
template toSszType*(x: ContentType): uint8 =
|
||||||
uint8(x)
|
uint8(x)
|
||||||
|
|
||||||
template toSszType*(x: auto): auto =
|
|
||||||
x
|
|
||||||
|
|
||||||
func fromSszBytes*(T: type ContentType, data: openArray[byte]):
|
func fromSszBytes*(T: type ContentType, data: openArray[byte]):
|
||||||
T {.raises: [MalformedSszError, Defect].} =
|
T {.raises: [MalformedSszError, Defect].} =
|
||||||
if data.len != sizeof(uint8):
|
if data.len != sizeof(uint8):
|
||||||
|
|
|
@ -11,6 +11,7 @@ import
|
||||||
./test_portal_wire_encoding,
|
./test_portal_wire_encoding,
|
||||||
./test_portal_wire_protocol,
|
./test_portal_wire_protocol,
|
||||||
./test_state_distance,
|
./test_state_distance,
|
||||||
|
./test_state_content,
|
||||||
./test_state_network,
|
./test_state_network,
|
||||||
./test_history_content,
|
./test_history_content,
|
||||||
./test_content_db,
|
./test_content_db,
|
||||||
|
|
|
@ -8,22 +8,28 @@
|
||||||
{.used.}
|
{.used.}
|
||||||
|
|
||||||
import
|
import
|
||||||
unittest2, stint, stew/[byteutils, results],
|
unittest2, stew/byteutils,
|
||||||
../network/history/history_content
|
../network/history/history_content
|
||||||
|
|
||||||
suite "History ContentKey Encodings":
|
suite "History ContentKey Encodings":
|
||||||
test "ContentKey":
|
test "ContentKey":
|
||||||
var blockHash: BlockHash # All zeroes
|
var blockHash: BlockHash
|
||||||
let contentKey = ContentKey(chainId: 1'u16, contentType: BlockBody, blockHash: blockHash)
|
blockHash.data = hexToByteArray[sizeof(BlockHash)](
|
||||||
|
"0x0100000000000000000000000000000000000000000000000000000000000000")
|
||||||
|
let contentKey =
|
||||||
|
ContentKey(chainId: 1'u16, contentType: BlockBody, blockHash: blockHash)
|
||||||
|
|
||||||
let encoded = encode(contentKey)
|
let encoded = encode(contentKey)
|
||||||
check encoded.asSeq.toHex ==
|
check encoded.asSeq.toHex ==
|
||||||
"0100020000000000000000000000000000000000000000000000000000000000000000"
|
"0100020100000000000000000000000000000000000000000000000000000000000000"
|
||||||
let decoded = decode(encoded)
|
let decoded = decode(encoded)
|
||||||
check decoded.isSome()
|
check decoded.isSome()
|
||||||
|
|
||||||
let contentKey2 = decoded.get()
|
let contentKeyDecoded = decoded.get()
|
||||||
check:
|
check:
|
||||||
contentKey2.chainId == 1'u16
|
contentKeyDecoded.chainId == contentKey.chainId
|
||||||
contentKey2.contentType == BlockBody
|
contentKeyDecoded.contentType == contentKey.contentType
|
||||||
contentKey2.blockHash == blockHash
|
contentKeyDecoded.blockHash == contentKey.blockHash
|
||||||
|
|
||||||
|
toContentId(contentKey).toHex() ==
|
||||||
|
"36a55e9aa5125c5fecc16bcb0234d9d3d6065eabc890c0d3b24d413d6ae9f9da"
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
# Nimbus
|
||||||
|
# Copyright (c) 2021 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,
|
||||||
|
../network/state/state_content
|
||||||
|
|
||||||
|
suite "State ContentKey Encodings":
|
||||||
|
test "ContentKey - accountTrieNode":
|
||||||
|
let path = ByteList.init(hexToSeqByte("0x0304"))
|
||||||
|
var nodeHash: NodeHash
|
||||||
|
nodeHash.data = hexToByteArray[sizeof(NodeHash)](
|
||||||
|
"0x0100000000000000000000000000000000000000000000000000000000000000")
|
||||||
|
let stateRoot = hexToByteArray[sizeof(Bytes32)](
|
||||||
|
"0x0200000000000000000000000000000000000000000000000000000000000000")
|
||||||
|
|
||||||
|
let accountTrieNodeKey = AccountTrieNodeKey(
|
||||||
|
path: path, nodeHash: nodeHash, stateRoot: stateRoot)
|
||||||
|
let contentKey = ContentKey(
|
||||||
|
contentType: accountTrieNode, accountTrieNodeKey: accountTrieNodeKey)
|
||||||
|
|
||||||
|
let encoded = encode(contentKey)
|
||||||
|
check encoded.asSeq.toHex ==
|
||||||
|
"0044000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000304"
|
||||||
|
let decoded = decode(encoded)
|
||||||
|
check decoded.isSome()
|
||||||
|
|
||||||
|
let contentKeyDecoded = decoded.get()
|
||||||
|
check:
|
||||||
|
contentKeyDecoded.contentType == contentKey.contentType
|
||||||
|
contentKeyDecoded.accountTrieNodeKey == contentKey.accountTrieNodeKey
|
||||||
|
|
||||||
|
toContentId(contentKey).toHex() ==
|
||||||
|
"17cc73bd15072a4f62fbec6e4dd0fa99bdc103e73b90143f01bb4079955ba74c"
|
||||||
|
|
||||||
|
# TODO: Add test for each ContentType, perhaps when path is specced out and
|
||||||
|
# test vectors exist.
|
Loading…
Reference in New Issue