Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions yarn-project/pxe/src/block_synchronizer/block_synchronizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,43 @@ describe('BlockSynchronizer', () => {
expect(rollback).toHaveBeenCalledWith(3, 4);
});

describe('stop', () => {
it('resolves immediately when no sync is in progress', async () => {
await synchronizer.stop();
expect(blockStream.stop).toHaveBeenCalled();
});

it('waits for in-progress sync to complete', async () => {
let resolveSync!: () => void;
const syncBlocker = new Promise<void>(resolve => {
resolveSync = resolve;
});
blockStream.sync.mockReturnValue(syncBlocker);
aztecNode.getBlockHeader.mockResolvedValue((await L2Block.random(BlockNumber(0))).header);

// Start a sync (don't await)
const syncPromise = synchronizer.sync();

// stop() should not resolve until the sync finishes
let stopped = false;
const stopPromise = synchronizer.stop().then(() => {
stopped = true;
});

// Give the event loop a tick
await new Promise(resolve => setTimeout(resolve, 10));
expect(stopped).toBe(false);

// Release the sync
resolveSync();
await syncPromise;
await stopPromise;

expect(stopped).toBe(true);
expect(blockStream.stop).toHaveBeenCalled();
});
});

describe('syncChainTip config', () => {
it('updates anchor on blocks-added when syncChainTip is proposed (default)', async () => {
synchronizer = createSynchronizer({ syncChainTip: 'proposed' });
Expand Down
6 changes: 6 additions & 0 deletions yarn-project/pxe/src/block_synchronizer/block_synchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ export class BlockSynchronizer implements L2BlockStreamEventHandler {
}
}

/** Stops the block synchronizer, waiting for any in-progress sync to complete. */
public async stop() {
await this.isSyncing;
await this.blockStream.stop();
}

private async doSync() {
let currentHeader;

Expand Down
1 change: 1 addition & 0 deletions yarn-project/pxe/src/pxe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,7 @@ export class PXE {
*/
public async stop(): Promise<void> {
await this.jobQueue.end();
await this.blockStateSynchronizer.stop();
await this.db.close();
}
}
Loading