@aztec/p2p 0.0.1-commit.4ad48494d → 0.0.1-commit.4eabbdb

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 (182) hide show
  1. package/dest/client/factory.d.ts +3 -2
  2. package/dest/client/factory.d.ts.map +1 -1
  3. package/dest/client/factory.js +3 -2
  4. package/dest/client/interface.d.ts +7 -9
  5. package/dest/client/interface.d.ts.map +1 -1
  6. package/dest/client/p2p_client.d.ts +4 -7
  7. package/dest/client/p2p_client.d.ts.map +1 -1
  8. package/dest/client/p2p_client.js +38 -14
  9. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.js +5 -5
  10. package/dest/config.d.ts +3 -2
  11. package/dest/config.d.ts.map +1 -1
  12. package/dest/errors/tx-pool.error.d.ts +8 -0
  13. package/dest/errors/tx-pool.error.d.ts.map +1 -0
  14. package/dest/errors/tx-pool.error.js +9 -0
  15. package/dest/mem_pools/attestation_pool/mocks.d.ts +2 -2
  16. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  17. package/dest/mem_pools/attestation_pool/mocks.js +2 -2
  18. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.js +3 -3
  19. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts +3 -1
  20. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts.map +1 -1
  21. package/dest/mem_pools/tx_pool_v2/deleted_pool.js +9 -0
  22. package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.d.ts +3 -3
  23. package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.d.ts.map +1 -1
  24. package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.js +18 -9
  25. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.js +2 -2
  26. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts +3 -3
  27. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts.map +1 -1
  28. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.js +10 -4
  29. package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts +2 -2
  30. package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts.map +1 -1
  31. package/dest/mem_pools/tx_pool_v2/eviction/index.js +1 -1
  32. package/dest/mem_pools/tx_pool_v2/eviction/interfaces.d.ts +48 -5
  33. package/dest/mem_pools/tx_pool_v2/eviction/interfaces.d.ts.map +1 -1
  34. package/dest/mem_pools/tx_pool_v2/eviction/interfaces.js +8 -0
  35. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.js +4 -4
  36. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.js +3 -3
  37. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts +1 -1
  38. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts.map +1 -1
  39. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.js +6 -4
  40. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.d.ts +4 -4
  41. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.d.ts.map +1 -1
  42. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.js +14 -4
  43. package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.d.ts +3 -3
  44. package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.d.ts.map +1 -1
  45. package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.js +2 -2
  46. package/dest/mem_pools/tx_pool_v2/instrumentation.d.ts +15 -0
  47. package/dest/mem_pools/tx_pool_v2/instrumentation.d.ts.map +1 -0
  48. package/dest/mem_pools/tx_pool_v2/instrumentation.js +43 -0
  49. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +10 -2
  50. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
  51. package/dest/mem_pools/tx_pool_v2/interfaces.js +2 -1
  52. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +7 -5
  53. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
  54. package/dest/mem_pools/tx_pool_v2/tx_metadata.js +29 -5
  55. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts +5 -2
  56. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts.map +1 -1
  57. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.js +12 -2
  58. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +5 -2
  59. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -1
  60. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.js +6 -5
  61. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +10 -4
  62. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
  63. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +135 -37
  64. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +2 -2
  65. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -1
  66. package/dest/msg_validators/tx_validator/timestamp_validator.js +6 -6
  67. package/dest/services/dummy_service.d.ts +3 -2
  68. package/dest/services/dummy_service.d.ts.map +1 -1
  69. package/dest/services/dummy_service.js +3 -0
  70. package/dest/services/encoding.d.ts +1 -1
  71. package/dest/services/encoding.d.ts.map +1 -1
  72. package/dest/services/encoding.js +2 -1
  73. package/dest/services/gossipsub/topic_score_params.d.ts +18 -6
  74. package/dest/services/gossipsub/topic_score_params.d.ts.map +1 -1
  75. package/dest/services/gossipsub/topic_score_params.js +32 -10
  76. package/dest/services/libp2p/libp2p_service.d.ts +2 -1
  77. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  78. package/dest/services/libp2p/libp2p_service.js +7 -3
  79. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts +4 -3
  80. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts.map +1 -1
  81. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.js +5 -9
  82. package/dest/services/reqresp/batch-tx-requester/interface.d.ts +2 -6
  83. package/dest/services/reqresp/batch-tx-requester/interface.d.ts.map +1 -1
  84. package/dest/services/reqresp/batch-tx-requester/missing_txs.d.ts +10 -13
  85. package/dest/services/reqresp/batch-tx-requester/missing_txs.d.ts.map +1 -1
  86. package/dest/services/reqresp/batch-tx-requester/missing_txs.js +25 -46
  87. package/dest/services/service.d.ts +4 -2
  88. package/dest/services/service.d.ts.map +1 -1
  89. package/dest/services/tx_collection/fast_tx_collection.d.ts +1 -1
  90. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
  91. package/dest/services/tx_collection/fast_tx_collection.js +39 -33
  92. package/dest/services/tx_collection/file_store_tx_collection.d.ts +1 -1
  93. package/dest/services/tx_collection/file_store_tx_collection.d.ts.map +1 -1
  94. package/dest/services/tx_collection/file_store_tx_collection.js +4 -2
  95. package/dest/services/tx_collection/file_store_tx_source.d.ts +15 -6
  96. package/dest/services/tx_collection/file_store_tx_source.d.ts.map +1 -1
  97. package/dest/services/tx_collection/file_store_tx_source.js +47 -16
  98. package/dest/services/tx_collection/instrumentation.d.ts +1 -1
  99. package/dest/services/tx_collection/instrumentation.d.ts.map +1 -1
  100. package/dest/services/tx_collection/instrumentation.js +2 -1
  101. package/dest/services/tx_collection/missing_txs_tracker.d.ts +32 -0
  102. package/dest/services/tx_collection/missing_txs_tracker.d.ts.map +1 -0
  103. package/dest/services/tx_collection/missing_txs_tracker.js +27 -0
  104. package/dest/services/tx_collection/proposal_tx_collector.d.ts +7 -6
  105. package/dest/services/tx_collection/proposal_tx_collector.d.ts.map +1 -1
  106. package/dest/services/tx_collection/proposal_tx_collector.js +5 -4
  107. package/dest/services/tx_collection/slow_tx_collection.d.ts +2 -2
  108. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -1
  109. package/dest/services/tx_collection/slow_tx_collection.js +10 -8
  110. package/dest/services/tx_collection/tx_collection.d.ts +5 -4
  111. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
  112. package/dest/services/tx_collection/tx_collection_sink.d.ts +6 -5
  113. package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -1
  114. package/dest/services/tx_collection/tx_collection_sink.js +13 -22
  115. package/dest/services/tx_collection/tx_source.d.ts +8 -3
  116. package/dest/services/tx_collection/tx_source.d.ts.map +1 -1
  117. package/dest/services/tx_collection/tx_source.js +19 -2
  118. package/dest/services/tx_file_store/tx_file_store.js +1 -1
  119. package/dest/test-helpers/mock-pubsub.d.ts +3 -2
  120. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  121. package/dest/test-helpers/mock-pubsub.js +6 -0
  122. package/dest/test-helpers/testbench-utils.d.ts +5 -2
  123. package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
  124. package/dest/test-helpers/testbench-utils.js +1 -1
  125. package/dest/testbench/p2p_client_testbench_worker.d.ts +2 -2
  126. package/dest/testbench/p2p_client_testbench_worker.d.ts.map +1 -1
  127. package/dest/testbench/p2p_client_testbench_worker.js +8 -8
  128. package/dest/util.d.ts +2 -2
  129. package/dest/util.d.ts.map +1 -1
  130. package/package.json +14 -14
  131. package/src/client/factory.ts +5 -2
  132. package/src/client/interface.ts +14 -9
  133. package/src/client/p2p_client.ts +44 -17
  134. package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker.ts +18 -8
  135. package/src/config.ts +1 -1
  136. package/src/errors/tx-pool.error.ts +12 -0
  137. package/src/mem_pools/attestation_pool/mocks.ts +2 -1
  138. package/src/mem_pools/tx_pool/README.md +1 -1
  139. package/src/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.ts +3 -3
  140. package/src/mem_pools/tx_pool_v2/README.md +1 -1
  141. package/src/mem_pools/tx_pool_v2/deleted_pool.ts +11 -0
  142. package/src/mem_pools/tx_pool_v2/eviction/eviction_manager.ts +21 -8
  143. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.ts +2 -2
  144. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.ts +15 -4
  145. package/src/mem_pools/tx_pool_v2/eviction/index.ts +4 -0
  146. package/src/mem_pools/tx_pool_v2/eviction/interfaces.ts +49 -4
  147. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.ts +4 -4
  148. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.ts +3 -3
  149. package/src/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.ts +6 -7
  150. package/src/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.ts +24 -6
  151. package/src/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.ts +3 -3
  152. package/src/mem_pools/tx_pool_v2/instrumentation.ts +69 -0
  153. package/src/mem_pools/tx_pool_v2/interfaces.ts +8 -2
  154. package/src/mem_pools/tx_pool_v2/tx_metadata.ts +40 -9
  155. package/src/mem_pools/tx_pool_v2/tx_pool_indices.ts +14 -3
  156. package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +11 -6
  157. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +157 -29
  158. package/src/msg_validators/tx_validator/timestamp_validator.ts +7 -7
  159. package/src/services/dummy_service.ts +5 -1
  160. package/src/services/encoding.ts +2 -1
  161. package/src/services/gossipsub/README.md +29 -14
  162. package/src/services/gossipsub/topic_score_params.ts +49 -13
  163. package/src/services/libp2p/libp2p_service.ts +7 -2
  164. package/src/services/reqresp/batch-tx-requester/batch_tx_requester.ts +6 -6
  165. package/src/services/reqresp/batch-tx-requester/interface.ts +1 -5
  166. package/src/services/reqresp/batch-tx-requester/missing_txs.ts +23 -71
  167. package/src/services/service.ts +10 -1
  168. package/src/services/tx_collection/fast_tx_collection.ts +51 -30
  169. package/src/services/tx_collection/file_store_tx_collection.ts +7 -3
  170. package/src/services/tx_collection/file_store_tx_source.ts +61 -17
  171. package/src/services/tx_collection/instrumentation.ts +7 -1
  172. package/src/services/tx_collection/missing_txs_tracker.ts +52 -0
  173. package/src/services/tx_collection/proposal_tx_collector.ts +8 -7
  174. package/src/services/tx_collection/slow_tx_collection.ts +8 -9
  175. package/src/services/tx_collection/tx_collection.ts +4 -3
  176. package/src/services/tx_collection/tx_collection_sink.ts +15 -29
  177. package/src/services/tx_collection/tx_source.ts +22 -3
  178. package/src/services/tx_file_store/tx_file_store.ts +1 -1
  179. package/src/test-helpers/mock-pubsub.ts +10 -0
  180. package/src/test-helpers/testbench-utils.ts +2 -2
  181. package/src/testbench/p2p_client_testbench_worker.ts +20 -13
  182. package/src/util.ts +7 -1
@@ -9,6 +9,7 @@ import type { L2Block, L2BlockId, L2BlockSource } from '@aztec/stdlib/block';
9
9
  import type { WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
10
10
  import { DatabasePublicStateSource } from '@aztec/stdlib/trees';
11
11
  import { BlockHeader, Tx, TxHash, type TxValidator } from '@aztec/stdlib/tx';
12
+ import type { TelemetryClient } from '@aztec/telemetry-client';
12
13
 
13
14
  import { TxArchive } from './archive/index.js';
14
15
  import { DeletedPool } from './deleted_pool.js';
@@ -22,8 +23,12 @@ import {
22
23
  LowPriorityPreAddRule,
23
24
  NullifierConflictRule,
24
25
  type PoolOperations,
26
+ type PreAddContext,
25
27
  type PreAddPoolAccess,
28
+ TxPoolRejectionCode,
29
+ type TxPoolRejectionError,
26
30
  } from './eviction/index.js';
31
+ import { TxPoolV2Instrumentation } from './instrumentation.js';
27
32
  import {
28
33
  type AddTxsResult,
29
34
  DEFAULT_TX_POOL_V2_CONFIG,
@@ -66,6 +71,8 @@ export class TxPoolV2Impl {
66
71
  #deletedPool: DeletedPool;
67
72
  #evictionManager: EvictionManager;
68
73
  #dateProvider: DateProvider;
74
+ #instrumentation: TxPoolV2Instrumentation;
75
+ #evictedTxHashes: Set<string> = new Set();
69
76
  #log: Logger;
70
77
  #callbacks: TxPoolV2Callbacks;
71
78
 
@@ -74,6 +81,7 @@ export class TxPoolV2Impl {
74
81
  archiveStore: AztecAsyncKVStore,
75
82
  deps: TxPoolV2Dependencies,
76
83
  callbacks: TxPoolV2Callbacks,
84
+ telemetry: TelemetryClient,
77
85
  config: Partial<TxPoolV2Config> = {},
78
86
  dateProvider: DateProvider,
79
87
  log: Logger,
@@ -89,6 +97,7 @@ export class TxPoolV2Impl {
89
97
  this.#archive = new TxArchive(archiveStore, this.#config.archivedTxLimit, log);
90
98
  this.#deletedPool = new DeletedPool(store, this.#txsDB, log);
91
99
  this.#dateProvider = dateProvider;
100
+ this.#instrumentation = new TxPoolV2Instrumentation(telemetry, () => this.#indices.getTotalMetadataBytes());
92
101
  this.#log = log;
93
102
  this.#callbacks = callbacks;
94
103
 
@@ -171,13 +180,16 @@ export class TxPoolV2Impl {
171
180
  this.#log.info(`Deleted ${toDelete.length} invalid/rejected transactions on startup`, { txHashes: toDelete });
172
181
  }
173
182
 
174
- async addPendingTxs(txs: Tx[], opts: { source?: string }): Promise<AddTxsResult> {
183
+ async addPendingTxs(txs: Tx[], opts: { source?: string; feeComparisonOnly?: boolean }): Promise<AddTxsResult> {
175
184
  const accepted: TxHash[] = [];
176
185
  const ignored: TxHash[] = [];
177
186
  const rejected: TxHash[] = [];
187
+ const errors = new Map<string, TxPoolRejectionError>();
178
188
  const acceptedPending = new Set<string>();
179
189
 
180
190
  const poolAccess = this.#createPreAddPoolAccess();
191
+ const preAddContext: PreAddContext | undefined =
192
+ opts.feeComparisonOnly !== undefined ? { feeComparisonOnly: opts.feeComparisonOnly } : undefined;
181
193
 
182
194
  await this.#store.transactionAsync(async () => {
183
195
  for (const tx of txs) {
@@ -204,7 +216,15 @@ export class TxPoolV2Impl {
204
216
  accepted.push(txHash);
205
217
  } else {
206
218
  // Regular pending tx - validate and run pre-add rules
207
- const result = await this.#tryAddRegularPendingTx(tx, opts, poolAccess, acceptedPending, ignored);
219
+ const result = await this.#tryAddRegularPendingTx(
220
+ tx,
221
+ opts,
222
+ poolAccess,
223
+ acceptedPending,
224
+ ignored,
225
+ errors,
226
+ preAddContext,
227
+ );
208
228
  if (result.status === 'accepted') {
209
229
  acceptedPending.add(txHashStr);
210
230
  } else if (result.status === 'rejected') {
@@ -221,6 +241,14 @@ export class TxPoolV2Impl {
221
241
  accepted.push(TxHash.fromString(txHashStr));
222
242
  }
223
243
 
244
+ // Record metrics
245
+ if (ignored.length > 0) {
246
+ this.#instrumentation.recordIgnored(ignored.length);
247
+ }
248
+ if (rejected.length > 0) {
249
+ this.#instrumentation.recordRejected(rejected.length);
250
+ }
251
+
224
252
  // Run post-add eviction rules for pending txs
225
253
  if (acceptedPending.size > 0) {
226
254
  const feePayers = Array.from(acceptedPending).map(txHash => this.#indices.getMetadata(txHash)!.feePayer);
@@ -228,7 +256,7 @@ export class TxPoolV2Impl {
228
256
  await this.#evictionManager.evictAfterNewTxs(Array.from(acceptedPending), [...uniqueFeePayers]);
229
257
  }
230
258
 
231
- return { accepted, ignored, rejected };
259
+ return { accepted, ignored, rejected, ...(errors.size > 0 ? { errors } : {}) };
232
260
  }
233
261
 
234
262
  /** Validates and adds a regular pending tx. Returns status. */
@@ -238,6 +266,8 @@ export class TxPoolV2Impl {
238
266
  poolAccess: PreAddPoolAccess,
239
267
  acceptedPending: Set<string>,
240
268
  ignored: TxHash[],
269
+ errors: Map<string, TxPoolRejectionError>,
270
+ preAddContext?: PreAddContext,
241
271
  ): Promise<{ status: 'accepted' | 'ignored' | 'rejected' }> {
242
272
  const txHash = tx.getTxHash();
243
273
  const txHashStr = txHash.toString();
@@ -249,24 +279,40 @@ export class TxPoolV2Impl {
249
279
  }
250
280
 
251
281
  // Run pre-add rules
252
- const preAddResult = await this.#evictionManager.runPreAddRules(meta, poolAccess);
282
+ const preAddResult = await this.#evictionManager.runPreAddRules(meta, poolAccess, preAddContext);
253
283
 
254
284
  if (preAddResult.shouldIgnore) {
255
- this.#log.debug(`Ignoring tx ${txHashStr}: ${preAddResult.reason}`);
285
+ this.#log.debug(`Ignoring tx ${txHashStr}: ${preAddResult.reason?.message ?? 'unknown reason'}`);
286
+ if (preAddResult.reason && preAddResult.reason.code !== TxPoolRejectionCode.INTERNAL_ERROR) {
287
+ errors.set(txHashStr, preAddResult.reason);
288
+ }
256
289
  return { status: 'ignored' };
257
290
  }
258
291
 
259
- // Evict conflicts
260
- for (const evictHashStr of preAddResult.txHashesToEvict) {
261
- await this.#deleteTx(evictHashStr);
262
- this.#log.debug(`Evicted tx ${evictHashStr} due to higher-fee tx ${txHashStr}`, {
263
- evictedTxHash: evictHashStr,
264
- replacementTxHash: txHashStr,
265
- });
266
- if (acceptedPending.has(evictHashStr)) {
267
- // Evicted tx was from this batch - mark as ignored in result
268
- acceptedPending.delete(evictHashStr);
269
- ignored.push(TxHash.fromString(evictHashStr));
292
+ // Evict conflicts, grouped by rule name for metrics
293
+ if (preAddResult.evictions && preAddResult.evictions.length > 0) {
294
+ const byReason = new Map<string, string[]>();
295
+ for (const { txHash: evictHash, reason } of preAddResult.evictions) {
296
+ const group = byReason.get(reason);
297
+ if (group) {
298
+ group.push(evictHash);
299
+ } else {
300
+ byReason.set(reason, [evictHash]);
301
+ }
302
+ }
303
+ for (const [reason, hashes] of byReason) {
304
+ await this.#evictTxs(hashes, reason);
305
+ }
306
+ for (const evictHashStr of preAddResult.txHashesToEvict) {
307
+ this.#log.debug(`Evicted tx ${evictHashStr} due to higher-fee tx ${txHashStr}`, {
308
+ evictedTxHash: evictHashStr,
309
+ replacementTxHash: txHashStr,
310
+ });
311
+ if (acceptedPending.has(evictHashStr)) {
312
+ // Evicted tx was from this batch - mark as ignored in result
313
+ acceptedPending.delete(evictHashStr);
314
+ ignored.push(TxHash.fromString(evictHashStr));
315
+ }
270
316
  }
271
317
  }
272
318
 
@@ -327,9 +373,11 @@ export class TxPoolV2Impl {
327
373
  });
328
374
  }
329
375
 
330
- protectTxs(txHashes: TxHash[], block: BlockHeader): TxHash[] {
376
+ async protectTxs(txHashes: TxHash[], block: BlockHeader): Promise<TxHash[]> {
331
377
  const slotNumber = block.globalVariables.slotNumber;
332
378
  const missing: TxHash[] = [];
379
+ let softDeletedHits = 0;
380
+ let missingPreviouslyEvicted = 0;
333
381
 
334
382
  for (const txHash of txHashes) {
335
383
  const txHashStr = txHash.toString();
@@ -337,13 +385,44 @@ export class TxPoolV2Impl {
337
385
  if (this.#indices.has(txHashStr)) {
338
386
  // Update protection for existing tx
339
387
  this.#indices.updateProtection(txHashStr, slotNumber);
388
+ } else if (this.#deletedPool.isSoftDeleted(txHashStr)) {
389
+ // Resurrect soft-deleted tx as protected
390
+ const buffer = await this.#txsDB.getAsync(txHashStr);
391
+ if (buffer) {
392
+ const tx = Tx.fromBuffer(buffer);
393
+ await this.#addTx(tx, { protected: slotNumber });
394
+ softDeletedHits++;
395
+ } else {
396
+ // Data missing despite soft-delete flag — treat as truly missing
397
+ this.#indices.setProtection(txHashStr, slotNumber);
398
+ missing.push(txHash);
399
+ }
340
400
  } else {
341
- // Pre-record protection for tx we don't have yet
401
+ // Truly missing — pre-record protection for tx we don't have yet
342
402
  this.#indices.setProtection(txHashStr, slotNumber);
343
403
  missing.push(txHash);
404
+ if (this.#evictedTxHashes.has(txHashStr)) {
405
+ missingPreviouslyEvicted++;
406
+ }
344
407
  }
345
408
  }
346
409
 
410
+ // Record metrics
411
+ if (softDeletedHits > 0) {
412
+ this.#instrumentation.recordSoftDeletedHits(softDeletedHits);
413
+ }
414
+ if (missing.length > 0) {
415
+ this.#log.debug(`protectTxs missing tx hashes: ${missing.map(h => h.toString()).join(', ')}`);
416
+ this.#instrumentation.recordMissingOnProtect(missing.length);
417
+ }
418
+ if (missingPreviouslyEvicted > 0) {
419
+ this.#instrumentation.recordMissingPreviouslyEvicted(missingPreviouslyEvicted);
420
+ }
421
+
422
+ this.#log.info(
423
+ `Protected ${txHashes.length} txs, missing: ${missing.length}, soft-deleted hits: ${softDeletedHits}`,
424
+ );
425
+
347
426
  return missing;
348
427
  }
349
428
 
@@ -412,6 +491,7 @@ export class TxPoolV2Impl {
412
491
  // Step 3: Filter to only txs that have metadata and are not mined
413
492
  const txsToRestore = this.#indices.filterRestorable(expiredProtected);
414
493
  if (txsToRestore.length === 0) {
494
+ this.#log.debug(`Preparing for slot ${slotNumber}, no txs to unprotect`);
415
495
  return;
416
496
  }
417
497
 
@@ -423,8 +503,9 @@ export class TxPoolV2Impl {
423
503
  // Step 5: Resolve nullifier conflicts and add winners to pending indices
424
504
  const { added, toEvict } = this.#applyNullifierConflictResolution(valid);
425
505
 
426
- // Step 6: Delete invalid and evicted txs
427
- await this.#deleteTxsBatch([...invalid, ...toEvict]);
506
+ // Step 6: Delete invalid txs and evict conflict losers
507
+ await this.#deleteTxsBatch(invalid);
508
+ await this.#evictTxs(toEvict, 'NullifierConflict');
428
509
 
429
510
  // Step 7: Run eviction rules (enforce pool size limit)
430
511
  if (added.length > 0) {
@@ -437,7 +518,7 @@ export class TxPoolV2Impl {
437
518
  }
438
519
  }
439
520
 
440
- async handlePrunedBlocks(latestBlock: L2BlockId): Promise<void> {
521
+ async handlePrunedBlocks(latestBlock: L2BlockId, options?: { deleteAllTxs?: boolean }): Promise<void> {
441
522
  // Step 1: Find transactions mined after the prune point
442
523
  const txsToUnmine = this.#indices.findTxsMinedAfter(latestBlock.number);
443
524
  if (txsToUnmine.length === 0) {
@@ -462,17 +543,28 @@ export class TxPoolV2Impl {
462
543
  this.#indices.markAsUnmined(meta);
463
544
  }
464
545
 
546
+ // If deleteAllTxs is set (epoch prune), delete all un-mined txs and return early
547
+ if (options?.deleteAllTxs) {
548
+ const allTxHashes = txsToUnmine.map(m => m.txHash);
549
+ await this.#deleteTxsBatch(allTxHashes);
550
+ this.#log.info(
551
+ `Handled prune to block ${latestBlock.number} with deleteAllTxs: deleted ${allTxHashes.length} txs`,
552
+ );
553
+ return;
554
+ }
555
+
465
556
  // Step 4: Filter out protected txs (they'll be handled by prepareForSlot)
466
557
  const unprotectedTxs = this.#indices.filterUnprotected(txsToUnmine);
467
558
 
468
- // Step 4: Validate for pending pool
559
+ // Step 5: Validate for pending pool
469
560
  const { valid, invalid } = await this.#revalidateMetadata(unprotectedTxs, 'during handlePrunedBlocks');
470
561
 
471
562
  // Step 6: Resolve nullifier conflicts and add winners to pending indices
472
563
  const { toEvict } = this.#applyNullifierConflictResolution(valid);
473
564
 
474
- // Step 7: Delete invalid and evicted txs
475
- await this.#deleteTxsBatch([...invalid, ...toEvict]);
565
+ // Step 7: Delete invalid txs and evict conflict losers
566
+ await this.#deleteTxsBatch(invalid);
567
+ await this.#evictTxs(toEvict, 'NullifierConflict');
476
568
 
477
569
  this.#log.info(
478
570
  `Handled prune to block ${latestBlock.number}: ${valid.length} txs restored to pending, ${invalid.length} invalid, ${toEvict.length} evicted due to nullifier conflicts`,
@@ -637,8 +729,17 @@ export class TxPoolV2Impl {
637
729
 
638
730
  // === Metrics ===
639
731
 
640
- countTxs(): { pending: number; protected: number; mined: number } {
641
- return this.#indices.countTxs();
732
+ countTxs(): {
733
+ pending: number;
734
+ protected: number;
735
+ mined: number;
736
+ softDeleted: number;
737
+ totalMetadataBytes: number;
738
+ } {
739
+ return {
740
+ ...this.#indices.countTxs(),
741
+ softDeleted: this.#deletedPool.getSoftDeletedCount(),
742
+ };
642
743
  }
643
744
 
644
745
  // ============================================================================
@@ -672,9 +773,11 @@ export class TxPoolV2Impl {
672
773
  }
673
774
 
674
775
  const stateStr = typeof state === 'string' ? state : Object.keys(state)[0];
675
- this.#log.verbose(`Added ${stateStr} tx ${txHashStr}`, {
776
+ this.#log.debug(`Added tx ${txHashStr} as ${stateStr}`, {
676
777
  eventName: 'tx-added-to-pool',
778
+ txHash: txHashStr,
677
779
  state: stateStr,
780
+ source: opts.source,
678
781
  });
679
782
 
680
783
  return meta;
@@ -702,6 +805,29 @@ export class TxPoolV2Impl {
702
805
  }
703
806
  }
704
807
 
808
+ /** Evicts transactions: records eviction metric with reason, caches hashes, then deletes. */
809
+ async #evictTxs(txHashes: string[], reason: string): Promise<void> {
810
+ if (txHashes.length === 0) {
811
+ return;
812
+ }
813
+ this.#instrumentation.recordEvictions(txHashes.length, reason);
814
+ for (const txHashStr of txHashes) {
815
+ this.#log.debug(`Evicting tx ${txHashStr}`, { txHash: txHashStr, reason });
816
+ this.#addToEvictedCache(txHashStr);
817
+ }
818
+ await this.#deleteTxsBatch(txHashes);
819
+ }
820
+
821
+ /** Adds a tx hash to the bounded evicted cache, evicting the oldest entry if at capacity. */
822
+ #addToEvictedCache(txHashStr: string): void {
823
+ if (this.#evictedTxHashes.size >= this.#config.evictedTxCacheSize) {
824
+ // FIFO eviction: remove the first (oldest) entry
825
+ const oldest = this.#evictedTxHashes.values().next().value!;
826
+ this.#evictedTxHashes.delete(oldest);
827
+ }
828
+ this.#evictedTxHashes.add(txHashStr);
829
+ }
830
+
705
831
  // ============================================================================
706
832
  // PRIVATE HELPERS - Validation & Conflict Resolution
707
833
  // ============================================================================
@@ -857,7 +983,9 @@ export class TxPoolV2Impl {
857
983
  if (preAddResult.shouldIgnore) {
858
984
  // Transaction rejected - mark for deletion from DB
859
985
  rejected.push(meta.txHash);
860
- this.#log.debug(`Rejected tx ${meta.txHash} during rebuild: ${preAddResult.reason}`);
986
+ this.#log.debug(
987
+ `Rejected tx ${meta.txHash} during rebuild: ${preAddResult.reason?.message ?? 'unknown reason'}`,
988
+ );
861
989
  continue;
862
990
  }
863
991
 
@@ -893,7 +1021,7 @@ export class TxPoolV2Impl {
893
1021
  getFeePayerPendingTxs: (feePayer: string) => this.#indices.getFeePayerPendingTxs(feePayer),
894
1022
  getPendingTxCount: () => this.#indices.getPendingTxCount(),
895
1023
  getLowestPriorityPending: (limit: number) => this.#indices.getLowestPriorityPending(limit),
896
- deleteTxs: (txHashes: string[]) => this.#deleteTxsBatch(txHashes),
1024
+ deleteTxs: (txHashes: string[], reason?: string) => this.#evictTxs(txHashes, reason ?? 'unknown'),
897
1025
  };
898
1026
  }
899
1027
 
@@ -1,13 +1,13 @@
1
1
  import type { BlockNumber } from '@aztec/foundation/branded-types';
2
2
  import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
3
- import { TX_ERROR_INVALID_INCLUDE_BY_TIMESTAMP, type TxValidationResult, type TxValidator } from '@aztec/stdlib/tx';
3
+ import { TX_ERROR_INVALID_EXPIRATION_TIMESTAMP, type TxValidationResult, type TxValidator } from '@aztec/stdlib/tx';
4
4
  import type { UInt64 } from '@aztec/stdlib/types';
5
5
 
6
6
  /** Structural interface for timestamp validation. */
7
7
  export interface HasTimestampData {
8
8
  txHash: { toString(): string };
9
9
  data: {
10
- includeByTimestamp: bigint;
10
+ expirationTimestamp: bigint;
11
11
  constants: {
12
12
  anchorBlockHeader: {
13
13
  globalVariables: {
@@ -35,21 +35,21 @@ export class TimestampTxValidator<T extends HasTimestampData> implements TxValid
35
35
  }
36
36
 
37
37
  validateTx(tx: T): Promise<TxValidationResult> {
38
- const includeByTimestamp = tx.data.includeByTimestamp;
39
- // If building block 1, we skip the expiration check. For details on why see the `validate_include_by_timestamp`
38
+ const expirationTimestamp = tx.data.expirationTimestamp;
39
+ // If building block 1, we skip the expiration check. For details on why see the `validate_expiration_timestamp`
40
40
  // function in `noir-projects/noir-protocol-circuits/crates/rollup-lib/src/base/components/validation_requests.nr`.
41
41
  const buildingBlock1 = this.values.blockNumber === 1;
42
42
 
43
- if (!buildingBlock1 && includeByTimestamp < this.values.timestamp) {
43
+ if (!buildingBlock1 && expirationTimestamp < this.values.timestamp) {
44
44
  if (tx.data.constants.anchorBlockHeader.globalVariables.blockNumber === 0) {
45
45
  this.#log.warn(
46
46
  `A tx built against a genesis block failed to be included in block 1 which is the only block in which txs built against a genesis block are allowed to be included.`,
47
47
  );
48
48
  }
49
49
  this.#log.verbose(
50
- `Rejecting tx ${tx.txHash} for low expiration timestamp. Tx expiration timestamp: ${includeByTimestamp}, timestamp: ${this.values.timestamp}.`,
50
+ `Rejecting tx ${tx.txHash} for low expiration timestamp. Tx expiration timestamp: ${expirationTimestamp}, timestamp: ${this.values.timestamp}.`,
51
51
  );
52
- return Promise.resolve({ result: 'invalid', reason: [TX_ERROR_INVALID_INCLUDE_BY_TIMESTAMP] });
52
+ return Promise.resolve({ result: 'invalid', reason: [TX_ERROR_INVALID_EXPIRATION_TIMESTAMP] });
53
53
  } else {
54
54
  return Promise.resolve({ result: 'valid' });
55
55
  }
@@ -1,6 +1,6 @@
1
1
  import type { EthAddress } from '@aztec/foundation/eth-address';
2
2
  import type { PeerInfo } from '@aztec/stdlib/interfaces/server';
3
- import type { Gossipable, PeerErrorSeverity } from '@aztec/stdlib/p2p';
3
+ import type { Gossipable, PeerErrorSeverity, TopicType } from '@aztec/stdlib/p2p';
4
4
  import { Tx, TxHash } from '@aztec/stdlib/tx';
5
5
 
6
6
  import type { PeerId } from '@libp2p/interface';
@@ -44,6 +44,10 @@ export class DummyP2PService implements P2PService {
44
44
  return [];
45
45
  }
46
46
 
47
+ getGossipMeshPeerCount(_topicType: TopicType): number {
48
+ return 0;
49
+ }
50
+
47
51
  /**
48
52
  * Starts the dummy implementation.
49
53
  * @returns A resolved promise.
@@ -58,7 +58,8 @@ const DefaultMaxSizesKb: Record<TopicType, number> = {
58
58
  // Proposals may carry some tx objects, so we allow a larger size capped at 10mb
59
59
  // Note this may not be enough for carrying all tx objects in a block
60
60
  [TopicType.block_proposal]: 1024 * 10,
61
- // TODO(palla/mbps): Check size for checkpoint proposal
61
+ // Checkpoint proposals carry almost the same data as a block proposal (see the lastBlockProposal)
62
+ // Only diff is an additional header, which is pretty small compared to the 10mb limit
62
63
  [TopicType.checkpoint_proposal]: 1024 * 10,
63
64
  };
64
65
 
@@ -40,7 +40,7 @@ We configure all parameters (P1-P4) with values calculated dynamically from netw
40
40
  | P3b: meshFailurePenalty | -34 per topic | Sticky penalty after pruning |
41
41
  | P4: invalidMessageDeliveries | -20 per message | Attack detection |
42
42
 
43
- **Important:** P1 and P2 are only enabled on topics with P3 enabled (block_proposal, checkpoint_proposal, checkpoint_attestation). The tx topic has all scoring disabled except P4, to prevent free positive score accumulation that would offset penalties from other topics.
43
+ **Important:** P1 and P2 are only enabled on topics with P3 enabled. By default, P3 is enabled for checkpoint_proposal and checkpoint_attestation (2 topics). Block proposal scoring is controlled by `expectedBlockProposalsPerSlot` (current default: `0`, including when env var is unset, so disabled) - see [Block Proposals](#block-proposals-block_proposal) for details. The tx topic has all scoring disabled except P4, to prevent free positive score accumulation that would offset penalties from other topics.
44
44
 
45
45
  ## Exponential Decay
46
46
 
@@ -217,7 +217,21 @@ Transactions are submitted unpredictably by users, so we cannot set meaningful d
217
217
 
218
218
  ### Block Proposals (block_proposal)
219
219
 
220
- In Multi-Block-Per-Slot (MBPS) mode, N-1 block proposals are gossiped per slot (the last block is bundled with the checkpoint). In single-block mode, this is 0.
220
+ Block proposal scoring is controlled by the `expectedBlockProposalsPerSlot` config (`SEQ_EXPECTED_BLOCK_PROPOSALS_PER_SLOT` env var):
221
+
222
+ | Config Value | Behavior |
223
+ |-------------|----------|
224
+ | `0` (current default) | Block proposal P3 scoring is **disabled** |
225
+ | Positive number | Uses the provided value as expected proposals per slot |
226
+ | `undefined` | Falls back to `blocksPerSlot - 1` (MBPS mode: N-1, single block: 0) |
227
+
228
+ **Current behavior note:** In the current implementation, if `SEQ_EXPECTED_BLOCK_PROPOSALS_PER_SLOT` is not set, config mapping applies `0` by default (scoring disabled). The `undefined` fallback above is currently reachable only if the value is explicitly provided as `undefined` in code.
229
+
230
+ **Future intent:** Once throughput is stable, we may change env parsing/defaults so an unset env var resolves to `undefined` again (re-enabling automatic fallback to `blocksPerSlot - 1`).
231
+
232
+ **Why disabled by default?** In MBPS mode, gossipsub expects N-1 block proposals per slot. When transaction throughput is low (as expected at launch), fewer blocks are actually built, causing peers to be incorrectly penalized for under-delivering block proposals. The default of 0 disables this scoring. Set to a positive value when throughput increases and block production is consistent.
233
+
234
+ In MBPS mode (when enabled), N-1 block proposals are gossiped per slot (the last block is bundled with the checkpoint). In single-block mode, this is 0.
221
235
 
222
236
  ### Checkpoint Proposals (checkpoint_proposal)
223
237
 
@@ -241,6 +255,7 @@ The scoring parameters depend on:
241
255
  | `targetCommitteeSize` | L1RollupConstants | 48 |
242
256
  | `heartbeatInterval` | P2PConfig.gossipsubInterval | 700ms |
243
257
  | `blockDurationMs` | P2PConfig.blockDurationMs | undefined (single block) |
258
+ | `expectedBlockProposalsPerSlot` | P2PConfig.expectedBlockProposalsPerSlot | 0 (disabled; current unset-env behavior) |
244
259
 
245
260
  ## Invalid Message Handling (P4)
246
261
 
@@ -320,9 +335,9 @@ Conversely, if topic scores are low, a peer slightly above the disconnect thresh
320
335
 
321
336
  Topic scores provide **burst response** to attacks, while app score provides **stable baseline**:
322
337
 
323
- - P1 (time in mesh): Max +8 per topic (+24 across 3 topics)
324
- - P2 (first deliveries): Max +25 per topic (+75 across 3 topics, but decays fast)
325
- - P3 (under-delivery): Max -34 per topic (-102 across 3 topics in MBPS; -68 in single-block mode)
338
+ - P1 (time in mesh): Max +8 per topic (+16 default, +24 with block proposal scoring enabled)
339
+ - P2 (first deliveries): Max +25 per topic (+50 default, +75 with block proposal scoring, but decays fast)
340
+ - P3 (under-delivery): Max -34 per topic (-68 default with 2 topics, -102 with block proposal scoring enabled)
326
341
  - P4 (invalid messages): -20 per invalid message, can spike to -2000+ during attacks
327
342
 
328
343
  Example attack scenario:
@@ -373,21 +388,21 @@ When a peer is pruned from the mesh:
373
388
  3. **P3b captures the penalty**: The P3 deficit at prune time becomes P3b, which decays slowly
374
389
 
375
390
  After pruning, the peer's score consists mainly of P3b:
376
- - **Total P3b across 3 topics: -102** (max)
391
+ - **Total P3b: -68** (default, 2 topics) or **-102** (with block proposal scoring enabled, 3 topics)
377
392
  - **Recovery time**: P3b decays to ~1% over one decay window (2-5 slots = 2-6 minutes)
378
393
  - **Grafting eligibility**: Peer can be grafted when score ≥ 0, but asymptotic decay means recovery is slow
379
394
 
380
395
  ### Why Non-Contributors Aren't Disconnected
381
396
 
382
- With P3b capped at -102 total after pruning (MBPS mode). In single-block mode, the cap is -68:
397
+ With P3b capped at -68 (default, 2 topics) or -102 (with block proposal scoring, 3 topics) after pruning:
383
398
 
384
399
  | Threshold | Value | P3b Score | Triggered? |
385
400
  |-----------|-------|-----------|------------|
386
- | gossipThreshold | -500 | -102 (MBPS) / -68 (single) | No |
387
- | publishThreshold | -1000 | -102 (MBPS) / -68 (single) | No |
388
- | graylistThreshold | -2000 | -102 (MBPS) / -68 (single) | No |
401
+ | gossipThreshold | -500 | -68 (default) / -102 (block scoring on) | No |
402
+ | publishThreshold | -1000 | -68 (default) / -102 (block scoring on) | No |
403
+ | graylistThreshold | -2000 | -68 (default) / -102 (block scoring on) | No |
389
404
 
390
- **A score of -102 (MBPS) or -68 (single-block) is well above -500**, so non-contributing peers:
405
+ **A score of -68 or -102 is well above -500**, so non-contributing peers:
391
406
  - Are pruned from mesh (good - stops them slowing propagation)
392
407
  - Still receive gossip (can recover by reconnecting/restarting)
393
408
  - Are NOT disconnected unless they also have application-level penalties
@@ -547,7 +562,7 @@ What happens when a peer experiences a network outage and stops delivering messa
547
562
  While the peer is disconnected:
548
563
 
549
564
  1. **P3 penalty accumulates**: The message delivery counter decays toward 0, causing increasing P3 penalty
550
- 2. **Max P3 penalty reached**: Once counter drops below threshold, penalty hits -34 per topic (-102 total in MBPS; -68 single-block)
565
+ 2. **Max P3 penalty reached**: Once counter drops below threshold, penalty hits -34 per topic (-68 default, -102 with block proposal scoring)
551
566
  3. **Mesh pruning**: Topic score goes negative → peer is pruned from mesh
552
567
  4. **P3b captures penalty**: The P3 deficit at prune time becomes P3b (sticky penalty)
553
568
 
@@ -569,13 +584,13 @@ Note: If the peer just joined the mesh, P3 penalties only start after
569
584
  During a network outage, the peer:
570
585
  - **Does NOT send invalid messages** → No P4 penalty
571
586
  - **Does NOT violate protocols** → No application-level penalty
572
- - **Only accumulates topic-level penalties** → Max -102 (P3b, MBPS) or -68 (single-block)
587
+ - **Only accumulates topic-level penalties** → Max -68 (default) or -102 (with block proposal scoring)
573
588
 
574
589
  This is the crucial difference from malicious behavior:
575
590
 
576
591
  | Scenario | App Score | Topic Score | Total | Threshold Hit |
577
592
  |----------|-----------|-------------|-------|---------------|
578
- | Network outage | 0 | -102 (MBPS) / -68 (single) | -102 / -68 | None |
593
+ | Network outage | 0 | -68 (default) / -102 (block scoring on) | -68 / -102 | None |
579
594
  | Validation failure | -50 | -20 | -520 | gossipThreshold |
580
595
  | Malicious peer | -100 | -2000+ | -2100+ | graylistThreshold |
581
596
 
@@ -15,6 +15,8 @@ export type TopicScoringNetworkParams = {
15
15
  targetCommitteeSize: number;
16
16
  /** Duration per block in milliseconds when building multiple blocks per slot. If undefined, single block mode. */
17
17
  blockDurationMs?: number;
18
+ /** Expected number of block proposals per slot for scoring override. 0 disables scoring, undefined falls back to blocksPerSlot - 1. */
19
+ expectedBlockProposalsPerSlot?: number;
18
20
  };
19
21
 
20
22
  /**
@@ -89,18 +91,41 @@ export function computeThreshold(convergence: number, conservativeFactor: number
89
91
  return convergence * conservativeFactor;
90
92
  }
91
93
 
94
+ /**
95
+ * Determines the effective expected block proposals per slot for scoring.
96
+ * Returns undefined if scoring should be disabled, or a positive number if enabled.
97
+ *
98
+ * @param blocksPerSlot - Number of blocks per slot from timetable
99
+ * @param expectedBlockProposalsPerSlot - Config override. 0 disables scoring, undefined falls back to blocksPerSlot - 1.
100
+ * @returns Positive number of expected block proposals, or undefined if scoring is disabled
101
+ */
102
+ export function getEffectiveBlockProposalsPerSlot(
103
+ blocksPerSlot: number,
104
+ expectedBlockProposalsPerSlot?: number,
105
+ ): number | undefined {
106
+ if (expectedBlockProposalsPerSlot !== undefined) {
107
+ return expectedBlockProposalsPerSlot > 0 ? expectedBlockProposalsPerSlot : undefined;
108
+ }
109
+ // Fallback: In MBPS mode, N-1 block proposals per slot (last one bundled with checkpoint)
110
+ // In single block mode (blocksPerSlot=1), this is 0 → disabled
111
+ const fallback = Math.max(0, blocksPerSlot - 1);
112
+ return fallback > 0 ? fallback : undefined;
113
+ }
114
+
92
115
  /**
93
116
  * Gets the expected messages per slot for a given topic type.
94
117
  *
95
118
  * @param topicType - The topic type
96
119
  * @param targetCommitteeSize - Target committee size
97
120
  * @param blocksPerSlot - Number of blocks per slot
121
+ * @param expectedBlockProposalsPerSlot - Override for block proposals. 0 disables scoring, undefined falls back to blocksPerSlot - 1.
98
122
  * @returns Expected messages per slot, or undefined if unpredictable
99
123
  */
100
124
  export function getExpectedMessagesPerSlot(
101
125
  topicType: TopicType,
102
126
  targetCommitteeSize: number,
103
127
  blocksPerSlot: number,
128
+ expectedBlockProposalsPerSlot?: number,
104
129
  ): number | undefined {
105
130
  switch (topicType) {
106
131
  case TopicType.tx:
@@ -108,9 +133,7 @@ export function getExpectedMessagesPerSlot(
108
133
  return undefined;
109
134
 
110
135
  case TopicType.block_proposal:
111
- // In MBPS mode, N-1 block proposals per slot (last one bundled with checkpoint)
112
- // In single block mode (blocksPerSlot=1), this is 0
113
- return Math.max(0, blocksPerSlot - 1);
136
+ return getEffectiveBlockProposalsPerSlot(blocksPerSlot, expectedBlockProposalsPerSlot);
114
137
 
115
138
  case TopicType.checkpoint_proposal:
116
139
  // Exactly 1 checkpoint proposal per slot
@@ -190,10 +213,12 @@ const P2_DECAY_WINDOW_SLOTS = 2;
190
213
  // |P3| > 8 + 25 = 33
191
214
  //
192
215
  // We set P3 max = -34 per topic (slightly more than P1+P2) to ensure pruning.
193
- // With 3 topics having P3 enabled, total P3b after pruning = -102.
216
+ // The number of P3-enabled topics depends on config: by default 2 (checkpoint_proposal +
217
+ // checkpoint_attestation), or 3 if block proposal scoring is enabled via
218
+ // expectedBlockProposalsPerSlot. Total P3b after pruning = -68 (2 topics) or -102 (3 topics).
194
219
  //
195
- // With appSpecificWeight=10, ~20 HighTolerance errors (-40 app score) plus max P3b (-102)
196
- // would cross gossipThreshold (-500). This keeps non-contributors from being disconnected
220
+ // With appSpecificWeight=10, ~20 HighTolerance errors (-40 app score) plus max P3b (-68 or -102)
221
+ // would not cross gossipThreshold (-500). This keeps non-contributors from being disconnected
197
222
  // unless they also accrue app-level penalties.
198
223
  //
199
224
  // The weight formula ensures max penalty equals MAX_P3_PENALTY_PER_TOPIC:
@@ -203,12 +228,6 @@ const P2_DECAY_WINDOW_SLOTS = 2;
203
228
  /** Maximum P3 penalty per topic (must exceed P1 + P2 to cause pruning) */
204
229
  export const MAX_P3_PENALTY_PER_TOPIC = -(MAX_P1_SCORE + MAX_P2_SCORE + 1); // -34
205
230
 
206
- /** Number of topics with P3 enabled in MBPS mode (block_proposal + checkpoint_proposal + checkpoint_attestation) */
207
- export const NUM_P3_ENABLED_TOPICS = 3;
208
-
209
- /** Total maximum P3b penalty across all topics after pruning in MBPS mode */
210
- export const TOTAL_MAX_P3B_PENALTY = MAX_P3_PENALTY_PER_TOPIC * NUM_P3_ENABLED_TOPICS; // -102
211
-
212
231
  /**
213
232
  * Factory class for creating gossipsub topic scoring parameters.
214
233
  * Computes shared values once and reuses them across all topics.
@@ -384,6 +403,18 @@ export class TopicScoreParamsFactory {
384
403
  });
385
404
  }
386
405
 
406
+ /** Number of topics with P3 enabled, computed from config. Always 2 (checkpoint_proposal + checkpoint_attestation) plus optionally block_proposal. */
407
+ get numP3EnabledTopics(): number {
408
+ const blockProposalP3Enabled =
409
+ getEffectiveBlockProposalsPerSlot(this.blocksPerSlot, this.params.expectedBlockProposalsPerSlot) !== undefined;
410
+ return blockProposalP3Enabled ? 3 : 2;
411
+ }
412
+
413
+ /** Total maximum P3b penalty across all topics after pruning, computed from config */
414
+ get totalMaxP3bPenalty(): number {
415
+ return MAX_P3_PENALTY_PER_TOPIC * this.numP3EnabledTopics;
416
+ }
417
+
387
418
  /**
388
419
  * Creates topic score parameters for a specific topic type.
389
420
  *
@@ -391,7 +422,12 @@ export class TopicScoreParamsFactory {
391
422
  * @returns TopicScoreParams for the topic
392
423
  */
393
424
  createForTopic(topicType: TopicType): ReturnType<typeof createTopicScoreParams> {
394
- const expectedPerSlot = getExpectedMessagesPerSlot(topicType, this.params.targetCommitteeSize, this.blocksPerSlot);
425
+ const expectedPerSlot = getExpectedMessagesPerSlot(
426
+ topicType,
427
+ this.params.targetCommitteeSize,
428
+ this.blocksPerSlot,
429
+ this.params.expectedBlockProposalsPerSlot,
430
+ );
395
431
 
396
432
  // For unpredictable topics (tx) or topics with 0 expected messages, disable P3/P3b
397
433
  if (expectedPerSlot === undefined || expectedPerSlot === 0) {