trac-msb 0.2.18 → 0.2.19

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 (381) hide show
  1. package/README.md +62 -2
  2. package/SECURITY.md +9 -6
  3. package/cli/commandHandler.js +272 -0
  4. package/cli/handlers.js +210 -0
  5. package/cli/interactive.js +182 -0
  6. package/msb.mjs +7 -8
  7. package/package.json +25 -8
  8. package/rpc/create_server.js +44 -44
  9. package/rpc/handlers.js +43 -56
  10. package/rpc/routes/index.js +8 -8
  11. package/rpc/rpc_server.js +6 -2
  12. package/rpc/utils/helpers.js +46 -66
  13. package/scripts/pear-runner.js +170 -0
  14. package/src/config/args.js +60 -13
  15. package/src/config/config.js +138 -10
  16. package/src/config/env.js +36 -18
  17. package/src/core/network/Network.js +52 -24
  18. package/src/core/network/protocols/ProtocolInterface.js +5 -5
  19. package/src/core/network/protocols/connectionPolicies.js +86 -0
  20. package/src/core/network/protocols/legacy/NetworkMessageRouter.js +1 -1
  21. package/src/core/network/protocols/legacy/handlers/BaseStateOperationHandler.js +2 -2
  22. package/src/core/network/protocols/legacy/handlers/LegacyGetRequestHandler.js +3 -3
  23. package/src/core/network/protocols/legacy/handlers/LegacyRoleOperationHandler.js +1 -1
  24. package/src/core/network/protocols/legacy/handlers/LegacySubnetworkOperationHandler.js +1 -1
  25. package/src/core/network/protocols/legacy/handlers/LegacyTransferOperationHandler.js +1 -1
  26. package/src/core/network/protocols/legacy/validators/base/BaseResponse.js +7 -10
  27. package/src/core/network/protocols/shared/validators/PartialBootstrapDeploymentValidator.js +2 -2
  28. package/src/core/network/protocols/shared/validators/PartialOperationValidator.js +23 -23
  29. package/src/core/network/protocols/shared/validators/PartialRoleAccessValidator.js +14 -14
  30. package/src/core/network/protocols/shared/validators/PartialTransactionValidator.js +5 -5
  31. package/src/core/network/protocols/shared/validators/PartialTransferValidator.js +12 -11
  32. package/src/core/network/protocols/v1/V1ProtocolError.js +5 -69
  33. package/src/core/network/protocols/v1/handlers/V1BaseOperationHandler.js +3 -2
  34. package/src/core/network/protocols/v1/handlers/V1BroadcastTransactionOperationHandler.js +130 -247
  35. package/src/core/network/protocols/v1/handlers/V1LivenessOperationHandler.js +6 -5
  36. package/src/core/network/protocols/v1/handlers/broadcastTransaction/BroadcastTransactionErrorMapper.js +84 -0
  37. package/src/core/network/protocols/v1/handlers/broadcastTransaction/BroadcastTransactionOperationStrategies.js +128 -0
  38. package/src/core/network/protocols/v1/handlers/broadcastTransaction/BroadcastTransactionPayloadUtils.js +26 -0
  39. package/src/core/network/protocols/v1/validators/V1BaseOperation.js +32 -31
  40. package/src/core/network/protocols/v1/validators/V1BroadcastTransactionRequest.js +5 -3
  41. package/src/core/network/protocols/v1/validators/V1BroadcastTransactionResponse.js +34 -50
  42. package/src/core/network/protocols/v1/validators/V1ValidationSchema.js +1 -1
  43. package/src/core/network/services/ConnectionManager.js +11 -10
  44. package/src/core/network/services/MessageOrchestrator.js +53 -17
  45. package/src/core/network/services/PendingRequestService.js +22 -19
  46. package/src/core/network/services/TransactionCommitService.js +36 -36
  47. package/src/core/network/services/TransactionPoolService.js +39 -39
  48. package/src/core/network/services/TransactionRateLimiterService.js +8 -4
  49. package/src/core/network/services/ValidatorObserverService.js +397 -110
  50. package/src/core/state/State.js +190 -138
  51. package/src/core/state/utils/address.js +4 -9
  52. package/src/core/state/utils/balance.js +7 -7
  53. package/src/core/state/utils/roles.js +1 -1
  54. package/src/index.js +316 -275
  55. package/src/messages/network/v1/NetworkMessageBuilder.js +12 -21
  56. package/src/messages/network/v1/networkMessageFactory.js +1 -1
  57. package/src/messages/state/ApplyStateMessageBuilder.js +6 -6
  58. package/src/messages/state/applyStateMessageFactory.js +1 -1
  59. package/src/utils/buffer.js +1 -1
  60. package/src/utils/check.js +12 -13
  61. package/src/utils/constants.js +7 -0
  62. package/src/utils/errors.js +20 -0
  63. package/src/utils/fileUtils.js +12 -3
  64. package/src/utils/helpers.js +2 -2
  65. package/src/utils/migrationUtils.js +3 -3
  66. package/.dockerignore +0 -16
  67. package/CODE_OF_CONDUCT.md +0 -128
  68. package/docker-compose.yml +0 -17
  69. package/dockerfile +0 -41
  70. package/docs/fee_distribution.md +0 -89
  71. package/docs/trac_network_http_api.openapi.yaml +0 -889
  72. package/migration/.gitkeep +0 -0
  73. package/proto/applyOperations.proto +0 -117
  74. package/proto/network/v1/enums/message_type.proto +0 -16
  75. package/proto/network/v1/enums/result_code.proto +0 -84
  76. package/proto/network/v1/messages/broadcast_transaction_request.proto +0 -9
  77. package/proto/network/v1/messages/broadcast_transaction_response.proto +0 -13
  78. package/proto/network/v1/messages/liveness_request.proto +0 -8
  79. package/proto/network/v1/messages/liveness_response.proto +0 -11
  80. package/proto/network/v1/network_message.proto +0 -22
  81. package/rpc/rpc_services.js +0 -181
  82. package/scripts/generate-protobufs.js +0 -67
  83. package/src/core/network/identity/NetworkWalletFactory.js +0 -77
  84. package/src/core/network/protocols/shared/errors/SharedValidatorRejectionError.js +0 -27
  85. package/src/utils/cli.js +0 -130
  86. package/src/utils/cliCommands.js +0 -280
  87. package/tests/acceptance/v1/account/account.test.mjs +0 -129
  88. package/tests/acceptance/v1/balance/balance.test.mjs +0 -54
  89. package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +0 -116
  90. package/tests/acceptance/v1/confirmed-length/confirmed-length.test.mjs +0 -19
  91. package/tests/acceptance/v1/fee/fee.test.mjs +0 -11
  92. package/tests/acceptance/v1/health/health.test.mjs +0 -33
  93. package/tests/acceptance/v1/rpc.test.mjs +0 -95
  94. package/tests/acceptance/v1/tx/tx.test.mjs +0 -133
  95. package/tests/acceptance/v1/tx-details/tx-details.test.mjs +0 -241
  96. package/tests/acceptance/v1/tx-hashes/tx-hashes.test.mjs +0 -72
  97. package/tests/acceptance/v1/tx-payloads-bulk/tx-payloads-bulk.test.mjs +0 -27
  98. package/tests/acceptance/v1/txv/txv.test.mjs +0 -11
  99. package/tests/acceptance/v1/unconfirmed-length/unconfirmed-length.test.mjs +0 -11
  100. package/tests/fixtures/apply.fixtures.js +0 -77
  101. package/tests/fixtures/assembleMessage.fixtures.js +0 -41
  102. package/tests/fixtures/check.fixtures.js +0 -428
  103. package/tests/fixtures/networkV1.fixtures.js +0 -59
  104. package/tests/fixtures/protobuf.fixtures.js +0 -388
  105. package/tests/helpers/StateNetworkFactory.js +0 -155
  106. package/tests/helpers/address.js +0 -6
  107. package/tests/helpers/autobaseTestHelpers.js +0 -419
  108. package/tests/helpers/config.js +0 -4
  109. package/tests/helpers/createTestSignature.js +0 -13
  110. package/tests/helpers/regexHelper.js +0 -3
  111. package/tests/helpers/setupApplyTests.js +0 -587
  112. package/tests/helpers/transactionPayloads.mjs +0 -92
  113. package/tests/helpers/wrapper.js +0 -28
  114. package/tests/unit/messages/messages.test.js +0 -12
  115. package/tests/unit/messages/network/NetworkMessageBuilder.test.js +0 -436
  116. package/tests/unit/messages/network/NetworkMessageDirector.test.js +0 -347
  117. package/tests/unit/messages/state/applyStateMessageBuilder.complete.test.js +0 -525
  118. package/tests/unit/messages/state/applyStateMessageBuilder.partial.test.js +0 -237
  119. package/tests/unit/network/LegacyNetworkMessageRouter.test.js +0 -54
  120. package/tests/unit/network/NetworkWalletFactory.test.js +0 -156
  121. package/tests/unit/network/ProtocolSession.test.js +0 -127
  122. package/tests/unit/network/networkModule.test.js +0 -13
  123. package/tests/unit/network/services/ConnectionManager.test.js +0 -450
  124. package/tests/unit/network/services/MessageOrchestrator.test.js +0 -445
  125. package/tests/unit/network/services/PendingRequestService.test.js +0 -431
  126. package/tests/unit/network/services/TransactionCommitService.test.js +0 -246
  127. package/tests/unit/network/services/TransactionPoolService.test.js +0 -489
  128. package/tests/unit/network/services/TransactionRateLimiterService.test.js +0 -139
  129. package/tests/unit/network/services/ValidatorHealthCheckService.test.js +0 -115
  130. package/tests/unit/network/services/services.test.js +0 -17
  131. package/tests/unit/network/utils/v1TestUtils.js +0 -153
  132. package/tests/unit/network/v1/NetworkMessageRouterV1.test.js +0 -151
  133. package/tests/unit/network/v1/V1BaseOperation.test.js +0 -356
  134. package/tests/unit/network/v1/V1BroadcastTransactionOperationHandler.test.js +0 -129
  135. package/tests/unit/network/v1/V1BroadcastTransactionRequest.test.js +0 -53
  136. package/tests/unit/network/v1/V1BroadcastTransactionResponse.test.js +0 -512
  137. package/tests/unit/network/v1/V1LivenessRequest.test.js +0 -32
  138. package/tests/unit/network/v1/V1LivenessResponse.test.js +0 -45
  139. package/tests/unit/network/v1/V1ResultCode.test.js +0 -84
  140. package/tests/unit/network/v1/V1ValidationSchema.test.js +0 -13
  141. package/tests/unit/network/v1/connectionPolicies.test.js +0 -49
  142. package/tests/unit/network/v1/handlers/V1BaseOperationHandler.test.js +0 -284
  143. package/tests/unit/network/v1/handlers/V1BroadcastTransactionOperationHandler.test.js +0 -794
  144. package/tests/unit/network/v1/handlers/V1LivenessOperationHandler.test.js +0 -193
  145. package/tests/unit/network/v1/v1.handlers.test.js +0 -15
  146. package/tests/unit/network/v1/v1.test.js +0 -19
  147. package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionRequest.test.js +0 -119
  148. package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionResponse.test.js +0 -136
  149. package/tests/unit/network/v1/v1ValidationSchema/common.test.js +0 -308
  150. package/tests/unit/network/v1/v1ValidationSchema/livenessRequest.test.js +0 -90
  151. package/tests/unit/network/v1/v1ValidationSchema/livenessResponse.test.js +0 -133
  152. package/tests/unit/state/State.test.js +0 -80
  153. package/tests/unit/state/apply/addAdmin/addAdminHappyPathScenario.js +0 -42
  154. package/tests/unit/state/apply/addAdmin/addAdminScenarioHelpers.js +0 -276
  155. package/tests/unit/state/apply/addAdmin/adminEntryEncodingFailureScenario.js +0 -30
  156. package/tests/unit/state/apply/addAdmin/adminEntryExistsScenario.js +0 -78
  157. package/tests/unit/state/apply/addAdmin/nodeEntryInitializationFailureScenario.js +0 -30
  158. package/tests/unit/state/apply/addAdmin/nonBootstrapNodeScenario.js +0 -68
  159. package/tests/unit/state/apply/addAdmin/state.apply.addAdmin.test.js +0 -159
  160. package/tests/unit/state/apply/addIndexer/addIndexerHappyPathScenario.js +0 -39
  161. package/tests/unit/state/apply/addIndexer/addIndexerMultipleIndexersInTheNetworkScenario.js +0 -167
  162. package/tests/unit/state/apply/addIndexer/addIndexerPretenderAlreadyIndexerScenario.js +0 -21
  163. package/tests/unit/state/apply/addIndexer/addIndexerPretenderNotWriterScenario.js +0 -21
  164. package/tests/unit/state/apply/addIndexer/addIndexerRemoveAndReAddScenario.js +0 -186
  165. package/tests/unit/state/apply/addIndexer/addIndexerScenarioHelpers.js +0 -443
  166. package/tests/unit/state/apply/addIndexer/addIndexerWriterKeyAlreadyRegisteredScenario.js +0 -32
  167. package/tests/unit/state/apply/addIndexer/state.apply.addIndexer.test.js +0 -297
  168. package/tests/unit/state/apply/addWriter/addWriterHappyPathScenario.js +0 -41
  169. package/tests/unit/state/apply/addWriter/addWriterInvalidValidatorSignatureScenario.js +0 -32
  170. package/tests/unit/state/apply/addWriter/addWriterNewWkScenario.js +0 -149
  171. package/tests/unit/state/apply/addWriter/addWriterRequesterAlreadyWriterScenario.js +0 -21
  172. package/tests/unit/state/apply/addWriter/addWriterRequesterBalanceInsufficientScenario.js +0 -21
  173. package/tests/unit/state/apply/addWriter/addWriterRequesterEntryDecodeFailureScenario.js +0 -19
  174. package/tests/unit/state/apply/addWriter/addWriterRequesterEntryMissingScenario.js +0 -19
  175. package/tests/unit/state/apply/addWriter/addWriterRequesterIndexerScenario.js +0 -21
  176. package/tests/unit/state/apply/addWriter/addWriterRequesterNotWhitelistedScenario.js +0 -21
  177. package/tests/unit/state/apply/addWriter/addWriterScenarioHelpers.js +0 -766
  178. package/tests/unit/state/apply/addWriter/addWriterStakeBalanceUpdateFailureScenario.js +0 -50
  179. package/tests/unit/state/apply/addWriter/addWriterStakeInsufficientBalanceScenario.js +0 -29
  180. package/tests/unit/state/apply/addWriter/addWriterStakeInvalidBalanceScenario.js +0 -29
  181. package/tests/unit/state/apply/addWriter/addWriterStakeInvalidEntryScenario.js +0 -21
  182. package/tests/unit/state/apply/addWriter/addWriterStakeStakedBalanceFailureScenario.js +0 -37
  183. package/tests/unit/state/apply/addWriter/addWriterStakeSubtractFailureScenario.js +0 -42
  184. package/tests/unit/state/apply/addWriter/addWriterValidatorRewardScenario.js +0 -106
  185. package/tests/unit/state/apply/addWriter/addWriterWriterKeyMismatchScenario.js +0 -54
  186. package/tests/unit/state/apply/addWriter/addWriterWriterKeyOwnershipScenario.js +0 -54
  187. package/tests/unit/state/apply/addWriter/addWriterZeroWriterKeyScenario.js +0 -29
  188. package/tests/unit/state/apply/addWriter/state.apply.addWriter.test.js +0 -309
  189. package/tests/unit/state/apply/adminRecovery/adminRecoveryHappyPathScenario.js +0 -30
  190. package/tests/unit/state/apply/adminRecovery/adminRecoveryScenarioHelpers.js +0 -881
  191. package/tests/unit/state/apply/adminRecovery/state.apply.adminRecovery.test.js +0 -435
  192. package/tests/unit/state/apply/appendWhitelist/appendWhitelistBanAndReapplyScenario.js +0 -78
  193. package/tests/unit/state/apply/appendWhitelist/appendWhitelistExistingReaderHappyPathScenario.js +0 -98
  194. package/tests/unit/state/apply/appendWhitelist/appendWhitelistFeeAfterDisableScenario.js +0 -66
  195. package/tests/unit/state/apply/appendWhitelist/appendWhitelistHappyPathScenario.js +0 -55
  196. package/tests/unit/state/apply/appendWhitelist/appendWhitelistInsufficientAdminBalanceScenario.js +0 -103
  197. package/tests/unit/state/apply/appendWhitelist/appendWhitelistNodeAlreadyWhitelistedScenario.js +0 -60
  198. package/tests/unit/state/apply/appendWhitelist/appendWhitelistScenarioHelpers.js +0 -189
  199. package/tests/unit/state/apply/appendWhitelist/state.apply.appendWhitelist.test.js +0 -220
  200. package/tests/unit/state/apply/balanceInitialization/balanceInitializationHappyPathScenario.js +0 -82
  201. package/tests/unit/state/apply/balanceInitialization/balanceInitializationScenarioHelpers.js +0 -111
  202. package/tests/unit/state/apply/balanceInitialization/invalidAmountScenario.js +0 -45
  203. package/tests/unit/state/apply/balanceInitialization/nodeEntryBalanceUpdateFailureScenario.js +0 -82
  204. package/tests/unit/state/apply/balanceInitialization/state.apply.balanceInitialization.test.js +0 -189
  205. package/tests/unit/state/apply/banValidator/banValidatorBanAndReWhitelistScenario.js +0 -156
  206. package/tests/unit/state/apply/banValidator/banValidatorHappyPathScenario.js +0 -36
  207. package/tests/unit/state/apply/banValidator/banValidatorScenarioHelpers.js +0 -531
  208. package/tests/unit/state/apply/banValidator/banValidatorSequentialBansScenario.js +0 -74
  209. package/tests/unit/state/apply/banValidator/banValidatorTargetDecodeFailureScenario.js +0 -19
  210. package/tests/unit/state/apply/banValidator/banValidatorTargetIndexerScenario.js +0 -32
  211. package/tests/unit/state/apply/banValidator/banValidatorTargetNodeEntryMissingScenario.js +0 -19
  212. package/tests/unit/state/apply/banValidator/banValidatorTargetRoleUpdateFailureScenario.js +0 -19
  213. package/tests/unit/state/apply/banValidator/banValidatorWhitelistedNonWriterScenario.js +0 -38
  214. package/tests/unit/state/apply/banValidator/banValidatorWhitelistedZeroBalanceScenario.js +0 -91
  215. package/tests/unit/state/apply/banValidator/banValidatorWithdrawFailureScenario.js +0 -19
  216. package/tests/unit/state/apply/banValidator/state.apply.banValidator.test.js +0 -266
  217. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentDuplicateRegistrationScenario.js +0 -143
  218. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentHappyPathScenario.js +0 -26
  219. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentIncompleteOperationScenario.js +0 -94
  220. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentInvalidDeploymentEntryScenario.js +0 -37
  221. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentMultipleBootstrapScenario.js +0 -86
  222. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentScenarioHelpers.js +0 -347
  223. package/tests/unit/state/apply/bootstrapDeployment/invalidValidatorNodeEntryScenario.js +0 -57
  224. package/tests/unit/state/apply/bootstrapDeployment/state.apply.bootstrapDeployment.test.js +0 -429
  225. package/tests/unit/state/apply/common/access-control/adminConsistencyMismatchScenario.js +0 -120
  226. package/tests/unit/state/apply/common/access-control/adminEntryDecodeFailureScenario.js +0 -130
  227. package/tests/unit/state/apply/common/access-control/adminEntryExistsScenario.js +0 -93
  228. package/tests/unit/state/apply/common/access-control/adminEntryMissingScenario.js +0 -108
  229. package/tests/unit/state/apply/common/access-control/adminOnlyGuardScenario.js +0 -126
  230. package/tests/unit/state/apply/common/access-control/adminPublicKeyDecodeFailureScenario.js +0 -121
  231. package/tests/unit/state/apply/common/access-control/roleAccessOperationValidationScenario.js +0 -50
  232. package/tests/unit/state/apply/common/adminControlOperationValidationScenario.js +0 -56
  233. package/tests/unit/state/apply/common/balances/adminEntryUpdateFailureScenario.js +0 -52
  234. package/tests/unit/state/apply/common/balances/base/requesterBalanceScenarioBase.js +0 -198
  235. package/tests/unit/state/apply/common/balances/feeDecodeFailureScenario.js +0 -52
  236. package/tests/unit/state/apply/common/balances/requesterBalanceDecodeFailureScenario.js +0 -15
  237. package/tests/unit/state/apply/common/balances/requesterBalanceFeeApplicationFailureScenario.js +0 -11
  238. package/tests/unit/state/apply/common/balances/requesterBalanceInsufficientScenario.js +0 -15
  239. package/tests/unit/state/apply/common/balances/requesterBalanceUpdateFailureScenario.js +0 -11
  240. package/tests/unit/state/apply/common/balances/validatorEntryRewardFailureScenario.js +0 -11
  241. package/tests/unit/state/apply/common/balances/validatorEntryUpdateFailureScenario.js +0 -11
  242. package/tests/unit/state/apply/common/balances/validatorNodeEntryDecodeFailureScenario.js +0 -40
  243. package/tests/unit/state/apply/common/base/OperationValidationScenarioBase.js +0 -114
  244. package/tests/unit/state/apply/common/commonScenarioHelper.js +0 -103
  245. package/tests/unit/state/apply/common/indexer/indexerNodeEntryDecodeFailureScenario.js +0 -36
  246. package/tests/unit/state/apply/common/indexer/indexerNodeEntryMissingScenario.js +0 -36
  247. package/tests/unit/state/apply/common/indexer/indexerRoleUpdateFailureScenario.js +0 -29
  248. package/tests/unit/state/apply/common/indexer/indexerSequenceStateInvalidScenario.js +0 -66
  249. package/tests/unit/state/apply/common/invalidMessageComponentValidationScenario.js +0 -84
  250. package/tests/unit/state/apply/common/nodeEntryInitializationFailureScenario.js +0 -47
  251. package/tests/unit/state/apply/common/operationAlreadyAppliedScenario.js +0 -85
  252. package/tests/unit/state/apply/common/payload-structure/addressWithInvalidPublicKeyScenario.js +0 -52
  253. package/tests/unit/state/apply/common/payload-structure/initializationDisabledScenario.js +0 -54
  254. package/tests/unit/state/apply/common/payload-structure/invalidAddressValidationScenario.js +0 -73
  255. package/tests/unit/state/apply/common/payload-structure/invalidHashValidationScenario.js +0 -71
  256. package/tests/unit/state/apply/common/payload-structure/invalidPayloadValidationScenario.js +0 -31
  257. package/tests/unit/state/apply/common/payload-structure/invalidSignatureValidationScenario.js +0 -142
  258. package/tests/unit/state/apply/common/payload-structure/partialOperationValidationScenario.js +0 -87
  259. package/tests/unit/state/apply/common/requester/requesterNodeEntryBufferMissingScenario.js +0 -71
  260. package/tests/unit/state/apply/common/requester/requesterNodeEntryDecodeFailureScenario.js +0 -73
  261. package/tests/unit/state/apply/common/requester/requesterNodeEntryMissingScenario.js +0 -36
  262. package/tests/unit/state/apply/common/requesterAddressValidationScenario.js +0 -44
  263. package/tests/unit/state/apply/common/requesterPublicKeyValidationScenario.js +0 -25
  264. package/tests/unit/state/apply/common/transactionValidityMismatchScenario.js +0 -98
  265. package/tests/unit/state/apply/common/validatorConsistency/base/validatorConsistencyScenarioBase.js +0 -202
  266. package/tests/unit/state/apply/common/validatorConsistency/validatorEntryDecodeFailureScenario.js +0 -17
  267. package/tests/unit/state/apply/common/validatorConsistency/validatorEntryMissingScenario.js +0 -44
  268. package/tests/unit/state/apply/common/validatorConsistency/validatorInactiveScenario.js +0 -19
  269. package/tests/unit/state/apply/common/validatorConsistency/validatorWriterKeyMismatchScenario.js +0 -18
  270. package/tests/unit/state/apply/common/validatorEntryValidation/base/validatorEntryValidationScenarioBase.js +0 -315
  271. package/tests/unit/state/apply/common/validatorEntryValidation/validatorEntryInvalidBalanceScenario.js +0 -18
  272. package/tests/unit/state/apply/common/writerKeyExistsValidationScenario.js +0 -43
  273. package/tests/unit/state/apply/disableInitialization/disableInitializationAlreadyDisabledScenario.js +0 -53
  274. package/tests/unit/state/apply/disableInitialization/disableInitializationHappyPathScenario.js +0 -24
  275. package/tests/unit/state/apply/disableInitialization/disableInitializationScenarioHelpers.js +0 -204
  276. package/tests/unit/state/apply/disableInitialization/state.apply.disableInitialization.test.js +0 -161
  277. package/tests/unit/state/apply/missing-tests.md +0 -18
  278. package/tests/unit/state/apply/removeIndexer/removeIndexerHappyPathScenario.js +0 -58
  279. package/tests/unit/state/apply/removeIndexer/removeIndexerReAddAndRemoveAgainScenario.js +0 -98
  280. package/tests/unit/state/apply/removeIndexer/removeIndexerRemoveMultipleIndexersScenario.js +0 -167
  281. package/tests/unit/state/apply/removeIndexer/removeIndexerScenarioHelpers.js +0 -429
  282. package/tests/unit/state/apply/removeIndexer/removeIndexerTargetNotIndexerScenario.js +0 -22
  283. package/tests/unit/state/apply/removeIndexer/removeIndexerWriterKeyMissingScenario.js +0 -20
  284. package/tests/unit/state/apply/removeIndexer/state.apply.removeIndexer.test.js +0 -291
  285. package/tests/unit/state/apply/removeWriter/removeWriterAndAddWriterAgainScenario.js +0 -87
  286. package/tests/unit/state/apply/removeWriter/removeWriterHappyPathScenario.js +0 -38
  287. package/tests/unit/state/apply/removeWriter/removeWriterInvalidValidatorSignatureScenario.js +0 -32
  288. package/tests/unit/state/apply/removeWriter/removeWriterRequesterBalanceInsufficientScenario.js +0 -21
  289. package/tests/unit/state/apply/removeWriter/removeWriterRequesterEntryDecodeFailureScenario.js +0 -19
  290. package/tests/unit/state/apply/removeWriter/removeWriterRequesterEntryMissingScenario.js +0 -19
  291. package/tests/unit/state/apply/removeWriter/removeWriterRequesterIndexerScenario.js +0 -21
  292. package/tests/unit/state/apply/removeWriter/removeWriterRequesterNotWriterScenario.js +0 -21
  293. package/tests/unit/state/apply/removeWriter/removeWriterRequesterRoleUpdateFailureScenario.js +0 -19
  294. package/tests/unit/state/apply/removeWriter/removeWriterScenarioHelpers.js +0 -348
  295. package/tests/unit/state/apply/removeWriter/removeWriterThroughWriterValidatorScenario.js +0 -113
  296. package/tests/unit/state/apply/removeWriter/removeWriterUnstakeFailureScenario.js +0 -33
  297. package/tests/unit/state/apply/removeWriter/removeWriterWriterKeyMismatchScenario.js +0 -21
  298. package/tests/unit/state/apply/removeWriter/removeWriterWriterKeyOwnershipScenario.js +0 -26
  299. package/tests/unit/state/apply/removeWriter/removeWriterWriterKeyRegistryMissingScenario.js +0 -22
  300. package/tests/unit/state/apply/removeWriter/state.apply.removeWriter.test.js +0 -307
  301. package/tests/unit/state/apply/state.apply.test.js +0 -24
  302. package/tests/unit/state/apply/transfer/state.apply.transfer.test.js +0 -819
  303. package/tests/unit/state/apply/transfer/transferContractSchemaValidationScenario.js +0 -22
  304. package/tests/unit/state/apply/transfer/transferDoubleSpendAcrossValidatorsScenario.js +0 -146
  305. package/tests/unit/state/apply/transfer/transferDoubleSpendSameBatchScenario.js +0 -63
  306. package/tests/unit/state/apply/transfer/transferDoubleSpendSingleValidatorScenario.js +0 -67
  307. package/tests/unit/state/apply/transfer/transferExistingRecipientAmountScenario.js +0 -31
  308. package/tests/unit/state/apply/transfer/transferExistingRecipientZeroAmountScenario.js +0 -31
  309. package/tests/unit/state/apply/transfer/transferHandlerGuardScenarios.js +0 -22
  310. package/tests/unit/state/apply/transfer/transferHappyPathScenario.js +0 -8
  311. package/tests/unit/state/apply/transfer/transferInvalidIncomingDataScenario.js +0 -66
  312. package/tests/unit/state/apply/transfer/transferNewRecipientAmountScenario.js +0 -31
  313. package/tests/unit/state/apply/transfer/transferNewRecipientZeroAmountScenario.js +0 -31
  314. package/tests/unit/state/apply/transfer/transferScenarioHelpers.js +0 -1170
  315. package/tests/unit/state/apply/transfer/transferSelfTransferAmountScenario.js +0 -38
  316. package/tests/unit/state/apply/transfer/transferSelfTransferZeroAmountScenario.js +0 -38
  317. package/tests/unit/state/apply/transfer/transferValidatorRecipientAmountScenario.js +0 -38
  318. package/tests/unit/state/apply/transfer/transferValidatorRecipientZeroAmountScenario.js +0 -38
  319. package/tests/unit/state/apply/txOperation/state.apply.txOperation.test.js +0 -318
  320. package/tests/unit/state/apply/txOperation/txOperationBootstrapNotRegisteredScenario.js +0 -70
  321. package/tests/unit/state/apply/txOperation/txOperationDifferentValidatorCreatorHappyPathScenario.js +0 -23
  322. package/tests/unit/state/apply/txOperation/txOperationInvalidDeploymentEntryScenario.js +0 -48
  323. package/tests/unit/state/apply/txOperation/txOperationInvalidFeeAmountScenario.js +0 -39
  324. package/tests/unit/state/apply/txOperation/txOperationInvalidSubnetCreatorAddressScenario.js +0 -46
  325. package/tests/unit/state/apply/txOperation/txOperationRequesterCreatorHappyPathScenario.js +0 -21
  326. package/tests/unit/state/apply/txOperation/txOperationScenarioHelpers.js +0 -432
  327. package/tests/unit/state/apply/txOperation/txOperationStandardHappyPathScenario.js +0 -21
  328. package/tests/unit/state/apply/txOperation/txOperationTransferFeeAddCreatorBalanceFailureScenario.js +0 -26
  329. package/tests/unit/state/apply/txOperation/txOperationTransferFeeAddValidatorBalanceFailureScenario.js +0 -25
  330. package/tests/unit/state/apply/txOperation/txOperationTransferFeeAddValidatorBonusFailureScenario.js +0 -27
  331. package/tests/unit/state/apply/txOperation/txOperationTransferFeeDecodeCreatorEntryScenario.js +0 -18
  332. package/tests/unit/state/apply/txOperation/txOperationTransferFeeDecodeRequesterEntryScenario.js +0 -17
  333. package/tests/unit/state/apply/txOperation/txOperationTransferFeeDecodeValidatorEntryScenario.js +0 -31
  334. package/tests/unit/state/apply/txOperation/txOperationTransferFeeGuardBypassScenario.js +0 -49
  335. package/tests/unit/state/apply/txOperation/txOperationTransferFeeGuardScenarioFactory.js +0 -93
  336. package/tests/unit/state/apply/txOperation/txOperationTransferFeeInsufficientRequesterBalanceScenario.js +0 -28
  337. package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidCreatorBalanceScenario.js +0 -29
  338. package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidRequesterBalanceScenario.js +0 -28
  339. package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidRequesterEntryScenario.js +0 -17
  340. package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidValidatorBalanceScenario.js +0 -33
  341. package/tests/unit/state/apply/txOperation/txOperationTransferFeeMissingCreatorEntryScenario.js +0 -18
  342. package/tests/unit/state/apply/txOperation/txOperationTransferFeeSubtractFailureScenario.js +0 -25
  343. package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateCreatorBalanceFailureScenario.js +0 -26
  344. package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateFailureScenario.js +0 -25
  345. package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateValidatorBalanceFailureScenario.js +0 -26
  346. package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateValidatorBonusFailureScenario.js +0 -27
  347. package/tests/unit/state/apply/txOperation/txOperationValidatorCreatorHappyPathScenario.js +0 -21
  348. package/tests/unit/state/stateModule.test.js +0 -23
  349. package/tests/unit/state/stateTestUtils.js +0 -32
  350. package/tests/unit/state/utils/address.test.js +0 -63
  351. package/tests/unit/state/utils/adminEntry.test.js +0 -52
  352. package/tests/unit/state/utils/balance.test.js +0 -320
  353. package/tests/unit/state/utils/lengthEntry.test.js +0 -54
  354. package/tests/unit/state/utils/nodeEntry.test.js +0 -298
  355. package/tests/unit/state/utils/roles.test.js +0 -44
  356. package/tests/unit/unit.test.js +0 -14
  357. package/tests/unit/utils/amountSerialization/amountSerialization.test.js +0 -237
  358. package/tests/unit/utils/buffer/buffer.test.js +0 -251
  359. package/tests/unit/utils/check/adminControlOperation.test.js +0 -156
  360. package/tests/unit/utils/check/balanceInitializationOperation.test.js +0 -54
  361. package/tests/unit/utils/check/bootstrapDeploymentOperation.test.js +0 -104
  362. package/tests/unit/utils/check/check.test.js +0 -17
  363. package/tests/unit/utils/check/common.test.js +0 -419
  364. package/tests/unit/utils/check/coreAdminOperation.test.js +0 -95
  365. package/tests/unit/utils/check/roleAccessOperation.test.js +0 -255
  366. package/tests/unit/utils/check/transactionOperation.test.js +0 -107
  367. package/tests/unit/utils/check/transferOperation.test.js +0 -102
  368. package/tests/unit/utils/deepEqualApplyPayload/deepEqualApplyPayload.test.js +0 -102
  369. package/tests/unit/utils/fileUtils/readAddressesFromWhitelistFile.test.js +0 -95
  370. package/tests/unit/utils/fileUtils/readBalanceMigrationFile.test.js +0 -149
  371. package/tests/unit/utils/helpers/helpers.test.js +0 -11
  372. package/tests/unit/utils/helpers/isHexString.test.js +0 -12
  373. package/tests/unit/utils/helpers/normalizeHex.test.js +0 -82
  374. package/tests/unit/utils/migrationUtils/validateAddressFromIncomingFile.test.js +0 -100
  375. package/tests/unit/utils/normalizers/normalizers.test.js +0 -469
  376. package/tests/unit/utils/protobuf/applyOperations.test.js +0 -91
  377. package/tests/unit/utils/protobuf/operationHelpers.test.js +0 -303
  378. package/tests/unit/utils/protobuf/protobuf.test.js +0 -12
  379. package/tests/unit/utils/type/type.test.js +0 -25
  380. package/tests/unit/utils/utils.test.js +0 -21
  381. package/whitelist/.gitkeep +0 -0
@@ -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
- });