trac-msb 0.2.4 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dockerignore +16 -0
- package/.github/workflows/acceptance-tests.yml +7 -0
- package/.github/workflows/publish.yml +40 -0
- package/.github/workflows/{CI.yml → unit-tests.yml} +1 -1
- package/README.md +175 -50
- package/docker-compose.yml +16 -0
- package/dockerfile +41 -0
- package/docs/fee_distribution.md +89 -0
- package/msb.mjs +12 -14
- package/package.json +8 -4
- package/rpc/constants.mjs +4 -1
- package/rpc/handlers.mjs +109 -66
- package/rpc/routes/v1.mjs +3 -1
- package/rpc/rpc_services.js +126 -0
- package/rpc/utils/confirmedParameter.mjs +17 -0
- package/rpc/utils/url.mjs +38 -0
- package/src/core/network/Network.js +27 -10
- package/src/core/network/identity/NetworkWalletFactory.js +78 -0
- package/src/core/network/services/ConnectionManager.js +2 -2
- package/src/core/network/services/ValidatorObserverService.js +7 -4
- package/src/core/state/State.js +28 -22
- package/src/index.js +197 -385
- package/src/utils/cliCommands.js +280 -0
- package/src/utils/constants.js +3 -1
- package/tests/acceptance/v1/account/account.test.mjs +123 -0
- package/tests/acceptance/v1/balance/balance.test.mjs +55 -0
- package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +111 -0
- package/tests/acceptance/v1/confirmed-length/confirmed-length.test.mjs +19 -0
- package/tests/acceptance/v1/fee/fee.test.mjs +11 -0
- package/tests/acceptance/v1/rpc.test.mjs +62 -291
- package/tests/acceptance/v1/tx/tx.test.mjs +98 -0
- package/tests/acceptance/v1/tx-details/tx-details.test.mjs +195 -0
- package/tests/acceptance/v1/tx-hashes/tx-hashes.test.mjs +72 -0
- package/tests/acceptance/v1/tx-payloads-bulk/tx-payloads-bulk.test.mjs +27 -0
- package/tests/acceptance/v1/txv/txv.test.mjs +11 -0
- package/tests/acceptance/v1/unconfirmed-length/unconfirmed-length.test.mjs +11 -0
- package/tests/helpers/StateNetworkFactory.js +157 -0
- package/tests/helpers/autobaseTestHelpers.js +369 -0
- package/tests/helpers/createTestSignature.js +12 -0
- package/tests/helpers/transactionPayloads.mjs +78 -0
- package/tests/unit/network/NetworkWalletFactory.test.js +156 -0
- package/tests/unit/state/apply/addAdmin/addAdminHappyPathScenario.js +38 -0
- package/tests/unit/state/apply/addAdmin/addAdminScenarioHelpers.js +273 -0
- package/tests/unit/state/apply/addAdmin/adminEntryEncodingFailureScenario.js +30 -0
- package/tests/unit/state/apply/addAdmin/adminEntryExistsScenario.js +78 -0
- package/tests/unit/state/apply/addAdmin/nodeEntryInitializationFailureScenario.js +30 -0
- package/tests/unit/state/apply/addAdmin/nonBootstrapNodeScenario.js +68 -0
- package/tests/unit/state/apply/addAdmin/state.apply.addAdmin.test.js +155 -0
- package/tests/unit/state/apply/addIndexer/addIndexerHappyPathScenario.js +39 -0
- package/tests/unit/state/apply/addIndexer/addIndexerMultipleIndexersInTheNetworkScenario.js +167 -0
- package/tests/unit/state/apply/addIndexer/addIndexerPretenderAlreadyIndexerScenario.js +21 -0
- package/tests/unit/state/apply/addIndexer/addIndexerPretenderNotWriterScenario.js +21 -0
- package/tests/unit/state/apply/addIndexer/addIndexerRemoveAndReAddScenario.js +186 -0
- package/tests/unit/state/apply/addIndexer/addIndexerScenarioHelpers.js +445 -0
- package/tests/unit/state/apply/addIndexer/addIndexerWriterKeyAlreadyRegisteredScenario.js +32 -0
- package/tests/unit/state/apply/addIndexer/state.apply.addIndexer.test.js +297 -0
- package/tests/unit/state/apply/addWriter/addWriterHappyPathScenario.js +41 -0
- package/tests/unit/state/apply/addWriter/addWriterInvalidValidatorSignatureScenario.js +32 -0
- package/tests/unit/state/apply/addWriter/addWriterNewWkScenario.js +149 -0
- package/tests/unit/state/apply/addWriter/addWriterRequesterAlreadyWriterScenario.js +21 -0
- package/tests/unit/state/apply/addWriter/addWriterRequesterBalanceInsufficientScenario.js +21 -0
- package/tests/unit/state/apply/addWriter/addWriterRequesterEntryDecodeFailureScenario.js +19 -0
- package/tests/unit/state/apply/addWriter/addWriterRequesterEntryMissingScenario.js +19 -0
- package/tests/unit/state/apply/addWriter/addWriterRequesterIndexerScenario.js +21 -0
- package/tests/unit/state/apply/addWriter/addWriterRequesterNotWhitelistedScenario.js +21 -0
- package/tests/unit/state/apply/addWriter/addWriterScenarioHelpers.js +757 -0
- package/tests/unit/state/apply/addWriter/addWriterStakeBalanceUpdateFailureScenario.js +50 -0
- package/tests/unit/state/apply/addWriter/addWriterStakeInsufficientBalanceScenario.js +29 -0
- package/tests/unit/state/apply/addWriter/addWriterStakeInvalidBalanceScenario.js +29 -0
- package/tests/unit/state/apply/addWriter/addWriterStakeInvalidEntryScenario.js +21 -0
- package/tests/unit/state/apply/addWriter/addWriterStakeStakedBalanceFailureScenario.js +37 -0
- package/tests/unit/state/apply/addWriter/addWriterStakeSubtractFailureScenario.js +42 -0
- package/tests/unit/state/apply/addWriter/addWriterValidatorRewardScenario.js +105 -0
- package/tests/unit/state/apply/addWriter/addWriterWriterKeyMismatchScenario.js +54 -0
- package/tests/unit/state/apply/addWriter/addWriterWriterKeyOwnershipScenario.js +54 -0
- package/tests/unit/state/apply/addWriter/addWriterZeroWriterKeyScenario.js +29 -0
- package/tests/unit/state/apply/addWriter/state.apply.addWriter.test.js +309 -0
- package/tests/unit/state/apply/adminRecovery/adminRecoveryHappyPathScenario.js +30 -0
- package/tests/unit/state/apply/adminRecovery/adminRecoveryScenarioHelpers.js +866 -0
- package/tests/unit/state/apply/adminRecovery/state.apply.adminRecovery.test.js +439 -0
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistBanAndReapplyScenario.js +78 -0
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistExistingReaderHappyPathScenario.js +98 -0
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistFeeAfterDisableScenario.js +66 -0
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistHappyPathScenario.js +55 -0
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistInsufficientAdminBalanceScenario.js +103 -0
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistNodeAlreadyWhitelistedScenario.js +60 -0
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistScenarioHelpers.js +191 -0
- package/tests/unit/state/apply/appendWhitelist/state.apply.appendWhitelist.test.js +220 -0
- package/tests/unit/state/apply/balanceInitialization/balanceInitializationHappyPathScenario.js +82 -0
- package/tests/unit/state/apply/balanceInitialization/balanceInitializationScenarioHelpers.js +106 -0
- package/tests/unit/state/apply/balanceInitialization/invalidAmountScenario.js +45 -0
- package/tests/unit/state/apply/balanceInitialization/nodeEntryBalanceUpdateFailureScenario.js +81 -0
- package/tests/unit/state/apply/balanceInitialization/state.apply.balanceInitialization.test.js +189 -0
- package/tests/unit/state/apply/banValidator/banValidatorBanAndReWhitelistScenario.js +155 -0
- package/tests/unit/state/apply/banValidator/banValidatorHappyPathScenario.js +36 -0
- package/tests/unit/state/apply/banValidator/banValidatorScenarioHelpers.js +534 -0
- package/tests/unit/state/apply/banValidator/banValidatorSequentialBansScenario.js +74 -0
- package/tests/unit/state/apply/banValidator/banValidatorTargetDecodeFailureScenario.js +19 -0
- package/tests/unit/state/apply/banValidator/banValidatorTargetIndexerScenario.js +32 -0
- package/tests/unit/state/apply/banValidator/banValidatorTargetNodeEntryMissingScenario.js +19 -0
- package/tests/unit/state/apply/banValidator/banValidatorTargetRoleUpdateFailureScenario.js +19 -0
- package/tests/unit/state/apply/banValidator/banValidatorWhitelistedNonWriterScenario.js +38 -0
- package/tests/unit/state/apply/banValidator/banValidatorWhitelistedZeroBalanceScenario.js +91 -0
- package/tests/unit/state/apply/banValidator/banValidatorWithdrawFailureScenario.js +19 -0
- package/tests/unit/state/apply/banValidator/state.apply.banValidator.test.js +266 -0
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentDuplicateRegistrationScenario.js +142 -0
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentHappyPathScenario.js +26 -0
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentIncompleteOperationScenario.js +94 -0
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentInvalidDeploymentEntryScenario.js +37 -0
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentMultipleBootstrapScenario.js +86 -0
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentScenarioHelpers.js +344 -0
- package/tests/unit/state/apply/bootstrapDeployment/invalidValidatorNodeEntryScenario.js +57 -0
- package/tests/unit/state/apply/bootstrapDeployment/state.apply.bootstrapDeployment.test.js +429 -0
- package/tests/unit/state/apply/common/access-control/adminConsistencyMismatchScenario.js +119 -0
- package/tests/unit/state/apply/common/access-control/adminEntryDecodeFailureScenario.js +130 -0
- package/tests/unit/state/apply/common/access-control/adminEntryExistsScenario.js +93 -0
- package/tests/unit/state/apply/common/access-control/adminEntryMissingScenario.js +108 -0
- package/tests/unit/state/apply/common/access-control/adminOnlyGuardScenario.js +126 -0
- package/tests/unit/state/apply/common/access-control/adminPublicKeyDecodeFailureScenario.js +120 -0
- package/tests/unit/state/apply/common/access-control/roleAccessOperationValidationScenario.js +50 -0
- package/tests/unit/state/apply/common/adminControlOperationValidationScenario.js +56 -0
- package/tests/unit/state/apply/common/balances/adminEntryUpdateFailureScenario.js +52 -0
- package/tests/unit/state/apply/common/balances/base/requesterBalanceScenarioBase.js +197 -0
- package/tests/unit/state/apply/common/balances/feeDecodeFailureScenario.js +52 -0
- package/tests/unit/state/apply/common/balances/requesterBalanceDecodeFailureScenario.js +15 -0
- package/tests/unit/state/apply/common/balances/requesterBalanceFeeApplicationFailureScenario.js +11 -0
- package/tests/unit/state/apply/common/balances/requesterBalanceInsufficientScenario.js +15 -0
- package/tests/unit/state/apply/common/balances/requesterBalanceUpdateFailureScenario.js +11 -0
- package/tests/unit/state/apply/common/balances/validatorEntryRewardFailureScenario.js +11 -0
- package/tests/unit/state/apply/common/balances/validatorEntryUpdateFailureScenario.js +11 -0
- package/tests/unit/state/apply/common/balances/validatorNodeEntryDecodeFailureScenario.js +40 -0
- package/tests/unit/state/apply/common/base/OperationValidationScenarioBase.js +114 -0
- package/tests/unit/state/apply/common/commonScenarioHelper.js +103 -0
- package/tests/unit/state/apply/common/indexer/indexerNodeEntryDecodeFailureScenario.js +36 -0
- package/tests/unit/state/apply/common/indexer/indexerNodeEntryMissingScenario.js +36 -0
- package/tests/unit/state/apply/common/indexer/indexerRoleUpdateFailureScenario.js +29 -0
- package/tests/unit/state/apply/common/indexer/indexerSequenceStateInvalidScenario.js +66 -0
- package/tests/unit/state/apply/common/invalidMessageComponentValidationScenario.js +84 -0
- package/tests/unit/state/apply/common/nodeEntryInitializationFailureScenario.js +47 -0
- package/tests/unit/state/apply/common/operationAlreadyAppliedScenario.js +85 -0
- package/tests/unit/state/apply/common/payload-structure/addressWithInvalidPublicKeyScenario.js +52 -0
- package/tests/unit/state/apply/common/payload-structure/initializationDisabledScenario.js +49 -0
- package/tests/unit/state/apply/common/payload-structure/invalidAddressValidationScenario.js +73 -0
- package/tests/unit/state/apply/common/payload-structure/invalidHashValidationScenario.js +71 -0
- package/tests/unit/state/apply/common/payload-structure/invalidPayloadValidationScenario.js +31 -0
- package/tests/unit/state/apply/common/payload-structure/invalidSignatureValidationScenario.js +142 -0
- package/tests/unit/state/apply/common/payload-structure/partialOperationValidationScenario.js +87 -0
- package/tests/unit/state/apply/common/requester/requesterNodeEntryBufferMissingScenario.js +70 -0
- package/tests/unit/state/apply/common/requester/requesterNodeEntryDecodeFailureScenario.js +72 -0
- package/tests/unit/state/apply/common/requester/requesterNodeEntryMissingScenario.js +36 -0
- package/tests/unit/state/apply/common/requesterAddressValidationScenario.js +44 -0
- package/tests/unit/state/apply/common/requesterPublicKeyValidationScenario.js +25 -0
- package/tests/unit/state/apply/common/transactionValidityMismatchScenario.js +98 -0
- package/tests/unit/state/apply/common/validatorConsistency/base/validatorConsistencyScenarioBase.js +201 -0
- package/tests/unit/state/apply/common/validatorConsistency/validatorEntryDecodeFailureScenario.js +17 -0
- package/tests/unit/state/apply/common/validatorConsistency/validatorEntryMissingScenario.js +44 -0
- package/tests/unit/state/apply/common/validatorConsistency/validatorInactiveScenario.js +19 -0
- package/tests/unit/state/apply/common/validatorConsistency/validatorWriterKeyMismatchScenario.js +18 -0
- package/tests/unit/state/apply/common/validatorEntryValidation/base/validatorEntryValidationScenarioBase.js +314 -0
- package/tests/unit/state/apply/common/validatorEntryValidation/validatorEntryInvalidBalanceScenario.js +18 -0
- package/tests/unit/state/apply/common/writerKeyExistsValidationScenario.js +43 -0
- package/tests/unit/state/apply/disableInitialization/disableInitializationAlreadyDisabledScenario.js +53 -0
- package/tests/unit/state/apply/disableInitialization/disableInitializationHappyPathScenario.js +24 -0
- package/tests/unit/state/apply/disableInitialization/disableInitializationScenarioHelpers.js +197 -0
- package/tests/unit/state/apply/disableInitialization/state.apply.disableInitialization.test.js +161 -0
- package/tests/unit/state/apply/missing-tests.md +18 -0
- package/tests/unit/state/apply/removeIndexer/removeIndexerHappyPathScenario.js +58 -0
- package/tests/unit/state/apply/removeIndexer/removeIndexerReAddAndRemoveAgainScenario.js +98 -0
- package/tests/unit/state/apply/removeIndexer/removeIndexerRemoveMultipleIndexersScenario.js +167 -0
- package/tests/unit/state/apply/removeIndexer/removeIndexerScenarioHelpers.js +428 -0
- package/tests/unit/state/apply/removeIndexer/removeIndexerTargetNotIndexerScenario.js +22 -0
- package/tests/unit/state/apply/removeIndexer/removeIndexerWriterKeyMissingScenario.js +20 -0
- package/tests/unit/state/apply/removeIndexer/state.apply.removeIndexer.test.js +291 -0
- package/tests/unit/state/apply/removeWriter/removeWriterAndAddWriterAgainScenario.js +87 -0
- package/tests/unit/state/apply/removeWriter/removeWriterHappyPathScenario.js +38 -0
- package/tests/unit/state/apply/removeWriter/removeWriterInvalidValidatorSignatureScenario.js +32 -0
- package/tests/unit/state/apply/removeWriter/removeWriterRequesterBalanceInsufficientScenario.js +21 -0
- package/tests/unit/state/apply/removeWriter/removeWriterRequesterEntryDecodeFailureScenario.js +19 -0
- package/tests/unit/state/apply/removeWriter/removeWriterRequesterEntryMissingScenario.js +19 -0
- package/tests/unit/state/apply/removeWriter/removeWriterRequesterIndexerScenario.js +21 -0
- package/tests/unit/state/apply/removeWriter/removeWriterRequesterNotWriterScenario.js +21 -0
- package/tests/unit/state/apply/removeWriter/removeWriterRequesterRoleUpdateFailureScenario.js +19 -0
- package/tests/unit/state/apply/removeWriter/removeWriterScenarioHelpers.js +344 -0
- package/tests/unit/state/apply/removeWriter/removeWriterThroughWriterValidatorScenario.js +113 -0
- package/tests/unit/state/apply/removeWriter/removeWriterUnstakeFailureScenario.js +33 -0
- package/tests/unit/state/apply/removeWriter/removeWriterWriterKeyMismatchScenario.js +21 -0
- package/tests/unit/state/apply/removeWriter/removeWriterWriterKeyOwnershipScenario.js +26 -0
- package/tests/unit/state/apply/removeWriter/removeWriterWriterKeyRegistryMissingScenario.js +22 -0
- package/tests/unit/state/apply/removeWriter/state.apply.removeWriter.test.js +307 -0
- package/tests/unit/state/apply/state.apply.test.js +24 -0
- package/tests/unit/state/apply/transfer/state.apply.transfer.test.js +819 -0
- package/tests/unit/state/apply/transfer/transferContractSchemaValidationScenario.js +22 -0
- package/tests/unit/state/apply/transfer/transferDoubleSpendAcrossValidatorsScenario.js +137 -0
- package/tests/unit/state/apply/transfer/transferDoubleSpendSameBatchScenario.js +63 -0
- package/tests/unit/state/apply/transfer/transferDoubleSpendSingleValidatorScenario.js +67 -0
- package/tests/unit/state/apply/transfer/transferExistingRecipientAmountScenario.js +31 -0
- package/tests/unit/state/apply/transfer/transferExistingRecipientZeroAmountScenario.js +31 -0
- package/tests/unit/state/apply/transfer/transferHandlerGuardScenarios.js +22 -0
- package/tests/unit/state/apply/transfer/transferHappyPathScenario.js +8 -0
- package/tests/unit/state/apply/transfer/transferInvalidIncomingDataScenario.js +66 -0
- package/tests/unit/state/apply/transfer/transferNewRecipientAmountScenario.js +31 -0
- package/tests/unit/state/apply/transfer/transferNewRecipientZeroAmountScenario.js +31 -0
- package/tests/unit/state/apply/transfer/transferScenarioHelpers.js +1167 -0
- package/tests/unit/state/apply/transfer/transferSelfTransferAmountScenario.js +38 -0
- package/tests/unit/state/apply/transfer/transferSelfTransferZeroAmountScenario.js +38 -0
- package/tests/unit/state/apply/transfer/transferValidatorRecipientAmountScenario.js +38 -0
- package/tests/unit/state/apply/transfer/transferValidatorRecipientZeroAmountScenario.js +38 -0
- package/tests/unit/state/apply/txOperation/state.apply.txOperation.test.js +318 -0
- package/tests/unit/state/apply/txOperation/txOperationBootstrapNotRegisteredScenario.js +70 -0
- package/tests/unit/state/apply/txOperation/txOperationDifferentValidatorCreatorHappyPathScenario.js +23 -0
- package/tests/unit/state/apply/txOperation/txOperationInvalidDeploymentEntryScenario.js +48 -0
- package/tests/unit/state/apply/txOperation/txOperationInvalidFeeAmountScenario.js +39 -0
- package/tests/unit/state/apply/txOperation/txOperationInvalidSubnetCreatorAddressScenario.js +46 -0
- package/tests/unit/state/apply/txOperation/txOperationRequesterCreatorHappyPathScenario.js +21 -0
- package/tests/unit/state/apply/txOperation/txOperationScenarioHelpers.js +429 -0
- package/tests/unit/state/apply/txOperation/txOperationStandardHappyPathScenario.js +21 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeAddCreatorBalanceFailureScenario.js +26 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeAddValidatorBalanceFailureScenario.js +25 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeAddValidatorBonusFailureScenario.js +27 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeDecodeCreatorEntryScenario.js +18 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeDecodeRequesterEntryScenario.js +17 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeDecodeValidatorEntryScenario.js +31 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeGuardBypassScenario.js +49 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeGuardScenarioFactory.js +92 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeInsufficientRequesterBalanceScenario.js +28 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidCreatorBalanceScenario.js +29 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidRequesterBalanceScenario.js +28 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidRequesterEntryScenario.js +17 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeInvalidValidatorBalanceScenario.js +33 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeMissingCreatorEntryScenario.js +18 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeSubtractFailureScenario.js +25 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateCreatorBalanceFailureScenario.js +26 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateFailureScenario.js +25 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateValidatorBalanceFailureScenario.js +26 -0
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeUpdateValidatorBonusFailureScenario.js +27 -0
- package/tests/unit/state/apply/txOperation/txOperationValidatorCreatorHappyPathScenario.js +21 -0
- package/tests/unit/state/stateModule.test.js +1 -0
- package/tests/unit/state/stateTestUtils.js +5 -1
- package/.env +0 -3
|
@@ -0,0 +1,757 @@
|
|
|
1
|
+
import b4a from 'b4a';
|
|
2
|
+
import PartialStateMessageOperations from '../../../../../src/messages/partialStateMessages/PartialStateMessageOperations.js';
|
|
3
|
+
import CompleteStateMessageOperations from '../../../../../src/messages/completeStateMessages/CompleteStateMessageOperations.js';
|
|
4
|
+
import { deriveIndexerSequenceState, eventFlush } from '../../../../helpers/autobaseTestHelpers.js';
|
|
5
|
+
import { safeDecodeApplyOperation } from '../../../../../src/utils/protobuf/operationHelpers.js';
|
|
6
|
+
import nodeEntryUtils, { ZERO_LICENSE } from '../../../../../src/core/state/utils/nodeEntry.js';
|
|
7
|
+
import nodeRoleUtils from '../../../../../src/core/state/utils/roles.js';
|
|
8
|
+
import lengthEntryUtils from '../../../../../src/core/state/utils/lengthEntry.js';
|
|
9
|
+
import addressUtils from '../../../../../src/core/state/utils/address.js';
|
|
10
|
+
import { EntryType } from '../../../../../src/utils/constants.js';
|
|
11
|
+
import {
|
|
12
|
+
toBalance,
|
|
13
|
+
BALANCE_FEE,
|
|
14
|
+
BALANCE_TO_STAKE,
|
|
15
|
+
BALANCE_ZERO,
|
|
16
|
+
PERCENT_75
|
|
17
|
+
} from '../../../../../src/core/state/utils/balance.js';
|
|
18
|
+
import { decimalStringToBigInt, bigIntTo16ByteBuffer } from '../../../../../src/utils/amountSerialization.js';
|
|
19
|
+
import { setupAdminAndWhitelistedReaderNetwork } from '../common/commonScenarioHelper.js';
|
|
20
|
+
|
|
21
|
+
const DEFAULT_WRITER_FUNDING = bigIntTo16ByteBuffer(decimalStringToBigInt('10'));
|
|
22
|
+
const STAKE_ENTRY_MARK = Symbol('stake-entry-mark');
|
|
23
|
+
|
|
24
|
+
export async function setupAddWriterScenario(
|
|
25
|
+
t,
|
|
26
|
+
{ nodes = 2, writerInitialBalance = DEFAULT_WRITER_FUNDING } = {}
|
|
27
|
+
) {
|
|
28
|
+
const context = await setupAdminAndWhitelistedReaderNetwork(t, {
|
|
29
|
+
nodes: Math.max(nodes, 2),
|
|
30
|
+
readerInitialBalance: writerInitialBalance
|
|
31
|
+
});
|
|
32
|
+
context.addWriterScenario = { writerInitialBalance };
|
|
33
|
+
return context;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function selectWriterPeer(context, offset = 0) {
|
|
37
|
+
const candidates = context.peers.slice(1);
|
|
38
|
+
if (!candidates.length) {
|
|
39
|
+
throw new Error('AddWriter scenarios require at least one reader peer.');
|
|
40
|
+
}
|
|
41
|
+
return candidates[Math.min(offset, candidates.length - 1)];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function selectValidatorPeerWithoutEntry(context) {
|
|
45
|
+
const writerPeer = selectWriterPeer(context);
|
|
46
|
+
return (
|
|
47
|
+
context.peers.find(
|
|
48
|
+
peer =>
|
|
49
|
+
peer.wallet.address !== context.adminBootstrap.wallet.address &&
|
|
50
|
+
peer.wallet.address !== writerPeer.wallet.address
|
|
51
|
+
) ?? null
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function promotePeerToWriter(
|
|
56
|
+
t,
|
|
57
|
+
context,
|
|
58
|
+
{
|
|
59
|
+
readerPeer = selectWriterPeer(context),
|
|
60
|
+
validatorPeer = context.adminBootstrap,
|
|
61
|
+
writerKeyBuffer = null,
|
|
62
|
+
expectedWriterIndex = null
|
|
63
|
+
} = {}
|
|
64
|
+
) {
|
|
65
|
+
if (!readerPeer) {
|
|
66
|
+
throw new Error('promotePeerToWriter requires a reader peer.');
|
|
67
|
+
}
|
|
68
|
+
if (!validatorPeer) {
|
|
69
|
+
throw new Error('promotePeerToWriter requires a validator peer.');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const payload = await buildAddWriterPayload(context, {
|
|
73
|
+
readerPeer,
|
|
74
|
+
validatorPeer,
|
|
75
|
+
writerKeyBuffer
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
await validatorPeer.base.append(payload);
|
|
79
|
+
await validatorPeer.base.update();
|
|
80
|
+
await eventFlush();
|
|
81
|
+
|
|
82
|
+
const assertOptions = {
|
|
83
|
+
readerPeer,
|
|
84
|
+
validatorPeer,
|
|
85
|
+
writerKeyBuffer,
|
|
86
|
+
payload
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
if (typeof expectedWriterIndex === 'number') {
|
|
90
|
+
assertOptions.expectedWriterIndex = expectedWriterIndex;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
await assertAddWriterSuccessState(t, context, assertOptions);
|
|
94
|
+
|
|
95
|
+
return payload;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function buildAddWriterPayload(
|
|
99
|
+
context,
|
|
100
|
+
{
|
|
101
|
+
readerPeer = selectWriterPeer(context),
|
|
102
|
+
validatorPeer = context.adminBootstrap,
|
|
103
|
+
writerKeyBuffer = null
|
|
104
|
+
} = {}
|
|
105
|
+
) {
|
|
106
|
+
const txValidity = await deriveIndexerSequenceState(validatorPeer.base);
|
|
107
|
+
const writingKey = writerKeyBuffer ?? readerPeer.base.local.key;
|
|
108
|
+
const partial = await PartialStateMessageOperations.assembleAddWriterMessage(
|
|
109
|
+
readerPeer.wallet,
|
|
110
|
+
writingKey.toString('hex'),
|
|
111
|
+
txValidity.toString('hex')
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return CompleteStateMessageOperations.assembleAddWriterMessage(
|
|
115
|
+
validatorPeer.wallet,
|
|
116
|
+
partial.address,
|
|
117
|
+
b4a.from(partial.rao.tx, 'hex'),
|
|
118
|
+
b4a.from(partial.rao.txv, 'hex'),
|
|
119
|
+
b4a.from(partial.rao.iw, 'hex'),
|
|
120
|
+
b4a.from(partial.rao.in, 'hex'),
|
|
121
|
+
b4a.from(partial.rao.is, 'hex')
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export async function buildAddWriterPayloadWithTxValidity(
|
|
126
|
+
context,
|
|
127
|
+
mutatedTxValidity,
|
|
128
|
+
{
|
|
129
|
+
readerPeer = selectWriterPeer(context),
|
|
130
|
+
validatorPeer = context.adminBootstrap,
|
|
131
|
+
writerKeyBuffer = null
|
|
132
|
+
} = {}
|
|
133
|
+
) {
|
|
134
|
+
if (!b4a.isBuffer(mutatedTxValidity)) {
|
|
135
|
+
throw new Error('buildAddWriterPayloadWithTxValidity requires a tx validity buffer.');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const writingKey = writerKeyBuffer ?? readerPeer.base.local.key;
|
|
139
|
+
const partial = await PartialStateMessageOperations.assembleAddWriterMessage(
|
|
140
|
+
readerPeer.wallet,
|
|
141
|
+
writingKey.toString('hex'),
|
|
142
|
+
mutatedTxValidity.toString('hex')
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
return CompleteStateMessageOperations.assembleAddWriterMessage(
|
|
146
|
+
validatorPeer.wallet,
|
|
147
|
+
partial.address,
|
|
148
|
+
b4a.from(partial.rao.tx, 'hex'),
|
|
149
|
+
mutatedTxValidity,
|
|
150
|
+
b4a.from(partial.rao.iw, 'hex'),
|
|
151
|
+
b4a.from(partial.rao.in, 'hex'),
|
|
152
|
+
b4a.from(partial.rao.is, 'hex')
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export async function buildRemoveWriterPayload(
|
|
157
|
+
context,
|
|
158
|
+
{
|
|
159
|
+
readerPeer = selectWriterPeer(context),
|
|
160
|
+
validatorPeer = context.adminBootstrap,
|
|
161
|
+
writerKeyBuffer = null
|
|
162
|
+
} = {}
|
|
163
|
+
) {
|
|
164
|
+
const txValidity = await deriveIndexerSequenceState(validatorPeer.base);
|
|
165
|
+
const writerKey = writerKeyBuffer ?? readerPeer.base.local.key;
|
|
166
|
+
const partial = await PartialStateMessageOperations.assembleRemoveWriterMessage(
|
|
167
|
+
readerPeer.wallet,
|
|
168
|
+
writerKey.toString('hex'),
|
|
169
|
+
txValidity.toString('hex')
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
return CompleteStateMessageOperations.assembleRemoveWriterMessage(
|
|
173
|
+
validatorPeer.wallet,
|
|
174
|
+
partial.address,
|
|
175
|
+
b4a.from(partial.rao.tx, 'hex'),
|
|
176
|
+
b4a.from(partial.rao.txv, 'hex'),
|
|
177
|
+
b4a.from(partial.rao.iw, 'hex'),
|
|
178
|
+
b4a.from(partial.rao.in, 'hex'),
|
|
179
|
+
b4a.from(partial.rao.is, 'hex')
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export async function assertAddWriterSuccessState(
|
|
184
|
+
t,
|
|
185
|
+
context,
|
|
186
|
+
{
|
|
187
|
+
readerPeer = selectWriterPeer(context),
|
|
188
|
+
validatorPeer = context.adminBootstrap,
|
|
189
|
+
writerInitialBalance = context.addWriterScenario?.writerInitialBalance,
|
|
190
|
+
validatorBalanceBefore = null,
|
|
191
|
+
payload = null,
|
|
192
|
+
expectedWriterIndex = 1,
|
|
193
|
+
skipSync = false,
|
|
194
|
+
writerKeyBuffer = null
|
|
195
|
+
} = {}
|
|
196
|
+
) {
|
|
197
|
+
if (!writerInitialBalance) {
|
|
198
|
+
throw new Error('assertAddWriterSuccessState requires writerInitialBalance buffer.');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const writerInitial = toBalance(writerInitialBalance);
|
|
202
|
+
const afterFee = writerInitial.sub(BALANCE_FEE);
|
|
203
|
+
const expectedLiquid = afterFee?.sub(BALANCE_TO_STAKE);
|
|
204
|
+
const expectedStaked = BALANCE_TO_STAKE.value;
|
|
205
|
+
if (!expectedLiquid) {
|
|
206
|
+
throw new Error('Failed to derive expected writer balance.');
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const writerAddress = readerPeer.wallet.address;
|
|
210
|
+
const writerAddressBuffer = addressUtils.addressToBuffer(writerAddress);
|
|
211
|
+
const writingKey = writerKeyBuffer ?? readerPeer.base.local.key;
|
|
212
|
+
const writingKeyHex = writingKey.toString('hex');
|
|
213
|
+
|
|
214
|
+
await assertWriterEntry(t, validatorPeer.base, writerAddress, writingKey, {
|
|
215
|
+
expectedBalance: expectedLiquid.value,
|
|
216
|
+
expectedStakedBalance: expectedStaked
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
await assertWriterRegistry(t, validatorPeer.base, writerAddressBuffer, writingKeyHex, expectedWriterIndex);
|
|
220
|
+
|
|
221
|
+
if (validatorBalanceBefore) {
|
|
222
|
+
await assertValidatorReward(t, validatorPeer, validatorBalanceBefore);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (payload) {
|
|
226
|
+
await assertReplayProtection(t, validatorPeer.base, payload);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (!skipSync) {
|
|
230
|
+
await context.sync();
|
|
231
|
+
await assertWriterEntry(t, readerPeer.base, writerAddress, writingKey, {
|
|
232
|
+
expectedBalance: expectedLiquid.value,
|
|
233
|
+
expectedStakedBalance: expectedStaked
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function assertWriterEntry(
|
|
239
|
+
t,
|
|
240
|
+
base,
|
|
241
|
+
address,
|
|
242
|
+
writingKey,
|
|
243
|
+
{ expectedBalance, expectedStakedBalance }
|
|
244
|
+
) {
|
|
245
|
+
const entry = await base.view.get(address);
|
|
246
|
+
t.ok(entry, 'writer node entry exists');
|
|
247
|
+
const decoded = nodeEntryUtils.decode(entry.value);
|
|
248
|
+
t.ok(decoded, 'writer node entry decodes');
|
|
249
|
+
t.is(decoded.isWhitelisted, true, 'writer remains whitelisted');
|
|
250
|
+
t.is(decoded.isWriter, true, 'writer role assigned');
|
|
251
|
+
t.is(decoded.isIndexer, false, 'writer not promoted to indexer');
|
|
252
|
+
t.ok(b4a.equals(decoded.wk, writingKey), 'writer key stored on node entry');
|
|
253
|
+
if (expectedBalance) {
|
|
254
|
+
t.ok(
|
|
255
|
+
b4a.equals(decoded.balance, expectedBalance),
|
|
256
|
+
'writer liquid balance reflects stake and fee deductions'
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
if (expectedStakedBalance) {
|
|
260
|
+
t.ok(
|
|
261
|
+
b4a.equals(decoded.stakedBalance, expectedStakedBalance),
|
|
262
|
+
'writer staked balance recorded'
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
async function assertReaderNotPromoted(t, base, peer, { expectRegistryEntry = false } = {}) {
|
|
268
|
+
const address = peer.wallet.address;
|
|
269
|
+
const entry = await base.view.get(address);
|
|
270
|
+
t.ok(entry, 'reader entry exists');
|
|
271
|
+
const decoded = nodeEntryUtils.decode(entry.value);
|
|
272
|
+
t.ok(decoded, 'reader entry decodes');
|
|
273
|
+
t.is(decoded.isWhitelisted, true, 'reader stays whitelisted');
|
|
274
|
+
t.is(decoded.isWriter, false, 'reader not promoted to writer');
|
|
275
|
+
t.is(decoded.isIndexer, false, 'reader not an indexer');
|
|
276
|
+
t.ok(
|
|
277
|
+
b4a.equals(decoded.stakedBalance, BALANCE_ZERO.value),
|
|
278
|
+
'reader staked balance remains zero'
|
|
279
|
+
);
|
|
280
|
+
const registryEntry = await base.view.get(
|
|
281
|
+
EntryType.WRITER_ADDRESS + peer.base.local.key.toString('hex')
|
|
282
|
+
);
|
|
283
|
+
if (expectRegistryEntry) {
|
|
284
|
+
t.ok(registryEntry, 'writer registry entry persists for reader');
|
|
285
|
+
} else {
|
|
286
|
+
t.is(registryEntry, null, 'writer registry entry remains absent for reader');
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export async function assertAddWriterFailureState(
|
|
291
|
+
t,
|
|
292
|
+
context,
|
|
293
|
+
{ skipSync = false, expectRegistryEntry = false } = {}
|
|
294
|
+
) {
|
|
295
|
+
const writerPeer = selectWriterPeer(context);
|
|
296
|
+
const validatorPeer = context.adminBootstrap;
|
|
297
|
+
|
|
298
|
+
await assertReaderNotPromoted(t, validatorPeer.base, writerPeer, { expectRegistryEntry });
|
|
299
|
+
|
|
300
|
+
if (!skipSync) {
|
|
301
|
+
await context.sync();
|
|
302
|
+
await assertReaderNotPromoted(t, writerPeer.base, writerPeer, { expectRegistryEntry });
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async function assertWriterRegistry(t, base, addressBuffer, writingKeyHex, expectedWriterIndex) {
|
|
307
|
+
const registryEntry = await base.view.get(EntryType.WRITER_ADDRESS + writingKeyHex);
|
|
308
|
+
t.ok(registryEntry, 'writer registry entry exists');
|
|
309
|
+
t.ok(
|
|
310
|
+
b4a.equals(registryEntry.value, addressBuffer),
|
|
311
|
+
'writer registry links writing key to node address'
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
const writersLengthEntry = await base.view.get(EntryType.WRITERS_LENGTH);
|
|
315
|
+
t.ok(writersLengthEntry, 'writers length entry exists');
|
|
316
|
+
const writersLength = lengthEntryUtils.decodeBE(writersLengthEntry.value);
|
|
317
|
+
t.is(writersLength, expectedWriterIndex + 1, 'writers length increments');
|
|
318
|
+
|
|
319
|
+
const indexEntry = await base.view.get(`${EntryType.WRITERS_INDEX}${expectedWriterIndex}`);
|
|
320
|
+
t.ok(indexEntry, 'writers index entry stored');
|
|
321
|
+
t.ok(b4a.equals(indexEntry.value, addressBuffer), 'writer index stores node address');
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export async function assertValidatorReward(t, validatorPeer, validatorBalanceBefore) {
|
|
325
|
+
const validatorEntry = await validatorPeer.base.view.get(validatorPeer.wallet.address);
|
|
326
|
+
t.ok(validatorEntry, 'validator entry exists');
|
|
327
|
+
const decodedValidator = nodeEntryUtils.decode(validatorEntry.value);
|
|
328
|
+
t.ok(decodedValidator, 'validator entry decodes');
|
|
329
|
+
const initialBalance = toBalance(validatorBalanceBefore);
|
|
330
|
+
const expected = initialBalance.add(BALANCE_FEE.percentage(PERCENT_75));
|
|
331
|
+
const afterBalance = toBalance(decodedValidator.balance);
|
|
332
|
+
t.ok(afterBalance, 'validator balance decodes');
|
|
333
|
+
t.ok(
|
|
334
|
+
b4a.equals(afterBalance.value, expected.value),
|
|
335
|
+
'validator receives 75% of the writer fee'
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
async function assertReplayProtection(t, base, payload) {
|
|
340
|
+
const decodedPayload = safeDecodeApplyOperation(payload);
|
|
341
|
+
t.ok(decodedPayload, 'addWriter payload decodes');
|
|
342
|
+
const txKey = decodedPayload?.rao?.tx?.toString('hex');
|
|
343
|
+
t.ok(txKey, 'addWriter tx hash extracted');
|
|
344
|
+
const txEntry = await base.view.get(txKey);
|
|
345
|
+
t.ok(txEntry, 'addWriter transaction recorded for replay protection');
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export async function assertWriterRemovalState(
|
|
349
|
+
t,
|
|
350
|
+
context,
|
|
351
|
+
{
|
|
352
|
+
readerPeer = selectWriterPeer(context),
|
|
353
|
+
validatorPeer = context.adminBootstrap,
|
|
354
|
+
writerKeyBuffer = null,
|
|
355
|
+
expectedBalanceBuffer = null,
|
|
356
|
+
expectedLicenseBuffer = null,
|
|
357
|
+
payload = null,
|
|
358
|
+
skipSync = false
|
|
359
|
+
} = {}
|
|
360
|
+
) {
|
|
361
|
+
const writerAddress = readerPeer.wallet.address;
|
|
362
|
+
const writingKey = writerKeyBuffer ?? readerPeer.base.local.key;
|
|
363
|
+
await assertWriterDowngradedEntry(
|
|
364
|
+
t,
|
|
365
|
+
validatorPeer.base,
|
|
366
|
+
writerAddress,
|
|
367
|
+
writingKey,
|
|
368
|
+
expectedBalanceBuffer,
|
|
369
|
+
expectedLicenseBuffer
|
|
370
|
+
);
|
|
371
|
+
|
|
372
|
+
if (payload) {
|
|
373
|
+
await assertReplayProtection(t, validatorPeer.base, payload);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (!skipSync) {
|
|
377
|
+
await context.sync();
|
|
378
|
+
await assertWriterDowngradedEntry(
|
|
379
|
+
t,
|
|
380
|
+
readerPeer.base,
|
|
381
|
+
writerAddress,
|
|
382
|
+
writingKey,
|
|
383
|
+
expectedBalanceBuffer,
|
|
384
|
+
expectedLicenseBuffer
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export async function applyWithRoleAccessBypass(context, invalidPayload) {
|
|
390
|
+
const node = context.bootstrap ?? context.adminBootstrap;
|
|
391
|
+
const state = node.state;
|
|
392
|
+
const originalValidate = state.check.validateRoleAccessOperation;
|
|
393
|
+
state.check.validateRoleAccessOperation = () => true;
|
|
394
|
+
try {
|
|
395
|
+
await node.base.append(invalidPayload);
|
|
396
|
+
await node.base.update();
|
|
397
|
+
await eventFlush();
|
|
398
|
+
} finally {
|
|
399
|
+
state.check.validateRoleAccessOperation = originalValidate;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
export async function applyWithMissingComponentBypass(context, invalidPayload) {
|
|
404
|
+
const node = context.bootstrap ?? context.adminBootstrap;
|
|
405
|
+
const state = node.state;
|
|
406
|
+
const originalValidate = state.check.validateRoleAccessOperation;
|
|
407
|
+
const originalHasOwn = Object.hasOwn;
|
|
408
|
+
Object.hasOwn = (obj, prop) => {
|
|
409
|
+
if (prop === 'vs') return false;
|
|
410
|
+
return originalHasOwn(obj, prop);
|
|
411
|
+
};
|
|
412
|
+
state.check.validateRoleAccessOperation = () => true;
|
|
413
|
+
try {
|
|
414
|
+
await node.base.append(invalidPayload);
|
|
415
|
+
await node.base.update();
|
|
416
|
+
await eventFlush();
|
|
417
|
+
} finally {
|
|
418
|
+
state.check.validateRoleAccessOperation = originalValidate;
|
|
419
|
+
Object.hasOwn = originalHasOwn;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
export async function applyWithRequesterEntryRemoval(context, invalidPayload, { peer = null } = {}) {
|
|
424
|
+
const writerPeer = peer ?? selectWriterPeer(context);
|
|
425
|
+
await withPeerEntryOverrideOnApply({
|
|
426
|
+
context,
|
|
427
|
+
peer: writerPeer,
|
|
428
|
+
mutateEntry: () => null,
|
|
429
|
+
fn: async node => {
|
|
430
|
+
await node.base.append(invalidPayload);
|
|
431
|
+
await node.base.update();
|
|
432
|
+
await eventFlush();
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
export async function applyWithRequesterEntryCorruption(
|
|
438
|
+
context,
|
|
439
|
+
invalidPayload,
|
|
440
|
+
{ peer = null } = {}
|
|
441
|
+
) {
|
|
442
|
+
const writerPeer = peer ?? selectWriterPeer(context);
|
|
443
|
+
await withPeerEntryOverrideOnApply({
|
|
444
|
+
context,
|
|
445
|
+
peer: writerPeer,
|
|
446
|
+
mutateEntry: entry => {
|
|
447
|
+
if (!entry?.value) {
|
|
448
|
+
throw new Error('Requester entry corruption requires an existing entry.');
|
|
449
|
+
}
|
|
450
|
+
return { ...entry, value: b4a.alloc(1) };
|
|
451
|
+
},
|
|
452
|
+
fn: async node => {
|
|
453
|
+
await node.base.append(invalidPayload);
|
|
454
|
+
await node.base.update();
|
|
455
|
+
await eventFlush();
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
export async function applyWithRequesterWriterKeyMismatch(context, invalidPayload) {
|
|
461
|
+
const writerPeer = selectWriterPeer(context);
|
|
462
|
+
await withPeerEntryOverrideOnApply({
|
|
463
|
+
context,
|
|
464
|
+
peer: writerPeer,
|
|
465
|
+
mutateEntry: entry => {
|
|
466
|
+
if (!entry?.value) {
|
|
467
|
+
throw new Error('Requester writer key mutation requires an existing entry.');
|
|
468
|
+
}
|
|
469
|
+
const decoded = nodeEntryUtils.decode(entry.value);
|
|
470
|
+
if (!decoded?.wk) {
|
|
471
|
+
throw new Error('Requester writer key mutation requires a decodable entry.');
|
|
472
|
+
}
|
|
473
|
+
const mutatedWk = b4a.from(decoded.wk);
|
|
474
|
+
mutatedWk[0] ^= 0xff;
|
|
475
|
+
const mutatedEntry = nodeEntryUtils.setWritingKey(b4a.from(entry.value), mutatedWk);
|
|
476
|
+
if (!mutatedEntry) {
|
|
477
|
+
throw new Error('Failed to mutate requester writing key.');
|
|
478
|
+
}
|
|
479
|
+
return { ...entry, value: mutatedEntry };
|
|
480
|
+
},
|
|
481
|
+
fn: async node => {
|
|
482
|
+
await node.base.append(invalidPayload);
|
|
483
|
+
await node.base.update();
|
|
484
|
+
await eventFlush();
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
export async function applyWithStakeEntryMutation(
|
|
490
|
+
context,
|
|
491
|
+
invalidPayload,
|
|
492
|
+
mutateNodeEntryBuffer,
|
|
493
|
+
mutateDecodedEntry = null
|
|
494
|
+
) {
|
|
495
|
+
if (typeof mutateNodeEntryBuffer !== 'function') {
|
|
496
|
+
throw new Error('Stake entry mutation requires a mutateNodeEntryBuffer function.');
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const writerPeer = selectWriterPeer(context);
|
|
500
|
+
const balancePrototype = Object.getPrototypeOf(BALANCE_ZERO);
|
|
501
|
+
const originalUpdate = balancePrototype.update;
|
|
502
|
+
const originalDecode = nodeEntryUtils.decode;
|
|
503
|
+
let hasMutated = false;
|
|
504
|
+
|
|
505
|
+
await withPeerEntryOverrideOnApply({
|
|
506
|
+
context,
|
|
507
|
+
peer: writerPeer,
|
|
508
|
+
mutateEntry: entry => {
|
|
509
|
+
if (!entry?.value) {
|
|
510
|
+
throw new Error('Stake entry mutation requires an existing entry.');
|
|
511
|
+
}
|
|
512
|
+
return entry;
|
|
513
|
+
},
|
|
514
|
+
fn: async node => {
|
|
515
|
+
balancePrototype.update = function patchedUpdate(nodeEntryBuffer) {
|
|
516
|
+
const result = originalUpdate.call(this, nodeEntryBuffer);
|
|
517
|
+
if (!hasMutated) {
|
|
518
|
+
hasMutated = true;
|
|
519
|
+
const mutatedEntry =
|
|
520
|
+
mutateNodeEntryBuffer(result ?? nodeEntryBuffer, nodeEntryBuffer) ??
|
|
521
|
+
result ??
|
|
522
|
+
nodeEntryBuffer;
|
|
523
|
+
|
|
524
|
+
if (mutateDecodedEntry && mutatedEntry && typeof mutatedEntry === 'object') {
|
|
525
|
+
mutatedEntry[STAKE_ENTRY_MARK] = true;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
return mutatedEntry;
|
|
529
|
+
}
|
|
530
|
+
return result;
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
if (typeof mutateDecodedEntry === 'function') {
|
|
534
|
+
nodeEntryUtils.decode = function patchedDecode(buffer) {
|
|
535
|
+
const decoded = originalDecode(buffer);
|
|
536
|
+
if (buffer?.[STAKE_ENTRY_MARK]) {
|
|
537
|
+
const mutatedDecoded = mutateDecodedEntry(decoded, buffer);
|
|
538
|
+
if (mutatedDecoded && typeof mutatedDecoded === 'object') {
|
|
539
|
+
return mutatedDecoded;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
return decoded;
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
try {
|
|
547
|
+
await node.base.append(invalidPayload);
|
|
548
|
+
await node.base.update();
|
|
549
|
+
await eventFlush();
|
|
550
|
+
} finally {
|
|
551
|
+
balancePrototype.update = originalUpdate;
|
|
552
|
+
nodeEntryUtils.decode = originalDecode;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
async function assertWriterDowngradedEntry(
|
|
559
|
+
t,
|
|
560
|
+
base,
|
|
561
|
+
address,
|
|
562
|
+
writingKey,
|
|
563
|
+
expectedBalanceBuffer,
|
|
564
|
+
expectedLicenseBuffer
|
|
565
|
+
) {
|
|
566
|
+
const entry = await base.view.get(address);
|
|
567
|
+
t.ok(entry, 'writer node entry exists');
|
|
568
|
+
const decoded = nodeEntryUtils.decode(entry.value);
|
|
569
|
+
t.ok(decoded, 'writer node entry decodes');
|
|
570
|
+
t.is(decoded.isWhitelisted, true, 'node remains whitelisted');
|
|
571
|
+
t.is(decoded.isWriter, false, 'writer role removed');
|
|
572
|
+
t.is(decoded.isIndexer, false, 'node not promoted to indexer');
|
|
573
|
+
t.ok(b4a.equals(decoded.wk, writingKey), 'writer key preserved on entry');
|
|
574
|
+
t.ok(
|
|
575
|
+
b4a.equals(decoded.stakedBalance, BALANCE_ZERO.value),
|
|
576
|
+
'writer staked balance cleared'
|
|
577
|
+
);
|
|
578
|
+
t.ok(!b4a.equals(decoded.license, ZERO_LICENSE), 'license retained after downgrade');
|
|
579
|
+
if (expectedLicenseBuffer) {
|
|
580
|
+
t.ok(
|
|
581
|
+
b4a.equals(decoded.license, expectedLicenseBuffer),
|
|
582
|
+
'writer license remains unchanged after downgrade'
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
if (expectedBalanceBuffer) {
|
|
586
|
+
t.ok(
|
|
587
|
+
b4a.equals(decoded.balance, expectedBalanceBuffer),
|
|
588
|
+
'writer liquid balance matches expected amount after downgrade'
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
const addressBuffer = addressUtils.addressToBuffer(address);
|
|
592
|
+
const writerRegistryEntry = await base.view.get(
|
|
593
|
+
EntryType.WRITER_ADDRESS + writingKey.toString('hex')
|
|
594
|
+
);
|
|
595
|
+
t.ok(writerRegistryEntry, 'writer registry entry persists for ownership tracking');
|
|
596
|
+
t.ok(
|
|
597
|
+
b4a.equals(writerRegistryEntry.value, addressBuffer),
|
|
598
|
+
'writer registry continues to link downgraded node to its previous writing key'
|
|
599
|
+
);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
export const defaultWriterFunding = DEFAULT_WRITER_FUNDING;
|
|
603
|
+
|
|
604
|
+
export default {
|
|
605
|
+
setupAddWriterScenario,
|
|
606
|
+
selectWriterPeer,
|
|
607
|
+
selectValidatorPeerWithoutEntry,
|
|
608
|
+
buildAddWriterPayload,
|
|
609
|
+
buildAddWriterPayloadWithTxValidity,
|
|
610
|
+
buildRemoveWriterPayload,
|
|
611
|
+
assertAddWriterSuccessState,
|
|
612
|
+
assertAddWriterFailureState,
|
|
613
|
+
assertWriterRemovalState,
|
|
614
|
+
assertValidatorReward,
|
|
615
|
+
applyWithRoleAccessBypass,
|
|
616
|
+
applyWithMissingComponentBypass,
|
|
617
|
+
applyWithRequesterEntryRemoval,
|
|
618
|
+
applyWithRequesterEntryCorruption,
|
|
619
|
+
applyWithRequesterWriterKeyMismatch,
|
|
620
|
+
applyWithRequesterRoleOverride,
|
|
621
|
+
mutateValidatorEntry,
|
|
622
|
+
defaultWriterFunding
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
function assertWritableNode(node) {
|
|
626
|
+
if (!node.base.view.batch) {
|
|
627
|
+
throw new Error('Validator entry mutation requires a writable node.');
|
|
628
|
+
}
|
|
629
|
+
return node;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
async function writeValidatorEntry(base, key, value) {
|
|
633
|
+
const batch = base.view.batch();
|
|
634
|
+
await batch.put(key, value);
|
|
635
|
+
await batch.flush();
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
async function withPeerEntryOverrideOnApply({
|
|
639
|
+
context,
|
|
640
|
+
peer,
|
|
641
|
+
selectNode = defaultSelectNode,
|
|
642
|
+
mutateEntry,
|
|
643
|
+
fn
|
|
644
|
+
}) {
|
|
645
|
+
if (typeof mutateEntry !== 'function') {
|
|
646
|
+
throw new Error('Peer entry override requires a mutateEntry function.');
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
const targetPeer = peer ?? selectWriterPeer(context);
|
|
650
|
+
if (!targetPeer?.wallet?.address) {
|
|
651
|
+
throw new Error('Peer entry override requires a target peer.');
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const node = assertWritableNode(selectNode(context));
|
|
655
|
+
const base = node.base;
|
|
656
|
+
const targetAddress = targetPeer.wallet.address;
|
|
657
|
+
const targetBuffer = addressUtils.addressToBuffer(targetAddress);
|
|
658
|
+
const originalApply = base._handlers.apply;
|
|
659
|
+
|
|
660
|
+
base._handlers.apply = async function patchedApply(nodes, view, baseCtx) {
|
|
661
|
+
const originalBatch = view.batch;
|
|
662
|
+
view.batch = function patchedBatch(...args) {
|
|
663
|
+
const batch = originalBatch.apply(this, args);
|
|
664
|
+
if (!batch?.get) {
|
|
665
|
+
return batch;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
const originalGet = batch.get.bind(batch);
|
|
669
|
+
batch.get = async key => {
|
|
670
|
+
if (isTargetKey(key, targetAddress, targetBuffer)) {
|
|
671
|
+
const entry = await originalGet(key);
|
|
672
|
+
return mutateEntry(entry);
|
|
673
|
+
}
|
|
674
|
+
return originalGet(key);
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
return batch;
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
try {
|
|
681
|
+
return await originalApply.call(this, nodes, view, baseCtx);
|
|
682
|
+
} finally {
|
|
683
|
+
view.batch = originalBatch;
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
try {
|
|
688
|
+
await fn(node, targetPeer);
|
|
689
|
+
} finally {
|
|
690
|
+
base._handlers.apply = originalApply;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
function isTargetKey(key, targetAddressString, targetAddressBuffer) {
|
|
695
|
+
if (typeof key === 'string') {
|
|
696
|
+
return key === targetAddressString;
|
|
697
|
+
}
|
|
698
|
+
if (b4a.isBuffer(key) && targetAddressBuffer) {
|
|
699
|
+
return b4a.equals(key, targetAddressBuffer);
|
|
700
|
+
}
|
|
701
|
+
return false;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
export async function applyWithRequesterRoleOverride(context, invalidPayload, role) {
|
|
705
|
+
const writerPeer = selectWriterPeer(context);
|
|
706
|
+
await withPeerEntryOverrideOnApply({
|
|
707
|
+
context,
|
|
708
|
+
peer: writerPeer,
|
|
709
|
+
mutateEntry: entry => {
|
|
710
|
+
if (!entry?.value) {
|
|
711
|
+
throw new Error('Requester role override requires an existing entry.');
|
|
712
|
+
}
|
|
713
|
+
const mutatedValue = nodeEntryUtils.setRole(b4a.from(entry.value), role);
|
|
714
|
+
if (!mutatedValue) {
|
|
715
|
+
throw new Error('Failed to mutate requester role entry.');
|
|
716
|
+
}
|
|
717
|
+
return { ...entry, value: mutatedValue };
|
|
718
|
+
},
|
|
719
|
+
fn: async node => {
|
|
720
|
+
await node.base.append(invalidPayload);
|
|
721
|
+
await node.base.update();
|
|
722
|
+
await eventFlush();
|
|
723
|
+
}
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
export async function mutateValidatorEntry({
|
|
728
|
+
context,
|
|
729
|
+
selectNode = defaultSelectNode,
|
|
730
|
+
mutateValue,
|
|
731
|
+
fn
|
|
732
|
+
}) {
|
|
733
|
+
const node = assertWritableNode(selectNode(context));
|
|
734
|
+
const validatorAddress = node.wallet.address;
|
|
735
|
+
const originalEntry = await node.base.view.get(validatorAddress);
|
|
736
|
+
if (!originalEntry?.value) {
|
|
737
|
+
throw new Error('Validator entry mutation requires an existing validator entry.');
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
const originalValue = b4a.from(originalEntry.value);
|
|
741
|
+
const mutatedValue = mutateValue(b4a.from(originalValue));
|
|
742
|
+
if (!b4a.isBuffer(mutatedValue) || mutatedValue.length === 0) {
|
|
743
|
+
throw new Error('Validator entry mutation must return a non-empty buffer.');
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
await writeValidatorEntry(node.base, validatorAddress, mutatedValue);
|
|
747
|
+
|
|
748
|
+
try {
|
|
749
|
+
await fn(node);
|
|
750
|
+
} finally {
|
|
751
|
+
await writeValidatorEntry(node.base, validatorAddress, originalValue);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
function defaultSelectNode(context) {
|
|
756
|
+
return context.adminBootstrap ?? context.bootstrap ?? context.peers?.[0] ?? null;
|
|
757
|
+
}
|