trac-msb 0.2.4 → 0.2.6

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 (239) hide show
  1. package/.dockerignore +16 -0
  2. package/.github/workflows/acceptance-tests.yml +7 -0
  3. package/.github/workflows/publish.yml +40 -0
  4. package/.github/workflows/{CI.yml → unit-tests.yml} +1 -1
  5. package/README.md +175 -50
  6. package/docker-compose.yml +16 -0
  7. package/dockerfile +41 -0
  8. package/docs/fee_distribution.md +89 -0
  9. package/msb.mjs +12 -14
  10. package/package.json +8 -4
  11. package/rpc/constants.mjs +4 -1
  12. package/rpc/handlers.mjs +109 -66
  13. package/rpc/routes/v1.mjs +3 -1
  14. package/rpc/rpc_services.js +126 -0
  15. package/rpc/utils/confirmedParameter.mjs +17 -0
  16. package/rpc/utils/url.mjs +38 -0
  17. package/src/core/network/Network.js +27 -10
  18. package/src/core/network/identity/NetworkWalletFactory.js +78 -0
  19. package/src/core/network/services/ConnectionManager.js +2 -2
  20. package/src/core/network/services/ValidatorObserverService.js +7 -4
  21. package/src/core/state/State.js +28 -22
  22. package/src/index.js +197 -385
  23. package/src/utils/cliCommands.js +280 -0
  24. package/src/utils/constants.js +3 -1
  25. package/tests/acceptance/v1/account/account.test.mjs +123 -0
  26. package/tests/acceptance/v1/balance/balance.test.mjs +55 -0
  27. package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +111 -0
  28. package/tests/acceptance/v1/confirmed-length/confirmed-length.test.mjs +19 -0
  29. package/tests/acceptance/v1/fee/fee.test.mjs +11 -0
  30. package/tests/acceptance/v1/rpc.test.mjs +62 -291
  31. package/tests/acceptance/v1/tx/tx.test.mjs +98 -0
  32. package/tests/acceptance/v1/tx-details/tx-details.test.mjs +195 -0
  33. package/tests/acceptance/v1/tx-hashes/tx-hashes.test.mjs +72 -0
  34. package/tests/acceptance/v1/tx-payloads-bulk/tx-payloads-bulk.test.mjs +27 -0
  35. package/tests/acceptance/v1/txv/txv.test.mjs +11 -0
  36. package/tests/acceptance/v1/unconfirmed-length/unconfirmed-length.test.mjs +11 -0
  37. package/tests/helpers/StateNetworkFactory.js +157 -0
  38. package/tests/helpers/autobaseTestHelpers.js +369 -0
  39. package/tests/helpers/createTestSignature.js +12 -0
  40. package/tests/helpers/transactionPayloads.mjs +78 -0
  41. package/tests/unit/network/NetworkWalletFactory.test.js +156 -0
  42. package/tests/unit/state/apply/addAdmin/addAdminHappyPathScenario.js +38 -0
  43. package/tests/unit/state/apply/addAdmin/addAdminScenarioHelpers.js +273 -0
  44. package/tests/unit/state/apply/addAdmin/adminEntryEncodingFailureScenario.js +30 -0
  45. package/tests/unit/state/apply/addAdmin/adminEntryExistsScenario.js +78 -0
  46. package/tests/unit/state/apply/addAdmin/nodeEntryInitializationFailureScenario.js +30 -0
  47. package/tests/unit/state/apply/addAdmin/nonBootstrapNodeScenario.js +68 -0
  48. package/tests/unit/state/apply/addAdmin/state.apply.addAdmin.test.js +155 -0
  49. package/tests/unit/state/apply/addIndexer/addIndexerHappyPathScenario.js +39 -0
  50. package/tests/unit/state/apply/addIndexer/addIndexerMultipleIndexersInTheNetworkScenario.js +167 -0
  51. package/tests/unit/state/apply/addIndexer/addIndexerPretenderAlreadyIndexerScenario.js +21 -0
  52. package/tests/unit/state/apply/addIndexer/addIndexerPretenderNotWriterScenario.js +21 -0
  53. package/tests/unit/state/apply/addIndexer/addIndexerRemoveAndReAddScenario.js +186 -0
  54. package/tests/unit/state/apply/addIndexer/addIndexerScenarioHelpers.js +445 -0
  55. package/tests/unit/state/apply/addIndexer/addIndexerWriterKeyAlreadyRegisteredScenario.js +32 -0
  56. package/tests/unit/state/apply/addIndexer/state.apply.addIndexer.test.js +297 -0
  57. package/tests/unit/state/apply/addWriter/addWriterHappyPathScenario.js +41 -0
  58. package/tests/unit/state/apply/addWriter/addWriterInvalidValidatorSignatureScenario.js +32 -0
  59. package/tests/unit/state/apply/addWriter/addWriterNewWkScenario.js +149 -0
  60. package/tests/unit/state/apply/addWriter/addWriterRequesterAlreadyWriterScenario.js +21 -0
  61. package/tests/unit/state/apply/addWriter/addWriterRequesterBalanceInsufficientScenario.js +21 -0
  62. package/tests/unit/state/apply/addWriter/addWriterRequesterEntryDecodeFailureScenario.js +19 -0
  63. package/tests/unit/state/apply/addWriter/addWriterRequesterEntryMissingScenario.js +19 -0
  64. package/tests/unit/state/apply/addWriter/addWriterRequesterIndexerScenario.js +21 -0
  65. package/tests/unit/state/apply/addWriter/addWriterRequesterNotWhitelistedScenario.js +21 -0
  66. package/tests/unit/state/apply/addWriter/addWriterScenarioHelpers.js +757 -0
  67. package/tests/unit/state/apply/addWriter/addWriterStakeBalanceUpdateFailureScenario.js +50 -0
  68. package/tests/unit/state/apply/addWriter/addWriterStakeInsufficientBalanceScenario.js +29 -0
  69. package/tests/unit/state/apply/addWriter/addWriterStakeInvalidBalanceScenario.js +29 -0
  70. package/tests/unit/state/apply/addWriter/addWriterStakeInvalidEntryScenario.js +21 -0
  71. package/tests/unit/state/apply/addWriter/addWriterStakeStakedBalanceFailureScenario.js +37 -0
  72. package/tests/unit/state/apply/addWriter/addWriterStakeSubtractFailureScenario.js +42 -0
  73. package/tests/unit/state/apply/addWriter/addWriterValidatorRewardScenario.js +105 -0
  74. package/tests/unit/state/apply/addWriter/addWriterWriterKeyMismatchScenario.js +54 -0
  75. package/tests/unit/state/apply/addWriter/addWriterWriterKeyOwnershipScenario.js +54 -0
  76. package/tests/unit/state/apply/addWriter/addWriterZeroWriterKeyScenario.js +29 -0
  77. package/tests/unit/state/apply/addWriter/state.apply.addWriter.test.js +309 -0
  78. package/tests/unit/state/apply/adminRecovery/adminRecoveryHappyPathScenario.js +30 -0
  79. package/tests/unit/state/apply/adminRecovery/adminRecoveryScenarioHelpers.js +866 -0
  80. package/tests/unit/state/apply/adminRecovery/state.apply.adminRecovery.test.js +439 -0
  81. package/tests/unit/state/apply/appendWhitelist/appendWhitelistBanAndReapplyScenario.js +78 -0
  82. package/tests/unit/state/apply/appendWhitelist/appendWhitelistExistingReaderHappyPathScenario.js +98 -0
  83. package/tests/unit/state/apply/appendWhitelist/appendWhitelistFeeAfterDisableScenario.js +66 -0
  84. package/tests/unit/state/apply/appendWhitelist/appendWhitelistHappyPathScenario.js +55 -0
  85. package/tests/unit/state/apply/appendWhitelist/appendWhitelistInsufficientAdminBalanceScenario.js +103 -0
  86. package/tests/unit/state/apply/appendWhitelist/appendWhitelistNodeAlreadyWhitelistedScenario.js +60 -0
  87. package/tests/unit/state/apply/appendWhitelist/appendWhitelistScenarioHelpers.js +191 -0
  88. package/tests/unit/state/apply/appendWhitelist/state.apply.appendWhitelist.test.js +220 -0
  89. package/tests/unit/state/apply/balanceInitialization/balanceInitializationHappyPathScenario.js +82 -0
  90. package/tests/unit/state/apply/balanceInitialization/balanceInitializationScenarioHelpers.js +106 -0
  91. package/tests/unit/state/apply/balanceInitialization/invalidAmountScenario.js +45 -0
  92. package/tests/unit/state/apply/balanceInitialization/nodeEntryBalanceUpdateFailureScenario.js +81 -0
  93. package/tests/unit/state/apply/balanceInitialization/state.apply.balanceInitialization.test.js +189 -0
  94. package/tests/unit/state/apply/banValidator/banValidatorBanAndReWhitelistScenario.js +155 -0
  95. package/tests/unit/state/apply/banValidator/banValidatorHappyPathScenario.js +36 -0
  96. package/tests/unit/state/apply/banValidator/banValidatorScenarioHelpers.js +534 -0
  97. package/tests/unit/state/apply/banValidator/banValidatorSequentialBansScenario.js +74 -0
  98. package/tests/unit/state/apply/banValidator/banValidatorTargetDecodeFailureScenario.js +19 -0
  99. package/tests/unit/state/apply/banValidator/banValidatorTargetIndexerScenario.js +32 -0
  100. package/tests/unit/state/apply/banValidator/banValidatorTargetNodeEntryMissingScenario.js +19 -0
  101. package/tests/unit/state/apply/banValidator/banValidatorTargetRoleUpdateFailureScenario.js +19 -0
  102. package/tests/unit/state/apply/banValidator/banValidatorWhitelistedNonWriterScenario.js +38 -0
  103. package/tests/unit/state/apply/banValidator/banValidatorWhitelistedZeroBalanceScenario.js +91 -0
  104. package/tests/unit/state/apply/banValidator/banValidatorWithdrawFailureScenario.js +19 -0
  105. package/tests/unit/state/apply/banValidator/state.apply.banValidator.test.js +266 -0
  106. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentDuplicateRegistrationScenario.js +142 -0
  107. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentHappyPathScenario.js +26 -0
  108. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentIncompleteOperationScenario.js +94 -0
  109. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentInvalidDeploymentEntryScenario.js +37 -0
  110. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentMultipleBootstrapScenario.js +86 -0
  111. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentScenarioHelpers.js +344 -0
  112. package/tests/unit/state/apply/bootstrapDeployment/invalidValidatorNodeEntryScenario.js +57 -0
  113. package/tests/unit/state/apply/bootstrapDeployment/state.apply.bootstrapDeployment.test.js +429 -0
  114. package/tests/unit/state/apply/common/access-control/adminConsistencyMismatchScenario.js +119 -0
  115. package/tests/unit/state/apply/common/access-control/adminEntryDecodeFailureScenario.js +130 -0
  116. package/tests/unit/state/apply/common/access-control/adminEntryExistsScenario.js +93 -0
  117. package/tests/unit/state/apply/common/access-control/adminEntryMissingScenario.js +108 -0
  118. package/tests/unit/state/apply/common/access-control/adminOnlyGuardScenario.js +126 -0
  119. package/tests/unit/state/apply/common/access-control/adminPublicKeyDecodeFailureScenario.js +120 -0
  120. package/tests/unit/state/apply/common/access-control/roleAccessOperationValidationScenario.js +50 -0
  121. package/tests/unit/state/apply/common/adminControlOperationValidationScenario.js +56 -0
  122. package/tests/unit/state/apply/common/balances/adminEntryUpdateFailureScenario.js +52 -0
  123. package/tests/unit/state/apply/common/balances/base/requesterBalanceScenarioBase.js +197 -0
  124. package/tests/unit/state/apply/common/balances/feeDecodeFailureScenario.js +52 -0
  125. package/tests/unit/state/apply/common/balances/requesterBalanceDecodeFailureScenario.js +15 -0
  126. package/tests/unit/state/apply/common/balances/requesterBalanceFeeApplicationFailureScenario.js +11 -0
  127. package/tests/unit/state/apply/common/balances/requesterBalanceInsufficientScenario.js +15 -0
  128. package/tests/unit/state/apply/common/balances/requesterBalanceUpdateFailureScenario.js +11 -0
  129. package/tests/unit/state/apply/common/balances/validatorEntryRewardFailureScenario.js +11 -0
  130. package/tests/unit/state/apply/common/balances/validatorEntryUpdateFailureScenario.js +11 -0
  131. package/tests/unit/state/apply/common/balances/validatorNodeEntryDecodeFailureScenario.js +40 -0
  132. package/tests/unit/state/apply/common/base/OperationValidationScenarioBase.js +114 -0
  133. package/tests/unit/state/apply/common/commonScenarioHelper.js +103 -0
  134. package/tests/unit/state/apply/common/indexer/indexerNodeEntryDecodeFailureScenario.js +36 -0
  135. package/tests/unit/state/apply/common/indexer/indexerNodeEntryMissingScenario.js +36 -0
  136. package/tests/unit/state/apply/common/indexer/indexerRoleUpdateFailureScenario.js +29 -0
  137. package/tests/unit/state/apply/common/indexer/indexerSequenceStateInvalidScenario.js +66 -0
  138. package/tests/unit/state/apply/common/invalidMessageComponentValidationScenario.js +84 -0
  139. package/tests/unit/state/apply/common/nodeEntryInitializationFailureScenario.js +47 -0
  140. package/tests/unit/state/apply/common/operationAlreadyAppliedScenario.js +85 -0
  141. package/tests/unit/state/apply/common/payload-structure/addressWithInvalidPublicKeyScenario.js +52 -0
  142. package/tests/unit/state/apply/common/payload-structure/initializationDisabledScenario.js +49 -0
  143. package/tests/unit/state/apply/common/payload-structure/invalidAddressValidationScenario.js +73 -0
  144. package/tests/unit/state/apply/common/payload-structure/invalidHashValidationScenario.js +71 -0
  145. package/tests/unit/state/apply/common/payload-structure/invalidPayloadValidationScenario.js +31 -0
  146. package/tests/unit/state/apply/common/payload-structure/invalidSignatureValidationScenario.js +142 -0
  147. package/tests/unit/state/apply/common/payload-structure/partialOperationValidationScenario.js +87 -0
  148. package/tests/unit/state/apply/common/requester/requesterNodeEntryBufferMissingScenario.js +70 -0
  149. package/tests/unit/state/apply/common/requester/requesterNodeEntryDecodeFailureScenario.js +72 -0
  150. package/tests/unit/state/apply/common/requester/requesterNodeEntryMissingScenario.js +36 -0
  151. package/tests/unit/state/apply/common/requesterAddressValidationScenario.js +44 -0
  152. package/tests/unit/state/apply/common/requesterPublicKeyValidationScenario.js +25 -0
  153. package/tests/unit/state/apply/common/transactionValidityMismatchScenario.js +98 -0
  154. package/tests/unit/state/apply/common/validatorConsistency/base/validatorConsistencyScenarioBase.js +201 -0
  155. package/tests/unit/state/apply/common/validatorConsistency/validatorEntryDecodeFailureScenario.js +17 -0
  156. package/tests/unit/state/apply/common/validatorConsistency/validatorEntryMissingScenario.js +44 -0
  157. package/tests/unit/state/apply/common/validatorConsistency/validatorInactiveScenario.js +19 -0
  158. package/tests/unit/state/apply/common/validatorConsistency/validatorWriterKeyMismatchScenario.js +18 -0
  159. package/tests/unit/state/apply/common/validatorEntryValidation/base/validatorEntryValidationScenarioBase.js +314 -0
  160. package/tests/unit/state/apply/common/validatorEntryValidation/validatorEntryInvalidBalanceScenario.js +18 -0
  161. package/tests/unit/state/apply/common/writerKeyExistsValidationScenario.js +43 -0
  162. package/tests/unit/state/apply/disableInitialization/disableInitializationAlreadyDisabledScenario.js +53 -0
  163. package/tests/unit/state/apply/disableInitialization/disableInitializationHappyPathScenario.js +24 -0
  164. package/tests/unit/state/apply/disableInitialization/disableInitializationScenarioHelpers.js +197 -0
  165. package/tests/unit/state/apply/disableInitialization/state.apply.disableInitialization.test.js +161 -0
  166. package/tests/unit/state/apply/missing-tests.md +18 -0
  167. package/tests/unit/state/apply/removeIndexer/removeIndexerHappyPathScenario.js +58 -0
  168. package/tests/unit/state/apply/removeIndexer/removeIndexerReAddAndRemoveAgainScenario.js +98 -0
  169. package/tests/unit/state/apply/removeIndexer/removeIndexerRemoveMultipleIndexersScenario.js +167 -0
  170. package/tests/unit/state/apply/removeIndexer/removeIndexerScenarioHelpers.js +428 -0
  171. package/tests/unit/state/apply/removeIndexer/removeIndexerTargetNotIndexerScenario.js +22 -0
  172. package/tests/unit/state/apply/removeIndexer/removeIndexerWriterKeyMissingScenario.js +20 -0
  173. package/tests/unit/state/apply/removeIndexer/state.apply.removeIndexer.test.js +291 -0
  174. package/tests/unit/state/apply/removeWriter/removeWriterAndAddWriterAgainScenario.js +87 -0
  175. package/tests/unit/state/apply/removeWriter/removeWriterHappyPathScenario.js +38 -0
  176. package/tests/unit/state/apply/removeWriter/removeWriterInvalidValidatorSignatureScenario.js +32 -0
  177. package/tests/unit/state/apply/removeWriter/removeWriterRequesterBalanceInsufficientScenario.js +21 -0
  178. package/tests/unit/state/apply/removeWriter/removeWriterRequesterEntryDecodeFailureScenario.js +19 -0
  179. package/tests/unit/state/apply/removeWriter/removeWriterRequesterEntryMissingScenario.js +19 -0
  180. package/tests/unit/state/apply/removeWriter/removeWriterRequesterIndexerScenario.js +21 -0
  181. package/tests/unit/state/apply/removeWriter/removeWriterRequesterNotWriterScenario.js +21 -0
  182. package/tests/unit/state/apply/removeWriter/removeWriterRequesterRoleUpdateFailureScenario.js +19 -0
  183. package/tests/unit/state/apply/removeWriter/removeWriterScenarioHelpers.js +344 -0
  184. package/tests/unit/state/apply/removeWriter/removeWriterThroughWriterValidatorScenario.js +113 -0
  185. package/tests/unit/state/apply/removeWriter/removeWriterUnstakeFailureScenario.js +33 -0
  186. package/tests/unit/state/apply/removeWriter/removeWriterWriterKeyMismatchScenario.js +21 -0
  187. package/tests/unit/state/apply/removeWriter/removeWriterWriterKeyOwnershipScenario.js +26 -0
  188. package/tests/unit/state/apply/removeWriter/removeWriterWriterKeyRegistryMissingScenario.js +22 -0
  189. package/tests/unit/state/apply/removeWriter/state.apply.removeWriter.test.js +307 -0
  190. package/tests/unit/state/apply/state.apply.test.js +24 -0
  191. package/tests/unit/state/apply/transfer/state.apply.transfer.test.js +819 -0
  192. package/tests/unit/state/apply/transfer/transferContractSchemaValidationScenario.js +22 -0
  193. package/tests/unit/state/apply/transfer/transferDoubleSpendAcrossValidatorsScenario.js +137 -0
  194. package/tests/unit/state/apply/transfer/transferDoubleSpendSameBatchScenario.js +63 -0
  195. package/tests/unit/state/apply/transfer/transferDoubleSpendSingleValidatorScenario.js +67 -0
  196. package/tests/unit/state/apply/transfer/transferExistingRecipientAmountScenario.js +31 -0
  197. package/tests/unit/state/apply/transfer/transferExistingRecipientZeroAmountScenario.js +31 -0
  198. package/tests/unit/state/apply/transfer/transferHandlerGuardScenarios.js +22 -0
  199. package/tests/unit/state/apply/transfer/transferHappyPathScenario.js +8 -0
  200. package/tests/unit/state/apply/transfer/transferInvalidIncomingDataScenario.js +66 -0
  201. package/tests/unit/state/apply/transfer/transferNewRecipientAmountScenario.js +31 -0
  202. package/tests/unit/state/apply/transfer/transferNewRecipientZeroAmountScenario.js +31 -0
  203. package/tests/unit/state/apply/transfer/transferScenarioHelpers.js +1167 -0
  204. package/tests/unit/state/apply/transfer/transferSelfTransferAmountScenario.js +38 -0
  205. package/tests/unit/state/apply/transfer/transferSelfTransferZeroAmountScenario.js +38 -0
  206. package/tests/unit/state/apply/transfer/transferValidatorRecipientAmountScenario.js +38 -0
  207. package/tests/unit/state/apply/transfer/transferValidatorRecipientZeroAmountScenario.js +38 -0
  208. package/tests/unit/state/apply/txOperation/state.apply.txOperation.test.js +318 -0
  209. package/tests/unit/state/apply/txOperation/txOperationBootstrapNotRegisteredScenario.js +70 -0
  210. package/tests/unit/state/apply/txOperation/txOperationDifferentValidatorCreatorHappyPathScenario.js +23 -0
  211. package/tests/unit/state/apply/txOperation/txOperationInvalidDeploymentEntryScenario.js +48 -0
  212. package/tests/unit/state/apply/txOperation/txOperationInvalidFeeAmountScenario.js +39 -0
  213. package/tests/unit/state/apply/txOperation/txOperationInvalidSubnetCreatorAddressScenario.js +46 -0
  214. package/tests/unit/state/apply/txOperation/txOperationRequesterCreatorHappyPathScenario.js +21 -0
  215. package/tests/unit/state/apply/txOperation/txOperationScenarioHelpers.js +429 -0
  216. package/tests/unit/state/apply/txOperation/txOperationStandardHappyPathScenario.js +21 -0
  217. package/tests/unit/state/apply/txOperation/txOperationTransferFeeAddCreatorBalanceFailureScenario.js +26 -0
  218. package/tests/unit/state/apply/txOperation/txOperationTransferFeeAddValidatorBalanceFailureScenario.js +25 -0
  219. package/tests/unit/state/apply/txOperation/txOperationTransferFeeAddValidatorBonusFailureScenario.js +27 -0
  220. package/tests/unit/state/apply/txOperation/txOperationTransferFeeDecodeCreatorEntryScenario.js +18 -0
  221. package/tests/unit/state/apply/txOperation/txOperationTransferFeeDecodeRequesterEntryScenario.js +17 -0
  222. package/tests/unit/state/apply/txOperation/txOperationTransferFeeDecodeValidatorEntryScenario.js +31 -0
  223. package/tests/unit/state/apply/txOperation/txOperationTransferFeeGuardBypassScenario.js +49 -0
  224. package/tests/unit/state/apply/txOperation/txOperationTransferFeeGuardScenarioFactory.js +92 -0
  225. package/tests/unit/state/apply/txOperation/txOperationTransferFeeInsufficientRequesterBalanceScenario.js +28 -0
  226. package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidCreatorBalanceScenario.js +29 -0
  227. package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidRequesterBalanceScenario.js +28 -0
  228. package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidRequesterEntryScenario.js +17 -0
  229. package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidValidatorBalanceScenario.js +33 -0
  230. package/tests/unit/state/apply/txOperation/txOperationTransferFeeMissingCreatorEntryScenario.js +18 -0
  231. package/tests/unit/state/apply/txOperation/txOperationTransferFeeSubtractFailureScenario.js +25 -0
  232. package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateCreatorBalanceFailureScenario.js +26 -0
  233. package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateFailureScenario.js +25 -0
  234. package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateValidatorBalanceFailureScenario.js +26 -0
  235. package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateValidatorBonusFailureScenario.js +27 -0
  236. package/tests/unit/state/apply/txOperation/txOperationValidatorCreatorHappyPathScenario.js +21 -0
  237. package/tests/unit/state/stateModule.test.js +1 -0
  238. package/tests/unit/state/stateTestUtils.js +5 -1
  239. package/.env +0 -3
@@ -0,0 +1,94 @@
1
+ import b4a from 'b4a';
2
+ import {
3
+ setupBootstrapDeploymentScenario,
4
+ buildBootstrapDeploymentPayload,
5
+ assertBootstrapDeploymentFailureState
6
+ } from './bootstrapDeploymentScenarioHelpers.js';
7
+ import { safeDecodeApplyOperation, safeEncodeApplyOperation } from '../../../../../src/utils/protobuf/operationHelpers.js';
8
+ import OperationValidationScenarioBase from '../common/base/OperationValidationScenarioBase.js';
9
+
10
+ function removeValidatorFields(t, validPayload) {
11
+ const decoded = safeDecodeApplyOperation(validPayload);
12
+ t.ok(decoded, 'valid payload decodes before mutation');
13
+ if (!decoded?.bdo) return validPayload;
14
+ const mutated = safeEncodeApplyOperation({
15
+ type: decoded.type,
16
+ address: decoded.address,
17
+ bdo: {
18
+ tx: decoded.bdo.tx,
19
+ txv: decoded.bdo.txv,
20
+ bs: decoded.bdo.bs,
21
+ ic: decoded.bdo.ic,
22
+ in: decoded.bdo.in,
23
+ is: decoded.bdo.is
24
+ }
25
+ });
26
+ const roundtrip = safeDecodeApplyOperation(mutated);
27
+ t.ok(roundtrip, 'mutated payload decodes');
28
+ return mutated;
29
+ }
30
+
31
+ async function assertStateUnchanged(t, context, validPayload) {
32
+ const decoded = safeDecodeApplyOperation(validPayload);
33
+ t.ok(decoded, 'valid payload decodes for assertions');
34
+ if (!decoded) return;
35
+
36
+ const { validatorEntryBefore, deployerEntryBefore, externalBootstrap } = context.bootstrapDeployment ?? {};
37
+ const validatorPeer = context.bootstrapDeployment?.validatorPeer ?? context.peers?.[1];
38
+ const deployerPeer = context.bootstrapDeployment?.deployerPeer ?? context.peers?.[2];
39
+
40
+ const validatorEntryAfter = await validatorPeer.base.view.get(validatorPeer.wallet.address);
41
+ t.ok(validatorEntryAfter, 'validator entry still exists after rejected incomplete op');
42
+ if (validatorEntryBefore?.value && validatorEntryAfter?.value) {
43
+ t.ok(
44
+ b4a.equals(validatorEntryAfter.value, validatorEntryBefore.value),
45
+ 'validator entry remains unchanged after incomplete op'
46
+ );
47
+ }
48
+
49
+ const deployerEntryAfter = await validatorPeer.base.view.get(deployerPeer.wallet.address);
50
+ t.ok(deployerEntryAfter, 'deployer entry still exists after rejected incomplete op');
51
+ if (deployerEntryBefore?.value && deployerEntryAfter?.value) {
52
+ const before = nodeEntryUtils.decode(deployerEntryBefore.value);
53
+ const after = nodeEntryUtils.decode(deployerEntryAfter.value);
54
+ t.ok(before, 'deployer entry before decodes');
55
+ t.ok(after, 'deployer entry after decodes');
56
+ if (before && after) {
57
+ t.ok(b4a.equals(after.balance, before.balance), 'deployer balance unchanged after incomplete op');
58
+ t.ok(
59
+ b4a.equals(after.stakedBalance, before.stakedBalance),
60
+ 'deployer stake unchanged after incomplete op'
61
+ );
62
+ }
63
+ }
64
+
65
+ const bootstrapBuffer = externalBootstrap ?? decoded?.bdo?.bs;
66
+ if (bootstrapBuffer) {
67
+ const deploymentKey = `${EntryType.DEPLOYMENT}${bootstrapBuffer.toString('hex')}`;
68
+ const deploymentEntry = await validatorPeer.base.view.get(deploymentKey);
69
+ t.is(deploymentEntry, null, 'deployment entry not created after incomplete op');
70
+ }
71
+
72
+ const txHashBuffer = decoded?.bdo?.tx;
73
+ if (txHashBuffer) {
74
+ const txEntry = await validatorPeer.base.view.get(txHashBuffer.toString('hex'));
75
+ t.is(txEntry, null, 'tx hash not recorded after incomplete op');
76
+ }
77
+ }
78
+
79
+ export default function bootstrapDeploymentIncompleteOperationScenario() {
80
+ new OperationValidationScenarioBase({
81
+ title: 'State.apply bootstrapDeployment rejects operations missing validator cosignature',
82
+ setupScenario: setupBootstrapDeploymentScenario,
83
+ buildValidPayload: buildBootstrapDeploymentPayload,
84
+ mutatePayload: removeValidatorFields,
85
+ applyInvalidPayload: async (context, invalidPayload) => {
86
+ const { bootstrap } = context;
87
+ await bootstrap.base.append(invalidPayload);
88
+ await bootstrap.base.update();
89
+ },
90
+ assertStateUnchanged: (t, context, _validPayload, invalidPayload) =>
91
+ assertBootstrapDeploymentFailureState(t, context, { payload: invalidPayload }),
92
+ expectedLogs: ['Contract schema validation failed.']
93
+ }).performScenario();
94
+ }
@@ -0,0 +1,37 @@
1
+ import b4a from 'b4a';
2
+ import OperationValidationScenarioBase from '../common/base/OperationValidationScenarioBase.js';
3
+ import {
4
+ setupBootstrapDeploymentScenario,
5
+ buildBootstrapDeploymentPayload,
6
+ assertBootstrapDeploymentFailureState
7
+ } from './bootstrapDeploymentScenarioHelpers.js';
8
+ import deploymentEntryUtils from '../../../../../src/core/state/utils/deploymentEntry.js';
9
+
10
+ async function applyPayloadWithEncodeFailure(context, invalidPayload) {
11
+ const node = context.bootstrapDeployment?.validatorPeer ?? context.bootstrap ?? context.peers?.[0];
12
+ const originalEncode = deploymentEntryUtils.encode;
13
+ deploymentEntryUtils.encode = () => b4a.alloc(0);
14
+
15
+ try {
16
+ await node.base.append(invalidPayload);
17
+ await node.base.update();
18
+ } finally {
19
+ deploymentEntryUtils.encode = originalEncode;
20
+ }
21
+ }
22
+
23
+ export default function bootstrapDeploymentInvalidDeploymentEntryScenario() {
24
+ new OperationValidationScenarioBase({
25
+ title: 'State.apply bootstrapDeployment rejects payloads when deployment entry cannot be encoded',
26
+ setupScenario: setupBootstrapDeploymentScenario,
27
+ buildValidPayload: buildBootstrapDeploymentPayload,
28
+ mutatePayload: (_t, validPayload) => validPayload,
29
+ applyInvalidPayload: applyPayloadWithEncodeFailure,
30
+ assertStateUnchanged: (t, context, _valid, invalidPayload) =>
31
+ assertBootstrapDeploymentFailureState(t, context, {
32
+ payload: invalidPayload,
33
+ skipValidatorEquality: true
34
+ }),
35
+ expectedLogs: ['Invalid deployment entry.']
36
+ }).performScenario();
37
+ }
@@ -0,0 +1,86 @@
1
+ import { test } from 'brittle';
2
+ import b4a from 'b4a';
3
+ import { eventFlush } from '../../../../helpers/autobaseTestHelpers.js';
4
+ import {
5
+ setupBootstrapDeploymentScenario,
6
+ buildBootstrapDeploymentPayload,
7
+ assertBootstrapDeploymentSuccessState
8
+ } from './bootstrapDeploymentScenarioHelpers.js';
9
+ import { EntryType } from '../../../../../src/utils/constants.js';
10
+ import { decimalStringToBigInt, bigIntTo16ByteBuffer } from '../../../../../src/utils/amountSerialization.js';
11
+
12
+ const DOUBLE_FUNDING = bigIntTo16ByteBuffer(decimalStringToBigInt('20'));
13
+
14
+ export default function bootstrapDeploymentMultipleBootstrapScenario() {
15
+ test(
16
+ 'State.apply bootstrapDeployment allows a requester to register multiple external bootstraps',
17
+ async t => {
18
+ const context = await setupBootstrapDeploymentScenario(t, {
19
+ nodes: 4,
20
+ deployerInitialBalance: DOUBLE_FUNDING,
21
+ validatorInitialBalance: DOUBLE_FUNDING
22
+ });
23
+
24
+ const validatorPeer = context.bootstrapDeployment?.validatorPeer ?? context.peers[1];
25
+ const deployerPeer = context.bootstrapDeployment?.deployerPeer ?? context.peers[2];
26
+ const extraPeer = context.peers.at(-1);
27
+
28
+ const firstBootstrap = context.bootstrapDeployment?.externalBootstrap ?? b4a.from(deployerPeer.base.local.key);
29
+ const secondBootstrap = extraPeer ? b4a.from(extraPeer.base.local.key) : b4a.alloc(firstBootstrap.length, 0x02);
30
+ const secondChannel = extraPeer ? b4a.from(extraPeer.base.local.key) : b4a.alloc(firstBootstrap.length, 0x03);
31
+
32
+ const validatorEntryBefore1 = await validatorPeer.base.view.get(validatorPeer.wallet.address);
33
+ const deployerEntryBefore1 = await validatorPeer.base.view.get(deployerPeer.wallet.address);
34
+
35
+ const firstPayload = await buildBootstrapDeploymentPayload(context, {
36
+ externalBootstrap: firstBootstrap
37
+ });
38
+
39
+ await validatorPeer.base.append(firstPayload);
40
+ await validatorPeer.base.update();
41
+ await eventFlush();
42
+
43
+ await assertBootstrapDeploymentSuccessState(t, context, {
44
+ payload: firstPayload,
45
+ externalBootstrap: firstBootstrap,
46
+ validatorEntryBefore: validatorEntryBefore1?.value,
47
+ deployerEntryBefore: deployerEntryBefore1?.value
48
+ });
49
+
50
+ const validatorEntryBefore2 = await validatorPeer.base.view.get(validatorPeer.wallet.address);
51
+ const deployerEntryBefore2 = await validatorPeer.base.view.get(deployerPeer.wallet.address);
52
+
53
+ const secondPayload = await buildBootstrapDeploymentPayload(context, {
54
+ externalBootstrap: secondBootstrap,
55
+ channel: secondChannel
56
+ });
57
+
58
+ await validatorPeer.base.append(secondPayload);
59
+ await validatorPeer.base.update();
60
+ await eventFlush();
61
+
62
+ await assertBootstrapDeploymentSuccessState(t, context, {
63
+ payload: secondPayload,
64
+ externalBootstrap: secondBootstrap,
65
+ channel: secondChannel,
66
+ validatorEntryBefore: validatorEntryBefore2?.value,
67
+ deployerEntryBefore: deployerEntryBefore2?.value
68
+ });
69
+
70
+ const firstKey = `${EntryType.DEPLOYMENT}${firstBootstrap.toString('hex')}`;
71
+ const secondKey = `${EntryType.DEPLOYMENT}${secondBootstrap.toString('hex')}`;
72
+
73
+ const firstEntry = await validatorPeer.base.view.get(firstKey);
74
+ const secondEntry = await validatorPeer.base.view.get(secondKey);
75
+ t.ok(firstEntry, 'first deployment entry stored on validator view');
76
+ t.ok(secondEntry, 'second deployment entry stored on validator view');
77
+
78
+ await context.sync();
79
+
80
+ const firstEntryReplica = await deployerPeer.base.view.get(firstKey);
81
+ const secondEntryReplica = await deployerPeer.base.view.get(secondKey);
82
+ t.ok(firstEntryReplica, 'first deployment entry replicated to deployer view');
83
+ t.ok(secondEntryReplica, 'second deployment entry replicated to deployer view');
84
+ }
85
+ );
86
+ }
@@ -0,0 +1,344 @@
1
+ import b4a from 'b4a';
2
+ import PartialStateMessageOperations from '../../../../../src/messages/partialStateMessages/PartialStateMessageOperations.js';
3
+ import CompleteStateMessageOperations from '../../../../../src/messages/completeStateMessages/CompleteStateMessageOperations.js';
4
+ import { deriveIndexerSequenceState } from '../../../../helpers/autobaseTestHelpers.js';
5
+ import {
6
+ setupAdminNetwork,
7
+ initializeBalances,
8
+ whitelistAddress
9
+ } from '../common/commonScenarioHelper.js';
10
+ import {
11
+ promotePeerToWriter,
12
+ assertValidatorReward
13
+ } from '../addWriter/addWriterScenarioHelpers.js';
14
+ import nodeEntryUtils from '../../../../../src/core/state/utils/nodeEntry.js';
15
+ import deploymentEntryUtils from '../../../../../src/core/state/utils/deploymentEntry.js';
16
+ import transactionUtils from '../../../../../src/core/state/utils/transaction.js';
17
+ import addressUtils from '../../../../../src/core/state/utils/address.js';
18
+ import { toBalance } from '../../../../../src/core/state/utils/balance.js';
19
+ import { EntryType } from '../../../../../src/utils/constants.js';
20
+ import { decimalStringToBigInt, bigIntTo16ByteBuffer } from '../../../../../src/utils/amountSerialization.js';
21
+ import { safeDecodeApplyOperation, safeEncodeApplyOperation } from '../../../../../src/utils/protobuf/operationHelpers.js';
22
+
23
+ const DEFAULT_FUNDING = bigIntTo16ByteBuffer(decimalStringToBigInt('10'));
24
+
25
+ export function selectBootstrapValidatorPeer(context, offset = 0) {
26
+ const peers = context.peers.slice(1);
27
+ if (!peers.length) {
28
+ throw new Error('Bootstrap deployment scenarios require a validator peer.');
29
+ }
30
+ return peers[Math.min(offset, peers.length - 1)];
31
+ }
32
+
33
+ export function selectBootstrapDeployerPeer(context, offset = 1) {
34
+ const peers = context.peers.slice(1);
35
+ if (peers.length < 2) {
36
+ throw new Error('Bootstrap deployment scenarios require a deployer peer.');
37
+ }
38
+ return peers[Math.min(offset, peers.length - 1)];
39
+ }
40
+
41
+ export async function setupBootstrapDeploymentScenario(
42
+ t,
43
+ {
44
+ nodes = 3,
45
+ validatorInitialBalance = DEFAULT_FUNDING,
46
+ deployerInitialBalance = DEFAULT_FUNDING,
47
+ externalBootstrap = null,
48
+ channel = null
49
+ } = {}
50
+ ) {
51
+ const context = await setupAdminNetwork(t, { nodes: Math.max(nodes, 3) });
52
+ const validatorPeer = selectBootstrapValidatorPeer(context);
53
+ const deployerPeer = selectBootstrapDeployerPeer(context);
54
+
55
+ context.addWriterScenario = { writerInitialBalance: validatorInitialBalance };
56
+
57
+ await initializeBalances(context, [
58
+ [validatorPeer.wallet.address, validatorInitialBalance],
59
+ [deployerPeer.wallet.address, deployerInitialBalance]
60
+ ]);
61
+
62
+ await whitelistAddress(context, validatorPeer.wallet.address);
63
+ await whitelistAddress(context, deployerPeer.wallet.address);
64
+
65
+ await promotePeerToWriter(t, context, { readerPeer: validatorPeer });
66
+ await context.sync();
67
+
68
+ const txValidity = await deriveIndexerSequenceState(validatorPeer.base);
69
+ const resolvedExternalBootstrap = externalBootstrap ?? b4a.from(deployerPeer.base.local.key);
70
+ const resolvedChannel = channel ?? b4a.from(validatorPeer.base.local.key);
71
+
72
+ const validatorEntryBefore = await validatorPeer.base.view.get(validatorPeer.wallet.address);
73
+ const deployerEntryBefore = await validatorPeer.base.view.get(deployerPeer.wallet.address);
74
+
75
+ context.bootstrapDeployment = {
76
+ validatorPeer,
77
+ deployerPeer,
78
+ externalBootstrap: resolvedExternalBootstrap,
79
+ channel: resolvedChannel,
80
+ txValidity,
81
+ validatorEntryBefore: validatorEntryBefore
82
+ ? { value: b4a.from(validatorEntryBefore.value) }
83
+ : null,
84
+ deployerEntryBefore: deployerEntryBefore ? { value: b4a.from(deployerEntryBefore.value) } : null
85
+ };
86
+
87
+ return context;
88
+ }
89
+
90
+ export async function buildBootstrapDeploymentPayload(context, options = {}) {
91
+ const validatorPeer =
92
+ options.validatorPeer ??
93
+ context.bootstrapDeployment?.validatorPeer ??
94
+ selectBootstrapValidatorPeer(context);
95
+ const deployerPeer =
96
+ options.deployerPeer ??
97
+ context.bootstrapDeployment?.deployerPeer ??
98
+ selectBootstrapDeployerPeer(context);
99
+
100
+ const externalBootstrap =
101
+ options.externalBootstrap ??
102
+ context.bootstrapDeployment?.externalBootstrap ??
103
+ b4a.from(deployerPeer.base.local.key);
104
+ const channel =
105
+ options.channel ?? context.bootstrapDeployment?.channel ?? b4a.from(validatorPeer.base.local.key);
106
+ const txValidity =
107
+ options.txValidity ??
108
+ context.bootstrapDeployment?.txValidity ??
109
+ (await deriveIndexerSequenceState(validatorPeer.base));
110
+
111
+ const partial = await PartialStateMessageOperations.assembleBootstrapDeploymentMessage(
112
+ deployerPeer.wallet,
113
+ externalBootstrap.toString('hex'),
114
+ channel.toString('hex'),
115
+ txValidity.toString('hex')
116
+ );
117
+
118
+ return CompleteStateMessageOperations.assembleCompleteBootstrapDeployment(
119
+ validatorPeer.wallet,
120
+ partial.address,
121
+ b4a.from(partial.bdo.tx, 'hex'),
122
+ b4a.from(partial.bdo.txv, 'hex'),
123
+ b4a.from(partial.bdo.bs, 'hex'),
124
+ b4a.from(partial.bdo.ic, 'hex'),
125
+ b4a.from(partial.bdo.in, 'hex'),
126
+ b4a.from(partial.bdo.is, 'hex')
127
+ );
128
+ }
129
+
130
+ export async function buildBootstrapDeploymentPayloadWithTxValidity(context, txValidity, options = {}) {
131
+ return buildBootstrapDeploymentPayload(context, {
132
+ ...options,
133
+ txValidity
134
+ });
135
+ }
136
+
137
+ export async function assertBootstrapDeploymentSuccessState(
138
+ t,
139
+ context,
140
+ {
141
+ payload = null,
142
+ validatorPeer = context.bootstrapDeployment?.validatorPeer ?? selectBootstrapValidatorPeer(context),
143
+ deployerPeer = context.bootstrapDeployment?.deployerPeer ?? selectBootstrapDeployerPeer(context),
144
+ externalBootstrap = context.bootstrapDeployment?.externalBootstrap ?? null,
145
+ channel = context.bootstrapDeployment?.channel ?? null,
146
+ validatorEntryBefore = context.bootstrapDeployment?.validatorEntryBefore?.value ?? null,
147
+ deployerEntryBefore = context.bootstrapDeployment?.deployerEntryBefore?.value ?? null,
148
+ txValidity = context.bootstrapDeployment?.txValidity ?? null,
149
+ skipSync = false
150
+ } = {}
151
+ ) {
152
+ if (!payload) throw new Error('assertBootstrapDeploymentSuccessState requires the processed payload.');
153
+ if (!validatorEntryBefore) {
154
+ throw new Error('assertBootstrapDeploymentSuccessState requires the validator entry before execution.');
155
+ }
156
+ if (!deployerEntryBefore) {
157
+ throw new Error('assertBootstrapDeploymentSuccessState requires the deployer entry before execution.');
158
+ }
159
+
160
+ const bootstrapBuffer = externalBootstrap ?? b4a.from(deployerPeer.base.local.key);
161
+ const channelBuffer = channel ?? b4a.from(validatorPeer.base.local.key);
162
+ const decodedOperation = safeDecodeApplyOperation(payload);
163
+ t.ok(decodedOperation, 'bootstrapDeployment payload decodes');
164
+
165
+ const requesterAddressBuffer = decodedOperation?.address;
166
+ const validatorAddressBuffer = decodedOperation?.bdo?.va;
167
+ const txHashBuffer = decodedOperation?.bdo?.tx;
168
+ const txValidityBuffer = decodedOperation?.bdo?.txv;
169
+ const channelFromPayload = decodedOperation?.bdo?.ic;
170
+ const bootstrapFromPayload = decodedOperation?.bdo?.bs;
171
+
172
+ t.ok(requesterAddressBuffer, 'payload carries requester address');
173
+ t.ok(validatorAddressBuffer, 'payload carries validator address');
174
+ t.ok(txHashBuffer, 'payload exposes tx hash');
175
+
176
+ const requesterAddress = addressUtils.bufferToAddress(requesterAddressBuffer);
177
+ const validatorAddress = addressUtils.bufferToAddress(validatorAddressBuffer);
178
+
179
+ if (requesterAddress) {
180
+ t.is(requesterAddress, deployerPeer.wallet.address, 'payload signed by expected requester');
181
+ }
182
+ if (validatorAddress) {
183
+ t.is(validatorAddress, validatorPeer.wallet.address, 'payload signed by expected validator');
184
+ }
185
+
186
+ if (bootstrapFromPayload) {
187
+ t.ok(b4a.equals(bootstrapFromPayload, bootstrapBuffer), 'payload carries external bootstrap');
188
+ }
189
+ if (channelFromPayload) {
190
+ t.ok(b4a.equals(channelFromPayload, channelBuffer), 'payload carries expected channel');
191
+ }
192
+ if (txValidity && txValidityBuffer) {
193
+ t.ok(b4a.equals(txValidityBuffer, txValidity), 'payload tx validity matches current indexer state');
194
+ }
195
+
196
+ const decodedValidatorBefore = nodeEntryUtils.decode(validatorEntryBefore);
197
+ t.ok(decodedValidatorBefore, 'validator entry before bootstrapDeployment decodes');
198
+ if (!decodedValidatorBefore) return;
199
+
200
+ await assertRequesterPaidFee(t, validatorPeer.base, deployerPeer.wallet.address, deployerEntryBefore);
201
+ await assertValidatorReward(t, validatorPeer, b4a.from(decodedValidatorBefore.balance));
202
+ await assertDeploymentEntry(t, validatorPeer.base, {
203
+ bootstrapBuffer,
204
+ txHashBuffer,
205
+ requesterAddressBuffer
206
+ });
207
+
208
+ if (!skipSync) {
209
+ await context.sync();
210
+ await assertRequesterPaidFee(t, deployerPeer.base, deployerPeer.wallet.address, deployerEntryBefore);
211
+ await assertDeploymentEntry(t, deployerPeer.base, {
212
+ bootstrapBuffer,
213
+ txHashBuffer,
214
+ requesterAddressBuffer
215
+ });
216
+ }
217
+ }
218
+
219
+ export async function assertBootstrapDeploymentFailureState(
220
+ t,
221
+ context,
222
+ { payload, bootstrapBufferOverride = null, skipValidatorEquality = false } = {}
223
+ ) {
224
+ if (!payload) throw new Error('assertBootstrapDeploymentFailureState requires a payload.');
225
+
226
+ const decoded = safeDecodeApplyOperation(payload);
227
+ t.ok(decoded, 'invalid payload decodes for assertions');
228
+ if (!decoded) return;
229
+
230
+ const validatorPeer = context.bootstrapDeployment?.validatorPeer ?? context.peers?.[1];
231
+ const deployerPeer = context.bootstrapDeployment?.deployerPeer ?? context.peers?.[2];
232
+ const validatorEntryBefore = context.bootstrapDeployment?.validatorEntryBefore?.value ?? null;
233
+ const deployerEntryBefore = context.bootstrapDeployment?.deployerEntryBefore?.value ?? null;
234
+
235
+ if (validatorPeer) {
236
+ const after = await validatorPeer.base.view.get(validatorPeer.wallet.address);
237
+ t.ok(after, 'validator entry still exists after rejection');
238
+ if (!skipValidatorEquality && validatorEntryBefore && after?.value) {
239
+ t.ok(b4a.equals(after.value, validatorEntryBefore), 'validator entry remains unchanged');
240
+ }
241
+ }
242
+
243
+ if (deployerPeer) {
244
+ const after = await validatorPeer.base.view.get(deployerPeer.wallet.address);
245
+ t.ok(after, 'deployer entry still exists after rejection');
246
+ if (deployerEntryBefore && after?.value) {
247
+ const beforeDecoded = nodeEntryUtils.decode(deployerEntryBefore);
248
+ const afterDecoded = nodeEntryUtils.decode(after.value);
249
+ t.ok(beforeDecoded, 'deployer entry before decodes');
250
+ t.ok(afterDecoded, 'deployer entry after decodes');
251
+ if (beforeDecoded && afterDecoded) {
252
+ t.ok(b4a.equals(afterDecoded.balance, beforeDecoded.balance), 'deployer balance unchanged');
253
+ t.ok(
254
+ b4a.equals(afterDecoded.stakedBalance, beforeDecoded.stakedBalance),
255
+ 'deployer stake unchanged'
256
+ );
257
+ }
258
+ }
259
+ }
260
+
261
+ const bootstrapBuffer = bootstrapBufferOverride ?? decoded?.bdo?.bs ?? context.bootstrapDeployment?.externalBootstrap;
262
+ if (bootstrapBuffer && validatorPeer) {
263
+ const deploymentKey = `${EntryType.DEPLOYMENT}${bootstrapBuffer.toString('hex')}`;
264
+ const deploymentEntry = await validatorPeer.base.view.get(deploymentKey);
265
+ t.is(deploymentEntry, null, 'deployment entry not created after rejection');
266
+ }
267
+
268
+ const txHashBuffer = decoded?.bdo?.tx;
269
+ if (txHashBuffer && validatorPeer) {
270
+ const txEntry = await validatorPeer.base.view.get(txHashBuffer.toString('hex'));
271
+ t.is(txEntry, null, 'tx hash not recorded after rejection');
272
+ }
273
+ }
274
+
275
+ export function mutateToNetworkBootstrap(t, payload, context) {
276
+ const decoded = safeDecodeApplyOperation(payload);
277
+ t.ok(decoded, 'valid payload decodes before mutation');
278
+ if (!decoded?.bdo) return payload;
279
+ const bootstrapKey = context.bootstrap?.base?.local?.key;
280
+ t.ok(bootstrapKey, 'network bootstrap key available');
281
+ if (!bootstrapKey) return payload;
282
+ decoded.bdo.bs = bootstrapKey;
283
+ return safeEncodeApplyOperation(decoded);
284
+ }
285
+
286
+ export async function appendInvalidPayload(context, invalidPayload) {
287
+ const node = context.bootstrap ?? context.adminBootstrap ?? context.peers?.[0];
288
+ await node.base.append(invalidPayload);
289
+ await node.base.update();
290
+ }
291
+
292
+ async function assertRequesterPaidFee(t, base, address, entryBeforeValue) {
293
+ const beforeEntry = nodeEntryUtils.decode(entryBeforeValue);
294
+ t.ok(beforeEntry, 'deployer entry before bootstrapDeployment decodes');
295
+ if (!beforeEntry) return;
296
+
297
+ const beforeBalance = toBalance(beforeEntry.balance);
298
+ t.ok(beforeBalance, 'deployer balance before bootstrapDeployment decodes');
299
+ if (!beforeBalance) return;
300
+
301
+ const expectedBalance = beforeBalance.sub(toBalance(transactionUtils.FEE));
302
+ t.ok(expectedBalance, 'deployer balance after fee computation succeeds');
303
+ if (!expectedBalance) return;
304
+
305
+ const entry = await base.view.get(address);
306
+ t.ok(entry, 'deployer entry exists after bootstrapDeployment');
307
+ const decoded = nodeEntryUtils.decode(entry?.value);
308
+ t.ok(decoded, 'deployer entry decodes after bootstrapDeployment');
309
+ if (!decoded) return;
310
+
311
+ t.is(decoded.isWhitelisted, true, 'deployer remains whitelisted');
312
+ t.is(decoded.isWriter, false, 'deployer not promoted to writer');
313
+ t.is(decoded.isIndexer, false, 'deployer not promoted to indexer');
314
+ t.ok(
315
+ b4a.equals(decoded.balance, expectedBalance.value),
316
+ 'deployer balance reduced by bootstrapDeployment fee'
317
+ );
318
+ }
319
+
320
+ async function assertDeploymentEntry(
321
+ t,
322
+ base,
323
+ { bootstrapBuffer, txHashBuffer, requesterAddressBuffer }
324
+ ) {
325
+ if (!txHashBuffer) {
326
+ t.fail('bootstrapDeployment tx hash missing');
327
+ return;
328
+ }
329
+ const deploymentKey = `${EntryType.DEPLOYMENT}${bootstrapBuffer.toString('hex')}`;
330
+ const deploymentEntry = await base.view.get(deploymentKey);
331
+ t.ok(deploymentEntry, 'deployment entry stored');
332
+ const decodedDeployment = deploymentEntryUtils.decode(deploymentEntry?.value);
333
+ t.ok(decodedDeployment?.txHash, 'deployment entry decodes');
334
+ if (!decodedDeployment || !decodedDeployment.txHash) return;
335
+
336
+ t.ok(b4a.equals(decodedDeployment.txHash, txHashBuffer), 'deployment entry stores tx hash');
337
+ t.ok(
338
+ b4a.equals(decodedDeployment.address, requesterAddressBuffer),
339
+ 'deployment entry binds bootstrap to requester address'
340
+ );
341
+
342
+ const txEntry = await base.view.get(txHashBuffer.toString('hex'));
343
+ t.ok(txEntry, 'bootstrapDeployment transaction recorded for replay protection');
344
+ }
@@ -0,0 +1,57 @@
1
+ import b4a from 'b4a';
2
+ import OperationValidationScenarioBase from '../common/base/OperationValidationScenarioBase.js';
3
+ import nodeEntryUtils from '../../../../../src/core/state/utils/nodeEntry.js';
4
+ import { eventFlush } from '../../../../helpers/autobaseTestHelpers.js';
5
+ import {
6
+ setupBootstrapDeploymentScenario,
7
+ buildBootstrapDeploymentPayload,
8
+ assertBootstrapDeploymentFailureState
9
+ } from './bootstrapDeploymentScenarioHelpers.js';
10
+
11
+ export default function bootstrapDeploymentInvalidValidatorNodeEntryScenario() {
12
+ new OperationValidationScenarioBase({
13
+ title: 'State.apply bootstrapDeployment rejects payloads when validator node entry becomes invalid',
14
+ setupScenario: setupBootstrapDeploymentScenario,
15
+ buildValidPayload: buildBootstrapDeploymentPayload,
16
+ mutatePayload: (_t, payload) => payload,
17
+ applyInvalidPayload: applyWithValidatorDecodeFailureOnSecondRead,
18
+ assertStateUnchanged: (t, context, _valid, invalidPayload) =>
19
+ assertBootstrapDeploymentFailureState(t, context, {
20
+ payload: invalidPayload,
21
+ skipValidatorEquality: true
22
+ }),
23
+ expectedLogs: ['Invalid validator node entry.']
24
+ }).performScenario();
25
+ }
26
+
27
+ async function applyWithValidatorDecodeFailureOnSecondRead(context, payload) {
28
+ const node = context.bootstrapDeployment?.validatorPeer ?? context.bootstrap ?? context.adminBootstrap;
29
+ const validatorPeer = context.bootstrapDeployment?.validatorPeer ?? context.peers?.[1];
30
+ if (!node?.base || !validatorPeer?.base?.local?.key) {
31
+ throw new Error('Validator node entry invalidation scenario requires validator peer and writable base.');
32
+ }
33
+
34
+ const validatorWk = validatorPeer.base.local.key;
35
+ const originalDecode = nodeEntryUtils.decode;
36
+ let hasReturnedValid = false;
37
+
38
+ nodeEntryUtils.decode = function patchedDecode(buffer) {
39
+ const decoded = originalDecode(buffer);
40
+ if (decoded && b4a.equals(decoded.wk, validatorWk)) {
41
+ if (!hasReturnedValid) {
42
+ hasReturnedValid = true;
43
+ return decoded;
44
+ }
45
+ return null;
46
+ }
47
+ return decoded;
48
+ };
49
+
50
+ try {
51
+ await node.base.append(payload);
52
+ await node.base.update();
53
+ await eventFlush();
54
+ } finally {
55
+ nodeEntryUtils.decode = originalDecode;
56
+ }
57
+ }