|
| 1 | +import type { AztecNodeService } from '@aztec/aztec-node'; |
| 2 | +import { type Logger, retryUntil } from '@aztec/aztec.js'; |
| 3 | +import { type ExtendedViemWalletClient, type Operator, RollupContract } from '@aztec/ethereum'; |
| 4 | +import { asyncMap } from '@aztec/foundation/async-map'; |
| 5 | +import { times } from '@aztec/foundation/collection'; |
| 6 | +import { EthAddress } from '@aztec/foundation/eth-address'; |
| 7 | +import { bufferToHex } from '@aztec/foundation/string'; |
| 8 | +import { RollupAbi } from '@aztec/l1-artifacts'; |
| 9 | +import type { SpamContract } from '@aztec/noir-test-contracts.js/Spam'; |
| 10 | + |
| 11 | +import { jest } from '@jest/globals'; |
| 12 | +import { privateKeyToAccount } from 'viem/accounts'; |
| 13 | + |
| 14 | +import { type EndToEndContext, getPrivateKeyFromIndex } from '../fixtures/utils.js'; |
| 15 | +import { EpochsTestContext } from './epochs_test.js'; |
| 16 | + |
| 17 | +jest.setTimeout(1000 * 60 * 10); |
| 18 | + |
| 19 | +const NODE_COUNT = 3; |
| 20 | +const VALIDATOR_COUNT = 3; |
| 21 | + |
| 22 | +// This test validates the scenario where: |
| 23 | +// 1. A sequencer posts a block without all necessary attestations |
| 24 | +// 2. The next proposer sees the invalid block and invalidates it as part of publishing a new block |
| 25 | +// 3. All nodes sync the block with correct attestations |
| 26 | +describe('e2e_epochs/epochs_invalidate_block', () => { |
| 27 | + let context: EndToEndContext; |
| 28 | + let logger: Logger; |
| 29 | + let l1Client: ExtendedViemWalletClient; |
| 30 | + let rollupContract: RollupContract; |
| 31 | + |
| 32 | + let test: EpochsTestContext; |
| 33 | + let validators: (Operator & { privateKey: `0x${string}` })[]; |
| 34 | + let nodes: AztecNodeService[]; |
| 35 | + let contract: SpamContract; |
| 36 | + |
| 37 | + beforeEach(async () => { |
| 38 | + validators = times(VALIDATOR_COUNT, i => { |
| 39 | + const privateKey = bufferToHex(getPrivateKeyFromIndex(i + 3)!); |
| 40 | + const attester = EthAddress.fromString(privateKeyToAccount(privateKey).address); |
| 41 | + return { attester, withdrawer: attester, privateKey }; |
| 42 | + }); |
| 43 | + |
| 44 | + // Setup context with the given set of validators, mocked gossip sub network, and no anvil test watcher. |
| 45 | + test = await EpochsTestContext.setup({ |
| 46 | + numberOfAccounts: 1, |
| 47 | + initialValidators: validators, |
| 48 | + mockGossipSubNetwork: true, |
| 49 | + disableAnvilTestWatcher: true, |
| 50 | + aztecProofSubmissionEpochs: 1024, |
| 51 | + startProverNode: false, |
| 52 | + aztecTargetCommitteeSize: VALIDATOR_COUNT, |
| 53 | + }); |
| 54 | + |
| 55 | + ({ context, logger, l1Client } = test); |
| 56 | + rollupContract = new RollupContract(l1Client, test.rollup.address); |
| 57 | + |
| 58 | + // Halt block building in initial aztec node |
| 59 | + logger.warn(`Stopping sequencer in initial aztec node.`); |
| 60 | + await context.sequencer!.stop(); |
| 61 | + |
| 62 | + // Start the validator nodes |
| 63 | + logger.warn(`Initial setup complete. Starting ${NODE_COUNT} validator nodes.`); |
| 64 | + const validatorNodes = validators.slice(0, NODE_COUNT); |
| 65 | + nodes = await asyncMap(validatorNodes, ({ privateKey }) => |
| 66 | + test.createValidatorNode([privateKey], { dontStartSequencer: true, minTxsPerBlock: 1, maxTxsPerBlock: 1 }), |
| 67 | + ); |
| 68 | + logger.warn(`Started ${NODE_COUNT} validator nodes.`, { validators: validatorNodes.map(v => v.attester) }); |
| 69 | + |
| 70 | + // Register spam contract for sending txs. |
| 71 | + contract = await test.registerSpamContract(context.wallet); |
| 72 | + logger.warn(`Test setup completed.`, { validators: validators.map(v => v.attester.toString()) }); |
| 73 | + }); |
| 74 | + |
| 75 | + afterEach(async () => { |
| 76 | + jest.restoreAllMocks(); |
| 77 | + await test.teardown(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('invalidates a block published without sufficient attestations', async () => { |
| 81 | + const sequencers = nodes.map(node => node.getSequencer()!); |
| 82 | + const initialBlockNumber = await nodes[0].getBlockNumber(); |
| 83 | + |
| 84 | + // Configure all sequencers to skip collecting attestations before starting |
| 85 | + logger.warn('Configuring all sequencers to skip attestation collection'); |
| 86 | + sequencers.forEach(sequencer => { |
| 87 | + sequencer.updateSequencerConfig({ skipCollectingAttestations: true }); |
| 88 | + }); |
| 89 | + |
| 90 | + // Send a transaction so the sequencer builds a block |
| 91 | + logger.warn('Sending transaction to trigger block building'); |
| 92 | + const sentTx = contract.methods.spam(1, 1n, false).send(); |
| 93 | + |
| 94 | + // Disable skipCollectingAttestations after the first block is mined |
| 95 | + test.monitor.once('l2-block', ({ l2BlockNumber }) => { |
| 96 | + logger.warn(`Disabling skipCollectingAttestations after L2 block ${l2BlockNumber} has been mined`); |
| 97 | + sequencers.forEach(sequencer => { |
| 98 | + sequencer.updateSequencerConfig({ skipCollectingAttestations: false }); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + // Start all sequencers |
| 103 | + await Promise.all(sequencers.map(s => s.start())); |
| 104 | + logger.warn(`Started all sequencers with skipCollectingAttestations=true`); |
| 105 | + |
| 106 | + // Create a filter for BlockInvalidated events |
| 107 | + const blockInvalidatedFilter = await l1Client.createContractEventFilter({ |
| 108 | + address: rollupContract.address, |
| 109 | + abi: RollupAbi, |
| 110 | + eventName: 'BlockInvalidated', |
| 111 | + fromBlock: 1n, |
| 112 | + toBlock: 'latest', |
| 113 | + }); |
| 114 | + |
| 115 | + // The next proposer should invalidate the previous block and publish a new one |
| 116 | + logger.warn('Waiting for next proposer to invalidate the previous block'); |
| 117 | + |
| 118 | + // Wait for the BlockInvalidated event |
| 119 | + const blockInvalidatedEvents = await retryUntil( |
| 120 | + async () => { |
| 121 | + const events = await l1Client.getFilterLogs({ filter: blockInvalidatedFilter }); |
| 122 | + return events.length > 0 ? events : undefined; |
| 123 | + }, |
| 124 | + 'BlockInvalidated event', |
| 125 | + test.L2_SLOT_DURATION_IN_S * 5, |
| 126 | + 0.1, |
| 127 | + ); |
| 128 | + |
| 129 | + // Verify the BlockInvalidated event was emitted |
| 130 | + const [event] = blockInvalidatedEvents; |
| 131 | + logger.warn(`BlockInvalidated event emitted`, { event }); |
| 132 | + expect(event.args.blockNumber).toBeGreaterThan(initialBlockNumber); |
| 133 | + |
| 134 | + // Wait for all nodes to sync the new block |
| 135 | + logger.warn('Waiting for all nodes to sync'); |
| 136 | + await retryUntil( |
| 137 | + async () => { |
| 138 | + const blockNumbers = await Promise.all(nodes.map(node => node.getBlockNumber())); |
| 139 | + logger.info(`Node synced block numbers: ${blockNumbers.join(', ')}`); |
| 140 | + return blockNumbers.every(bn => bn > initialBlockNumber); |
| 141 | + }, |
| 142 | + 'Node sync check', |
| 143 | + test.L2_SLOT_DURATION_IN_S * 5, |
| 144 | + 0.5, |
| 145 | + ); |
| 146 | + |
| 147 | + // Verify the transaction was eventually included |
| 148 | + const receipt = await sentTx.wait({ timeout: 30 }); |
| 149 | + expect(receipt.status).toBe('success'); |
| 150 | + logger.warn(`Transaction included in block ${receipt.blockNumber}`); |
| 151 | + }); |
| 152 | +}); |
0 commit comments