diff --git a/fluffy/network/history/history_content.nim b/fluffy/network/history/history_content.nim index 6959a1e25..dc9d59f77 100644 --- a/fluffy/network/history/history_content.nim +++ b/fluffy/network/history/history_content.nim @@ -35,9 +35,6 @@ type template toSszType*(x: ContentType): uint8 = uint8(x) -template toSszType*(x: auto): auto = - x - func fromSszBytes*(T: type ContentType, data: openArray[byte]): T {.raises: [MalformedSszError, Defect].} = if data.len != sizeof(uint8): diff --git a/fluffy/tests/all_fluffy_tests.nim b/fluffy/tests/all_fluffy_tests.nim index 9982b98d4..40035326d 100644 --- a/fluffy/tests/all_fluffy_tests.nim +++ b/fluffy/tests/all_fluffy_tests.nim @@ -11,6 +11,7 @@ import ./test_portal_wire_encoding, ./test_portal_wire_protocol, ./test_state_distance, + ./test_state_content, ./test_state_network, ./test_history_content, ./test_content_db, diff --git a/fluffy/tests/test_history_content.nim b/fluffy/tests/test_history_content.nim index a799706e9..8d75a5d89 100644 --- a/fluffy/tests/test_history_content.nim +++ b/fluffy/tests/test_history_content.nim @@ -8,22 +8,28 @@ {.used.} import - unittest2, stint, stew/[byteutils, results], + unittest2, stew/byteutils, ../network/history/history_content suite "History ContentKey Encodings": test "ContentKey": - var blockHash: BlockHash # All zeroes - let contentKey = ContentKey(chainId: 1'u16, contentType: BlockBody, blockHash: blockHash) + var blockHash: BlockHash + blockHash.data = hexToByteArray[sizeof(BlockHash)]( + "0x0100000000000000000000000000000000000000000000000000000000000000") + let contentKey = + ContentKey(chainId: 1'u16, contentType: BlockBody, blockHash: blockHash) let encoded = encode(contentKey) check encoded.asSeq.toHex == - "0100020000000000000000000000000000000000000000000000000000000000000000" + "0100020100000000000000000000000000000000000000000000000000000000000000" let decoded = decode(encoded) check decoded.isSome() - let contentKey2 = decoded.get() + let contentKeyDecoded = decoded.get() check: - contentKey2.chainId == 1'u16 - contentKey2.contentType == BlockBody - contentKey2.blockHash == blockHash + contentKeyDecoded.chainId == contentKey.chainId + contentKeyDecoded.contentType == contentKey.contentType + contentKeyDecoded.blockHash == contentKey.blockHash + + toContentId(contentKey).toHex() == + "36a55e9aa5125c5fecc16bcb0234d9d3d6065eabc890c0d3b24d413d6ae9f9da" diff --git a/fluffy/tests/test_state_content.nim b/fluffy/tests/test_state_content.nim new file mode 100644 index 000000000..1d9823bf9 --- /dev/null +++ b/fluffy/tests/test_state_content.nim @@ -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.