Skip to content

Commit

Permalink
fix!: sandbox uses Anvil with interval mining (#11383)
Browse files Browse the repository at this point in the history
This PR makes the Anvil instance run in sandbox to do interval mining
with a standard slot duration of 12 seconds.
  • Loading branch information
alexghr authored Jan 29, 2025
1 parent 9eb5214 commit 0ba31df
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
25 changes: 19 additions & 6 deletions yarn-project/aztec.js/src/utils/anvil_test_watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class AnvilTestWatcher {
const isAutoMining = await this.cheatcodes.isAutoMining();

if (isAutoMining) {
this.filledRunningPromise = new RunningPromise(() => this.mineIfSlotFilled(), this.logger, 1000);
this.filledRunningPromise = new RunningPromise(() => this.warpTimeIfNeeded(), this.logger, 1000);
this.filledRunningPromise.start();
this.logger.info(`Watcher started for rollup at ${this.rollup.address}`);
} else {
Expand All @@ -60,23 +60,36 @@ export class AnvilTestWatcher {
await this.filledRunningPromise?.stop();
}

async mineIfSlotFilled() {
async warpTimeIfNeeded() {
try {
const currentSlot = await this.rollup.read.getCurrentSlot();
const pendingBlockNumber = BigInt(await this.rollup.read.getPendingBlockNumber());
const blockLog = await this.rollup.read.getBlock([pendingBlockNumber]);
const nextSlotTimestamp = Number(await this.rollup.read.getTimestampForSlot([currentSlot + 1n]));

if (currentSlot === blockLog.slotNumber) {
// We should jump to the next slot
const timestamp = await this.rollup.read.getTimestampForSlot([currentSlot + 1n]);
try {
await this.cheatcodes.warp(Number(timestamp));
this.dateProvider?.setTime(Number(timestamp) * 1000);
await this.cheatcodes.warp(nextSlotTimestamp);
this.dateProvider?.setTime(nextSlotTimestamp * 1000);
} catch (e) {
this.logger.error(`Failed to warp to timestamp ${timestamp}: ${e}`);
this.logger.error(`Failed to warp to timestamp ${nextSlotTimestamp}: ${e}`);
}

this.logger.info(`Slot ${currentSlot} was filled, jumped to next slot`);
return;
}

const currentTimestamp = this.dateProvider?.now() ?? Date.now();
if (currentTimestamp > nextSlotTimestamp * 1000) {
try {
await this.cheatcodes.warp(nextSlotTimestamp);
this.dateProvider?.setTime(nextSlotTimestamp * 1000);
} catch (e) {
this.logger.error(`Failed to warp to timestamp ${nextSlotTimestamp}: ${e}`);
}

this.logger.info(`Slot ${currentSlot} was missed, jumped to next slot`);
}
} catch (err) {
this.logger.error('mineIfSlotFilled failed');
Expand Down
1 change: 1 addition & 0 deletions yarn-project/end-to-end/scripts/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
ARCHIVER_VIEM_POLLING_INTERVAL_MS: 500
HARDWARE_CONCURRENCY: ${HARDWARE_CONCURRENCY:-}


end-to-end:
image: aztecprotocol/build:2.0
volumes:
Expand Down

0 comments on commit 0ba31df

Please sign in to comment.