@aztec/p2p 0.0.1-commit.1142ef1 → 0.0.1-commit.1bea0213
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/client/p2p_client.js +1 -1
- package/dest/config.js +2 -2
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +20 -9
- package/dest/mem_pools/attestation_pool/mocks.d.ts +4 -2
- package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/mocks.js +8 -5
- package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts +15 -10
- package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.js +91 -50
- package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts +19 -5
- package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/eviction/eviction_manager.js +59 -3
- package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts +79 -5
- package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/eviction/eviction_strategy.js +47 -0
- package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts +16 -0
- package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.js +122 -0
- package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.d.ts +2 -2
- package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.d.ts +4 -4
- package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.js +2 -0
- package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.d.ts +2 -2
- package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.d.ts +25 -0
- package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.js +57 -0
- package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +3 -3
- package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
- package/dest/msg_validators/attestation_validator/attestation_validator.js +41 -10
- package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +3 -3
- package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -1
- package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.js +11 -4
- package/dest/msg_validators/clock_tolerance.d.ts +21 -0
- package/dest/msg_validators/clock_tolerance.d.ts.map +1 -0
- package/dest/msg_validators/clock_tolerance.js +37 -0
- package/dest/msg_validators/proposal_validator/proposal_validator.d.ts +3 -3
- package/dest/msg_validators/proposal_validator/proposal_validator.d.ts.map +1 -1
- package/dest/msg_validators/proposal_validator/proposal_validator.js +52 -28
- package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts +3 -3
- package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts.map +1 -1
- package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.js +93 -64
- package/dest/msg_validators/tx_validator/block_header_validator.d.ts +1 -1
- package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/block_header_validator.js +1 -1
- package/dest/msg_validators/tx_validator/fee_payer_balance.d.ts +10 -0
- package/dest/msg_validators/tx_validator/fee_payer_balance.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/fee_payer_balance.js +20 -0
- package/dest/msg_validators/tx_validator/gas_validator.d.ts +1 -1
- package/dest/msg_validators/tx_validator/gas_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/gas_validator.js +8 -14
- package/dest/msg_validators/tx_validator/index.d.ts +2 -1
- package/dest/msg_validators/tx_validator/index.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/index.js +1 -0
- package/dest/msg_validators/tx_validator/size_validator.d.ts +6 -0
- package/dest/msg_validators/tx_validator/size_validator.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/size_validator.js +20 -0
- package/dest/services/encoding.d.ts +1 -1
- package/dest/services/encoding.d.ts.map +1 -1
- package/dest/services/encoding.js +2 -3
- package/dest/services/libp2p/instrumentation.js +5 -5
- package/dest/services/libp2p/libp2p_service.d.ts +5 -5
- package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
- package/dest/services/libp2p/libp2p_service.js +26 -26
- package/dest/services/reqresp/constants.d.ts +12 -0
- package/dest/services/reqresp/constants.d.ts.map +1 -0
- package/dest/services/reqresp/constants.js +7 -0
- package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts +1 -1
- package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/block_txs/bitvector.js +7 -0
- package/dest/services/reqresp/protocols/status.d.ts +1 -1
- package/dest/services/reqresp/protocols/status.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/status.js +2 -1
- package/dest/services/tx_collection/slow_tx_collection.d.ts +3 -3
- package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -1
- package/dest/services/tx_collection/tx_collection.d.ts +5 -5
- package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
- package/dest/services/tx_provider.d.ts +3 -3
- package/dest/services/tx_provider.d.ts.map +1 -1
- package/dest/testbench/p2p_client_testbench_worker.js +5 -6
- package/package.json +16 -16
- package/src/client/p2p_client.ts +6 -6
- package/src/config.ts +2 -2
- package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +17 -9
- package/src/mem_pools/attestation_pool/mocks.ts +6 -4
- package/src/mem_pools/tx_pool/README.md +28 -13
- package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +130 -75
- package/src/mem_pools/tx_pool/eviction/eviction_manager.ts +66 -5
- package/src/mem_pools/tx_pool/eviction/eviction_strategy.ts +119 -4
- package/src/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.ts +162 -0
- package/src/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.ts +4 -2
- package/src/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.ts +75 -0
- package/src/msg_validators/attestation_validator/attestation_validator.ts +26 -14
- package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +6 -6
- package/src/msg_validators/clock_tolerance.ts +51 -0
- package/src/msg_validators/proposal_validator/proposal_validator.ts +28 -28
- package/src/msg_validators/proposal_validator/proposal_validator_test_suite.ts +91 -67
- package/src/msg_validators/tx_validator/block_header_validator.ts +3 -1
- package/src/msg_validators/tx_validator/fee_payer_balance.ts +40 -0
- package/src/msg_validators/tx_validator/gas_validator.ts +8 -25
- package/src/msg_validators/tx_validator/index.ts +1 -0
- package/src/msg_validators/tx_validator/size_validator.ts +18 -0
- package/src/services/encoding.ts +2 -3
- package/src/services/libp2p/instrumentation.ts +5 -5
- package/src/services/libp2p/libp2p_service.ts +39 -27
- package/src/services/reqresp/constants.ts +14 -0
- package/src/services/reqresp/protocols/block_txs/bitvector.ts +9 -0
- package/src/services/reqresp/protocols/status.ts +5 -3
- package/src/services/tx_collection/slow_tx_collection.ts +2 -2
- package/src/services/tx_collection/tx_collection.ts +4 -4
- package/src/services/tx_provider.ts +2 -2
- package/src/testbench/p2p_client_testbench_worker.ts +6 -10
- package/dest/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.d.ts +0 -15
- package/dest/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.d.ts.map +0 -1
- package/dest/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.js +0 -88
- package/src/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.ts +0 -108
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { EpochCacheInterface } from '@aztec/epoch-cache';
|
|
2
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Maximum clock disparity tolerance for P2P message validation (in milliseconds).
|
|
6
|
+
* Messages for the previous slot are accepted if we're within this many milliseconds
|
|
7
|
+
* of the current slot start. This prevents penalizing peers for messages that
|
|
8
|
+
* were valid when sent but arrived slightly late due to network latency.
|
|
9
|
+
*
|
|
10
|
+
* This follows Ethereum's MAXIMUM_GOSSIP_CLOCK_DISPARITY approach.
|
|
11
|
+
*/
|
|
12
|
+
export const MAXIMUM_GOSSIP_CLOCK_DISPARITY_MS = 500;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Checks if a message for the previous slot should be accepted due to clock tolerance.
|
|
16
|
+
*
|
|
17
|
+
* @param messageSlot - The slot number from the received message
|
|
18
|
+
* @param currentSlot - The current slot number
|
|
19
|
+
* @param epochCache - EpochCache to get timing information
|
|
20
|
+
* @returns true if the message is for the previous slot AND we're within the clock tolerance window
|
|
21
|
+
*/
|
|
22
|
+
export function isWithinClockTolerance(
|
|
23
|
+
messageSlot: SlotNumber,
|
|
24
|
+
currentSlot: SlotNumber,
|
|
25
|
+
epochCache: EpochCacheInterface,
|
|
26
|
+
): boolean {
|
|
27
|
+
// Guard against slot 0 edge case (genesis)
|
|
28
|
+
if (currentSlot === SlotNumber.ZERO) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Only apply tolerance to messages for the previous slot
|
|
33
|
+
const previousSlot = SlotNumber(currentSlot - 1);
|
|
34
|
+
if (messageSlot !== previousSlot) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Check how far we are into the current slot (in milliseconds)
|
|
39
|
+
const { ts: slotStartTs, nowMs, slot } = epochCache.getEpochAndSlotNow();
|
|
40
|
+
|
|
41
|
+
// Sanity check: ensure the epoch cache's current slot matches the expected current slot
|
|
42
|
+
if (slot !== currentSlot) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ts is in seconds, convert to ms; nowMs is already in milliseconds
|
|
47
|
+
const slotStartMs = slotStartTs * 1000n;
|
|
48
|
+
const elapsedMs = Number(nowMs - slotStartMs);
|
|
49
|
+
|
|
50
|
+
return elapsedMs < MAXIMUM_GOSSIP_CLOCK_DISPARITY_MS;
|
|
51
|
+
}
|
|
@@ -1,7 +1,9 @@
|
|
|
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 { BlockProposal, CheckpointProposal, PeerErrorSeverity } from '@aztec/stdlib/p2p';
|
|
4
|
+
import { BlockProposal, CheckpointProposal, PeerErrorSeverity, type ValidationResult } from '@aztec/stdlib/p2p';
|
|
5
|
+
|
|
6
|
+
import { isWithinClockTolerance } from '../clock_tolerance.js';
|
|
5
7
|
|
|
6
8
|
export abstract class ProposalValidator<TProposal extends BlockProposal | CheckpointProposal> {
|
|
7
9
|
protected epochCache: EpochCacheInterface;
|
|
@@ -14,13 +16,26 @@ export abstract class ProposalValidator<TProposal extends BlockProposal | Checkp
|
|
|
14
16
|
this.logger = createLogger(loggerName);
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
public async validate(proposal: TProposal): Promise<
|
|
19
|
+
public async validate(proposal: TProposal): Promise<ValidationResult> {
|
|
18
20
|
try {
|
|
21
|
+
// Slot check
|
|
22
|
+
const { currentSlot, nextSlot } = this.epochCache.getCurrentAndNextSlot();
|
|
23
|
+
const slotNumber = proposal.slotNumber;
|
|
24
|
+
if (slotNumber !== currentSlot && slotNumber !== nextSlot) {
|
|
25
|
+
// Check if message is for previous slot and within clock tolerance
|
|
26
|
+
if (!isWithinClockTolerance(slotNumber, currentSlot, this.epochCache)) {
|
|
27
|
+
this.logger.debug(`Penalizing peer for invalid slot number ${slotNumber}`, { currentSlot, nextSlot });
|
|
28
|
+
return { result: 'reject', severity: PeerErrorSeverity.HighToleranceError };
|
|
29
|
+
}
|
|
30
|
+
this.logger.debug(`Ignoring proposal for previous slot ${slotNumber} within clock tolerance`);
|
|
31
|
+
return { result: 'ignore' };
|
|
32
|
+
}
|
|
33
|
+
|
|
19
34
|
// Signature validity
|
|
20
35
|
const proposer = proposal.getSender();
|
|
21
36
|
if (!proposer) {
|
|
22
37
|
this.logger.debug(`Penalizing peer for proposal with invalid signature`);
|
|
23
|
-
return PeerErrorSeverity.MidToleranceError;
|
|
38
|
+
return { result: 'reject', severity: PeerErrorSeverity.MidToleranceError };
|
|
24
39
|
}
|
|
25
40
|
|
|
26
41
|
// Transactions permitted check
|
|
@@ -29,7 +44,7 @@ export abstract class ProposalValidator<TProposal extends BlockProposal | Checkp
|
|
|
29
44
|
this.logger.debug(
|
|
30
45
|
`Penalizing peer for proposal with ${proposal.txHashes.length} transaction(s) when transactions are not permitted`,
|
|
31
46
|
);
|
|
32
|
-
return PeerErrorSeverity.MidToleranceError;
|
|
47
|
+
return { result: 'reject', severity: PeerErrorSeverity.MidToleranceError };
|
|
33
48
|
}
|
|
34
49
|
|
|
35
50
|
// Embedded txs must be listed in txHashes
|
|
@@ -44,32 +59,17 @@ export abstract class ProposalValidator<TProposal extends BlockProposal | Checkp
|
|
|
44
59
|
txHashesLength: proposal.txHashes.length,
|
|
45
60
|
missingTxHashes,
|
|
46
61
|
});
|
|
47
|
-
return PeerErrorSeverity.MidToleranceError;
|
|
62
|
+
return { result: 'reject', severity: PeerErrorSeverity.MidToleranceError };
|
|
48
63
|
}
|
|
49
64
|
|
|
50
|
-
//
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
const slotNumber = proposal.slotNumber;
|
|
54
|
-
if (slotNumber !== currentSlot && slotNumber !== nextSlot) {
|
|
55
|
-
this.logger.debug(`Penalizing peer for invalid slot number ${slotNumber}`, { currentSlot, nextSlot });
|
|
56
|
-
return PeerErrorSeverity.HighToleranceError;
|
|
57
|
-
}
|
|
58
|
-
if (slotNumber === currentSlot && currentProposer !== undefined && !proposer.equals(currentProposer)) {
|
|
65
|
+
// Proposer check
|
|
66
|
+
const expectedProposer = await this.epochCache.getProposerAttesterAddressInSlot(slotNumber);
|
|
67
|
+
if (expectedProposer !== undefined && !proposer.equals(expectedProposer)) {
|
|
59
68
|
this.logger.debug(`Penalizing peer for invalid proposer for current slot ${slotNumber}`, {
|
|
60
|
-
|
|
61
|
-
nextProposer,
|
|
62
|
-
proposer: proposer.toString(),
|
|
63
|
-
});
|
|
64
|
-
return PeerErrorSeverity.MidToleranceError;
|
|
65
|
-
}
|
|
66
|
-
if (slotNumber === nextSlot && nextProposer !== undefined && !proposer.equals(nextProposer)) {
|
|
67
|
-
this.logger.debug(`Penalizing peer for invalid proposer for next slot ${slotNumber}`, {
|
|
68
|
-
currentProposer,
|
|
69
|
-
nextProposer,
|
|
69
|
+
expectedProposer,
|
|
70
70
|
proposer: proposer.toString(),
|
|
71
71
|
});
|
|
72
|
-
return PeerErrorSeverity.MidToleranceError;
|
|
72
|
+
return { result: 'reject', severity: PeerErrorSeverity.MidToleranceError };
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
// Validate tx hashes for all txs embedded in the proposal
|
|
@@ -78,13 +78,13 @@ export abstract class ProposalValidator<TProposal extends BlockProposal | Checkp
|
|
|
78
78
|
proposer,
|
|
79
79
|
slotNumber,
|
|
80
80
|
});
|
|
81
|
-
return PeerErrorSeverity.LowToleranceError;
|
|
81
|
+
return { result: 'reject', severity: PeerErrorSeverity.LowToleranceError };
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
return
|
|
84
|
+
return { result: 'accept' };
|
|
85
85
|
} catch (e) {
|
|
86
86
|
if (e instanceof NoCommitteeError) {
|
|
87
|
-
return PeerErrorSeverity.LowToleranceError;
|
|
87
|
+
return { result: 'reject', severity: PeerErrorSeverity.LowToleranceError };
|
|
88
88
|
}
|
|
89
89
|
throw e;
|
|
90
90
|
}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import type { EpochCacheInterface } from '@aztec/epoch-cache';
|
|
2
2
|
import type { Secp256k1Signer } from '@aztec/foundation/crypto/secp256k1-signer';
|
|
3
3
|
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
type BlockProposal,
|
|
6
|
+
type CheckpointProposal,
|
|
7
|
+
PeerErrorSeverity,
|
|
8
|
+
type ValidationResult,
|
|
9
|
+
} from '@aztec/stdlib/p2p';
|
|
5
10
|
import type { TxHash } from '@aztec/stdlib/tx';
|
|
6
11
|
|
|
7
12
|
import type { MockProxy } from 'jest-mock-extended';
|
|
@@ -10,7 +15,7 @@ export interface ProposalValidatorTestParams<TProposal extends BlockProposal | C
|
|
|
10
15
|
validatorFactory: (
|
|
11
16
|
epochCache: EpochCacheInterface,
|
|
12
17
|
opts: { txsPermitted: boolean },
|
|
13
|
-
) => { validate: (proposal: TProposal) => Promise<
|
|
18
|
+
) => { validate: (proposal: TProposal) => Promise<ValidationResult> };
|
|
14
19
|
makeProposal: (options?: any) => Promise<TProposal>;
|
|
15
20
|
makeHeader: (epochNumber: number | bigint, slotNumber: number | bigint, blockNumber: number | bigint) => any;
|
|
16
21
|
getSigner: () => Secp256k1Signer;
|
|
@@ -29,24 +34,75 @@ export function sharedProposalValidatorTests<TProposal extends BlockProposal | C
|
|
|
29
34
|
|
|
30
35
|
describe('shared proposal validation logic', () => {
|
|
31
36
|
let epochCache: MockProxy<EpochCacheInterface>;
|
|
32
|
-
let validator: { validate: (proposal: TProposal) => Promise<
|
|
37
|
+
let validator: { validate: (proposal: TProposal) => Promise<ValidationResult> };
|
|
38
|
+
const previousSlot = getSlot(99);
|
|
39
|
+
const currentSlot = getSlot(100);
|
|
40
|
+
const nextSlot = getSlot(101);
|
|
41
|
+
|
|
42
|
+
function mockGetProposer(currentProposer: EthAddress, nextProposer: EthAddress, previousProposer?: EthAddress) {
|
|
43
|
+
epochCache.getProposerAttesterAddressInSlot.mockImplementation(slot => {
|
|
44
|
+
if (slot === currentSlot) {
|
|
45
|
+
return Promise.resolve(currentProposer);
|
|
46
|
+
}
|
|
47
|
+
if (slot === nextSlot) {
|
|
48
|
+
return Promise.resolve(nextProposer);
|
|
49
|
+
}
|
|
50
|
+
if (slot === previousSlot && previousProposer) {
|
|
51
|
+
return Promise.resolve(previousProposer);
|
|
52
|
+
}
|
|
53
|
+
throw new Error('Unexpected argument');
|
|
54
|
+
});
|
|
55
|
+
}
|
|
33
56
|
|
|
34
57
|
beforeEach(() => {
|
|
35
58
|
epochCache = epochCacheMock();
|
|
36
59
|
validator = validatorFactory(epochCache, { txsPermitted: true });
|
|
60
|
+
epochCache.getCurrentAndNextSlot.mockReturnValue({
|
|
61
|
+
currentSlot: currentSlot,
|
|
62
|
+
nextSlot: nextSlot,
|
|
63
|
+
});
|
|
37
64
|
});
|
|
38
65
|
|
|
39
|
-
it('returns high tolerance error if slot number is not current or next slot', async () => {
|
|
40
|
-
const header = makeHeader(1,
|
|
66
|
+
it('returns high tolerance error if slot number is not current or next slot (outside clock tolerance)', async () => {
|
|
67
|
+
const header = makeHeader(1, 99, 99);
|
|
41
68
|
const mockProposal = await makeProposal({ blockHeader: header, lastBlockHeader: header });
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
69
|
+
|
|
70
|
+
// Mock getEpochAndSlotNow to return time OUTSIDE clock tolerance (1000ms elapsed)
|
|
71
|
+
epochCache.getEpochAndSlotNow.mockReturnValue({
|
|
72
|
+
epoch: 1 as any,
|
|
73
|
+
slot: currentSlot,
|
|
74
|
+
ts: 1000n, // slot started at 1000 seconds
|
|
75
|
+
nowMs: 1001000n, // 1000ms elapsed, outside 500ms tolerance
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
epochCache.getProposerAttesterAddressInSlot.mockResolvedValue(getAddress());
|
|
79
|
+
const result = await validator.validate(mockProposal);
|
|
80
|
+
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.HighToleranceError });
|
|
81
|
+
|
|
82
|
+
// Should not try to resolve proposers if base validation fails
|
|
83
|
+
expect(epochCache.getProposerAttesterAddressInSlot).not.toHaveBeenCalled();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('returns ignore if previous slot proposal is within clock tolerance', async () => {
|
|
87
|
+
const previousProposer = getSigner();
|
|
88
|
+
const header = makeHeader(1, 99, 99);
|
|
89
|
+
const mockProposal = await makeProposal({
|
|
90
|
+
blockHeader: header,
|
|
91
|
+
lastBlockHeader: header,
|
|
92
|
+
signer: previousProposer,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Mock getEpochAndSlotNow to return time WITHIN clock tolerance (100ms elapsed)
|
|
96
|
+
epochCache.getEpochAndSlotNow.mockReturnValue({
|
|
97
|
+
epoch: 1 as any,
|
|
98
|
+
slot: currentSlot,
|
|
99
|
+
ts: 1000n, // slot started at 1000 seconds
|
|
100
|
+
nowMs: 1000100n, // 100ms elapsed, within 500ms tolerance
|
|
47
101
|
});
|
|
102
|
+
|
|
103
|
+
mockGetProposer(getAddress(), getAddress(), getAddress(previousProposer));
|
|
48
104
|
const result = await validator.validate(mockProposal);
|
|
49
|
-
expect(result).
|
|
105
|
+
expect(result).toEqual({ result: 'ignore' });
|
|
50
106
|
});
|
|
51
107
|
|
|
52
108
|
it('returns mid tolerance error if proposer is not current proposer for current slot', async () => {
|
|
@@ -59,14 +115,10 @@ export function sharedProposalValidatorTests<TProposal extends BlockProposal | C
|
|
|
59
115
|
lastBlockHeader: header,
|
|
60
116
|
signer: invalidProposer,
|
|
61
117
|
});
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
nextSlot: getSlot(101),
|
|
65
|
-
currentProposer: getAddress(currentProposer),
|
|
66
|
-
nextProposer: getAddress(nextProposer),
|
|
67
|
-
});
|
|
118
|
+
|
|
119
|
+
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
68
120
|
const result = await validator.validate(mockProposal);
|
|
69
|
-
expect(result).
|
|
121
|
+
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.MidToleranceError });
|
|
70
122
|
});
|
|
71
123
|
|
|
72
124
|
it('returns mid tolerance error if proposer is not next proposer for next slot', async () => {
|
|
@@ -79,14 +131,10 @@ export function sharedProposalValidatorTests<TProposal extends BlockProposal | C
|
|
|
79
131
|
lastBlockHeader: header,
|
|
80
132
|
signer: invalidProposer,
|
|
81
133
|
});
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
nextSlot: getSlot(101),
|
|
85
|
-
currentProposer: getAddress(currentProposer),
|
|
86
|
-
nextProposer: getAddress(nextProposer),
|
|
87
|
-
});
|
|
134
|
+
|
|
135
|
+
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
88
136
|
const result = await validator.validate(mockProposal);
|
|
89
|
-
expect(result).
|
|
137
|
+
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.MidToleranceError });
|
|
90
138
|
});
|
|
91
139
|
|
|
92
140
|
it('returns mid tolerance error if proposer is current proposer but proposal is for next slot', async () => {
|
|
@@ -98,14 +146,10 @@ export function sharedProposalValidatorTests<TProposal extends BlockProposal | C
|
|
|
98
146
|
lastBlockHeader: header,
|
|
99
147
|
signer: currentProposer,
|
|
100
148
|
});
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
nextSlot: getSlot(101),
|
|
104
|
-
currentProposer: getAddress(currentProposer),
|
|
105
|
-
nextProposer: getAddress(nextProposer),
|
|
106
|
-
});
|
|
149
|
+
|
|
150
|
+
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
107
151
|
const result = await validator.validate(mockProposal);
|
|
108
|
-
expect(result).
|
|
152
|
+
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.MidToleranceError });
|
|
109
153
|
});
|
|
110
154
|
|
|
111
155
|
it('returns undefined if proposal is valid for current slot and proposer', async () => {
|
|
@@ -117,14 +161,10 @@ export function sharedProposalValidatorTests<TProposal extends BlockProposal | C
|
|
|
117
161
|
lastBlockHeader: header,
|
|
118
162
|
signer: currentProposer,
|
|
119
163
|
});
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
nextSlot: getSlot(101),
|
|
123
|
-
currentProposer: getAddress(currentProposer),
|
|
124
|
-
nextProposer: getAddress(nextProposer),
|
|
125
|
-
});
|
|
164
|
+
|
|
165
|
+
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
126
166
|
const result = await validator.validate(mockProposal);
|
|
127
|
-
expect(result).
|
|
167
|
+
expect(result).toEqual({ result: 'accept' });
|
|
128
168
|
});
|
|
129
169
|
|
|
130
170
|
it('returns undefined if proposal is valid for next slot and proposer', async () => {
|
|
@@ -132,14 +172,10 @@ export function sharedProposalValidatorTests<TProposal extends BlockProposal | C
|
|
|
132
172
|
const nextProposer = getSigner();
|
|
133
173
|
const header = makeHeader(1, 101, 101);
|
|
134
174
|
const mockProposal = await makeProposal({ blockHeader: header, lastBlockHeader: header, signer: nextProposer });
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
nextSlot: getSlot(101),
|
|
138
|
-
currentProposer: getAddress(currentProposer),
|
|
139
|
-
nextProposer: getAddress(nextProposer),
|
|
140
|
-
});
|
|
175
|
+
|
|
176
|
+
mockGetProposer(getAddress(currentProposer), getAddress(nextProposer));
|
|
141
177
|
const result = await validator.validate(mockProposal);
|
|
142
|
-
expect(result).
|
|
178
|
+
expect(result).toEqual({ result: 'accept' });
|
|
143
179
|
});
|
|
144
180
|
|
|
145
181
|
describe('transaction permission validation', () => {
|
|
@@ -153,14 +189,10 @@ export function sharedProposalValidatorTests<TProposal extends BlockProposal | C
|
|
|
153
189
|
signer: currentProposer,
|
|
154
190
|
txHashes: getTxHashes(2),
|
|
155
191
|
});
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
nextSlot: getSlot(101),
|
|
159
|
-
currentProposer: getAddress(currentProposer),
|
|
160
|
-
nextProposer: getAddress(),
|
|
161
|
-
});
|
|
192
|
+
|
|
193
|
+
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
162
194
|
const result = await validatorWithTxsDisabled.validate(mockProposal);
|
|
163
|
-
expect(result).
|
|
195
|
+
expect(result).toEqual({ result: 'reject', severity: PeerErrorSeverity.MidToleranceError });
|
|
164
196
|
});
|
|
165
197
|
|
|
166
198
|
it('returns undefined if txs not permitted but proposal has no txHashes', async () => {
|
|
@@ -173,14 +205,10 @@ export function sharedProposalValidatorTests<TProposal extends BlockProposal | C
|
|
|
173
205
|
signer: currentProposer,
|
|
174
206
|
txHashes: getTxHashes(0),
|
|
175
207
|
});
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
nextSlot: getSlot(101),
|
|
179
|
-
currentProposer: getAddress(currentProposer),
|
|
180
|
-
nextProposer: getAddress(),
|
|
181
|
-
});
|
|
208
|
+
|
|
209
|
+
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
182
210
|
const result = await validatorWithTxsDisabled.validate(mockProposal);
|
|
183
|
-
expect(result).
|
|
211
|
+
expect(result).toEqual({ result: 'accept' });
|
|
184
212
|
});
|
|
185
213
|
|
|
186
214
|
it('returns undefined if txs permitted and proposal contains txHashes', async () => {
|
|
@@ -192,14 +220,10 @@ export function sharedProposalValidatorTests<TProposal extends BlockProposal | C
|
|
|
192
220
|
signer: currentProposer,
|
|
193
221
|
txHashes: getTxHashes(2),
|
|
194
222
|
});
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
nextSlot: getSlot(101),
|
|
198
|
-
currentProposer: getAddress(currentProposer),
|
|
199
|
-
nextProposer: getAddress(),
|
|
200
|
-
});
|
|
223
|
+
|
|
224
|
+
mockGetProposer(getAddress(currentProposer), getAddress());
|
|
201
225
|
const result = await validator.validate(mockProposal);
|
|
202
|
-
expect(result).
|
|
226
|
+
expect(result).toEqual({ result: 'accept' });
|
|
203
227
|
});
|
|
204
228
|
});
|
|
205
229
|
});
|
|
@@ -15,7 +15,9 @@ export class BlockHeaderTxValidator<T extends AnyTx> implements TxValidator<T> {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
async validateTx(tx: T): Promise<TxValidationResult> {
|
|
18
|
-
const [index] = await this.#archiveSource.getArchiveIndices([
|
|
18
|
+
const [index] = await this.#archiveSource.getArchiveIndices([
|
|
19
|
+
(await tx.data.constants.anchorBlockHeader.hash()).toField(),
|
|
20
|
+
]);
|
|
19
21
|
if (index === undefined) {
|
|
20
22
|
this.#log.verbose(`Rejecting tx ${'txHash' in tx ? tx.txHash : tx.hash} for referencing an unknown block header`);
|
|
21
23
|
return { result: 'invalid', reason: [TX_ERROR_BLOCK_HEADER] };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { getCallRequestsWithCalldataByPhase } from '@aztec/simulator/server';
|
|
2
|
+
import { FunctionSelector } from '@aztec/stdlib/abi';
|
|
3
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
4
|
+
import { type Tx, TxExecutionPhase } from '@aztec/stdlib/tx';
|
|
5
|
+
|
|
6
|
+
export type FeePayerBalanceDelta = {
|
|
7
|
+
feeLimit: bigint;
|
|
8
|
+
claimAmount: bigint;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const increasePublicBalanceSelectorPromise = FunctionSelector.fromSignature('_increase_public_balance((Field),u128)');
|
|
12
|
+
|
|
13
|
+
export function getTxFeeLimit(tx: Tx): bigint {
|
|
14
|
+
return tx.data.constants.txContext.gasSettings.getFeeLimit().toBigInt();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function getFeePayerClaimAmount(tx: Tx, feeJuiceAddress: AztecAddress): Promise<bigint> {
|
|
18
|
+
const setupFns = getCallRequestsWithCalldataByPhase(tx, TxExecutionPhase.SETUP);
|
|
19
|
+
const increasePublicBalanceSelector = await increasePublicBalanceSelectorPromise;
|
|
20
|
+
const feePayer = tx.data.feePayer;
|
|
21
|
+
|
|
22
|
+
const claimFunctionCall = setupFns.find(
|
|
23
|
+
fn =>
|
|
24
|
+
fn.request.contractAddress.equals(feeJuiceAddress) &&
|
|
25
|
+
fn.request.msgSender.equals(feeJuiceAddress) &&
|
|
26
|
+
fn.calldata.length > 2 &&
|
|
27
|
+
fn.functionSelector.equals(increasePublicBalanceSelector) &&
|
|
28
|
+
fn.args[0].equals(feePayer.toField()) &&
|
|
29
|
+
!fn.request.isStaticCall,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
return claimFunctionCall ? claimFunctionCall.args[1].toBigInt() : 0n;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function getFeePayerBalanceDelta(tx: Tx, feeJuiceAddress: AztecAddress): Promise<FeePayerBalanceDelta> {
|
|
36
|
+
return {
|
|
37
|
+
feeLimit: getTxFeeLimit(tx),
|
|
38
|
+
claimAmount: await getFeePayerClaimAmount(tx, feeJuiceAddress),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { AVM_MAX_PROCESSABLE_L2_GAS, FIXED_DA_GAS, FIXED_L2_GAS } from '@aztec/constants';
|
|
2
2
|
import { createLogger } from '@aztec/foundation/log';
|
|
3
3
|
import { computeFeePayerBalanceStorageSlot } from '@aztec/protocol-contracts/fee-juice';
|
|
4
|
-
import { getCallRequestsWithCalldataByPhase } from '@aztec/simulator/server';
|
|
5
|
-
import { FunctionSelector } from '@aztec/stdlib/abi';
|
|
6
4
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
7
5
|
import { Gas, GasFees } from '@aztec/stdlib/gas';
|
|
8
6
|
import type { PublicStateSource } from '@aztec/stdlib/trees';
|
|
@@ -12,11 +10,12 @@ import {
|
|
|
12
10
|
TX_ERROR_INSUFFICIENT_FEE_PER_GAS,
|
|
13
11
|
TX_ERROR_INSUFFICIENT_GAS_LIMIT,
|
|
14
12
|
type Tx,
|
|
15
|
-
TxExecutionPhase,
|
|
16
13
|
type TxValidationResult,
|
|
17
14
|
type TxValidator,
|
|
18
15
|
} from '@aztec/stdlib/tx';
|
|
19
16
|
|
|
17
|
+
import { getFeePayerClaimAmount, getTxFeeLimit } from './fee_payer_balance.js';
|
|
18
|
+
|
|
20
19
|
export class GasTxValidator implements TxValidator<Tx> {
|
|
21
20
|
#log = createLogger('sequencer:tx_validator:tx_gas');
|
|
22
21
|
#publicDataSource: PublicStateSource;
|
|
@@ -93,7 +92,7 @@ export class GasTxValidator implements TxValidator<Tx> {
|
|
|
93
92
|
const feePayer = tx.data.feePayer;
|
|
94
93
|
|
|
95
94
|
// Compute the maximum fee that this tx may pay, based on its gasLimits and maxFeePerGas
|
|
96
|
-
const feeLimit = tx
|
|
95
|
+
const feeLimit = getTxFeeLimit(tx);
|
|
97
96
|
|
|
98
97
|
// Read current balance of the feePayer
|
|
99
98
|
const initialBalance = await this.#publicDataSource.storageRead(
|
|
@@ -102,30 +101,14 @@ export class GasTxValidator implements TxValidator<Tx> {
|
|
|
102
101
|
);
|
|
103
102
|
|
|
104
103
|
// If there is a claim in this tx that increases the fee payer balance in Fee Juice, add it to balance
|
|
105
|
-
const
|
|
106
|
-
const
|
|
107
|
-
'_increase_public_balance((Field),u128)',
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
// Arguments of the claim function call:
|
|
111
|
-
// - args[0]: Amount recipient.
|
|
112
|
-
// - args[1]: Amount being claimed.
|
|
113
|
-
const claimFunctionCall = setupFns.find(
|
|
114
|
-
fn =>
|
|
115
|
-
fn.request.contractAddress.equals(this.#feeJuiceAddress) &&
|
|
116
|
-
fn.request.msgSender.equals(this.#feeJuiceAddress) &&
|
|
117
|
-
fn.calldata.length > 2 &&
|
|
118
|
-
fn.functionSelector.equals(increasePublicBalanceSelector) &&
|
|
119
|
-
fn.args[0].equals(feePayer.toField()) &&
|
|
120
|
-
!fn.request.isStaticCall,
|
|
121
|
-
);
|
|
104
|
+
const claimAmount = await getFeePayerClaimAmount(tx, this.#feeJuiceAddress);
|
|
105
|
+
const balance = initialBalance.toBigInt() + claimAmount;
|
|
122
106
|
|
|
123
|
-
|
|
124
|
-
if (balance.lt(feeLimit)) {
|
|
107
|
+
if (balance < feeLimit) {
|
|
125
108
|
this.#log.verbose(`Rejecting transaction due to not enough fee payer balance`, {
|
|
126
109
|
feePayer,
|
|
127
|
-
balance
|
|
128
|
-
feeLimit
|
|
110
|
+
balance,
|
|
111
|
+
feeLimit,
|
|
129
112
|
});
|
|
130
113
|
return { result: 'invalid', reason: [TX_ERROR_INSUFFICIENT_FEE_PAYER_BALANCE] };
|
|
131
114
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
+
import { MAX_TX_SIZE_KB } from '@aztec/stdlib/p2p';
|
|
3
|
+
import { Tx, type TxValidationResult, type TxValidator } from '@aztec/stdlib/tx';
|
|
4
|
+
|
|
5
|
+
export class SizeTxValidator implements TxValidator<Tx> {
|
|
6
|
+
#log = createLogger('sequencer:tx_validator:tx_size');
|
|
7
|
+
|
|
8
|
+
validateTx(tx: Tx): Promise<TxValidationResult> {
|
|
9
|
+
const txSize = tx.getSize();
|
|
10
|
+
if (txSize > MAX_TX_SIZE_KB * 1024) {
|
|
11
|
+
this.#log.verbose(
|
|
12
|
+
`Rejecting transaction ${tx.getTxHash().toString()}. Reason: size above size limit. ${txSize}bytes > ${MAX_TX_SIZE_KB}Kb`,
|
|
13
|
+
);
|
|
14
|
+
return Promise.resolve({ result: 'invalid', reason: ['Transaction size above size limit'] });
|
|
15
|
+
}
|
|
16
|
+
return Promise.resolve({ result: 'valid' });
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/services/encoding.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Taken from lodestar: https://github.com/ChainSafe/lodestar
|
|
2
2
|
import { sha256 } from '@aztec/foundation/crypto/sha256';
|
|
3
3
|
import { createLogger } from '@aztec/foundation/log';
|
|
4
|
-
import { TopicType, getTopicFromString } from '@aztec/stdlib/p2p';
|
|
4
|
+
import { MAX_TX_SIZE_KB, TopicType, getTopicFromString } from '@aztec/stdlib/p2p';
|
|
5
5
|
|
|
6
6
|
import type { RPC } from '@chainsafe/libp2p-gossipsub/message';
|
|
7
7
|
import type { DataTransform } from '@chainsafe/libp2p-gossipsub/types';
|
|
@@ -52,8 +52,7 @@ export function getMsgIdFn(message: Message) {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
const DefaultMaxSizesKb: Record<TopicType, number> = {
|
|
55
|
-
|
|
56
|
-
[TopicType.tx]: 512,
|
|
55
|
+
[TopicType.tx]: MAX_TX_SIZE_KB,
|
|
57
56
|
// An attestation has roughly 30 fields, which is 1kb, so 5x is plenty
|
|
58
57
|
[TopicType.checkpoint_attestation]: 5,
|
|
59
58
|
// Proposals may carry some tx objects, so we allow a larger size capped at 10mb
|
|
@@ -102,11 +102,11 @@ export class P2PInstrumentation {
|
|
|
102
102
|
continue;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
res.observe(metrics.avg, histogram.mean, { [Attributes.TOPIC_NAME]: topicName });
|
|
106
|
-
res.observe(metrics.max, histogram.max, { [Attributes.TOPIC_NAME]: topicName });
|
|
107
|
-
res.observe(metrics.min, histogram.min, { [Attributes.TOPIC_NAME]: topicName });
|
|
108
|
-
res.observe(metrics.p50, histogram.percentile(50), { [Attributes.TOPIC_NAME]: topicName });
|
|
109
|
-
res.observe(metrics.p90, histogram.percentile(90), { [Attributes.TOPIC_NAME]: topicName });
|
|
105
|
+
res.observe(metrics.avg, Math.ceil(histogram.mean), { [Attributes.TOPIC_NAME]: topicName });
|
|
106
|
+
res.observe(metrics.max, Math.ceil(histogram.max), { [Attributes.TOPIC_NAME]: topicName });
|
|
107
|
+
res.observe(metrics.min, Math.ceil(histogram.min), { [Attributes.TOPIC_NAME]: topicName });
|
|
108
|
+
res.observe(metrics.p50, Math.ceil(histogram.percentile(50)), { [Attributes.TOPIC_NAME]: topicName });
|
|
109
|
+
res.observe(metrics.p90, Math.ceil(histogram.percentile(90)), { [Attributes.TOPIC_NAME]: topicName });
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
};
|