Skip to content

Commit

Permalink
fix #10077
Browse files Browse the repository at this point in the history
  • Loading branch information
just-mitch committed Dec 2, 2024
1 parent f670067 commit 58aa154
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ class TestSubject extends Sequencer {
}

public override doRealWork() {
this.setState(SequencerState.IDLE, 0, true /** force */);
this.setState(SequencerState.IDLE, 0n, true /** force */);
return super.doRealWork();
}
}
59 changes: 24 additions & 35 deletions yarn-project/sequencer-client/src/sequencer/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class Sequencer {
public start() {
this.runningPromise = new RunningPromise(this.work.bind(this), this.pollingIntervalMs);
this.runningPromise.start();
this.setState(SequencerState.IDLE, 0, true /** force */);
this.setState(SequencerState.IDLE, 0n, true /** force */);
this.log.info('Sequencer started');
return Promise.resolve();
}
Expand All @@ -201,7 +201,7 @@ export class Sequencer {
this.log.debug(`Stopping sequencer`);
await this.runningPromise?.stop();
this.publisher.interrupt();
this.setState(SequencerState.STOPPED, 0, true /** force */);
this.setState(SequencerState.STOPPED, 0n, true /** force */);
this.log.info('Stopped sequencer');
}

Expand All @@ -212,7 +212,7 @@ export class Sequencer {
this.log.info('Restarting sequencer');
this.publisher.restart();
this.runningPromise!.start();
this.setState(SequencerState.IDLE, 0, true /** force */);
this.setState(SequencerState.IDLE, 0n, true /** force */);
}

/**
Expand All @@ -232,7 +232,7 @@ export class Sequencer {
* - If our block for some reason is not included, revert the state
*/
protected async doRealWork() {
this.setState(SequencerState.SYNCHRONIZING, 0);
this.setState(SequencerState.SYNCHRONIZING, 0n);
// Update state when the previous block has been synced
const prevBlockSynced = await this.isBlockSynced();
// Do not go forward with new block if the previous one has not been mined and processed
Expand All @@ -243,7 +243,7 @@ export class Sequencer {

this.log.debug('Previous block has been mined and processed');

this.setState(SequencerState.PROPOSER_CHECK, 0);
this.setState(SequencerState.PROPOSER_CHECK, 0n);

const chainTip = await this.l2BlockSource.getBlock(-1);
const historicalHeader = chainTip?.header;
Expand Down Expand Up @@ -277,9 +277,8 @@ export class Sequencer {
if (!this.shouldProposeBlock(historicalHeader, {})) {
return;
}
const secondsIntoSlot = getSecondsIntoSlot(this.l1GenesisTime, this.aztecSlotDuration, Number(slot));

this.setState(SequencerState.WAITING_FOR_TXS, secondsIntoSlot);
this.setState(SequencerState.WAITING_FOR_TXS, slot);

// Get txs to build the new block.
const pendingTxs = this.p2pClient.getTxs('pending');
Expand Down Expand Up @@ -325,7 +324,7 @@ export class Sequencer {
} catch (err) {
this.log.error(`Error assembling block`, (err as any).stack);
}
this.setState(SequencerState.IDLE, 0);
this.setState(SequencerState.IDLE, 0n);
}

protected async work() {
Expand All @@ -339,7 +338,7 @@ export class Sequencer {
throw err;
}
} finally {
this.setState(SequencerState.IDLE, 0);
this.setState(SequencerState.IDLE, 0n);
}
}

Expand Down Expand Up @@ -398,13 +397,23 @@ export class Sequencer {
return true;
}

setState(proposedState: SequencerState, secondsIntoSlot: number, force: boolean = false) {
/**
* Sets the sequencer state and checks if we have enough time left in the slot to transition to the new state.
* @param proposedState - The new state to transition to.
* @param currentSlotNumber - The current slot number.
* @param force - Whether to force the transition even if the sequencer is stopped.
*
* @dev If the `currentSlotNumber` doesn't matter (e.g. transitioning to IDLE), pass in `0n`;
* it is only used to check if we have enough time left in the slot to transition to the new state.
*/
setState(proposedState: SequencerState, currentSlotNumber: bigint, force: boolean = false) {
if (this.state === SequencerState.STOPPED && force !== true) {
this.log.warn(
`Cannot set sequencer from ${this.state} to ${proposedState} as it is stopped. Set force=true to override.`,
);
return;
}
const secondsIntoSlot = getSecondsIntoSlot(this.l1GenesisTime, this.aztecSlotDuration, Number(currentSlotNumber));
if (!this.doIHaveEnoughTimeLeft(proposedState, secondsIntoSlot)) {
throw new SequencerTooSlowError(this.state, proposedState, this.timeTable[proposedState], secondsIntoSlot);
}
Expand Down Expand Up @@ -567,12 +576,7 @@ export class Sequencer {

this.metrics.recordNewBlock(newGlobalVariables.blockNumber.toNumber(), validTxs.length);
const workTimer = new Timer();
const secondsIntoSlot = getSecondsIntoSlot(
this.l1GenesisTime,
this.aztecSlotDuration,
newGlobalVariables.slotNumber.toNumber(),
);
this.setState(SequencerState.CREATING_BLOCK, secondsIntoSlot);
this.setState(SequencerState.CREATING_BLOCK, newGlobalVariables.slotNumber.toBigInt());
this.log.info(
`Building blockNumber=${newGlobalVariables.blockNumber.toNumber()} txCount=${
validTxs.length
Expand Down Expand Up @@ -688,23 +692,13 @@ export class Sequencer {
this.log.info('Creating block proposal');
const proposal = await this.validatorClient.createBlockProposal(block.header, block.archive.root, txHashes);

let secondsIntoSlot = getSecondsIntoSlot(
this.l1GenesisTime,
this.aztecSlotDuration,
block.header.globalVariables.slotNumber.toNumber(),
);
const slotNumber = block.header.globalVariables.slotNumber.toBigInt();

this.setState(SequencerState.PUBLISHING_BLOCK_TO_PEERS, secondsIntoSlot);
this.setState(SequencerState.PUBLISHING_BLOCK_TO_PEERS, slotNumber);
this.log.info('Broadcasting block proposal to validators');
this.validatorClient.broadcastBlockProposal(proposal);

secondsIntoSlot = getSecondsIntoSlot(
this.l1GenesisTime,
this.aztecSlotDuration,
block.header.globalVariables.slotNumber.toNumber(),
);

this.setState(SequencerState.WAITING_FOR_ATTESTATIONS, secondsIntoSlot);
this.setState(SequencerState.WAITING_FOR_ATTESTATIONS, slotNumber);
const attestations = await this.validatorClient.collectAttestations(proposal, numberOfRequiredAttestations);
this.log.info(`Collected attestations from validators, number of attestations: ${attestations.length}`);

Expand Down Expand Up @@ -761,13 +755,8 @@ export class Sequencer {
txHashes?: TxHash[],
proofQuote?: EpochProofQuote,
) {
const secondsIntoSlot = getSecondsIntoSlot(
this.l1GenesisTime,
this.aztecSlotDuration,
block.header.globalVariables.slotNumber.toNumber(),
);
// Publishes new block to the network and awaits the tx to be mined
this.setState(SequencerState.PUBLISHING_BLOCK, secondsIntoSlot);
this.setState(SequencerState.PUBLISHING_BLOCK, block.header.globalVariables.slotNumber.toBigInt());

const publishedL2Block = await this.publisher.proposeL2Block(block, attestations, txHashes, proofQuote);
if (!publishedL2Block) {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/sequencer-client/src/sequencer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ export function orderAttestations(attestations: BlockAttestation[], orderAddress

export function getSecondsIntoSlot(l1GenesisTime: number, aztecSlotDuration: number, slotNumber: number): number {
const slotStartTimestamp = l1GenesisTime + slotNumber * aztecSlotDuration;
return Date.now() / 1000 - slotStartTimestamp;
return Number((Date.now() / 1000 - slotStartTimestamp).toFixed(3));
}

0 comments on commit 58aa154

Please sign in to comment.