Skip to content

Commit

Permalink
move to simple global builder
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-kothari committed Aug 24, 2023
1 parent 72dc397 commit a9cf859
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
2 changes: 2 additions & 0 deletions yarn-project/aztec.js/src/utils/cheat_codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ export class AztecCheatCodes {
// also store this time on the rollup contract (slot 1 tracks `lastBlockTs`).
// This is because when the sequencer executes public functions, it uses the timestamp stored in the rollup contract.
await this.eth.store(rollupContract, 1n, BigInt(to));
// also store this on slot 2 of the rollup contract (`lastWarpedBlockTs`) which tracks the last time warp was used.
await this.eth.store(rollupContract, 2n, BigInt(to));
}

/**
Expand Down
15 changes: 9 additions & 6 deletions yarn-project/end-to-end/src/e2e_cheat_codes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,19 @@ describe('e2e_cheat_codes', () => {
// ensure rollup contract is correctly updated
const rollup = getContract({ address: getAddress(rollupAddress.toString()), abi: RollupAbi, publicClient });
expect(Number(await rollup.read.lastBlockTs())).toEqual(newTimestamp);
expect(Number(await rollup.read.lastWarpedBlockTs())).toEqual(newTimestamp);

const txIsTimeEqual = contract.methods.isTimeEqual(newTimestamp).send({ origin: recipient });
await txIsTimeEqual.isMined({ interval: 0.1 });
const isTimeEqualReceipt = await txIsTimeEqual.getReceipt();
const isTimeEqualReceipt = await txIsTimeEqual.wait({ interval: 0.1 });
expect(isTimeEqualReceipt.status).toBe(TxStatus.MINED);

const txTimeNotEqual = contract.methods.isTimeEqual(0).send({ origin: recipient });
await txTimeNotEqual.isMined({ interval: 0.1 });
const isTimeNotEqualReceipt = await txTimeNotEqual.getReceipt();
expect(isTimeNotEqualReceipt.status).toBe(TxStatus.DROPPED);
// Since last rollup block was warped, txs for this rollup will have time incremented by 1
// See https://github.com/AztecProtocol/aztec-packages/issues/1614 for details
const txTimeNotEqual = contract.methods.isTimeEqual(newTimestamp+1).send({ origin: recipient });
const isTimeNotEqualReceipt = await txTimeNotEqual.wait({ interval: 0.1 });
expect(isTimeNotEqualReceipt.status).toBe(TxStatus.MINED);
// block is published at t >= newTimestamp + 1.
expect(Number(await rollup.read.lastBlockTs())).toBeGreaterThanOrEqual(newTimestamp + 1);
}, 50_000);

it('should throw if setting L2 block time to a past timestamp', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ export interface L1GlobalReader {
* @returns The chain id.
*/
getChainId(): Promise<bigint>;

/**
* Gets the current L1 time.
* @returns The current L1 time.
*/
getL1CurrentTime(): Promise<bigint>;

/**
* Gets the last time L2 was warped as tracked by the rollup contract.
* @returns The warped time.
*/
getLastWarpedBlockTs(): Promise<bigint>
}

/**
Expand Down Expand Up @@ -47,10 +59,24 @@ export class SimpleGlobalVariableBuilder implements GlobalVariableBuilder {
* @returns The global variables for the given block number.
*/
public async buildGlobalVariables(blockNumber: Fr): Promise<GlobalVariables> {
const lastTimestamp = new Fr(await this.reader.getLastTimestamp());
let lastTimestamp = new Fr(await this.reader.getLastTimestamp());
const version = new Fr(await this.reader.getVersion());
const chainId = new Fr(await this.reader.getChainId());

// TODO(rahul) - fix #1614. By using the cheatcode warp to modify L2 time,
// txs in the next rollup would have same time as the txs in the current rollup (i.e. the rollup that was warped).
// So, for now you check if L2 time was warped and if so, serve warpedTime + 1 to txs in the new rollup.
// Check if L2 time was warped in the last rollup by checking if current L1 time is same as the warpedTime (stored on the rollup contract).
// more details at https://github.com/AztecProtocol/aztec-packages/issues/1614

const currTimestamp = await this.reader.getL1CurrentTime();
const rollupWarpTime = await this.reader.getLastWarpedBlockTs();
const isLastBlockWarped = rollupWarpTime === currTimestamp;
this.log("isLastBlockWarped, ", isLastBlockWarped, rollupWarpTime, currTimestamp, lastTimestamp)
if (isLastBlockWarped) {
lastTimestamp = new Fr(lastTimestamp.value + 1n);
}

this.log(
`Built global variables for block ${blockNumber}: (${chainId}, ${version}, ${blockNumber}, ${lastTimestamp})`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,12 @@ export class ViemReader implements L1GlobalReader {
public async getChainId(): Promise<bigint> {
return await Promise.resolve(BigInt(this.publicClient.chain.id));
}

public async getL1CurrentTime(): Promise<bigint> {
return await Promise.resolve((await this.publicClient.getBlock()).timestamp);
}

public async getLastWarpedBlockTs(): Promise<bigint> {
return BigInt(await this.rollupContract.read.lastWarpedBlockTs());
}
}
12 changes: 0 additions & 12 deletions yarn-project/sequencer-client/src/sequencer/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,6 @@ export class Sequencer {
const newGlobalVariables = await this.globalsBuilder.buildGlobalVariables(new Fr(blockNumber));
const prevGlobalVariables = (await this.l2BlockSource.getL2Block(-1))?.globalVariables ?? GlobalVariables.empty();

// TODO(rahul) - fix #1614. By using the cheatcode warp to modify L2 time,
// txs in the new rollup would have same time as the txs in the previous rollup.
// We overcome this now by identifying if the last rollup time was warped (if two rollups have same time)
// and tell public-processor to use a different time (increment last rollup block)
// more details at https://github.com/AztecProtocol/aztec-packages/issues/1614
const isWarped =
!prevGlobalVariables.timestamp.equals(Fr.ZERO) &&
prevGlobalVariables.timestamp.equals(newGlobalVariables.timestamp);
if (isWarped) {
newGlobalVariables.timestamp = new Fr(newGlobalVariables.timestamp.value + 1n);
}

// Process txs and drop the ones that fail processing
// We create a fresh processor each time to reset any cached state (eg storage writes)
const processor = await this.publicProcessorFactory.create(prevGlobalVariables, newGlobalVariables);
Expand Down

0 comments on commit a9cf859

Please sign in to comment.