From 96bcd9dde2a836a90ae9c6c050f0edf053404097 Mon Sep 17 00:00:00 2001 From: PhilWindle <60546371+PhilWindle@users.noreply.github.com> Date: Mon, 13 Mar 2023 17:20:13 +0000 Subject: [PATCH] Pw/block download fix (#27) * Fixed the block download calculation * Comment fix * More fixes --- .../sdk/src/core_sdk/block_downloader.ts | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/yarn-project/sdk/src/core_sdk/block_downloader.ts b/yarn-project/sdk/src/core_sdk/block_downloader.ts index 015dcb108..b7fe24e72 100644 --- a/yarn-project/sdk/src/core_sdk/block_downloader.ts +++ b/yarn-project/sdk/src/core_sdk/block_downloader.ts @@ -11,10 +11,17 @@ export class BlockDownloader { private interruptableSleep = new InterruptableSleep(); private semaphore: Semaphore; private queue = new MemoryFifo(); + private genesisTake; private debug = createDebugLogger('bb:block_downloader'); - constructor(private rollupProvider: RollupProvider, maxQueueSize: number, private initialTreeSize: number) { + constructor(private rollupProvider: RollupProvider, maxQueueSize: number, initialTreeSize: number) { this.semaphore = new Semaphore(maxQueueSize); + // Choosing 55 as an initial chunk to insert if starting from 0, is an aztec-connect optimisation. + // The aztec-connect genesis data consists of 73 rollups. + // Initially inserting 55 brings us to 128, after which we work with chunks of 128 rollups. + // If not synching from zero, the chunk size is whatever takes us up to the next 128 alignment. + // This allows for optimal subtree insertions in the client side merkle tree for better sync performance. + this.genesisTake = 128 - (initialTreeSize % 128); } public start(from = 0) { @@ -31,13 +38,12 @@ export class BlockDownloader { const fn = async () => { while (this.running) { try { - // Choosing 55 as an initial chunk to insert if starting from 0, is an aztec-connect optimisation. - // The aztec-connect genesis data consists of 73 rollups. - // Initially inserting 55 brings us to 128, after which we work with chunks of 128 rollups. - // If not synching from zero, the chunk size is whatever takes us up to the next 128 alignment. - // This allows for optimal subtree insertions in the client side merkle tree for better sync performance. - const initialTake = 128 - ((this.from === 0 ? this.initialTreeSize : this.from) % 128); - const blocks = await this.rollupProvider.getBlocks(this.from, this.from === from ? initialTake : 128); + // If requesting from block 0, then take the fixed number of blocks to take us to 128 (genesisTake) + // Otherwise, take blocks as required to get us to a 128 aligned boundary starting from block (128 - initialTreeSize). + // e.g. we are trying to get to blocks 183, 311, 439 etc.... + const takeValue = + this.from < this.genesisTake ? this.genesisTake - this.from : 128 - ((this.from - this.genesisTake) % 128); + const blocks = await this.rollupProvider.getBlocks(this.from, takeValue); if (!blocks.length) { await this.interruptableSleep.sleep(10000);