-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab3f318
commit b598cf8
Showing
28 changed files
with
344 additions
and
345 deletions.
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
10 changes: 3 additions & 7 deletions
10
yarn-project/circuit-types/src/tx/validator/empty_validator.ts
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 |
---|---|---|
@@ -1,11 +1,7 @@ | ||
import { type AnyTx, type TxValidator } from './tx_validator.js'; | ||
import { type AnyTx, type TxValidationResult, type TxValidator } from './tx_validator.js'; | ||
|
||
export class EmptyTxValidator<T extends AnyTx = AnyTx> implements TxValidator<T> { | ||
public validateTxs(txs: T[]): Promise<[validTxs: T[], invalidTxs: T[], skippedTxs: T[]]> { | ||
return Promise.resolve([txs, [], []]); | ||
} | ||
|
||
public validateTx(_tx: T): Promise<boolean> { | ||
return Promise.resolve(true); | ||
public validateTx(_tx: T): Promise<TxValidationResult> { | ||
return Promise.resolve({ result: 'valid' }); | ||
} | ||
} |
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
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
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
54 changes: 10 additions & 44 deletions
54
yarn-project/p2p/src/msg_validators/tx_validator/double_spend_validator.ts
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 |
---|---|---|
@@ -1,69 +1,35 @@ | ||
import { type AnyTx, Tx, type TxValidator } from '@aztec/circuit-types'; | ||
import { type AnyTx, Tx, type TxValidationResult, type TxValidator } from '@aztec/circuit-types'; | ||
import { createLogger } from '@aztec/foundation/log'; | ||
|
||
export interface NullifierSource { | ||
getNullifierIndices: (nullifiers: Buffer[]) => Promise<(bigint | undefined)[]>; | ||
nullifierExists: (nullifier: Buffer) => Promise<boolean>; | ||
} | ||
|
||
export class DoubleSpendTxValidator<T extends AnyTx> implements TxValidator<T> { | ||
#log = createLogger('p2p:tx_validator:tx_double_spend'); | ||
#nullifierSource: NullifierSource; | ||
|
||
constructor(nullifierSource: NullifierSource, private readonly isValidatingBlock: boolean = true) { | ||
constructor(nullifierSource: NullifierSource) { | ||
this.#nullifierSource = nullifierSource; | ||
} | ||
|
||
async validateTxs(txs: T[]): Promise<[validTxs: T[], invalidTxs: T[]]> { | ||
const validTxs: T[] = []; | ||
const invalidTxs: T[] = []; | ||
const thisBlockNullifiers = new Set<bigint>(); | ||
|
||
for (const tx of txs) { | ||
if (!(await this.#uniqueNullifiers(tx, thisBlockNullifiers))) { | ||
invalidTxs.push(tx); | ||
continue; | ||
} | ||
|
||
validTxs.push(tx); | ||
} | ||
|
||
return [validTxs, invalidTxs]; | ||
} | ||
|
||
validateTx(tx: T): Promise<boolean> { | ||
return this.#uniqueNullifiers(tx, new Set<bigint>()); | ||
} | ||
|
||
async #uniqueNullifiers(tx: AnyTx, thisBlockNullifiers: Set<bigint>): Promise<boolean> { | ||
async validateTx(tx: T): Promise<TxValidationResult> { | ||
const nullifiers = tx instanceof Tx ? tx.data.getNonEmptyNullifiers() : tx.txEffect.nullifiers; | ||
|
||
// Ditch this tx if it has repeated nullifiers | ||
const uniqueNullifiers = new Set(nullifiers); | ||
if (uniqueNullifiers.size !== nullifiers.length) { | ||
this.#log.warn(`Rejecting tx ${Tx.getHash(tx)} for emitting duplicate nullifiers`); | ||
return false; | ||
return { result: 'invalid', reason: ['Duplicate nullifier in tx'] }; | ||
} | ||
|
||
if (this.isValidatingBlock) { | ||
for (const nullifier of nullifiers) { | ||
const nullifierBigInt = nullifier.toBigInt(); | ||
if (thisBlockNullifiers.has(nullifierBigInt)) { | ||
this.#log.warn(`Rejecting tx ${Tx.getHash(tx)} for repeating a nullifier in the same block`); | ||
return false; | ||
} | ||
|
||
thisBlockNullifiers.add(nullifierBigInt); | ||
for (const nullifier of nullifiers) { | ||
if (await this.#nullifierSource.nullifierExists(nullifier.toBuffer())) { | ||
this.#log.warn(`Rejecting tx ${Tx.getHash(tx)} for repeating a nullifier`); | ||
return { result: 'invalid', reason: ['Repeated nullifier'] }; | ||
} | ||
} | ||
|
||
const nullifierIndexes = await this.#nullifierSource.getNullifierIndices(nullifiers.map(n => n.toBuffer())); | ||
|
||
const hasDuplicates = nullifierIndexes.some(index => index !== undefined); | ||
if (hasDuplicates) { | ||
this.#log.warn(`Rejecting tx ${Tx.getHash(tx)} for repeating nullifiers present in state trees`); | ||
return false; | ||
} | ||
|
||
return true; | ||
return { result: 'valid' }; | ||
} | ||
} |
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.