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.
- package/README.md +62 -2
- package/SECURITY.md +9 -6
- package/cli/commandHandler.js +272 -0
- package/cli/handlers.js +210 -0
- package/cli/interactive.js +182 -0
- package/msb.mjs +7 -8
- package/package.json +25 -8
- package/rpc/create_server.js +44 -44
- package/rpc/handlers.js +43 -56
- package/rpc/routes/index.js +8 -8
- package/rpc/rpc_server.js +6 -2
- package/rpc/utils/helpers.js +46 -66
- package/scripts/pear-runner.js +170 -0
- package/src/config/args.js +60 -13
- package/src/config/config.js +138 -10
- package/src/config/env.js +36 -18
- package/src/core/network/Network.js +130 -34
- package/src/core/network/protocols/ProtocolInterface.js +5 -5
- package/src/core/network/protocols/connectionPolicies.js +86 -0
- package/src/core/network/protocols/legacy/NetworkMessageRouter.js +1 -1
- package/src/core/network/protocols/legacy/handlers/BaseStateOperationHandler.js +2 -2
- package/src/core/network/protocols/legacy/handlers/LegacyGetRequestHandler.js +3 -3
- package/src/core/network/protocols/legacy/handlers/LegacyRoleOperationHandler.js +1 -1
- package/src/core/network/protocols/legacy/handlers/LegacySubnetworkOperationHandler.js +1 -1
- package/src/core/network/protocols/legacy/handlers/LegacyTransferOperationHandler.js +1 -1
- package/src/core/network/protocols/legacy/validators/base/BaseResponse.js +7 -10
- package/src/core/network/protocols/shared/validators/PartialBootstrapDeploymentValidator.js +2 -2
- package/src/core/network/protocols/shared/validators/PartialOperationValidator.js +23 -23
- package/src/core/network/protocols/shared/validators/PartialRoleAccessValidator.js +14 -14
- package/src/core/network/protocols/shared/validators/PartialTransactionValidator.js +5 -5
- package/src/core/network/protocols/shared/validators/PartialTransferValidator.js +12 -11
- package/src/core/network/protocols/v1/V1ProtocolError.js +5 -69
- package/src/core/network/protocols/v1/handlers/V1BaseOperationHandler.js +3 -2
- package/src/core/network/protocols/v1/handlers/V1BroadcastTransactionOperationHandler.js +130 -247
- package/src/core/network/protocols/v1/handlers/V1LivenessOperationHandler.js +6 -5
- package/src/core/network/protocols/v1/handlers/broadcastTransaction/BroadcastTransactionErrorMapper.js +84 -0
- package/src/core/network/protocols/v1/handlers/broadcastTransaction/BroadcastTransactionOperationStrategies.js +128 -0
- package/src/core/network/protocols/v1/handlers/broadcastTransaction/BroadcastTransactionPayloadUtils.js +26 -0
- package/src/core/network/protocols/v1/validators/V1BaseOperation.js +32 -31
- package/src/core/network/protocols/v1/validators/V1BroadcastTransactionRequest.js +5 -3
- package/src/core/network/protocols/v1/validators/V1BroadcastTransactionResponse.js +34 -50
- package/src/core/network/protocols/v1/validators/V1ValidationSchema.js +1 -1
- package/src/core/network/services/ConnectionManager.js +11 -10
- package/src/core/network/services/MessageOrchestrator.js +53 -17
- package/src/core/network/services/PendingRequestService.js +22 -19
- package/src/core/network/services/TransactionCommitService.js +36 -36
- package/src/core/network/services/TransactionPoolService.js +39 -39
- package/src/core/network/services/TransactionRateLimiterService.js +8 -4
- package/src/core/network/services/ValidatorObserverService.js +397 -110
- package/src/core/state/State.js +190 -138
- package/src/core/state/utils/address.js +4 -9
- package/src/core/state/utils/balance.js +7 -7
- package/src/core/state/utils/roles.js +1 -1
- package/src/index.js +316 -275
- package/src/messages/network/v1/NetworkMessageBuilder.js +12 -21
- package/src/messages/network/v1/networkMessageFactory.js +1 -1
- package/src/messages/state/ApplyStateMessageBuilder.js +6 -6
- package/src/messages/state/applyStateMessageFactory.js +1 -1
- package/src/utils/buffer.js +1 -1
- package/src/utils/check.js +12 -13
- package/src/utils/constants.js +7 -0
- package/src/utils/errors.js +20 -0
- package/src/utils/fileUtils.js +12 -3
- package/src/utils/helpers.js +2 -2
- package/src/utils/migrationUtils.js +3 -3
- package/.dockerignore +0 -16
- package/CODE_OF_CONDUCT.md +0 -128
- package/docker-compose.yml +0 -17
- package/dockerfile +0 -41
- package/docs/fee_distribution.md +0 -89
- package/docs/trac_network_http_api.openapi.yaml +0 -889
- package/migration/.gitkeep +0 -0
- package/proto/applyOperations.proto +0 -117
- package/proto/network/v1/enums/message_type.proto +0 -16
- package/proto/network/v1/enums/result_code.proto +0 -84
- package/proto/network/v1/messages/broadcast_transaction_request.proto +0 -9
- package/proto/network/v1/messages/broadcast_transaction_response.proto +0 -13
- package/proto/network/v1/messages/liveness_request.proto +0 -8
- package/proto/network/v1/messages/liveness_response.proto +0 -11
- package/proto/network/v1/network_message.proto +0 -22
- package/rpc/rpc_services.js +0 -181
- package/scripts/generate-protobufs.js +0 -67
- package/src/core/network/identity/NetworkWalletFactory.js +0 -77
- package/src/core/network/protocols/shared/errors/SharedValidatorRejectionError.js +0 -27
- package/src/utils/cli.js +0 -130
- package/src/utils/cliCommands.js +0 -280
- package/tests/acceptance/v1/account/account.test.mjs +0 -129
- package/tests/acceptance/v1/balance/balance.test.mjs +0 -54
- package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +0 -116
- package/tests/acceptance/v1/confirmed-length/confirmed-length.test.mjs +0 -19
- package/tests/acceptance/v1/fee/fee.test.mjs +0 -11
- package/tests/acceptance/v1/health/health.test.mjs +0 -33
- package/tests/acceptance/v1/rpc.test.mjs +0 -95
- package/tests/acceptance/v1/tx/tx.test.mjs +0 -133
- package/tests/acceptance/v1/tx-details/tx-details.test.mjs +0 -241
- package/tests/acceptance/v1/tx-hashes/tx-hashes.test.mjs +0 -72
- package/tests/acceptance/v1/tx-payloads-bulk/tx-payloads-bulk.test.mjs +0 -27
- package/tests/acceptance/v1/txv/txv.test.mjs +0 -11
- package/tests/acceptance/v1/unconfirmed-length/unconfirmed-length.test.mjs +0 -11
- package/tests/fixtures/apply.fixtures.js +0 -77
- package/tests/fixtures/assembleMessage.fixtures.js +0 -41
- package/tests/fixtures/check.fixtures.js +0 -428
- package/tests/fixtures/networkV1.fixtures.js +0 -59
- package/tests/fixtures/protobuf.fixtures.js +0 -388
- package/tests/helpers/StateNetworkFactory.js +0 -155
- package/tests/helpers/address.js +0 -6
- package/tests/helpers/autobaseTestHelpers.js +0 -419
- package/tests/helpers/config.js +0 -4
- package/tests/helpers/createTestSignature.js +0 -13
- package/tests/helpers/regexHelper.js +0 -3
- package/tests/helpers/setupApplyTests.js +0 -587
- package/tests/helpers/transactionPayloads.mjs +0 -92
- package/tests/helpers/wrapper.js +0 -28
- package/tests/unit/messages/messages.test.js +0 -12
- package/tests/unit/messages/network/NetworkMessageBuilder.test.js +0 -436
- package/tests/unit/messages/network/NetworkMessageDirector.test.js +0 -347
- package/tests/unit/messages/state/applyStateMessageBuilder.complete.test.js +0 -525
- package/tests/unit/messages/state/applyStateMessageBuilder.partial.test.js +0 -237
- package/tests/unit/network/LegacyNetworkMessageRouter.test.js +0 -54
- package/tests/unit/network/NetworkWalletFactory.test.js +0 -156
- package/tests/unit/network/ProtocolSession.test.js +0 -127
- package/tests/unit/network/networkModule.test.js +0 -13
- package/tests/unit/network/services/ConnectionManager.test.js +0 -450
- package/tests/unit/network/services/MessageOrchestrator.test.js +0 -445
- package/tests/unit/network/services/PendingRequestService.test.js +0 -431
- package/tests/unit/network/services/TransactionCommitService.test.js +0 -246
- package/tests/unit/network/services/TransactionPoolService.test.js +0 -489
- package/tests/unit/network/services/TransactionRateLimiterService.test.js +0 -139
- package/tests/unit/network/services/ValidatorHealthCheckService.test.js +0 -115
- package/tests/unit/network/services/services.test.js +0 -17
- package/tests/unit/network/utils/v1TestUtils.js +0 -153
- package/tests/unit/network/v1/NetworkMessageRouterV1.test.js +0 -151
- package/tests/unit/network/v1/V1BaseOperation.test.js +0 -356
- package/tests/unit/network/v1/V1BroadcastTransactionOperationHandler.test.js +0 -129
- package/tests/unit/network/v1/V1BroadcastTransactionRequest.test.js +0 -53
- package/tests/unit/network/v1/V1BroadcastTransactionResponse.test.js +0 -512
- package/tests/unit/network/v1/V1LivenessRequest.test.js +0 -32
- package/tests/unit/network/v1/V1LivenessResponse.test.js +0 -45
- package/tests/unit/network/v1/V1ResultCode.test.js +0 -84
- package/tests/unit/network/v1/V1ValidationSchema.test.js +0 -13
- package/tests/unit/network/v1/connectionPolicies.test.js +0 -49
- package/tests/unit/network/v1/handlers/V1BaseOperationHandler.test.js +0 -284
- package/tests/unit/network/v1/handlers/V1BroadcastTransactionOperationHandler.test.js +0 -794
- package/tests/unit/network/v1/handlers/V1LivenessOperationHandler.test.js +0 -193
- package/tests/unit/network/v1/v1.handlers.test.js +0 -15
- package/tests/unit/network/v1/v1.test.js +0 -19
- package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionRequest.test.js +0 -119
- package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionResponse.test.js +0 -136
- package/tests/unit/network/v1/v1ValidationSchema/common.test.js +0 -308
- package/tests/unit/network/v1/v1ValidationSchema/livenessRequest.test.js +0 -90
- package/tests/unit/network/v1/v1ValidationSchema/livenessResponse.test.js +0 -133
- package/tests/unit/state/State.test.js +0 -80
- package/tests/unit/state/apply/addAdmin/addAdminHappyPathScenario.js +0 -42
- package/tests/unit/state/apply/addAdmin/addAdminScenarioHelpers.js +0 -276
- package/tests/unit/state/apply/addAdmin/adminEntryEncodingFailureScenario.js +0 -30
- package/tests/unit/state/apply/addAdmin/adminEntryExistsScenario.js +0 -78
- package/tests/unit/state/apply/addAdmin/nodeEntryInitializationFailureScenario.js +0 -30
- package/tests/unit/state/apply/addAdmin/nonBootstrapNodeScenario.js +0 -68
- package/tests/unit/state/apply/addAdmin/state.apply.addAdmin.test.js +0 -159
- package/tests/unit/state/apply/addIndexer/addIndexerHappyPathScenario.js +0 -39
- package/tests/unit/state/apply/addIndexer/addIndexerMultipleIndexersInTheNetworkScenario.js +0 -167
- package/tests/unit/state/apply/addIndexer/addIndexerPretenderAlreadyIndexerScenario.js +0 -21
- package/tests/unit/state/apply/addIndexer/addIndexerPretenderNotWriterScenario.js +0 -21
- package/tests/unit/state/apply/addIndexer/addIndexerRemoveAndReAddScenario.js +0 -186
- package/tests/unit/state/apply/addIndexer/addIndexerScenarioHelpers.js +0 -443
- package/tests/unit/state/apply/addIndexer/addIndexerWriterKeyAlreadyRegisteredScenario.js +0 -32
- package/tests/unit/state/apply/addIndexer/state.apply.addIndexer.test.js +0 -297
- package/tests/unit/state/apply/addWriter/addWriterHappyPathScenario.js +0 -41
- package/tests/unit/state/apply/addWriter/addWriterInvalidValidatorSignatureScenario.js +0 -32
- package/tests/unit/state/apply/addWriter/addWriterNewWkScenario.js +0 -149
- package/tests/unit/state/apply/addWriter/addWriterRequesterAlreadyWriterScenario.js +0 -21
- package/tests/unit/state/apply/addWriter/addWriterRequesterBalanceInsufficientScenario.js +0 -21
- package/tests/unit/state/apply/addWriter/addWriterRequesterEntryDecodeFailureScenario.js +0 -19
- package/tests/unit/state/apply/addWriter/addWriterRequesterEntryMissingScenario.js +0 -19
- package/tests/unit/state/apply/addWriter/addWriterRequesterIndexerScenario.js +0 -21
- package/tests/unit/state/apply/addWriter/addWriterRequesterNotWhitelistedScenario.js +0 -21
- package/tests/unit/state/apply/addWriter/addWriterScenarioHelpers.js +0 -766
- package/tests/unit/state/apply/addWriter/addWriterStakeBalanceUpdateFailureScenario.js +0 -50
- package/tests/unit/state/apply/addWriter/addWriterStakeInsufficientBalanceScenario.js +0 -29
- package/tests/unit/state/apply/addWriter/addWriterStakeInvalidBalanceScenario.js +0 -29
- package/tests/unit/state/apply/addWriter/addWriterStakeInvalidEntryScenario.js +0 -21
- package/tests/unit/state/apply/addWriter/addWriterStakeStakedBalanceFailureScenario.js +0 -37
- package/tests/unit/state/apply/addWriter/addWriterStakeSubtractFailureScenario.js +0 -42
- package/tests/unit/state/apply/addWriter/addWriterValidatorRewardScenario.js +0 -106
- package/tests/unit/state/apply/addWriter/addWriterWriterKeyMismatchScenario.js +0 -54
- package/tests/unit/state/apply/addWriter/addWriterWriterKeyOwnershipScenario.js +0 -54
- package/tests/unit/state/apply/addWriter/addWriterZeroWriterKeyScenario.js +0 -29
- package/tests/unit/state/apply/addWriter/state.apply.addWriter.test.js +0 -309
- package/tests/unit/state/apply/adminRecovery/adminRecoveryHappyPathScenario.js +0 -30
- package/tests/unit/state/apply/adminRecovery/adminRecoveryScenarioHelpers.js +0 -881
- package/tests/unit/state/apply/adminRecovery/state.apply.adminRecovery.test.js +0 -435
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistBanAndReapplyScenario.js +0 -78
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistExistingReaderHappyPathScenario.js +0 -98
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistFeeAfterDisableScenario.js +0 -66
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistHappyPathScenario.js +0 -55
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistInsufficientAdminBalanceScenario.js +0 -103
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistNodeAlreadyWhitelistedScenario.js +0 -60
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistScenarioHelpers.js +0 -189
- package/tests/unit/state/apply/appendWhitelist/state.apply.appendWhitelist.test.js +0 -220
- package/tests/unit/state/apply/balanceInitialization/balanceInitializationHappyPathScenario.js +0 -82
- package/tests/unit/state/apply/balanceInitialization/balanceInitializationScenarioHelpers.js +0 -111
- package/tests/unit/state/apply/balanceInitialization/invalidAmountScenario.js +0 -45
- package/tests/unit/state/apply/balanceInitialization/nodeEntryBalanceUpdateFailureScenario.js +0 -82
- package/tests/unit/state/apply/balanceInitialization/state.apply.balanceInitialization.test.js +0 -189
- package/tests/unit/state/apply/banValidator/banValidatorBanAndReWhitelistScenario.js +0 -156
- package/tests/unit/state/apply/banValidator/banValidatorHappyPathScenario.js +0 -36
- package/tests/unit/state/apply/banValidator/banValidatorScenarioHelpers.js +0 -531
- package/tests/unit/state/apply/banValidator/banValidatorSequentialBansScenario.js +0 -74
- package/tests/unit/state/apply/banValidator/banValidatorTargetDecodeFailureScenario.js +0 -19
- package/tests/unit/state/apply/banValidator/banValidatorTargetIndexerScenario.js +0 -32
- package/tests/unit/state/apply/banValidator/banValidatorTargetNodeEntryMissingScenario.js +0 -19
- package/tests/unit/state/apply/banValidator/banValidatorTargetRoleUpdateFailureScenario.js +0 -19
- package/tests/unit/state/apply/banValidator/banValidatorWhitelistedNonWriterScenario.js +0 -38
- package/tests/unit/state/apply/banValidator/banValidatorWhitelistedZeroBalanceScenario.js +0 -91
- package/tests/unit/state/apply/banValidator/banValidatorWithdrawFailureScenario.js +0 -19
- package/tests/unit/state/apply/banValidator/state.apply.banValidator.test.js +0 -266
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentDuplicateRegistrationScenario.js +0 -143
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentHappyPathScenario.js +0 -26
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentIncompleteOperationScenario.js +0 -94
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentInvalidDeploymentEntryScenario.js +0 -37
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentMultipleBootstrapScenario.js +0 -86
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentScenarioHelpers.js +0 -347
- package/tests/unit/state/apply/bootstrapDeployment/invalidValidatorNodeEntryScenario.js +0 -57
- package/tests/unit/state/apply/bootstrapDeployment/state.apply.bootstrapDeployment.test.js +0 -429
- package/tests/unit/state/apply/common/access-control/adminConsistencyMismatchScenario.js +0 -120
- package/tests/unit/state/apply/common/access-control/adminEntryDecodeFailureScenario.js +0 -130
- package/tests/unit/state/apply/common/access-control/adminEntryExistsScenario.js +0 -93
- package/tests/unit/state/apply/common/access-control/adminEntryMissingScenario.js +0 -108
- package/tests/unit/state/apply/common/access-control/adminOnlyGuardScenario.js +0 -126
- package/tests/unit/state/apply/common/access-control/adminPublicKeyDecodeFailureScenario.js +0 -121
- package/tests/unit/state/apply/common/access-control/roleAccessOperationValidationScenario.js +0 -50
- package/tests/unit/state/apply/common/adminControlOperationValidationScenario.js +0 -56
- package/tests/unit/state/apply/common/balances/adminEntryUpdateFailureScenario.js +0 -52
- package/tests/unit/state/apply/common/balances/base/requesterBalanceScenarioBase.js +0 -198
- package/tests/unit/state/apply/common/balances/feeDecodeFailureScenario.js +0 -52
- package/tests/unit/state/apply/common/balances/requesterBalanceDecodeFailureScenario.js +0 -15
- package/tests/unit/state/apply/common/balances/requesterBalanceFeeApplicationFailureScenario.js +0 -11
- package/tests/unit/state/apply/common/balances/requesterBalanceInsufficientScenario.js +0 -15
- package/tests/unit/state/apply/common/balances/requesterBalanceUpdateFailureScenario.js +0 -11
- package/tests/unit/state/apply/common/balances/validatorEntryRewardFailureScenario.js +0 -11
- package/tests/unit/state/apply/common/balances/validatorEntryUpdateFailureScenario.js +0 -11
- package/tests/unit/state/apply/common/balances/validatorNodeEntryDecodeFailureScenario.js +0 -40
- package/tests/unit/state/apply/common/base/OperationValidationScenarioBase.js +0 -114
- package/tests/unit/state/apply/common/commonScenarioHelper.js +0 -103
- package/tests/unit/state/apply/common/indexer/indexerNodeEntryDecodeFailureScenario.js +0 -36
- package/tests/unit/state/apply/common/indexer/indexerNodeEntryMissingScenario.js +0 -36
- package/tests/unit/state/apply/common/indexer/indexerRoleUpdateFailureScenario.js +0 -29
- package/tests/unit/state/apply/common/indexer/indexerSequenceStateInvalidScenario.js +0 -66
- package/tests/unit/state/apply/common/invalidMessageComponentValidationScenario.js +0 -84
- package/tests/unit/state/apply/common/nodeEntryInitializationFailureScenario.js +0 -47
- package/tests/unit/state/apply/common/operationAlreadyAppliedScenario.js +0 -85
- package/tests/unit/state/apply/common/payload-structure/addressWithInvalidPublicKeyScenario.js +0 -52
- package/tests/unit/state/apply/common/payload-structure/initializationDisabledScenario.js +0 -54
- package/tests/unit/state/apply/common/payload-structure/invalidAddressValidationScenario.js +0 -73
- package/tests/unit/state/apply/common/payload-structure/invalidHashValidationScenario.js +0 -71
- package/tests/unit/state/apply/common/payload-structure/invalidPayloadValidationScenario.js +0 -31
- package/tests/unit/state/apply/common/payload-structure/invalidSignatureValidationScenario.js +0 -142
- package/tests/unit/state/apply/common/payload-structure/partialOperationValidationScenario.js +0 -87
- package/tests/unit/state/apply/common/requester/requesterNodeEntryBufferMissingScenario.js +0 -71
- package/tests/unit/state/apply/common/requester/requesterNodeEntryDecodeFailureScenario.js +0 -73
- package/tests/unit/state/apply/common/requester/requesterNodeEntryMissingScenario.js +0 -36
- package/tests/unit/state/apply/common/requesterAddressValidationScenario.js +0 -44
- package/tests/unit/state/apply/common/requesterPublicKeyValidationScenario.js +0 -25
- package/tests/unit/state/apply/common/transactionValidityMismatchScenario.js +0 -98
- package/tests/unit/state/apply/common/validatorConsistency/base/validatorConsistencyScenarioBase.js +0 -202
- package/tests/unit/state/apply/common/validatorConsistency/validatorEntryDecodeFailureScenario.js +0 -17
- package/tests/unit/state/apply/common/validatorConsistency/validatorEntryMissingScenario.js +0 -44
- package/tests/unit/state/apply/common/validatorConsistency/validatorInactiveScenario.js +0 -19
- package/tests/unit/state/apply/common/validatorConsistency/validatorWriterKeyMismatchScenario.js +0 -18
- package/tests/unit/state/apply/common/validatorEntryValidation/base/validatorEntryValidationScenarioBase.js +0 -315
- package/tests/unit/state/apply/common/validatorEntryValidation/validatorEntryInvalidBalanceScenario.js +0 -18
- package/tests/unit/state/apply/common/writerKeyExistsValidationScenario.js +0 -43
- package/tests/unit/state/apply/disableInitialization/disableInitializationAlreadyDisabledScenario.js +0 -53
- package/tests/unit/state/apply/disableInitialization/disableInitializationHappyPathScenario.js +0 -24
- package/tests/unit/state/apply/disableInitialization/disableInitializationScenarioHelpers.js +0 -204
- package/tests/unit/state/apply/disableInitialization/state.apply.disableInitialization.test.js +0 -161
- package/tests/unit/state/apply/missing-tests.md +0 -18
- package/tests/unit/state/apply/removeIndexer/removeIndexerHappyPathScenario.js +0 -58
- package/tests/unit/state/apply/removeIndexer/removeIndexerReAddAndRemoveAgainScenario.js +0 -98
- package/tests/unit/state/apply/removeIndexer/removeIndexerRemoveMultipleIndexersScenario.js +0 -167
- package/tests/unit/state/apply/removeIndexer/removeIndexerScenarioHelpers.js +0 -429
- package/tests/unit/state/apply/removeIndexer/removeIndexerTargetNotIndexerScenario.js +0 -22
- package/tests/unit/state/apply/removeIndexer/removeIndexerWriterKeyMissingScenario.js +0 -20
- package/tests/unit/state/apply/removeIndexer/state.apply.removeIndexer.test.js +0 -291
- package/tests/unit/state/apply/removeWriter/removeWriterAndAddWriterAgainScenario.js +0 -87
- package/tests/unit/state/apply/removeWriter/removeWriterHappyPathScenario.js +0 -38
- package/tests/unit/state/apply/removeWriter/removeWriterInvalidValidatorSignatureScenario.js +0 -32
- package/tests/unit/state/apply/removeWriter/removeWriterRequesterBalanceInsufficientScenario.js +0 -21
- package/tests/unit/state/apply/removeWriter/removeWriterRequesterEntryDecodeFailureScenario.js +0 -19
- package/tests/unit/state/apply/removeWriter/removeWriterRequesterEntryMissingScenario.js +0 -19
- package/tests/unit/state/apply/removeWriter/removeWriterRequesterIndexerScenario.js +0 -21
- package/tests/unit/state/apply/removeWriter/removeWriterRequesterNotWriterScenario.js +0 -21
- package/tests/unit/state/apply/removeWriter/removeWriterRequesterRoleUpdateFailureScenario.js +0 -19
- package/tests/unit/state/apply/removeWriter/removeWriterScenarioHelpers.js +0 -348
- package/tests/unit/state/apply/removeWriter/removeWriterThroughWriterValidatorScenario.js +0 -113
- package/tests/unit/state/apply/removeWriter/removeWriterUnstakeFailureScenario.js +0 -33
- package/tests/unit/state/apply/removeWriter/removeWriterWriterKeyMismatchScenario.js +0 -21
- package/tests/unit/state/apply/removeWriter/removeWriterWriterKeyOwnershipScenario.js +0 -26
- package/tests/unit/state/apply/removeWriter/removeWriterWriterKeyRegistryMissingScenario.js +0 -22
- package/tests/unit/state/apply/removeWriter/state.apply.removeWriter.test.js +0 -307
- package/tests/unit/state/apply/state.apply.test.js +0 -24
- package/tests/unit/state/apply/transfer/state.apply.transfer.test.js +0 -819
- package/tests/unit/state/apply/transfer/transferContractSchemaValidationScenario.js +0 -22
- package/tests/unit/state/apply/transfer/transferDoubleSpendAcrossValidatorsScenario.js +0 -146
- package/tests/unit/state/apply/transfer/transferDoubleSpendSameBatchScenario.js +0 -63
- package/tests/unit/state/apply/transfer/transferDoubleSpendSingleValidatorScenario.js +0 -67
- package/tests/unit/state/apply/transfer/transferExistingRecipientAmountScenario.js +0 -31
- package/tests/unit/state/apply/transfer/transferExistingRecipientZeroAmountScenario.js +0 -31
- package/tests/unit/state/apply/transfer/transferHandlerGuardScenarios.js +0 -22
- package/tests/unit/state/apply/transfer/transferHappyPathScenario.js +0 -8
- package/tests/unit/state/apply/transfer/transferInvalidIncomingDataScenario.js +0 -66
- package/tests/unit/state/apply/transfer/transferNewRecipientAmountScenario.js +0 -31
- package/tests/unit/state/apply/transfer/transferNewRecipientZeroAmountScenario.js +0 -31
- package/tests/unit/state/apply/transfer/transferScenarioHelpers.js +0 -1170
- package/tests/unit/state/apply/transfer/transferSelfTransferAmountScenario.js +0 -38
- package/tests/unit/state/apply/transfer/transferSelfTransferZeroAmountScenario.js +0 -38
- package/tests/unit/state/apply/transfer/transferValidatorRecipientAmountScenario.js +0 -38
- package/tests/unit/state/apply/transfer/transferValidatorRecipientZeroAmountScenario.js +0 -38
- package/tests/unit/state/apply/txOperation/state.apply.txOperation.test.js +0 -318
- package/tests/unit/state/apply/txOperation/txOperationBootstrapNotRegisteredScenario.js +0 -70
- package/tests/unit/state/apply/txOperation/txOperationDifferentValidatorCreatorHappyPathScenario.js +0 -23
- package/tests/unit/state/apply/txOperation/txOperationInvalidDeploymentEntryScenario.js +0 -48
- package/tests/unit/state/apply/txOperation/txOperationInvalidFeeAmountScenario.js +0 -39
- package/tests/unit/state/apply/txOperation/txOperationInvalidSubnetCreatorAddressScenario.js +0 -46
- package/tests/unit/state/apply/txOperation/txOperationRequesterCreatorHappyPathScenario.js +0 -21
- package/tests/unit/state/apply/txOperation/txOperationScenarioHelpers.js +0 -432
- package/tests/unit/state/apply/txOperation/txOperationStandardHappyPathScenario.js +0 -21
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeAddCreatorBalanceFailureScenario.js +0 -26
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeAddValidatorBalanceFailureScenario.js +0 -25
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeAddValidatorBonusFailureScenario.js +0 -27
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeDecodeCreatorEntryScenario.js +0 -18
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeDecodeRequesterEntryScenario.js +0 -17
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeDecodeValidatorEntryScenario.js +0 -31
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeGuardBypassScenario.js +0 -49
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeGuardScenarioFactory.js +0 -93
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeInsufficientRequesterBalanceScenario.js +0 -28
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidCreatorBalanceScenario.js +0 -29
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidRequesterBalanceScenario.js +0 -28
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidRequesterEntryScenario.js +0 -17
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidValidatorBalanceScenario.js +0 -33
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeMissingCreatorEntryScenario.js +0 -18
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeSubtractFailureScenario.js +0 -25
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateCreatorBalanceFailureScenario.js +0 -26
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateFailureScenario.js +0 -25
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateValidatorBalanceFailureScenario.js +0 -26
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateValidatorBonusFailureScenario.js +0 -27
- package/tests/unit/state/apply/txOperation/txOperationValidatorCreatorHappyPathScenario.js +0 -21
- package/tests/unit/state/stateModule.test.js +0 -23
- package/tests/unit/state/stateTestUtils.js +0 -32
- package/tests/unit/state/utils/address.test.js +0 -63
- package/tests/unit/state/utils/adminEntry.test.js +0 -52
- package/tests/unit/state/utils/balance.test.js +0 -320
- package/tests/unit/state/utils/lengthEntry.test.js +0 -54
- package/tests/unit/state/utils/nodeEntry.test.js +0 -298
- package/tests/unit/state/utils/roles.test.js +0 -44
- package/tests/unit/unit.test.js +0 -14
- package/tests/unit/utils/amountSerialization/amountSerialization.test.js +0 -237
- package/tests/unit/utils/buffer/buffer.test.js +0 -251
- package/tests/unit/utils/check/adminControlOperation.test.js +0 -156
- package/tests/unit/utils/check/balanceInitializationOperation.test.js +0 -54
- package/tests/unit/utils/check/bootstrapDeploymentOperation.test.js +0 -104
- package/tests/unit/utils/check/check.test.js +0 -17
- package/tests/unit/utils/check/common.test.js +0 -419
- package/tests/unit/utils/check/coreAdminOperation.test.js +0 -95
- package/tests/unit/utils/check/roleAccessOperation.test.js +0 -255
- package/tests/unit/utils/check/transactionOperation.test.js +0 -107
- package/tests/unit/utils/check/transferOperation.test.js +0 -102
- package/tests/unit/utils/deepEqualApplyPayload/deepEqualApplyPayload.test.js +0 -102
- package/tests/unit/utils/fileUtils/readAddressesFromWhitelistFile.test.js +0 -95
- package/tests/unit/utils/fileUtils/readBalanceMigrationFile.test.js +0 -149
- package/tests/unit/utils/helpers/helpers.test.js +0 -11
- package/tests/unit/utils/helpers/isHexString.test.js +0 -12
- package/tests/unit/utils/helpers/normalizeHex.test.js +0 -82
- package/tests/unit/utils/migrationUtils/validateAddressFromIncomingFile.test.js +0 -100
- package/tests/unit/utils/normalizers/normalizers.test.js +0 -469
- package/tests/unit/utils/protobuf/applyOperations.test.js +0 -91
- package/tests/unit/utils/protobuf/operationHelpers.test.js +0 -303
- package/tests/unit/utils/protobuf/protobuf.test.js +0 -12
- package/tests/unit/utils/type/type.test.js +0 -25
- package/tests/unit/utils/utils.test.js +0 -21
- package/whitelist/.gitkeep +0 -0
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import test from 'brittle';
|
|
2
|
-
import roles from '../../../../src/core/state/utils/roles.js';
|
|
3
|
-
|
|
4
|
-
const { NodeRole, WHITELISTED_MASK, WRITER_MASK, INDEXER_MASK, calculateNodeRole, isNodeRoleValid } = roles;
|
|
5
|
-
|
|
6
|
-
test('NodeRole enum values', t => {
|
|
7
|
-
t.is(NodeRole.READER, 0x0);
|
|
8
|
-
t.is(NodeRole.WHITELISTED, 0x1);
|
|
9
|
-
t.is(NodeRole.WRITER, 0x3);
|
|
10
|
-
t.is(NodeRole.INDEXER, 0x7);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test('Bitmask constants', t => {
|
|
14
|
-
t.is(WHITELISTED_MASK, 0x1);
|
|
15
|
-
t.is(WRITER_MASK, 0x2);
|
|
16
|
-
t.is(INDEXER_MASK, 0x4);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test('calculateNodeRole returns correct results', t => {
|
|
20
|
-
t.is(calculateNodeRole({}), NodeRole.READER);
|
|
21
|
-
t.is(calculateNodeRole({ isWhitelisted: true }), NodeRole.WHITELISTED);
|
|
22
|
-
t.is(calculateNodeRole({ isWhitelisted: true, isWriter: true }), NodeRole.WRITER);
|
|
23
|
-
t.is(calculateNodeRole({ isWhitelisted: true, isWriter: true, isIndexer: true }), NodeRole.INDEXER);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test('calculateNodeRole returns correct bitmask for partial roles', t => {
|
|
27
|
-
t.is(calculateNodeRole({ isWriter: true }), NodeRole.READER | WRITER_MASK);
|
|
28
|
-
t.is(calculateNodeRole({ isIndexer: true }), NodeRole.READER | INDEXER_MASK);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('isNodeRoleValid returns true for valid roles', t => {
|
|
32
|
-
t.ok(isNodeRoleValid(NodeRole.READER));
|
|
33
|
-
t.ok(isNodeRoleValid(NodeRole.WHITELISTED));
|
|
34
|
-
t.ok(isNodeRoleValid(NodeRole.WRITER));
|
|
35
|
-
t.ok(isNodeRoleValid(NodeRole.INDEXER));
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test('isNodeRoleValid returns false for invalid roles', t => {
|
|
39
|
-
t.not(isNodeRoleValid(0x2));
|
|
40
|
-
t.not(isNodeRoleValid(0x4));
|
|
41
|
-
t.not(isNodeRoleValid(0x5));
|
|
42
|
-
t.not(isNodeRoleValid(-1));
|
|
43
|
-
t.not(isNodeRoleValid(999));
|
|
44
|
-
});
|
package/tests/unit/unit.test.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// This runner is auto-generated by Brittle
|
|
2
|
-
|
|
3
|
-
import { default as test } from 'brittle';
|
|
4
|
-
|
|
5
|
-
async function runTests() {
|
|
6
|
-
test.pause();
|
|
7
|
-
await import('./utils/utils.test.js');
|
|
8
|
-
await import('./messages/messages.test.js');
|
|
9
|
-
await import('./network/networkModule.test.js')
|
|
10
|
-
await import('./state/stateModule.test.js');
|
|
11
|
-
test.resume();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
await runTests();
|
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
import test from 'brittle';
|
|
2
|
-
import b4a from 'b4a';
|
|
3
|
-
import { decimalStringToBigInt, bigIntTo16ByteBuffer, bufferToBigInt, bigIntToDecimalString, licenseBufferToBigInt } from '../../../../src/utils/amountSerialization.js';
|
|
4
|
-
import { errorMessageIncludes } from "../../../helpers/regexHelper.js";
|
|
5
|
-
import lengthEntryUtils from '../../../../src/core/state/utils/lengthEntry.js';
|
|
6
|
-
|
|
7
|
-
test('decimalStringToBigInt', async t => {
|
|
8
|
-
// Zero cases - all valid
|
|
9
|
-
t.is(decimalStringToBigInt('0'), 0n, 'Simple zero');
|
|
10
|
-
t.is(decimalStringToBigInt('0.0'), 0n, 'Zero with decimal point');
|
|
11
|
-
t.is(decimalStringToBigInt('00.00'), 0n, 'Zero with leading and trailing zeros');
|
|
12
|
-
|
|
13
|
-
// Valid cases
|
|
14
|
-
t.is(decimalStringToBigInt('123'), 123000000000000000000n, 'Simple integer');
|
|
15
|
-
t.is(decimalStringToBigInt('123.456'), 123456000000000000000n, 'Decimal number');
|
|
16
|
-
t.is(decimalStringToBigInt('0.1'), 100000000000000000n, 'Decimal less than 1');
|
|
17
|
-
t.is(decimalStringToBigInt('0.000000000000000001'), 1n, 'Smallest possible decimal');
|
|
18
|
-
t.is(decimalStringToBigInt('1000000.000000000000000000'), 1000000000000000000000000n, 'Large number');
|
|
19
|
-
t.is(decimalStringToBigInt('340282366920938463463.374607431768211455'), (2n ** 128n - 1n), 'Maximum allowed value');
|
|
20
|
-
|
|
21
|
-
// Invalid cases
|
|
22
|
-
await Promise.all([
|
|
23
|
-
t.exception.all(() => decimalStringToBigInt(123),
|
|
24
|
-
errorMessageIncludes('Input must be a string')),
|
|
25
|
-
t.exception(() => decimalStringToBigInt('-1'),
|
|
26
|
-
errorMessageIncludes('Negative amounts are not allowed')),
|
|
27
|
-
t.exception(() => decimalStringToBigInt('-0.1'),
|
|
28
|
-
errorMessageIncludes('Negative amounts are not allowed')),
|
|
29
|
-
t.exception(() => decimalStringToBigInt('-123.456'),
|
|
30
|
-
errorMessageIncludes('Negative amounts are not allowed')),
|
|
31
|
-
t.exception(() => decimalStringToBigInt('-0'),
|
|
32
|
-
errorMessageIncludes('Negative amounts are not allowed')),
|
|
33
|
-
t.exception(() => decimalStringToBigInt('abc'),
|
|
34
|
-
errorMessageIncludes('Invalid decimal format. Use format: 123.456')),
|
|
35
|
-
t.exception(() => decimalStringToBigInt('123.456.789'),
|
|
36
|
-
errorMessageIncludes('Invalid decimal format. Use format: 123.456')),
|
|
37
|
-
t.exception(() => decimalStringToBigInt('123.1234567890123456789'),
|
|
38
|
-
errorMessageIncludes('Too many decimal places. Maximum allowed: 18')),
|
|
39
|
-
t.exception(() => decimalStringToBigInt(''),
|
|
40
|
-
errorMessageIncludes('Invalid decimal format. Use format: 123.456')),
|
|
41
|
-
t.exception(() => decimalStringToBigInt(' '),
|
|
42
|
-
errorMessageIncludes('Invalid decimal format. Use format: 123.456')),
|
|
43
|
-
t.exception(() => decimalStringToBigInt('12..34'),
|
|
44
|
-
errorMessageIncludes('Invalid decimal format. Use format: 123.456')),
|
|
45
|
-
t.exception(() => decimalStringToBigInt('.123'),
|
|
46
|
-
errorMessageIncludes('Invalid decimal format. Use format: 123.456')),
|
|
47
|
-
t.exception(() => decimalStringToBigInt('340282366920938463463.374607431768211456'),
|
|
48
|
-
errorMessageIncludes('Amount exceeds maximum allowed value (2^128 - 1)')),
|
|
49
|
-
t.exception(() => decimalStringToBigInt('999999999999999999999999999999999999999'),
|
|
50
|
-
errorMessageIncludes('Amount exceeds maximum allowed value (2^128 - 1)'))
|
|
51
|
-
]);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test('bigIntTo16ByteBuffer', async t => {
|
|
55
|
-
// Basic conversion tests
|
|
56
|
-
const zeroResult = bigIntTo16ByteBuffer(0n);
|
|
57
|
-
t.is(zeroResult.toString('hex'), '00000000000000000000000000000000', 'Should convert 0 to a buffer of all zeros');
|
|
58
|
-
|
|
59
|
-
const oneResult = bigIntTo16ByteBuffer(1n);
|
|
60
|
-
t.is(oneResult.toString('hex'), '00000000000000000000000000000001', 'Should convert 1 to a buffer of zeros with 1 at the end');
|
|
61
|
-
|
|
62
|
-
const number256Result = bigIntTo16ByteBuffer(256n);
|
|
63
|
-
t.is(number256Result.toString('hex'), '00000000000000000000000000000100', 'Should convert 256 to zeros with "0100" at the end (256 in hex)');
|
|
64
|
-
|
|
65
|
-
const maxValueResult = bigIntTo16ByteBuffer(2n ** 128n - 1n);
|
|
66
|
-
t.is(maxValueResult.toString('hex'), 'ffffffffffffffffffffffffffffffff', 'Should convert max value to all f\'s');
|
|
67
|
-
|
|
68
|
-
const specificNumberResult = bigIntTo16ByteBuffer(123456789n);
|
|
69
|
-
t.is(specificNumberResult.toString('hex'), '000000000000000000000000075bcd15', 'Should convert 123456789 to its proper hex representation');
|
|
70
|
-
|
|
71
|
-
const overflowResult = bigIntTo16ByteBuffer(2n ** 128n + 5n);
|
|
72
|
-
t.is(overflowResult.toString('hex'), '00000000000000000000000000000005', 'Number larger than 2^128 should wrap around to modulo');
|
|
73
|
-
|
|
74
|
-
// Error cases tests
|
|
75
|
-
await Promise.all([
|
|
76
|
-
t.exception.all(() => bigIntTo16ByteBuffer(123),
|
|
77
|
-
errorMessageIncludes('Input must be a BigInt')),
|
|
78
|
-
t.exception.all(() => bigIntTo16ByteBuffer('123'),
|
|
79
|
-
errorMessageIncludes('Input must be a BigInt')),
|
|
80
|
-
t.exception.all(() => bigIntTo16ByteBuffer(null),
|
|
81
|
-
errorMessageIncludes('Input must be a BigInt')),
|
|
82
|
-
t.exception.all(() => bigIntTo16ByteBuffer(undefined),
|
|
83
|
-
errorMessageIncludes('Input must be a BigInt'))
|
|
84
|
-
]);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test('bufferToBigInt', async t => {
|
|
88
|
-
// Basic conversion tests
|
|
89
|
-
const zeroBuffer = b4a.from('00000000000000000000000000000000', 'hex');
|
|
90
|
-
t.is(bufferToBigInt(zeroBuffer), 0n, 'Should convert buffer of zeros to 0');
|
|
91
|
-
|
|
92
|
-
const oneBuffer = b4a.from('00000000000000000000000000000001', 'hex');
|
|
93
|
-
t.is(bufferToBigInt(oneBuffer), 1n, 'Should convert buffer with 1 at the end to 1');
|
|
94
|
-
|
|
95
|
-
const number256Buffer = b4a.from('00000000000000000000000000000100', 'hex');
|
|
96
|
-
t.is(bufferToBigInt(number256Buffer), 256n, 'Should convert buffer representing 256 (hex: 0100)');
|
|
97
|
-
|
|
98
|
-
const maxValueBuffer = b4a.from('ffffffffffffffffffffffffffffffff', 'hex');
|
|
99
|
-
t.is(bufferToBigInt(maxValueBuffer), (2n ** 128n - 1n), 'Should convert buffer of all f\'s to max value');
|
|
100
|
-
|
|
101
|
-
const specificNumberBuffer = b4a.from('000000000000000000000000075bcd15', 'hex');
|
|
102
|
-
t.is(bufferToBigInt(specificNumberBuffer), 123456789n, 'Should convert buffer to specific number (123456789)');
|
|
103
|
-
|
|
104
|
-
// Roundtrip test - verify that conversion works both ways
|
|
105
|
-
const originalValue = 987654321n;
|
|
106
|
-
const buffer = bigIntTo16ByteBuffer(originalValue);
|
|
107
|
-
const roundtripped = bufferToBigInt(buffer);
|
|
108
|
-
t.is(roundtripped, originalValue, 'Should preserve value through buffer conversion roundtrip');
|
|
109
|
-
|
|
110
|
-
// Error cases tests
|
|
111
|
-
await Promise.all([
|
|
112
|
-
t.exception.all(() => bufferToBigInt(b4a.alloc(15)),
|
|
113
|
-
errorMessageIncludes('Input must be a 16-byte Buffer')),
|
|
114
|
-
t.exception.all(() => bufferToBigInt(b4a.alloc(17)),
|
|
115
|
-
errorMessageIncludes('Input must be a 16-byte Buffer')),
|
|
116
|
-
t.exception.all(() => bufferToBigInt(null),
|
|
117
|
-
errorMessageIncludes('Input must be a 16-byte Buffer')),
|
|
118
|
-
t.exception.all(() => bufferToBigInt(undefined),
|
|
119
|
-
errorMessageIncludes('Input must be a 16-byte Buffer')),
|
|
120
|
-
t.exception.all(() => bufferToBigInt('not a buffer'),
|
|
121
|
-
errorMessageIncludes('Input must be a 16-byte Buffer')),
|
|
122
|
-
t.exception.all(() => bufferToBigInt([1, 2, 3, 4]),
|
|
123
|
-
errorMessageIncludes('Input must be a 16-byte Buffer'))
|
|
124
|
-
]);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
test('licenseBufferToBigInt', async t => {
|
|
128
|
-
// Basic conversion tests
|
|
129
|
-
const zeroBuffer = b4a.from('00000000', 'hex');
|
|
130
|
-
t.is(licenseBufferToBigInt(zeroBuffer), 0n, 'Should convert buffer of zeros to 0');
|
|
131
|
-
|
|
132
|
-
const oneBuffer = b4a.from('00000001', 'hex');
|
|
133
|
-
t.is(licenseBufferToBigInt(oneBuffer), 1n, 'Should convert BE buffer with 1 at end to 1');
|
|
134
|
-
|
|
135
|
-
const number256Buffer = b4a.from('00000100', 'hex');
|
|
136
|
-
t.is(licenseBufferToBigInt(number256Buffer), 256n, 'Should convert BE buffer representing 256');
|
|
137
|
-
|
|
138
|
-
const maxValueBuffer = b4a.from('ffffffff', 'hex');
|
|
139
|
-
t.is(licenseBufferToBigInt(maxValueBuffer), (2n ** 32n - 1n), 'Should convert buffer of all f\'s to max 32-bit value');
|
|
140
|
-
|
|
141
|
-
const specificNumberBuffer = b4a.from('075bcd15', 'hex');
|
|
142
|
-
t.is(licenseBufferToBigInt(specificNumberBuffer), 123456789n, 'Should convert BE buffer to specific number (123456789)');
|
|
143
|
-
|
|
144
|
-
// Roundtrip test
|
|
145
|
-
const originalValue = 987654321n;
|
|
146
|
-
const buffer = lengthEntryUtils.encodeBE(Number(originalValue));
|
|
147
|
-
const roundtripped = licenseBufferToBigInt(buffer);
|
|
148
|
-
t.is(roundtripped, originalValue, 'Should preserve value through buffer conversion roundtrip');
|
|
149
|
-
|
|
150
|
-
// Error cases tests
|
|
151
|
-
await Promise.all([
|
|
152
|
-
t.exception.all(() => licenseBufferToBigInt(b4a.alloc(15)),
|
|
153
|
-
errorMessageIncludes('Input must be a 4-byte Buffer')),
|
|
154
|
-
t.exception.all(() => licenseBufferToBigInt(b4a.alloc(17)),
|
|
155
|
-
errorMessageIncludes('Input must be a 4-byte Buffer')),
|
|
156
|
-
t.exception.all(() => licenseBufferToBigInt(null),
|
|
157
|
-
errorMessageIncludes('Input must be a 4-byte Buffer')),
|
|
158
|
-
t.exception.all(() => licenseBufferToBigInt(undefined),
|
|
159
|
-
errorMessageIncludes('Input must be a 4-byte Buffer')),
|
|
160
|
-
t.exception.all(() => licenseBufferToBigInt('not a buffer'),
|
|
161
|
-
errorMessageIncludes('Input must be a 4-byte Buffer')),
|
|
162
|
-
t.exception.all(() => licenseBufferToBigInt([1, 2, 3, 4]),
|
|
163
|
-
errorMessageIncludes('Input must be a 4-byte Buffer'))
|
|
164
|
-
]);
|
|
165
|
-
})
|
|
166
|
-
|
|
167
|
-
test('Integration: amount serialization roundtrip', async t => {
|
|
168
|
-
// Test regular integer value
|
|
169
|
-
const regularInt = '1';
|
|
170
|
-
const regularIntBigInt = decimalStringToBigInt(regularInt);
|
|
171
|
-
const regularIntBuffer = bigIntTo16ByteBuffer(regularIntBigInt);
|
|
172
|
-
const regularIntRoundtrip = bufferToBigInt(regularIntBuffer);
|
|
173
|
-
t.is(regularIntRoundtrip, regularIntBigInt, 'Should preserve regular integer through conversion');
|
|
174
|
-
|
|
175
|
-
// Test decimal value
|
|
176
|
-
const decimalValue = '123.456';
|
|
177
|
-
const decimalBigInt = decimalStringToBigInt(decimalValue);
|
|
178
|
-
const decimalBuffer = bigIntTo16ByteBuffer(decimalBigInt);
|
|
179
|
-
const decimalRoundtrip = bufferToBigInt(decimalBuffer);
|
|
180
|
-
t.is(decimalRoundtrip, decimalBigInt, 'Should preserve decimal value through conversion');
|
|
181
|
-
|
|
182
|
-
// Test small decimal value
|
|
183
|
-
const smallDecimal = '0.1';
|
|
184
|
-
const smallDecimalBigInt = decimalStringToBigInt(smallDecimal);
|
|
185
|
-
const smallDecimalBuffer = bigIntTo16ByteBuffer(smallDecimalBigInt);
|
|
186
|
-
const smallDecimalRoundtrip = bufferToBigInt(smallDecimalBuffer);
|
|
187
|
-
t.is(smallDecimalRoundtrip, smallDecimalBigInt, 'Should preserve small decimal through conversion');
|
|
188
|
-
|
|
189
|
-
// Test large value with many decimal places
|
|
190
|
-
const largeValue = '9999999.999999999999999999';
|
|
191
|
-
const largeValueBigInt = decimalStringToBigInt(largeValue);
|
|
192
|
-
const largeValueBuffer = bigIntTo16ByteBuffer(largeValueBigInt);
|
|
193
|
-
const largeValueRoundtrip = bufferToBigInt(largeValueBuffer);
|
|
194
|
-
t.is(largeValueRoundtrip, largeValueBigInt, 'Should preserve large value with many decimals through conversion');
|
|
195
|
-
|
|
196
|
-
// Test smallest possible decimal
|
|
197
|
-
const smallestValue = '0.000000000000000001';
|
|
198
|
-
const smallestValueBigInt = decimalStringToBigInt(smallestValue);
|
|
199
|
-
const smallestValueBuffer = bigIntTo16ByteBuffer(smallestValueBigInt);
|
|
200
|
-
const smallestValueRoundtrip = bufferToBigInt(smallestValueBuffer);
|
|
201
|
-
t.is(smallestValueRoundtrip, smallestValueBigInt, 'Should preserve smallest possible decimal through conversion');
|
|
202
|
-
|
|
203
|
-
// Test maximum allowed value
|
|
204
|
-
const maxValue = '340282366920938463463.374607431768211455';
|
|
205
|
-
const maxValueBigInt = decimalStringToBigInt(maxValue);
|
|
206
|
-
const maxValueBuffer = bigIntTo16ByteBuffer(maxValueBigInt);
|
|
207
|
-
const maxValueRoundtrip = bufferToBigInt(maxValueBuffer);
|
|
208
|
-
t.is(maxValueRoundtrip, maxValueBigInt, 'Should preserve maximum allowed value through conversion');
|
|
209
|
-
|
|
210
|
-
// Verify that zero is handled correctly
|
|
211
|
-
const zeroBigInt = decimalStringToBigInt('0');
|
|
212
|
-
const zeroBuffer = bigIntTo16ByteBuffer(zeroBigInt);
|
|
213
|
-
const zeroRoundtrip = bufferToBigInt(zeroBuffer);
|
|
214
|
-
t.is(zeroRoundtrip, 0n, 'Should preserve zero through conversion');
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
test('bigIntToDecimalString', async t => {
|
|
218
|
-
t.is(bigIntToDecimalString(1234567890000000000000000000n), '1234567890');
|
|
219
|
-
t.is(bigIntToDecimalString(123456789012345678901234567890n), '123456789012.34567890123456789');
|
|
220
|
-
t.is(bigIntToDecimalString(1n), '0.000000000000000001');
|
|
221
|
-
t.is(bigIntToDecimalString(0n), '0');
|
|
222
|
-
t.is(bigIntToDecimalString(1000000000000000000n), '1');
|
|
223
|
-
t.is(bigIntToDecimalString(1000000000000000001n), '1.000000000000000001');
|
|
224
|
-
|
|
225
|
-
await Promise.all([
|
|
226
|
-
t.exception.all(() => bigIntToDecimalString(-1n),
|
|
227
|
-
errorMessageIncludes('Negative amounts are not allowed')),
|
|
228
|
-
t.exception.all(() => bigIntToDecimalString(2n ** 128n),
|
|
229
|
-
errorMessageIncludes('Amount exceeds maximum allowed value')),
|
|
230
|
-
t.exception.all(() => bigIntToDecimalString(1n, -1),
|
|
231
|
-
errorMessageIncludes('Decimals must be a non-negative integer')),
|
|
232
|
-
t.exception.all(() => bigIntToDecimalString(1n, 18.5),
|
|
233
|
-
errorMessageIncludes('Decimals must be a non-negative integer')),
|
|
234
|
-
t.exception.all(() => bigIntToDecimalString('123n'),
|
|
235
|
-
errorMessageIncludes('Input must be a BigInt'))
|
|
236
|
-
]);
|
|
237
|
-
});
|
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import test from 'brittle';
|
|
2
|
-
import b4a from 'b4a';
|
|
3
|
-
import { createMessage, isBufferValid, safeWriteUInt32BE, deepCopyBuffer, encodeCapabilities, timestampToBuffer, idToBuffer } from '../../../../src/utils/buffer.js';
|
|
4
|
-
import { errorMessageIncludes } from "../../../helpers/regexHelper.js";
|
|
5
|
-
|
|
6
|
-
const invalidDataTypes = [
|
|
7
|
-
null,
|
|
8
|
-
undefined,
|
|
9
|
-
true,
|
|
10
|
-
false,
|
|
11
|
-
0,
|
|
12
|
-
NaN,
|
|
13
|
-
Infinity,
|
|
14
|
-
-1,
|
|
15
|
-
'',
|
|
16
|
-
'string',
|
|
17
|
-
Symbol('sym'),
|
|
18
|
-
BigInt(10),
|
|
19
|
-
[],
|
|
20
|
-
{},
|
|
21
|
-
() => { },
|
|
22
|
-
new Date(),
|
|
23
|
-
{ foo: 'bar' },
|
|
24
|
-
{ type: '1', key: b4a.from('01', 'hex') },
|
|
25
|
-
{ type: 1, key: null },
|
|
26
|
-
{ type: 1 },
|
|
27
|
-
{ key: b4a.from('01', 'hex') },
|
|
28
|
-
{ type: 5, key: [] },
|
|
29
|
-
{ type: 1, key: b4a.from('01', 'hex'), bko: null },
|
|
30
|
-
{ type: 1, key: b4a.from('01', 'hex'), bko: 123 },
|
|
31
|
-
{ type: 1, key: b4a.from('01', 'hex'), bko: b4a.from('01', 'hex'), data: 'string' },
|
|
32
|
-
{ type: 1, key: b4a.from('01', 'hex'), bko: b4a.from('01', 'hex'), data: null },
|
|
33
|
-
{ type: 1, key: b4a.from('01', 'hex'), memo: 123 },
|
|
34
|
-
{ type: 1, key: 'string' },
|
|
35
|
-
{ type: 1, key: {}, bko: {} },
|
|
36
|
-
{ type: 'foo', key: [], bko: 'bar' },
|
|
37
|
-
(() => { const a = {}; a.self = a; return a; })(),
|
|
38
|
-
Object.create(null),
|
|
39
|
-
new Map(),
|
|
40
|
-
new Set(),
|
|
41
|
-
new Float64Array([1.1, 2.2]),
|
|
42
|
-
new Int16Array([1, 2, 3]),
|
|
43
|
-
{ type: 9999, key: b4a.from('01', 'hex') },
|
|
44
|
-
{ type: 1, key: 'not-a-buffer' },
|
|
45
|
-
Number.MAX_SAFE_INTEGER + 1,
|
|
46
|
-
-Number.MAX_SAFE_INTEGER - 1,
|
|
47
|
-
{ type: 1, key: b4a.from('01', 'hex'), callback: () => { } },
|
|
48
|
-
];
|
|
49
|
-
|
|
50
|
-
test('createMessage', async (t) => {
|
|
51
|
-
const expected = b4a.alloc(32);
|
|
52
|
-
for (let i = 0; i < 32; i++) expected[i] = i;
|
|
53
|
-
|
|
54
|
-
const buf1 = expected.subarray(0, 8);
|
|
55
|
-
const buf2 = expected.subarray(8, 16);
|
|
56
|
-
const buf3 = expected.subarray(16, 24);
|
|
57
|
-
const buf4 = expected.subarray(24, 32);
|
|
58
|
-
|
|
59
|
-
t.test('createMessage returns concatenated buffer for multiple buffers', async k => {
|
|
60
|
-
const result = createMessage(buf1, buf2, buf3, buf4);
|
|
61
|
-
k.is(result.length, expected.length, 'length matches');
|
|
62
|
-
k.ok(b4a.equals(result, expected), 'contents match');
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
t.test('createMessage encodes multiple numbers as BE UInt32 buffers', async k => {
|
|
66
|
-
const nums = Array.from({ length: 8 }, (_, i) => i + 1);
|
|
67
|
-
const part1 = nums.slice(0, 2); // [1,2] - 8 bytes
|
|
68
|
-
const part2 = nums.slice(2, 4); // [3,4]
|
|
69
|
-
const expectedNums = b4a.alloc(16);
|
|
70
|
-
part1.forEach((n, i) => expectedNums.writeUInt32BE(n, i * 4));
|
|
71
|
-
part2.forEach((n, i) => expectedNums.writeUInt32BE(n, 8 + i * 4));
|
|
72
|
-
const result = createMessage(...part1, ...part2);
|
|
73
|
-
k.is(result.length, 16, '16 bytes for 4 numbers');
|
|
74
|
-
k.ok(b4a.equals(result.subarray(0, 16), expectedNums), 'BE encoding correct');
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
t.test('createMessage handles mixed buffer and number arguments', async k => {
|
|
78
|
-
const num = 0x01020304;
|
|
79
|
-
const result = createMessage(buf1, num, buf2);
|
|
80
|
-
k.is(result.length, 8 + 4 + 8, 'combined length');
|
|
81
|
-
k.ok(b4a.equals(result.subarray(0, 8), buf1), 'first segment');
|
|
82
|
-
k.is(result.readUInt32BE(8), num, 'middle segment number');
|
|
83
|
-
k.ok(b4a.equals(result.subarray(12, 20), buf2), 'last segment');
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
t.test('createMessage returns empty buffer for no arguments', async k => {
|
|
87
|
-
const result = createMessage();
|
|
88
|
-
k.is(result.length, 0, 'zero length');
|
|
89
|
-
k.ok(b4a.isBuffer(result), 'still a buffer');
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test('createMessage returns empty buffer for invalid input types', async t => {
|
|
93
|
-
|
|
94
|
-
for (const invalidDataType of invalidDataTypes) {
|
|
95
|
-
const res = createMessage(invalidDataType);
|
|
96
|
-
t.is(res.length, 0, `empty for ${Object.prototype.toString.call(invalidDataType)}`);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test('createMessage returns buffer unchanged if single buffer is passed', async t => {
|
|
101
|
-
const buf = b4a.from([1, 2, 3]);
|
|
102
|
-
const res = createMessage(buf);
|
|
103
|
-
t.is(res.length, buf.length);
|
|
104
|
-
t.ok(b4a.equals(res, buf));
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
test('createMessage encodes valid numbers as 4-byte BE buffers', async t => {
|
|
108
|
-
const valid = [1, 256, 0xFFFFFFFF];
|
|
109
|
-
for (const n of valid) {
|
|
110
|
-
const res = createMessage(n);
|
|
111
|
-
t.is(res.length, 4, `4 bytes for ${n}`);
|
|
112
|
-
t.is(res.readUInt32BE(0), n);
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
// TODO: This tests is working on node but not on bare. Investigate and reactivate
|
|
117
|
-
// t.test('createMessage - function throws an error when invalid arguments are passed', async (k) => {
|
|
118
|
-
// try {
|
|
119
|
-
// const res = createMessage(1, 2, 3, 4);
|
|
120
|
-
// k.fail('Should throw an error for invalid arguments. Returned: ' + res);
|
|
121
|
-
// } catch (e) {
|
|
122
|
-
// k.pass('Correctly threw an error for invalid arguments');
|
|
123
|
-
// }
|
|
124
|
-
// });
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
test('isBufferValid - positive cases', t => {
|
|
128
|
-
const buf = b4a.alloc(8);
|
|
129
|
-
t.ok(isBufferValid(buf, 8), 'valid buffer and size');
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
test('isBufferValid - negative case', t => {
|
|
133
|
-
const buf = b4a.alloc(8);
|
|
134
|
-
t.not(isBufferValid(buf, 4), 'invalid size');
|
|
135
|
-
t.not(isBufferValid(b4a.alloc(0), 1), 'empty buffer, size 1');
|
|
136
|
-
t.not(isBufferValid(b4a.from('abcd', 'utf8'), 10), 'buffer too short');
|
|
137
|
-
t.not(isBufferValid(b4a.from('abcd', 'utf8'), 2), 'buffer too long');
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
test('safeWriteUInt32BE - positive case', t => {
|
|
141
|
-
const buf = safeWriteUInt32BE(0x01020304, 0);
|
|
142
|
-
t.ok(b4a.isBuffer(buf), 'returns a buffer');
|
|
143
|
-
t.is(buf.length, 4, 'buffer is 4 bytes');
|
|
144
|
-
t.is(buf.readUInt32BE(0), 0x01020304, 'encodes value correctly');
|
|
145
|
-
|
|
146
|
-
const edge = safeWriteUInt32BE(0x01020304, 4);
|
|
147
|
-
t.ok(b4a.isBuffer(edge), 'returns a buffer for offset at buffer end');
|
|
148
|
-
t.is(edge.length, 4, 'buffer is 4 bytes');
|
|
149
|
-
t.is(edge.readUInt32BE(0), 0, 'buffer is zeroed for out of bounds offset');
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
test('safeWriteUInt32BE - negative case', t => {
|
|
153
|
-
const negOffset = safeWriteUInt32BE(0x01020304, -1);
|
|
154
|
-
t.ok(b4a.isBuffer(negOffset), 'returns buffer for negative offset');
|
|
155
|
-
t.is(negOffset.length, 4, 'buffer is 4 bytes');
|
|
156
|
-
t.is(negOffset.readUInt32BE(0), 0, 'buffer is zeroed for negative offset');
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
test('deepCopyBuffer - returns null for falsy inputs', t => {
|
|
160
|
-
t.is(deepCopyBuffer(null), null, 'null input returns null');
|
|
161
|
-
t.is(deepCopyBuffer(undefined), null, 'undefined input returns null');
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
test('deepCopyBuffer - copies non-empty buffer', t => {
|
|
165
|
-
const buf = b4a.from([1, 2, 3, 4]);
|
|
166
|
-
const copy = deepCopyBuffer(buf);
|
|
167
|
-
|
|
168
|
-
t.ok(b4a.isBuffer(copy), 'returns a buffer');
|
|
169
|
-
t.is(copy.length, buf.length, 'same length');
|
|
170
|
-
t.ok(b4a.equals(copy, buf), 'contents match');
|
|
171
|
-
t.not(copy, buf, 'is a different buffer object');
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
test('deepCopyBuffer - copies empty buffer', t => {
|
|
175
|
-
const buf = b4a.alloc(0);
|
|
176
|
-
const copy = deepCopyBuffer(buf);
|
|
177
|
-
|
|
178
|
-
t.ok(b4a.isBuffer(copy), 'returns a buffer');
|
|
179
|
-
t.is(copy.length, 0, 'length is zero');
|
|
180
|
-
t.not(copy, buf, 'is a different buffer object');
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
test('deepCopyBuffer - modifying copy does not affect original (is not a reference)', t => {
|
|
184
|
-
const buf = b4a.from([9, 9, 9]);
|
|
185
|
-
const copy = deepCopyBuffer(buf);
|
|
186
|
-
|
|
187
|
-
copy[0] = 1;
|
|
188
|
-
|
|
189
|
-
t.is(buf[0], 9, 'original unchanged');
|
|
190
|
-
t.is(copy[0], 1, 'copy modified independently');
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
test('encodeCapabilities - deterministic ordering and encoding', t => {
|
|
194
|
-
const caps = ['cap-b', 'cap-a'];
|
|
195
|
-
const result = encodeCapabilities(caps);
|
|
196
|
-
|
|
197
|
-
const capA = b4a.from('cap-a', 'utf8');
|
|
198
|
-
const capB = b4a.from('cap-b', 'utf8');
|
|
199
|
-
|
|
200
|
-
const expected = b4a.concat([
|
|
201
|
-
b4a.from([0x00, capA.length]),
|
|
202
|
-
capA,
|
|
203
|
-
b4a.from([0x00, capB.length]),
|
|
204
|
-
capB
|
|
205
|
-
]);
|
|
206
|
-
|
|
207
|
-
t.is(result.length, expected.length, 'length matches');
|
|
208
|
-
t.ok(b4a.equals(result, expected), 'ordering is sorted and encoding matches');
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
test('encodeCapabilities - empty array yields empty buffer', t => {
|
|
212
|
-
const result = encodeCapabilities([]);
|
|
213
|
-
t.ok(b4a.isBuffer(result), 'returns a buffer');
|
|
214
|
-
t.is(result.length, 0, 'empty buffer for no caps');
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
test('encodeCapabilities - throws on invalid input', t => {
|
|
218
|
-
t.exception(
|
|
219
|
-
() => encodeCapabilities('not-array'),
|
|
220
|
-
errorMessageIncludes('Capabilities must be an array')
|
|
221
|
-
);
|
|
222
|
-
|
|
223
|
-
t.exception(
|
|
224
|
-
() => encodeCapabilities([1, 2]),
|
|
225
|
-
errorMessageIncludes('must contain only strings')
|
|
226
|
-
);
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
test('timestampToBuffer - encodes uint64 BE', t => {
|
|
230
|
-
const ts = 2n ** 53n; // beyond uint32
|
|
231
|
-
|
|
232
|
-
const tsBuf = timestampToBuffer(ts);
|
|
233
|
-
|
|
234
|
-
t.is(tsBuf.length, 8);
|
|
235
|
-
t.is(tsBuf.readBigUInt64BE(0), ts);
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
test('idToBuffer - encodes utf8 string', t => {
|
|
239
|
-
const id = 'test-id';
|
|
240
|
-
const idBuf = idToBuffer(id);
|
|
241
|
-
t.ok(b4a.isBuffer(idBuf));
|
|
242
|
-
t.ok(b4a.equals(idBuf, b4a.from(id, 'utf8')));
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
test('timestampToBuffer and idToBuffer - reject invalid input', t => {
|
|
246
|
-
t.exception(() => timestampToBuffer(-1), errorMessageIncludes('timestamp'));
|
|
247
|
-
t.exception(() => timestampToBuffer(1.5), errorMessageIncludes('timestamp'));
|
|
248
|
-
t.exception(() => timestampToBuffer('1'), errorMessageIncludes('timestamp'));
|
|
249
|
-
t.exception.all(() => idToBuffer(1));
|
|
250
|
-
t.exception.all(() => idToBuffer(null));
|
|
251
|
-
});
|