From c26ae503427043780e5d827f106f57023a820a69 Mon Sep 17 00:00:00 2001 From: Agnish Ghosh Date: Tue, 10 Dec 2024 15:07:00 +0530 Subject: [PATCH] rewrite: response checking from RequestManager for blobs --- beacon_chain/spec/helpers.nim | 19 +++++++++++++++++ beacon_chain/sync/request_manager.nim | 30 ++++++++++++++++++--------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/beacon_chain/spec/helpers.nim b/beacon_chain/spec/helpers.nim index e1df9ee2b3..92c3c605d2 100644 --- a/beacon_chain/spec/helpers.nim +++ b/beacon_chain/spec/helpers.nim @@ -10,6 +10,7 @@ # Uncategorized helper functions from the spec import + std/sequtils, # Status libraries stew/[byteutils, endians2, objects], nimcrypto/sha2, @@ -251,6 +252,24 @@ func create_blob_sidecars*( res.add(sidecar) res +func search_sidecar_identifier*( + data: seq[BlobIdentifier] | seq[DataColumnIdentifier], + target: ref BlobSidecar | ref DataColumnSidecar): int = + var + low_pt = 0 + high_pt = data.high + + while low_pt <= high_pt: + let mid = (low_pt + high_pt) div 2 + if data[mid].index == target.index: + return mid # Target found at index `mid` + elif data[mid].index < target.index: + low_pt = mid + 1 + else: + high_pt = mid - 1 + + -1 # Target not found + # https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.5/specs/altair/light-client/sync-protocol.md#is_sync_committee_update template is_sync_committee_update*(update: SomeForkyLightClientUpdate): bool = when update is SomeForkyLightClientUpdateWithSyncCommittee: diff --git a/beacon_chain/sync/request_manager.nim b/beacon_chain/sync/request_manager.nim index 964e9fd799..a0fef50c22 100644 --- a/beacon_chain/sync/request_manager.nim +++ b/beacon_chain/sync/request_manager.nim @@ -104,19 +104,29 @@ proc checkResponse(roots: openArray[Eth2Digest], proc checkResponse(idList: seq[BlobIdentifier], blobs: openArray[ref BlobSidecar]): bool = - if len(blobs) > len(idList): + if blobs.len > idList.len: return false - for blob in blobs: - let block_root = hash_tree_root(blob.signed_block_header.message) - var found = false - for id in idList: - if id.block_root == block_root and id.index == blob.index: - found = true - break - if not found: + + var i = 0 + while i < blobs.len: + let + block_root = hash_tree_root(blobs[i].signed_block_header.message) + id = idList[i] + + # Check if the blob response is a subset + if search_sidecar_identifier(idList, blobs[i]) == -1: + i = if i != blobs.len - 1: i + 2 else: i + 1 + continue + + # Verify block_root and index match + if id.block_root != block_root or id.index != blobs[i].index: return false - blob[].verify_blob_sidecar_inclusion_proof().isOkOr: + + # Verify inclusion proof + blobs[i][].verify_blob_sidecar_inclusion_proof().isOkOr: return false + + inc i true proc requestBlocksByRoot(rman: RequestManager, items: seq[Eth2Digest]) {.async: (raises: [CancelledError]).} =