Skip to content

Commit

Permalink
Pw/block download fix (#27)
Browse files Browse the repository at this point in the history
* Fixed the block download calculation

* Comment fix

* More fixes
  • Loading branch information
PhilWindle committed Mar 13, 2023
1 parent bad0524 commit 96bcd9d
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions yarn-project/sdk/src/core_sdk/block_downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ export class BlockDownloader {
private interruptableSleep = new InterruptableSleep();
private semaphore: Semaphore;
private queue = new MemoryFifo<Block[]>();
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) {
Expand All @@ -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);
Expand Down

0 comments on commit 96bcd9d

Please sign in to comment.