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