Skip to content
Merged
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
46 changes: 35 additions & 11 deletions yarn-project/world-state/src/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('world-state integration', () => {
log.info(`Generating ${MAX_BLOCK_COUNT} mock blocks`);
({ blocks, messages } = await mockBlocks(1, MAX_BLOCK_COUNT, 1, db));
log.info(`Generated ${blocks.length} mock blocks`);
});
}, 30_000);

beforeEach(async () => {
config = {
Expand All @@ -56,7 +56,30 @@ describe('world-state integration', () => {
await db.close();
});

const awaitSync = () => sleep(200);
const awaitSync = async (blockToSyncTo: number, finalized?: number, maxTimeoutMS = 2000) => {
const startTime = Date.now();
let sleepTime = 0;
let tips = await synchronizer.getL2Tips();

const waitForFinalised = (tipFinalised?: number) => {
if (finalized == undefined || tipFinalised == undefined) {
return false;
}
return finalized > tipFinalised;
};

while (tips.latest < blockToSyncTo && sleepTime < maxTimeoutMS) {
await sleep(100);
sleepTime = Date.now() - startTime;
tips = await synchronizer.getL2Tips();
}

while (waitForFinalised(tips.finalized) && sleepTime < maxTimeoutMS) {
await sleep(100);
sleepTime = Date.now() - startTime;
tips = await synchronizer.getL2Tips();
}
};

const expectSynchedBlockHashMatches = async (number: number) => {
const syncedBlockHash = await db.getCommitted().getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number));
Expand Down Expand Up @@ -85,7 +108,7 @@ describe('world-state integration', () => {
it('syncs new blocks from the archiver from genesis', async () => {
await synchronizer.start();
archiver.createBlocks(5);
await awaitSync();
await awaitSync(5);
await expectSynchedToBlock(5);
});

Expand All @@ -94,7 +117,7 @@ describe('world-state integration', () => {
await synchronizer.start();

archiver.createBlocks(3);
await awaitSync();
await awaitSync(8);
await expectSynchedToBlock(8);
});

Expand All @@ -104,7 +127,7 @@ describe('world-state integration', () => {
await expectSynchedToBlock(10);

archiver.createBlocks(10);
await awaitSync();
await awaitSync(20);
await expectSynchedToBlock(20);
});

Expand All @@ -113,7 +136,7 @@ describe('world-state integration', () => {

await synchronizer.start();
archiver.createBlocks(5);
await awaitSync();
await awaitSync(5);
await expectSynchedToBlock(5);
await synchronizer.stopBlockStream();

Expand All @@ -124,7 +147,7 @@ describe('world-state integration', () => {
await expectSynchedToBlock(8);

archiver.createBlocks(4);
await awaitSync();
await awaitSync(12);
await expectSynchedToBlock(12);

expect(getBlocksSpy).toHaveBeenCalledTimes(3);
Expand All @@ -142,7 +165,7 @@ describe('world-state integration', () => {
await expectSynchedToBlock(3);

archiver.setProvenBlockNumber(4);
await awaitSync();
await awaitSync(4);
await expectSynchedToBlock(4);
});
});
Expand All @@ -159,7 +182,8 @@ describe('world-state integration', () => {

archiver.removeBlocks(3);
archiver.createBlocks(2);
await awaitSync();
await sleep(2000);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this sleep, given the awaitSync?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out we do, to give time for the reorg to be picked up

await awaitSync(4);
await expectSynchedToBlock(4);
});
});
Expand Down Expand Up @@ -220,11 +244,11 @@ describe('world-state integration', () => {
archiver.setProvenBlockNumber(3);

await synchronizer.start();
await awaitSync();
await awaitSync(5, 3);
await expectSynchedToBlock(5, 3);

archiver.setProvenBlockNumber(4);
await awaitSync();
await awaitSync(5, 4);
await expectSynchedToBlock(5, 4);
});
});
Expand Down