@aztec/p2p 4.1.2 → 4.2.0-aztecnr-rc.2

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.
Files changed (73) hide show
  1. package/README.md +129 -3
  2. package/dest/client/factory.d.ts +1 -1
  3. package/dest/client/factory.d.ts.map +1 -1
  4. package/dest/client/factory.js +22 -16
  5. package/dest/client/p2p_client.d.ts +1 -1
  6. package/dest/client/p2p_client.d.ts.map +1 -1
  7. package/dest/client/p2p_client.js +10 -6
  8. package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts +1 -1
  9. package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -1
  10. package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.js +2 -1
  11. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts +1 -1
  12. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -1
  13. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.js +2 -1
  14. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +3 -1
  15. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
  16. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +9 -2
  17. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
  18. package/dest/mem_pools/tx_pool_v2/tx_metadata.js +7 -1
  19. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +4 -2
  20. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -1
  21. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.js +3 -0
  22. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +1 -1
  23. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
  24. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +11 -4
  25. package/dest/msg_validators/tx_validator/data_validator.d.ts +1 -1
  26. package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
  27. package/dest/msg_validators/tx_validator/data_validator.js +35 -2
  28. package/dest/msg_validators/tx_validator/factory.d.ts +9 -1
  29. package/dest/msg_validators/tx_validator/factory.d.ts.map +1 -1
  30. package/dest/msg_validators/tx_validator/factory.js +15 -2
  31. package/dest/msg_validators/tx_validator/phases_validator.d.ts +21 -1
  32. package/dest/msg_validators/tx_validator/phases_validator.d.ts.map +1 -1
  33. package/dest/msg_validators/tx_validator/phases_validator.js +28 -1
  34. package/dest/services/encoding.d.ts +5 -1
  35. package/dest/services/encoding.d.ts.map +1 -1
  36. package/dest/services/encoding.js +7 -1
  37. package/dest/services/libp2p/libp2p_service.d.ts +1 -1
  38. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  39. package/dest/services/libp2p/libp2p_service.js +5 -0
  40. package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts +5 -4
  41. package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts.map +1 -1
  42. package/dest/services/reqresp/rate-limiter/rate_limiter.js +10 -8
  43. package/dest/services/reqresp/reqresp.d.ts +1 -1
  44. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  45. package/dest/services/reqresp/reqresp.js +16 -8
  46. package/dest/services/tx_collection/file_store_tx_source.d.ts +4 -5
  47. package/dest/services/tx_collection/file_store_tx_source.d.ts.map +1 -1
  48. package/dest/services/tx_collection/file_store_tx_source.js +29 -39
  49. package/dest/services/tx_collection/tx_source.d.ts +5 -6
  50. package/dest/services/tx_collection/tx_source.d.ts.map +1 -1
  51. package/dest/services/tx_collection/tx_source.js +7 -9
  52. package/package.json +14 -14
  53. package/src/client/factory.ts +33 -23
  54. package/src/client/p2p_client.ts +13 -6
  55. package/src/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.ts +2 -1
  56. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.ts +2 -1
  57. package/src/mem_pools/tx_pool_v2/interfaces.ts +2 -0
  58. package/src/mem_pools/tx_pool_v2/tx_metadata.ts +11 -1
  59. package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +13 -1
  60. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +12 -4
  61. package/src/msg_validators/attestation_validator/README.md +49 -0
  62. package/src/msg_validators/proposal_validator/README.md +123 -0
  63. package/src/msg_validators/tx_validator/README.md +5 -1
  64. package/src/msg_validators/tx_validator/data_validator.ts +42 -1
  65. package/src/msg_validators/tx_validator/factory.ts +21 -1
  66. package/src/msg_validators/tx_validator/phases_validator.ts +31 -1
  67. package/src/services/encoding.ts +9 -1
  68. package/src/services/libp2p/libp2p_service.ts +5 -0
  69. package/src/services/reqresp/README.md +229 -0
  70. package/src/services/reqresp/rate-limiter/rate_limiter.ts +13 -9
  71. package/src/services/reqresp/reqresp.ts +18 -10
  72. package/src/services/tx_collection/file_store_tx_source.ts +31 -43
  73. package/src/services/tx_collection/tx_source.ts +7 -8
@@ -0,0 +1,229 @@
1
+ # ReqResp Protocols
2
+
3
+ This module implements libp2p request-response protocols for the Aztec P2P network. All protocols share common transport-level validation (rate limiting, timeouts, Snappy decompression, error penalties) with protocol-specific logic layered on top.
4
+
5
+ ## Common Transport Validation
6
+
7
+ ### Rate Limiting (Responder Side)
8
+
9
+ Applied before the protocol handler runs.
10
+
11
+ | Protocol | Peer Limit | Global Limit | File |
12
+ |----------|-----------|-------------|------|
13
+ | PING | 5/s | 10/s | `rate-limiter/rate_limits.ts` |
14
+ | STATUS | 5/s | 10/s | same |
15
+ | AUTH | 5/s | 10/s | same |
16
+ | GOODBYE | 5/s | 10/s | same |
17
+ | BLOCK | 2/s | 5/s | same |
18
+ | BLOCK_TXS | 10/s | 200/s | same |
19
+ | TX | (see rate limits file) | (see rate limits file) | same |
20
+
21
+ - Per-peer limit exceeded: `HighToleranceError` penalty + `RATE_LIMIT_EXCEEDED` status. Penalty fires inside `RequestResponseRateLimiter.allow()`, not the stream handler.
22
+ - Global limit exceeded: `RATE_LIMIT_EXCEEDED` status only (no peer penalty).
23
+
24
+ ### Response Status Byte (Requester Side)
25
+
26
+ | Rule | Consequence | File |
27
+ |------|-------------|------|
28
+ | First chunk must be exactly 1 byte | `ReqRespStatusError(UNKNOWN)` | `status.ts` |
29
+ | Byte must be valid `ReqRespStatus` enum (0-4, 126, 127) | `ReqRespStatusError(UNKNOWN)` | same |
30
+
31
+ Note: `prettyPrintReqRespStatus` is missing a `NOT_FOUND` case (minor logging bug).
32
+
33
+ ### Snappy Decompression (Requester Side)
34
+
35
+ Per-protocol size limits checked via preamble before decompression.
36
+
37
+ ### Timeouts (Requester Side)
38
+
39
+ | Timeout | Default | Penalty |
40
+ |---------|---------|---------|
41
+ | Individual request | 10s | HighToleranceError |
42
+ | Dial | 5s | HighToleranceError |
43
+
44
+ ### Error Penalty Categorization (Requester Side)
45
+
46
+ | Error Type | Severity |
47
+ |------------|----------|
48
+ | GOODBYE subprotocol errors | None |
49
+ | `CollectiveReqRespTimeoutError` / `InvalidResponseError` | None |
50
+ | `AbortError` / connection close / muxer closed | None |
51
+ | `ECONNRESET` / `EPIPE` / `ECONNREFUSED` / `ERR_UNEXPECTED_EOF` | HighToleranceError |
52
+ | `ERR_UNSUPPORTED_PROTOCOL` | HighToleranceError |
53
+ | `IndividualReqRespTimeoutError` / `TimeoutError` | HighToleranceError |
54
+ | Catch-all | HighToleranceError |
55
+
56
+ ### Request Error Penalty (Responder Side)
57
+
58
+ | Error Type | Severity |
59
+ |------------|----------|
60
+ | `BADLY_FORMED_REQUEST` | LowToleranceError |
61
+ | All others | None |
62
+
63
+ ### Notes
64
+
65
+ - Request payloads are NOT snappy-compressed (asymmetric: only responses use snappy).
66
+
67
+ ---
68
+
69
+ ## Handshake Protocols
70
+
71
+ ### Connection-Level Gating (Before Any Handshake)
72
+
73
+ | Rule | Consequence | File |
74
+ |------|-------------|------|
75
+ | Deny inbound connection from IP/peerId with too many failed auth handshakes | Connection denied | `libp2p_service.ts` |
76
+ | Threshold: `p2pMaxFailedAuthAttemptsAllowed` (default 3) | Tracked per peerId AND per IP | `peer_manager.ts` |
77
+ | Failed auth entries expire after 1 hour | Peer can reconnect; no escalating penalty for repeat offenders | same |
78
+
79
+ ### Handshake Trigger Logic (`peer:connect`)
80
+
81
+ 1. `p2pDisableStatusHandshake` = true: no handshake
82
+ 2. `p2pAllowOnlyValidators` = false: STATUS handshake
83
+ 3. Peer is protected (trusted/private/preferred): STATUS handshake
84
+ 4. Otherwise: AUTH handshake (superset of STATUS)
85
+
86
+ Config constraint: `p2pDisableStatusHandshake && p2pAllowOnlyValidators` is disallowed.
87
+
88
+ ### STATUS Protocol (`/aztec/req/status/1.0.0`)
89
+
90
+ **Requester side** (`peer_manager.ts`):
91
+
92
+ | Rule | Consequence |
93
+ |------|-------------|
94
+ | Response status must be SUCCESS | Peer scheduled for disconnect |
95
+ | `compressedComponentsVersion` must match | Peer scheduled for disconnect |
96
+ | Any exception | Peer scheduled for disconnect |
97
+
98
+ `StatusMessage.validate()` currently only checks `compressedComponentsVersion`. Fields `latestBlockNumber`, `latestBlockHash`, `finalizedBlockNumber` are NOT validated (TODO in code).
99
+
100
+ **Responder side**: no validation of incoming request content (always responds with own status). This means the requester leaks its blockchain state to any peer before validation.
101
+
102
+ **Deserialization bounds**: `MAX_VERSION_STRING_LENGTH` = 64 bytes, `MAX_BLOCK_HASH_STRING_LENGTH` = 128 bytes. Expected response size: 1 KB.
103
+
104
+ ### AUTH Protocol (`/aztec/req/auth/1.0.0`)
105
+
106
+ **Requester side** (`peer_manager.ts`):
107
+
108
+ | # | Rule | Consequence |
109
+ |---|------|-------------|
110
+ | 1 | Response status is SUCCESS | `markAuthHandshakeFailed` + disconnect |
111
+ | 2 | `compressedComponentsVersion` match | `markAuthHandshakeFailed` + disconnect |
112
+ | 3 | Valid ECDSA signature recovery from challenge response | `markAuthHandshakeFailed` + disconnect |
113
+ | 4 | Recovered address is a registered validator | `markAuthHandshakeFailed` + disconnect |
114
+ | 5 | Validator address not already authenticated to different peerId | Silent return (no disconnect, no failure marking -- peer stays connected but unauthenticated) |
115
+ | 6 | Any exception | `markAuthHandshakeFailed` + disconnect |
116
+
117
+ Challenge: random `Fr`, payload = `keccak256("Aztec Validator Challenge:" + challenge)`, signed with `eth_sign` style. Challenge is NOT bound to peer identity (transport encryption via Noise is the binding layer).
118
+
119
+ On success: peer added to authenticated maps, prior failures cleared (including IP-based ones -- shared-IP peers benefit from a legitimate validator's success).
120
+
121
+ **Responder side** (`validator-client/src/validator.ts` + `peer_manager.ts`):
122
+
123
+ | # | Rule | Consequence |
124
+ |---|------|-------------|
125
+ | 1 | Peer must be protected (`shouldTrustWithIdentity` in `peer_manager.ts`) | Returns empty buffer (SUCCESS status + empty payload -> requester gets parse error -> `markAuthHandshakeFailed`) |
126
+ | 2 | Node must have registered validator address | Returns empty buffer (same consequence) |
127
+
128
+ **Unauthenticated peer gossip**: when `p2pAllowOnlyValidators` is true, unauthenticated peers get `appSpecificScore = -Infinity`, completely excluding them from all gossip.
129
+
130
+ ### PING Protocol (`/aztec/req/ping/1.0.0`)
131
+
132
+ No validation on either side. Responder returns `Buffer.from('pong')`. Expected response: 1 KB.
133
+
134
+ ### GOODBYE Protocol (`/aztec/req/goodbye/1.0.0`)
135
+
136
+ **Responder**: buffer must be 1 byte (defaults to `UNKNOWN` on invalid length). Goodbye reason byte is NOT validated against the enum -- any byte 0-255 accepted. Peer scheduled for disconnect regardless of reason.
137
+
138
+ **Requester**: response errors are never penalized (GOODBYE subprotocol exempt from error categorization).
139
+
140
+ ### Periodic Re-validation
141
+
142
+ | Rule | Interval | File |
143
+ |------|----------|------|
144
+ | Authenticated validators re-checked against current validator set | Every heartbeat (`peerCheckIntervalMS`) | `peer_manager.ts` |
145
+ | If validator address no longer registered, auth entry removed | Same | same |
146
+
147
+ Protected peers (private/trusted/preferred) are always considered "authenticated" without AUTH handshake.
148
+
149
+ ---
150
+
151
+ ## Block Data Protocols
152
+
153
+ ### BLOCK Protocol (`/aztec/req/block/1.0.0`)
154
+
155
+ **Server side**:
156
+
157
+ | Rule | Consequence | File |
158
+ |------|-------------|------|
159
+ | Request must parse as `Fr` | `BADLY_FORMED_REQUEST` + LowToleranceError | `protocols/block.ts` |
160
+ | Block lookup throws | `INTERNAL_ERROR` status | same |
161
+ | Block not found | SUCCESS + empty buffer (design choice; no `NOT_FOUND` status used) | same |
162
+
163
+ **Requester side** (Snappy limit: 3 MB):
164
+
165
+ | Rule | Consequence | File |
166
+ |------|-------------|------|
167
+ | Response block number must match requested | LowToleranceError; rejected | `libp2p_service.ts` (`validateRequestedBlock`) |
168
+ | Local block must exist for hash verification | Rejected (no penalty) | same |
169
+ | Response block hash must equal local block hash | MidToleranceError; rejected | same |
170
+
171
+ **Limitation**: the local-block requirement means BLOCK req/resp is unusable for initial P2P-only sync (before L1 sync provides local copies for verification). A TODO in the code acknowledges this.
172
+
173
+ ### BLOCK_TXS Protocol (`/aztec/req/block_txs/1.0.0`)
174
+
175
+ **Server side**:
176
+
177
+ | Rule | Consequence | File |
178
+ |------|-------------|------|
179
+ | Request must parse as `BlockTxsRequest` (Fr + TxHashArray + BitVector) | `BADLY_FORMED_REQUEST` + LowToleranceError | `protocols/block_txs/block_txs_handler.ts` |
180
+ | BitVector length: non-negative and <= `MAX_TXS_PER_BLOCK` (65536) | Deserialization throws -> `BADLY_FORMED_REQUEST` | `protocols/block_txs/bitvector.ts` |
181
+ | Archive root not found and no explicit txHashes | `NOT_FOUND` status | handler |
182
+ | Internal error during lookup | Unhandled exception -> stream abort (no `INTERNAL_ERROR` status, unlike BLOCK) | handler |
183
+
184
+ Conditional registration: BLOCK_TXS handler only registered when `config.disableTransactions` is false. Otherwise peers get `ERR_UNSUPPORTED_PROTOCOL`.
185
+
186
+ **Requester side via `sendBatchRequest`** (Snappy limit: `max(N, 1) * 512 + 1` KB):
187
+
188
+ | Rule | Consequence | File |
189
+ |------|-------------|------|
190
+ | Archive root must match request | MidToleranceError | `libp2p_service.ts` (`validateRequestedBlockTxs`) |
191
+ | BitVector length must match request | MidToleranceError | same |
192
+ | No duplicate tx hashes | MidToleranceError | same |
193
+ | Tx count within bounds | MidToleranceError | same |
194
+ | Local block proposal must exist for archive root | Rejected (no penalty) | same |
195
+ | All tx hashes must be in proposal's tx list at allowed indices | LowToleranceError | same |
196
+ | Txs in strictly increasing index order | LowToleranceError | same |
197
+ | Each tx passes well-formedness (Metadata [4 fields], Size, Data, Proof) | LowToleranceError | same |
198
+
199
+ **Requester side via `BatchTxRequester`** (separate validation path):
200
+
201
+ | Rule | Consequence | File |
202
+ |------|-------------|------|
203
+ | Non-SUCCESS status: `FAILURE`/`UNKNOWN` | HighToleranceError + "bad peer" tracking | `batch-tx-requester/batch_tx_requester.ts` |
204
+ | `RATE_LIMIT_EXCEEDED` | Peer marked rate-limited (cooldown) | same |
205
+ | `NOT_FOUND` / `BADLY_FORMED_REQUEST` / `INTERNAL_ERROR` | Falls through silently (no penalty) | same |
206
+ | Each tx validated (Metadata + Size + Data + Proof) | LowToleranceError per invalid tx; valid txs from same response still accepted | same |
207
+ | Archive root match + non-empty txIndices | No penalty on mismatch; peer not promoted to "smart" | same |
208
+
209
+ **Double penalty on transport errors**: when `BatchTxRequester` encounters a transport error (e.g., ECONNRESET), both `sendRequestToPeer`'s internal handler and the `BatchTxRequester`'s catch block penalize the peer, resulting in double HighToleranceError.
210
+
211
+ See [BatchTxRequester README](batch-tx-requester/README.md) for the full architecture (peer classification, worker model, wire protocol).
212
+
213
+ ### TX Protocol (`/aztec/req/tx/1.0.0`)
214
+
215
+ **Server side**:
216
+
217
+ | Rule | Consequence | File |
218
+ |------|-------------|------|
219
+ | Request must parse as `TxHashArray` | `BADLY_FORMED_REQUEST` + LowToleranceError | `protocols/tx.ts` |
220
+
221
+ **Requester side** (validator registered at startup, not the default noop):
222
+
223
+ | Rule | Consequence | File |
224
+ |------|-------------|------|
225
+ | Each returned tx hash must be in the requested set | MidToleranceError | `libp2p_service.ts` (`validateRequestedTxs`) |
226
+ | Each tx passes well-formedness (Metadata + Size + Data + Proof) | LowToleranceError | same |
227
+
228
+ Snappy limit: `max(N, 1) * 512 + 1` KB.
229
+
@@ -97,9 +97,10 @@ export function prettyPrintRateLimitStatus(status: RateLimitStatus) {
97
97
  * 2. Individual rate limits for each peer.
98
98
  *
99
99
  * How it works:
100
- * - When a request comes in, it first checks against the global rate limit.
101
- * - If the global limit allows, it then checks against the specific peer's rate limit.
102
- * - The request is only allowed if both the global and peer-specific limits allow it.
100
+ * - When a request comes in, it first checks against the peer's individual rate limit.
101
+ * - If the peer limit allows, it then checks against the global rate limit.
102
+ * - The request is only allowed if both the peer-specific and global limits allow it.
103
+ * - Checking peer limit first ensures a rate-limited peer cannot exhaust the global quota.
103
104
  * - It automatically creates and manages rate limiters for new peers as they make requests.
104
105
  * - It periodically cleans up rate limiters for inactive peers to conserve memory.
105
106
  *
@@ -119,10 +120,6 @@ export class SubProtocolRateLimiter {
119
120
  }
120
121
 
121
122
  allow(peerId: PeerId): RateLimitStatus {
122
- if (!this.globalLimiter.allow()) {
123
- return RateLimitStatus.DeniedGlobal;
124
- }
125
-
126
123
  const peerIdStr = peerId.toString();
127
124
  let peerLimiter: PeerRateLimiter | undefined = this.peerLimiters.get(peerIdStr);
128
125
  if (!peerLimiter) {
@@ -135,10 +132,17 @@ export class SubProtocolRateLimiter {
135
132
  } else {
136
133
  peerLimiter.lastAccess = Date.now();
137
134
  }
138
- const peerLimitAllowed = peerLimiter.limiter.allow();
139
- if (!peerLimitAllowed) {
135
+
136
+ // Check peer limit first: a rate-limited peer must not consume global quota,
137
+ // otherwise one spamming peer can starve all others by exhausting the global bucket.
138
+ if (!peerLimiter.limiter.allow()) {
140
139
  return RateLimitStatus.DeniedPeer;
141
140
  }
141
+
142
+ if (!this.globalLimiter.allow()) {
143
+ return RateLimitStatus.DeniedGlobal;
144
+ }
145
+
142
146
  return RateLimitStatus.Allowed;
143
147
  }
144
148
 
@@ -16,7 +16,7 @@ import {
16
16
  IndividualReqRespTimeoutError,
17
17
  InvalidResponseError,
18
18
  } from '../../errors/reqresp.error.js';
19
- import { SnappyTransform } from '../encoding.js';
19
+ import { OversizedSnappyResponseError, SnappyTransform } from '../encoding.js';
20
20
  import type { PeerScoring } from '../peer-manager/peer_scoring.js';
21
21
  import {
22
22
  DEFAULT_INDIVIDUAL_REQUEST_TIMEOUT_MS,
@@ -553,16 +553,10 @@ export class ReqResp implements ReqRespInterface {
553
553
  data: message,
554
554
  };
555
555
  } catch (e: any) {
556
+ // All errors (invalid status bytes, oversized snappy responses, corrupt data, etc.)
557
+ // are re-thrown so the caller can penalize the peer via handleResponseError.
556
558
  this.logger.debug(`Reading message failed: ${e.message}`);
557
-
558
- let status = ReqRespStatus.UNKNOWN;
559
- if (e instanceof ReqRespStatusError) {
560
- status = e.status;
561
- }
562
-
563
- return {
564
- status,
565
- };
559
+ throw e;
566
560
  }
567
561
  }
568
562
 
@@ -778,6 +772,20 @@ export class ReqResp implements ReqRespInterface {
778
772
  return undefined;
779
773
  }
780
774
 
775
+ // Invalid status byte: the peer sent a status byte that doesn't match any known status code.
776
+ // This is a protocol violation, penalize harshly.
777
+ if (e instanceof ReqRespStatusError) {
778
+ this.logger.warn(`Invalid status byte from peer ${peerId.toString()} in ${subProtocol}: ${e.message}`, logTags);
779
+ return PeerErrorSeverity.LowToleranceError;
780
+ }
781
+
782
+ // Oversized snappy response: the peer is sending data that exceeds the allowed size.
783
+ // This is a protocol violation that wastes bandwidth, so penalize harshly.
784
+ if (e instanceof OversizedSnappyResponseError) {
785
+ this.logger.warn(`Oversized response from peer ${peerId.toString()} in ${subProtocol}: ${e.message}`, logTags);
786
+ return PeerErrorSeverity.LowToleranceError;
787
+ }
788
+
781
789
  return this.categorizeConnectionErrors(e, peerId, subProtocol);
782
790
  }
783
791
 
@@ -1,8 +1,7 @@
1
- import { partitionAsync } from '@aztec/foundation/collection';
2
1
  import { type Logger, createLogger } from '@aztec/foundation/log';
3
2
  import { Timer } from '@aztec/foundation/timer';
4
3
  import { type ReadOnlyFileStore, createReadOnlyFileStore } from '@aztec/stdlib/file-store';
5
- import { Tx, type TxHash, type TxValidator } from '@aztec/stdlib/tx';
4
+ import { Tx, type TxHash } from '@aztec/stdlib/tx';
6
5
  import {
7
6
  type Histogram,
8
7
  Metrics,
@@ -24,7 +23,6 @@ export class FileStoreTxSource implements TxSource {
24
23
  private readonly fileStore: ReadOnlyFileStore,
25
24
  private readonly baseUrl: string,
26
25
  private readonly basePath: string,
27
- private readonly txValidator: TxValidator,
28
26
  private readonly log: Logger,
29
27
  telemetry: TelemetryClient,
30
28
  ) {
@@ -46,7 +44,6 @@ export class FileStoreTxSource implements TxSource {
46
44
  public static async create(
47
45
  url: string,
48
46
  basePath: string,
49
- txValidator: TxValidator,
50
47
  log: Logger = createLogger('p2p:file_store_tx_source'),
51
48
  telemetry: TelemetryClient = getTelemetryClient(),
52
49
  ): Promise<FileStoreTxSource | undefined> {
@@ -56,7 +53,7 @@ export class FileStoreTxSource implements TxSource {
56
53
  log.warn(`Failed to create file store for URL: ${url}`);
57
54
  return undefined;
58
55
  }
59
- return new FileStoreTxSource(fileStore, url, basePath, txValidator, log, telemetry);
56
+ return new FileStoreTxSource(fileStore, url, basePath, log, telemetry);
60
57
  } catch (err) {
61
58
  log.warn(`Error creating file store for URL: ${url}`, { error: err });
62
59
  return undefined;
@@ -68,41 +65,35 @@ export class FileStoreTxSource implements TxSource {
68
65
  }
69
66
 
70
67
  public async getTxsByHash(txHashes: TxHash[]): Promise<TxSourceCollectionResult> {
71
- const results = await Promise.all(
72
- txHashes.map(async txHash => {
73
- const path = `${this.basePath}/txs/${txHash.toString()}.bin`;
74
- const timer = new Timer();
75
- try {
76
- const buffer = await this.fileStore.read(path);
77
- const tx = Tx.fromBuffer(buffer);
78
- return { tx, downloadDuration: timer.ms(), downloadSize: buffer.length };
79
- } catch {
80
- this.downloadsFailed.add(1);
81
- return undefined;
82
- }
83
- }),
84
- );
85
-
86
- const txs = results.filter(tx => tx !== undefined);
87
- const [validTxs, invalidTxs] = await partitionAsync(
88
- txs,
89
- async ({ tx, downloadDuration, downloadSize }): Promise<boolean> => {
90
- const valid = await this.txValidator.validateTx(tx);
91
- if (valid.result === 'valid') {
92
- this.downloadsSuccess.add(1);
93
- this.downloadDuration.record(Math.ceil(downloadDuration));
94
- this.downloadSize.record(downloadSize);
95
- return true;
96
- } else {
97
- this.downloadsFailed.add(1);
98
- return false;
99
- }
100
- },
101
- );
102
-
68
+ const invalidTxHashes: string[] = [];
103
69
  return {
104
- validTxs: validTxs.map(({ tx }) => tx),
105
- invalidTxHashes: invalidTxs.map(({ tx }) => tx.getTxHash().toString()),
70
+ validTxs: (
71
+ await Promise.all(
72
+ txHashes.map(async txHash => {
73
+ const path = `${this.basePath}/txs/${txHash.toString()}.bin`;
74
+ const timer = new Timer();
75
+ try {
76
+ const buffer = await this.fileStore.read(path);
77
+ const tx = Tx.fromBuffer(buffer);
78
+ if ((await tx.validateTxHash()) && txHash.equals(tx.txHash)) {
79
+ this.downloadsSuccess.add(1);
80
+ this.downloadDuration.record(Math.ceil(timer.ms()));
81
+ this.downloadSize.record(buffer.length);
82
+ return tx;
83
+ } else {
84
+ invalidTxHashes.push(tx.txHash.toString());
85
+ this.downloadsFailed.add(1);
86
+ return undefined;
87
+ }
88
+ } catch {
89
+ // Tx not found or error reading - return undefined
90
+ this.downloadsFailed.add(1);
91
+ return undefined;
92
+ }
93
+ }),
94
+ )
95
+ ).filter(tx => tx !== undefined),
96
+ invalidTxHashes: invalidTxHashes,
106
97
  };
107
98
  }
108
99
  }
@@ -118,12 +109,9 @@ export class FileStoreTxSource implements TxSource {
118
109
  export async function createFileStoreTxSources(
119
110
  urls: string[],
120
111
  basePath: string,
121
- txValidator: TxValidator,
122
112
  log: Logger = createLogger('p2p:file_store_tx_source'),
123
113
  telemetry: TelemetryClient = getTelemetryClient(),
124
114
  ): Promise<FileStoreTxSource[]> {
125
- const sources = await Promise.all(
126
- urls.map(url => FileStoreTxSource.create(url, basePath, txValidator, log, telemetry)),
127
- );
115
+ const sources = await Promise.all(urls.map(url => FileStoreTxSource.create(url, basePath, log, telemetry)));
128
116
  return sources.filter((s): s is FileStoreTxSource => s !== undefined);
129
117
  }
@@ -2,7 +2,7 @@ import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
2
2
  import { protocolContractsHash } from '@aztec/protocol-contracts';
3
3
  import type { ChainConfig } from '@aztec/stdlib/config';
4
4
  import { type AztecNode, createAztecNodeClient } from '@aztec/stdlib/interfaces/client';
5
- import type { Tx, TxHash, TxValidator } from '@aztec/stdlib/tx';
5
+ import type { Tx, TxHash } from '@aztec/stdlib/tx';
6
6
  import { type ComponentsVersions, getComponentsVersionsFromConfig } from '@aztec/stdlib/versioning';
7
7
  import { makeTracedFetch } from '@aztec/telemetry-client';
8
8
 
@@ -16,13 +16,12 @@ export interface TxSource {
16
16
  export class NodeRpcTxSource implements TxSource {
17
17
  constructor(
18
18
  private readonly client: Pick<AztecNode, 'getTxsByHash'>,
19
- private readonly txValidator: TxValidator,
20
19
  private readonly info: string,
21
20
  ) {}
22
21
 
23
- public static fromUrl(nodeUrl: string, txValidator: TxValidator, versions: ComponentsVersions): NodeRpcTxSource {
22
+ public static fromUrl(nodeUrl: string, versions: ComponentsVersions): NodeRpcTxSource {
24
23
  const client = createAztecNodeClient(nodeUrl, versions, makeTracedFetch([1, 2, 3], false));
25
- return new NodeRpcTxSource(client, txValidator, nodeUrl);
24
+ return new NodeRpcTxSource(client, nodeUrl);
26
25
  }
27
26
 
28
27
  public getInfo() {
@@ -39,8 +38,8 @@ export class NodeRpcTxSource implements TxSource {
39
38
  const invalidTxHashes: string[] = [];
40
39
  await Promise.all(
41
40
  txs.map(async tx => {
42
- const validation = await this.txValidator.validateTx(tx);
43
- if (validation.result === 'valid') {
41
+ const isValid = await tx.validateTxHash();
42
+ if (isValid) {
44
43
  validTxs.push(tx);
45
44
  } else {
46
45
  invalidTxHashes.push(tx.getTxHash().toString());
@@ -51,7 +50,7 @@ export class NodeRpcTxSource implements TxSource {
51
50
  }
52
51
  }
53
52
 
54
- export function createNodeRpcTxSources(urls: string[], txValidator: TxValidator, chainConfig: ChainConfig) {
53
+ export function createNodeRpcTxSources(urls: string[], chainConfig: ChainConfig) {
55
54
  const versions = getComponentsVersionsFromConfig(chainConfig, protocolContractsHash, getVKTreeRoot());
56
- return urls.map(url => NodeRpcTxSource.fromUrl(url, txValidator, versions));
55
+ return urls.map(url => NodeRpcTxSource.fromUrl(url, versions));
57
56
  }