trac-msb 0.2.18 → 0.2.20

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 +130 -34
  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,881 +0,0 @@
1
- import b4a from 'b4a';
2
- import adminEntryUtils from '../../../../../src/core/state/utils/adminEntry.js';
3
- import nodeEntryUtils, { setWritingKey } from '../../../../../src/core/state/utils/nodeEntry.js';
4
- import { EntryType } from '../../../../../src/utils/constants.js';
5
- import { decimalStringToBigInt, bigIntTo16ByteBuffer } from '../../../../../src/utils/amountSerialization.js';
6
- import { deriveIndexerSequenceState, eventFlush } from '../../../../helpers/autobaseTestHelpers.js';
7
- import { applyStateMessageFactory } from '../../../../../src/messages/state/applyStateMessageFactory.js';
8
- import { safeEncodeApplyOperation } from '../../../../../src/utils/protobuf/operationHelpers.js';
9
- import {
10
- setupAdminNetwork,
11
- initializeBalances,
12
- whitelistAddress
13
- } from '../common/commonScenarioHelper.js';
14
- import { promotePeerToWriter } from '../addWriter/addWriterScenarioHelpers.js';
15
- import { buildAddIndexerPayload } from '../addIndexer/addIndexerScenarioHelpers.js';
16
- import { toBalance, BALANCE_FEE } from '../../../../../src/core/state/utils/balance.js';
17
- import lengthEntryUtils from '../../../../../src/core/state/utils/lengthEntry.js';
18
- import { safeDecodeApplyOperation } from '../../../../../src/utils/protobuf/operationHelpers.js';
19
- import { config } from '../../../../helpers/config.js';
20
-
21
- export const DEFAULT_FUNDING = bigIntTo16ByteBuffer(decimalStringToBigInt('50'));
22
- export const TRANSFER_AMOUNT = bigIntTo16ByteBuffer(decimalStringToBigInt('1'));
23
- export const TRANSFER_COUNT = 20;
24
-
25
- export async function setupAdminRecoveryScenario(t) {
26
- const context = await setupAdminNetwork(t, { nodes: 5 });
27
- const [adminPeer, indexerPeer1, indexerPeer2, validatorPeer1, validatorPeer2] = context.peers;
28
-
29
- // Fund everyone (including admin) so fees/stakes succeed.
30
- await initializeBalances(context, [
31
- [adminPeer.wallet.address, DEFAULT_FUNDING],
32
- [indexerPeer1.wallet.address, DEFAULT_FUNDING],
33
- [indexerPeer2.wallet.address, DEFAULT_FUNDING],
34
- [validatorPeer1.wallet.address, DEFAULT_FUNDING],
35
- [validatorPeer2.wallet.address, DEFAULT_FUNDING]
36
- ]);
37
-
38
- context.addWriterScenario = { writerInitialBalance: DEFAULT_FUNDING };
39
-
40
- // Whitelist all non-admin peers.
41
- for (const peer of [indexerPeer1, indexerPeer2, validatorPeer1, validatorPeer2]) {
42
- await whitelistAddress(context, peer.wallet.address);
43
- }
44
-
45
- // Promote validators to writers.
46
- await promotePeerToWriter(t, context, {
47
- readerPeer: validatorPeer1,
48
- expectedWriterIndex: await currentWritersLength(adminPeer)
49
- });
50
- await promotePeerToWriter(t, context, {
51
- readerPeer: validatorPeer2,
52
- expectedWriterIndex: await currentWritersLength(adminPeer)
53
- });
54
-
55
- // Register validator writers as indexers.
56
- await addIndexer(context, validatorPeer1);
57
- await addIndexer(context, validatorPeer2);
58
-
59
- // Use an existing peer's writing key (currently not registered as a writer) for recovery.
60
- const newWriterKey = indexerPeer1.base.local.key;
61
-
62
- context.adminRecovery = {
63
- adminPeer,
64
- indexerPeer1,
65
- indexerPeer2,
66
- validatorPeer1,
67
- validatorPeer2,
68
- oldAdminWriterKey: adminPeer.base.local.key,
69
- newAdminWriterKey: newWriterKey
70
- };
71
-
72
- await context.sync();
73
- return context;
74
- }
75
-
76
- async function addIndexer(context, writerPeer) {
77
- const adminPeer = context.adminBootstrap;
78
- const payload = await buildAddIndexerPayload(context, { writerPeer, adminPeer });
79
- await adminPeer.base.append(payload);
80
- await adminPeer.base.update();
81
- await eventFlush();
82
- }
83
-
84
- async function currentWritersLength(adminPeer) {
85
- const writersLengthEntry = await adminPeer.base.view.get(EntryType.WRITERS_LENGTH);
86
- return writersLengthEntry ? lengthEntryUtils.decodeBE(writersLengthEntry.value) : 0;
87
- }
88
-
89
- export async function buildAdminRecoveryPayload(context) {
90
- const { adminPeer, validatorPeer1, newAdminWriterKey } = context.adminRecovery;
91
- const txValidity = await deriveIndexerSequenceState(validatorPeer1.base);
92
-
93
- const partial = await applyStateMessageFactory(adminPeer.wallet, config)
94
- .buildPartialAdminRecoveryMessage(
95
- adminPeer.wallet.address,
96
- b4a.toString(newAdminWriterKey, 'hex'),
97
- b4a.toString(txValidity, 'hex'),
98
- 'json'
99
- );
100
-
101
- const payload = await applyStateMessageFactory(validatorPeer1.wallet, config)
102
- .buildCompleteAdminRecoveryMessage(
103
- partial.address,
104
- b4a.from(partial.rao.tx, 'hex'),
105
- b4a.from(partial.rao.txv, 'hex'),
106
- b4a.from(partial.rao.iw, 'hex'),
107
- b4a.from(partial.rao.in, 'hex'),
108
- b4a.from(partial.rao.is, 'hex')
109
- );
110
- return safeEncodeApplyOperation(payload);
111
- }
112
-
113
- export async function buildAdminRecoveryPayloadWithTxValidity(context, mutatedTxValidity) {
114
- if (!b4a.isBuffer(mutatedTxValidity)) {
115
- throw new Error('buildAdminRecoveryPayloadWithTxValidity requires a tx validity buffer.');
116
- }
117
-
118
- const { adminPeer, validatorPeer1, newAdminWriterKey } = context.adminRecovery;
119
- const partial = await applyStateMessageFactory(adminPeer.wallet, config)
120
- .buildPartialAdminRecoveryMessage(
121
- adminPeer.wallet.address,
122
- b4a.toString(newAdminWriterKey, 'hex'),
123
- b4a.toString(mutatedTxValidity, 'hex'),
124
- 'json'
125
- );
126
-
127
- const payload = await applyStateMessageFactory(validatorPeer1.wallet, config)
128
- .buildCompleteAdminRecoveryMessage(
129
- partial.address,
130
- b4a.from(partial.rao.tx, 'hex'),
131
- mutatedTxValidity,
132
- b4a.from(partial.rao.iw, 'hex'),
133
- b4a.from(partial.rao.in, 'hex'),
134
- b4a.from(partial.rao.is, 'hex')
135
- );
136
- return safeEncodeApplyOperation(payload);
137
- }
138
-
139
- export async function applyAdminRecovery(context, payload) {
140
- const { validatorPeer1 } = context.adminRecovery;
141
- await validatorPeer1.base.append(payload);
142
- await validatorPeer1.base.update();
143
- await eventFlush();
144
- }
145
-
146
- export async function applyAdminRecoveryViaValidator(context, payload) {
147
- const { validatorPeer1 } = context.adminRecovery;
148
- await validatorPeer1.base.append(payload);
149
- await validatorPeer1.base.update();
150
- await eventFlush();
151
- }
152
-
153
- export async function applyWithAdminEncodeFailure(context, payload) {
154
- const originalEncode = adminEntryUtils.encode;
155
- adminEntryUtils.encode = () => b4a.alloc(0);
156
- try {
157
- await applyAdminRecoveryViaValidator(context, payload);
158
- } finally {
159
- adminEntryUtils.encode = originalEncode;
160
- }
161
- }
162
-
163
- export async function applyWithAdminBalanceDecodeFailure(context, payload) {
164
- const originalDecode = nodeEntryUtils.decode;
165
- let shouldMutateBalance = false;
166
-
167
- nodeEntryUtils.decode = function patchedDecode(buffer) {
168
- const decoded = originalDecode(buffer);
169
- if (shouldMutateBalance && decoded) {
170
- shouldMutateBalance = false;
171
- return { ...decoded, balance: b4a.alloc(1) };
172
- }
173
- return decoded;
174
- };
175
-
176
- try {
177
- await applyWithAdminNodeEntryMutation(context, payload, entry => {
178
- if (!entry?.value) return entry;
179
- shouldMutateBalance = true;
180
- return entry;
181
- });
182
- } finally {
183
- nodeEntryUtils.decode = originalDecode;
184
- }
185
- }
186
-
187
- export async function applyWithAdminInsufficientBalance(context, payload) {
188
- await applyWithAdminNodeEntryMutation(context, payload, entry => {
189
- if (!entry?.value) return entry;
190
- const mutated = b4a.from(entry.value);
191
- const updated = nodeEntryUtils.setBalance(mutated, b4a.alloc(16, 0x00));
192
- return updated ? { ...entry, value: updated } : entry;
193
- });
194
- }
195
-
196
- export async function applyWithAdminFeeSubtractionFailure(context, payload) {
197
- const balanceUtils = await import('../../../../../src/core/state/utils/balance.js');
198
- const sample = balanceUtils.toBalance(b4a.alloc(16));
199
- const prototype = sample ? Object.getPrototypeOf(sample) : null;
200
- const originalSub = prototype?.sub;
201
-
202
- if (!prototype || typeof originalSub !== 'function') {
203
- throw new Error('Failed to patch balance subtraction for admin fee scenario.');
204
- }
205
-
206
- prototype.sub = function patchedSub() {
207
- prototype.sub = originalSub;
208
- return null;
209
- };
210
-
211
- try {
212
- await applyAdminRecoveryViaValidator(context, payload);
213
- } finally {
214
- prototype.sub = originalSub;
215
- }
216
- }
217
-
218
- export async function applyWithValidatorBalanceUpdateFailure(context, payload) {
219
- const balanceUtils = await import('../../../../../src/core/state/utils/balance.js');
220
- const sample = balanceUtils.toBalance(b4a.alloc(16));
221
- const prototype = sample ? Object.getPrototypeOf(sample) : null;
222
- const originalUpdate = prototype?.update;
223
- if (!prototype || typeof originalUpdate !== 'function') {
224
- throw new Error('Failed to patch balance update for validator fee.');
225
- }
226
-
227
- let targetBuffer = null;
228
- prototype.update = function patchedUpdate(buffer) {
229
- if (targetBuffer && buffer && b4a.isBuffer(buffer) && b4a.equals(buffer, targetBuffer)) {
230
- prototype.update = originalUpdate;
231
- return null;
232
- }
233
- return originalUpdate.call(this, buffer);
234
- };
235
-
236
- const { validatorPeer1 } = context.adminRecovery;
237
- const originalApply = validatorPeer1.base._handlers.apply;
238
- validatorPeer1.base._handlers.apply = async function patchedApply(nodes, view, baseCtx) {
239
- const originalBatch = view.batch;
240
- view.batch = function patchedBatch(...args) {
241
- const batch = originalBatch.apply(this, args);
242
- const originalGet = batch.get?.bind(batch);
243
- if (typeof originalGet === 'function') {
244
- batch.get = async key => {
245
- if (key === validatorPeer1.wallet.address) {
246
- const entry = await originalGet(key);
247
- targetBuffer = entry?.value ?? null;
248
- return entry;
249
- }
250
- return originalGet(key);
251
- };
252
- }
253
- return batch;
254
- };
255
- try {
256
- return await originalApply.call(this, nodes, view, baseCtx);
257
- } finally {
258
- view.batch = originalBatch;
259
- }
260
- };
261
-
262
- try {
263
- await applyAdminRecoveryViaValidator(context, payload);
264
- } finally {
265
- prototype.update = originalUpdate;
266
- validatorPeer1.base._handlers.apply = originalApply;
267
- }
268
- }
269
-
270
- export async function applyWithValidatorNodeDecodeFailure(context, payload) {
271
- const originalDecode = nodeEntryUtils.decode;
272
- let validatorKey = null;
273
- let seenOnce = false;
274
-
275
- nodeEntryUtils.decode = function patchedDecode(buffer) {
276
- if (validatorKey && b4a.isBuffer(buffer) && b4a.equals(buffer, validatorKey)) {
277
- if (seenOnce) return null;
278
- seenOnce = true;
279
- }
280
- return originalDecode(buffer);
281
- };
282
-
283
- const { validatorPeer1 } = context.adminRecovery;
284
- const originalApply = validatorPeer1.base._handlers.apply;
285
- validatorPeer1.base._handlers.apply = async function patchedApply(nodes, view, baseCtx) {
286
- const originalBatch = view.batch;
287
- view.batch = function patchedBatch(...args) {
288
- const batch = originalBatch.apply(this, args);
289
- const originalGet = batch.get?.bind(batch);
290
- if (typeof originalGet === 'function') {
291
- batch.get = async key => {
292
- if (key === validatorPeer1.wallet.address) {
293
- const entry = await originalGet(key);
294
- validatorKey = entry?.value ?? null;
295
- return entry;
296
- }
297
- return originalGet(key);
298
- };
299
- }
300
- return batch;
301
- };
302
- try {
303
- return await originalApply.call(this, nodes, view, baseCtx);
304
- } finally {
305
- view.batch = originalBatch;
306
- }
307
- };
308
-
309
- try {
310
- await applyAdminRecoveryViaValidator(context, payload);
311
- } finally {
312
- nodeEntryUtils.decode = originalDecode;
313
- validatorPeer1.base._handlers.apply = originalApply;
314
- }
315
- }
316
-
317
- export async function applyWithValidatorBalanceDecodeFailure(context, payload) {
318
- const originalDecode = nodeEntryUtils.decode;
319
- let validatorKey = null;
320
- let seenOnce = false;
321
-
322
- nodeEntryUtils.decode = function patchedDecode(buffer) {
323
- const decoded = originalDecode(buffer);
324
- if (validatorKey && b4a.isBuffer(buffer) && b4a.equals(buffer, validatorKey)) {
325
- if (seenOnce) {
326
- return decoded ? { ...decoded, balance: b4a.alloc(1) } : decoded;
327
- }
328
- seenOnce = true;
329
- return decoded ? { ...decoded, balance: b4a.alloc(1) } : decoded;
330
- }
331
- return decoded;
332
- };
333
-
334
- const { validatorPeer1 } = context.adminRecovery;
335
- const originalApply = validatorPeer1.base._handlers.apply;
336
- validatorPeer1.base._handlers.apply = async function patchedApply(nodes, view, baseCtx) {
337
- const originalBatch = view.batch;
338
- view.batch = function patchedBatch(...args) {
339
- const batch = originalBatch.apply(this, args);
340
- const originalGet = batch.get?.bind(batch);
341
- if (typeof originalGet === 'function') {
342
- batch.get = async key => {
343
- if (key === validatorPeer1.wallet.address) {
344
- const entry = await originalGet(key);
345
- validatorKey = entry?.value ?? null;
346
- return entry;
347
- }
348
- return originalGet(key);
349
- };
350
- }
351
- return batch;
352
- };
353
- try {
354
- return await originalApply.call(this, nodes, view, baseCtx);
355
- } finally {
356
- view.batch = originalBatch;
357
- }
358
- };
359
-
360
- try {
361
- await applyAdminRecoveryViaValidator(context, payload);
362
- } finally {
363
- nodeEntryUtils.decode = originalDecode;
364
- validatorPeer1.base._handlers.apply = originalApply;
365
- }
366
- }
367
-
368
- export async function applyWithAdminNodeDecodeFailure(context, payload) {
369
- const originalDecode = nodeEntryUtils.decode;
370
- const adminNodeEntry = await context.adminRecovery.adminPeer.base.view.get(
371
- context.adminRecovery.adminPeer.wallet.address
372
- );
373
- const expectedBuffer =
374
- adminNodeEntry?.value && context.adminRecovery.newAdminWriterKey
375
- ? setWritingKey(adminNodeEntry.value, context.adminRecovery.newAdminWriterKey)
376
- : null;
377
-
378
- let triggered = false;
379
-
380
- nodeEntryUtils.decode = function patchedDecode(buffer) {
381
- if (
382
- !triggered &&
383
- expectedBuffer &&
384
- buffer &&
385
- b4a.isBuffer(buffer) &&
386
- b4a.equals(buffer, expectedBuffer)
387
- ) {
388
- triggered = true;
389
- return null;
390
- }
391
- return originalDecode(buffer);
392
- };
393
-
394
- try {
395
- await applyAdminRecoveryViaValidator(context, payload);
396
- } finally {
397
- nodeEntryUtils.decode = originalDecode;
398
- }
399
- }
400
-
401
- export async function applyWithValidatorFeeTransferFailure(context, payload) {
402
- const balanceUtils = await import('../../../../../src/core/state/utils/balance.js');
403
- const sample = balanceUtils.toBalance(b4a.alloc(16));
404
- const prototype = sample ? Object.getPrototypeOf(sample) : null;
405
- const originalAdd = prototype?.add;
406
- if (!prototype || typeof originalAdd !== 'function') {
407
- throw new Error('Failed to patch balance add for validator fee transfer.');
408
- }
409
-
410
- prototype.add = function patchedAdd() {
411
- prototype.add = originalAdd;
412
- return null;
413
- };
414
-
415
- try {
416
- await applyAdminRecoveryViaValidator(context, payload);
417
- } finally {
418
- prototype.add = originalAdd;
419
- }
420
- }
421
-
422
- export async function applyWithDuplicateOperation(context, payload) {
423
- const decoded = safeDecodeApplyOperation(payload);
424
- const txHashHexString = decoded?.rao?.tx ? b4a.toString(decoded.rao.tx, 'hex') : null;
425
- const { validatorPeer1 } = context.adminRecovery;
426
- const base = validatorPeer1.base;
427
- const originalApply = base._handlers.apply;
428
-
429
- base._handlers.apply = async function patchedApply(nodes, view, baseCtx) {
430
- const originalBatch = view.batch;
431
- view.batch = function patchedBatch(...args) {
432
- const batch = originalBatch.apply(this, args);
433
- if (!batch?.get) return batch;
434
-
435
- const originalGet = batch.get.bind(batch);
436
- batch.get = async key => {
437
- if (txHashHexString && key === txHashHexString) {
438
- return { value: b4a.from('applied') };
439
- }
440
- return originalGet(key);
441
- };
442
- return batch;
443
- };
444
-
445
- try {
446
- return await originalApply.call(this, nodes, view, baseCtx);
447
- } finally {
448
- view.batch = originalBatch;
449
- }
450
- };
451
-
452
- try {
453
- await applyAdminRecoveryViaValidator(context, payload);
454
- } finally {
455
- base._handlers.apply = originalApply;
456
- }
457
- }
458
-
459
- export async function applyWithOldWriterKeyMissing(context, payload) {
460
- const base = context.adminRecovery.validatorPeer1.base;
461
- const backup = cloneIndexers(base.system.indexers);
462
- const filteredEntries = Object.values(cloneIndexers(base.system.indexers) || {}).filter(
463
- entry => !b4a.equals(entry.key, context.adminRecovery.oldAdminWriterKey)
464
- );
465
- base.system.indexers = Array.isArray(backup)
466
- ? filteredEntries
467
- : Object.fromEntries(filteredEntries.map((entry, idx) => [idx, entry]));
468
-
469
- try {
470
- const rebuiltPayload = await buildAdminRecoveryPayload(context);
471
- await applyAdminRecoveryViaValidator(context, rebuiltPayload);
472
- } finally {
473
- base.system.indexers = backup;
474
- }
475
- }
476
-
477
- export async function applyWithNewWriterKeyPresent(context, payload) {
478
- const base = context.adminRecovery.validatorPeer1.base;
479
- const backup = cloneIndexers(base.system.indexers);
480
- const entries = Object.values(cloneIndexers(base.system.indexers) || {});
481
- const templateEntry = entries[0] ?? {};
482
- entries.push({ ...templateEntry, key: b4a.from(context.adminRecovery.newAdminWriterKey) });
483
- base.system.indexers = Array.isArray(backup)
484
- ? entries
485
- : Object.fromEntries(entries.map((entry, idx) => [idx, entry]));
486
-
487
- try {
488
- const rebuiltPayload = await buildAdminRecoveryPayload(context);
489
- await applyAdminRecoveryViaValidator(context, rebuiltPayload);
490
- } finally {
491
- base.system.indexers = backup;
492
- }
493
- }
494
-
495
- export function adminRecoveryScenarioDefaults({
496
- assertStateUnchanged = (t, context) => assertAdminRecoveryFailureState(t, context)
497
- } = {}) {
498
- return {
499
- setupScenario: setupAdminRecoveryScenario,
500
- buildValidPayload: buildAdminRecoveryPayload,
501
- assertStateUnchanged,
502
- applyInvalidPayload: async (context, payload) => applyAdminRecoveryViaValidator(context, payload)
503
- };
504
- }
505
-
506
- export async function applyTransferSeries(context, count = TRANSFER_COUNT) {
507
- const { indexerPeer1, indexerPeer2, validatorPeer2 } = context.adminRecovery;
508
-
509
- for (let i = 0; i < count; i++) {
510
- const transferPayload = await buildSimpleTransferPayload({
511
- requesterPeer: indexerPeer2,
512
- validatorPeer: validatorPeer2,
513
- recipientPeer: indexerPeer1,
514
- amount: TRANSFER_AMOUNT
515
- });
516
-
517
- await validatorPeer2.base.append(transferPayload);
518
- await validatorPeer2.base.update();
519
- await eventFlush();
520
- }
521
- }
522
-
523
- async function buildSimpleTransferPayload({ requesterPeer, validatorPeer, recipientPeer, amount }) {
524
- const txValidity = await deriveIndexerSequenceState(validatorPeer.base);
525
- const partial = await applyStateMessageFactory(requesterPeer.wallet, config)
526
- .buildPartialTransferOperationMessage(
527
- requesterPeer.wallet.address,
528
- recipientPeer.wallet.address,
529
- b4a.toString(amount, 'hex'),
530
- b4a.toString(txValidity, 'hex'),
531
- 'json'
532
- );
533
-
534
- const payload = await applyStateMessageFactory(validatorPeer.wallet, config)
535
- .buildCompleteTransferOperationMessage(
536
- partial.address,
537
- b4a.from(partial.tro.tx, 'hex'),
538
- b4a.from(partial.tro.txv, 'hex'),
539
- b4a.from(partial.tro.in, 'hex'),
540
- partial.tro.to,
541
- b4a.from(partial.tro.am, 'hex'),
542
- b4a.from(partial.tro.is, 'hex')
543
- );
544
- return safeEncodeApplyOperation(payload);
545
- }
546
-
547
- export async function assertAdminRecoverySuccessState(t, context, { viewBase } = {}) {
548
- const {
549
- adminPeer,
550
- validatorPeer2,
551
- oldAdminWriterKey,
552
- newAdminWriterKey
553
- } = context.adminRecovery;
554
-
555
- const base = viewBase ?? validatorPeer2.base;
556
- const adminEntry = await base.view.get(EntryType.ADMIN);
557
- t.ok(adminEntry, 'admin entry exists');
558
-
559
- const decodedAdminEntry = adminEntryUtils.decode(adminEntry.value, config.addressPrefix);
560
- t.ok(decodedAdminEntry, 'admin entry decodes');
561
- t.ok(b4a.equals(decodedAdminEntry.wk, newAdminWriterKey), 'admin writer key updated');
562
-
563
- const adminNodeEntry = await base.view.get(adminPeer.wallet.address);
564
- const decodedNodeEntry = nodeEntryUtils.decode(adminNodeEntry?.value);
565
- t.ok(decodedNodeEntry, 'admin node entry decodes');
566
- t.ok(b4a.equals(decodedNodeEntry.wk, newAdminWriterKey), 'admin node entry writer key updated');
567
-
568
- const adminBalance = toBalance(decodedNodeEntry.balance);
569
- const initialAdminBalance = toBalance(DEFAULT_FUNDING);
570
- const feeBalance = toBalance(BALANCE_FEE);
571
- if (adminBalance && initialAdminBalance && feeBalance) {
572
- const expectedBalance = initialAdminBalance
573
- .sub(feeBalance)
574
- ?.sub(feeBalance)
575
- ?.sub(feeBalance);
576
- if (expectedBalance) {
577
- t.ok(
578
- b4a.equals(adminBalance.value, expectedBalance.value),
579
- 'admin balance reduced by accumulated fees'
580
- );
581
- }
582
- }
583
-
584
- const writerAddressEntry = await base.view.get(
585
- EntryType.WRITER_ADDRESS + newAdminWriterKey.toString('hex')
586
- );
587
- t.ok(writerAddressEntry, 'writer address mapping exists for new admin key');
588
-
589
- const indexerKeys = Object.values(base.system.indexers || {}).map(entry =>
590
- b4a.toString(entry.key, 'hex')
591
- );
592
- const oldKeyHex = b4a.toString(oldAdminWriterKey, 'hex');
593
- const newKeyHex = b4a.toString(newAdminWriterKey, 'hex');
594
- t.ok(indexerKeys.includes(newKeyHex), 'new admin writer key present in indexers');
595
- t.ok(!indexerKeys.includes(oldKeyHex), 'bootstrap writer key removed from indexers');
596
- }
597
-
598
- export async function assertAdminRecoveryFailureState(t, context, { skipSync } = {}) {
599
- const { adminPeer, oldAdminWriterKey, newAdminWriterKey } = context.adminRecovery;
600
-
601
- if (!skipSync && typeof context.sync === 'function') {
602
- await context.sync();
603
- }
604
-
605
- const adminEntry = await adminPeer.base.view.get(EntryType.ADMIN);
606
- t.ok(adminEntry, 'admin entry persists');
607
-
608
- const decodedAdminEntry = adminEntryUtils.decode(adminEntry.value, config.addressPrefix);
609
- t.ok(decodedAdminEntry, 'admin entry decodes');
610
- t.ok(b4a.equals(decodedAdminEntry.wk, oldAdminWriterKey), 'admin writer key remains unchanged');
611
-
612
- const adminNodeEntry = await adminPeer.base.view.get(adminPeer.wallet.address);
613
- const decodedNodeEntry = nodeEntryUtils.decode(adminNodeEntry?.value);
614
- t.ok(decodedNodeEntry, 'admin node entry decodes');
615
- t.ok(b4a.equals(decodedNodeEntry.wk, oldAdminWriterKey), 'admin node entry writer key unchanged');
616
-
617
- const writerRegistryEntry = await adminPeer.base.view.get(
618
- EntryType.WRITER_ADDRESS + newAdminWriterKey.toString('hex')
619
- );
620
- t.ok(!writerRegistryEntry, 'new admin writer key not registered');
621
-
622
- const indexerKeys = Object.values(adminPeer.base.system.indexers || {}).map(entry =>
623
- b4a.toString(entry.key, 'hex')
624
- );
625
- const oldKeyHex = b4a.toString(oldAdminWriterKey, 'hex');
626
- const newKeyHex = b4a.toString(newAdminWriterKey, 'hex');
627
- t.ok(indexerKeys.includes(oldKeyHex), 'old admin writer key still in indexers');
628
- t.ok(!indexerKeys.includes(newKeyHex), 'new admin writer key not in indexers');
629
- }
630
-
631
- export async function applyWithRoleAccessBypass(context, invalidPayload) {
632
- const { validatorPeer1 } = context.adminRecovery;
633
- const state = validatorPeer1.state;
634
- const originalValidate = state.check.validateRoleAccessOperation;
635
- state.check.validateRoleAccessOperation = () => true;
636
- try {
637
- await validatorPeer1.base.append(invalidPayload);
638
- await validatorPeer1.base.update();
639
- await eventFlush();
640
- } finally {
641
- state.check.validateRoleAccessOperation = originalValidate;
642
- }
643
- }
644
-
645
- export async function applyWithMissingComponentBypass(context, invalidPayload, { missingKey = 'vs' } = {}) {
646
- const { validatorPeer1 } = context.adminRecovery;
647
- const state = validatorPeer1.state;
648
- const originalValidate = state.check.validateRoleAccessOperation;
649
- const originalHasOwn = Object.hasOwn;
650
- Object.hasOwn = (obj, prop) => {
651
- if (prop === missingKey) return false;
652
- return originalHasOwn(obj, prop);
653
- };
654
- state.check.validateRoleAccessOperation = () => true;
655
- try {
656
- await validatorPeer1.base.append(invalidPayload);
657
- await validatorPeer1.base.update();
658
- await eventFlush();
659
- } finally {
660
- state.check.validateRoleAccessOperation = originalValidate;
661
- Object.hasOwn = originalHasOwn;
662
- }
663
- }
664
-
665
- export async function applyWithInvalidRequesterMessage(context, payload) {
666
- const originalConcat = b4a.concat;
667
- b4a.concat = (...args) => {
668
- const stack = new Error().stack || '';
669
- if (stack.includes('utils/buffer.js') && stack.includes('createMessage')) {
670
- b4a.concat = originalConcat;
671
- return b4a.alloc(0);
672
- }
673
- return originalConcat(...args);
674
- };
675
-
676
- try {
677
- await applyAdminRecoveryViaValidator(context, payload);
678
- } finally {
679
- b4a.concat = originalConcat;
680
- }
681
- }
682
-
683
- export async function applyWithInvalidValidatorMessage(context, payload) {
684
- const originalConcat = b4a.concat;
685
- let createMessageCalls = 0;
686
- b4a.concat = (...args) => {
687
- const stack = new Error().stack || '';
688
- if (stack.includes('utils/buffer.js') && stack.includes('createMessage')) {
689
- createMessageCalls += 1;
690
- if (createMessageCalls === 2) {
691
- b4a.concat = originalConcat;
692
- return b4a.alloc(0);
693
- }
694
- }
695
- return originalConcat(...args);
696
- };
697
-
698
- try {
699
- await applyAdminRecoveryViaValidator(context, payload);
700
- } finally {
701
- b4a.concat = originalConcat;
702
- }
703
- }
704
-
705
- export async function applyWithRegisteredWriterKey(context, payload) {
706
- const decoded = safeDecodeApplyOperation(payload);
707
- const writerKeyHex = decoded?.rao?.iw ? b4a.toString(decoded.rao.iw, 'hex') : null;
708
- const { validatorPeer1 } = context.adminRecovery;
709
- const base = validatorPeer1.base;
710
- const originalApply = base._handlers.apply;
711
-
712
- try {
713
- base._handlers.apply = async function patchedApply(nodes, view, baseCtx) {
714
- const originalBatch = view.batch;
715
- view.batch = function patchedBatch(...args) {
716
- const batch = originalBatch.apply(this, args);
717
- if (!batch?.get) return batch;
718
-
719
- const originalGet = batch.get.bind(batch);
720
- batch.get = async key => {
721
- if (writerKeyHex && key === EntryType.WRITER_ADDRESS + writerKeyHex) {
722
- return { value: b4a.from('registered') };
723
- }
724
- return originalGet(key);
725
- };
726
- return batch;
727
- };
728
-
729
- try {
730
- return await originalApply.call(this, nodes, view, baseCtx);
731
- } finally {
732
- view.batch = originalBatch;
733
- }
734
- };
735
-
736
- await applyAdminRecoveryViaValidator(context, payload);
737
- } finally {
738
- base._handlers.apply = originalApply;
739
- }
740
- }
741
-
742
- export async function applyWithIndexerSequenceFailure(context, payload) {
743
- const { validatorPeer1 } = context.adminRecovery;
744
- const system = validatorPeer1.base.system;
745
- const originalDescriptor = Object.getOwnPropertyDescriptor(system, 'indexers');
746
- const originalValue = system.indexers;
747
- let injected = false;
748
-
749
- Object.defineProperty(system, 'indexers', {
750
- configurable: true,
751
- enumerable: true,
752
- get() {
753
- if (!injected) {
754
- injected = true;
755
- throw new Error('forced indexer sequence failure');
756
- }
757
- return originalValue;
758
- },
759
- set(value) {
760
- return Reflect.set(system, 'indexers', value);
761
- }
762
- });
763
-
764
- try {
765
- await validatorPeer1.base.append(payload).catch(() => {});
766
- await validatorPeer1.base.update().catch(() => {});
767
- await eventFlush();
768
- } finally {
769
- if (originalDescriptor) {
770
- Object.defineProperty(system, 'indexers', originalDescriptor);
771
- } else {
772
- system.indexers = originalValue;
773
- }
774
- }
775
- }
776
-
777
- export async function applyWithIndexerSequenceCorruption(context, payload) {
778
- const { PeerWallet } = await import('trac-wallet');
779
- const originalHash = PeerWallet.blake3;
780
- const originalHashSafe = PeerWallet.blake3Safe;
781
-
782
- PeerWallet.blake3 = async () => {
783
- throw new Error('forced indexer sequence state failure');
784
- };
785
- PeerWallet.blake3Safe = async () => {
786
- return b4a.alloc(0);
787
- }
788
-
789
- try {
790
- await applyAdminRecoveryViaValidator(context, payload);
791
- } finally {
792
- PeerWallet.blake3 = originalHash;
793
- PeerWallet.blake3Safe = originalHashSafe;
794
- }
795
- }
796
-
797
- export async function applyWithAdminEntryMutation(context, payload, mutateEntry) {
798
- const { validatorPeer1 } = context.adminRecovery;
799
- const base = validatorPeer1.base;
800
- const originalApply = base._handlers.apply;
801
-
802
- base._handlers.apply = async function patchedApply(nodes, view, baseCtx) {
803
- const originalBatch = view.batch;
804
- view.batch = function patchedBatch(...args) {
805
- const batch = originalBatch.apply(this, args);
806
- if (!batch?.get) return batch;
807
-
808
- const originalGet = batch.get.bind(batch);
809
- batch.get = async key => {
810
- if (key === EntryType.ADMIN) {
811
- const entry = await originalGet(key);
812
- return mutateEntry(entry);
813
- }
814
- return originalGet(key);
815
- };
816
- return batch;
817
- };
818
-
819
- try {
820
- return await originalApply.call(this, nodes, view, baseCtx);
821
- } finally {
822
- view.batch = originalBatch;
823
- }
824
- };
825
-
826
- try {
827
- await applyAdminRecoveryViaValidator(context, payload);
828
- } finally {
829
- base._handlers.apply = originalApply;
830
- }
831
- }
832
-
833
- export async function applyWithAdminNodeEntryMutation(context, payload, mutateEntry) {
834
- const { validatorPeer1, adminPeer } = context.adminRecovery;
835
- const targetKey = adminPeer.wallet.address;
836
- const base = validatorPeer1.base;
837
- const originalApply = base._handlers.apply;
838
-
839
- base._handlers.apply = async function patchedApply(nodes, view, baseCtx) {
840
- const originalBatch = view.batch;
841
- view.batch = function patchedBatch(...args) {
842
- const batch = originalBatch.apply(this, args);
843
- if (!batch?.get) return batch;
844
-
845
- const originalGet = batch.get.bind(batch);
846
- batch.get = async key => {
847
- if (key === targetKey) {
848
- const entry = await originalGet(key);
849
- return mutateEntry(entry);
850
- }
851
- return originalGet(key);
852
- };
853
- return batch;
854
- };
855
-
856
- try {
857
- return await originalApply.call(this, nodes, view, baseCtx);
858
- } finally {
859
- view.batch = originalBatch;
860
- }
861
- };
862
-
863
- try {
864
- await applyAdminRecoveryViaValidator(context, payload);
865
- } finally {
866
- base._handlers.apply = originalApply;
867
- }
868
- }
869
-
870
- export function cloneIndexers(indexers) {
871
- if (Array.isArray(indexers)) {
872
- return indexers.map(entry => ({ ...entry, key: entry?.key ? b4a.from(entry.key) : entry.key }));
873
- }
874
-
875
- return Object.fromEntries(
876
- Object.entries(indexers || {}).map(([k, v]) => [
877
- k,
878
- { ...v, key: v?.key ? b4a.from(v.key) : v.key }
879
- ])
880
- );
881
- }