Skip to content

Commit

Permalink
rewrite: response checking from RequestManager for blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
agnxsh committed Dec 10, 2024
1 parent 7d81ee1 commit c26ae50
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
19 changes: 19 additions & 0 deletions beacon_chain/spec/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# Uncategorized helper functions from the spec

import
std/sequtils,
# Status libraries
stew/[byteutils, endians2, objects],
nimcrypto/sha2,
Expand Down Expand Up @@ -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:
Expand Down
30 changes: 20 additions & 10 deletions beacon_chain/sync/request_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]).} =
Expand Down

0 comments on commit c26ae50

Please sign in to comment.