mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-02-02 15:24:01 +00:00
Remove ChaindId from BlockKey and update test vectors (#1242)
This commit is contained in:
parent
b84c59eef2
commit
1bd4911e78
@ -83,7 +83,7 @@ func readBlockData*(
|
|||||||
$blockData.number & ": " & e.msg)
|
$blockData.number & ": " & e.msg)
|
||||||
|
|
||||||
let contentKeyType =
|
let contentKeyType =
|
||||||
BlockKey(chainId: 1'u16, blockHash: blockHash)
|
BlockKey(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
|
||||||
|
@ -18,12 +18,13 @@ import
|
|||||||
export ssz_serialization, common_types, hash
|
export ssz_serialization, common_types, hash
|
||||||
|
|
||||||
## Types and calls for history network content keys
|
## Types and calls for history network content keys
|
||||||
|
|
||||||
const
|
const
|
||||||
# Maximum content key size comes from:
|
# Maximum content key size:
|
||||||
# 34 bytes for ssz serialized BlockKey
|
# - 32 bytes for SSZ serialized `BlockKey`
|
||||||
# 1 byte for contentType
|
# - 1 byte for `ContentType`
|
||||||
# TODO it would be nice to caluclate it somehow from the object definition (macro?)
|
# TODO: calculate it somehow from the object definition (macro?)
|
||||||
maxContentKeySize* = 35
|
maxContentKeySize* = 33
|
||||||
|
|
||||||
type
|
type
|
||||||
ContentType* = enum
|
ContentType* = enum
|
||||||
@ -34,7 +35,6 @@ type
|
|||||||
masterAccumulator = 0x04
|
masterAccumulator = 0x04
|
||||||
|
|
||||||
BlockKey* = object
|
BlockKey* = object
|
||||||
chainId*: uint16
|
|
||||||
blockHash*: BlockHash
|
blockHash*: BlockHash
|
||||||
|
|
||||||
EpochAccumulatorKey* = object
|
EpochAccumulatorKey* = object
|
||||||
@ -85,7 +85,7 @@ func `$`*(x: BlockHash): string =
|
|||||||
"0x" & x.data.toHex()
|
"0x" & x.data.toHex()
|
||||||
|
|
||||||
func `$`*(x: BlockKey): string =
|
func `$`*(x: BlockKey): string =
|
||||||
"blockHash: " & $x.blockHash & ", chainId: " & $x.chainId
|
"blockHash: " & $x.blockHash
|
||||||
|
|
||||||
func `$`*(x: ContentKey): string =
|
func `$`*(x: ContentKey): string =
|
||||||
var res = "(type: " & $x.contentType & ", "
|
var res = "(type: " & $x.contentType & ", "
|
||||||
|
@ -52,9 +52,9 @@ func encodeKey(k: ContentKey): (ByteList, ContentId) =
|
|||||||
return (keyEncoded, toContentId(keyEncoded))
|
return (keyEncoded, toContentId(keyEncoded))
|
||||||
|
|
||||||
func getEncodedKeyForContent(
|
func getEncodedKeyForContent(
|
||||||
cType: ContentType, chainId: uint16, hash: BlockHash):
|
cType: ContentType, hash: BlockHash):
|
||||||
(ByteList, ContentId) =
|
(ByteList, ContentId) =
|
||||||
let contentKeyType = BlockKey(chainId: chainId, blockHash: hash)
|
let contentKeyType = BlockKey(blockHash: hash)
|
||||||
|
|
||||||
let contentKey =
|
let contentKey =
|
||||||
case cType
|
case cType
|
||||||
@ -296,10 +296,10 @@ const requestRetries = 4
|
|||||||
# however that response is not yet validated at that moment.
|
# however that response is not yet validated at that moment.
|
||||||
|
|
||||||
proc getBlockHeader*(
|
proc getBlockHeader*(
|
||||||
n: HistoryNetwork, chainId: uint16, hash: BlockHash):
|
n: HistoryNetwork, hash: BlockHash):
|
||||||
Future[Option[BlockHeader]] {.async.} =
|
Future[Option[BlockHeader]] {.async.} =
|
||||||
let (keyEncoded, contentId) =
|
let (keyEncoded, contentId) =
|
||||||
getEncodedKeyForContent(blockHeader, chainId, hash)
|
getEncodedKeyForContent(blockHeader, hash)
|
||||||
|
|
||||||
let headerFromDb = n.getContentFromDb(BlockHeader, contentId)
|
let headerFromDb = n.getContentFromDb(BlockHeader, contentId)
|
||||||
if headerFromDb.isSome():
|
if headerFromDb.isSome():
|
||||||
@ -335,7 +335,7 @@ proc getBlockHeader*(
|
|||||||
return none(BlockHeader)
|
return none(BlockHeader)
|
||||||
|
|
||||||
proc getBlockBody*(
|
proc getBlockBody*(
|
||||||
n: HistoryNetwork, chainId: uint16, hash: BlockHash, header: BlockHeader):
|
n: HistoryNetwork, hash: BlockHash, header: BlockHeader):
|
||||||
Future[Option[BlockBody]] {.async.} =
|
Future[Option[BlockBody]] {.async.} =
|
||||||
|
|
||||||
# Got header with empty body, no need to make any db calls or network requests
|
# Got header with empty body, no need to make any db calls or network requests
|
||||||
@ -343,7 +343,7 @@ proc getBlockBody*(
|
|||||||
return some(BlockBody(transactions: @[], uncles: @[]))
|
return some(BlockBody(transactions: @[], uncles: @[]))
|
||||||
|
|
||||||
let
|
let
|
||||||
(keyEncoded, contentId) = getEncodedKeyForContent(blockBody, chainId, hash)
|
(keyEncoded, contentId) = getEncodedKeyForContent(blockBody, hash)
|
||||||
bodyFromDb = n.getContentFromDb(BlockBody, contentId)
|
bodyFromDb = n.getContentFromDb(BlockBody, contentId)
|
||||||
|
|
||||||
if bodyFromDb.isSome():
|
if bodyFromDb.isSome():
|
||||||
@ -381,11 +381,11 @@ proc getBlockBody*(
|
|||||||
return none(BlockBody)
|
return none(BlockBody)
|
||||||
|
|
||||||
proc getBlock*(
|
proc getBlock*(
|
||||||
n: HistoryNetwork, chainId: uint16, hash: BlockHash):
|
n: HistoryNetwork, hash: BlockHash):
|
||||||
Future[Option[Block]] {.async.} =
|
Future[Option[Block]] {.async.} =
|
||||||
debug "Trying to retrieve block with hash", hash
|
debug "Trying to retrieve block with hash", hash
|
||||||
|
|
||||||
let headerOpt = await n.getBlockHeader(chainId, hash)
|
let headerOpt = await n.getBlockHeader(hash)
|
||||||
if headerOpt.isNone():
|
if headerOpt.isNone():
|
||||||
warn "Failed to get header when getting block with hash", hash
|
warn "Failed to get header when getting block with hash", hash
|
||||||
# Cannot validate block without header.
|
# Cannot validate block without header.
|
||||||
@ -393,7 +393,7 @@ proc getBlock*(
|
|||||||
|
|
||||||
let header = headerOpt.unsafeGet()
|
let header = headerOpt.unsafeGet()
|
||||||
|
|
||||||
let bodyOpt = await n.getBlockBody(chainId, hash, header)
|
let bodyOpt = await n.getBlockBody(hash, header)
|
||||||
|
|
||||||
if bodyOpt.isNone():
|
if bodyOpt.isNone():
|
||||||
warn "Failed to get body when gettin block with hash", hash
|
warn "Failed to get body when gettin block with hash", hash
|
||||||
@ -405,14 +405,13 @@ proc getBlock*(
|
|||||||
|
|
||||||
proc getReceipts*(
|
proc getReceipts*(
|
||||||
n: HistoryNetwork,
|
n: HistoryNetwork,
|
||||||
chainId: uint16,
|
|
||||||
hash: BlockHash,
|
hash: BlockHash,
|
||||||
header: BlockHeader): Future[Option[seq[Receipt]]] {.async.} =
|
header: BlockHeader): Future[Option[seq[Receipt]]] {.async.} =
|
||||||
if header.receiptRoot == EMPTY_ROOT_HASH:
|
if header.receiptRoot == EMPTY_ROOT_HASH:
|
||||||
# Short path for empty receipts indicated by receipts root
|
# Short path for empty receipts indicated by receipts root
|
||||||
return some(newSeq[Receipt]())
|
return some(newSeq[Receipt]())
|
||||||
|
|
||||||
let (keyEncoded, contentId) = getEncodedKeyForContent(receipts, chainId, hash)
|
let (keyEncoded, contentId) = getEncodedKeyForContent(receipts, hash)
|
||||||
|
|
||||||
let receiptsFromDb = n.getContentFromDb(seq[Receipt], contentId)
|
let receiptsFromDb = n.getContentFromDb(seq[Receipt], contentId)
|
||||||
|
|
||||||
@ -503,7 +502,7 @@ proc getEpochAccumulator(
|
|||||||
return none(EpochAccumulator)
|
return none(EpochAccumulator)
|
||||||
|
|
||||||
proc getBlock*(
|
proc getBlock*(
|
||||||
n: HistoryNetwork, chainId: uint16, bn: UInt256):
|
n: HistoryNetwork, bn: UInt256):
|
||||||
Future[Result[Option[Block], string]] {.async.} =
|
Future[Result[Option[Block], string]] {.async.} =
|
||||||
|
|
||||||
# TODO for now checking accumulator only in db, we could also ask our
|
# TODO for now checking accumulator only in db, we could also ask our
|
||||||
@ -520,7 +519,7 @@ proc getBlock*(
|
|||||||
case hashResponse.kind
|
case hashResponse.kind
|
||||||
of BHash:
|
of BHash:
|
||||||
# we got header hash in current epoch accumulator, try to retrieve it from network
|
# we got header hash in current epoch accumulator, try to retrieve it from network
|
||||||
let blockResponse = await n.getBlock(chainId, hashResponse.blockHash)
|
let blockResponse = await n.getBlock(hashResponse.blockHash)
|
||||||
return ok(blockResponse)
|
return ok(blockResponse)
|
||||||
of HEpoch:
|
of HEpoch:
|
||||||
let digest = Digest(data: hashResponse.epochHash)
|
let digest = Digest(data: hashResponse.epochHash)
|
||||||
@ -534,7 +533,7 @@ proc getBlock*(
|
|||||||
epoch = epochOpt.unsafeGet()
|
epoch = epochOpt.unsafeGet()
|
||||||
blockHash = epoch[hashResponse.blockRelativeIndex].blockHash
|
blockHash = epoch[hashResponse.blockRelativeIndex].blockHash
|
||||||
|
|
||||||
let maybeBlock = await n.getBlock(chainId, blockHash)
|
let maybeBlock = await n.getBlock(blockHash)
|
||||||
|
|
||||||
return ok(maybeBlock)
|
return ok(maybeBlock)
|
||||||
of UnknownBlockNumber:
|
of UnknownBlockNumber:
|
||||||
@ -657,8 +656,7 @@ proc validateContent(
|
|||||||
else:
|
else:
|
||||||
return true
|
return true
|
||||||
of blockBody:
|
of blockBody:
|
||||||
let headerOpt = await n.getBlockHeader(
|
let headerOpt = await n.getBlockHeader(key.blockBodyKey.blockHash)
|
||||||
key.blockBodyKey.chainId, key.blockBodyKey.blockHash)
|
|
||||||
|
|
||||||
if headerOpt.isNone():
|
if headerOpt.isNone():
|
||||||
warn "Cannot find the header, no way to validate the block body"
|
warn "Cannot find the header, no way to validate the block body"
|
||||||
@ -680,8 +678,7 @@ proc validateContent(
|
|||||||
else:
|
else:
|
||||||
return true
|
return true
|
||||||
of receipts:
|
of receipts:
|
||||||
let headerOpt = await n.getBlockHeader(
|
let headerOpt = await n.getBlockHeader(key.receiptsKey.blockHash)
|
||||||
key.receiptsKey.chainId, key.receiptsKey.blockHash)
|
|
||||||
|
|
||||||
if headerOpt.isNone():
|
if headerOpt.isNone():
|
||||||
warn "Cannot find the header, no way to validate the receipts"
|
warn "Cannot find the header, no way to validate the receipts"
|
||||||
|
@ -203,7 +203,7 @@ proc installEthApiHandlers*(
|
|||||||
## Returns BlockObject or nil when no block was found.
|
## Returns BlockObject or nil when no block was found.
|
||||||
let
|
let
|
||||||
blockHash = data.toHash()
|
blockHash = data.toHash()
|
||||||
blockRes = await historyNetwork.getBlock(1'u16, blockHash)
|
blockRes = await historyNetwork.getBlock(blockHash)
|
||||||
|
|
||||||
if blockRes.isNone():
|
if blockRes.isNone():
|
||||||
return none(BlockObject)
|
return none(BlockObject)
|
||||||
@ -222,7 +222,7 @@ proc installEthApiHandlers*(
|
|||||||
|
|
||||||
let
|
let
|
||||||
blockNumber = fromHex(UInt256, quantityTag)
|
blockNumber = fromHex(UInt256, quantityTag)
|
||||||
blockResult = await historyNetwork.getBlock(1'u16, blockNumber)
|
blockResult = await historyNetwork.getBlock(blockNumber)
|
||||||
|
|
||||||
if blockResult.isOk():
|
if blockResult.isOk():
|
||||||
let maybeBlock = blockResult.get()
|
let maybeBlock = blockResult.get()
|
||||||
@ -244,7 +244,7 @@ proc installEthApiHandlers*(
|
|||||||
## Returns integer of the number of transactions in this block.
|
## Returns integer of the number of transactions in this block.
|
||||||
let
|
let
|
||||||
blockHash = data.toHash()
|
blockHash = data.toHash()
|
||||||
blockRes = await historyNetwork.getBlock(1'u16, blockHash)
|
blockRes = await historyNetwork.getBlock(blockHash)
|
||||||
|
|
||||||
if blockRes.isNone():
|
if blockRes.isNone():
|
||||||
raise newException(ValueError, "Could not find block with requested hash")
|
raise newException(ValueError, "Could not find block with requested hash")
|
||||||
@ -273,7 +273,7 @@ proc installEthApiHandlers*(
|
|||||||
else:
|
else:
|
||||||
let hash = filterOptions.blockHash.unsafeGet()
|
let hash = filterOptions.blockHash.unsafeGet()
|
||||||
|
|
||||||
let headerOpt = await historyNetwork.getBlockHeader(1'u16, hash)
|
let headerOpt = await historyNetwork.getBlockHeader(hash)
|
||||||
if headerOpt.isNone():
|
if headerOpt.isNone():
|
||||||
raise newException(ValueError,
|
raise newException(ValueError,
|
||||||
"Could not find header with requested hash")
|
"Could not find header with requested hash")
|
||||||
@ -285,8 +285,8 @@ proc installEthApiHandlers*(
|
|||||||
# are no assumptions about usage of concurrent queries on portal
|
# are no assumptions about usage of concurrent queries on portal
|
||||||
# wire protocol level
|
# wire protocol level
|
||||||
let
|
let
|
||||||
bodyOpt = await historyNetwork.getBlockBody(1'u16, hash, header)
|
bodyOpt = await historyNetwork.getBlockBody(hash, header)
|
||||||
receiptsOpt = await historyNetwork.getReceipts(1'u16, hash, header)
|
receiptsOpt = await historyNetwork.getReceipts(hash, header)
|
||||||
|
|
||||||
if bodyOpt.isSome() and receiptsOpt.isSome():
|
if bodyOpt.isSome() and receiptsOpt.isSome():
|
||||||
let
|
let
|
||||||
|
@ -23,16 +23,16 @@ suite "History ContentKey Encodings":
|
|||||||
# Output
|
# Output
|
||||||
const
|
const
|
||||||
contentKeyHex =
|
contentKeyHex =
|
||||||
"000f00d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
"00d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||||
contentId =
|
contentId =
|
||||||
"15025167517633317571792618561170587584740338038067807801482118109695980329625"
|
"28281392725701906550238743427348001871342819822834514257505083923073246729726"
|
||||||
# or
|
# or
|
||||||
contentIdHexBE =
|
contentIdHexBE =
|
||||||
"2137f185b713a60dd1190e650d01227b4f94ecddc9c95478e2c591c40557da99"
|
"3e86b3767b57402ea72e369ae0496ce47cc15be685bec3b4726b9f316e3895fe"
|
||||||
|
|
||||||
let contentKey = ContentKey(
|
let contentKey = ContentKey(
|
||||||
contentType: blockHeader,
|
contentType: blockHeader,
|
||||||
blockHeaderKey: BlockKey(chainId: 15'u16, blockHash: blockHash))
|
blockHeaderKey: BlockKey(blockHash: blockHash))
|
||||||
|
|
||||||
let encoded = encode(contentKey)
|
let encoded = encode(contentKey)
|
||||||
check encoded.asSeq.toHex == contentKeyHex
|
check encoded.asSeq.toHex == contentKeyHex
|
||||||
@ -56,16 +56,16 @@ suite "History ContentKey Encodings":
|
|||||||
# Output
|
# Output
|
||||||
const
|
const
|
||||||
contentKeyHex =
|
contentKeyHex =
|
||||||
"011400d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
"01d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||||
contentId =
|
contentId =
|
||||||
"12834862124958403129911294156243112356210437741210740000860318140844473844426"
|
"106696502175825986237944249828698290888857178633945273402044845898673345165419"
|
||||||
# or
|
# or
|
||||||
contentIdHexBE =
|
contentIdHexBE =
|
||||||
"1c6046475f0772132774ab549173ca8487bea031ce539cad8e990c08df5802ca"
|
"ebe414854629d60c58ddd5bf60fd72e41760a5f7a463fdcb169f13ee4a26786b"
|
||||||
|
|
||||||
let contentKey = ContentKey(
|
let contentKey = ContentKey(
|
||||||
contentType: blockBody,
|
contentType: blockBody,
|
||||||
blockBodyKey: BlockKey(chainId: 20'u16, blockHash: blockHash))
|
blockBodyKey: BlockKey(blockHash: blockHash))
|
||||||
|
|
||||||
let encoded = encode(contentKey)
|
let encoded = encode(contentKey)
|
||||||
check encoded.asSeq.toHex == contentKeyHex
|
check encoded.asSeq.toHex == contentKeyHex
|
||||||
@ -89,16 +89,16 @@ suite "History ContentKey Encodings":
|
|||||||
# Output
|
# Output
|
||||||
const
|
const
|
||||||
contentKeyHex =
|
contentKeyHex =
|
||||||
"020400d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
"02d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
|
||||||
contentId =
|
contentId =
|
||||||
"76995449220721979583200368506411933662679656077191192504502358532083948020658"
|
"76230538398907151249589044529104962263309222250374376758768131420767496438948"
|
||||||
# or
|
# or
|
||||||
contentIdHexBE =
|
contentIdHexBE =
|
||||||
"aa39e1423e92f5a667ace5b79c2c98adbfd79c055d891d0b9c49c40f816563b2"
|
"a888f4aafe9109d495ac4d4774a6277c1ada42035e3da5e10a04cc93247c04a4"
|
||||||
|
|
||||||
let contentKey = ContentKey(
|
let contentKey = ContentKey(
|
||||||
contentType: receipts,
|
contentType: receipts,
|
||||||
receiptsKey: BlockKey(chainId: 4'u16, blockHash: blockHash))
|
receiptsKey: BlockKey(blockHash: blockHash))
|
||||||
|
|
||||||
let encoded = encode(contentKey)
|
let encoded = encode(contentKey)
|
||||||
check encoded.asSeq.toHex == contentKeyHex
|
check encoded.asSeq.toHex == contentKeyHex
|
||||||
|
@ -63,7 +63,7 @@ proc headersToContentInfo(headers: seq[BlockHeader]): seq[ContentInfo] =
|
|||||||
for h in headers:
|
for h in headers:
|
||||||
let
|
let
|
||||||
headerHash = h.blockHash()
|
headerHash = h.blockHash()
|
||||||
bk = BlockKey(chainId: 1'u16, blockHash: headerHash)
|
bk = BlockKey(blockHash: headerHash)
|
||||||
ck = encode(ContentKey(contentType: blockHeader, blockHeaderKey: bk))
|
ck = encode(ContentKey(contentType: blockHeader, blockHeaderKey: bk))
|
||||||
headerEncoded = rlp.encode(h)
|
headerEncoded = rlp.encode(h)
|
||||||
ci = ContentInfo(contentKey: ck, content: headerEncoded)
|
ci = ContentInfo(contentKey: ck, content: headerEncoded)
|
||||||
@ -96,7 +96,7 @@ procSuite "History Content Network":
|
|||||||
for h in headers:
|
for h in headers:
|
||||||
let
|
let
|
||||||
headerHash = h.blockHash()
|
headerHash = h.blockHash()
|
||||||
blockKey = BlockKey(chainId: 1'u16, blockHash: headerHash)
|
blockKey = BlockKey(blockHash: headerHash)
|
||||||
contentKey = ContentKey(
|
contentKey = ContentKey(
|
||||||
contentType: blockHeader, blockHeaderKey: blockKey)
|
contentType: blockHeader, blockHeaderKey: blockKey)
|
||||||
contentId = toContentId(contentKey)
|
contentId = toContentId(contentKey)
|
||||||
@ -116,7 +116,7 @@ procSuite "History Content Network":
|
|||||||
(await historyNode2.portalProtocol().ping(historyNode1.localNode())).isOk()
|
(await historyNode2.portalProtocol().ping(historyNode1.localNode())).isOk()
|
||||||
|
|
||||||
for i in 0..lastBlockNumber:
|
for i in 0..lastBlockNumber:
|
||||||
let blockResponse = await historyNode1.historyNetwork.getBlock(1'u16, u256(i))
|
let blockResponse = await historyNode1.historyNetwork.getBlock(u256(i))
|
||||||
|
|
||||||
check blockResponse.isOk()
|
check blockResponse.isOk()
|
||||||
|
|
||||||
|
@ -294,7 +294,7 @@ proc writeBlocksToDb(config: ExporterConf, client: RpcClient) =
|
|||||||
let
|
let
|
||||||
blck = downloadBlock(i, client)
|
blck = downloadBlock(i, client)
|
||||||
blockHash = blck.header.blockHash()
|
blockHash = blck.header.blockHash()
|
||||||
contentKeyType = BlockKey(chainId: 1, blockHash: blockHash)
|
contentKeyType = BlockKey(blockHash: blockHash)
|
||||||
headerKey = encode(ContentKey(
|
headerKey = encode(ContentKey(
|
||||||
contentType: blockHeader, blockHeaderKey: contentKeyType))
|
contentType: blockHeader, blockHeaderKey: contentKeyType))
|
||||||
bodyKey = encode(ContentKey(
|
bodyKey = encode(ContentKey(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user