@aztec/p2p 4.0.4-rc.7 → 4.0.4-rc.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts +5 -4
- package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts.map +1 -1
- package/dest/msg_validators/proposal_validator/block_proposal_validator.js +10 -2
- package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts +5 -4
- package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts.map +1 -1
- package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.js +16 -2
- package/dest/msg_validators/proposal_validator/proposal_validator.d.ts +12 -9
- package/dest/msg_validators/proposal_validator/proposal_validator.d.ts.map +1 -1
- package/dest/msg_validators/proposal_validator/proposal_validator.js +46 -44
- package/package.json +14 -14
- package/src/msg_validators/proposal_validator/block_proposal_validator.ts +13 -3
- package/src/msg_validators/proposal_validator/checkpoint_proposal_validator.ts +19 -6
- package/src/msg_validators/proposal_validator/proposal_validator.ts +57 -48
- package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts +0 -24
- package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts.map +0 -1
- package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.js +0 -381
- package/src/msg_validators/proposal_validator/proposal_validator_test_suite.ts +0 -379
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { EpochCacheInterface } from '@aztec/epoch-cache';
|
|
2
|
-
import type { BlockProposal, P2PValidator } from '@aztec/stdlib/p2p';
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import type { BlockProposal, P2PValidator, ValidationResult } from '@aztec/stdlib/p2p';
|
|
3
|
+
export declare class BlockProposalValidator implements P2PValidator<BlockProposal> {
|
|
4
|
+
private proposalValidator;
|
|
5
5
|
constructor(epochCache: EpochCacheInterface, opts: {
|
|
6
6
|
txsPermitted: boolean;
|
|
7
7
|
maxTxsPerBlock?: number;
|
|
8
8
|
});
|
|
9
|
+
validate(proposal: BlockProposal): Promise<ValidationResult>;
|
|
9
10
|
}
|
|
10
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
11
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmxvY2tfcHJvcG9zYWxfdmFsaWRhdG9yLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbXNnX3ZhbGlkYXRvcnMvcHJvcG9zYWxfdmFsaWRhdG9yL2Jsb2NrX3Byb3Bvc2FsX3ZhbGlkYXRvci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxtQkFBbUIsRUFBRSxNQUFNLG9CQUFvQixDQUFDO0FBQzlELE9BQU8sS0FBSyxFQUFFLGFBQWEsRUFBRSxZQUFZLEVBQUUsZ0JBQWdCLEVBQUUsTUFBTSxtQkFBbUIsQ0FBQztBQUl2RixxQkFBYSxzQkFBdUIsWUFBVyxZQUFZLENBQUMsYUFBYSxDQUFDO0lBQ3hFLE9BQU8sQ0FBQyxpQkFBaUIsQ0FBb0I7SUFFN0MsWUFBWSxVQUFVLEVBQUUsbUJBQW1CLEVBQUUsSUFBSSxFQUFFO1FBQUUsWUFBWSxFQUFFLE9BQU8sQ0FBQztRQUFDLGNBQWMsQ0FBQyxFQUFFLE1BQU0sQ0FBQTtLQUFFLEVBRXBHO0lBRUssUUFBUSxDQUFDLFFBQVEsRUFBRSxhQUFhLEdBQUcsT0FBTyxDQUFDLGdCQUFnQixDQUFDLENBTWpFO0NBQ0YifQ==
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block_proposal_validator.d.ts","sourceRoot":"","sources":["../../../src/msg_validators/proposal_validator/block_proposal_validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,
|
|
1
|
+
{"version":3,"file":"block_proposal_validator.d.ts","sourceRoot":"","sources":["../../../src/msg_validators/proposal_validator/block_proposal_validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAIvF,qBAAa,sBAAuB,YAAW,YAAY,CAAC,aAAa,CAAC;IACxE,OAAO,CAAC,iBAAiB,CAAoB;IAE7C,YAAY,UAAU,EAAE,mBAAmB,EAAE,IAAI,EAAE;QAAE,YAAY,EAAE,OAAO,CAAC;QAAC,cAAc,CAAC,EAAE,MAAM,CAAA;KAAE,EAEpG;IAEK,QAAQ,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAMjE;CACF"}
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { ProposalValidator } from '../proposal_validator/proposal_validator.js';
|
|
2
|
-
export class BlockProposalValidator
|
|
2
|
+
export class BlockProposalValidator {
|
|
3
|
+
proposalValidator;
|
|
3
4
|
constructor(epochCache, opts){
|
|
4
|
-
|
|
5
|
+
this.proposalValidator = new ProposalValidator(epochCache, opts, 'p2p:block_proposal_validator');
|
|
6
|
+
}
|
|
7
|
+
async validate(proposal) {
|
|
8
|
+
const headerResult = await this.proposalValidator.validate(proposal);
|
|
9
|
+
if (headerResult.result !== 'accept') {
|
|
10
|
+
return headerResult;
|
|
11
|
+
}
|
|
12
|
+
return this.proposalValidator.validateTxs(proposal);
|
|
5
13
|
}
|
|
6
14
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { EpochCacheInterface } from '@aztec/epoch-cache';
|
|
2
|
-
import type { CheckpointProposal, P2PValidator } from '@aztec/stdlib/p2p';
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import type { CheckpointProposal, P2PValidator, ValidationResult } from '@aztec/stdlib/p2p';
|
|
3
|
+
export declare class CheckpointProposalValidator implements P2PValidator<CheckpointProposal> {
|
|
4
|
+
private proposalValidator;
|
|
5
5
|
constructor(epochCache: EpochCacheInterface, opts: {
|
|
6
6
|
txsPermitted: boolean;
|
|
7
7
|
maxTxsPerBlock?: number;
|
|
8
8
|
});
|
|
9
|
+
validate(proposal: CheckpointProposal): Promise<ValidationResult>;
|
|
9
10
|
}
|
|
10
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
11
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2hlY2twb2ludF9wcm9wb3NhbF92YWxpZGF0b3IuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9tc2dfdmFsaWRhdG9ycy9wcm9wb3NhbF92YWxpZGF0b3IvY2hlY2twb2ludF9wcm9wb3NhbF92YWxpZGF0b3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLEVBQUUsbUJBQW1CLEVBQUUsTUFBTSxvQkFBb0IsQ0FBQztBQUM5RCxPQUFPLEtBQUssRUFBRSxrQkFBa0IsRUFBRSxZQUFZLEVBQUUsZ0JBQWdCLEVBQUUsTUFBTSxtQkFBbUIsQ0FBQztBQUk1RixxQkFBYSwyQkFBNEIsWUFBVyxZQUFZLENBQUMsa0JBQWtCLENBQUM7SUFDbEYsT0FBTyxDQUFDLGlCQUFpQixDQUFvQjtJQUU3QyxZQUFZLFVBQVUsRUFBRSxtQkFBbUIsRUFBRSxJQUFJLEVBQUU7UUFBRSxZQUFZLEVBQUUsT0FBTyxDQUFDO1FBQUMsY0FBYyxDQUFDLEVBQUUsTUFBTSxDQUFBO0tBQUUsRUFFcEc7SUFFSyxRQUFRLENBQUMsUUFBUSxFQUFFLGtCQUFrQixHQUFHLE9BQU8sQ0FBQyxnQkFBZ0IsQ0FBQyxDQVl0RTtDQUNGIn0=
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkpoint_proposal_validator.d.ts","sourceRoot":"","sources":["../../../src/msg_validators/proposal_validator/checkpoint_proposal_validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,KAAK,EAAE,kBAAkB,EAAE,YAAY,EAAE,
|
|
1
|
+
{"version":3,"file":"checkpoint_proposal_validator.d.ts","sourceRoot":"","sources":["../../../src/msg_validators/proposal_validator/checkpoint_proposal_validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,KAAK,EAAE,kBAAkB,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAI5F,qBAAa,2BAA4B,YAAW,YAAY,CAAC,kBAAkB,CAAC;IAClF,OAAO,CAAC,iBAAiB,CAAoB;IAE7C,YAAY,UAAU,EAAE,mBAAmB,EAAE,IAAI,EAAE;QAAE,YAAY,EAAE,OAAO,CAAC;QAAC,cAAc,CAAC,EAAE,MAAM,CAAA;KAAE,EAEpG;IAEK,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAYtE;CACF"}
|
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import { ProposalValidator } from '../proposal_validator/proposal_validator.js';
|
|
2
|
-
export class CheckpointProposalValidator
|
|
2
|
+
export class CheckpointProposalValidator {
|
|
3
|
+
proposalValidator;
|
|
3
4
|
constructor(epochCache, opts){
|
|
4
|
-
|
|
5
|
+
this.proposalValidator = new ProposalValidator(epochCache, opts, 'p2p:checkpoint_proposal_validator');
|
|
6
|
+
}
|
|
7
|
+
async validate(proposal) {
|
|
8
|
+
const headerResult = await this.proposalValidator.validate(proposal);
|
|
9
|
+
if (headerResult.result !== 'accept') {
|
|
10
|
+
return headerResult;
|
|
11
|
+
}
|
|
12
|
+
const blockProposal = proposal.getBlockProposal();
|
|
13
|
+
if (blockProposal) {
|
|
14
|
+
return this.proposalValidator.validateTxs(blockProposal);
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
result: 'accept'
|
|
18
|
+
};
|
|
5
19
|
}
|
|
6
20
|
}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import type { EpochCacheInterface } from '@aztec/epoch-cache';
|
|
2
|
-
import { type
|
|
3
|
-
|
|
4
|
-
export declare
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
import { type BlockProposal, type CheckpointProposalCore, type ValidationResult } from '@aztec/stdlib/p2p';
|
|
3
|
+
/** Validates header-level and tx-level fields of block and checkpoint proposals. */
|
|
4
|
+
export declare class ProposalValidator {
|
|
5
|
+
private epochCache;
|
|
6
|
+
private logger;
|
|
7
|
+
private txsPermitted;
|
|
8
|
+
private maxTxsPerBlock?;
|
|
9
9
|
constructor(epochCache: EpochCacheInterface, opts: {
|
|
10
10
|
txsPermitted: boolean;
|
|
11
11
|
maxTxsPerBlock?: number;
|
|
12
12
|
}, loggerName: string);
|
|
13
|
-
|
|
13
|
+
/** Validates header-level fields: slot, signature, and proposer. */
|
|
14
|
+
validate(proposal: BlockProposal | CheckpointProposalCore): Promise<ValidationResult>;
|
|
15
|
+
/** Validates transaction-related fields of a block proposal. */
|
|
16
|
+
validateTxs(proposal: BlockProposal): Promise<ValidationResult>;
|
|
14
17
|
}
|
|
15
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
18
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHJvcG9zYWxfdmFsaWRhdG9yLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbXNnX3ZhbGlkYXRvcnMvcHJvcG9zYWxfdmFsaWRhdG9yL3Byb3Bvc2FsX3ZhbGlkYXRvci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxtQkFBbUIsRUFBRSxNQUFNLG9CQUFvQixDQUFDO0FBRzlELE9BQU8sRUFDTCxLQUFLLGFBQWEsRUFDbEIsS0FBSyxzQkFBc0IsRUFFM0IsS0FBSyxnQkFBZ0IsRUFDdEIsTUFBTSxtQkFBbUIsQ0FBQztBQUkzQixvRkFBb0Y7QUFDcEYscUJBQWEsaUJBQWlCO0lBQzVCLE9BQU8sQ0FBQyxVQUFVLENBQXNCO0lBQ3hDLE9BQU8sQ0FBQyxNQUFNLENBQVM7SUFDdkIsT0FBTyxDQUFDLFlBQVksQ0FBVTtJQUM5QixPQUFPLENBQUMsY0FBYyxDQUFDLENBQVM7SUFFaEMsWUFDRSxVQUFVLEVBQUUsbUJBQW1CLEVBQy9CLElBQUksRUFBRTtRQUFFLFlBQVksRUFBRSxPQUFPLENBQUM7UUFBQyxjQUFjLENBQUMsRUFBRSxNQUFNLENBQUE7S0FBRSxFQUN4RCxVQUFVLEVBQUUsTUFBTSxFQU1uQjtJQUVELG9FQUFvRTtJQUN2RCxRQUFRLENBQUMsUUFBUSxFQUFFLGFBQWEsR0FBRyxzQkFBc0IsR0FBRyxPQUFPLENBQUMsZ0JBQWdCLENBQUMsQ0F1Q2pHO0lBRUQsZ0VBQWdFO0lBQ25ELFdBQVcsQ0FBQyxRQUFRLEVBQUUsYUFBYSxHQUFHLE9BQU8sQ0FBQyxnQkFBZ0IsQ0FBQyxDQXdDM0U7Q0FDRiJ9
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proposal_validator.d.ts","sourceRoot":"","sources":["../../../src/msg_validators/proposal_validator/proposal_validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"proposal_validator.d.ts","sourceRoot":"","sources":["../../../src/msg_validators/proposal_validator/proposal_validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAG9D,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,sBAAsB,EAE3B,KAAK,gBAAgB,EACtB,MAAM,mBAAmB,CAAC;AAI3B,oFAAoF;AACpF,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,UAAU,CAAsB;IACxC,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,YAAY,CAAU;IAC9B,OAAO,CAAC,cAAc,CAAC,CAAS;IAEhC,YACE,UAAU,EAAE,mBAAmB,EAC/B,IAAI,EAAE;QAAE,YAAY,EAAE,OAAO,CAAC;QAAC,cAAc,CAAC,EAAE,MAAM,CAAA;KAAE,EACxD,UAAU,EAAE,MAAM,EAMnB;IAED,oEAAoE;IACvD,QAAQ,CAAC,QAAQ,EAAE,aAAa,GAAG,sBAAsB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAuCjG;IAED,gEAAgE;IACnD,WAAW,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAwC3E;CACF"}
|
|
@@ -2,7 +2,7 @@ import { NoCommitteeError } from '@aztec/ethereum/contracts';
|
|
|
2
2
|
import { createLogger } from '@aztec/foundation/log';
|
|
3
3
|
import { PeerErrorSeverity } from '@aztec/stdlib/p2p';
|
|
4
4
|
import { isWithinClockTolerance } from '../clock_tolerance.js';
|
|
5
|
-
export class ProposalValidator {
|
|
5
|
+
/** Validates header-level and tx-level fields of block and checkpoint proposals. */ export class ProposalValidator {
|
|
6
6
|
epochCache;
|
|
7
7
|
logger;
|
|
8
8
|
txsPermitted;
|
|
@@ -13,7 +13,7 @@ export class ProposalValidator {
|
|
|
13
13
|
this.maxTxsPerBlock = opts.maxTxsPerBlock;
|
|
14
14
|
this.logger = createLogger(loggerName);
|
|
15
15
|
}
|
|
16
|
-
async validate(proposal) {
|
|
16
|
+
/** Validates header-level fields: slot, signature, and proposer. */ async validate(proposal) {
|
|
17
17
|
try {
|
|
18
18
|
// Slot check
|
|
19
19
|
const { currentSlot, nextSlot } = this.epochCache.getCurrentAndNextSlot();
|
|
@@ -44,37 +44,6 @@ export class ProposalValidator {
|
|
|
44
44
|
severity: PeerErrorSeverity.MidToleranceError
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
|
-
// Transactions permitted check
|
|
48
|
-
const embeddedTxCount = proposal.txs?.length ?? 0;
|
|
49
|
-
if (!this.txsPermitted && (proposal.txHashes.length > 0 || embeddedTxCount > 0)) {
|
|
50
|
-
this.logger.warn(`Penalizing peer for proposal with ${proposal.txHashes.length} transaction(s) when transactions are not permitted`);
|
|
51
|
-
return {
|
|
52
|
-
result: 'reject',
|
|
53
|
-
severity: PeerErrorSeverity.MidToleranceError
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
// Max txs per block check
|
|
57
|
-
if (this.maxTxsPerBlock !== undefined && proposal.txHashes.length > this.maxTxsPerBlock) {
|
|
58
|
-
this.logger.warn(`Penalizing peer for proposal with ${proposal.txHashes.length} transaction(s) when max is ${this.maxTxsPerBlock}`);
|
|
59
|
-
return {
|
|
60
|
-
result: 'reject',
|
|
61
|
-
severity: PeerErrorSeverity.MidToleranceError
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
// Embedded txs must be listed in txHashes
|
|
65
|
-
const hashSet = new Set(proposal.txHashes.map((h)=>h.toString()));
|
|
66
|
-
const missingTxHashes = embeddedTxCount > 0 ? proposal.txs.filter((tx)=>!hashSet.has(tx.getTxHash().toString())).map((tx)=>tx.getTxHash().toString()) : [];
|
|
67
|
-
if (embeddedTxCount > 0 && missingTxHashes.length > 0) {
|
|
68
|
-
this.logger.warn('Penalizing peer for embedded transaction(s) not included in txHashes', {
|
|
69
|
-
embeddedTxCount,
|
|
70
|
-
txHashesLength: proposal.txHashes.length,
|
|
71
|
-
missingTxHashes
|
|
72
|
-
});
|
|
73
|
-
return {
|
|
74
|
-
result: 'reject',
|
|
75
|
-
severity: PeerErrorSeverity.MidToleranceError
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
47
|
// Proposer check
|
|
79
48
|
const expectedProposer = await this.epochCache.getProposerAttesterAddressInSlot(slotNumber);
|
|
80
49
|
if (expectedProposer !== undefined && !proposer.equals(expectedProposer)) {
|
|
@@ -87,17 +56,6 @@ export class ProposalValidator {
|
|
|
87
56
|
severity: PeerErrorSeverity.MidToleranceError
|
|
88
57
|
};
|
|
89
58
|
}
|
|
90
|
-
// Validate tx hashes for all txs embedded in the proposal
|
|
91
|
-
if (!(await Promise.all(proposal.txs?.map((tx)=>tx.validateTxHash()) ?? [])).every((v)=>v)) {
|
|
92
|
-
this.logger.warn(`Penalizing peer for invalid tx hashes in proposal`, {
|
|
93
|
-
proposer,
|
|
94
|
-
slotNumber
|
|
95
|
-
});
|
|
96
|
-
return {
|
|
97
|
-
result: 'reject',
|
|
98
|
-
severity: PeerErrorSeverity.LowToleranceError
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
59
|
return {
|
|
102
60
|
result: 'accept'
|
|
103
61
|
};
|
|
@@ -111,4 +69,48 @@ export class ProposalValidator {
|
|
|
111
69
|
throw e;
|
|
112
70
|
}
|
|
113
71
|
}
|
|
72
|
+
/** Validates transaction-related fields of a block proposal. */ async validateTxs(proposal) {
|
|
73
|
+
// Transactions permitted check
|
|
74
|
+
const embeddedTxCount = proposal.txs?.length ?? 0;
|
|
75
|
+
if (!this.txsPermitted && (proposal.txHashes.length > 0 || embeddedTxCount > 0)) {
|
|
76
|
+
this.logger.warn(`Penalizing peer for proposal with ${proposal.txHashes.length} transaction(s) when transactions are not permitted`);
|
|
77
|
+
return {
|
|
78
|
+
result: 'reject',
|
|
79
|
+
severity: PeerErrorSeverity.MidToleranceError
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
// Max txs per block check
|
|
83
|
+
if (this.maxTxsPerBlock !== undefined && proposal.txHashes.length > this.maxTxsPerBlock) {
|
|
84
|
+
this.logger.warn(`Penalizing peer for proposal with ${proposal.txHashes.length} transaction(s) when max is ${this.maxTxsPerBlock}`);
|
|
85
|
+
return {
|
|
86
|
+
result: 'reject',
|
|
87
|
+
severity: PeerErrorSeverity.MidToleranceError
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// Embedded txs must be listed in txHashes
|
|
91
|
+
const hashSet = new Set(proposal.txHashes.map((h)=>h.toString()));
|
|
92
|
+
const missingTxHashes = embeddedTxCount > 0 ? proposal.txs.filter((tx)=>!hashSet.has(tx.getTxHash().toString())).map((tx)=>tx.getTxHash().toString()) : [];
|
|
93
|
+
if (embeddedTxCount > 0 && missingTxHashes.length > 0) {
|
|
94
|
+
this.logger.warn('Penalizing peer for embedded transaction(s) not included in txHashes', {
|
|
95
|
+
embeddedTxCount,
|
|
96
|
+
txHashesLength: proposal.txHashes.length,
|
|
97
|
+
missingTxHashes
|
|
98
|
+
});
|
|
99
|
+
return {
|
|
100
|
+
result: 'reject',
|
|
101
|
+
severity: PeerErrorSeverity.MidToleranceError
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
// Validate tx hashes for all txs embedded in the proposal
|
|
105
|
+
if (!(await Promise.all(proposal.txs?.map((tx)=>tx.validateTxHash()) ?? [])).every((v)=>v)) {
|
|
106
|
+
this.logger.warn(`Penalizing peer for invalid tx hashes in proposal`);
|
|
107
|
+
return {
|
|
108
|
+
result: 'reject',
|
|
109
|
+
severity: PeerErrorSeverity.LowToleranceError
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
result: 'accept'
|
|
114
|
+
};
|
|
115
|
+
}
|
|
114
116
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/p2p",
|
|
3
|
-
"version": "4.0.4-rc.
|
|
3
|
+
"version": "4.0.4-rc.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dest/index.js",
|
|
@@ -67,17 +67,17 @@
|
|
|
67
67
|
]
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@aztec/constants": "4.0.4-rc.
|
|
71
|
-
"@aztec/epoch-cache": "4.0.4-rc.
|
|
72
|
-
"@aztec/ethereum": "4.0.4-rc.
|
|
73
|
-
"@aztec/foundation": "4.0.4-rc.
|
|
74
|
-
"@aztec/kv-store": "4.0.4-rc.
|
|
75
|
-
"@aztec/noir-contracts.js": "4.0.4-rc.
|
|
76
|
-
"@aztec/noir-protocol-circuits-types": "4.0.4-rc.
|
|
77
|
-
"@aztec/protocol-contracts": "4.0.4-rc.
|
|
78
|
-
"@aztec/simulator": "4.0.4-rc.
|
|
79
|
-
"@aztec/stdlib": "4.0.4-rc.
|
|
80
|
-
"@aztec/telemetry-client": "4.0.4-rc.
|
|
70
|
+
"@aztec/constants": "4.0.4-rc.8",
|
|
71
|
+
"@aztec/epoch-cache": "4.0.4-rc.8",
|
|
72
|
+
"@aztec/ethereum": "4.0.4-rc.8",
|
|
73
|
+
"@aztec/foundation": "4.0.4-rc.8",
|
|
74
|
+
"@aztec/kv-store": "4.0.4-rc.8",
|
|
75
|
+
"@aztec/noir-contracts.js": "4.0.4-rc.8",
|
|
76
|
+
"@aztec/noir-protocol-circuits-types": "4.0.4-rc.8",
|
|
77
|
+
"@aztec/protocol-contracts": "4.0.4-rc.8",
|
|
78
|
+
"@aztec/simulator": "4.0.4-rc.8",
|
|
79
|
+
"@aztec/stdlib": "4.0.4-rc.8",
|
|
80
|
+
"@aztec/telemetry-client": "4.0.4-rc.8",
|
|
81
81
|
"@chainsafe/libp2p-gossipsub": "13.0.0",
|
|
82
82
|
"@chainsafe/libp2p-noise": "^15.0.0",
|
|
83
83
|
"@chainsafe/libp2p-yamux": "^6.0.2",
|
|
@@ -104,8 +104,8 @@
|
|
|
104
104
|
"xxhash-wasm": "^1.1.0"
|
|
105
105
|
},
|
|
106
106
|
"devDependencies": {
|
|
107
|
-
"@aztec/archiver": "4.0.4-rc.
|
|
108
|
-
"@aztec/world-state": "4.0.4-rc.
|
|
107
|
+
"@aztec/archiver": "4.0.4-rc.8",
|
|
108
|
+
"@aztec/world-state": "4.0.4-rc.8",
|
|
109
109
|
"@jest/globals": "^30.0.0",
|
|
110
110
|
"@types/jest": "^30.0.0",
|
|
111
111
|
"@types/node": "^22.15.17",
|
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import type { EpochCacheInterface } from '@aztec/epoch-cache';
|
|
2
|
-
import type { BlockProposal, P2PValidator } from '@aztec/stdlib/p2p';
|
|
2
|
+
import type { BlockProposal, P2PValidator, ValidationResult } from '@aztec/stdlib/p2p';
|
|
3
3
|
|
|
4
4
|
import { ProposalValidator } from '../proposal_validator/proposal_validator.js';
|
|
5
5
|
|
|
6
|
-
export class BlockProposalValidator
|
|
6
|
+
export class BlockProposalValidator implements P2PValidator<BlockProposal> {
|
|
7
|
+
private proposalValidator: ProposalValidator;
|
|
8
|
+
|
|
7
9
|
constructor(epochCache: EpochCacheInterface, opts: { txsPermitted: boolean; maxTxsPerBlock?: number }) {
|
|
8
|
-
|
|
10
|
+
this.proposalValidator = new ProposalValidator(epochCache, opts, 'p2p:block_proposal_validator');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async validate(proposal: BlockProposal): Promise<ValidationResult> {
|
|
14
|
+
const headerResult = await this.proposalValidator.validate(proposal);
|
|
15
|
+
if (headerResult.result !== 'accept') {
|
|
16
|
+
return headerResult;
|
|
17
|
+
}
|
|
18
|
+
return this.proposalValidator.validateTxs(proposal);
|
|
9
19
|
}
|
|
10
20
|
}
|
|
@@ -1,13 +1,26 @@
|
|
|
1
1
|
import type { EpochCacheInterface } from '@aztec/epoch-cache';
|
|
2
|
-
import type { CheckpointProposal, P2PValidator } from '@aztec/stdlib/p2p';
|
|
2
|
+
import type { CheckpointProposal, P2PValidator, ValidationResult } from '@aztec/stdlib/p2p';
|
|
3
3
|
|
|
4
4
|
import { ProposalValidator } from '../proposal_validator/proposal_validator.js';
|
|
5
5
|
|
|
6
|
-
export class CheckpointProposalValidator
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
{
|
|
6
|
+
export class CheckpointProposalValidator implements P2PValidator<CheckpointProposal> {
|
|
7
|
+
private proposalValidator: ProposalValidator;
|
|
8
|
+
|
|
10
9
|
constructor(epochCache: EpochCacheInterface, opts: { txsPermitted: boolean; maxTxsPerBlock?: number }) {
|
|
11
|
-
|
|
10
|
+
this.proposalValidator = new ProposalValidator(epochCache, opts, 'p2p:checkpoint_proposal_validator');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async validate(proposal: CheckpointProposal): Promise<ValidationResult> {
|
|
14
|
+
const headerResult = await this.proposalValidator.validate(proposal);
|
|
15
|
+
if (headerResult.result !== 'accept') {
|
|
16
|
+
return headerResult;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const blockProposal = proposal.getBlockProposal();
|
|
20
|
+
if (blockProposal) {
|
|
21
|
+
return this.proposalValidator.validateTxs(blockProposal);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return { result: 'accept' };
|
|
12
25
|
}
|
|
13
26
|
}
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import type { EpochCacheInterface } from '@aztec/epoch-cache';
|
|
2
2
|
import { NoCommitteeError } from '@aztec/ethereum/contracts';
|
|
3
3
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
type BlockProposal,
|
|
6
|
+
type CheckpointProposalCore,
|
|
7
|
+
PeerErrorSeverity,
|
|
8
|
+
type ValidationResult,
|
|
9
|
+
} from '@aztec/stdlib/p2p';
|
|
5
10
|
|
|
6
11
|
import { isWithinClockTolerance } from '../clock_tolerance.js';
|
|
7
12
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
/** Validates header-level and tx-level fields of block and checkpoint proposals. */
|
|
14
|
+
export class ProposalValidator {
|
|
15
|
+
private epochCache: EpochCacheInterface;
|
|
16
|
+
private logger: Logger;
|
|
17
|
+
private txsPermitted: boolean;
|
|
18
|
+
private maxTxsPerBlock?: number;
|
|
13
19
|
|
|
14
20
|
constructor(
|
|
15
21
|
epochCache: EpochCacheInterface,
|
|
@@ -22,7 +28,8 @@ export abstract class ProposalValidator<TProposal extends BlockProposal | Checkp
|
|
|
22
28
|
this.logger = createLogger(loggerName);
|
|
23
29
|
}
|
|
24
30
|
|
|
25
|
-
|
|
31
|
+
/** Validates header-level fields: slot, signature, and proposer. */
|
|
32
|
+
public async validate(proposal: BlockProposal | CheckpointProposalCore): Promise<ValidationResult> {
|
|
26
33
|
try {
|
|
27
34
|
// Slot check
|
|
28
35
|
const { currentSlot, nextSlot } = this.epochCache.getCurrentAndNextSlot();
|
|
@@ -44,38 +51,6 @@ export abstract class ProposalValidator<TProposal extends BlockProposal | Checkp
|
|
|
44
51
|
return { result: 'reject', severity: PeerErrorSeverity.MidToleranceError };
|
|
45
52
|
}
|
|
46
53
|
|
|
47
|
-
// Transactions permitted check
|
|
48
|
-
const embeddedTxCount = proposal.txs?.length ?? 0;
|
|
49
|
-
if (!this.txsPermitted && (proposal.txHashes.length > 0 || embeddedTxCount > 0)) {
|
|
50
|
-
this.logger.warn(
|
|
51
|
-
`Penalizing peer for proposal with ${proposal.txHashes.length} transaction(s) when transactions are not permitted`,
|
|
52
|
-
);
|
|
53
|
-
return { result: 'reject', severity: PeerErrorSeverity.MidToleranceError };
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Max txs per block check
|
|
57
|
-
if (this.maxTxsPerBlock !== undefined && proposal.txHashes.length > this.maxTxsPerBlock) {
|
|
58
|
-
this.logger.warn(
|
|
59
|
-
`Penalizing peer for proposal with ${proposal.txHashes.length} transaction(s) when max is ${this.maxTxsPerBlock}`,
|
|
60
|
-
);
|
|
61
|
-
return { result: 'reject', severity: PeerErrorSeverity.MidToleranceError };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Embedded txs must be listed in txHashes
|
|
65
|
-
const hashSet = new Set(proposal.txHashes.map(h => h.toString()));
|
|
66
|
-
const missingTxHashes =
|
|
67
|
-
embeddedTxCount > 0
|
|
68
|
-
? proposal.txs!.filter(tx => !hashSet.has(tx.getTxHash().toString())).map(tx => tx.getTxHash().toString())
|
|
69
|
-
: [];
|
|
70
|
-
if (embeddedTxCount > 0 && missingTxHashes.length > 0) {
|
|
71
|
-
this.logger.warn('Penalizing peer for embedded transaction(s) not included in txHashes', {
|
|
72
|
-
embeddedTxCount,
|
|
73
|
-
txHashesLength: proposal.txHashes.length,
|
|
74
|
-
missingTxHashes,
|
|
75
|
-
});
|
|
76
|
-
return { result: 'reject', severity: PeerErrorSeverity.MidToleranceError };
|
|
77
|
-
}
|
|
78
|
-
|
|
79
54
|
// Proposer check
|
|
80
55
|
const expectedProposer = await this.epochCache.getProposerAttesterAddressInSlot(slotNumber);
|
|
81
56
|
if (expectedProposer !== undefined && !proposer.equals(expectedProposer)) {
|
|
@@ -86,15 +61,6 @@ export abstract class ProposalValidator<TProposal extends BlockProposal | Checkp
|
|
|
86
61
|
return { result: 'reject', severity: PeerErrorSeverity.MidToleranceError };
|
|
87
62
|
}
|
|
88
63
|
|
|
89
|
-
// Validate tx hashes for all txs embedded in the proposal
|
|
90
|
-
if (!(await Promise.all(proposal.txs?.map(tx => tx.validateTxHash()) ?? [])).every(v => v)) {
|
|
91
|
-
this.logger.warn(`Penalizing peer for invalid tx hashes in proposal`, {
|
|
92
|
-
proposer,
|
|
93
|
-
slotNumber,
|
|
94
|
-
});
|
|
95
|
-
return { result: 'reject', severity: PeerErrorSeverity.LowToleranceError };
|
|
96
|
-
}
|
|
97
|
-
|
|
98
64
|
return { result: 'accept' };
|
|
99
65
|
} catch (e) {
|
|
100
66
|
if (e instanceof NoCommitteeError) {
|
|
@@ -103,4 +69,47 @@ export abstract class ProposalValidator<TProposal extends BlockProposal | Checkp
|
|
|
103
69
|
throw e;
|
|
104
70
|
}
|
|
105
71
|
}
|
|
72
|
+
|
|
73
|
+
/** Validates transaction-related fields of a block proposal. */
|
|
74
|
+
public async validateTxs(proposal: BlockProposal): Promise<ValidationResult> {
|
|
75
|
+
// Transactions permitted check
|
|
76
|
+
const embeddedTxCount = proposal.txs?.length ?? 0;
|
|
77
|
+
if (!this.txsPermitted && (proposal.txHashes.length > 0 || embeddedTxCount > 0)) {
|
|
78
|
+
this.logger.warn(
|
|
79
|
+
`Penalizing peer for proposal with ${proposal.txHashes.length} transaction(s) when transactions are not permitted`,
|
|
80
|
+
);
|
|
81
|
+
return { result: 'reject', severity: PeerErrorSeverity.MidToleranceError };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Max txs per block check
|
|
85
|
+
if (this.maxTxsPerBlock !== undefined && proposal.txHashes.length > this.maxTxsPerBlock) {
|
|
86
|
+
this.logger.warn(
|
|
87
|
+
`Penalizing peer for proposal with ${proposal.txHashes.length} transaction(s) when max is ${this.maxTxsPerBlock}`,
|
|
88
|
+
);
|
|
89
|
+
return { result: 'reject', severity: PeerErrorSeverity.MidToleranceError };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Embedded txs must be listed in txHashes
|
|
93
|
+
const hashSet = new Set(proposal.txHashes.map(h => h.toString()));
|
|
94
|
+
const missingTxHashes =
|
|
95
|
+
embeddedTxCount > 0
|
|
96
|
+
? proposal.txs!.filter(tx => !hashSet.has(tx.getTxHash().toString())).map(tx => tx.getTxHash().toString())
|
|
97
|
+
: [];
|
|
98
|
+
if (embeddedTxCount > 0 && missingTxHashes.length > 0) {
|
|
99
|
+
this.logger.warn('Penalizing peer for embedded transaction(s) not included in txHashes', {
|
|
100
|
+
embeddedTxCount,
|
|
101
|
+
txHashesLength: proposal.txHashes.length,
|
|
102
|
+
missingTxHashes,
|
|
103
|
+
});
|
|
104
|
+
return { result: 'reject', severity: PeerErrorSeverity.MidToleranceError };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Validate tx hashes for all txs embedded in the proposal
|
|
108
|
+
if (!(await Promise.all(proposal.txs?.map(tx => tx.validateTxHash()) ?? [])).every(v => v)) {
|
|
109
|
+
this.logger.warn(`Penalizing peer for invalid tx hashes in proposal`);
|
|
110
|
+
return { result: 'reject', severity: PeerErrorSeverity.LowToleranceError };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return { result: 'accept' };
|
|
114
|
+
}
|
|
106
115
|
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { EpochCacheInterface } from '@aztec/epoch-cache';
|
|
2
|
-
import type { Secp256k1Signer } from '@aztec/foundation/crypto/secp256k1-signer';
|
|
3
|
-
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
4
|
-
import { type BlockProposal, type CheckpointProposal, type ValidationResult } from '@aztec/stdlib/p2p';
|
|
5
|
-
import type { TxHash } from '@aztec/stdlib/tx';
|
|
6
|
-
import type { MockProxy } from 'jest-mock-extended';
|
|
7
|
-
export interface ProposalValidatorTestParams<TProposal extends BlockProposal | CheckpointProposal> {
|
|
8
|
-
validatorFactory: (epochCache: EpochCacheInterface, opts: {
|
|
9
|
-
txsPermitted: boolean;
|
|
10
|
-
maxTxsPerBlock?: number;
|
|
11
|
-
}) => {
|
|
12
|
-
validate: (proposal: TProposal) => Promise<ValidationResult>;
|
|
13
|
-
};
|
|
14
|
-
makeProposal: (options?: any) => Promise<TProposal>;
|
|
15
|
-
makeHeader: (epochNumber: number | bigint, slotNumber: number | bigint, blockNumber: number | bigint) => any;
|
|
16
|
-
getSigner: () => Secp256k1Signer;
|
|
17
|
-
getAddress: (signer?: Secp256k1Signer) => EthAddress;
|
|
18
|
-
getSlot: (slot: number | bigint) => any;
|
|
19
|
-
getTxHashes: (n: number) => TxHash[];
|
|
20
|
-
getTxs: () => any[];
|
|
21
|
-
epochCacheMock: () => MockProxy<EpochCacheInterface>;
|
|
22
|
-
}
|
|
23
|
-
export declare function sharedProposalValidatorTests<TProposal extends BlockProposal | CheckpointProposal>(params: ProposalValidatorTestParams<TProposal>): void;
|
|
24
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHJvcG9zYWxfdmFsaWRhdG9yX3Rlc3Rfc3VpdGUuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9tc2dfdmFsaWRhdG9ycy9wcm9wb3NhbF92YWxpZGF0b3IvcHJvcG9zYWxfdmFsaWRhdG9yX3Rlc3Rfc3VpdGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLEVBQUUsbUJBQW1CLEVBQUUsTUFBTSxvQkFBb0IsQ0FBQztBQUU5RCxPQUFPLEtBQUssRUFBRSxlQUFlLEVBQUUsTUFBTSwyQ0FBMkMsQ0FBQztBQUNqRixPQUFPLEtBQUssRUFBRSxVQUFVLEVBQUUsTUFBTSwrQkFBK0IsQ0FBQztBQUNoRSxPQUFPLEVBQ0wsS0FBSyxhQUFhLEVBQ2xCLEtBQUssa0JBQWtCLEVBRXZCLEtBQUssZ0JBQWdCLEVBQ3RCLE1BQU0sbUJBQW1CLENBQUM7QUFDM0IsT0FBTyxLQUFLLEVBQUUsTUFBTSxFQUFFLE1BQU0sa0JBQWtCLENBQUM7QUFHL0MsT0FBTyxLQUFLLEVBQUUsU0FBUyxFQUFFLE1BQU0sb0JBQW9CLENBQUM7QUFFcEQsTUFBTSxXQUFXLDJCQUEyQixDQUFDLFNBQVMsU0FBUyxhQUFhLEdBQUcsa0JBQWtCO0lBQy9GLGdCQUFnQixFQUFFLENBQ2hCLFVBQVUsRUFBRSxtQkFBbUIsRUFDL0IsSUFBSSxFQUFFO1FBQUUsWUFBWSxFQUFFLE9BQU8sQ0FBQztRQUFDLGNBQWMsQ0FBQyxFQUFFLE1BQU0sQ0FBQTtLQUFFLEtBQ3JEO1FBQUUsUUFBUSxFQUFFLENBQUMsUUFBUSxFQUFFLFNBQVMsS0FBSyxPQUFPLENBQUMsZ0JBQWdCLENBQUMsQ0FBQTtLQUFFLENBQUM7SUFDdEUsWUFBWSxFQUFFLENBQUMsT0FBTyxDQUFDLEVBQUUsR0FBRyxLQUFLLE9BQU8sQ0FBQyxTQUFTLENBQUMsQ0FBQztJQUNwRCxVQUFVLEVBQUUsQ0FBQyxXQUFXLEVBQUUsTUFBTSxHQUFHLE1BQU0sRUFBRSxVQUFVLEVBQUUsTUFBTSxHQUFHLE1BQU0sRUFBRSxXQUFXLEVBQUUsTUFBTSxHQUFHLE1BQU0sS0FBSyxHQUFHLENBQUM7SUFDN0csU0FBUyxFQUFFLE1BQU0sZUFBZSxDQUFDO0lBQ2pDLFVBQVUsRUFBRSxDQUFDLE1BQU0sQ0FBQyxFQUFFLGVBQWUsS0FBSyxVQUFVLENBQUM7SUFDckQsT0FBTyxFQUFFLENBQUMsSUFBSSxFQUFFLE1BQU0sR0FBRyxNQUFNLEtBQUssR0FBRyxDQUFDO0lBQ3hDLFdBQVcsRUFBRSxDQUFDLENBQUMsRUFBRSxNQUFNLEtBQUssTUFBTSxFQUFFLENBQUM7SUFDckMsTUFBTSxFQUFFLE1BQU0sR0FBRyxFQUFFLENBQUM7SUFDcEIsY0FBYyxFQUFFLE1BQU0sU0FBUyxDQUFDLG1CQUFtQixDQUFDLENBQUM7Q0FDdEQ7QUFFRCx3QkFBZ0IsNEJBQTRCLENBQUMsU0FBUyxTQUFTLGFBQWEsR0FBRyxrQkFBa0IsRUFDL0YsTUFBTSxFQUFFLDJCQUEyQixDQUFDLFNBQVMsQ0FBQyxRQTJWL0MifQ==
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"proposal_validator_test_suite.d.ts","sourceRoot":"","sources":["../../../src/msg_validators/proposal_validator/proposal_validator_test_suite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAE9D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,kBAAkB,EAEvB,KAAK,gBAAgB,EACtB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEpD,MAAM,WAAW,2BAA2B,CAAC,SAAS,SAAS,aAAa,GAAG,kBAAkB;IAC/F,gBAAgB,EAAE,CAChB,UAAU,EAAE,mBAAmB,EAC/B,IAAI,EAAE;QAAE,YAAY,EAAE,OAAO,CAAC;QAAC,cAAc,CAAC,EAAE,MAAM,CAAA;KAAE,KACrD;QAAE,QAAQ,EAAE,CAAC,QAAQ,EAAE,SAAS,KAAK,OAAO,CAAC,gBAAgB,CAAC,CAAA;KAAE,CAAC;IACtE,YAAY,EAAE,CAAC,OAAO,CAAC,EAAE,GAAG,KAAK,OAAO,CAAC,SAAS,CAAC,CAAC;IACpD,UAAU,EAAE,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,KAAK,GAAG,CAAC;IAC7G,SAAS,EAAE,MAAM,eAAe,CAAC;IACjC,UAAU,EAAE,CAAC,MAAM,CAAC,EAAE,eAAe,KAAK,UAAU,CAAC;IACrD,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,KAAK,GAAG,CAAC;IACxC,WAAW,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,EAAE,CAAC;IACrC,MAAM,EAAE,MAAM,GAAG,EAAE,CAAC;IACpB,cAAc,EAAE,MAAM,SAAS,CAAC,mBAAmB,CAAC,CAAC;CACtD;AAED,wBAAgB,4BAA4B,CAAC,SAAS,SAAS,aAAa,GAAG,kBAAkB,EAC/F,MAAM,EAAE,2BAA2B,CAAC,SAAS,CAAC,QA2V/C"}
|
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
import { NoCommitteeError } from '@aztec/ethereum/contracts';
|
|
2
|
-
import { PeerErrorSeverity } from '@aztec/stdlib/p2p';
|
|
3
|
-
import { jest } from '@jest/globals';
|
|
4
|
-
export function sharedProposalValidatorTests(params) {
|
|
5
|
-
const { validatorFactory, makeProposal, makeHeader, getSigner, getAddress, getSlot, getTxHashes, epochCacheMock } = params;
|
|
6
|
-
describe('shared proposal validation logic', ()=>{
|
|
7
|
-
let epochCache;
|
|
8
|
-
let validator;
|
|
9
|
-
const previousSlot = getSlot(99);
|
|
10
|
-
const currentSlot = getSlot(100);
|
|
11
|
-
const nextSlot = getSlot(101);
|
|
12
|
-
function mockGetProposer(currentProposer, nextProposer, previousProposer) {
|
|
13
|
-
epochCache.getProposerAttesterAddressInSlot.mockImplementation((slot)=>{
|
|
14
|
-
if (slot === currentSlot) {
|
|
15
|
-
return Promise.resolve(currentProposer);
|
|
16
|
-
}
|
|
17
|
-
if (slot === nextSlot) {
|
|
18
|
-
return Promise.resolve(nextProposer);
|
|
19
|
-
}
|
|
20
|
-
if (slot === previousSlot && previousProposer) {
|
|
21
|
-
return Promise.resolve(previousProposer);
|
|
22
|
-
}
|
|
23
|
-
throw new Error('Unexpected argument');
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
beforeEach(()=>{
|
|
27
|
-
epochCache = epochCacheMock();
|
|
28
|
-
validator = validatorFactory(epochCache, {
|
|
29
|
-
txsPermitted: true,
|
|
30
|
-
maxTxsPerBlock: undefined
|
|
31
|
-
});
|
|
32
|
-
epochCache.getCurrentAndNextSlot.mockReturnValue({
|
|
33
|
-
currentSlot: currentSlot,
|
|
34
|
-
nextSlot: nextSlot
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
it('returns high tolerance error if slot number is not current or next slot (outside clock tolerance)', async ()=>{
|
|
38
|
-
const header = makeHeader(1, 99, 99);
|
|
39
|
-
const mockProposal = await makeProposal({
|
|
40
|
-
blockHeader: header,
|
|
41
|
-
lastBlockHeader: header
|
|
42
|
-
});
|
|
43
|
-
// Mock getEpochAndSlotNow to return time OUTSIDE clock tolerance (1000ms elapsed)
|
|
44
|
-
epochCache.getEpochAndSlotNow.mockReturnValue({
|
|
45
|
-
epoch: 1,
|
|
46
|
-
slot: currentSlot,
|
|
47
|
-
ts: 1000n,
|
|
48
|
-
nowMs: 1001000n
|
|
49
|
-
});
|
|
50
|
-
epochCache.getProposerAttesterAddressInSlot.mockResolvedValue(getAddress());
|
|
51
|
-
const result = await validator.validate(mockProposal);
|
|
52
|
-
expect(result).toEqual({
|
|
53
|
-
result: 'reject',
|
|
54
|
-
severity: PeerErrorSeverity.HighToleranceError
|
|
55
|
-
});
|
|
56
|
-
// Should not try to resolve proposers if base validation fails
|
|
57
|
-
expect(epochCache.getProposerAttesterAddressInSlot).not.toHaveBeenCalled();
|
|
58
|
-
});
|
|
59
|
-
it('returns ignore if previous slot proposal is within clock tolerance', async ()=>{
|
|
60
|
-
const previousProposer = getSigner();
|
|
61
|
-
const header = makeHeader(1, 99, 99);
|
|
62
|
-
const mockProposal = await makeProposal({
|
|
63
|
-
blockHeader: header,
|
|
64
|
-
lastBlockHeader: header,
|
|
65
|
-
signer: previousProposer
|
|
66
|
-
});
|
|
67
|
-
// Mock getEpochAndSlotNow to return time WITHIN clock tolerance (100ms elapsed)
|
|
68
|
-
epochCache.getEpochAndSlotNow.mockReturnValue({
|
|
69
|
-
epoch: 1,
|
|
70
|
-
slot: currentSlot,
|
|
71
|
-
ts: 1000n,
|
|
72
|
-
nowMs: 1000100n
|
|
73
|
-
});
|
|
74
|
-
mockGetProposer(getAddress(), getAddress(), getAddress(previousProposer));
|
|
75
|
-
const result = await validator.validate(mockProposal);
|
|
76
|
-
expect(result).toEqual({
|
|
77
|
-
result: 'ignore'
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
it('returns mid tolerance error if proposal has invalid signature', async ()=>{
|
|
81
|
-
const currentProposer = getSigner();
|
|
82
|
-
const header = makeHeader(1, 100, 100);
|
|
83
|
-
const mockProposal = await makeProposal({
|
|
84
|
-
blockHeader: header,
|
|
85
|
-
lastBlockHeader: header,
|
|
86
|
-
signer: currentProposer
|
|
87
|
-
});
|
|
88
|
-
// Override getSender to return undefined (invalid signature)
|
|
89
|
-
jest.spyOn(mockProposal, 'getSender').mockReturnValue(undefined);
|
|
90
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
91
|
-
const result = await validator.validate(mockProposal);
|
|
92
|
-
expect(result).toEqual({
|
|
93
|
-
result: 'reject',
|
|
94
|
-
severity: PeerErrorSeverity.MidToleranceError
|
|
95
|
-
});
|
|
96
|
-
// Should not try to resolve proposer if signature is invalid
|
|
97
|
-
expect(epochCache.getProposerAttesterAddressInSlot).not.toHaveBeenCalled();
|
|
98
|
-
});
|
|
99
|
-
it('returns mid tolerance error if proposer is not current proposer for current slot', async ()=>{
|
|
100
|
-
const currentProposer = getSigner();
|
|
101
|
-
const nextProposer = getSigner();
|
|
102
|
-
const invalidProposer = getSigner();
|
|
103
|
-
const header = makeHeader(1, 100, 100);
|
|
104
|
-
const mockProposal = await makeProposal({
|
|
105
|
-
blockHeader: header,
|
|
106
|
-
lastBlockHeader: header,
|
|
107
|
-
signer: invalidProposer
|
|
108
|
-
});
|
|
109
|
-
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
110
|
-
const result = await validator.validate(mockProposal);
|
|
111
|
-
expect(result).toEqual({
|
|
112
|
-
result: 'reject',
|
|
113
|
-
severity: PeerErrorSeverity.MidToleranceError
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
it('returns mid tolerance error if proposer is not next proposer for next slot', async ()=>{
|
|
117
|
-
const currentProposer = getSigner();
|
|
118
|
-
const nextProposer = getSigner();
|
|
119
|
-
const invalidProposer = getSigner();
|
|
120
|
-
const header = makeHeader(1, 101, 101);
|
|
121
|
-
const mockProposal = await makeProposal({
|
|
122
|
-
blockHeader: header,
|
|
123
|
-
lastBlockHeader: header,
|
|
124
|
-
signer: invalidProposer
|
|
125
|
-
});
|
|
126
|
-
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
127
|
-
const result = await validator.validate(mockProposal);
|
|
128
|
-
expect(result).toEqual({
|
|
129
|
-
result: 'reject',
|
|
130
|
-
severity: PeerErrorSeverity.MidToleranceError
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
it('returns mid tolerance error if proposer is current proposer but proposal is for next slot', async ()=>{
|
|
134
|
-
const currentProposer = getSigner();
|
|
135
|
-
const nextProposer = getSigner();
|
|
136
|
-
const header = makeHeader(1, 101, 101);
|
|
137
|
-
const mockProposal = await makeProposal({
|
|
138
|
-
blockHeader: header,
|
|
139
|
-
lastBlockHeader: header,
|
|
140
|
-
signer: currentProposer
|
|
141
|
-
});
|
|
142
|
-
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
143
|
-
const result = await validator.validate(mockProposal);
|
|
144
|
-
expect(result).toEqual({
|
|
145
|
-
result: 'reject',
|
|
146
|
-
severity: PeerErrorSeverity.MidToleranceError
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
it('accepts proposal when proposer is undefined (open committee)', async ()=>{
|
|
150
|
-
const currentProposer = getSigner();
|
|
151
|
-
const header = makeHeader(1, 100, 100);
|
|
152
|
-
const mockProposal = await makeProposal({
|
|
153
|
-
blockHeader: header,
|
|
154
|
-
lastBlockHeader: header,
|
|
155
|
-
signer: currentProposer
|
|
156
|
-
});
|
|
157
|
-
epochCache.getProposerAttesterAddressInSlot.mockResolvedValue(undefined);
|
|
158
|
-
const result = await validator.validate(mockProposal);
|
|
159
|
-
expect(result).toEqual({
|
|
160
|
-
result: 'accept'
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
it('returns low tolerance error when getProposerAttesterAddressInSlot throws NoCommitteeError', async ()=>{
|
|
164
|
-
const currentProposer = getSigner();
|
|
165
|
-
const header = makeHeader(1, 100, 100);
|
|
166
|
-
const mockProposal = await makeProposal({
|
|
167
|
-
blockHeader: header,
|
|
168
|
-
lastBlockHeader: header,
|
|
169
|
-
signer: currentProposer
|
|
170
|
-
});
|
|
171
|
-
epochCache.getProposerAttesterAddressInSlot.mockRejectedValue(new NoCommitteeError());
|
|
172
|
-
const result = await validator.validate(mockProposal);
|
|
173
|
-
expect(result).toEqual({
|
|
174
|
-
result: 'reject',
|
|
175
|
-
severity: PeerErrorSeverity.LowToleranceError
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
it('returns undefined if proposal is valid for current slot and proposer', async ()=>{
|
|
179
|
-
const currentProposer = getSigner();
|
|
180
|
-
const nextProposer = getSigner();
|
|
181
|
-
const header = makeHeader(1, 100, 100);
|
|
182
|
-
const mockProposal = await makeProposal({
|
|
183
|
-
blockHeader: header,
|
|
184
|
-
lastBlockHeader: header,
|
|
185
|
-
signer: currentProposer
|
|
186
|
-
});
|
|
187
|
-
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
188
|
-
const result = await validator.validate(mockProposal);
|
|
189
|
-
expect(result).toEqual({
|
|
190
|
-
result: 'accept'
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
it('returns undefined if proposal is valid for next slot and proposer', async ()=>{
|
|
194
|
-
const currentProposer = getSigner();
|
|
195
|
-
const nextProposer = getSigner();
|
|
196
|
-
const header = makeHeader(1, 101, 101);
|
|
197
|
-
const mockProposal = await makeProposal({
|
|
198
|
-
blockHeader: header,
|
|
199
|
-
lastBlockHeader: header,
|
|
200
|
-
signer: nextProposer
|
|
201
|
-
});
|
|
202
|
-
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
203
|
-
const result = await validator.validate(mockProposal);
|
|
204
|
-
expect(result).toEqual({
|
|
205
|
-
result: 'accept'
|
|
206
|
-
});
|
|
207
|
-
});
|
|
208
|
-
describe('transaction permission validation', ()=>{
|
|
209
|
-
it('returns mid tolerance error if txs not permitted and proposal contains txHashes', async ()=>{
|
|
210
|
-
const currentProposer = getSigner();
|
|
211
|
-
const validatorWithTxsDisabled = validatorFactory(epochCache, {
|
|
212
|
-
txsPermitted: false,
|
|
213
|
-
maxTxsPerBlock: undefined
|
|
214
|
-
});
|
|
215
|
-
const header = makeHeader(1, 100, 100);
|
|
216
|
-
const mockProposal = await makeProposal({
|
|
217
|
-
blockHeader: header,
|
|
218
|
-
lastBlockHeader: header,
|
|
219
|
-
signer: currentProposer,
|
|
220
|
-
txHashes: getTxHashes(2)
|
|
221
|
-
});
|
|
222
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
223
|
-
const result = await validatorWithTxsDisabled.validate(mockProposal);
|
|
224
|
-
expect(result).toEqual({
|
|
225
|
-
result: 'reject',
|
|
226
|
-
severity: PeerErrorSeverity.MidToleranceError
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
it('returns undefined if txs not permitted but proposal has no txHashes', async ()=>{
|
|
230
|
-
const currentProposer = getSigner();
|
|
231
|
-
const validatorWithTxsDisabled = validatorFactory(epochCache, {
|
|
232
|
-
txsPermitted: false,
|
|
233
|
-
maxTxsPerBlock: undefined
|
|
234
|
-
});
|
|
235
|
-
const header = makeHeader(1, 100, 100);
|
|
236
|
-
const mockProposal = await makeProposal({
|
|
237
|
-
blockHeader: header,
|
|
238
|
-
lastBlockHeader: header,
|
|
239
|
-
signer: currentProposer,
|
|
240
|
-
txHashes: getTxHashes(0)
|
|
241
|
-
});
|
|
242
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
243
|
-
const result = await validatorWithTxsDisabled.validate(mockProposal);
|
|
244
|
-
expect(result).toEqual({
|
|
245
|
-
result: 'accept'
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
it('returns undefined if txs permitted and proposal contains txHashes', async ()=>{
|
|
249
|
-
const currentProposer = getSigner();
|
|
250
|
-
const header = makeHeader(1, 100, 100);
|
|
251
|
-
const mockProposal = await makeProposal({
|
|
252
|
-
blockHeader: header,
|
|
253
|
-
lastBlockHeader: header,
|
|
254
|
-
signer: currentProposer,
|
|
255
|
-
txHashes: getTxHashes(2)
|
|
256
|
-
});
|
|
257
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
258
|
-
const result = await validator.validate(mockProposal);
|
|
259
|
-
expect(result).toEqual({
|
|
260
|
-
result: 'accept'
|
|
261
|
-
});
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
describe('embedded tx validation', ()=>{
|
|
265
|
-
it('returns mid tolerance error if embedded txs are not listed in txHashes', async ()=>{
|
|
266
|
-
const currentProposer = getSigner();
|
|
267
|
-
const txHashes = getTxHashes(2);
|
|
268
|
-
const header = makeHeader(1, 100, 100);
|
|
269
|
-
const mockProposal = await makeProposal({
|
|
270
|
-
blockHeader: header,
|
|
271
|
-
lastBlockHeader: header,
|
|
272
|
-
signer: currentProposer,
|
|
273
|
-
txHashes
|
|
274
|
-
});
|
|
275
|
-
// Create a fake tx whose hash is NOT in txHashes
|
|
276
|
-
const fakeTxHash = getTxHashes(1)[0];
|
|
277
|
-
const fakeTx = {
|
|
278
|
-
getTxHash: ()=>fakeTxHash,
|
|
279
|
-
validateTxHash: ()=>Promise.resolve(true)
|
|
280
|
-
};
|
|
281
|
-
Object.defineProperty(mockProposal, 'txs', {
|
|
282
|
-
get: ()=>[
|
|
283
|
-
fakeTx
|
|
284
|
-
],
|
|
285
|
-
configurable: true
|
|
286
|
-
});
|
|
287
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
288
|
-
const result = await validator.validate(mockProposal);
|
|
289
|
-
expect(result).toEqual({
|
|
290
|
-
result: 'reject',
|
|
291
|
-
severity: PeerErrorSeverity.MidToleranceError
|
|
292
|
-
});
|
|
293
|
-
});
|
|
294
|
-
it('returns low tolerance error if embedded tx has invalid tx hash', async ()=>{
|
|
295
|
-
const currentProposer = getSigner();
|
|
296
|
-
const txHashes = getTxHashes(2);
|
|
297
|
-
const header = makeHeader(1, 100, 100);
|
|
298
|
-
const mockProposal = await makeProposal({
|
|
299
|
-
blockHeader: header,
|
|
300
|
-
lastBlockHeader: header,
|
|
301
|
-
signer: currentProposer,
|
|
302
|
-
txHashes
|
|
303
|
-
});
|
|
304
|
-
// Create a fake tx whose hash IS in txHashes but validateTxHash returns false
|
|
305
|
-
const fakeTx = {
|
|
306
|
-
getTxHash: ()=>txHashes[0],
|
|
307
|
-
validateTxHash: ()=>Promise.resolve(false)
|
|
308
|
-
};
|
|
309
|
-
Object.defineProperty(mockProposal, 'txs', {
|
|
310
|
-
get: ()=>[
|
|
311
|
-
fakeTx
|
|
312
|
-
],
|
|
313
|
-
configurable: true
|
|
314
|
-
});
|
|
315
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
316
|
-
const result = await validator.validate(mockProposal);
|
|
317
|
-
expect(result).toEqual({
|
|
318
|
-
result: 'reject',
|
|
319
|
-
severity: PeerErrorSeverity.LowToleranceError
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
describe('maxTxsPerBlock validation', ()=>{
|
|
324
|
-
it('rejects proposal when txHashes exceed maxTxsPerBlock', async ()=>{
|
|
325
|
-
const validatorWithMaxTxs = validatorFactory(epochCache, {
|
|
326
|
-
txsPermitted: true,
|
|
327
|
-
maxTxsPerBlock: 2
|
|
328
|
-
});
|
|
329
|
-
const currentProposer = getSigner();
|
|
330
|
-
const header = makeHeader(1, 100, 100);
|
|
331
|
-
const mockProposal = await makeProposal({
|
|
332
|
-
blockHeader: header,
|
|
333
|
-
lastBlockHeader: header,
|
|
334
|
-
signer: currentProposer,
|
|
335
|
-
txHashes: getTxHashes(3)
|
|
336
|
-
});
|
|
337
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
338
|
-
const result = await validatorWithMaxTxs.validate(mockProposal);
|
|
339
|
-
expect(result).toEqual({
|
|
340
|
-
result: 'reject',
|
|
341
|
-
severity: PeerErrorSeverity.MidToleranceError
|
|
342
|
-
});
|
|
343
|
-
});
|
|
344
|
-
it('accepts proposal when txHashes count equals maxTxsPerBlock', async ()=>{
|
|
345
|
-
const validatorWithMaxTxs = validatorFactory(epochCache, {
|
|
346
|
-
txsPermitted: true,
|
|
347
|
-
maxTxsPerBlock: 2
|
|
348
|
-
});
|
|
349
|
-
const currentProposer = getSigner();
|
|
350
|
-
const header = makeHeader(1, 100, 100);
|
|
351
|
-
const mockProposal = await makeProposal({
|
|
352
|
-
blockHeader: header,
|
|
353
|
-
lastBlockHeader: header,
|
|
354
|
-
signer: currentProposer,
|
|
355
|
-
txHashes: getTxHashes(2)
|
|
356
|
-
});
|
|
357
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
358
|
-
const result = await validatorWithMaxTxs.validate(mockProposal);
|
|
359
|
-
expect(result).toEqual({
|
|
360
|
-
result: 'accept'
|
|
361
|
-
});
|
|
362
|
-
});
|
|
363
|
-
it('accepts proposal when maxTxsPerBlock is not set (unlimited)', async ()=>{
|
|
364
|
-
// Default validator has no maxTxsPerBlock
|
|
365
|
-
const currentProposer = getSigner();
|
|
366
|
-
const header = makeHeader(1, 100, 100);
|
|
367
|
-
const mockProposal = await makeProposal({
|
|
368
|
-
blockHeader: header,
|
|
369
|
-
lastBlockHeader: header,
|
|
370
|
-
signer: currentProposer,
|
|
371
|
-
txHashes: getTxHashes(10)
|
|
372
|
-
});
|
|
373
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
374
|
-
const result = await validator.validate(mockProposal);
|
|
375
|
-
expect(result).toEqual({
|
|
376
|
-
result: 'accept'
|
|
377
|
-
});
|
|
378
|
-
});
|
|
379
|
-
});
|
|
380
|
-
});
|
|
381
|
-
}
|
|
@@ -1,379 +0,0 @@
|
|
|
1
|
-
import type { EpochCacheInterface } from '@aztec/epoch-cache';
|
|
2
|
-
import { NoCommitteeError } from '@aztec/ethereum/contracts';
|
|
3
|
-
import type { Secp256k1Signer } from '@aztec/foundation/crypto/secp256k1-signer';
|
|
4
|
-
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
5
|
-
import {
|
|
6
|
-
type BlockProposal,
|
|
7
|
-
type CheckpointProposal,
|
|
8
|
-
PeerErrorSeverity,
|
|
9
|
-
type ValidationResult,
|
|
10
|
-
} from '@aztec/stdlib/p2p';
|
|
11
|
-
import type { TxHash } from '@aztec/stdlib/tx';
|
|
12
|
-
|
|
13
|
-
import { jest } from '@jest/globals';
|
|
14
|
-
import type { MockProxy } from 'jest-mock-extended';
|
|
15
|
-
|
|
16
|
-
export interface ProposalValidatorTestParams<TProposal extends BlockProposal | CheckpointProposal> {
|
|
17
|
-
validatorFactory: (
|
|
18
|
-
epochCache: EpochCacheInterface,
|
|
19
|
-
opts: { txsPermitted: boolean; maxTxsPerBlock?: number },
|
|
20
|
-
) => { validate: (proposal: TProposal) => Promise<ValidationResult> };
|
|
21
|
-
makeProposal: (options?: any) => Promise<TProposal>;
|
|
22
|
-
makeHeader: (epochNumber: number | bigint, slotNumber: number | bigint, blockNumber: number | bigint) => any;
|
|
23
|
-
getSigner: () => Secp256k1Signer;
|
|
24
|
-
getAddress: (signer?: Secp256k1Signer) => EthAddress;
|
|
25
|
-
getSlot: (slot: number | bigint) => any;
|
|
26
|
-
getTxHashes: (n: number) => TxHash[];
|
|
27
|
-
getTxs: () => any[];
|
|
28
|
-
epochCacheMock: () => MockProxy<EpochCacheInterface>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function sharedProposalValidatorTests<TProposal extends BlockProposal | CheckpointProposal>(
|
|
32
|
-
params: ProposalValidatorTestParams<TProposal>,
|
|
33
|
-
) {
|
|
34
|
-
const { validatorFactory, makeProposal, makeHeader, getSigner, getAddress, getSlot, getTxHashes, epochCacheMock } =
|
|
35
|
-
params;
|
|
36
|
-
|
|
37
|
-
describe('shared proposal validation logic', () => {
|
|
38
|
-
let epochCache: MockProxy<EpochCacheInterface>;
|
|
39
|
-
let validator: { validate: (proposal: TProposal) => Promise<ValidationResult> };
|
|
40
|
-
const previousSlot = getSlot(99);
|
|
41
|
-
const currentSlot = getSlot(100);
|
|
42
|
-
const nextSlot = getSlot(101);
|
|
43
|
-
|
|
44
|
-
function mockGetProposer(currentProposer: EthAddress, nextProposer: EthAddress, previousProposer?: EthAddress) {
|
|
45
|
-
epochCache.getProposerAttesterAddressInSlot.mockImplementation(slot => {
|
|
46
|
-
if (slot === currentSlot) {
|
|
47
|
-
return Promise.resolve(currentProposer);
|
|
48
|
-
}
|
|
49
|
-
if (slot === nextSlot) {
|
|
50
|
-
return Promise.resolve(nextProposer);
|
|
51
|
-
}
|
|
52
|
-
if (slot === previousSlot && previousProposer) {
|
|
53
|
-
return Promise.resolve(previousProposer);
|
|
54
|
-
}
|
|
55
|
-
throw new Error('Unexpected argument');
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
beforeEach(() => {
|
|
60
|
-
epochCache = epochCacheMock();
|
|
61
|
-
validator = validatorFactory(epochCache, { txsPermitted: true, maxTxsPerBlock: undefined });
|
|
62
|
-
epochCache.getCurrentAndNextSlot.mockReturnValue({
|
|
63
|
-
currentSlot: currentSlot,
|
|
64
|
-
nextSlot: nextSlot,
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('returns high tolerance error if slot number is not current or next slot (outside clock tolerance)', async () => {
|
|
69
|
-
const header = makeHeader(1, 99, 99);
|
|
70
|
-
const mockProposal = await makeProposal({ blockHeader: header, lastBlockHeader: header });
|
|
71
|
-
|
|
72
|
-
// Mock getEpochAndSlotNow to return time OUTSIDE clock tolerance (1000ms elapsed)
|
|
73
|
-
epochCache.getEpochAndSlotNow.mockReturnValue({
|
|
74
|
-
epoch: 1 as any,
|
|
75
|
-
slot: currentSlot,
|
|
76
|
-
ts: 1000n, // slot started at 1000 seconds
|
|
77
|
-
nowMs: 1001000n, // 1000ms elapsed, outside 500ms tolerance
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
epochCache.getProposerAttesterAddressInSlot.mockResolvedValue(getAddress());
|
|
81
|
-
const result = await validator.validate(mockProposal);
|
|
82
|
-
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.HighToleranceError });
|
|
83
|
-
|
|
84
|
-
// Should not try to resolve proposers if base validation fails
|
|
85
|
-
expect(epochCache.getProposerAttesterAddressInSlot).not.toHaveBeenCalled();
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('returns ignore if previous slot proposal is within clock tolerance', async () => {
|
|
89
|
-
const previousProposer = getSigner();
|
|
90
|
-
const header = makeHeader(1, 99, 99);
|
|
91
|
-
const mockProposal = await makeProposal({
|
|
92
|
-
blockHeader: header,
|
|
93
|
-
lastBlockHeader: header,
|
|
94
|
-
signer: previousProposer,
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
// Mock getEpochAndSlotNow to return time WITHIN clock tolerance (100ms elapsed)
|
|
98
|
-
epochCache.getEpochAndSlotNow.mockReturnValue({
|
|
99
|
-
epoch: 1 as any,
|
|
100
|
-
slot: currentSlot,
|
|
101
|
-
ts: 1000n, // slot started at 1000 seconds
|
|
102
|
-
nowMs: 1000100n, // 100ms elapsed, within 500ms tolerance
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
mockGetProposer(getAddress(), getAddress(), getAddress(previousProposer));
|
|
106
|
-
const result = await validator.validate(mockProposal);
|
|
107
|
-
expect(result).toEqual({ result: 'ignore' });
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it('returns mid tolerance error if proposal has invalid signature', async () => {
|
|
111
|
-
const currentProposer = getSigner();
|
|
112
|
-
const header = makeHeader(1, 100, 100);
|
|
113
|
-
const mockProposal = await makeProposal({
|
|
114
|
-
blockHeader: header,
|
|
115
|
-
lastBlockHeader: header,
|
|
116
|
-
signer: currentProposer,
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
// Override getSender to return undefined (invalid signature)
|
|
120
|
-
jest.spyOn(mockProposal as any, 'getSender').mockReturnValue(undefined);
|
|
121
|
-
|
|
122
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
123
|
-
const result = await validator.validate(mockProposal);
|
|
124
|
-
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.MidToleranceError });
|
|
125
|
-
|
|
126
|
-
// Should not try to resolve proposer if signature is invalid
|
|
127
|
-
expect(epochCache.getProposerAttesterAddressInSlot).not.toHaveBeenCalled();
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('returns mid tolerance error if proposer is not current proposer for current slot', async () => {
|
|
131
|
-
const currentProposer = getSigner();
|
|
132
|
-
const nextProposer = getSigner();
|
|
133
|
-
const invalidProposer = getSigner();
|
|
134
|
-
const header = makeHeader(1, 100, 100);
|
|
135
|
-
const mockProposal = await makeProposal({
|
|
136
|
-
blockHeader: header,
|
|
137
|
-
lastBlockHeader: header,
|
|
138
|
-
signer: invalidProposer,
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
142
|
-
const result = await validator.validate(mockProposal);
|
|
143
|
-
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.MidToleranceError });
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
it('returns mid tolerance error if proposer is not next proposer for next slot', async () => {
|
|
147
|
-
const currentProposer = getSigner();
|
|
148
|
-
const nextProposer = getSigner();
|
|
149
|
-
const invalidProposer = getSigner();
|
|
150
|
-
const header = makeHeader(1, 101, 101);
|
|
151
|
-
const mockProposal = await makeProposal({
|
|
152
|
-
blockHeader: header,
|
|
153
|
-
lastBlockHeader: header,
|
|
154
|
-
signer: invalidProposer,
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
158
|
-
const result = await validator.validate(mockProposal);
|
|
159
|
-
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.MidToleranceError });
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it('returns mid tolerance error if proposer is current proposer but proposal is for next slot', async () => {
|
|
163
|
-
const currentProposer = getSigner();
|
|
164
|
-
const nextProposer = getSigner();
|
|
165
|
-
const header = makeHeader(1, 101, 101);
|
|
166
|
-
const mockProposal = await makeProposal({
|
|
167
|
-
blockHeader: header,
|
|
168
|
-
lastBlockHeader: header,
|
|
169
|
-
signer: currentProposer,
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
173
|
-
const result = await validator.validate(mockProposal);
|
|
174
|
-
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.MidToleranceError });
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
it('accepts proposal when proposer is undefined (open committee)', async () => {
|
|
178
|
-
const currentProposer = getSigner();
|
|
179
|
-
const header = makeHeader(1, 100, 100);
|
|
180
|
-
const mockProposal = await makeProposal({
|
|
181
|
-
blockHeader: header,
|
|
182
|
-
lastBlockHeader: header,
|
|
183
|
-
signer: currentProposer,
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
epochCache.getProposerAttesterAddressInSlot.mockResolvedValue(undefined);
|
|
187
|
-
const result = await validator.validate(mockProposal);
|
|
188
|
-
expect(result).toEqual({ result: 'accept' });
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
it('returns low tolerance error when getProposerAttesterAddressInSlot throws NoCommitteeError', async () => {
|
|
192
|
-
const currentProposer = getSigner();
|
|
193
|
-
const header = makeHeader(1, 100, 100);
|
|
194
|
-
const mockProposal = await makeProposal({
|
|
195
|
-
blockHeader: header,
|
|
196
|
-
lastBlockHeader: header,
|
|
197
|
-
signer: currentProposer,
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
epochCache.getProposerAttesterAddressInSlot.mockRejectedValue(new NoCommitteeError());
|
|
201
|
-
const result = await validator.validate(mockProposal);
|
|
202
|
-
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.LowToleranceError });
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
it('returns undefined if proposal is valid for current slot and proposer', async () => {
|
|
206
|
-
const currentProposer = getSigner();
|
|
207
|
-
const nextProposer = getSigner();
|
|
208
|
-
const header = makeHeader(1, 100, 100);
|
|
209
|
-
const mockProposal = await makeProposal({
|
|
210
|
-
blockHeader: header,
|
|
211
|
-
lastBlockHeader: header,
|
|
212
|
-
signer: currentProposer,
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
216
|
-
const result = await validator.validate(mockProposal);
|
|
217
|
-
expect(result).toEqual({ result: 'accept' });
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
it('returns undefined if proposal is valid for next slot and proposer', async () => {
|
|
221
|
-
const currentProposer = getSigner();
|
|
222
|
-
const nextProposer = getSigner();
|
|
223
|
-
const header = makeHeader(1, 101, 101);
|
|
224
|
-
const mockProposal = await makeProposal({ blockHeader: header, lastBlockHeader: header, signer: nextProposer });
|
|
225
|
-
|
|
226
|
-
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
227
|
-
const result = await validator.validate(mockProposal);
|
|
228
|
-
expect(result).toEqual({ result: 'accept' });
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
describe('transaction permission validation', () => {
|
|
232
|
-
it('returns mid tolerance error if txs not permitted and proposal contains txHashes', async () => {
|
|
233
|
-
const currentProposer = getSigner();
|
|
234
|
-
const validatorWithTxsDisabled = validatorFactory(epochCache, {
|
|
235
|
-
txsPermitted: false,
|
|
236
|
-
maxTxsPerBlock: undefined,
|
|
237
|
-
});
|
|
238
|
-
const header = makeHeader(1, 100, 100);
|
|
239
|
-
const mockProposal = await makeProposal({
|
|
240
|
-
blockHeader: header,
|
|
241
|
-
lastBlockHeader: header,
|
|
242
|
-
signer: currentProposer,
|
|
243
|
-
txHashes: getTxHashes(2),
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
247
|
-
const result = await validatorWithTxsDisabled.validate(mockProposal);
|
|
248
|
-
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.MidToleranceError });
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
it('returns undefined if txs not permitted but proposal has no txHashes', async () => {
|
|
252
|
-
const currentProposer = getSigner();
|
|
253
|
-
const validatorWithTxsDisabled = validatorFactory(epochCache, {
|
|
254
|
-
txsPermitted: false,
|
|
255
|
-
maxTxsPerBlock: undefined,
|
|
256
|
-
});
|
|
257
|
-
const header = makeHeader(1, 100, 100);
|
|
258
|
-
const mockProposal = await makeProposal({
|
|
259
|
-
blockHeader: header,
|
|
260
|
-
lastBlockHeader: header,
|
|
261
|
-
signer: currentProposer,
|
|
262
|
-
txHashes: getTxHashes(0),
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
266
|
-
const result = await validatorWithTxsDisabled.validate(mockProposal);
|
|
267
|
-
expect(result).toEqual({ result: 'accept' });
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
it('returns undefined if txs permitted and proposal contains txHashes', async () => {
|
|
271
|
-
const currentProposer = getSigner();
|
|
272
|
-
const header = makeHeader(1, 100, 100);
|
|
273
|
-
const mockProposal = await makeProposal({
|
|
274
|
-
blockHeader: header,
|
|
275
|
-
lastBlockHeader: header,
|
|
276
|
-
signer: currentProposer,
|
|
277
|
-
txHashes: getTxHashes(2),
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
281
|
-
const result = await validator.validate(mockProposal);
|
|
282
|
-
expect(result).toEqual({ result: 'accept' });
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
describe('embedded tx validation', () => {
|
|
287
|
-
it('returns mid tolerance error if embedded txs are not listed in txHashes', async () => {
|
|
288
|
-
const currentProposer = getSigner();
|
|
289
|
-
const txHashes = getTxHashes(2);
|
|
290
|
-
const header = makeHeader(1, 100, 100);
|
|
291
|
-
const mockProposal = await makeProposal({
|
|
292
|
-
blockHeader: header,
|
|
293
|
-
lastBlockHeader: header,
|
|
294
|
-
signer: currentProposer,
|
|
295
|
-
txHashes,
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
// Create a fake tx whose hash is NOT in txHashes
|
|
299
|
-
const fakeTxHash = getTxHashes(1)[0];
|
|
300
|
-
const fakeTx = { getTxHash: () => fakeTxHash, validateTxHash: () => Promise.resolve(true) };
|
|
301
|
-
Object.defineProperty(mockProposal, 'txs', { get: () => [fakeTx], configurable: true });
|
|
302
|
-
|
|
303
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
304
|
-
const result = await validator.validate(mockProposal);
|
|
305
|
-
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.MidToleranceError });
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
it('returns low tolerance error if embedded tx has invalid tx hash', async () => {
|
|
309
|
-
const currentProposer = getSigner();
|
|
310
|
-
const txHashes = getTxHashes(2);
|
|
311
|
-
const header = makeHeader(1, 100, 100);
|
|
312
|
-
const mockProposal = await makeProposal({
|
|
313
|
-
blockHeader: header,
|
|
314
|
-
lastBlockHeader: header,
|
|
315
|
-
signer: currentProposer,
|
|
316
|
-
txHashes,
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
// Create a fake tx whose hash IS in txHashes but validateTxHash returns false
|
|
320
|
-
const fakeTx = { getTxHash: () => txHashes[0], validateTxHash: () => Promise.resolve(false) };
|
|
321
|
-
Object.defineProperty(mockProposal, 'txs', { get: () => [fakeTx], configurable: true });
|
|
322
|
-
|
|
323
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
324
|
-
const result = await validator.validate(mockProposal);
|
|
325
|
-
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.LowToleranceError });
|
|
326
|
-
});
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
describe('maxTxsPerBlock validation', () => {
|
|
330
|
-
it('rejects proposal when txHashes exceed maxTxsPerBlock', async () => {
|
|
331
|
-
const validatorWithMaxTxs = validatorFactory(epochCache, { txsPermitted: true, maxTxsPerBlock: 2 });
|
|
332
|
-
const currentProposer = getSigner();
|
|
333
|
-
const header = makeHeader(1, 100, 100);
|
|
334
|
-
const mockProposal = await makeProposal({
|
|
335
|
-
blockHeader: header,
|
|
336
|
-
lastBlockHeader: header,
|
|
337
|
-
signer: currentProposer,
|
|
338
|
-
txHashes: getTxHashes(3),
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
342
|
-
const result = await validatorWithMaxTxs.validate(mockProposal);
|
|
343
|
-
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.MidToleranceError });
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
it('accepts proposal when txHashes count equals maxTxsPerBlock', async () => {
|
|
347
|
-
const validatorWithMaxTxs = validatorFactory(epochCache, { txsPermitted: true, maxTxsPerBlock: 2 });
|
|
348
|
-
const currentProposer = getSigner();
|
|
349
|
-
const header = makeHeader(1, 100, 100);
|
|
350
|
-
const mockProposal = await makeProposal({
|
|
351
|
-
blockHeader: header,
|
|
352
|
-
lastBlockHeader: header,
|
|
353
|
-
signer: currentProposer,
|
|
354
|
-
txHashes: getTxHashes(2),
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
358
|
-
const result = await validatorWithMaxTxs.validate(mockProposal);
|
|
359
|
-
expect(result).toEqual({ result: 'accept' });
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
it('accepts proposal when maxTxsPerBlock is not set (unlimited)', async () => {
|
|
363
|
-
// Default validator has no maxTxsPerBlock
|
|
364
|
-
const currentProposer = getSigner();
|
|
365
|
-
const header = makeHeader(1, 100, 100);
|
|
366
|
-
const mockProposal = await makeProposal({
|
|
367
|
-
blockHeader: header,
|
|
368
|
-
lastBlockHeader: header,
|
|
369
|
-
signer: currentProposer,
|
|
370
|
-
txHashes: getTxHashes(10),
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
374
|
-
const result = await validator.validate(mockProposal);
|
|
375
|
-
expect(result).toEqual({ result: 'accept' });
|
|
376
|
-
});
|
|
377
|
-
});
|
|
378
|
-
});
|
|
379
|
-
}
|