mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-11 21:04:11 +00:00
History content keys (#919)
* Change History content key to us SSZ Union and adjust tests * Change slot to byteBE instead of LE This is currently not specified in the Portal network specifications, but we are using already BE for the actual content key, so change it also here to remain consistent.
This commit is contained in:
parent
f4ca598c03
commit
a8640fe57c
@ -19,33 +19,27 @@ export ssz_serialization, common_types
|
||||
|
||||
type
|
||||
ContentType* = enum
|
||||
BlockHeader = 0x01
|
||||
BlockBody = 0x02
|
||||
Receipts = 0x03
|
||||
blockHeader = 0x00
|
||||
blockBody = 0x01
|
||||
receipts = 0x02
|
||||
|
||||
BlockHash* = MDigest[32 * 8] # Bytes32
|
||||
|
||||
ContentKey* = object
|
||||
ContentKeyType* = object
|
||||
chainId*: uint16
|
||||
contentType*: ContentType
|
||||
blockHash*: BlockHash
|
||||
|
||||
ContentKey* = object
|
||||
case contentType*: ContentType
|
||||
of blockHeader:
|
||||
blockHeaderKey*: ContentKeyType
|
||||
of blockBody:
|
||||
blockBodyKey*: ContentKeyType
|
||||
of receipts:
|
||||
receiptsKey*: ContentKeyType
|
||||
|
||||
ContentId* = Uint256
|
||||
|
||||
template toSszType*(x: ContentType): uint8 =
|
||||
uint8(x)
|
||||
|
||||
func fromSszBytes*(T: type ContentType, data: openArray[byte]):
|
||||
T {.raises: [MalformedSszError, Defect].} =
|
||||
if data.len != sizeof(uint8):
|
||||
raiseIncorrectSize T
|
||||
|
||||
var contentType: T
|
||||
if not checkedEnumAssign(contentType, data[0]):
|
||||
raiseIncorrectSize T
|
||||
|
||||
contentType
|
||||
|
||||
func encode*(contentKey: ContentKey): ByteList =
|
||||
ByteList.init(SSZ.encode(contentKey))
|
||||
|
||||
|
@ -111,7 +111,7 @@ func toContentId*(contentKey: ContentKey): ContentId =
|
||||
h.update(key.address)
|
||||
let n2 =
|
||||
block: computeContentId keccak256:
|
||||
h.update(toBytesLE(key.slot))
|
||||
h.update(toBytesBE(key.slot))
|
||||
|
||||
n1 + n2 # uint256 will wrap arround, practically applying the modulo 256
|
||||
of contractBytecode: # sha256(address | code_hash)
|
||||
|
@ -8,77 +8,110 @@
|
||||
{.used.}
|
||||
|
||||
import
|
||||
unittest2, stew/byteutils,
|
||||
unittest2, stew/byteutils, stint,
|
||||
../network/history/history_content
|
||||
|
||||
# According to test vectors:
|
||||
# TODO: Add link once test vectors are merged
|
||||
suite "History ContentKey Encodings":
|
||||
test "BlockHeader":
|
||||
# Input
|
||||
var blockHash: BlockHash
|
||||
blockHash.data = hexToByteArray[sizeof(BlockHash)](
|
||||
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
|
||||
let contentKey =
|
||||
ContentKey(chainId: 15'u16, contentType: BlockHeader, blockHash: blockHash)
|
||||
|
||||
# Output
|
||||
const
|
||||
contentKeyHex =
|
||||
"000f00d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
contentId =
|
||||
"15025167517633317571792618561170587584740338038067807801482118109695980329625"
|
||||
# or
|
||||
contentIdHexBE =
|
||||
"2137f185b713a60dd1190e650d01227b4f94ecddc9c95478e2c591c40557da99"
|
||||
|
||||
let contentKey = ContentKey(
|
||||
contentType: blockHeader,
|
||||
blockHeaderKey: ContentKeyType(chainId: 15'u16, blockHash: blockHash))
|
||||
|
||||
let encoded = encode(contentKey)
|
||||
check encoded.asSeq.toHex ==
|
||||
"0f0001d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
# "010f00d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
check encoded.asSeq.toHex == contentKeyHex
|
||||
let decoded = decode(encoded)
|
||||
check decoded.isSome()
|
||||
|
||||
let contentKeyDecoded = decoded.get()
|
||||
check:
|
||||
contentKeyDecoded.chainId == contentKey.chainId
|
||||
contentKeyDecoded.contentType == contentKey.contentType
|
||||
contentKeyDecoded.blockHash == contentKey.blockHash
|
||||
contentKeyDecoded.blockHeaderKey == contentKey.blockHeaderKey
|
||||
|
||||
toContentId(contentKey).toHex() ==
|
||||
"9a310df5e6135cbd834041011be1b350e589ba013f11584ed527583bc39d3c27"
|
||||
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
||||
# In stint this does BE hex string
|
||||
toContentId(contentKey).toHex() == contentIdHexBE
|
||||
|
||||
test "BlockBody":
|
||||
# Input
|
||||
var blockHash: BlockHash
|
||||
blockHash.data = hexToByteArray[sizeof(BlockHash)](
|
||||
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
|
||||
let contentKey =
|
||||
ContentKey(chainId: 20'u16, contentType: BlockBody, blockHash: blockHash)
|
||||
|
||||
# Output
|
||||
const
|
||||
contentKeyHex =
|
||||
"011400d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
contentId =
|
||||
"12834862124958403129911294156243112356210437741210740000860318140844473844426"
|
||||
# or
|
||||
contentIdHexBE =
|
||||
"1c6046475f0772132774ab549173ca8487bea031ce539cad8e990c08df5802ca"
|
||||
|
||||
let contentKey = ContentKey(
|
||||
contentType: blockBody,
|
||||
blockBodyKey: ContentKeyType(chainId: 20'u16, blockHash: blockHash))
|
||||
|
||||
let encoded = encode(contentKey)
|
||||
check encoded.asSeq.toHex ==
|
||||
"140002d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
# "021400d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
check encoded.asSeq.toHex == contentKeyHex
|
||||
let decoded = decode(encoded)
|
||||
check decoded.isSome()
|
||||
|
||||
let contentKeyDecoded = decoded.get()
|
||||
check:
|
||||
contentKeyDecoded.chainId == contentKey.chainId
|
||||
contentKeyDecoded.contentType == contentKey.contentType
|
||||
contentKeyDecoded.blockHash == contentKey.blockHash
|
||||
contentKeyDecoded.blockBodyKey == contentKey.blockBodyKey
|
||||
|
||||
toContentId(contentKey).toHex() ==
|
||||
"42a9bb9fd974f4d3020fe81aa584277010a9e344bed52bf1610e9d360203380a"
|
||||
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
||||
# In stint this does BE hex string
|
||||
toContentId(contentKey).toHex() == contentIdHexBE
|
||||
|
||||
test "Receipts":
|
||||
var blockHash: BlockHash
|
||||
blockHash.data = hexToByteArray[sizeof(BlockHash)](
|
||||
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
|
||||
let contentKey =
|
||||
ContentKey(chainId: 4'u16, contentType: Receipts, blockHash: blockHash)
|
||||
|
||||
# Output
|
||||
const
|
||||
contentKeyHex =
|
||||
"020400d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
contentId =
|
||||
"76995449220721979583200368506411933662679656077191192504502358532083948020658"
|
||||
# or
|
||||
contentIdHexBE =
|
||||
"aa39e1423e92f5a667ace5b79c2c98adbfd79c055d891d0b9c49c40f816563b2"
|
||||
|
||||
let contentKey = ContentKey(
|
||||
contentType: receipts,
|
||||
receiptsKey: ContentKeyType(chainId: 4'u16, blockHash: blockHash))
|
||||
|
||||
|
||||
let encoded = encode(contentKey)
|
||||
check encoded.asSeq.toHex ==
|
||||
"040003d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
# "030400d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
check encoded.asSeq.toHex == contentKeyHex
|
||||
let decoded = decode(encoded)
|
||||
check decoded.isSome()
|
||||
|
||||
let contentKeyDecoded = decoded.get()
|
||||
check:
|
||||
contentKeyDecoded.chainId == contentKey.chainId
|
||||
contentKeyDecoded.contentType == contentKey.contentType
|
||||
contentKeyDecoded.blockHash == contentKey.blockHash
|
||||
contentKeyDecoded.receiptsKey == contentKey.receiptsKey
|
||||
|
||||
toContentId(contentKey).toHex() ==
|
||||
"4b92510bafa02f62811ce6d0e27d2424ba34d41fbe38abc3ea4e274d6c76fa3e"
|
||||
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
||||
# In stint this does BE hex string
|
||||
toContentId(contentKey).toHex() == contentIdHexBE
|
||||
|
@ -15,6 +15,7 @@ import
|
||||
# TODO: Add link once test vectors are merged
|
||||
|
||||
suite "State ContentKey Encodings":
|
||||
# Common input
|
||||
const
|
||||
stateRoot = hexToByteArray[sizeof(Bytes32)](
|
||||
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
|
||||
@ -22,10 +23,21 @@ suite "State ContentKey Encodings":
|
||||
"0x829bd824b016326a401d083b33d092293333a830")
|
||||
|
||||
test "AccountTrieNode":
|
||||
# Input
|
||||
var nodeHash: NodeHash
|
||||
nodeHash.data = hexToByteArray[sizeof(NodeHash)](
|
||||
"0xb8be7903aee73b8f6a59cd44a1f52c62148e1f376c0dfa1f5f773a98666efc2b")
|
||||
let path = ByteList.init(@[byte 1, 2, 0, 1])
|
||||
const
|
||||
path = ByteList.init(@[byte 1, 2, 0, 1])
|
||||
|
||||
# Output
|
||||
contentKeyHex =
|
||||
"0044000000b8be7903aee73b8f6a59cd44a1f52c62148e1f376c0dfa1f5f773a98666efc2bd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d01020001"
|
||||
contentId =
|
||||
"41237096982860596884042712109427867048220765019203857308279863638242761605893"
|
||||
# or
|
||||
contentIdHexBE =
|
||||
"5b2b5ea9a7384491010c1aa459a0f967dcf8b69988adbfe7e0bed513e9bb8305"
|
||||
|
||||
let
|
||||
accountTrieNodeKey = AccountTrieNodeKey(
|
||||
@ -34,8 +46,7 @@ suite "State ContentKey Encodings":
|
||||
contentType: accountTrieNode, accountTrieNodeKey: accountTrieNodeKey)
|
||||
|
||||
let encoded = encode(contentKey)
|
||||
check encoded.asSeq.toHex ==
|
||||
"0044000000b8be7903aee73b8f6a59cd44a1f52c62148e1f376c0dfa1f5f773a98666efc2bd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d01020001"
|
||||
check encoded.asSeq.toHex == contentKeyHex
|
||||
let decoded = decode(encoded)
|
||||
check decoded.isSome()
|
||||
|
||||
@ -44,14 +55,26 @@ suite "State ContentKey Encodings":
|
||||
contentKeyDecoded.contentType == contentKey.contentType
|
||||
contentKeyDecoded.accountTrieNodeKey == contentKey.accountTrieNodeKey
|
||||
|
||||
toContentId(contentKey).toHex() ==
|
||||
"5b2b5ea9a7384491010c1aa459a0f967dcf8b69988adbfe7e0bed513e9bb8305"
|
||||
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
||||
# In stint this does BE hex string
|
||||
toContentId(contentKey).toHex() == contentIdHexBE
|
||||
|
||||
test "ContractStorageTrieNode":
|
||||
# Input
|
||||
var nodeHash: NodeHash
|
||||
nodeHash.data = hexToByteArray[sizeof(NodeHash)](
|
||||
"0x3e190b68719aecbcb28ed2271014dd25f2aa633184988eb414189ce0899cade5")
|
||||
let path = ByteList.init(@[byte 1, 0, 15, 14, 12, 0])
|
||||
const
|
||||
path = ByteList.init(@[byte 1, 0, 15, 14, 12, 0])
|
||||
|
||||
# Output
|
||||
contentKeyHex =
|
||||
"01829bd824b016326a401d083b33d092293333a830580000003e190b68719aecbcb28ed2271014dd25f2aa633184988eb414189ce0899cade5d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d01000f0e0c00"
|
||||
contentId =
|
||||
"43529358882110548041037387588279806363134301284609868141745095118932570363585"
|
||||
# or
|
||||
contentIdHexBE =
|
||||
"603cbe7902925ce359822378a4cb1b4b53e1bf19d003de2c26e55812d76956c1"
|
||||
|
||||
let
|
||||
contractStorageTrieNodeKey = ContractStorageTrieNodeKey(
|
||||
@ -61,8 +84,7 @@ suite "State ContentKey Encodings":
|
||||
contractStorageTrieNodeKey: contractStorageTrieNodeKey)
|
||||
|
||||
let encoded = encode(contentKey)
|
||||
check encoded.asSeq.toHex ==
|
||||
"01829bd824b016326a401d083b33d092293333a830580000003e190b68719aecbcb28ed2271014dd25f2aa633184988eb414189ce0899cade5d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d01000f0e0c00"
|
||||
check encoded.asSeq.toHex == contentKeyHex
|
||||
let decoded = decode(encoded)
|
||||
check decoded.isSome()
|
||||
|
||||
@ -72,10 +94,21 @@ suite "State ContentKey Encodings":
|
||||
contentKeyDecoded.contractStorageTrieNodeKey ==
|
||||
contentKey.contractStorageTrieNodeKey
|
||||
|
||||
toContentId(contentKey).toHex() ==
|
||||
"603cbe7902925ce359822378a4cb1b4b53e1bf19d003de2c26e55812d76956c1"
|
||||
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
||||
# In stint this does BE hex string
|
||||
toContentId(contentKey).toHex() == contentIdHexBE
|
||||
|
||||
test "AccountTrieProof":
|
||||
# Output
|
||||
const
|
||||
contentKeyHex =
|
||||
"02829bd824b016326a401d083b33d092293333a830d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
contentId =
|
||||
"45301550050471302973396879294932122279426162994178563319590607565171451545101"
|
||||
# or
|
||||
contentIdHexBE =
|
||||
"6427c4c8d42db15c2aca8dfc7dff7ce2c8c835441b566424fa3377dd031cc60d"
|
||||
|
||||
let
|
||||
accountTrieProofKey = AccountTrieProofKey(
|
||||
address: address, stateRoot: stateRoot)
|
||||
@ -84,8 +117,7 @@ suite "State ContentKey Encodings":
|
||||
accountTrieProofKey: accountTrieProofKey)
|
||||
|
||||
let encoded = encode(contentKey)
|
||||
check encoded.asSeq.toHex ==
|
||||
"02829bd824b016326a401d083b33d092293333a830d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
check encoded.asSeq.toHex == contentKeyHex
|
||||
let decoded = decode(encoded)
|
||||
check decoded.isSome()
|
||||
|
||||
@ -94,11 +126,23 @@ suite "State ContentKey Encodings":
|
||||
contentKeyDecoded.contentType == contentKey.contentType
|
||||
contentKeyDecoded.accountTrieProofKey == contentKey.accountTrieProofKey
|
||||
|
||||
toContentId(contentKey).toHex() ==
|
||||
"6427c4c8d42db15c2aca8dfc7dff7ce2c8c835441b566424fa3377dd031cc60d"
|
||||
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
||||
# In stint this does BE hex string
|
||||
toContentId(contentKey).toHex() == contentIdHexBE
|
||||
|
||||
test "ContractStorageTrieProof":
|
||||
let slot = 239304.stuint(256)
|
||||
# Input
|
||||
const
|
||||
slot = 239304.stuint(256)
|
||||
|
||||
# Output
|
||||
contentKeyHex =
|
||||
"03829bd824b016326a401d083b33d092293333a830c8a6030000000000000000000000000000000000000000000000000000000000d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
contentId =
|
||||
"80413803151602881485894828440259195604313253842905231566803078625935967002376"
|
||||
# or
|
||||
contentIdHexBE =
|
||||
"b1c89984803cebd325303ba035f9c4ca0d0d91b2cbfef84d455e7a847ade1f08"
|
||||
|
||||
let
|
||||
contractStorageTrieProofKey = ContractStorageTrieProofKey(
|
||||
@ -108,8 +152,7 @@ suite "State ContentKey Encodings":
|
||||
contractStorageTrieProofKey: contractStorageTrieProofKey)
|
||||
|
||||
let encoded = encode(contentKey)
|
||||
check encoded.asSeq.toHex ==
|
||||
"03829bd824b016326a401d083b33d092293333a830c8a6030000000000000000000000000000000000000000000000000000000000d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
check encoded.asSeq.toHex == contentKeyHex
|
||||
let decoded = decode(encoded)
|
||||
check decoded.isSome()
|
||||
|
||||
@ -119,14 +162,26 @@ suite "State ContentKey Encodings":
|
||||
contentKeyDecoded.contractStorageTrieProofKey ==
|
||||
contentKey.contractStorageTrieProofKey
|
||||
|
||||
toContentId(contentKey).toHex() ==
|
||||
"ce5a3a6bc958561da0015d92f2f6b4f5a2cf6a4ae3f6a75c97f05e9e2a6f4387"
|
||||
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
||||
# In stint this does BE hex string
|
||||
toContentId(contentKey).toHex() == contentIdHexBE
|
||||
|
||||
test "ContractBytecode":
|
||||
# Input
|
||||
var codeHash: CodeHash
|
||||
codeHash.data = hexToByteArray[sizeof(CodeHash)](
|
||||
"0xd1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d")
|
||||
|
||||
# Output
|
||||
const
|
||||
contentKeyHex =
|
||||
"04829bd824b016326a401d083b33d092293333a830d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
contentId =
|
||||
"9243655320250466575533858917172702581481192615849913473767356296630272634800"
|
||||
# or
|
||||
contentIdHexBE =
|
||||
"146fb937afe42bcf11d25ad57d67734b9a7138677d59eeec3f402908f54dafb0"
|
||||
|
||||
let
|
||||
contractBytecodeKey = ContractBytecodeKey(
|
||||
address: address, codeHash: codeHash)
|
||||
@ -135,8 +190,7 @@ suite "State ContentKey Encodings":
|
||||
contractBytecodeKey: contractBytecodeKey)
|
||||
|
||||
let encoded = encode(contentKey)
|
||||
check encoded.asSeq.toHex ==
|
||||
"04829bd824b016326a401d083b33d092293333a830d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||
check encoded.asSeq.toHex == contentKeyHex
|
||||
let decoded = decode(encoded)
|
||||
check decoded.isSome()
|
||||
|
||||
@ -145,5 +199,6 @@ suite "State ContentKey Encodings":
|
||||
contentKeyDecoded.contentType == contentKey.contentType
|
||||
contentKeyDecoded.contractBytecodeKey == contentKey.contractBytecodeKey
|
||||
|
||||
toContentId(contentKey).toHex() ==
|
||||
"146fb937afe42bcf11d25ad57d67734b9a7138677d59eeec3f402908f54dafb0"
|
||||
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
||||
# In stint this does BE hex string
|
||||
toContentId(contentKey).toHex() == contentIdHexBE
|
||||
|
Loading…
x
Reference in New Issue
Block a user