Remove ChaindId from BlockKey and update test vectors (#1242)

This commit is contained in:
Kim De Mey 2022-09-28 09:09:38 +02:00 committed by GitHub
parent b84c59eef2
commit 1bd4911e78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 48 deletions

View File

@ -83,7 +83,7 @@ func readBlockData*(
$blockData.number & ": " & e.msg)
let contentKeyType =
BlockKey(chainId: 1'u16, blockHash: blockHash)
BlockKey(blockHash: blockHash)
try:
# If wanted the hash for the corresponding header can be verified

View File

@ -18,12 +18,13 @@ import
export ssz_serialization, common_types, hash
## Types and calls for history network content keys
const
# Maximum content key size comes from:
# 34 bytes for ssz serialized BlockKey
# 1 byte for contentType
# TODO it would be nice to caluclate it somehow from the object definition (macro?)
maxContentKeySize* = 35
# Maximum content key size:
# - 32 bytes for SSZ serialized `BlockKey`
# - 1 byte for `ContentType`
# TODO: calculate it somehow from the object definition (macro?)
maxContentKeySize* = 33
type
ContentType* = enum
@ -34,7 +35,6 @@ type
masterAccumulator = 0x04
BlockKey* = object
chainId*: uint16
blockHash*: BlockHash
EpochAccumulatorKey* = object
@ -85,7 +85,7 @@ func `$`*(x: BlockHash): string =
"0x" & x.data.toHex()
func `$`*(x: BlockKey): string =
"blockHash: " & $x.blockHash & ", chainId: " & $x.chainId
"blockHash: " & $x.blockHash
func `$`*(x: ContentKey): string =
var res = "(type: " & $x.contentType & ", "

View File

@ -52,9 +52,9 @@ func encodeKey(k: ContentKey): (ByteList, ContentId) =
return (keyEncoded, toContentId(keyEncoded))
func getEncodedKeyForContent(
cType: ContentType, chainId: uint16, hash: BlockHash):
cType: ContentType, hash: BlockHash):
(ByteList, ContentId) =
let contentKeyType = BlockKey(chainId: chainId, blockHash: hash)
let contentKeyType = BlockKey(blockHash: hash)
let contentKey =
case cType
@ -296,10 +296,10 @@ const requestRetries = 4
# however that response is not yet validated at that moment.
proc getBlockHeader*(
n: HistoryNetwork, chainId: uint16, hash: BlockHash):
n: HistoryNetwork, hash: BlockHash):
Future[Option[BlockHeader]] {.async.} =
let (keyEncoded, contentId) =
getEncodedKeyForContent(blockHeader, chainId, hash)
getEncodedKeyForContent(blockHeader, hash)
let headerFromDb = n.getContentFromDb(BlockHeader, contentId)
if headerFromDb.isSome():
@ -335,7 +335,7 @@ proc getBlockHeader*(
return none(BlockHeader)
proc getBlockBody*(
n: HistoryNetwork, chainId: uint16, hash: BlockHash, header: BlockHeader):
n: HistoryNetwork, hash: BlockHash, header: BlockHeader):
Future[Option[BlockBody]] {.async.} =
# 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: @[]))
let
(keyEncoded, contentId) = getEncodedKeyForContent(blockBody, chainId, hash)
(keyEncoded, contentId) = getEncodedKeyForContent(blockBody, hash)
bodyFromDb = n.getContentFromDb(BlockBody, contentId)
if bodyFromDb.isSome():
@ -381,11 +381,11 @@ proc getBlockBody*(
return none(BlockBody)
proc getBlock*(
n: HistoryNetwork, chainId: uint16, hash: BlockHash):
n: HistoryNetwork, hash: BlockHash):
Future[Option[Block]] {.async.} =
debug "Trying to retrieve block with hash", hash
let headerOpt = await n.getBlockHeader(chainId, hash)
let headerOpt = await n.getBlockHeader(hash)
if headerOpt.isNone():
warn "Failed to get header when getting block with hash", hash
# Cannot validate block without header.
@ -393,7 +393,7 @@ proc getBlock*(
let header = headerOpt.unsafeGet()
let bodyOpt = await n.getBlockBody(chainId, hash, header)
let bodyOpt = await n.getBlockBody(hash, header)
if bodyOpt.isNone():
warn "Failed to get body when gettin block with hash", hash
@ -405,14 +405,13 @@ proc getBlock*(
proc getReceipts*(
n: HistoryNetwork,
chainId: uint16,
hash: BlockHash,
header: BlockHeader): Future[Option[seq[Receipt]]] {.async.} =
if header.receiptRoot == EMPTY_ROOT_HASH:
# Short path for empty receipts indicated by receipts root
return some(newSeq[Receipt]())
let (keyEncoded, contentId) = getEncodedKeyForContent(receipts, chainId, hash)
let (keyEncoded, contentId) = getEncodedKeyForContent(receipts, hash)
let receiptsFromDb = n.getContentFromDb(seq[Receipt], contentId)
@ -503,7 +502,7 @@ proc getEpochAccumulator(
return none(EpochAccumulator)
proc getBlock*(
n: HistoryNetwork, chainId: uint16, bn: UInt256):
n: HistoryNetwork, bn: UInt256):
Future[Result[Option[Block], string]] {.async.} =
# TODO for now checking accumulator only in db, we could also ask our
@ -520,7 +519,7 @@ proc getBlock*(
case hashResponse.kind
of BHash:
# 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)
of HEpoch:
let digest = Digest(data: hashResponse.epochHash)
@ -534,7 +533,7 @@ proc getBlock*(
epoch = epochOpt.unsafeGet()
blockHash = epoch[hashResponse.blockRelativeIndex].blockHash
let maybeBlock = await n.getBlock(chainId, blockHash)
let maybeBlock = await n.getBlock(blockHash)
return ok(maybeBlock)
of UnknownBlockNumber:
@ -657,8 +656,7 @@ proc validateContent(
else:
return true
of blockBody:
let headerOpt = await n.getBlockHeader(
key.blockBodyKey.chainId, key.blockBodyKey.blockHash)
let headerOpt = await n.getBlockHeader(key.blockBodyKey.blockHash)
if headerOpt.isNone():
warn "Cannot find the header, no way to validate the block body"
@ -680,8 +678,7 @@ proc validateContent(
else:
return true
of receipts:
let headerOpt = await n.getBlockHeader(
key.receiptsKey.chainId, key.receiptsKey.blockHash)
let headerOpt = await n.getBlockHeader(key.receiptsKey.blockHash)
if headerOpt.isNone():
warn "Cannot find the header, no way to validate the receipts"

View File

@ -203,7 +203,7 @@ proc installEthApiHandlers*(
## Returns BlockObject or nil when no block was found.
let
blockHash = data.toHash()
blockRes = await historyNetwork.getBlock(1'u16, blockHash)
blockRes = await historyNetwork.getBlock(blockHash)
if blockRes.isNone():
return none(BlockObject)
@ -222,7 +222,7 @@ proc installEthApiHandlers*(
let
blockNumber = fromHex(UInt256, quantityTag)
blockResult = await historyNetwork.getBlock(1'u16, blockNumber)
blockResult = await historyNetwork.getBlock(blockNumber)
if blockResult.isOk():
let maybeBlock = blockResult.get()
@ -244,7 +244,7 @@ proc installEthApiHandlers*(
## Returns integer of the number of transactions in this block.
let
blockHash = data.toHash()
blockRes = await historyNetwork.getBlock(1'u16, blockHash)
blockRes = await historyNetwork.getBlock(blockHash)
if blockRes.isNone():
raise newException(ValueError, "Could not find block with requested hash")
@ -273,7 +273,7 @@ proc installEthApiHandlers*(
else:
let hash = filterOptions.blockHash.unsafeGet()
let headerOpt = await historyNetwork.getBlockHeader(1'u16, hash)
let headerOpt = await historyNetwork.getBlockHeader(hash)
if headerOpt.isNone():
raise newException(ValueError,
"Could not find header with requested hash")
@ -285,8 +285,8 @@ proc installEthApiHandlers*(
# are no assumptions about usage of concurrent queries on portal
# wire protocol level
let
bodyOpt = await historyNetwork.getBlockBody(1'u16, hash, header)
receiptsOpt = await historyNetwork.getReceipts(1'u16, hash, header)
bodyOpt = await historyNetwork.getBlockBody(hash, header)
receiptsOpt = await historyNetwork.getReceipts(hash, header)
if bodyOpt.isSome() and receiptsOpt.isSome():
let

View File

@ -23,16 +23,16 @@ suite "History ContentKey Encodings":
# Output
const
contentKeyHex =
"000f00d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
"00d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
contentId =
"15025167517633317571792618561170587584740338038067807801482118109695980329625"
"28281392725701906550238743427348001871342819822834514257505083923073246729726"
# or
contentIdHexBE =
"2137f185b713a60dd1190e650d01227b4f94ecddc9c95478e2c591c40557da99"
"3e86b3767b57402ea72e369ae0496ce47cc15be685bec3b4726b9f316e3895fe"
let contentKey = ContentKey(
contentType: blockHeader,
blockHeaderKey: BlockKey(chainId: 15'u16, blockHash: blockHash))
blockHeaderKey: BlockKey(blockHash: blockHash))
let encoded = encode(contentKey)
check encoded.asSeq.toHex == contentKeyHex
@ -56,16 +56,16 @@ suite "History ContentKey Encodings":
# Output
const
contentKeyHex =
"011400d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
"01d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
contentId =
"12834862124958403129911294156243112356210437741210740000860318140844473844426"
"106696502175825986237944249828698290888857178633945273402044845898673345165419"
# or
contentIdHexBE =
"1c6046475f0772132774ab549173ca8487bea031ce539cad8e990c08df5802ca"
"ebe414854629d60c58ddd5bf60fd72e41760a5f7a463fdcb169f13ee4a26786b"
let contentKey = ContentKey(
contentType: blockBody,
blockBodyKey: BlockKey(chainId: 20'u16, blockHash: blockHash))
blockBodyKey: BlockKey(blockHash: blockHash))
let encoded = encode(contentKey)
check encoded.asSeq.toHex == contentKeyHex
@ -89,16 +89,16 @@ suite "History ContentKey Encodings":
# Output
const
contentKeyHex =
"020400d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
"02d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d"
contentId =
"76995449220721979583200368506411933662679656077191192504502358532083948020658"
"76230538398907151249589044529104962263309222250374376758768131420767496438948"
# or
contentIdHexBE =
"aa39e1423e92f5a667ace5b79c2c98adbfd79c055d891d0b9c49c40f816563b2"
"a888f4aafe9109d495ac4d4774a6277c1ada42035e3da5e10a04cc93247c04a4"
let contentKey = ContentKey(
contentType: receipts,
receiptsKey: BlockKey(chainId: 4'u16, blockHash: blockHash))
receiptsKey: BlockKey(blockHash: blockHash))
let encoded = encode(contentKey)
check encoded.asSeq.toHex == contentKeyHex

View File

@ -63,7 +63,7 @@ proc headersToContentInfo(headers: seq[BlockHeader]): seq[ContentInfo] =
for h in headers:
let
headerHash = h.blockHash()
bk = BlockKey(chainId: 1'u16, blockHash: headerHash)
bk = BlockKey(blockHash: headerHash)
ck = encode(ContentKey(contentType: blockHeader, blockHeaderKey: bk))
headerEncoded = rlp.encode(h)
ci = ContentInfo(contentKey: ck, content: headerEncoded)
@ -96,7 +96,7 @@ procSuite "History Content Network":
for h in headers:
let
headerHash = h.blockHash()
blockKey = BlockKey(chainId: 1'u16, blockHash: headerHash)
blockKey = BlockKey(blockHash: headerHash)
contentKey = ContentKey(
contentType: blockHeader, blockHeaderKey: blockKey)
contentId = toContentId(contentKey)
@ -116,7 +116,7 @@ procSuite "History Content Network":
(await historyNode2.portalProtocol().ping(historyNode1.localNode())).isOk()
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()

View File

@ -294,7 +294,7 @@ proc writeBlocksToDb(config: ExporterConf, client: RpcClient) =
let
blck = downloadBlock(i, client)
blockHash = blck.header.blockHash()
contentKeyType = BlockKey(chainId: 1, blockHash: blockHash)
contentKeyType = BlockKey(blockHash: blockHash)
headerKey = encode(ContentKey(
contentType: blockHeader, blockHeaderKey: contentKeyType))
bodyKey = encode(ContentKey(