trac-msb 0.2.6 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/.github/workflows/publish.yml +9 -16
  2. package/docs/networking-dualstack-plan.md +75 -0
  3. package/docs/networking-layer-redesign.md +155 -0
  4. package/msb.mjs +11 -23
  5. package/package.json +2 -3
  6. package/rpc/{create_server.mjs → create_server.js} +2 -2
  7. package/rpc/{handlers.mjs → handlers.js} +5 -5
  8. package/rpc/routes/{index.mjs → index.js} +1 -1
  9. package/rpc/routes/{v1.mjs → v1.js} +1 -1
  10. package/rpc/{rpc_server.mjs → rpc_server.js} +1 -1
  11. package/rpc/rpc_services.js +4 -4
  12. package/src/config/config.js +137 -0
  13. package/src/config/env.js +61 -0
  14. package/src/core/network/Network.js +131 -72
  15. package/src/core/network/identity/NetworkWalletFactory.js +3 -4
  16. package/src/core/network/messaging/NetworkMessages.js +12 -11
  17. package/src/core/network/messaging/handlers/GetRequestHandler.js +5 -4
  18. package/src/core/network/messaging/handlers/ResponseHandler.js +4 -5
  19. package/src/core/network/messaging/handlers/RoleOperationHandler.js +17 -19
  20. package/src/core/network/messaging/handlers/SubnetworkOperationHandler.js +44 -38
  21. package/src/core/network/messaging/handlers/TransferOperationHandler.js +29 -25
  22. package/src/core/network/messaging/handlers/base/BaseOperationHandler.js +20 -21
  23. package/src/core/network/messaging/routes/NetworkMessageRouter.js +24 -20
  24. package/src/core/network/messaging/validators/AdminResponse.js +2 -2
  25. package/src/core/network/messaging/validators/CustomNodeResponse.js +2 -2
  26. package/src/core/network/messaging/validators/PartialBootstrapDeployment.js +3 -3
  27. package/src/core/network/messaging/validators/PartialRoleAccess.js +15 -12
  28. package/src/core/network/messaging/validators/PartialTransaction.js +9 -10
  29. package/src/core/network/messaging/validators/PartialTransfer.js +10 -7
  30. package/src/core/network/messaging/validators/ValidatorResponse.js +2 -2
  31. package/src/core/network/messaging/validators/base/BaseResponse.js +13 -5
  32. package/src/core/network/messaging/validators/base/PartialOperation.js +37 -21
  33. package/src/core/network/services/ConnectionManager.js +248 -62
  34. package/src/core/network/services/MessageOrchestrator.js +83 -0
  35. package/src/core/network/services/TransactionPoolService.js +9 -8
  36. package/src/core/network/services/ValidatorObserverService.js +95 -34
  37. package/src/core/state/State.js +136 -139
  38. package/src/core/state/utils/address.js +18 -16
  39. package/src/core/state/utils/adminEntry.js +17 -16
  40. package/src/core/state/utils/deploymentEntry.js +15 -15
  41. package/src/core/state/utils/transaction.js +3 -95
  42. package/src/index.js +153 -201
  43. package/src/messages/completeStateMessages/CompleteStateMessageBuilder.js +36 -32
  44. package/src/messages/completeStateMessages/CompleteStateMessageOperations.js +39 -42
  45. package/src/messages/partialStateMessages/PartialStateMessageBuilder.js +20 -20
  46. package/src/messages/partialStateMessages/PartialStateMessageOperations.js +29 -22
  47. package/src/utils/check.js +21 -17
  48. package/src/utils/cliCommands.js +11 -11
  49. package/src/utils/constants.js +2 -9
  50. package/src/utils/fileUtils.js +1 -4
  51. package/src/utils/helpers.js +9 -20
  52. package/src/utils/migrationUtils.js +2 -2
  53. package/src/utils/normalizers.js +10 -9
  54. package/tests/acceptance/v1/account/account.test.mjs +2 -2
  55. package/tests/acceptance/v1/balance/balance.test.mjs +1 -1
  56. package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +11 -2
  57. package/tests/acceptance/v1/rpc.test.mjs +9 -9
  58. package/tests/acceptance/v1/tx/tx.test.mjs +4 -2
  59. package/tests/acceptance/v1/tx-details/tx-details.test.mjs +7 -3
  60. package/tests/fixtures/check.fixtures.js +42 -42
  61. package/tests/fixtures/protobuf.fixtures.js +27 -26
  62. package/tests/helpers/StateNetworkFactory.js +3 -5
  63. package/tests/helpers/autobaseTestHelpers.js +48 -2
  64. package/tests/helpers/config.js +3 -0
  65. package/tests/helpers/setupApplyTests.js +89 -82
  66. package/tests/helpers/transactionPayloads.mjs +26 -12
  67. package/tests/integration/apply/addAdmin/addAdminBasic.test.js +10 -9
  68. package/tests/integration/apply/addAdmin/addAdminRecovery.test.js +20 -19
  69. package/tests/integration/apply/addIndexer.test.js +23 -21
  70. package/tests/integration/apply/addWhitelist.test.js +9 -9
  71. package/tests/integration/apply/addWriter.test.js +33 -32
  72. package/tests/integration/apply/banValidator.test.js +16 -9
  73. package/tests/integration/apply/postTx/invalidSubValues.test.js +4 -4
  74. package/tests/integration/apply/postTx/postTx.test.js +7 -33
  75. package/tests/integration/apply/removeIndexer.test.js +11 -7
  76. package/tests/integration/apply/removeWriter.test.js +20 -19
  77. package/tests/integration/apply/transfer.test.js +18 -16
  78. package/tests/unit/messageOperations/assembleAddIndexerMessage.test.js +2 -2
  79. package/tests/unit/messageOperations/assembleAddWriterMessage.test.js +2 -1
  80. package/tests/unit/messageOperations/assembleAdminMessage.test.js +9 -10
  81. package/tests/unit/messageOperations/assembleBanWriterMessage.test.js +3 -2
  82. package/tests/unit/messageOperations/assemblePostTransaction.test.js +25 -43
  83. package/tests/unit/messageOperations/assembleRemoveIndexerMessage.test.js +2 -2
  84. package/tests/unit/messageOperations/assembleRemoveWriterMessage.test.js +2 -2
  85. package/tests/unit/messageOperations/assembleWhitelistMessages.test.js +5 -4
  86. package/tests/unit/messageOperations/commonsStateMessageOperationsTest.js +4 -3
  87. package/tests/unit/network/ConnectionManager.test.js +41 -70
  88. package/tests/unit/network/NetworkWalletFactory.test.js +14 -14
  89. package/tests/unit/state/apply/addAdmin/addAdminHappyPathScenario.js +6 -6
  90. package/tests/unit/state/apply/addAdmin/addAdminScenarioHelpers.js +8 -8
  91. package/tests/unit/state/apply/addAdmin/state.apply.addAdmin.test.js +6 -5
  92. package/tests/unit/state/apply/addIndexer/addIndexerScenarioHelpers.js +24 -23
  93. package/tests/unit/state/apply/addWriter/addWriterScenarioHelpers.js +10 -16
  94. package/tests/unit/state/apply/addWriter/addWriterValidatorRewardScenario.js +2 -1
  95. package/tests/unit/state/apply/adminRecovery/adminRecoveryScenarioHelpers.js +45 -41
  96. package/tests/unit/state/apply/adminRecovery/state.apply.adminRecovery.test.js +3 -7
  97. package/tests/unit/state/apply/appendWhitelist/appendWhitelistScenarioHelpers.js +17 -16
  98. package/tests/unit/state/apply/balanceInitialization/balanceInitializationScenarioHelpers.js +3 -4
  99. package/tests/unit/state/apply/balanceInitialization/nodeEntryBalanceUpdateFailureScenario.js +2 -1
  100. package/tests/unit/state/apply/banValidator/banValidatorBanAndReWhitelistScenario.js +2 -1
  101. package/tests/unit/state/apply/banValidator/banValidatorScenarioHelpers.js +23 -25
  102. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentDuplicateRegistrationScenario.js +2 -1
  103. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentScenarioHelpers.js +19 -18
  104. package/tests/unit/state/apply/common/access-control/adminConsistencyMismatchScenario.js +5 -4
  105. package/tests/unit/state/apply/common/access-control/adminPublicKeyDecodeFailureScenario.js +4 -3
  106. package/tests/unit/state/apply/common/balances/base/requesterBalanceScenarioBase.js +2 -1
  107. package/tests/unit/state/apply/common/commonScenarioHelper.js +3 -4
  108. package/tests/unit/state/apply/common/payload-structure/initializationDisabledScenario.js +2 -2
  109. package/tests/unit/state/apply/common/payload-structure/invalidHashValidationScenario.js +2 -2
  110. package/tests/unit/state/apply/common/requester/requesterNodeEntryBufferMissingScenario.js +2 -1
  111. package/tests/unit/state/apply/common/requester/requesterNodeEntryDecodeFailureScenario.js +2 -1
  112. package/tests/unit/state/apply/common/validatorConsistency/base/validatorConsistencyScenarioBase.js +2 -1
  113. package/tests/unit/state/apply/common/validatorEntryValidation/base/validatorEntryValidationScenarioBase.js +2 -1
  114. package/tests/unit/state/apply/disableInitialization/disableInitializationScenarioHelpers.js +11 -10
  115. package/tests/unit/state/apply/removeIndexer/removeIndexerScenarioHelpers.js +6 -5
  116. package/tests/unit/state/apply/removeWriter/removeWriterScenarioHelpers.js +6 -7
  117. package/tests/unit/state/apply/transfer/transferDoubleSpendAcrossValidatorsScenario.js +35 -34
  118. package/tests/unit/state/apply/transfer/transferScenarioHelpers.js +44 -43
  119. package/tests/unit/state/apply/txOperation/txOperationScenarioHelpers.js +26 -25
  120. package/tests/unit/state/apply/txOperation/txOperationTransferFeeGuardScenarioFactory.js +2 -1
  121. package/tests/unit/state/stateModule.test.js +0 -1
  122. package/tests/unit/state/stateTestUtils.js +7 -3
  123. package/tests/unit/state/utils/address.test.js +3 -3
  124. package/tests/unit/state/utils/adminEntry.test.js +10 -9
  125. package/tests/unit/utils/check/adminControlOperation.test.js +3 -3
  126. package/tests/unit/utils/check/balanceInitializationOperation.test.js +2 -2
  127. package/tests/unit/utils/check/bootstrapDeploymentOperation.test.js +2 -3
  128. package/tests/unit/utils/check/common.test.js +7 -6
  129. package/tests/unit/utils/check/coreAdminOperation.test.js +3 -3
  130. package/tests/unit/utils/check/roleAccessOperation.test.js +3 -2
  131. package/tests/unit/utils/check/transactionOperation.test.js +3 -3
  132. package/tests/unit/utils/check/transferOperation.test.js +3 -3
  133. package/tests/unit/utils/fileUtils/readAddressesFromWhitelistFile.test.js +2 -1
  134. package/tests/unit/utils/fileUtils/readBalanceMigrationFile.test.js +2 -1
  135. package/tests/unit/utils/migrationUtils/validateAddressFromIncomingFile.test.js +7 -0
  136. package/tests/unit/utils/utils.test.js +0 -1
  137. package/src/core/state/utils/indexerEntry.js +0 -105
  138. package/src/utils/crypto.js +0 -11
  139. package/tests/unit/state/utils/indexerEntry.test.js +0 -83
  140. package/tests/unit/state/utils/transaction.test.js +0 -97
  141. package/tests/unit/utils/crypto/createHash.test.js +0 -15
  142. /package/rpc/{constants.mjs → constants.js} +0 -0
  143. /package/rpc/{cors.mjs → cors.js} +0 -0
  144. /package/rpc/utils/{confirmedParameter.mjs → confirmedParameter.js} +0 -0
  145. /package/rpc/utils/{helpers.mjs → helpers.js} +0 -0
  146. /package/rpc/utils/{url.mjs → url.js} +0 -0
@@ -1,6 +1,5 @@
1
1
  import test from 'brittle';
2
2
  import b4a from 'b4a';
3
-
4
3
  import CompleteStateMessageOperations from '../../src/messages/completeStateMessages/CompleteStateMessageOperations.js';
5
4
  import {default as fixtures} from '../fixtures/assembleMessage.fixtures.js';
6
5
  import {OperationType} from "../../src/utils/constants.js";
@@ -8,6 +7,7 @@ import {bufferToAddress} from "../../src/core/state/utils/address.js";
8
7
  import {safeDecodeApplyOperation} from '../../src/utils/protobuf/operationHelpers.js';
9
8
  import {isAddressValid} from "../../src/core/state/utils/address.js";
10
9
  import {errorMessageIncludes} from "../utils/regexHelper.js";
10
+ import { config } from '../../helpers/config.js';
11
11
 
12
12
  test('assembleAdminMessage', async (t) => {
13
13
  await fixtures.initAll();
@@ -18,16 +18,15 @@ test('assembleAdminMessage', async (t) => {
18
18
 
19
19
 
20
20
  t.test('assembleAdminMessage - setup admin', async (k) => {
21
-
22
- const msg = safeDecodeApplyOperation(await CompleteStateMessageOperations.assembleAddAdminMessage(walletAdmin, writingKeyAdmin));
21
+ const msg = safeDecodeApplyOperation(await new CompleteStateMessageOperations(walletAdmin, config).assembleAddAdminMessage(writingKeyAdmin));
23
22
 
24
23
  k.ok(msg, 'Message should be created');
25
24
  k.is(Object.keys(msg).length, 3, 'Message should have 3 keys');
26
25
  k.is(Object.keys(msg.eko).length, 3, 'Message value have 3 keys');
27
26
  k.is(msg.type, OperationType.ADD_ADMIN, 'Message type should be ADD_ADMIN');
28
- k.is(bufferToAddress(msg.address), walletAdmin.address, 'Message address should be the public key of the wallet');
27
+ k.is(bufferToAddress(msg.address, config.addressPrefix), walletAdmin.address, 'Message address should be the public key of the wallet');
29
28
 
30
- k.ok(isAddressValid(msg.address), 'Message address should be a valid address');
29
+ k.ok(isAddressValid(msg.address, config.addressPrefix), 'Message address should be a valid address');
31
30
 
32
31
  k.ok(b4a.equals(msg.eko.wk, writingKeyAdmin), 'Message wk should be the writing key');
33
32
  k.is(msg.eko.nonce.length, 32, 'Message nonce should be 32 bytes long');
@@ -37,15 +36,15 @@ test('assembleAdminMessage', async (t) => {
37
36
  });
38
37
 
39
38
  t.test('assembleAdminMessage - admin recovery message', async (k) => {
40
- const msg = safeDecodeApplyOperation(await CompleteStateMessageOperations.assembleAddAdminMessage(walletAdmin, writingKeyNonAdmin));
39
+ const msg = safeDecodeApplyOperation(await new CompleteStateMessageOperations(walletAdmin, config).assembleAddAdminMessage(writingKeyNonAdmin));
41
40
 
42
41
  k.ok(msg, 'Message should be created');
43
42
  k.is(Object.keys(msg).length, 3, 'Message should have 3 keys');
44
43
  k.is(Object.keys(msg.eko).length, 3, 'Message value have 3 keys');
45
44
  k.is(msg.type, OperationType.ADD_ADMIN, 'Message type should be ADD_ADMIN');
46
45
 
47
- k.is(bufferToAddress(msg.address), walletAdmin.address, 'Message address should be address of the wallet');
48
- k.ok(isAddressValid(msg.address), 'Message address should be a valid address');
46
+ k.is(bufferToAddress(msg.address, config.addressPrefix), walletAdmin.address, 'Message address should be address of the wallet');
47
+ k.ok(isAddressValid(msg.address, config.addressPrefix), 'Message address should be a valid address');
49
48
 
50
49
  k.ok(b4a.equals(msg.eko.wk, writingKeyNonAdmin), 'Message wk should be the writing key');
51
50
  k.is(msg.eko.sig.length, 64, 'Message signature should be 64 bytes long')
@@ -55,14 +54,14 @@ test('assembleAdminMessage', async (t) => {
55
54
 
56
55
  t.test('assembleAdminMessage - writer key is null', async (k) => {
57
56
  await k.exception(
58
- async () => await CompleteStateMessageOperations.assembleAddAdminMessage(walletAdmin, null),
57
+ async () => await new CompleteStateMessageOperations(walletAdmin, config).assembleAddAdminMessage(null),
59
58
  errorMessageIncludes('Writer key must be a 32 length buffer')
60
59
  );
61
60
  });
62
61
 
63
62
  t.test("assembleAdminMessage - admin wallet is null", async (k) => {
64
63
  await k.exception(
65
- async () => await CompleteStateMessageOperations.assembleAddAdminMessage(null, writingKeyAdmin),
64
+ async () => await new CompleteStateMessageOperations(null, config).assembleAddAdminMessage(writingKeyAdmin),
66
65
  errorMessageIncludes('Wallet must be a valid wallet object')
67
66
  );
68
67
  });
@@ -4,13 +4,14 @@ import { OperationType } from '../../src/utils/protobuf/applyOperations.cjs';
4
4
  import { writingKeyNonAdmin, walletNonAdmin, initAll, walletAdmin } from '../fixtures/assembleMessage.fixtures.js';
5
5
  import { messageOperationsBkoTest } from './commonsStateMessageOperationsTest.js';
6
6
  import { safeDecodeApplyOperation } from '../../src/utils/protobuf/operationHelpers.js';
7
+ import { config } from '../../helpers/config.js';
7
8
 
8
9
  const testName = 'assembleBanWriterMessage';
9
10
  test(testName, async (t) => {
10
11
  await initAll();
11
12
  const assembler = async (wallet,address) => {
12
- return safeDecodeApplyOperation(await CompleteStateMessageOperations.assembleBanWriterMessage(wallet,address));
13
+ return safeDecodeApplyOperation(await new CompleteStateMessageOperations(wallet, config).assembleBanWriterMessage(address));
13
14
  }
14
15
  await messageOperationsBkoTest(t, testName, assembler, walletAdmin, writingKeyNonAdmin, OperationType.BAN_VALIDATOR, 2, walletNonAdmin.address);
15
16
 
16
- });
17
+ });
@@ -7,7 +7,8 @@ import b4a from 'b4a';
7
7
  import {safeDecodeApplyOperation} from '../../src/utils/protobuf/operationHelpers.js';
8
8
  import {isAddressValid} from "../../src/core/state/utils/address.js";
9
9
  import {errorMessageIncludes} from "../utils/regexHelper.js";
10
- import {generatePostTx, randomBytes} from "../../helpers/setupApplyTests.js";
10
+ import {randomBytes} from "../../helpers/setupApplyTests.js";
11
+ import { config } from '../../helpers/config.js';
11
12
 
12
13
  const msgTxoLength = 10;
13
14
  const opType = OperationType.TX;
@@ -26,8 +27,7 @@ test('assemblePostTxMessage - ....', async (k) => {
26
27
  const externalBootstrap = randomBytes(32);
27
28
  const msbBootstrap = randomBytes(32);
28
29
 
29
- const decodedPostTx = safeDecodeApplyOperation(await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
30
- nonAdminWallet,
30
+ const decodedPostTx = safeDecodeApplyOperation(await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
31
31
  validatorAddress,
32
32
  txHash,
33
33
  incomingAddress,
@@ -44,11 +44,11 @@ test('assemblePostTxMessage - ....', async (k) => {
44
44
 
45
45
  k.is(decodedPostTx.type, opType, `Message type should be ${opType}`);
46
46
 
47
- k.ok(isAddressValid(decodedPostTx.address), 'Message validator address should be a valid address');
48
- k.ok(isAddressValid(decodedPostTx.txo.ia), 'Message incoming address should be a valid address');
47
+ k.ok(isAddressValid(decodedPostTx.address, config.addressPrefix), 'Message validator address should be a valid address');
48
+ k.ok(isAddressValid(decodedPostTx.txo.ia, config.addressPrefix), 'Message incoming address should be a valid address');
49
49
 
50
- k.ok(bufferToAddress(decodedPostTx.txo.ia) === incomingAddress, 'Message incoming address should be the address of the peer wallet');
51
- k.ok(bufferToAddress(decodedPostTx.address) === validatorAddress, 'Message validator address should be the address of the non-admin wallet');
50
+ k.ok(bufferToAddress(decodedPostTx.txo.ia, config.addressPrefix) === incomingAddress, 'Message incoming address should be the address of the peer wallet');
51
+ k.ok(bufferToAddress(decodedPostTx.address, config.addressPrefix) === validatorAddress, 'Message validator address should be the address of the non-admin wallet');
52
52
 
53
53
  k.ok(b4a.isBuffer(decodedPostTx.txo.tx), 'tx should be a buffer');
54
54
  k.is(decodedPostTx.txo.tx.length, 32, 'tx should be 32 bytes long');
@@ -76,8 +76,7 @@ test('assemblePostTxMessage - ....', async (k) => {
76
76
  address: 'trac1y6kkq48fgu3ur'
77
77
  }
78
78
  await k.exception(
79
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
80
- invalidWallet,
79
+ async () => await new CompleteStateMessageOperations(invalidWallet, config).assembleCompleteTransactionOperationMessage(
81
80
  validatorAddress,
82
81
  txHash,
83
82
  incomingAddress,
@@ -97,8 +96,7 @@ test('assemblePostTxMessage - ....', async (k) => {
97
96
  address: 'testnet1y6kkq48fgu3urrhg0gm7h8zdyxl3gnaazd2u7568lfl5zxqs285q6kuljk'
98
97
  }
99
98
  await k.exception(
100
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
101
- invalidWallet,
99
+ async () => await new CompleteStateMessageOperations(invalidWallet, config).assembleCompleteTransactionOperationMessage(
102
100
  validatorAddress,
103
101
  txHash,
104
102
  incomingAddress,
@@ -118,8 +116,7 @@ test('assemblePostTxMessage - ....', async (k) => {
118
116
  address: ''
119
117
  }
120
118
  await k.exception(
121
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
122
- invalidWallet,
119
+ async () => await new CompleteStateMessageOperations(invalidWallet, config).assembleCompleteTransactionOperationMessage(
123
120
  validatorAddress,
124
121
  txHash,
125
122
  incomingAddress,
@@ -136,8 +133,7 @@ test('assemblePostTxMessage - ....', async (k) => {
136
133
 
137
134
  k.test(`assemblePostTxMessage - Invalid wallet instance - null Wallet `, async (k) => {
138
135
  await k.exception(
139
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
140
- null,
136
+ async () => await new CompleteStateMessageOperations(null, config).assembleCompleteTransactionOperationMessage(
141
137
  validatorAddress,
142
138
  txHash,
143
139
  incomingAddress,
@@ -154,8 +150,7 @@ test('assemblePostTxMessage - ....', async (k) => {
154
150
 
155
151
  k.test(`assemblePostTxMessage - Invalid wallet instance - undefined Wallet`, async (k) => {
156
152
  await k.exception(
157
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
158
- undefined,
153
+ async () => await new CompleteStateMessageOperations(undefined, config).assembleCompleteTransactionOperationMessage(
159
154
  validatorAddress,
160
155
  txHash,
161
156
  incomingAddress,
@@ -175,8 +170,7 @@ test('assemblePostTxMessage - ....', async (k) => {
175
170
  const invalid = 'trac1y6kkq48fgu3urrhg0gm7h8zdyxl3gnaazd2u7568lfl5zxqs285q6kuljką';
176
171
 
177
172
  await k.exception(
178
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
179
- nonAdminWallet,
173
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
180
174
  invalid,
181
175
  txHash,
182
176
  incomingAddress,
@@ -195,8 +189,7 @@ test('assemblePostTxMessage - ....', async (k) => {
195
189
  const invalid = 'trac1y6kkq48fgu3ur';
196
190
 
197
191
  await k.exception(
198
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
199
- nonAdminWallet,
192
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
200
193
  invalid,
201
194
  txHash,
202
195
  incomingAddress,
@@ -215,8 +208,7 @@ test('assemblePostTxMessage - ....', async (k) => {
215
208
  const invalid = 'testnet1y6kkq48fgu3urrhg0gm7h8zdyxl3gnaazd2u7568lfl5zxqs285q6kuljk';
216
209
 
217
210
  await k.exception(
218
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
219
- nonAdminWallet,
211
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
220
212
  invalid,
221
213
  txHash,
222
214
  incomingAddress,
@@ -235,8 +227,7 @@ test('assemblePostTxMessage - ....', async (k) => {
235
227
  const invalid = '';
236
228
 
237
229
  await k.exception(
238
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
239
- nonAdminWallet,
230
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
240
231
  invalid,
241
232
  txHash,
242
233
  incomingAddress,
@@ -253,8 +244,7 @@ test('assemblePostTxMessage - ....', async (k) => {
253
244
  k.test(`assemblePostTxMessage - Address parameter (validator address) - Null`, async (k) => {
254
245
 
255
246
  await k.exception(
256
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
257
- nonAdminWallet,
247
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
258
248
  null,
259
249
  txHash,
260
250
  incomingAddress,
@@ -270,8 +260,7 @@ test('assemblePostTxMessage - ....', async (k) => {
270
260
 
271
261
  k.test(`assemblePostTxMessage - Address parameter (validator address) - undefined`, async (k) => {
272
262
  await k.exception(
273
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
274
- nonAdminWallet,
263
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
275
264
  undefined,
276
265
  txHash,
277
266
  incomingAddress,
@@ -288,8 +277,7 @@ test('assemblePostTxMessage - ....', async (k) => {
288
277
  k.test(`assemblePostTxMessage - Address parameter (incoming address) - 'ą' does not belongs to the TRAC bench alphabet`, async (k) => {
289
278
  const invalid = 'trac1y6kkq48fgu3urrhg0gm7h8zdyxl3gnaazd2u7568lfl5zxqs285q6kuljką';
290
279
  await k.exception(
291
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
292
- nonAdminWallet,
280
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
293
281
  validatorAddress, // correct validator address
294
282
  txHash,
295
283
  invalid, // invalid incoming address
@@ -307,8 +295,7 @@ test('assemblePostTxMessage - ....', async (k) => {
307
295
  k.test(`assemblePostTxMessage - Address parameter (incoming address) - trac address is to short`, async (k) => {
308
296
  const invalid = 'trac1y6kkq48fgu3ur';
309
297
  await k.exception(
310
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
311
- nonAdminWallet,
298
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
312
299
  validatorAddress,
313
300
  txHash,
314
301
  invalid,
@@ -326,8 +313,7 @@ test('assemblePostTxMessage - ....', async (k) => {
326
313
  k.test(`assemblePostTxMessage- Address parameter (incoming address) - invalid prefix`, async (k) => {
327
314
  const invalid = 'testnet1y6kkq48fgu3urrhg0gm7h8zdyxl3gnaazd2u7568lfl5zxqs285q6kuljk';
328
315
  await k.exception(
329
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
330
- nonAdminWallet,
316
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
331
317
  validatorAddress,
332
318
  txHash,
333
319
  invalid,
@@ -344,8 +330,7 @@ test('assemblePostTxMessage - ....', async (k) => {
344
330
  k.test(`assemblePostTxMessage - Address parameter (incoming address) - empty string`, async (k) => {
345
331
  const invalid = '';
346
332
  await k.exception(
347
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
348
- nonAdminWallet,
333
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
349
334
  validatorAddress,
350
335
  txHash,
351
336
  invalid,
@@ -361,8 +346,7 @@ test('assemblePostTxMessage - ....', async (k) => {
361
346
 
362
347
  k.test(`assemblePostTxMessage - Address parameter (incoming address) - Null`, async (k) => {
363
348
  await k.exception(
364
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
365
- nonAdminWallet,
349
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
366
350
  validatorAddress,
367
351
  txHash,
368
352
  null,
@@ -378,8 +362,7 @@ test('assemblePostTxMessage - ....', async (k) => {
378
362
 
379
363
  k.test(`assemblePostTxMessage - Address parameter (incoming address) - undefined`, async (k) => {
380
364
  await k.exception(
381
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
382
- nonAdminWallet,
365
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
383
366
  validatorAddress,
384
367
  txHash,
385
368
  undefined,
@@ -421,8 +404,7 @@ test('assemblePostTxMessage - ....', async (k) => {
421
404
  };
422
405
  args[param.key] = invalid.value;
423
406
  await k.exception(
424
- async () => await CompleteStateMessageOperations.assembleCompleteTransactionOperationMessage(
425
- nonAdminWallet,
407
+ async () => await new CompleteStateMessageOperations(nonAdminWallet, config).assembleCompleteTransactionOperationMessage(
426
408
  validatorAddress,
427
409
  args.txHash,
428
410
  incomingAddress,
@@ -4,16 +4,16 @@ import { OperationType } from '../../src/utils/protobuf/applyOperations.cjs';
4
4
  import { writingKeyNonAdmin, walletNonAdmin, initAll ,walletAdmin} from '../fixtures/assembleMessage.fixtures.js';
5
5
  import { messageOperationsBkoTest } from './commonsStateMessageOperationsTest.js';
6
6
  import { safeDecodeApplyOperation } from '../../src/utils/protobuf/operationHelpers.js';
7
+ import { config } from '../../helpers/config.js';
7
8
 
8
9
  const testName = 'assembleRemoveIndexerMessage';
9
10
  test(testName, async (t) => {
10
11
  await initAll();
11
12
  const assembler = async (wallet,address) => {
12
- return safeDecodeApplyOperation(await CompleteStateMessageOperations.assembleRemoveIndexerMessage(wallet,address));
13
+ return safeDecodeApplyOperation(await new CompleteStateMessageOperations(wallet, config).assembleRemoveIndexerMessage(address));
13
14
  }
14
15
 
15
16
  await messageOperationsBkoTest(t, testName, assembler, walletAdmin, writingKeyNonAdmin, OperationType.REMOVE_INDEXER, 2, walletNonAdmin.address);
16
17
  });
17
18
 
18
19
 
19
-
@@ -4,13 +4,13 @@ import {OperationType} from '../../src/utils/protobuf/applyOperations.cjs';
4
4
  import {initAll, walletNonAdmin, writingKeyNonAdmin} from '../fixtures/assembleMessage.fixtures.js';
5
5
  import {messageOperationsEkoTest} from './commonsStateMessageOperationsTest.js';
6
6
  import {safeDecodeApplyOperation} from '../../src/utils/protobuf/operationHelpers.js';
7
-
7
+ import { config } from '../../helpers/config.js';
8
8
 
9
9
  const testName = 'assembleRemoveWriterMessage';
10
10
  test(testName, async (t) => {
11
11
  await initAll();
12
12
  const assembler = async (wallet, writingKey) => {
13
- return safeDecodeApplyOperation(await CompleteStateMessageOperations.assembleRemoveWriterMessage(wallet, writingKey));
13
+ return safeDecodeApplyOperation(await new CompleteStateMessageOperations(wallet, config).assembleRemoveWriterMessage(writingKey));
14
14
  }
15
15
  await messageOperationsEkoTest(t, testName, assembler, walletNonAdmin, writingKeyNonAdmin, OperationType.REMOVE_WRITER, 3, walletNonAdmin.address);
16
16
  });
@@ -7,6 +7,7 @@ import fileUtils from "../../src/utils/fileUtils.js";
7
7
  import CompleteStateMessageOperations from "../../src/messages/completeStateMessages/CompleteStateMessageOperations.js";
8
8
  import {bufferToAddress} from "../../src/core/state/utils/address.js";
9
9
  import {errorMessageIncludes} from "../utils/regexHelper.js";
10
+ import { config } from '../../helpers/config.js'
10
11
 
11
12
  // MOCK SETUP
12
13
  const whitelistAddresses = [
@@ -22,7 +23,7 @@ test('assembleWhitelistMessages', async (t) => {
22
23
 
23
24
 
24
25
  t.test('assembleWhitelistMessages - Happy Path', async (k) => {
25
- const mapMsg = await CompleteStateMessageOperations.assembleAppendWhitelistMessages(walletAdmin);
26
+ const mapMsg = await new CompleteStateMessageOperations(walletAdmin, config).assembleAppendWhitelistMessages();
26
27
  const msg = mapMsg.get(whitelistAddresses[0])
27
28
  k.ok(msg, 'Message should be created');
28
29
  k.ok(msg.length > 0, 'Message should be an array with at least one element');
@@ -31,7 +32,7 @@ test('assembleWhitelistMessages', async (t) => {
31
32
  k.is(Object.keys(decodedMsg.bko).length, 2, 'Message value should have 2 keys');
32
33
  k.is(decodedMsg.type, OperationType.APPEND_WHITELIST, 'Message type should be APPEND_WHITELIST');
33
34
 
34
- k.is(bufferToAddress(decodedMsg.address) , whitelistAddresses[0], 'Message address should be the address in the file');
35
+ k.is(bufferToAddress(decodedMsg.address, config.addressPrefix) , whitelistAddresses[0], 'Message address should be the address in the file');
35
36
  k.is(decodedMsg.bko.nonce.length, 32, 'Message nonce should be 32 bytes long');
36
37
  k.ok(b4a.isBuffer(decodedMsg.bko.nonce), 'Message nonce should be a buffer');
37
38
  k.is(decodedMsg.bko.sig.length, 64, 'Message signature should be 64 bytes long');
@@ -40,7 +41,7 @@ test('assembleWhitelistMessages', async (t) => {
40
41
 
41
42
  t.test('assembleWhitelistMessages - Should return null when wallet is invalid', async (k) => {
42
43
  await k.exception(
43
- async () => await CompleteStateMessageOperations.assembleAppendWhitelistMessages(null),
44
+ async () => await new CompleteStateMessageOperations(null, config).assembleAppendWhitelistMessages(),
44
45
  errorMessageIncludes('Wallet must be a valid wallet object')
45
46
  );
46
47
  });
@@ -48,7 +49,7 @@ test('assembleWhitelistMessages', async (t) => {
48
49
 
49
50
  t.test('assembleWhitelistMessages - Empty object', async (k) => {
50
51
  await k.exception(
51
- async () => await CompleteStateMessageOperations.assembleAppendWhitelistMessages({}),
52
+ async () => await new CompleteStateMessageOperations({}, config).assembleAppendWhitelistMessages(),
52
53
  errorMessageIncludes('Wallet should have a valid TRAC address.')
53
54
  );
54
55
 
@@ -2,6 +2,7 @@ import b4a from 'b4a';
2
2
  import {OperationType} from "../../src/utils/constants.js";
3
3
  import {bufferToAddress, isAddressValid} from "../../src/core/state/utils/address.js";
4
4
  import {errorMessageIncludes} from "../utils/regexHelper.js"
5
+ import { config } from '../../helpers/config.js'
5
6
 
6
7
  export async function messageOperationsEkoTest(t, fnName, assembler, wallet, writingKey, opType, msgValueLength, expectedMessageAddress) {
7
8
  console.log('address:', expectedMessageAddress)
@@ -17,8 +18,8 @@ export async function messageOperationsEkoTest(t, fnName, assembler, wallet, wri
17
18
  k.ok(b4a.equals(msg.eko.wk, writingKey), 'Message wk should be the writing key');
18
19
  }
19
20
 
20
- k.ok(bufferToAddress(msg.address) === expectedMessageAddress, 'Message key should be the the expected one');
21
- k.ok(isAddressValid(msg.address), 'Message address should be a valid address');
21
+ k.ok(bufferToAddress(msg.address, config.addressPrefix) === expectedMessageAddress, 'Message key should be the the expected one');
22
+ k.ok(isAddressValid(msg.address, config.addressPrefix), 'Message address should be a valid address');
22
23
 
23
24
  k.is(msg.eko.nonce.length, 32, 'Message nonce should be 32 bytes long');
24
25
  k.ok(b4a.isBuffer(msg.eko.nonce), 'Message nonce should be a buffer');
@@ -152,7 +153,7 @@ export async function messageOperationsBkoTest(t, fnName, assembler, wallet, wri
152
153
 
153
154
  k.is(msg.type, opType, `Message type should be ${opType}`);
154
155
 
155
- k.ok(bufferToAddress(msg.address) === expectedMessageAddress, 'Message address should be the the expected one');
156
+ k.ok(bufferToAddress(msg.address, config.addressPrefix) === expectedMessageAddress, 'Message address should be the the expected one');
156
157
  k.is(msg.bko.nonce.length, 32, 'Message nonce should be 32 bytes long');
157
158
  k.ok(b4a.isBuffer(msg.bko.nonce), 'Message nonce should be a buffer');
158
159
  k.is(msg.bko.sig.length, 64, 'Message signature should be 64 bytes long');
@@ -5,6 +5,7 @@ import { testKeyPair1, testKeyPair2, testKeyPair3, testKeyPair4, testKeyPair5, t
5
5
  import ConnectionManager from "../../../src/core/network/services/ConnectionManager.js";
6
6
  import { tick } from "../../helpers/setupApplyTests.js";
7
7
  import b4a from 'b4a'
8
+ import { createConfig, ENV } from "../../../src/config/env.js";
8
9
 
9
10
  const createConnection = (key) => {
10
11
  const emitter = new EventEmitter()
@@ -18,7 +19,8 @@ const createConnection = (key) => {
18
19
  }
19
20
 
20
21
  const makeManager = (maxValidators = 6, conns = connections) => {
21
- const connectionManager = new ConnectionManager({ maxValidators })
22
+ const merged = createConfig(ENV.DEVELOPMENT, { maxValidators })
23
+ const connectionManager = new ConnectionManager(merged)
22
24
 
23
25
  conns.forEach(({ key, connection }) => {
24
26
  connectionManager.addValidator(key, connection)
@@ -50,7 +52,6 @@ test('ConnectionManager', () => {
50
52
  reset()
51
53
  const connectionManager = makeManager()
52
54
  t.is(connectionManager.connectionCount(), connections.length, 'should have the same length')
53
-
54
55
  const data = createConnection(testKeyPair5.publicKey)
55
56
  connectionManager.addValidator(data.key, data.connection)
56
57
  t.is(connectionManager.connectionCount(), connections.length + 1, 'should have the same length')
@@ -70,6 +71,31 @@ test('ConnectionManager', () => {
70
71
  connectionManager.addValidator(toNotAdd.key, toNotAdd.connection)
71
72
  t.is(connectionManager.connectionCount(), maxConnections, 'should not increase length')
72
73
  })
74
+
75
+ test('does not add new validator when pool is full', async t => {
76
+ reset()
77
+ const maxConnections = 2
78
+ const localConnections = [
79
+ createConnection(testKeyPair1.publicKey),
80
+ createConnection(testKeyPair2.publicKey),
81
+ ]
82
+
83
+ const connectionManager = makeManager(maxConnections)
84
+ localConnections.forEach(({ key, connection }) => {
85
+ connectionManager.addValidator(key, connection)
86
+ })
87
+
88
+ t.is(connectionManager.connectionCount(), maxConnections, 'pool should be full')
89
+
90
+ const newConn = createConnection(testKeyPair3.publicKey)
91
+ connectionManager.addValidator(newConn.key, newConn.connection)
92
+
93
+ t.is(connectionManager.connectionCount(), maxConnections, 'should stay at max size')
94
+ t.not(connectionManager.connected(newConn.key), 'new validator should not be in the pool')
95
+
96
+ const remainingOld = localConnections.filter(c => connectionManager.connected(c.key)).length
97
+ t.is(remainingOld, 2, 'all of the old validators should remain')
98
+ })
73
99
  })
74
100
 
75
101
  test('connected', async t => {
@@ -93,84 +119,29 @@ test('ConnectionManager', () => {
93
119
  reset()
94
120
  const connectionManager = makeManager()
95
121
 
96
- connectionManager.send([1,2,3,4])
97
- t.ok(connections[0].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
98
- })
99
-
100
- test('rotate the messenger', async t => {
101
- reset()
102
- const connectionManager = makeManager()
103
-
104
- for (let i = 0; i < 10; i++) {
105
- connectionManager.send([1,2,3,4])
106
- t.ok(connections[0].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
107
- }
108
- })
109
-
110
- test('resets rotation', async t => {
111
- reset()
112
- const connectionManager = makeManager()
113
-
114
- for (let i = 0; i < 10; i++) {
115
- connectionManager.send([1,2,3,4])
116
- t.ok(connections[0].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
117
- }
118
-
119
- for (let i = 0; i < 10; i++) {
120
- connectionManager.send([1,2,3,4])
121
- t.ok(connections[1].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
122
- }
123
-
124
- for (let i = 0; i < 10; i++) {
125
- connectionManager.send([1,2,3,4])
126
- t.ok(connections[2].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
127
- }
128
-
129
- for (let i = 0; i < 10; i++) {
130
- connectionManager.send([1,2,3,4])
131
- t.ok(connections[3].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
132
- }
133
-
134
- t.is(connections[0].connection.messenger.send.callCount, 10, 'first should have been called 10 times')
135
- t.is(connections[1].connection.messenger.send.callCount, 10, 'second should have been called 10 times')
136
- t.is(connections[2].connection.messenger.send.callCount, 10, 'third should have been called 10 times')
137
- t.is(connections[3].connection.messenger.send.callCount, 10, 'fourth should have been called 10 times')
122
+ const target = connectionManager.send([1,2,3,4])
138
123
 
139
- connectionManager.send([1,2,3,4])
140
- t.ok(connections[0].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
141
- t.is(connections[0].connection.messenger.send.callCount, 11, 'first should have been called 11 times')
124
+ const totalCalls = connections.reduce((sum, con) => sum + con.connection.messenger.send.callCount, 0)
125
+ t.is(totalCalls, 1, 'should send to exactly one validator')
126
+ t.ok(target, 'should return a target public key')
142
127
  })
143
128
 
144
- test('rotates on exception', async t => {
129
+ test('does not throw on individual send errors', async t => {
145
130
  reset()
146
- const conns = [
131
+ const errorConnections = [
147
132
  createConnection(testKeyPair7.publicKey),
148
133
  createConnection(testKeyPair8.publicKey),
149
- createConnection(testKeyPair9.publicKey),
150
134
  ]
151
135
 
152
- conns[0].connection.messenger.send = sinon.stub().throws(new Error())
153
- conns[1].connection.messenger.send = sinon.stub().throws(new Error())
154
-
155
- const connectionManager = makeManager(5, conns)
156
- connectionManager.send([1,2,3,4])
157
-
158
- t.is(conns[0].connection.messenger.send.callCount, 1, 'first should have been called 10 times')
159
- t.is(conns[1].connection.messenger.send.callCount, 1, 'second should have been called 10 times')
160
- t.is(conns[2].connection.messenger.send.callCount, 1, 'third should have been called 10 times')
161
- })
162
- })
136
+ errorConnections.forEach(con => {
137
+ con.connection.messenger.send = sinon.stub().throws(new Error())
138
+ })
163
139
 
164
- test('rotate', async t => {
165
- test('resets the rotation', async t => {
166
- reset()
167
- const connectionManager = makeManager()
140
+ const connectionManager = makeManager(5, errorConnections)
168
141
 
169
- connectionManager.send([1,2,3,4]) // this shouldnt trigger rotation
170
- t.ok(connections[0].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
171
- connectionManager.rotate() // rotate
142
+ t.is(errorConnections.length, 2, 'should have two connections')
172
143
  connectionManager.send([1,2,3,4])
173
- t.ok(connections[1].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
144
+ t.ok(true, 'send should not throw even if individual sends fail')
174
145
  })
175
146
  })
176
147
 
@@ -216,4 +187,4 @@ test('ConnectionManager', () => {
216
187
  t.is(connectionCount, connectionManager.connectionCount() + 1, 'first on the list should have been called')
217
188
  })
218
189
  })
219
- })
190
+ })