|
| 1 | +import { type AztecNodeService } from '@aztec/aztec-node'; |
| 2 | +import { type SentTx, sleep } from '@aztec/aztec.js'; |
| 3 | + |
| 4 | +/* eslint-disable-next-line no-restricted-imports */ |
| 5 | +import { BlockProposal, SignatureDomainSeperator, getHashedSignaturePayload } from '@aztec/circuit-types'; |
| 6 | + |
| 7 | +import { beforeAll, describe, it, jest } from '@jest/globals'; |
| 8 | +import fs from 'fs'; |
| 9 | + |
| 10 | +import { createNodes } from '../fixtures/setup_p2p_test.js'; |
| 11 | +import { P2PNetworkTest } from './p2p_network.js'; |
| 12 | +import { submitComplexTxsTo } from './shared.js'; |
| 13 | + |
| 14 | +const NUM_NODES = 4; |
| 15 | +const NUM_TXS_PER_NODE = 1; |
| 16 | +const BOOT_NODE_UDP_PORT = 41000; |
| 17 | + |
| 18 | +const DATA_DIR = './data/re-ex'; |
| 19 | + |
| 20 | +describe('e2e_p2p_reex', () => { |
| 21 | + let t: P2PNetworkTest; |
| 22 | + let nodes: AztecNodeService[]; |
| 23 | + |
| 24 | + beforeAll(async () => { |
| 25 | + nodes = []; |
| 26 | + |
| 27 | + t = await P2PNetworkTest.create({ |
| 28 | + testName: 'e2e_p2p_reex', |
| 29 | + numberOfNodes: NUM_NODES, |
| 30 | + basePort: BOOT_NODE_UDP_PORT, |
| 31 | + }); |
| 32 | + |
| 33 | + t.logger.verbose('Setup account'); |
| 34 | + await t.setupAccount(); |
| 35 | + |
| 36 | + t.logger.verbose('Deploy spam contract'); |
| 37 | + await t.deploySpamContract(); |
| 38 | + |
| 39 | + t.logger.verbose('Apply base snapshots'); |
| 40 | + await t.applyBaseSnapshots(); |
| 41 | + |
| 42 | + t.logger.verbose('Setup nodes'); |
| 43 | + await t.setup(); |
| 44 | + }); |
| 45 | + |
| 46 | + afterAll(async () => { |
| 47 | + // shutdown all nodes. |
| 48 | + await t.stopNodes(nodes); |
| 49 | + await t.teardown(); |
| 50 | + for (let i = 0; i < NUM_NODES; i++) { |
| 51 | + fs.rmSync(`${DATA_DIR}-${i}`, { recursive: true, force: true }); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + it('validators should re-execute transactions before attesting', async () => { |
| 56 | + // create the bootstrap node for the network |
| 57 | + if (!t.bootstrapNodeEnr) { |
| 58 | + throw new Error('Bootstrap node ENR is not available'); |
| 59 | + } |
| 60 | + |
| 61 | + t.ctx.aztecNodeConfig.validatorReexecute = true; |
| 62 | + |
| 63 | + nodes = await createNodes( |
| 64 | + t.ctx.aztecNodeConfig, |
| 65 | + t.peerIdPrivateKeys, |
| 66 | + t.bootstrapNodeEnr, |
| 67 | + NUM_NODES, |
| 68 | + BOOT_NODE_UDP_PORT, |
| 69 | + ); |
| 70 | + |
| 71 | + // Hook into the node and intercept re-execution logic, ensuring that it was infact called |
| 72 | + const reExecutionSpies = []; |
| 73 | + for (const node of nodes) { |
| 74 | + // Make sure the nodes submit faulty proposals, in this case a faulty proposal is one where we remove one of the transactions |
| 75 | + // Such that the calculated archive will be different! |
| 76 | + jest.spyOn((node as any).p2pClient, 'broadcastProposal').mockImplementation(async (...args: unknown[]) => { |
| 77 | + // We remove one of the transactions, therefore the block root will be different! |
| 78 | + const proposal = args[0] as BlockProposal; |
| 79 | + const { txHashes } = proposal.payload; |
| 80 | + |
| 81 | + // We need to mutate the proposal, so we cast to any |
| 82 | + (proposal.payload as any).txHashes = txHashes.slice(0, txHashes.length - 1); |
| 83 | + |
| 84 | + // We sign over the proposal using the node's signing key |
| 85 | + // Abusing javascript to access the nodes signing key |
| 86 | + const signer = (node as any).sequencer.sequencer.validatorClient.validationService.keyStore; |
| 87 | + const newProposal = new BlockProposal( |
| 88 | + proposal.payload, |
| 89 | + await signer.signMessage(getHashedSignaturePayload(proposal.payload, SignatureDomainSeperator.blockProposal)), |
| 90 | + ); |
| 91 | + |
| 92 | + return (node as any).p2pClient.p2pService.propagate(newProposal); |
| 93 | + }); |
| 94 | + |
| 95 | + // Store re-execution spys node -> sequencer Client -> seqeuncer -> validator |
| 96 | + const spy = jest.spyOn((node as any).sequencer.sequencer.validatorClient, 'reExecuteTransactions'); |
| 97 | + reExecutionSpies.push(spy); |
| 98 | + } |
| 99 | + |
| 100 | + // wait a bit for peers to discover each other |
| 101 | + await sleep(4000); |
| 102 | + |
| 103 | + nodes.forEach(node => { |
| 104 | + node.getSequencer()?.updateSequencerConfig({ |
| 105 | + minTxsPerBlock: NUM_TXS_PER_NODE, |
| 106 | + maxTxsPerBlock: NUM_TXS_PER_NODE, |
| 107 | + }); |
| 108 | + }); |
| 109 | + const txs = await submitComplexTxsTo(t.logger, t.spamContract!, NUM_TXS_PER_NODE); |
| 110 | + |
| 111 | + // We ensure that the transactions are NOT mined |
| 112 | + try { |
| 113 | + await Promise.all( |
| 114 | + txs.map(async (tx: SentTx, i: number) => { |
| 115 | + t.logger.info(`Waiting for tx ${i}: ${await tx.getTxHash()} to be mined`); |
| 116 | + return tx.wait(); |
| 117 | + }), |
| 118 | + ); |
| 119 | + } catch (e) { |
| 120 | + t.logger.info('Failed to mine all txs, as planned'); |
| 121 | + } |
| 122 | + |
| 123 | + // Expect that all of the re-execution attempts failed with an invalid root |
| 124 | + for (const spy of reExecutionSpies) { |
| 125 | + for (const result of spy.mock.results) { |
| 126 | + await expect(result.value).rejects.toThrow('Validator Error: Re-execution state mismatch'); |
| 127 | + } |
| 128 | + } |
| 129 | + }); |
| 130 | +}); |
0 commit comments