Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sequencer aborts in-progress block #2883

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export class SequencerClient {
blockBuilder,
l2BlockSource,
l1ToL2MessageSource,
contractDataSource,
publicProcessorFactory,
config,
);
Expand Down
46 changes: 31 additions & 15 deletions yarn-project/sequencer-client/src/sequencer/sequencer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,10 @@ import {
makeEmptyProof,
} from '@aztec/circuits.js';
import { P2P, P2PClientState } from '@aztec/p2p';
import {
ContractDataSource,
L1ToL2MessageSource,
L2Block,
L2BlockSource,
MerkleTreeId,
Tx,
TxHash,
mockTx,
} from '@aztec/types';
import { L1ToL2MessageSource, L2Block, L2BlockSource, MerkleTreeId, Tx, TxHash, mockTx } from '@aztec/types';
import { MerkleTreeOperations, WorldStateRunningState, WorldStateSynchronizer } from '@aztec/world-state';

import { MockProxy, mock } from 'jest-mock-extended';
import { MockProxy, mock, mockFn } from 'jest-mock-extended';
import times from 'lodash.times';

import { BlockBuilder } from '../block_builder/index.js';
Expand Down Expand Up @@ -74,16 +65,14 @@ describe('sequencer', () => {
});

l2BlockSource = mock<L2BlockSource>({
getBlockNumber: () => Promise.resolve(lastBlockNumber),
getBlockNumber: mockFn().mockResolvedValue(lastBlockNumber),
});

l1ToL2MessageSource = mock<L1ToL2MessageSource>({
getPendingL1ToL2Messages: () => Promise.resolve(Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(Fr.ZERO)),
getBlockNumber: () => Promise.resolve(lastBlockNumber),
});

const contractDataSource = mock<ContractDataSource>({});

sequencer = new TestSubject(
publisher,
globalVariableBuilder,
Expand All @@ -92,7 +81,6 @@ describe('sequencer', () => {
blockBuilder,
l2BlockSource,
l1ToL2MessageSource,
contractDataSource,
publicProcessorFactory,
);
});
Expand Down Expand Up @@ -193,6 +181,34 @@ describe('sequencer', () => {
expect(publisher.processL2Block).toHaveBeenCalledWith(block);
expect(p2p.deleteTxs).toHaveBeenCalledWith([await invalidChainTx.getTxHash()]);
});

fit('aborts building a block if the chain moves underneath it', async () => {
const tx = mockTx();
tx.data.constants.txContext.chainId = chainId;
const block = L2Block.random(lastBlockNumber + 1);
const proof = makeEmptyProof();

p2p.getTxs.mockResolvedValueOnce([tx]);
blockBuilder.buildL2Block.mockResolvedValueOnce([block, proof]);
publisher.processL2Block.mockResolvedValueOnce(true);
globalVariableBuilder.buildGlobalVariables.mockResolvedValueOnce(
new GlobalVariables(chainId, version, new Fr(lastBlockNumber + 1), Fr.ZERO),
);

await sequencer.initialSync();

l2BlockSource.getBlockNumber
// let it work for a bit
.mockResolvedValueOnce(lastBlockNumber)
.mockResolvedValueOnce(lastBlockNumber)
.mockResolvedValueOnce(lastBlockNumber)
// then tell it to abort
.mockResolvedValue(lastBlockNumber + 1);

await sequencer.work();

expect(publisher.processL2Block).not.toHaveBeenCalled();
});
});

class TestSubject extends Sequencer {
Expand Down
26 changes: 22 additions & 4 deletions yarn-project/sequencer-client/src/sequencer/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createDebugLogger } from '@aztec/foundation/log';
import { RunningPromise } from '@aztec/foundation/running-promise';
import { Timer, elapsed } from '@aztec/foundation/timer';
import { P2P } from '@aztec/p2p';
import { ContractDataSource, L1ToL2MessageSource, L2Block, L2BlockSource, MerkleTreeId, Tx } from '@aztec/types';
import { L1ToL2MessageSource, L2Block, L2BlockSource, MerkleTreeId, Tx } from '@aztec/types';
import { L2BlockBuiltStats } from '@aztec/types/stats';
import { WorldStateStatus, WorldStateSynchronizer } from '@aztec/world-state';

Expand Down Expand Up @@ -43,7 +43,6 @@ export class Sequencer {
private blockBuilder: BlockBuilder,
private l2BlockSource: L2BlockSource,
private l1ToL2MessageSource: L1ToL2MessageSource,
private contractDataSource: ContractDataSource,
private publicProcessorFactory: PublicProcessorFactory,
config: SequencerConfig = {},
private log = createDebugLogger('aztec:sequencer'),
Expand Down Expand Up @@ -131,6 +130,17 @@ export class Sequencer {
this.log.info(`Retrieved ${pendingTxs.length} txs from P2P pool`);

const blockNumber = (await this.l2BlockSource.getBlockNumber()) + 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) {
throw new Error('New block was emitted while building block');
}
};

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

// Filter out invalid txs
Expand All @@ -156,14 +166,16 @@ export class Sequencer {
// Only accept processed transactions that are not double-spends,
// public functions emitting nullifiers would pass earlier check but fail here.
// Note that we're checking all nullifiers generated in the private execution twice,
// we could store the ones already checked and skip them here as an optimisation.
// we could store the ones already checked and skip them here as an optimization.
const processedValidTxs = await this.takeValidTxs(processedTxs, newGlobalVariables);

if (processedValidTxs.length === 0) {
this.log('No txs processed correctly to build block. Exiting');
return;
}

await assertBlockHeight();

// Get l1 to l2 messages from the contract
this.log('Requesting L1 to L2 messages from contract');
const l1ToL2Messages = await this.getPendingL1ToL2Messages();
Expand All @@ -172,6 +184,8 @@ export class Sequencer {
// Build the new block by running the rollup circuits
this.log(`Assembling block with txs ${processedValidTxs.map(tx => tx.hash).join(', ')}`);

await assertBlockHeight();

Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets put a check in before we publish the block too.

const emptyTx = await processor.makeEmptyProcessedTx();
const [rollupCircuitsDuration, block] = await elapsed(() =>
this.buildBlock(processedValidTxs, l1ToL2Messages, emptyTx, newGlobalVariables),
Expand All @@ -185,12 +199,16 @@ export class Sequencer {
...block.getStats(),
} satisfies L2BlockBuiltStats);

await assertBlockHeight();

await this.publishExtendedContractData(validTxs, block);

await assertBlockHeight();

await this.publishL2Block(block);
this.log.info(`Submitted rollup block ${block.number} with ${processedValidTxs.length} transactions`);
} catch (err) {
this.log.error(`Rolling back world state DB due to error assembling block`, err);
this.log.error(`Rolling back world state DB due to error assembling block`, (err as any).stack);
await this.worldState.getLatest().rollback();
}
}
Expand Down