trac-msb 0.2.17 → 0.2.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +33 -11
- 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 +163 -14
- package/src/config/env.js +57 -25
- package/src/core/network/Network.js +121 -56
- package/src/core/network/protocols/LegacyProtocol.js +21 -11
- package/src/core/network/protocols/NetworkMessages.js +38 -17
- package/src/core/network/protocols/ProtocolInterface.js +15 -3
- package/src/core/network/protocols/ProtocolSession.js +144 -17
- package/src/core/network/protocols/V1Protocol.js +37 -18
- package/src/core/network/protocols/connectionPolicies.js +174 -0
- package/src/core/network/protocols/legacy/NetworkMessageRouter.js +26 -20
- package/src/core/network/protocols/{shared/handlers/base/BaseOperationHandler.js → legacy/handlers/BaseStateOperationHandler.js} +25 -14
- package/src/core/network/protocols/legacy/handlers/{GetRequestHandler.js → LegacyGetRequestHandler.js} +9 -9
- package/src/core/network/protocols/legacy/handlers/LegacyResponseHandler.js +23 -0
- package/src/core/network/protocols/{shared/handlers/RoleOperationHandler.js → legacy/handlers/LegacyRoleOperationHandler.js} +19 -12
- package/src/core/network/protocols/{shared/handlers/SubnetworkOperationHandler.js → legacy/handlers/LegacySubnetworkOperationHandler.js} +29 -18
- package/src/core/network/protocols/{shared/handlers/TransferOperationHandler.js → legacy/handlers/LegacyTransferOperationHandler.js} +18 -12
- package/src/core/network/protocols/legacy/validators/base/BaseResponse.js +7 -10
- package/src/core/network/protocols/shared/validators/{PartialBootstrapDeployment.js → PartialBootstrapDeploymentValidator.js} +9 -4
- package/src/core/network/protocols/shared/validators/{base/PartialOperation.js → PartialOperationValidator.js} +53 -31
- package/src/core/network/protocols/shared/validators/{PartialRoleAccess.js → PartialRoleAccessValidator.js} +51 -17
- package/src/core/network/protocols/shared/validators/{PartialTransaction.js → PartialTransactionValidator.js} +21 -7
- package/src/core/network/protocols/shared/validators/{PartialTransfer.js → PartialTransferValidator.js} +32 -14
- package/src/core/network/protocols/v1/NetworkMessageRouter.js +91 -7
- package/src/core/network/protocols/v1/V1ProtocolError.js +27 -0
- package/src/core/network/protocols/v1/handlers/V1BaseOperationHandler.js +66 -0
- package/src/core/network/protocols/v1/handlers/V1BroadcastTransactionOperationHandler.js +272 -0
- package/src/core/network/protocols/v1/handlers/V1LivenessOperationHandler.js +88 -0
- 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 +212 -0
- package/src/core/network/protocols/v1/validators/V1BroadcastTransactionRequest.js +28 -0
- package/src/core/network/protocols/v1/validators/V1BroadcastTransactionResponse.js +260 -0
- package/src/core/network/protocols/v1/validators/V1LivenessRequest.js +15 -0
- package/src/core/network/protocols/v1/validators/V1LivenessResponse.js +17 -0
- package/src/core/network/protocols/v1/validators/V1ValidationSchema.js +210 -0
- package/src/core/network/services/ConnectionManager.js +149 -96
- package/src/core/network/services/MessageOrchestrator.js +197 -37
- package/src/core/network/services/PendingRequestService.js +175 -0
- package/src/core/network/services/TransactionCommitService.js +149 -0
- package/src/core/network/services/TransactionPoolService.js +129 -18
- package/src/core/network/services/TransactionRateLimiterService.js +57 -35
- package/src/core/network/services/ValidatorHealthCheckService.js +127 -0
- package/src/core/network/services/ValidatorObserverService.js +400 -121
- package/src/core/state/State.js +259 -156
- 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 +320 -278
- package/src/messages/network/v1/NetworkMessageBuilder.js +71 -100
- package/src/messages/network/v1/NetworkMessageDirector.js +16 -50
- 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/Scheduler.js +0 -8
- package/src/utils/buffer.js +1 -1
- package/src/utils/check.js +12 -13
- package/src/utils/constants.js +78 -5
- package/src/utils/deepEqualApplyPayload.js +40 -0
- package/src/utils/errors.js +20 -0
- package/src/utils/fileUtils.js +12 -3
- package/src/utils/helpers.js +10 -1
- package/src/utils/logger.js +25 -0
- package/src/utils/migrationUtils.js +3 -3
- package/src/utils/normalizers.js +38 -0
- package/src/utils/protobuf/networkV1.generated.cjs +2460 -0
- package/src/utils/protobuf/operationHelpers.js +24 -3
- package/.dockerignore +0 -16
- package/.github/workflows/acceptance-tests.yml +0 -38
- package/.github/workflows/lint-pr-title.yml +0 -26
- package/.github/workflows/publish.yml +0 -33
- package/.github/workflows/unit-tests.yml +0 -34
- 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.proto +0 -74
- package/rpc/rpc_services.js +0 -163
- package/scripts/generate-protobufs.js +0 -42
- package/src/core/network/identity/NetworkWalletFactory.js +0 -77
- package/src/core/network/protocols/legacy/handlers/ResponseHandler.js +0 -37
- package/src/utils/cli.js +0 -130
- package/src/utils/cliCommands.js +0 -280
- package/src/utils/protobuf/network.cjs +0 -840
- package/tests/acceptance/v1/account/account.test.mjs +0 -123
- 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 -111
- package/tests/acceptance/v1/tx-details/tx-details.test.mjs +0 -213
- 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 -85
- 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 -276
- package/tests/unit/messages/network/NetworkMessageDirector.test.js +0 -201
- 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/ConnectionManager.test.js +0 -191
- package/tests/unit/network/NetworkWalletFactory.test.js +0 -156
- package/tests/unit/network/networkModule.test.js +0 -10
- 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/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 -305
- 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 -20
- package/whitelist/.gitkeep +0 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import Validator from 'fastest-validator';
|
|
2
|
+
import b4a from 'b4a';
|
|
3
|
+
import {
|
|
4
|
+
NetworkOperationType,
|
|
5
|
+
NONCE_BYTE_LENGTH,
|
|
6
|
+
SIGNATURE_BYTE_LENGTH,
|
|
7
|
+
MAX_PARTIAL_TX_PAYLOAD_BYTE_SIZE,
|
|
8
|
+
ResultCode
|
|
9
|
+
} from '../../../../../utils/constants.js';
|
|
10
|
+
|
|
11
|
+
const ALLOWED_RESULT_CODES = Object.values(ResultCode);
|
|
12
|
+
|
|
13
|
+
class V1ValidationSchema {
|
|
14
|
+
#validator;
|
|
15
|
+
#validateV1LivenessRequest;
|
|
16
|
+
#validateV1LivenessResponse;
|
|
17
|
+
#validateV1BroadcastTransactionRequest;
|
|
18
|
+
#validateV1BroadcastTransactionResponse;
|
|
19
|
+
|
|
20
|
+
constructor() {
|
|
21
|
+
this.#validator = new Validator({
|
|
22
|
+
useNewCustomCheckerFunction: true,
|
|
23
|
+
messages: {
|
|
24
|
+
buffer: "The '{field}' field must be a Buffer! Actual: {actual}",
|
|
25
|
+
bufferLength: "The '{field}' field must be a Buffer with length {expected}! Actual: {actual}",
|
|
26
|
+
bufferMinLength: "The '{field}' field must be a Buffer with min length {expected}! Actual: {actual}",
|
|
27
|
+
bufferMaxLength: "The '{field}' field must be a Buffer with max length {expected}! Actual: {actual}",
|
|
28
|
+
nonZeroBuffer: "The '{field}' field must not be an empty or zero-filled Buffer!",
|
|
29
|
+
emptyBuffer: "The '{field}' field must not be an empty Buffer! Actual: {actual}",
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
const isBuffer = b4a.isBuffer;
|
|
33
|
+
this.#validator.add("buffer", function ({schema, messages}, _path, _context) {
|
|
34
|
+
const allowZero = schema.allowZero === true;
|
|
35
|
+
const allowEmpty = schema.allowEmpty === true;
|
|
36
|
+
const exactLength = Number.isInteger(schema.length) ? schema.length : null;
|
|
37
|
+
const minLength = Number.isInteger(schema.min) ? schema.min : null;
|
|
38
|
+
const maxLength = Number.isInteger(schema.max) ? schema.max : null;
|
|
39
|
+
return {
|
|
40
|
+
source:
|
|
41
|
+
`
|
|
42
|
+
if (!${isBuffer}(value)) {
|
|
43
|
+
${this.makeError({type: "buffer", actual: "value", messages})}
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
const len = value.length;
|
|
47
|
+
${exactLength === null ? '' : `
|
|
48
|
+
if (len !== ${exactLength}) {
|
|
49
|
+
${this.makeError({type: "bufferLength", expected: exactLength, actual: "len", messages})}
|
|
50
|
+
}`}
|
|
51
|
+
${minLength === null ? '' : `
|
|
52
|
+
if (len < ${minLength}) {
|
|
53
|
+
${this.makeError({type: "bufferMinLength", expected: minLength, actual: "len", messages})}
|
|
54
|
+
}`}
|
|
55
|
+
${maxLength === null ? '' : `
|
|
56
|
+
if (len > ${maxLength}) {
|
|
57
|
+
${this.makeError({type: "bufferMaxLength", expected: maxLength, actual: "len", messages})}
|
|
58
|
+
}`}
|
|
59
|
+
if (len === 0 && !${allowEmpty}) {
|
|
60
|
+
${this.makeError({type: "emptyBuffer", actual: "len", messages})}
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
if (!${allowZero}) {
|
|
64
|
+
let isZeroFilled = true;
|
|
65
|
+
for (let i = 0; i < len; i++) {
|
|
66
|
+
if (value[i] !== 0) {
|
|
67
|
+
isZeroFilled = false;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (isZeroFilled) {
|
|
72
|
+
${this.makeError({type: "nonZeroBuffer", actual: "value", messages})}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
`
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
this.#validateV1LivenessRequest = this.#compileV1LivenessRequestSchema();
|
|
81
|
+
this.#validateV1LivenessResponse = this.#compileV1LivenessResponseSchema();
|
|
82
|
+
this.#validateV1BroadcastTransactionRequest = this.#compileV1BroadcastTransactionRequestSchema();
|
|
83
|
+
this.#validateV1BroadcastTransactionResponse = this.#compileV1BroadcastTransactionResponseSchema();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
#compileV1LivenessRequestSchema() {
|
|
87
|
+
const schema = {
|
|
88
|
+
$$strict: true,
|
|
89
|
+
type: {type: 'number', integer: true, equal: NetworkOperationType.LIVENESS_REQUEST, required: true},
|
|
90
|
+
id: {type: 'string', min: 1, max: 64, required: true},
|
|
91
|
+
timestamp: {type: 'number', integer: true, min: 1, max: Number.MAX_SAFE_INTEGER, required: true},
|
|
92
|
+
liveness_request: {
|
|
93
|
+
strict: true,
|
|
94
|
+
type: 'object',
|
|
95
|
+
props: {
|
|
96
|
+
nonce: {type: 'buffer', length: NONCE_BYTE_LENGTH, required: true},
|
|
97
|
+
signature: {type: 'buffer', length: SIGNATURE_BYTE_LENGTH, required: true},
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
capabilities: {type: 'array', items: 'string', required: true},
|
|
101
|
+
|
|
102
|
+
};
|
|
103
|
+
return this.#validator.compile(schema);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
validateV1LivenessRequest(operation) {
|
|
107
|
+
return this.#validateV1LivenessRequest(operation) === true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#compileV1LivenessResponseSchema() {
|
|
111
|
+
const schema = {
|
|
112
|
+
$$strict: true,
|
|
113
|
+
type: {type: 'number', integer: true, equal: NetworkOperationType.LIVENESS_RESPONSE, required: true},
|
|
114
|
+
id: {type: 'string', min: 1, max: 64, required: true},
|
|
115
|
+
timestamp: {type: 'number', integer: true, min: 1, max: Number.MAX_SAFE_INTEGER, required: true},
|
|
116
|
+
liveness_response: {
|
|
117
|
+
strict: true,
|
|
118
|
+
type: 'object',
|
|
119
|
+
props: {
|
|
120
|
+
nonce: {type: 'buffer', length: NONCE_BYTE_LENGTH, required: true},
|
|
121
|
+
signature: {type: 'buffer', length: SIGNATURE_BYTE_LENGTH, required: true},
|
|
122
|
+
result: {type: 'enum', values: ALLOWED_RESULT_CODES, required: true},
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
capabilities: {type: 'array', items: 'string', required: true},
|
|
126
|
+
|
|
127
|
+
};
|
|
128
|
+
return this.#validator.compile(schema);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
validateV1LivenessResponse(operation) {
|
|
132
|
+
return this.#validateV1LivenessResponse(operation) === true;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
#compileV1BroadcastTransactionRequestSchema() {
|
|
136
|
+
const schema = {
|
|
137
|
+
$$strict: true,
|
|
138
|
+
type: {
|
|
139
|
+
type: 'number',
|
|
140
|
+
integer: true,
|
|
141
|
+
equal: NetworkOperationType.BROADCAST_TRANSACTION_REQUEST,
|
|
142
|
+
required: true
|
|
143
|
+
},
|
|
144
|
+
id: {type: 'string', min: 1, max: 64, required: true},
|
|
145
|
+
timestamp: {type: 'number', integer: true, min: 1, max: Number.MAX_SAFE_INTEGER, required: true},
|
|
146
|
+
broadcast_transaction_request: {
|
|
147
|
+
strict: true,
|
|
148
|
+
type: 'object',
|
|
149
|
+
props: {
|
|
150
|
+
data: {
|
|
151
|
+
type: 'buffer',
|
|
152
|
+
min: 1,
|
|
153
|
+
max: MAX_PARTIAL_TX_PAYLOAD_BYTE_SIZE,
|
|
154
|
+
allowZero: true,
|
|
155
|
+
required: true
|
|
156
|
+
},
|
|
157
|
+
nonce: {type: 'buffer', length: NONCE_BYTE_LENGTH, required: true},
|
|
158
|
+
signature: {type: 'buffer', length: SIGNATURE_BYTE_LENGTH, required: true},
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
capabilities: {type: 'array', items: 'string', required: true},
|
|
162
|
+
|
|
163
|
+
};
|
|
164
|
+
return this.#validator.compile(schema);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
validateV1BroadcastTransactionRequest(operation) {
|
|
168
|
+
return this.#validateV1BroadcastTransactionRequest(operation) === true;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
#compileV1BroadcastTransactionResponseSchema() {
|
|
172
|
+
const schema = {
|
|
173
|
+
$$strict: true,
|
|
174
|
+
type: {
|
|
175
|
+
type: 'number',
|
|
176
|
+
integer: true,
|
|
177
|
+
equal: NetworkOperationType.BROADCAST_TRANSACTION_RESPONSE,
|
|
178
|
+
required: true
|
|
179
|
+
},
|
|
180
|
+
id: {type: 'string', min: 1, max: 64, required: true},
|
|
181
|
+
timestamp: {type: 'number', integer: true, min: 1, max: Number.MAX_SAFE_INTEGER, required: true},
|
|
182
|
+
broadcast_transaction_response: {
|
|
183
|
+
strict: true,
|
|
184
|
+
type: 'object',
|
|
185
|
+
props: {
|
|
186
|
+
nonce: {type: 'buffer', length: NONCE_BYTE_LENGTH, required: true},
|
|
187
|
+
signature: {type: 'buffer', length: SIGNATURE_BYTE_LENGTH, required: true},
|
|
188
|
+
proof: {type: 'buffer', allowEmpty: true, allowZero: true, required: true},
|
|
189
|
+
timestamp: {
|
|
190
|
+
type: 'number',
|
|
191
|
+
integer: true,
|
|
192
|
+
min: 0,
|
|
193
|
+
max: Number.MAX_SAFE_INTEGER,
|
|
194
|
+
optional: true
|
|
195
|
+
},
|
|
196
|
+
result: {type: 'enum', values: ALLOWED_RESULT_CODES, required: true},
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
capabilities: {type: 'array', items: 'string', required: true},
|
|
200
|
+
|
|
201
|
+
};
|
|
202
|
+
return this.#validator.compile(schema);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
validateV1BroadcastTransactionResponse(operation) {
|
|
206
|
+
return this.#validateV1BroadcastTransactionResponse(operation) === true;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export default V1ValidationSchema;
|
|
@@ -1,89 +1,161 @@
|
|
|
1
1
|
import b4a from 'b4a'
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
import {EventType, ResultCode} from '../../../utils/constants.js';
|
|
3
|
+
import {publicKeyToAddress} from "../../../utils/helpers.js";
|
|
4
|
+
import {Logger} from "../../../utils/logger.js";
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {import('hyperswarm').Connection} Connection
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export class ConnectionManagerError extends Error {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = this.constructor.name;
|
|
11
13
|
}
|
|
12
|
-
}
|
|
14
|
+
}
|
|
13
15
|
|
|
14
16
|
class ConnectionManager {
|
|
15
17
|
#validators
|
|
16
18
|
#maxValidators
|
|
17
19
|
#config
|
|
18
|
-
|
|
20
|
+
#healthCheckService
|
|
21
|
+
#boundedHealthCheckHandler
|
|
22
|
+
#logger
|
|
19
23
|
// Note: #validators is using publicKey (Buffer) as key
|
|
20
24
|
// As Buffers are objects, we will rely on internal conversions done by JS to compare them.
|
|
21
25
|
// It would be better to handle these conversions manually by using hex strings as keys to avoid issues
|
|
22
26
|
/**
|
|
23
27
|
* @param {Config} config
|
|
24
28
|
**/
|
|
25
|
-
constructor(config)
|
|
29
|
+
constructor(config) {
|
|
26
30
|
this.#validators = new Map();
|
|
27
31
|
this.#config = config
|
|
28
32
|
this.#maxValidators = config.maxValidators
|
|
33
|
+
this.#boundedHealthCheckHandler = this.#healthCheckHandler.bind(this);
|
|
34
|
+
this.#logger = new Logger(config)
|
|
29
35
|
}
|
|
30
36
|
|
|
31
37
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* if the specified validator is unavailable.
|
|
35
|
-
* @param {Object} message - The message to send to the validator
|
|
36
|
-
* @returns {String} - The public key of the validator used
|
|
38
|
+
* Subscribes to periodic validator health checks.
|
|
39
|
+
* @param {ReadyResource} healthCheckService
|
|
37
40
|
*/
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
// TODO: We should consider moving this to ValidatorObserver instead.
|
|
42
|
+
// Keep here only if we forsee having health checks for non-validator connections in the future.
|
|
43
|
+
// For now, it seems that it would be better to keep this logic here.
|
|
44
|
+
subscribeToHealthChecks(healthCheckService) {
|
|
45
|
+
this.#logger.debug('subscribeToHealthChecks: subscribing to health check events');
|
|
46
|
+
if (!healthCheckService || typeof healthCheckService.on !== 'function' || typeof healthCheckService.off !== 'function') {
|
|
47
|
+
throw new Error('ConnectionManager: health check service must implement on/off');
|
|
48
|
+
}
|
|
40
49
|
|
|
41
|
-
if (
|
|
42
|
-
|
|
50
|
+
if (this.#healthCheckService && this.#boundedHealthCheckHandler) {
|
|
51
|
+
this.#logger.debug('subscribeToHealthChecks: removing previous health check handler');
|
|
52
|
+
// Unsubscribe from previous health check service if already subscribed
|
|
53
|
+
// TODO: Maybe we should not allow switching to a new health check service
|
|
54
|
+
this.#healthCheckService.off(EventType.VALIDATOR_HEALTH_CHECK, this.#boundedHealthCheckHandler);
|
|
43
55
|
}
|
|
44
56
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
57
|
+
this.#healthCheckService = healthCheckService; // TODO: Maybe this should be handled in the constructor directly?
|
|
58
|
+
// TODO: declare this method outside this function to avoid redeclaring it every time we subscribe to health checks. We can just bind it to 'this' in the constructor.
|
|
59
|
+
|
|
60
|
+
this.#healthCheckService.on(EventType.VALIDATOR_HEALTH_CHECK, this.#boundedHealthCheckHandler);
|
|
61
|
+
this.#logger.debug('subscribeToHealthChecks: subscribed to health check events');
|
|
62
|
+
}
|
|
48
63
|
|
|
64
|
+
async #healthCheckHandler(publicKey, requestId) {
|
|
65
|
+
if (typeof publicKey !== 'string' || typeof requestId !== 'string') {
|
|
66
|
+
// We can't throw here because this is an event handler, but we should at least log the error and return early to avoid further issues.
|
|
67
|
+
this.#logger.error(`healthCheck: malformed event payload. Typeof publicKey = ${typeof publicKey}. Typeof requestId = ${typeof requestId}`);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const targetAddress = publicKeyToAddress(publicKey, this.#config);
|
|
72
|
+
|
|
73
|
+
if (!this.exists(publicKey) || !this.connected(publicKey)) {
|
|
74
|
+
this.#logger.debug(`healthCheck: validator not connected, stopping checks. Address = ${targetAddress}; Request ID = ${requestId}`);
|
|
75
|
+
this.#stopHealthCheck(publicKey);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const connection = this.getConnection(publicKey);
|
|
80
|
+
if (!connection || !connection.protocolSession || typeof connection.protocolSession.sendHealthCheck !== 'function') {
|
|
81
|
+
this.#logger.debug(`healthCheck: missing protocol session, removing validator. Address = ${targetAddress}; Request ID = ${requestId}`);
|
|
82
|
+
this.#stopHealthCheck(publicKey);
|
|
83
|
+
this.remove(publicKey);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let success = false;
|
|
49
88
|
try {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
89
|
+
this.#logger.debug(`healthCheck: sending liveness request. Address = ${targetAddress}; Request ID = ${requestId}`);
|
|
90
|
+
|
|
91
|
+
const resultCode = await connection.protocolSession.sendHealthCheck();
|
|
92
|
+
success = resultCode === ResultCode.OK;
|
|
93
|
+
if (!success) {
|
|
94
|
+
this.#logger.debug(`healthCheck: non-OK result code. Address = ${targetAddress}; Request ID = ${requestId}`);
|
|
95
|
+
}
|
|
96
|
+
} catch {
|
|
97
|
+
success = false;
|
|
54
98
|
}
|
|
55
99
|
|
|
56
|
-
|
|
57
|
-
|
|
100
|
+
if (!success) {
|
|
101
|
+
this.#logger.debug(`healthCheck: liveness request failed, removing validator. Address = ${targetAddress}; Request ID = ${requestId}`);
|
|
102
|
+
this.remove(publicKey);
|
|
103
|
+
this.#stopHealthCheck(publicKey);
|
|
104
|
+
} else {
|
|
105
|
+
this.#logger.debug(`healthCheck: success. Address = ${targetAddress}; Request ID = ${requestId}`);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
58
108
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
* @param {Object} message - The message to send to the validator
|
|
62
|
-
* @param {String | Buffer} publicKey - A validator public key hex string to be fetched from the pool.
|
|
63
|
-
* @returns {Boolean} True if the message was sent, false otherwise.
|
|
64
|
-
*/
|
|
65
|
-
sendSingleMessage(message, publicKey) {
|
|
66
|
-
let publicKeyHex = this.#toHexString(publicKey);
|
|
67
|
-
if (!this.exists(publicKeyHex) || !this.connected(publicKeyHex)) return false; // Fail silently
|
|
109
|
+
#stopHealthCheck(publicKeyHex) {
|
|
110
|
+
const targetAddress = publicKeyToAddress(publicKeyHex, this.#config);
|
|
68
111
|
|
|
69
|
-
|
|
70
|
-
|
|
112
|
+
if (!this.#healthCheckService) {
|
|
113
|
+
this.#logger.debug(`stopHealthCheck: no health check service, cannot stop checks for ${targetAddress}`);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
71
116
|
try {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
117
|
+
if (this.#healthCheckService.has(publicKeyHex)) {
|
|
118
|
+
this.#logger.debug(`stopHealthCheck: stopping scheduled checks for ${targetAddress}`);
|
|
119
|
+
this.#healthCheckService.stop(publicKeyHex);
|
|
120
|
+
}
|
|
121
|
+
} catch (error) {
|
|
122
|
+
this.#logger.debug(`stopHealthCheck: failed to stop health check for validator ${targetAddress}. Error: ${error.message}`);
|
|
75
123
|
}
|
|
76
|
-
return true; // TODO: Implement better success/failure reporting
|
|
77
124
|
}
|
|
78
125
|
|
|
79
126
|
/**
|
|
80
|
-
*
|
|
81
|
-
* @param {String | Buffer} publicKey - The public key hex string of the validator
|
|
127
|
+
* Retrieves the Hyperswarm connection object for a given validator public key.
|
|
128
|
+
* @param {String | Buffer} publicKey - The public key (Buffer or hex string) of the validator.
|
|
129
|
+
* @returns {Connection|undefined} - The connection object if found, otherwise undefined.
|
|
130
|
+
*/
|
|
131
|
+
getConnection(publicKey) {
|
|
132
|
+
const publicKeyHex = this.#toHexString(publicKey);
|
|
133
|
+
const entry = this.#validators.get(publicKeyHex);
|
|
134
|
+
return entry ? entry.connection : undefined;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Sends a message through a specific validator without increasing sent messages count.
|
|
139
|
+
* @param {Object} message - The message to send to the validator.
|
|
140
|
+
* @param {String | Buffer} publicKey - A validator public key hex string to be fetched from the pool.
|
|
141
|
+
* @returns {Promise<*>} A promise returned by `validator.connection.protocolSession.send(message)`.
|
|
142
|
+
* @throws {ConnectionManagerError} If the validator is not connected.
|
|
143
|
+
* @throws {ConnectionManagerError} If the validator has no valid connection or protocol session.
|
|
82
144
|
*/
|
|
83
|
-
|
|
84
|
-
whiteList(publicKey) {
|
|
145
|
+
async sendSingleMessage(message, publicKey) {
|
|
85
146
|
let publicKeyHex = this.#toHexString(publicKey);
|
|
86
|
-
this
|
|
147
|
+
if (!this.connected(publicKeyHex)) {
|
|
148
|
+
throw new ConnectionManagerError(
|
|
149
|
+
`Cannot send message: validator ${publicKeyToAddress(publicKey, this.#config)} is not connected.`
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
const validator = this.#validators.get(publicKeyHex);
|
|
153
|
+
if (!validator || !validator.connection || !validator.connection.protocolSession) {
|
|
154
|
+
throw new ConnectionManagerError(
|
|
155
|
+
`Cannot send message: no valid connection found for validator ${publicKeyToAddress(publicKey, this.#config)}.`
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
return validator.connection.protocolSession.send(message)
|
|
87
159
|
}
|
|
88
160
|
|
|
89
161
|
/**
|
|
@@ -95,44 +167,47 @@ class ConnectionManager {
|
|
|
95
167
|
addValidator(publicKey, connection) {
|
|
96
168
|
let publicKeyHex = this.#toHexString(publicKey);
|
|
97
169
|
if (this.maxConnectionsReached()) {
|
|
98
|
-
|
|
170
|
+
this.#logger.debug('addValidator: max connections reached.');
|
|
99
171
|
return false;
|
|
100
172
|
}
|
|
101
|
-
|
|
173
|
+
this.#logger.debug(`addValidator: adding validator ${publicKeyToAddress(publicKeyHex, this.#config)}`);
|
|
102
174
|
if (!this.exists(publicKeyHex)) {
|
|
103
|
-
|
|
175
|
+
this.#logger.debug(`addValidator: appending validator ${publicKeyToAddress(publicKeyHex, this.#config)}`);
|
|
104
176
|
this.#append(publicKeyHex, connection);
|
|
105
177
|
return true;
|
|
106
178
|
} else if (!this.connected(publicKeyHex)) {
|
|
107
|
-
|
|
179
|
+
this.#logger.debug(`addValidator: updating validator ${publicKeyToAddress(publicKeyHex, this.#config)}`);
|
|
108
180
|
this.#update(publicKeyHex, connection);
|
|
109
181
|
return true;
|
|
110
182
|
}
|
|
111
|
-
|
|
183
|
+
this.#logger.debug(`addValidator: didn't add validator ${publicKeyToAddress(publicKeyHex, this.#config)}`);
|
|
112
184
|
return false; // TODO: Implement better success/failure reporting
|
|
113
185
|
}
|
|
114
186
|
|
|
115
187
|
/**
|
|
116
188
|
* Removes a validator from the pool.
|
|
117
189
|
* @param {String | Buffer} publicKey - The public key hex string of the validator to remove
|
|
190
|
+
* @param {object} [options]
|
|
191
|
+
* @param {boolean} [options.endConnection=true] - Whether to close the underlying socket.
|
|
118
192
|
*/
|
|
119
|
-
remove(publicKey) {
|
|
120
|
-
|
|
193
|
+
remove(publicKey, { endConnection = true } = {}) {
|
|
194
|
+
this.#logger.debug(`remove: removing validator ${publicKeyToAddress(publicKey, this.#config)}`);
|
|
121
195
|
const publicKeyHex = this.#toHexString(publicKey);
|
|
196
|
+
this.#stopHealthCheck(publicKeyHex);
|
|
122
197
|
if (this.exists(publicKeyHex)) {
|
|
123
|
-
// Close the connection socket
|
|
124
198
|
const entry = this.#validators.get(publicKeyHex);
|
|
125
|
-
if (entry && entry.connection && typeof entry.connection.end === 'function') {
|
|
199
|
+
if (endConnection && entry && entry.connection && typeof entry.connection.end === 'function') {
|
|
126
200
|
try {
|
|
127
201
|
entry.connection.end();
|
|
128
202
|
} catch (e) {
|
|
129
203
|
// Ignore errors on connection end
|
|
204
|
+
this.#logger.debug(`remove: failed to end connection: ${e.message}`);
|
|
130
205
|
// TODO: Consider logging these errors here in verbose mode
|
|
131
206
|
}
|
|
132
207
|
}
|
|
133
|
-
|
|
208
|
+
this.#logger.debug(`remove: removing validator from map: ${publicKeyToAddress(publicKeyHex, this.#config)}. Map size before removal: ${this.#validators.size}.`);
|
|
134
209
|
this.#validators.delete(publicKeyHex);
|
|
135
|
-
|
|
210
|
+
this.#logger.debug(`remove: validator removed successfully. Map size is now ${this.#validators.size}.`);
|
|
136
211
|
}
|
|
137
212
|
}
|
|
138
213
|
|
|
@@ -206,26 +281,12 @@ class ConnectionManager {
|
|
|
206
281
|
}
|
|
207
282
|
|
|
208
283
|
prettyPrint() {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
// just to fetch a small subset of it (most times, 1 element).
|
|
216
|
-
// There are more efficient ways to pick a small subset of validators. Consider optimizing.
|
|
217
|
-
// Note 2: This method is unused now, but will be kept here for future reference
|
|
218
|
-
// TODO: Deprecated/Unused - remove if not needed
|
|
219
|
-
pickRandomSubset(validators, maxTargets) {
|
|
220
|
-
const copy = validators.slice();
|
|
221
|
-
const count = Math.min(maxTargets, copy.length);
|
|
222
|
-
|
|
223
|
-
for (let i = copy.length - 1; i > 0; i--) {
|
|
224
|
-
const j = Math.floor(Math.random() * (i + 1));
|
|
225
|
-
[copy[i], copy[j]] = [copy[j], copy[i]];
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return copy.slice(0, count);
|
|
284
|
+
this.#logger.info(`Connection count: ${this.connectionCount()}`);
|
|
285
|
+
this.#logger.info(`Validator map keys count: ${this.#validators.size}`);
|
|
286
|
+
this.#logger.info(`Validator map keys:\n${Array.from(this.#validators.entries()).map(([publicKey, val]) => {
|
|
287
|
+
const protocols = val.connection?.protocolSession?.preferredProtocol || 'none';
|
|
288
|
+
return `${publicKeyToAddress(publicKey, this.#config)}: ${protocols}`;
|
|
289
|
+
}).join('\n')}`);
|
|
229
290
|
}
|
|
230
291
|
|
|
231
292
|
/**
|
|
@@ -257,18 +318,18 @@ class ConnectionManager {
|
|
|
257
318
|
* @param {Object} connection - The connection object
|
|
258
319
|
*/
|
|
259
320
|
#append(publicKey, connection) {
|
|
260
|
-
|
|
321
|
+
this.#logger.debug(`#append: appending validator ${publicKeyToAddress(publicKey, this.#config)}`);
|
|
261
322
|
const publicKeyHex = this.#toHexString(publicKey);
|
|
262
323
|
if (this.#validators.has(publicKeyHex)) {
|
|
263
324
|
// This should never happen, but just in case, we log it
|
|
264
|
-
|
|
325
|
+
this.#logger.debug(`#append: tried to append existing validator: ${publicKeyToAddress(publicKey, this.#config)}`);
|
|
265
326
|
return;
|
|
266
327
|
}
|
|
267
|
-
this.#validators.set(publicKeyHex, {
|
|
328
|
+
this.#validators.set(publicKeyHex, {connection, sent: 0});
|
|
268
329
|
connection.on('close', () => {
|
|
269
|
-
|
|
330
|
+
this.#logger.debug(`#append: connection closing for validator ${publicKeyToAddress(publicKey, this.#config)}`);
|
|
270
331
|
this.remove(publicKeyHex);
|
|
271
|
-
|
|
332
|
+
this.#logger.debug(`#append: connection closed for validator ${publicKeyToAddress(publicKey, this.#config)}`);
|
|
272
333
|
});
|
|
273
334
|
}
|
|
274
335
|
|
|
@@ -283,22 +344,14 @@ class ConnectionManager {
|
|
|
283
344
|
// It would be preferable to keep them separated though, but we would need to review all usages to ensure correctness.
|
|
284
345
|
// Also, we should remove the 'else' branch below if we decide to keep 'update' and 'append' separated.
|
|
285
346
|
const publicKeyHex = this.#toHexString(publicKey);
|
|
286
|
-
|
|
347
|
+
this.#logger.debug(`#update: updating validator ${publicKeyToAddress(publicKey, this.#config)}`);
|
|
287
348
|
if (this.#validators.has(publicKeyHex)) {
|
|
288
349
|
this.#validators.get(publicKeyHex).connection = connection;
|
|
289
350
|
} else {
|
|
290
|
-
this.#validators.set(publicKeyHex, {
|
|
351
|
+
this.#validators.set(publicKeyHex, {connection, sent: 0});
|
|
291
352
|
}
|
|
292
353
|
}
|
|
293
354
|
|
|
294
|
-
#toAddress(publicKey) {
|
|
295
|
-
const keyHex = b4a.isBuffer(publicKey) ? publicKey : b4a.from(publicKey, 'hex');
|
|
296
|
-
return PeerWallet.encodeBech32m(
|
|
297
|
-
this.#config.addressPrefix,
|
|
298
|
-
keyHex
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
355
|
#toHexString(publicKey) {
|
|
303
356
|
return b4a.isBuffer(publicKey) ? publicKey.toString('hex') : publicKey;
|
|
304
357
|
}
|