-
Notifications
You must be signed in to change notification settings - Fork 310
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: adding aztec node tx validation tests #8220
Merged
sklppy88
merged 1 commit into
ek/fix/4781/adding-double-spend-validator-to-node
from
ek/fix/4781/adding-tx-validation-tests-to-node
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
import { TestCircuitVerifier } from '@aztec/bb-prover'; | ||
import { | ||
type AztecNode, | ||
type L1ToL2MessageSource, | ||
type L2BlockSource, | ||
type L2LogsSource, | ||
MerkleTreeId, | ||
type MerkleTreeOperations, | ||
mockTxForRollup, | ||
} from '@aztec/circuit-types'; | ||
import { AztecAddress, EthAddress, Fr, GasFees, GlobalVariables, MaxBlockNumber } from '@aztec/circuits.js'; | ||
import { type AztecLmdbStore } from '@aztec/kv-store/lmdb'; | ||
import { type P2P } from '@aztec/p2p'; | ||
import { type GlobalVariableBuilder, type L1Publisher } from '@aztec/sequencer-client'; | ||
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop'; | ||
import { type ContractDataSource } from '@aztec/types/contracts'; | ||
import { type WorldStateSynchronizer } from '@aztec/world-state'; | ||
|
||
import { type MockProxy, mock, mockFn } from 'jest-mock-extended'; | ||
|
||
import { type AztecNodeConfig, getConfigEnvVars } from './config.js'; | ||
import { AztecNodeService } from './server.js'; | ||
|
||
describe('aztec node', () => { | ||
let p2p: MockProxy<P2P>; | ||
let globalVariablesBuilder: MockProxy<GlobalVariableBuilder>; | ||
let merkleTreeOps: MockProxy<MerkleTreeOperations>; | ||
|
||
let lastBlockNumber: number; | ||
|
||
let node: AztecNode; | ||
|
||
const chainId = new Fr(12345); | ||
const version = Fr.ZERO; | ||
const coinbase = EthAddress.random(); | ||
const feeRecipient = AztecAddress.random(); | ||
const gasFees = GasFees.empty(); | ||
|
||
beforeEach(() => { | ||
lastBlockNumber = 0; | ||
|
||
p2p = mock<P2P>(); | ||
|
||
const publisher = mock<L1Publisher>(); | ||
|
||
publisher.isItMyTurnToSubmit.mockResolvedValue(true); | ||
|
||
globalVariablesBuilder = mock<GlobalVariableBuilder>(); | ||
merkleTreeOps = mock<MerkleTreeOperations>(); | ||
|
||
const worldState = mock<WorldStateSynchronizer>({ | ||
getLatest: () => merkleTreeOps, | ||
}); | ||
|
||
const l2BlockSource = mock<L2BlockSource>({ | ||
getBlockNumber: mockFn().mockResolvedValue(lastBlockNumber), | ||
}); | ||
|
||
const l2LogsSource = mock<L2LogsSource>(); | ||
|
||
const l1ToL2MessageSource = mock<L1ToL2MessageSource>(); | ||
|
||
// all txs use the same allowed FPC class | ||
const contractSource = mock<ContractDataSource>(); | ||
|
||
const store = mock<AztecLmdbStore>(); | ||
|
||
const aztecNodeConfig: AztecNodeConfig = getConfigEnvVars(); | ||
|
||
node = new AztecNodeService( | ||
{ | ||
...aztecNodeConfig, | ||
l1Contracts: { | ||
...aztecNodeConfig.l1Contracts, | ||
rollupAddress: EthAddress.ZERO, | ||
registryAddress: EthAddress.ZERO, | ||
inboxAddress: EthAddress.ZERO, | ||
outboxAddress: EthAddress.ZERO, | ||
availabilityOracleAddress: EthAddress.ZERO, | ||
}, | ||
}, | ||
p2p, | ||
l2BlockSource, | ||
l2LogsSource, | ||
l2LogsSource, | ||
contractSource, | ||
l1ToL2MessageSource, | ||
worldState, | ||
undefined, | ||
31337, | ||
1, | ||
globalVariablesBuilder, | ||
store, | ||
new TestCircuitVerifier(), | ||
new NoopTelemetryClient(), | ||
); | ||
}); | ||
|
||
describe('tx validation', () => { | ||
it('tests that the node correctly validates double spends', async () => { | ||
const txs = [mockTxForRollup(0x10000), mockTxForRollup(0x20000)]; | ||
txs.forEach(tx => { | ||
tx.data.constants.txContext.chainId = chainId; | ||
}); | ||
const doubleSpendTx = txs[0]; | ||
const doubleSpendWithExistingTx = txs[1]; | ||
|
||
const mockedGlobalVariables = new GlobalVariables( | ||
chainId, | ||
version, | ||
new Fr(lastBlockNumber + 1), | ||
new Fr(1), | ||
Fr.ZERO, | ||
coinbase, | ||
feeRecipient, | ||
gasFees, | ||
); | ||
|
||
globalVariablesBuilder.buildGlobalVariables | ||
.mockResolvedValueOnce(mockedGlobalVariables) | ||
.mockResolvedValueOnce(mockedGlobalVariables); | ||
|
||
expect(await node.isValidTx(doubleSpendTx)).toBe(true); | ||
|
||
// We push a duplicate nullifier that was created in the same transaction | ||
doubleSpendTx.data.forRollup!.end.nullifiers.push(doubleSpendTx.data.forRollup!.end.nullifiers[0]); | ||
|
||
expect(await node.isValidTx(doubleSpendTx)).toBe(false); | ||
|
||
globalVariablesBuilder.buildGlobalVariables | ||
.mockResolvedValueOnce(mockedGlobalVariables) | ||
.mockResolvedValueOnce(mockedGlobalVariables); | ||
|
||
expect(await node.isValidTx(doubleSpendWithExistingTx)).toBe(true); | ||
|
||
// We make a nullifier from `doubleSpendWithExistingTx` a part of the nullifier tree, so it gets rejected as double spend | ||
const doubleSpendNullifier = doubleSpendWithExistingTx.data.forRollup!.end.nullifiers[0].toBuffer(); | ||
merkleTreeOps.findLeafIndex.mockImplementation((treeId: MerkleTreeId, value: any) => { | ||
return Promise.resolve( | ||
treeId === MerkleTreeId.NULLIFIER_TREE && value.equals(doubleSpendNullifier) ? 1n : undefined, | ||
); | ||
}); | ||
|
||
expect(await node.isValidTx(doubleSpendWithExistingTx)).toBe(false); | ||
}); | ||
|
||
it('tests that the node correctly validates chain id', async () => { | ||
const tx = mockTxForRollup(0x10000); | ||
tx.data.constants.txContext.chainId = chainId; | ||
|
||
const mockedGlobalVariables = new GlobalVariables( | ||
chainId, | ||
version, | ||
new Fr(lastBlockNumber + 1), | ||
new Fr(1), | ||
Fr.ZERO, | ||
coinbase, | ||
feeRecipient, | ||
gasFees, | ||
); | ||
|
||
globalVariablesBuilder.buildGlobalVariables | ||
.mockResolvedValueOnce(mockedGlobalVariables) | ||
.mockResolvedValueOnce(mockedGlobalVariables); | ||
|
||
expect(await node.isValidTx(tx)).toBe(true); | ||
|
||
// We make the chain id on the tx not equal to the configured chain id | ||
tx.data.constants.txContext.chainId = new Fr(1n + chainId.value); | ||
|
||
expect(await node.isValidTx(tx)).toBe(false); | ||
}); | ||
|
||
it('tests that the node correctly validates max block numbers', async () => { | ||
const txs = [mockTxForRollup(0x10000), mockTxForRollup(0x20000), mockTxForRollup(0x30000)]; | ||
txs.forEach(tx => { | ||
tx.data.constants.txContext.chainId = chainId; | ||
}); | ||
|
||
const noMaxBlockNumberMetadata = txs[0]; | ||
const invalidMaxBlockNumberMetadata = txs[1]; | ||
const validMaxBlockNumberMetadata = txs[2]; | ||
|
||
invalidMaxBlockNumberMetadata.data.forRollup!.rollupValidationRequests = { | ||
maxBlockNumber: new MaxBlockNumber(true, new Fr(1)), | ||
getSize: () => 1, | ||
toBuffer: () => Fr.ZERO.toBuffer(), | ||
}; | ||
|
||
validMaxBlockNumberMetadata.data.forRollup!.rollupValidationRequests = { | ||
maxBlockNumber: new MaxBlockNumber(true, new Fr(5)), | ||
getSize: () => 1, | ||
toBuffer: () => Fr.ZERO.toBuffer(), | ||
}; | ||
|
||
const mockedGlobalVariables = new GlobalVariables( | ||
chainId, | ||
version, | ||
new Fr(lastBlockNumber + 5), | ||
new Fr(1), | ||
Fr.ZERO, | ||
coinbase, | ||
feeRecipient, | ||
gasFees, | ||
); | ||
|
||
globalVariablesBuilder.buildGlobalVariables | ||
.mockResolvedValueOnce(mockedGlobalVariables) | ||
.mockResolvedValueOnce(mockedGlobalVariables) | ||
.mockResolvedValueOnce(mockedGlobalVariables); | ||
|
||
// Default tx with no max block number should be valid | ||
expect(await node.isValidTx(noMaxBlockNumberMetadata)).toBe(true); | ||
// Tx with max block number < current block number should be invalid | ||
expect(await node.isValidTx(invalidMaxBlockNumberMetadata)).toBe(false); | ||
// Tx with max block number >= current block number should be valid | ||
expect(await node.isValidTx(validMaxBlockNumberMetadata)).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised by all of this, did we have no tests for the node validations at all?