trac-msb 0.2.11 → 0.2.12

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 (114) hide show
  1. package/.github/workflows/acceptance-tests.yml +38 -0
  2. package/.github/workflows/lint-pr-title.yml +26 -0
  3. package/.github/workflows/publish.yml +33 -0
  4. package/.github/workflows/unit-tests.yml +34 -0
  5. package/package.json +5 -10
  6. package/proto/network.proto +74 -0
  7. package/rpc/rpc_services.js +4 -22
  8. package/scripts/generate-protobufs.js +12 -37
  9. package/src/config/config.js +5 -26
  10. package/src/config/env.js +11 -25
  11. package/src/core/network/Network.js +36 -73
  12. package/src/core/network/protocols/LegacyProtocol.js +11 -21
  13. package/src/core/network/protocols/NetworkMessages.js +17 -38
  14. package/src/core/network/protocols/ProtocolInterface.js +2 -14
  15. package/src/core/network/protocols/ProtocolSession.js +17 -144
  16. package/src/core/network/protocols/V1Protocol.js +18 -37
  17. package/src/core/network/protocols/legacy/NetworkMessageRouter.js +19 -25
  18. package/src/core/network/protocols/legacy/handlers/{LegacyGetRequestHandler.js → GetRequestHandler.js} +6 -6
  19. package/src/core/network/protocols/legacy/handlers/ResponseHandler.js +37 -0
  20. package/src/core/network/protocols/{legacy/handlers/LegacyRoleOperationHandler.js → shared/handlers/RoleOperationHandler.js} +11 -18
  21. package/src/core/network/protocols/{legacy/handlers/LegacySubnetworkOperationHandler.js → shared/handlers/SubnetworkOperationHandler.js} +17 -28
  22. package/src/core/network/protocols/{legacy/handlers/LegacyTransferOperationHandler.js → shared/handlers/TransferOperationHandler.js} +11 -17
  23. package/src/core/network/protocols/{legacy/handlers/BaseStateOperationHandler.js → shared/handlers/base/BaseOperationHandler.js} +12 -23
  24. package/src/core/network/protocols/shared/validators/{PartialBootstrapDeploymentValidator.js → PartialBootstrapDeployment.js} +4 -9
  25. package/src/core/network/protocols/shared/validators/{PartialRoleAccessValidator.js → PartialRoleAccess.js} +17 -51
  26. package/src/core/network/protocols/shared/validators/{PartialTransactionValidator.js → PartialTransaction.js} +7 -21
  27. package/src/core/network/protocols/shared/validators/{PartialTransferValidator.js → PartialTransfer.js} +9 -26
  28. package/src/core/network/protocols/shared/validators/{PartialOperationValidator.js → base/PartialOperation.js} +25 -47
  29. package/src/core/network/protocols/v1/NetworkMessageRouter.js +7 -91
  30. package/src/core/network/services/ConnectionManager.js +94 -146
  31. package/src/core/network/services/MessageOrchestrator.js +27 -151
  32. package/src/core/network/services/TransactionPoolService.js +18 -129
  33. package/src/core/network/services/TransactionRateLimiterService.js +34 -52
  34. package/src/core/network/services/ValidatorObserverService.js +26 -18
  35. package/src/core/state/State.js +19 -70
  36. package/src/index.js +4 -5
  37. package/src/messages/network/v1/NetworkMessageBuilder.js +79 -59
  38. package/src/messages/network/v1/NetworkMessageDirector.js +50 -16
  39. package/src/utils/Scheduler.js +8 -0
  40. package/src/utils/constants.js +5 -71
  41. package/src/utils/helpers.js +1 -10
  42. package/src/utils/normalizers.js +0 -38
  43. package/src/utils/protobuf/network.cjs +840 -0
  44. package/src/utils/protobuf/operationHelpers.js +3 -24
  45. package/tests/acceptance/v1/account/account.test.mjs +2 -8
  46. package/tests/acceptance/v1/tx/tx.test.mjs +1 -23
  47. package/tests/acceptance/v1/tx-details/tx-details.test.mjs +6 -34
  48. package/tests/fixtures/networkV1.fixtures.js +28 -2
  49. package/tests/helpers/transactionPayloads.mjs +2 -2
  50. package/tests/unit/messages/network/NetworkMessageBuilder.test.js +79 -239
  51. package/tests/unit/messages/network/NetworkMessageDirector.test.js +77 -223
  52. package/tests/unit/network/ConnectionManager.test.js +191 -0
  53. package/tests/unit/network/networkModule.test.js +1 -4
  54. package/tests/unit/unit.test.js +2 -2
  55. package/tests/unit/utils/protobuf/operationHelpers.test.js +4 -2
  56. package/tests/unit/utils/utils.test.js +0 -1
  57. package/proto/network/v1/enums/message_type.proto +0 -16
  58. package/proto/network/v1/enums/result_code.proto +0 -84
  59. package/proto/network/v1/messages/broadcast_transaction_request.proto +0 -9
  60. package/proto/network/v1/messages/broadcast_transaction_response.proto +0 -13
  61. package/proto/network/v1/messages/liveness_request.proto +0 -8
  62. package/proto/network/v1/messages/liveness_response.proto +0 -11
  63. package/proto/network/v1/network_message.proto +0 -22
  64. package/src/core/network/protocols/connectionPolicies.js +0 -88
  65. package/src/core/network/protocols/legacy/handlers/LegacyResponseHandler.js +0 -23
  66. package/src/core/network/protocols/shared/errors/SharedValidatorRejectionError.js +0 -27
  67. package/src/core/network/protocols/v1/V1ProtocolError.js +0 -91
  68. package/src/core/network/protocols/v1/handlers/V1BaseOperationHandler.js +0 -65
  69. package/src/core/network/protocols/v1/handlers/V1BroadcastTransactionOperationHandler.js +0 -389
  70. package/src/core/network/protocols/v1/handlers/V1LivenessOperationHandler.js +0 -87
  71. package/src/core/network/protocols/v1/validators/V1BaseOperation.js +0 -211
  72. package/src/core/network/protocols/v1/validators/V1BroadcastTransactionRequest.js +0 -26
  73. package/src/core/network/protocols/v1/validators/V1BroadcastTransactionResponse.js +0 -276
  74. package/src/core/network/protocols/v1/validators/V1LivenessRequest.js +0 -15
  75. package/src/core/network/protocols/v1/validators/V1LivenessResponse.js +0 -17
  76. package/src/core/network/protocols/v1/validators/V1ValidationSchema.js +0 -210
  77. package/src/core/network/services/PendingRequestService.js +0 -172
  78. package/src/core/network/services/TransactionCommitService.js +0 -149
  79. package/src/core/network/services/ValidatorHealthCheckService.js +0 -127
  80. package/src/utils/deepEqualApplyPayload.js +0 -40
  81. package/src/utils/logger.js +0 -25
  82. package/src/utils/protobuf/networkV1.generated.cjs +0 -2460
  83. package/tests/unit/network/LegacyNetworkMessageRouter.test.js +0 -54
  84. package/tests/unit/network/ProtocolSession.test.js +0 -127
  85. package/tests/unit/network/services/ConnectionManager.test.js +0 -450
  86. package/tests/unit/network/services/MessageOrchestrator.test.js +0 -445
  87. package/tests/unit/network/services/PendingRequestService.test.js +0 -431
  88. package/tests/unit/network/services/TransactionCommitService.test.js +0 -246
  89. package/tests/unit/network/services/TransactionPoolService.test.js +0 -489
  90. package/tests/unit/network/services/TransactionRateLimiterService.test.js +0 -139
  91. package/tests/unit/network/services/ValidatorHealthCheckService.test.js +0 -115
  92. package/tests/unit/network/services/services.test.js +0 -17
  93. package/tests/unit/network/utils/v1TestUtils.js +0 -153
  94. package/tests/unit/network/v1/NetworkMessageRouterV1.test.js +0 -151
  95. package/tests/unit/network/v1/V1BaseOperation.test.js +0 -356
  96. package/tests/unit/network/v1/V1BroadcastTransactionOperationHandler.test.js +0 -129
  97. package/tests/unit/network/v1/V1BroadcastTransactionRequest.test.js +0 -53
  98. package/tests/unit/network/v1/V1BroadcastTransactionResponse.test.js +0 -512
  99. package/tests/unit/network/v1/V1LivenessRequest.test.js +0 -32
  100. package/tests/unit/network/v1/V1LivenessResponse.test.js +0 -45
  101. package/tests/unit/network/v1/V1ResultCode.test.js +0 -84
  102. package/tests/unit/network/v1/V1ValidationSchema.test.js +0 -13
  103. package/tests/unit/network/v1/connectionPolicies.test.js +0 -49
  104. package/tests/unit/network/v1/handlers/V1BaseOperationHandler.test.js +0 -284
  105. package/tests/unit/network/v1/handlers/V1BroadcastTransactionOperationHandler.test.js +0 -794
  106. package/tests/unit/network/v1/handlers/V1LivenessOperationHandler.test.js +0 -193
  107. package/tests/unit/network/v1/v1.handlers.test.js +0 -15
  108. package/tests/unit/network/v1/v1.test.js +0 -19
  109. package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionRequest.test.js +0 -119
  110. package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionResponse.test.js +0 -136
  111. package/tests/unit/network/v1/v1ValidationSchema/common.test.js +0 -308
  112. package/tests/unit/network/v1/v1ValidationSchema/livenessRequest.test.js +0 -90
  113. package/tests/unit/network/v1/v1ValidationSchema/livenessResponse.test.js +0 -133
  114. package/tests/unit/utils/deepEqualApplyPayload/deepEqualApplyPayload.test.js +0 -102
@@ -1,489 +0,0 @@
1
- import { test } from 'brittle';
2
- import b4a from 'b4a';
3
- import remote from 'hypercore/lib/fully-remote-proof.js';
4
- import sinon from 'sinon';
5
-
6
- import TransactionPoolService, {
7
- TransactionPoolAlreadyQueuedError,
8
- TransactionPoolFullError,
9
- TransactionPoolInvalidIncomingDataError,
10
- TransactionPoolMissingCommitReceiptError,
11
- TransactionPoolProofUnavailableError
12
- } from '../../../../src/core/network/services/TransactionPoolService.js';
13
- import TransactionCommitService from '../../../../src/core/network/services/TransactionCommitService.js';
14
- import nodeEntryUtils from '../../../../src/core/state/utils/nodeEntry.js';
15
- import { safeDecodeApplyOperation } from '../../../../src/utils/protobuf/operationHelpers.js';
16
- import { bigIntTo16ByteBuffer, decimalStringToBigInt } from '../../../../src/utils/amountSerialization.js';
17
- import { BATCH_SIZE } from '../../../../src/utils/constants.js';
18
- import { config } from '../../../helpers/config.js';
19
- import {
20
- buildTransferPayload,
21
- setupTransferScenario
22
- } from '../../state/apply/transfer/transferScenarioHelpers.js';
23
-
24
- const CONFIG_DEFAULT = { enableWallet: true, txPoolSize: 10, processIntervalMs: 50 }
25
- const CONFIG_TX_POOL_INCREASE = { enableWallet: true, txPoolSize: 100, processIntervalMs: 50 };
26
-
27
-
28
- // TODO: base in the State.js is private, so I had to create an adapter fixture to expose the necessary methods for TransactionPoolService testing. Refactor State.js to allow better testability without needing this kind of workaround.
29
- function createStateFixture(validatorPeer) {
30
- const base = validatorPeer.base;
31
-
32
- return {
33
- async isAdminAllowedToValidate() {
34
- return false;
35
- },
36
- async allowedToValidate(address) {
37
- if (!base.writable || base.isIndexer) return false;
38
- const entry = await base.view.get(address);
39
- const decoded = entry?.value ? nodeEntryUtils.decode(entry.value) : null;
40
- return !!(decoded?.isWriter && !decoded?.isIndexer);
41
- },
42
- async appendWithProofOfPublication(batch, batchTxHashes) {
43
- const end = await base.append(batch);
44
- await base.update();
45
- const start = end - batch.length;
46
- const timestamp = Date.now();
47
- const snapshot = base.local.snapshot();
48
- await snapshot.ready();
49
-
50
- try {
51
- const receipts = [];
52
- for (let i = 0; i < batch.length; i++) {
53
- const blockNumber = start + i;
54
- const txHash = batchTxHashes[i];
55
- const completeTx = batch[i];
56
- const rawBlock = await snapshot.get(blockNumber, { raw: true, wait: false });
57
-
58
- let proof = null;
59
- let proofError = null;
60
-
61
- if (!rawBlock) {
62
- proofError = `Missing raw block after append (block=${blockNumber})`;
63
- } else {
64
- try {
65
- proof = await remote.proof(snapshot, { index: blockNumber, block: rawBlock });
66
- } catch (error) {
67
- proofError = error?.message ?? 'Proof generation failed';
68
- }
69
- }
70
-
71
- receipts.push({
72
- txHash,
73
- completeTx,
74
- proof,
75
- proofError,
76
- timestamp,
77
- blockNumber
78
- });
79
- }
80
-
81
- return receipts;
82
- } finally {
83
- await snapshot.close();
84
- }
85
- }
86
- };
87
- }
88
-
89
- test('TransactionPoolService processes queued transaction and resolves commit with proof', async t => {
90
- const context = await setupTransferScenario(t, { nodes: 4 });
91
- const validatorPeer = context.transferScenario.validatorPeer;
92
- const encodedTx = await buildTransferPayload(context);
93
- const decoded = safeDecodeApplyOperation(encodedTx);
94
- const txHash = decoded?.tro?.tx?.toString('hex');
95
-
96
- t.ok(txHash, 'tx hash extracted from transfer payload');
97
- if (!txHash) return;
98
-
99
- const txCommitService = new TransactionCommitService(config);
100
- const stateAddapter = createStateFixture(validatorPeer);
101
- const poolService = new TransactionPoolService(
102
- stateAddapter,
103
- validatorPeer.wallet.address,
104
- txCommitService,
105
- config
106
- );
107
-
108
- try {
109
- await poolService.start();
110
-
111
- const pendingCommit = txCommitService.registerPendingCommit(txHash);
112
- pendingCommit.catch(() => {});
113
- poolService.addTransaction(txHash, encodedTx);
114
-
115
- const receipt = await pendingCommit;
116
- t.ok(receipt, 'pending commit resolves');
117
- t.is(receipt.txHash, txHash, 'receipt txHash matches queued tx');
118
- t.ok(b4a.isBuffer(receipt.proof), 'receipt contains proof buffer');
119
- t.ok(receipt.proof.length > 0, 'proof is non-empty');
120
- t.ok(Number.isSafeInteger(receipt.blockNumber), 'blockNumber is safe integer');
121
- t.ok(receipt.blockNumber >= 0, 'blockNumber is non-negative');
122
- } finally {
123
- await poolService.stopPool();
124
- txCommitService.close();
125
- }
126
- });
127
-
128
- test('TransactionPoolService processes batch of 10 queued transactions and resolves 10 receipts', async t => {
129
- const context = await setupTransferScenario(t, {
130
- nodes: 4,
131
- senderInitialBalance: bigIntTo16ByteBuffer(decimalStringToBigInt('100'))
132
- });
133
- const validatorPeer = context.transferScenario.validatorPeer;
134
- const txCommitService = new TransactionCommitService(config);
135
- const stateAddapter = createStateFixture(validatorPeer);
136
- const poolService = new TransactionPoolService(
137
- stateAddapter,
138
- validatorPeer.wallet.address,
139
- txCommitService,
140
- config
141
- );
142
-
143
- try {
144
- await poolService.start();
145
-
146
- const txHashes = [];
147
- const pendingCommits = [];
148
- const txCount = 10;
149
-
150
- for (let i = 0; i < txCount; i++) {
151
- const encodedTx = await buildTransferPayload(context);
152
- const decoded = safeDecodeApplyOperation(encodedTx);
153
- const txHash = decoded?.tro?.tx?.toString('hex');
154
-
155
- t.ok(txHash, `tx hash extracted for tx ${i + 1}`);
156
- if (!txHash) continue;
157
-
158
- txHashes.push(txHash);
159
- const pendingCommit = txCommitService.registerPendingCommit(txHash);
160
- pendingCommit.catch(() => {});
161
- pendingCommits.push(pendingCommit);
162
- poolService.addTransaction(txHash, encodedTx);
163
- }
164
-
165
- const receipts = await Promise.all(pendingCommits);
166
- t.is(receipts.length, txCount, '10 pending commits resolved');
167
-
168
- const receiptsByHash = new Map(receipts.map(receipt => [receipt.txHash, receipt]));
169
- for (const txHash of txHashes) {
170
- const receipt = receiptsByHash.get(txHash);
171
- t.ok(receipt, `receipt exists for tx ${txHash}`);
172
- t.ok(b4a.isBuffer(receipt.proof), `proof is buffer for tx ${txHash}`);
173
- t.ok(receipt.proof.length > 0, `proof is non-empty for tx ${txHash}`);
174
- }
175
- } finally {
176
- await poolService.stopPool();
177
- txCommitService.close();
178
- }
179
- });
180
-
181
- test('TransactionPoolService.addTransaction enforces pool size limit via validateEnqueue', t => {
182
- const service = new TransactionPoolService({}, 'test', {}, { txPoolSize: 1, enableWallet: true });
183
-
184
- service.addTransaction('tx-1', b4a.from('aa', 'hex'));
185
- t.exception(
186
- () => service.addTransaction('tx-2', b4a.from('bb', 'hex')),
187
- TransactionPoolFullError
188
- );
189
- t.is(service.txPool.size(), 1);
190
- });
191
-
192
- test('TransactionPoolService.addTransaction rejects invalid incoming payload', t => {
193
- const service = new TransactionPoolService({}, 'test', {}, { txPoolSize: 10, enableWallet: true });
194
-
195
- t.exception(
196
- () => service.addTransaction('', b4a.from('aa', 'hex')),
197
- TransactionPoolInvalidIncomingDataError
198
- );
199
- t.exception(
200
- () => service.addTransaction('tx-1', 'not-a-buffer'),
201
- TransactionPoolInvalidIncomingDataError
202
- );
203
- });
204
-
205
- test('TransactionPoolService.addTransaction rejects duplicate txHash', t => {
206
- const service = new TransactionPoolService({}, 'validator-address', {}, { txPoolSize: 10, enableWallet: true });
207
- service.addTransaction('tx-dup', b4a.from('aa', 'hex'));
208
-
209
- t.exception(
210
- () => service.addTransaction('tx-dup', b4a.from('bb', 'hex')),
211
- TransactionPoolAlreadyQueuedError
212
- );
213
- t.is(service.txPool.size(), 1);
214
- });
215
-
216
- test('TransactionPoolService rejects pending commit when proof is unavailable', async t => {
217
- const txHash = 'a'.repeat(64);
218
- const txCommitService = new TransactionCommitService({ txCommitTimeout: 1_000 });
219
- const service = new TransactionPoolService(
220
- {
221
- async isAdminAllowedToValidate() { return false; },
222
- async allowedToValidate() { return true; },
223
- async appendWithProofOfPublication(_batch, hashes) {
224
- return [{
225
- txHash: hashes[0],
226
- proof: null,
227
- blockNumber: 123,
228
- proofError: 'proof-missing',
229
- timestamp: new Date(1234)
230
- }];
231
- }
232
- },
233
- 'validator-address',
234
- txCommitService,
235
- CONFIG_DEFAULT
236
- );
237
-
238
- try {
239
- await service.start();
240
- const pendingCommit = txCommitService.registerPendingCommit(txHash);
241
- pendingCommit.catch(() => {});
242
- service.addTransaction(txHash, b4a.from('aa', 'hex'));
243
-
244
- try {
245
- await pendingCommit;
246
- t.fail('expected pending commit to reject');
247
- } catch (error) {
248
- t.ok(error instanceof TransactionPoolProofUnavailableError);
249
- t.is(error.txHash, txHash);
250
- t.is(error.blockNumber, 123);
251
- t.is(error.timestamp, 1234);
252
- t.is(error.reason, 'proof-missing');
253
- }
254
- } finally {
255
- await service.stopPool();
256
- txCommitService.close();
257
- }
258
- });
259
-
260
- test('TransactionPoolService rejects pending commit when commit receipt is missing', async t => {
261
- const txHash = 'b'.repeat(64);
262
- const txCommitService = new TransactionCommitService({ txCommitTimeout: 1_000 });
263
- const service = new TransactionPoolService(
264
- {
265
- async isAdminAllowedToValidate() { return true; },
266
- async allowedToValidate() { return false; },
267
- async appendWithProofOfPublication() {
268
- return [{ txHash: 'c'.repeat(64), proof: b4a.from('aa', 'hex') }];
269
- }
270
- },
271
- 'validator-address',
272
- txCommitService,
273
- CONFIG_DEFAULT
274
- );
275
-
276
- try {
277
- await service.start();
278
- const pendingCommit = txCommitService.registerPendingCommit(txHash);
279
- pendingCommit.catch(() => {});
280
- service.addTransaction(txHash, b4a.from('aa', 'hex'));
281
-
282
- try {
283
- await pendingCommit;
284
- t.fail('expected pending commit to reject');
285
- } catch (error) {
286
- t.ok(error instanceof TransactionPoolMissingCommitReceiptError);
287
- t.is(error.txHash, txHash);
288
- }
289
- } finally {
290
- await service.stopPool();
291
- txCommitService.close();
292
- }
293
- });
294
-
295
- test('TransactionPoolService rejects all pending commits when appendWithProofOfPublication throws', async t => {
296
- const txHashA = 'd'.repeat(64);
297
- const txHashB = 'e'.repeat(64);
298
- const appendError = new Error('append failed');
299
- const txCommitService = new TransactionCommitService({ txCommitTimeout: 1_000 });
300
- const service = new TransactionPoolService(
301
- {
302
- async isAdminAllowedToValidate() { return false; },
303
- async allowedToValidate() { return true; },
304
- async appendWithProofOfPublication() {
305
- throw appendError;
306
- }
307
- },
308
- 'validator-address',
309
- txCommitService,
310
- CONFIG_DEFAULT
311
- );
312
-
313
- try {
314
- await service.start();
315
- const pendingA = txCommitService.registerPendingCommit(txHashA);
316
- const pendingB = txCommitService.registerPendingCommit(txHashB);
317
- pendingA.catch(() => {});
318
- pendingB.catch(() => {});
319
-
320
- service.addTransaction(txHashA, b4a.from('aa', 'hex'));
321
- service.addTransaction(txHashB, b4a.from('bb', 'hex'));
322
-
323
- const results = await Promise.allSettled([pendingA, pendingB]);
324
- t.is(results[0].status, 'rejected');
325
- t.is(results[1].status, 'rejected');
326
- t.is(results[0].reason, appendError);
327
- t.is(results[1].reason, appendError);
328
- } finally {
329
- await service.stopPool();
330
- txCommitService.close();
331
- }
332
- });
333
-
334
- test('TransactionPoolProofUnavailableError normalizes timestamp values', t => {
335
- const txHash = 'f'.repeat(64);
336
- const fromDate = new TransactionPoolProofUnavailableError(txHash, 1, 'no-proof', new Date(5000));
337
- const fromInvalid = new TransactionPoolProofUnavailableError(txHash, 2, 'no-proof', 'invalid');
338
-
339
- t.is(fromDate.timestamp, 5000);
340
- t.is(fromInvalid.timestamp, 0);
341
- });
342
-
343
- test('TransactionPoolService.start is idempotent when scheduler is already running', async t => {
344
- const logs = [];
345
- const originalInfo = console.info;
346
- console.info = (...args) => logs.push(args.join(' '));
347
-
348
- const service = new TransactionPoolService(
349
- {
350
- async isAdminAllowedToValidate() { return false; },
351
- async allowedToValidate() { return false; },
352
- async appendWithProofOfPublication() { return []; }
353
- },
354
- 'validator-address',
355
- {
356
- resolvePendingCommit() { return false; },
357
- rejectPendingCommit() { return false; }
358
- },
359
- CONFIG_DEFAULT
360
- );
361
-
362
- try {
363
- await service.start();
364
- await service.start();
365
-
366
- t.ok(
367
- logs.some(message => message.includes('TransactionPoolService is already started')),
368
- 'second start logs already started'
369
- );
370
- } finally {
371
- console.info = originalInfo;
372
- await service.stopPool();
373
- }
374
- });
375
-
376
- test('TransactionPoolService schedules immediate follow-up run when queue remains after batch', async t => {
377
- const clock = sinon.useFakeTimers({ now: 0 });
378
- const txCount = BATCH_SIZE + 1;
379
- let appendCalls = 0;
380
-
381
- const service = new TransactionPoolService(
382
- {
383
- async isAdminAllowedToValidate() { return true; },
384
- async allowedToValidate() { return true; },
385
- async appendWithProofOfPublication(_encodedBatch, txHashes) {
386
- appendCalls++;
387
- return txHashes.map((txHash, index) => ({
388
- txHash,
389
- proof: b4a.from('aa', 'hex'),
390
- blockNumber: index,
391
- timestamp: Date.now()
392
- }));
393
- }
394
- },
395
- 'validator-address',
396
- {
397
- resolvePendingCommit() { return true; },
398
- rejectPendingCommit() { return true; }
399
- },
400
- CONFIG_TX_POOL_INCREASE
401
- );
402
-
403
- try {
404
- for (let i = 0; i < txCount; i++) {
405
- service.addTransaction(`tx-${i}`, b4a.from('aa', 'hex'));
406
- }
407
-
408
- await service.start();
409
- for (let i = 0; i < 5 && appendCalls < 2; i++) {
410
- await clock.tickAsync(1);
411
- }
412
-
413
- t.ok(appendCalls >= 2, 'queue processed in at least two batches before 50ms interval');
414
- } finally {
415
- await service.stopPool();
416
- clock.restore();
417
- sinon.restore();
418
- }
419
- });
420
-
421
- test('TransactionPoolService wraps worker errors from validation permission checks', async t => {
422
- const clock = sinon.useFakeTimers({ now: 0 });
423
- const errors = [];
424
- const originalError = console.error;
425
- console.error = (...args) => errors.push(args);
426
-
427
- const service = new TransactionPoolService(
428
- {
429
- async isAdminAllowedToValidate() {
430
- throw new Error('permission boom');
431
- },
432
- async allowedToValidate() { return false; },
433
- async appendWithProofOfPublication() { return []; }
434
- },
435
- 'validator-address',
436
- {
437
- resolvePendingCommit() { return false; },
438
- rejectPendingCommit() { return false; }
439
- },
440
- { enableWallet: true, txPoolSize: 10 }
441
- );
442
-
443
- try {
444
- await service.start();
445
- await clock.tickAsync(0);
446
-
447
- const workerError = errors
448
- .map((entry) => entry[1])
449
- .find((value) => value instanceof Error && value.message.includes('TransactionPoolService worker error: permission boom'));
450
-
451
- t.ok(workerError, 'worker error is wrapped with TransactionPoolService context');
452
- } finally {
453
- console.error = originalError;
454
- await service.stopPool();
455
- clock.restore();
456
- sinon.restore();
457
- }
458
- });
459
-
460
- test('TransactionPoolService.start does nothing when wallet is disabled', async t => {
461
- const logs = [];
462
- const originalInfo = console.info;
463
- console.info = (...args) => logs.push(args.join(' '));
464
-
465
- const service = new TransactionPoolService(
466
- {
467
- async isAdminAllowedToValidate() { return true; },
468
- async allowedToValidate() { return true; },
469
- async appendWithProofOfPublication() { return []; }
470
- },
471
- 'validator-address',
472
- {
473
- resolvePendingCommit() { return false; },
474
- rejectPendingCommit() { return false; }
475
- },
476
- { enableWallet: false, txPoolSize: 10 }
477
- );
478
-
479
- try {
480
- await service.start();
481
- t.ok(
482
- logs.some(message => message.includes('Wallet is not enabled')),
483
- 'start logs wallet disabled'
484
- );
485
- } finally {
486
- console.info = originalInfo;
487
- await service.stopPool();
488
- }
489
- });
@@ -1,139 +0,0 @@
1
- import sinon from 'sinon';
2
- import { test } from 'brittle';
3
- import b4a from 'b4a';
4
-
5
- import TransactionRateLimiterService from '../../../../src/core/network/services/TransactionRateLimiterService.js';
6
- import { V1RateLimitedError } from '../../../../src/core/network/protocols/v1/V1ProtocolError.js';
7
- import { config } from '../../../helpers/config.js';
8
- import { testKeyPair1, testKeyPair2 } from '../../../fixtures/apply.fixtures.js';
9
-
10
- const CLEANUP_INTERVAL_MS = config.rateLimitCleanupIntervalMs;
11
- const CONNECTION_TIMEOUT_MS = config.rateLimitConnectionTimeoutMs;
12
- const MAX_TRANSACTIONS_PER_SECOND = config.rateLimitMaxTransactionsPerSecond;
13
-
14
- const makeConnection = (publicKeyHex) => {
15
- return {
16
- remotePublicKey: b4a.from(publicKeyHex, 'hex'),
17
- end: sinon.stub()
18
- };
19
- };
20
-
21
- const makeSwarm = () => {
22
- return {
23
- leavePeer: sinon.stub()
24
- };
25
- };
26
-
27
- test('TransactionRateLimiterService', async (t) => {
28
- test('legacyHandleRateLimit disconnects after MAX+1 tx in the same second', async (t) => {
29
- const clock = sinon.useFakeTimers({ now: 0 });
30
- try {
31
- const swarm = makeSwarm();
32
- const limiter = new TransactionRateLimiterService(swarm, config);
33
- const connection = makeConnection(testKeyPair1.publicKey);
34
-
35
- for (let i = 0; i < MAX_TRANSACTIONS_PER_SECOND; i++) {
36
- t.is(limiter.legacyHandleRateLimit(connection), false);
37
- }
38
-
39
- t.is(limiter.legacyHandleRateLimit(connection), true);
40
- t.is(swarm.leavePeer.callCount, 1);
41
- t.alike(swarm.leavePeer.firstCall.args[0], connection.remotePublicKey);
42
- t.is(connection.end.callCount, 1);
43
- } finally {
44
- clock.restore();
45
- sinon.restore();
46
- }
47
- });
48
-
49
- test('legacyHandleRateLimit resets the window on the next second', async (t) => {
50
- const clock = sinon.useFakeTimers({ now: 0 });
51
- try {
52
- const swarm = makeSwarm();
53
- const limiter = new TransactionRateLimiterService(swarm, config);
54
- const connection = makeConnection(testKeyPair1.publicKey);
55
-
56
- for (let i = 0; i < MAX_TRANSACTIONS_PER_SECOND; i++) {
57
- t.is(limiter.legacyHandleRateLimit(connection), false);
58
- }
59
-
60
- clock.tick(1000);
61
- t.is(limiter.legacyHandleRateLimit(connection), false);
62
- t.is(swarm.leavePeer.callCount, 0);
63
- t.is(connection.end.callCount, 0);
64
- } finally {
65
- clock.restore();
66
- sinon.restore();
67
- }
68
- });
69
-
70
- test('v1HandleRateLimit throws RateLimitedError after MAX+1 tx in the same second', async (t) => {
71
- const clock = sinon.useFakeTimers({ now: 0 });
72
- try {
73
- const limiter = new TransactionRateLimiterService(makeSwarm(), config);
74
- const connection = makeConnection(testKeyPair2.publicKey);
75
-
76
- for (let i = 0; i < MAX_TRANSACTIONS_PER_SECOND; i++) {
77
- limiter.v1HandleRateLimit(connection);
78
- }
79
-
80
- let err;
81
- try {
82
- limiter.v1HandleRateLimit(connection);
83
- } catch (error) {
84
- err = error;
85
- }
86
-
87
- t.ok(err instanceof V1RateLimitedError);
88
- t.ok(err.message.includes('Rate limit exceeded for peer'));
89
- } finally {
90
- clock.restore();
91
- sinon.restore();
92
- }
93
- });
94
-
95
- test('v1HandleRateLimit resets the window on the next second', async (t) => {
96
- const clock = sinon.useFakeTimers({ now: 0 });
97
- try {
98
- const limiter = new TransactionRateLimiterService(makeSwarm(), config);
99
- const connection = makeConnection(testKeyPair2.publicKey);
100
-
101
- for (let i = 0; i < MAX_TRANSACTIONS_PER_SECOND; i++) {
102
- limiter.v1HandleRateLimit(connection);
103
- }
104
-
105
- clock.tick(1000);
106
- limiter.v1HandleRateLimit(connection);
107
- } finally {
108
- clock.restore();
109
- sinon.restore();
110
- }
111
- });
112
-
113
- test('cleanUpOldConnections evicts inactive stats after cleanup interval', async (t) => {
114
- const clock = sinon.useFakeTimers({ now: 0 });
115
- try {
116
- const swarm = makeSwarm();
117
- const limiter = new TransactionRateLimiterService(swarm, config);
118
-
119
- const oldPeer = makeConnection(testKeyPair1.publicKey);
120
- const activePeer = makeConnection(testKeyPair2.publicKey);
121
-
122
- t.is(limiter.legacyHandleRateLimit(oldPeer), false);
123
- t.is(limiter.legacyHandleRateLimit(activePeer), false);
124
-
125
- clock.tick(CONNECTION_TIMEOUT_MS + 1);
126
- t.is(limiter.legacyHandleRateLimit(activePeer), false);
127
-
128
- clock.tick(CLEANUP_INTERVAL_MS - (CONNECTION_TIMEOUT_MS + 1));
129
- t.is(Date.now(), CLEANUP_INTERVAL_MS);
130
- t.is(limiter.legacyHandleRateLimit(activePeer), false);
131
-
132
- t.is(limiter.legacyHandleRateLimit(oldPeer), false);
133
- t.is(swarm.leavePeer.callCount, 0);
134
- } finally {
135
- clock.restore();
136
- sinon.restore();
137
- }
138
- });
139
- });