mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-02-04 08:16:19 +00:00
Add accumulator content keys for history network (#1123)
This commit is contained in:
parent
49bdaa112e
commit
6325712aa6
@ -22,19 +22,39 @@ type
|
|||||||
blockHeader = 0x00
|
blockHeader = 0x00
|
||||||
blockBody = 0x01
|
blockBody = 0x01
|
||||||
receipts = 0x02
|
receipts = 0x02
|
||||||
|
epochAccumulator = 0x03
|
||||||
|
masterAccumulator = 0x04
|
||||||
|
|
||||||
ContentKeyType* = object
|
BlockKey* = object
|
||||||
chainId*: uint16
|
chainId*: uint16
|
||||||
blockHash*: BlockHash
|
blockHash*: BlockHash
|
||||||
|
|
||||||
|
EpochAccumulatorKey* = object
|
||||||
|
epochHash*: Digest
|
||||||
|
|
||||||
|
MasterAccumulatorKeyType* = enum
|
||||||
|
latest = 0x00 # An SSZ Union None
|
||||||
|
masterHash = 0x01
|
||||||
|
|
||||||
|
MasterAccumulatorKey* = object
|
||||||
|
case accumulaterKeyType*: MasterAccumulatorKeyType
|
||||||
|
of latest:
|
||||||
|
discard
|
||||||
|
of masterHash:
|
||||||
|
masterHashKey*: Digest
|
||||||
|
|
||||||
ContentKey* = object
|
ContentKey* = object
|
||||||
case contentType*: ContentType
|
case contentType*: ContentType
|
||||||
of blockHeader:
|
of blockHeader:
|
||||||
blockHeaderKey*: ContentKeyType
|
blockHeaderKey*: BlockKey
|
||||||
of blockBody:
|
of blockBody:
|
||||||
blockBodyKey*: ContentKeyType
|
blockBodyKey*: BlockKey
|
||||||
of receipts:
|
of receipts:
|
||||||
receiptsKey*: ContentKeyType
|
receiptsKey*: BlockKey
|
||||||
|
of epochAccumulator:
|
||||||
|
epochAccumulatorKey*: EpochAccumulatorKey
|
||||||
|
of masterAccumulator:
|
||||||
|
masterAccumulatorKey*: MasterAccumulatorKey
|
||||||
|
|
||||||
func encode*(contentKey: ContentKey): ByteList =
|
func encode*(contentKey: ContentKey): ByteList =
|
||||||
ByteList.init(SSZ.encode(contentKey))
|
ByteList.init(SSZ.encode(contentKey))
|
||||||
@ -56,16 +76,30 @@ func toContentId*(contentKey: ContentKey): ContentId =
|
|||||||
func `$`*(x: BlockHash): string =
|
func `$`*(x: BlockHash): string =
|
||||||
"0x" & x.data.toHex()
|
"0x" & x.data.toHex()
|
||||||
|
|
||||||
func `$`*(x: ContentKey): string =
|
func `$`*(x: BlockKey): string =
|
||||||
let key =
|
"blockHash: " & $x.blockHash & ", chainId: " & $x.chainId
|
||||||
case x.contentType:
|
|
||||||
of blockHeader:
|
|
||||||
x.blockHeaderKey
|
|
||||||
of blockBody:
|
|
||||||
x.blockBodyKey
|
|
||||||
of receipts:
|
|
||||||
x.receiptsKey
|
|
||||||
|
|
||||||
"(contentType: " & $x.contentType &
|
func `$`*(x: ContentKey): string =
|
||||||
", blockHash: " & $key.blockHash &
|
var res = "(type: " & $x.contentType & ", "
|
||||||
", chainId: " & $key.chainId & ")"
|
|
||||||
|
case x.contentType:
|
||||||
|
of blockHeader:
|
||||||
|
res.add($x.blockHeaderKey)
|
||||||
|
of blockBody:
|
||||||
|
res.add($x.blockBodyKey)
|
||||||
|
of receipts:
|
||||||
|
res.add($x.receiptsKey)
|
||||||
|
of epochAccumulator:
|
||||||
|
let key = x.epochAccumulatorKey
|
||||||
|
res.add("epochHash: " & $key.epochHash)
|
||||||
|
of masterAccumulator:
|
||||||
|
let key = x.masterAccumulatorKey
|
||||||
|
case key.accumulaterKeyType:
|
||||||
|
of latest:
|
||||||
|
res.add($key.accumulaterKeyType)
|
||||||
|
of masterHash:
|
||||||
|
res.add($key.accumulaterKeyType & ": " & $key.masterHashKey)
|
||||||
|
|
||||||
|
res.add(")")
|
||||||
|
|
||||||
|
res
|
||||||
|
@ -44,7 +44,7 @@ func encodeKey(k: ContentKey): (ByteList, ContentId) =
|
|||||||
func getEncodedKeyForContent(
|
func getEncodedKeyForContent(
|
||||||
cType: ContentType, chainId: uint16, hash: BlockHash):
|
cType: ContentType, chainId: uint16, hash: BlockHash):
|
||||||
(ByteList, ContentId) =
|
(ByteList, ContentId) =
|
||||||
let contentKeyType = ContentKeyType(chainId: chainId, blockHash: hash)
|
let contentKeyType = BlockKey(chainId: chainId, blockHash: hash)
|
||||||
|
|
||||||
let contentKey =
|
let contentKey =
|
||||||
case cType
|
case cType
|
||||||
@ -54,6 +54,10 @@ func getEncodedKeyForContent(
|
|||||||
ContentKey(contentType: cType, blockBodyKey: contentKeyType)
|
ContentKey(contentType: cType, blockBodyKey: contentKeyType)
|
||||||
of receipts:
|
of receipts:
|
||||||
ContentKey(contentType: cType, receiptsKey: contentKeyType)
|
ContentKey(contentType: cType, receiptsKey: contentKeyType)
|
||||||
|
of epochAccumulator:
|
||||||
|
raiseAssert("Not implemented")
|
||||||
|
of masterAccumulator:
|
||||||
|
raiseAssert("Not implemented")
|
||||||
|
|
||||||
return encodeKey(contentKey)
|
return encodeKey(contentKey)
|
||||||
|
|
||||||
@ -327,6 +331,10 @@ proc validateContent(content: openArray[byte], contentKey: ByteList): bool =
|
|||||||
# to deal with this?
|
# to deal with this?
|
||||||
of receipts:
|
of receipts:
|
||||||
true
|
true
|
||||||
|
of epochAccumulator:
|
||||||
|
true
|
||||||
|
of masterAccumulator:
|
||||||
|
true
|
||||||
|
|
||||||
proc new*(
|
proc new*(
|
||||||
T: type HistoryNetwork,
|
T: type HistoryNetwork,
|
||||||
|
@ -82,7 +82,7 @@ func readBlockData(
|
|||||||
$blockData.number & ": " & e.msg)
|
$blockData.number & ": " & e.msg)
|
||||||
|
|
||||||
let contentKeyType =
|
let contentKeyType =
|
||||||
ContentKeyType(chainId: 1'u16, blockHash: blockHash)
|
BlockKey(chainId: 1'u16, blockHash: blockHash)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# If wanted the hash for the corresponding header can be verified
|
# If wanted the hash for the corresponding header can be verified
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# Nimbus
|
# Nimbus
|
||||||
# Copyright (c) 2021 Status Research & Development GmbH
|
# Copyright (c) 2021-2022 Status Research & Development GmbH
|
||||||
# Licensed and distributed under either of
|
# Licensed and distributed under either of
|
||||||
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
# * 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).
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
||||||
@ -12,7 +12,8 @@ import
|
|||||||
../network/history/history_content
|
../network/history/history_content
|
||||||
|
|
||||||
# According to test vectors:
|
# According to test vectors:
|
||||||
# TODO: Add link once test vectors are merged
|
# https://github.com/ethereum/portal-network-specs/blob/master/content-keys-test-vectors.md#history-network-keys
|
||||||
|
|
||||||
suite "History ContentKey Encodings":
|
suite "History ContentKey Encodings":
|
||||||
test "BlockHeader":
|
test "BlockHeader":
|
||||||
# Input
|
# Input
|
||||||
@ -32,7 +33,7 @@ suite "History ContentKey Encodings":
|
|||||||
|
|
||||||
let contentKey = ContentKey(
|
let contentKey = ContentKey(
|
||||||
contentType: blockHeader,
|
contentType: blockHeader,
|
||||||
blockHeaderKey: ContentKeyType(chainId: 15'u16, blockHash: blockHash))
|
blockHeaderKey: BlockKey(chainId: 15'u16, blockHash: blockHash))
|
||||||
|
|
||||||
let encoded = encode(contentKey)
|
let encoded = encode(contentKey)
|
||||||
check encoded.asSeq.toHex == contentKeyHex
|
check encoded.asSeq.toHex == contentKeyHex
|
||||||
@ -66,7 +67,7 @@ suite "History ContentKey Encodings":
|
|||||||
|
|
||||||
let contentKey = ContentKey(
|
let contentKey = ContentKey(
|
||||||
contentType: blockBody,
|
contentType: blockBody,
|
||||||
blockBodyKey: ContentKeyType(chainId: 20'u16, blockHash: blockHash))
|
blockBodyKey: BlockKey(chainId: 20'u16, blockHash: blockHash))
|
||||||
|
|
||||||
let encoded = encode(contentKey)
|
let encoded = encode(contentKey)
|
||||||
check encoded.asSeq.toHex == contentKeyHex
|
check encoded.asSeq.toHex == contentKeyHex
|
||||||
@ -99,8 +100,7 @@ suite "History ContentKey Encodings":
|
|||||||
|
|
||||||
let contentKey = ContentKey(
|
let contentKey = ContentKey(
|
||||||
contentType: receipts,
|
contentType: receipts,
|
||||||
receiptsKey: ContentKeyType(chainId: 4'u16, blockHash: blockHash))
|
receiptsKey: BlockKey(chainId: 4'u16, blockHash: blockHash))
|
||||||
|
|
||||||
|
|
||||||
let encoded = encode(contentKey)
|
let encoded = encode(contentKey)
|
||||||
check encoded.asSeq.toHex == contentKeyHex
|
check encoded.asSeq.toHex == contentKeyHex
|
||||||
@ -115,3 +115,104 @@ suite "History ContentKey Encodings":
|
|||||||
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
toContentId(contentKey) == parse(contentId, Stuint[256], 10)
|
||||||
# In stint this does BE hex string
|
# In stint this does BE hex string
|
||||||
toContentId(contentKey).toHex() == contentIdHexBE
|
toContentId(contentKey).toHex() == contentIdHexBE
|
||||||
|
|
||||||
|
test "Epoch Accumulator":
|
||||||
|
var epochHash: Digest
|
||||||
|
epochHash.data = hexToByteArray[sizeof(Digest)](
|
||||||
|
"0xe242814b90ed3950e13aac7e56ce116540c71b41d1516605aada26c6c07cc491")
|
||||||
|
|
||||||
|
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":
|
||||||
|
var accumulatorHash: Digest
|
||||||
|
accumulatorHash.data = hexToByteArray[sizeof(Digest)](
|
||||||
|
"0x88cce8439ebc0c1d007177ffb6831c15c07b4361984cc52235b6fd728434f0c7")
|
||||||
|
|
||||||
|
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":
|
||||||
|
var accumulatorHash: Digest
|
||||||
|
accumulatorHash.data = hexToByteArray[sizeof(Digest)](
|
||||||
|
"0x88cce8439ebc0c1d007177ffb6831c15c07b4361984cc52235b6fd728434f0c7")
|
||||||
|
|
||||||
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user