From f8a476f31b7af7806c4715607a6df40c1e3eb0ed Mon Sep 17 00:00:00 2001 From: cheatfate Date: Mon, 2 Dec 2024 12:50:23 +0200 Subject: [PATCH] Add fixes to map generation and add test helpers. --- beacon_chain/sync/sync_queue.nim | 33 ++++++++++++++--------- tests/test_sync_manager.nim | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/beacon_chain/sync/sync_queue.nim b/beacon_chain/sync/sync_queue.nim index 1488aabb2..3c6068a4c 100644 --- a/beacon_chain/sync/sync_queue.nim +++ b/beacon_chain/sync/sync_queue.nim @@ -7,7 +7,7 @@ {.push raises: [].} -import std/[deques, heapqueue, tables, strutils, sequtils, math] +import std/[deques, heapqueue, tables, strutils, sequtils, math, typetraits] import stew/base10, chronos, chronicles, results import ../spec/datatypes/[base, phase0, altair], @@ -183,21 +183,30 @@ proc getShortMap*[T]( var res = newStringOfCap(req.data.count) slider = req.data.slot - last = 0 + notFirst = false - for i in 0 ..< req.data.count: - if last < len(blobs): - if len(blobs[last]) > 0: - if blobs[last][0][].signed_block_header.message.slot == slider: - res.add(Base10.toString(lenu64(blobs[last]))) + for i in 0 ..< int(req.data.count): + if i >= len(blobs): + res.add('.'.repeat(int(req.data.count) - len(res))) + return res + + if len(blobs[i]) > 0: + let slot = blobs[i][0][].signed_block_header.message.slot + if not(notFirst): + doAssert(slot >= slider, "Incorrect slot number in blobs list") + let firstCount = int(slot - slider) + res.add('.'.repeat(firstCount)) + res.add(Base10.toString(lenu64(blobs[i]))) + slider = slot + notFirst = true + else: + if slot == slider: + res.add(Base10.toString(lenu64(blobs[i]))) else: res.add('.') - else: - res.add('.') - inc(last) else: - res.add('.') - inc(slider) + if notFirst: res.add('.') + if notFirst: inc(slider) res proc getShortMap*[T]( diff --git a/tests/test_sync_manager.nim b/tests/test_sync_manager.nim index ad3860d80..e1870049c 100644 --- a/tests/test_sync_manager.nim +++ b/tests/test_sync_manager.nim @@ -89,6 +89,52 @@ func createBlobs( inc sidecarIdx res +func createBlobSidecars(srange: SyncRange, map: string): seq[BlobSidecars] = + var + res: seq[BlobSidecars] + notFirst = false + doAssert(lenu64(map) == srange.count, + "Length of map string should be equal to range size") + for index in 0 ..< srange.count: + let slot = srange.slot + index + if map[index] == '.': + if notFirst: + res.add(default(BlobSidecars)) + else: + let count = Base10.decode(uint8, [map[index]]).get() + doAssert(count > 0 and count <= 9) + var subres: seq[ref BlobSidecar] + for i in 0 ..< int(count): + let car = + newClone(BlobSidecar( + index: uint64(i), + signed_block_header: + SignedBeaconBlockHeader( + message: BeaconBlockHeader(slot: slot)))) + subres.add(car) + res.add(BlobSidecars(subres)) + notFirst = true + res + +func createBlobRange(srange: SyncRange, map: string): seq[ref BlobSidecar] = + var res: seq[ref BlobSidecar] + doAssert(lenu64(map) == srange.count, + "Length of map string should be equal to range size") + for index in 0 ..< srange.count: + let slot = srange.slot + index + if map[index] != '.': + let count = Base10.decode(uint8, [map[index]]).get() + doAssert(count > 0 and count <= 9) + for i in 0 ..< int(count): + let car = + newClone(BlobSidecar( + index: uint64(i), + signed_block_header: + SignedBeaconBlockHeader( + message: BeaconBlockHeader(slot: slot)))) + res.add(car) + res + func collector(queue: AsyncQueue[BlockEntry]): BlockVerifier = proc verify( signedBlock: ForkedSignedBeaconBlock,