Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Dec 13, 2024
1 parent 64af3be commit 075171f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
7 changes: 4 additions & 3 deletions yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,8 @@ export class AztecNodeService implements AztecNode, Traceable {
}
}

// TODO(md): change this to run in parrel the same as in the p2p client - maybe not required
// as everything is sub ms apart from the double spend validator
public async isValidTx(tx: Tx, isSimulation: boolean = false): Promise<boolean> {
const blockNumber = (await this.blockSource.getBlockNumber()) + 1;
const db = this.worldStateSynchronizer.getCommitted();
Expand All @@ -855,9 +857,8 @@ export class AztecNodeService implements AztecNode, Traceable {
new DataTxValidator(),
new MetadataTxValidator(new Fr(this.l1ChainId), new Fr(blockNumber)),
new DoubleSpendTxValidator({
getNullifierIndex(nullifier) {
return db.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]).then(x => x[0]);
},
getNullifierIndices: nullifiers =>
db.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, nullifiers).then(x => x.filter(index => index !== undefined) as bigint[]),
}),
];

Expand Down
12 changes: 6 additions & 6 deletions yarn-project/p2p/src/services/libp2p/libp2p_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,10 +603,10 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
},
doubleSpendValidator: {
validator: new DoubleSpendTxValidator({
getNullifierIndex: async (nullifier: Fr) => {
getNullifierIndices: async (nullifiers: Buffer[]) => {
const merkleTree = this.worldStateSynchronizer.getCommitted();
const index = (await merkleTree.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]))[0];
return index;
const indices = await merkleTree.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, nullifiers);
return indices.filter(index => index !== undefined) as bigint[];
},
}),
severity: PeerErrorSeverity.HighToleranceError,
Expand Down Expand Up @@ -669,12 +669,12 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
}

const snapshotValidator = new DoubleSpendTxValidator({
getNullifierIndex: async (nullifier: Fr) => {
getNullifierIndices: async (nullifiers: Buffer[]) => {
const merkleTree = this.worldStateSynchronizer.getSnapshot(
blockNumber - this.config.severePeerPenaltyBlockLength,
);
const index = (await merkleTree.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]))[0];
return index;
const indices = await merkleTree.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, nullifiers);
return indices.filter(index => index !== undefined) as bigint[];
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('DoubleSpendTxValidator', () => {

beforeEach(() => {
nullifierSource = mock<NullifierSource>({
getNullifierIndex: mockFn().mockImplementation(() => {
getNullifierIndices: mockFn().mockImplementation(() => {
return Promise.resolve(undefined);
}),
});
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('DoubleSpendTxValidator', () => {

it('rejects duplicates against history', async () => {
const badTx = mockTx();
nullifierSource.getNullifierIndex.mockReturnValueOnce(Promise.resolve(1n));
nullifierSource.getNullifierIndices.mockReturnValueOnce(Promise.resolve([1n]));
await expect(txValidator.validateTxs([badTx])).resolves.toEqual([[], [badTx]]);
});
});
11 changes: 5 additions & 6 deletions yarn-project/p2p/src/tx_validator/double_spend_validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Fr } from '@aztec/circuits.js';
import { createLogger } from '@aztec/foundation/log';

export interface NullifierSource {
getNullifierIndex: (nullifier: Fr) => Promise<bigint | undefined>;
getNullifierIndices: (nullifiers: Buffer[]) => Promise<bigint[]>;
}

export class DoubleSpendTxValidator<T extends AnyTx> implements TxValidator<T> {
Expand Down Expand Up @@ -36,9 +36,7 @@ export class DoubleSpendTxValidator<T extends AnyTx> implements TxValidator<T> {
}

async #uniqueNullifiers(tx: AnyTx, thisBlockNullifiers: Set<bigint>): Promise<boolean> {
const nullifiers = (tx instanceof Tx ? tx.data.getNonEmptyNullifiers() : tx.txEffect.nullifiers).map(x =>
x.toBigInt(),
);
const nullifiers = (tx instanceof Tx ? tx.data.getNonEmptyNullifiers() : tx.txEffect.nullifiers);

// Ditch this tx if it has repeated nullifiers
const uniqueNullifiers = new Set(nullifiers);
Expand All @@ -48,7 +46,8 @@ export class DoubleSpendTxValidator<T extends AnyTx> implements TxValidator<T> {
}

if (this.isValidatingBlock) {
for (const nullifier of nullifiers) {
// TODO: remove all this type casting
for (const nullifier of nullifiers.map(n => n.toBigInt())) {
if (thisBlockNullifiers.has(nullifier)) {
this.#log.warn(`Rejecting tx ${Tx.getHash(tx)} for repeating a nullifier in the same block`);
return false;
Expand All @@ -58,7 +57,7 @@ export class DoubleSpendTxValidator<T extends AnyTx> implements TxValidator<T> {
}
}

const nullifierIndexes = await Promise.all(nullifiers.map(n => this.#nullifierSource.getNullifierIndex(new Fr(n))));
const nullifierIndexes = await this.#nullifierSource.getNullifierIndices(nullifiers.map(n => n.toBuffer()));

const hasDuplicates = nullifierIndexes.some(index => index !== undefined);
if (hasDuplicates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class TxValidatorFactory {
private enforceFees: boolean,
) {
this.nullifierSource = {
getNullifierIndex: nullifier =>
this.committedDb.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]).then(x => x[0]),
getNullifierIndices: nullifiers =>
this.committedDb.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, nullifiers).then(x => x.filter(index => index !== undefined) as bigint[]),
};

this.publicStateSource = {
Expand All @@ -52,8 +52,8 @@ export class TxValidatorFactory {

validatorForProcessedTxs(fork: MerkleTreeReadOperations): TxValidator<ProcessedTx> {
return new DoubleSpendTxValidator({
getNullifierIndex: nullifier =>
fork.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]).then(x => x[0]),
getNullifierIndices: nullifiers =>
fork.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, nullifiers).then(x => x.filter(index => index !== undefined) as bigint[]),
});
}
}

0 comments on commit 075171f

Please sign in to comment.