|
| 1 | +import { TestCircuitVerifier } from '@aztec/bb-prover'; |
| 2 | +import { |
| 3 | + type AztecNode, |
| 4 | + type L1ToL2MessageSource, |
| 5 | + type L2BlockSource, |
| 6 | + type L2LogsSource, |
| 7 | + MerkleTreeId, |
| 8 | + type MerkleTreeOperations, |
| 9 | + mockTxForRollup, |
| 10 | +} from '@aztec/circuit-types'; |
| 11 | +import { AztecAddress, EthAddress, Fr, GasFees, GlobalVariables, MaxBlockNumber } from '@aztec/circuits.js'; |
| 12 | +import { type AztecLmdbStore } from '@aztec/kv-store/lmdb'; |
| 13 | +import { type P2P } from '@aztec/p2p'; |
| 14 | +import { type GlobalVariableBuilder } from '@aztec/sequencer-client'; |
| 15 | +import { NoopTelemetryClient } from '@aztec/telemetry-client/noop'; |
| 16 | +import { type ContractDataSource } from '@aztec/types/contracts'; |
| 17 | +import { type WorldStateSynchronizer } from '@aztec/world-state'; |
| 18 | + |
| 19 | +import { type MockProxy, mock, mockFn } from 'jest-mock-extended'; |
| 20 | + |
| 21 | +import { type AztecNodeConfig, getConfigEnvVars } from './config.js'; |
| 22 | +import { AztecNodeService } from './server.js'; |
| 23 | + |
| 24 | +describe('aztec node', () => { |
| 25 | + let p2p: MockProxy<P2P>; |
| 26 | + let globalVariablesBuilder: MockProxy<GlobalVariableBuilder>; |
| 27 | + let merkleTreeOps: MockProxy<MerkleTreeOperations>; |
| 28 | + |
| 29 | + let lastBlockNumber: number; |
| 30 | + |
| 31 | + let node: AztecNode; |
| 32 | + |
| 33 | + const chainId = new Fr(12345); |
| 34 | + const version = Fr.ZERO; |
| 35 | + const coinbase = EthAddress.random(); |
| 36 | + const feeRecipient = AztecAddress.random(); |
| 37 | + const gasFees = GasFees.empty(); |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + lastBlockNumber = 0; |
| 41 | + |
| 42 | + p2p = mock<P2P>(); |
| 43 | + |
| 44 | + globalVariablesBuilder = mock<GlobalVariableBuilder>(); |
| 45 | + merkleTreeOps = mock<MerkleTreeOperations>(); |
| 46 | + |
| 47 | + const worldState = mock<WorldStateSynchronizer>({ |
| 48 | + getLatest: () => merkleTreeOps, |
| 49 | + }); |
| 50 | + |
| 51 | + const l2BlockSource = mock<L2BlockSource>({ |
| 52 | + getBlockNumber: mockFn().mockResolvedValue(lastBlockNumber), |
| 53 | + }); |
| 54 | + |
| 55 | + const l2LogsSource = mock<L2LogsSource>(); |
| 56 | + |
| 57 | + const l1ToL2MessageSource = mock<L1ToL2MessageSource>(); |
| 58 | + |
| 59 | + // all txs use the same allowed FPC class |
| 60 | + const contractSource = mock<ContractDataSource>(); |
| 61 | + |
| 62 | + const store = mock<AztecLmdbStore>(); |
| 63 | + |
| 64 | + const aztecNodeConfig: AztecNodeConfig = getConfigEnvVars(); |
| 65 | + |
| 66 | + node = new AztecNodeService( |
| 67 | + { |
| 68 | + ...aztecNodeConfig, |
| 69 | + l1Contracts: { |
| 70 | + ...aztecNodeConfig.l1Contracts, |
| 71 | + rollupAddress: EthAddress.ZERO, |
| 72 | + registryAddress: EthAddress.ZERO, |
| 73 | + inboxAddress: EthAddress.ZERO, |
| 74 | + outboxAddress: EthAddress.ZERO, |
| 75 | + availabilityOracleAddress: EthAddress.ZERO, |
| 76 | + }, |
| 77 | + }, |
| 78 | + p2p, |
| 79 | + l2BlockSource, |
| 80 | + l2LogsSource, |
| 81 | + l2LogsSource, |
| 82 | + contractSource, |
| 83 | + l1ToL2MessageSource, |
| 84 | + worldState, |
| 85 | + undefined, |
| 86 | + 31337, |
| 87 | + 1, |
| 88 | + globalVariablesBuilder, |
| 89 | + store, |
| 90 | + new TestCircuitVerifier(), |
| 91 | + new NoopTelemetryClient(), |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('tx validation', () => { |
| 96 | + it('tests that the node correctly validates double spends', async () => { |
| 97 | + const txs = [mockTxForRollup(0x10000), mockTxForRollup(0x20000)]; |
| 98 | + txs.forEach(tx => { |
| 99 | + tx.data.constants.txContext.chainId = chainId; |
| 100 | + }); |
| 101 | + const doubleSpendTx = txs[0]; |
| 102 | + const doubleSpendWithExistingTx = txs[1]; |
| 103 | + |
| 104 | + const mockedGlobalVariables = new GlobalVariables( |
| 105 | + chainId, |
| 106 | + version, |
| 107 | + new Fr(lastBlockNumber + 1), |
| 108 | + new Fr(1), |
| 109 | + Fr.ZERO, |
| 110 | + coinbase, |
| 111 | + feeRecipient, |
| 112 | + gasFees, |
| 113 | + ); |
| 114 | + |
| 115 | + globalVariablesBuilder.buildGlobalVariables |
| 116 | + .mockResolvedValueOnce(mockedGlobalVariables) |
| 117 | + .mockResolvedValueOnce(mockedGlobalVariables); |
| 118 | + |
| 119 | + expect(await node.isValidTx(doubleSpendTx)).toBe(true); |
| 120 | + |
| 121 | + // We push a duplicate nullifier that was created in the same transaction |
| 122 | + doubleSpendTx.data.forRollup!.end.nullifiers.push(doubleSpendTx.data.forRollup!.end.nullifiers[0]); |
| 123 | + |
| 124 | + expect(await node.isValidTx(doubleSpendTx)).toBe(false); |
| 125 | + |
| 126 | + globalVariablesBuilder.buildGlobalVariables |
| 127 | + .mockResolvedValueOnce(mockedGlobalVariables) |
| 128 | + .mockResolvedValueOnce(mockedGlobalVariables); |
| 129 | + |
| 130 | + expect(await node.isValidTx(doubleSpendWithExistingTx)).toBe(true); |
| 131 | + |
| 132 | + // We make a nullifier from `doubleSpendWithExistingTx` a part of the nullifier tree, so it gets rejected as double spend |
| 133 | + const doubleSpendNullifier = doubleSpendWithExistingTx.data.forRollup!.end.nullifiers[0].toBuffer(); |
| 134 | + merkleTreeOps.findLeafIndex.mockImplementation((treeId: MerkleTreeId, value: any) => { |
| 135 | + return Promise.resolve( |
| 136 | + treeId === MerkleTreeId.NULLIFIER_TREE && value.equals(doubleSpendNullifier) ? 1n : undefined, |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + expect(await node.isValidTx(doubleSpendWithExistingTx)).toBe(false); |
| 141 | + }); |
| 142 | + |
| 143 | + it('tests that the node correctly validates chain id', async () => { |
| 144 | + const tx = mockTxForRollup(0x10000); |
| 145 | + tx.data.constants.txContext.chainId = chainId; |
| 146 | + |
| 147 | + const mockedGlobalVariables = new GlobalVariables( |
| 148 | + chainId, |
| 149 | + version, |
| 150 | + new Fr(lastBlockNumber + 1), |
| 151 | + new Fr(1), |
| 152 | + Fr.ZERO, |
| 153 | + coinbase, |
| 154 | + feeRecipient, |
| 155 | + gasFees, |
| 156 | + ); |
| 157 | + |
| 158 | + globalVariablesBuilder.buildGlobalVariables |
| 159 | + .mockResolvedValueOnce(mockedGlobalVariables) |
| 160 | + .mockResolvedValueOnce(mockedGlobalVariables); |
| 161 | + |
| 162 | + expect(await node.isValidTx(tx)).toBe(true); |
| 163 | + |
| 164 | + // We make the chain id on the tx not equal to the configured chain id |
| 165 | + tx.data.constants.txContext.chainId = new Fr(1n + chainId.value); |
| 166 | + |
| 167 | + expect(await node.isValidTx(tx)).toBe(false); |
| 168 | + }); |
| 169 | + |
| 170 | + it('tests that the node correctly validates max block numbers', async () => { |
| 171 | + const txs = [mockTxForRollup(0x10000), mockTxForRollup(0x20000), mockTxForRollup(0x30000)]; |
| 172 | + txs.forEach(tx => { |
| 173 | + tx.data.constants.txContext.chainId = chainId; |
| 174 | + }); |
| 175 | + |
| 176 | + const noMaxBlockNumberMetadata = txs[0]; |
| 177 | + const invalidMaxBlockNumberMetadata = txs[1]; |
| 178 | + const validMaxBlockNumberMetadata = txs[2]; |
| 179 | + |
| 180 | + invalidMaxBlockNumberMetadata.data.forRollup!.rollupValidationRequests = { |
| 181 | + maxBlockNumber: new MaxBlockNumber(true, new Fr(1)), |
| 182 | + getSize: () => 1, |
| 183 | + toBuffer: () => Fr.ZERO.toBuffer(), |
| 184 | + }; |
| 185 | + |
| 186 | + validMaxBlockNumberMetadata.data.forRollup!.rollupValidationRequests = { |
| 187 | + maxBlockNumber: new MaxBlockNumber(true, new Fr(5)), |
| 188 | + getSize: () => 1, |
| 189 | + toBuffer: () => Fr.ZERO.toBuffer(), |
| 190 | + }; |
| 191 | + |
| 192 | + const mockedGlobalVariables = new GlobalVariables( |
| 193 | + chainId, |
| 194 | + version, |
| 195 | + new Fr(lastBlockNumber + 5), |
| 196 | + new Fr(1), |
| 197 | + Fr.ZERO, |
| 198 | + coinbase, |
| 199 | + feeRecipient, |
| 200 | + gasFees, |
| 201 | + ); |
| 202 | + |
| 203 | + globalVariablesBuilder.buildGlobalVariables |
| 204 | + .mockResolvedValueOnce(mockedGlobalVariables) |
| 205 | + .mockResolvedValueOnce(mockedGlobalVariables) |
| 206 | + .mockResolvedValueOnce(mockedGlobalVariables); |
| 207 | + |
| 208 | + // Default tx with no max block number should be valid |
| 209 | + expect(await node.isValidTx(noMaxBlockNumberMetadata)).toBe(true); |
| 210 | + // Tx with max block number < current block number should be invalid |
| 211 | + expect(await node.isValidTx(invalidMaxBlockNumberMetadata)).toBe(false); |
| 212 | + // Tx with max block number >= current block number should be valid |
| 213 | + expect(await node.isValidTx(validMaxBlockNumberMetadata)).toBe(true); |
| 214 | + }); |
| 215 | + }); |
| 216 | +}); |
0 commit comments