Skip to content

Commit

Permalink
saving one block num call
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jan 29, 2024
1 parent 485611b commit 1ac16e7
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions yarn-project/sequencer-client/src/sequencer/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,23 @@ export class Sequencer {
}
this.log.info(`Retrieved ${pendingTxs.length} txs from P2P pool`);

// TODO(benesjan): remove this call and replace the value with the value from the block header?
const blockNumber = (await this.l2BlockSource.getBlockNumber()) + 1;
const prevHeader = (await this.l2BlockSource.getBlock(-1))?.header;
const newBlockNumber =
(prevHeader === undefined
? await this.l2BlockSource.getBlockNumber()
: Number(prevHeader.globalVariables.blockNumber.toBigInt())) + 1;

/**
* We'll call this function before running expensive operations to avoid wasted work.
*/
const assertBlockHeight = async () => {
const currentBlockNumber = await this.l2BlockSource.getBlockNumber();
if (currentBlockNumber + 1 !== blockNumber) {
if (currentBlockNumber + 1 !== newBlockNumber) {
throw new Error('New block was emitted while building block');
}
};

const newGlobalVariables = await this.globalsBuilder.buildGlobalVariables(new Fr(blockNumber));
const newGlobalVariables = await this.globalsBuilder.buildGlobalVariables(new Fr(newBlockNumber));

// Filter out invalid txs
// TODO: It should be responsibility of the P2P layer to validate txs before passing them on here
Expand All @@ -161,12 +164,9 @@ export class Sequencer {
return;
}

this.log.info(`Building block ${blockNumber} with ${validTxs.length} transactions`);
this.log.info(`Building block ${newBlockNumber} with ${validTxs.length} transactions`);
this.state = SequencerState.CREATING_BLOCK;

// TODO(benesjan): is this correct? Should we add a check that all the tree roots are really empty?
const prevHeader = (await this.l2BlockSource.getBlock(-1))?.header;

// 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(prevHeader, newGlobalVariables);
Expand Down

0 comments on commit 1ac16e7

Please sign in to comment.