trac-msb 0.2.8 → 0.2.10

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 (151) hide show
  1. package/.github/workflows/acceptance-tests.yml +7 -11
  2. package/.github/workflows/lint-pr-title.yml +26 -0
  3. package/.github/workflows/unit-tests.yml +2 -8
  4. package/CODE_OF_CONDUCT.md +128 -0
  5. package/README.md +33 -18
  6. package/docker-compose.yml +1 -0
  7. package/docs/trac_network_http_api.openapi.yaml +889 -0
  8. package/msb.mjs +5 -22
  9. package/package.json +14 -10
  10. package/proto/network.proto +74 -0
  11. package/rpc/create_server.js +2 -2
  12. package/rpc/handlers.js +165 -92
  13. package/rpc/routes/v1.js +3 -1
  14. package/rpc/rpc_server.js +4 -4
  15. package/rpc/rpc_services.js +62 -25
  16. package/rpc/utils/helpers.js +83 -52
  17. package/src/config/args.js +46 -0
  18. package/src/config/config.js +78 -5
  19. package/src/config/env.js +70 -3
  20. package/src/core/network/Network.js +34 -70
  21. package/src/core/network/identity/NetworkWalletFactory.js +2 -2
  22. package/src/core/network/protocols/LegacyProtocol.js +67 -0
  23. package/src/core/network/protocols/NetworkMessages.js +48 -0
  24. package/src/core/network/protocols/ProtocolInterface.js +31 -0
  25. package/src/core/network/protocols/ProtocolSession.js +59 -0
  26. package/src/core/network/protocols/V1Protocol.js +64 -0
  27. package/src/core/network/protocols/legacy/NetworkMessageRouter.js +84 -0
  28. package/src/core/network/protocols/legacy/handlers/GetRequestHandler.js +53 -0
  29. package/src/core/network/protocols/legacy/handlers/ResponseHandler.js +37 -0
  30. package/src/core/network/{messaging → protocols/legacy}/validators/base/BaseResponse.js +2 -3
  31. package/src/core/network/protocols/shared/handlers/RoleOperationHandler.js +88 -0
  32. package/src/core/network/protocols/shared/handlers/SubnetworkOperationHandler.js +93 -0
  33. package/src/core/network/{messaging → protocols/shared}/handlers/TransferOperationHandler.js +17 -16
  34. package/src/core/network/{messaging → protocols/shared}/handlers/base/BaseOperationHandler.js +10 -15
  35. package/src/core/network/{messaging → protocols/shared}/validators/PartialBootstrapDeployment.js +2 -2
  36. package/src/core/network/{messaging → protocols/shared}/validators/PartialRoleAccess.js +5 -5
  37. package/src/core/network/{messaging → protocols/shared}/validators/PartialTransaction.js +4 -4
  38. package/src/core/network/{messaging → protocols/shared}/validators/PartialTransfer.js +4 -4
  39. package/src/core/network/{messaging → protocols/shared}/validators/base/PartialOperation.js +14 -12
  40. package/src/core/network/protocols/v1/NetworkMessageRouter.js +15 -0
  41. package/src/core/network/services/ConnectionManager.js +5 -5
  42. package/src/core/network/services/MessageOrchestrator.js +2 -2
  43. package/src/core/network/services/TransactionPoolService.js +5 -6
  44. package/src/core/network/services/TransactionRateLimiterService.js +12 -13
  45. package/src/core/network/services/ValidatorObserverService.js +5 -6
  46. package/src/core/state/State.js +3 -5
  47. package/src/index.js +156 -181
  48. package/src/messages/network/v1/NetworkMessageBuilder.js +325 -0
  49. package/src/messages/network/v1/NetworkMessageDirector.js +137 -0
  50. package/src/messages/network/v1/networkMessageFactory.js +12 -0
  51. package/src/messages/state/ApplyStateMessageBuilder.js +661 -0
  52. package/src/messages/state/ApplyStateMessageDirector.js +516 -0
  53. package/src/messages/state/applyStateMessageFactory.js +12 -0
  54. package/src/utils/buffer.js +53 -1
  55. package/src/utils/check.js +1 -1
  56. package/src/utils/cli.js +0 -8
  57. package/src/utils/constants.js +33 -30
  58. package/src/utils/fileUtils.js +13 -0
  59. package/src/utils/normalizers.js +84 -2
  60. package/src/utils/protobuf/network.cjs +840 -0
  61. package/src/utils/protobuf/operationHelpers.js +10 -0
  62. package/src/utils/type.js +26 -0
  63. package/tests/acceptance/v1/balance/balance.test.mjs +1 -2
  64. package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +26 -30
  65. package/tests/acceptance/v1/health/health.test.mjs +33 -0
  66. package/tests/acceptance/v1/rpc.test.mjs +4 -3
  67. package/tests/acceptance/v1/tx/tx.test.mjs +27 -16
  68. package/tests/acceptance/v1/tx-details/tx-details.test.mjs +26 -12
  69. package/tests/fixtures/check.fixtures.js +33 -32
  70. package/tests/fixtures/networkV1.fixtures.js +85 -0
  71. package/tests/fixtures/protobuf.fixtures.js +109 -25
  72. package/tests/helpers/StateNetworkFactory.js +2 -2
  73. package/tests/helpers/address.js +6 -0
  74. package/tests/helpers/autobaseTestHelpers.js +2 -1
  75. package/tests/helpers/config.js +2 -1
  76. package/tests/helpers/setupApplyTests.js +59 -56
  77. package/tests/unit/messages/messages.test.js +12 -0
  78. package/tests/unit/messages/network/NetworkMessageBuilder.test.js +276 -0
  79. package/tests/unit/messages/network/NetworkMessageDirector.test.js +201 -0
  80. package/tests/unit/messages/state/applyStateMessageBuilder.complete.test.js +521 -0
  81. package/tests/unit/messages/state/applyStateMessageBuilder.partial.test.js +233 -0
  82. package/tests/unit/network/ConnectionManager.test.js +6 -5
  83. package/tests/unit/network/networkModule.test.js +3 -2
  84. package/tests/unit/state/apply/addAdmin/addAdminHappyPathScenario.js +10 -6
  85. package/tests/unit/state/apply/addAdmin/addAdminScenarioHelpers.js +9 -6
  86. package/tests/unit/state/apply/addAdmin/state.apply.addAdmin.test.js +10 -7
  87. package/tests/unit/state/apply/addIndexer/addIndexerScenarioHelpers.js +18 -21
  88. package/tests/unit/state/apply/addWriter/addWriterScenarioHelpers.js +53 -38
  89. package/tests/unit/state/apply/adminRecovery/adminRecoveryScenarioHelpers.js +46 -35
  90. package/tests/unit/state/apply/appendWhitelist/appendWhitelistScenarioHelpers.js +13 -16
  91. package/tests/unit/state/apply/balanceInitialization/balanceInitializationScenarioHelpers.js +17 -11
  92. package/tests/unit/state/apply/banValidator/banValidatorScenarioHelpers.js +11 -12
  93. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentScenarioHelpers.js +9 -7
  94. package/tests/unit/state/apply/common/commonScenarioHelper.js +15 -14
  95. package/tests/unit/state/apply/common/payload-structure/initializationDisabledScenario.js +9 -4
  96. package/tests/unit/state/apply/disableInitialization/disableInitializationScenarioHelpers.js +17 -11
  97. package/tests/unit/state/apply/removeWriter/removeWriterScenarioHelpers.js +19 -14
  98. package/tests/unit/state/apply/transfer/transferDoubleSpendAcrossValidatorsScenario.js +37 -29
  99. package/tests/unit/state/apply/transfer/transferScenarioHelpers.js +9 -7
  100. package/tests/unit/state/apply/txOperation/txOperationScenarioHelpers.js +11 -9
  101. package/tests/unit/unit.test.js +1 -1
  102. package/tests/unit/utils/buffer/buffer.test.js +62 -1
  103. package/tests/unit/utils/fileUtils/readAddressesFromWhitelistFile.test.js +4 -3
  104. package/tests/unit/utils/fileUtils/readBalanceMigrationFile.test.js +3 -2
  105. package/tests/unit/utils/migrationUtils/validateAddressFromIncomingFile.test.js +3 -2
  106. package/tests/unit/utils/normalizers/normalizers.test.js +469 -0
  107. package/tests/unit/utils/protobuf/operationHelpers.test.js +120 -2
  108. package/tests/unit/utils/type/type.test.js +25 -0
  109. package/tests/unit/utils/utils.test.js +1 -0
  110. package/docs/networking-dualstack-plan.md +0 -75
  111. package/docs/networking-layer-redesign.md +0 -155
  112. package/src/core/network/messaging/NetworkMessages.js +0 -64
  113. package/src/core/network/messaging/handlers/GetRequestHandler.js +0 -113
  114. package/src/core/network/messaging/handlers/ResponseHandler.js +0 -107
  115. package/src/core/network/messaging/handlers/RoleOperationHandler.js +0 -114
  116. package/src/core/network/messaging/handlers/SubnetworkOperationHandler.js +0 -149
  117. package/src/core/network/messaging/routes/NetworkMessageRouter.js +0 -98
  118. package/src/core/network/messaging/validators/AdminResponse.js +0 -58
  119. package/src/core/network/messaging/validators/CustomNodeResponse.js +0 -46
  120. package/src/messages/base/StateBuilder.js +0 -25
  121. package/src/messages/completeStateMessages/CompleteStateMessageBuilder.js +0 -425
  122. package/src/messages/completeStateMessages/CompleteStateMessageDirector.js +0 -252
  123. package/src/messages/completeStateMessages/CompleteStateMessageOperations.js +0 -296
  124. package/src/messages/partialStateMessages/PartialStateMessageBuilder.js +0 -272
  125. package/src/messages/partialStateMessages/PartialStateMessageDirector.js +0 -137
  126. package/src/messages/partialStateMessages/PartialStateMessageOperations.js +0 -138
  127. package/tests/integration/apply/addAdmin/addAdminBasic.test.js +0 -69
  128. package/tests/integration/apply/addAdmin/addAdminRecovery.test.js +0 -126
  129. package/tests/integration/apply/addIndexer.test.js +0 -239
  130. package/tests/integration/apply/addWhitelist.test.js +0 -53
  131. package/tests/integration/apply/addWriter.test.js +0 -245
  132. package/tests/integration/apply/apply.test.js +0 -19
  133. package/tests/integration/apply/banValidator.test.js +0 -116
  134. package/tests/integration/apply/postTx/invalidSubValues.test.js +0 -103
  135. package/tests/integration/apply/postTx/postTx.test.js +0 -196
  136. package/tests/integration/apply/removeIndexer.test.js +0 -132
  137. package/tests/integration/apply/removeWriter.test.js +0 -168
  138. package/tests/integration/apply/transfer.test.js +0 -83
  139. package/tests/integration/integration.test.js +0 -9
  140. package/tests/unit/messageOperations/assembleAddIndexerMessage.test.js +0 -21
  141. package/tests/unit/messageOperations/assembleAddWriterMessage.test.js +0 -17
  142. package/tests/unit/messageOperations/assembleAdminMessage.test.js +0 -68
  143. package/tests/unit/messageOperations/assembleBanWriterMessage.test.js +0 -17
  144. package/tests/unit/messageOperations/assemblePostTransaction.test.js +0 -424
  145. package/tests/unit/messageOperations/assembleRemoveIndexerMessage.test.js +0 -19
  146. package/tests/unit/messageOperations/assembleRemoveWriterMessage.test.js +0 -17
  147. package/tests/unit/messageOperations/assembleWhitelistMessages.test.js +0 -59
  148. package/tests/unit/messageOperations/commonsStateMessageOperationsTest.js +0 -278
  149. package/tests/unit/messageOperations/stateMessageOperations.test.js +0 -19
  150. /package/src/core/network/{messaging → protocols/legacy}/validators/ValidatorResponse.js +0 -0
  151. /package/src/utils/{operations.js → applyOperations.js} +0 -0
@@ -1,245 +0,0 @@
1
- import b4a from 'b4a';
2
- import { test, hook } from '../../helpers/wrapper.js';
3
-
4
- import {
5
- setupMsbAdmin,
6
- setupMsbPeer,
7
- setupWhitelist,
8
- fundPeer,
9
- initTemporaryDirectory,
10
- removeTemporaryDirectory,
11
- randomBytes,
12
- setupMsbIndexer,
13
- setupMsbWriter,
14
- tryToSyncWriters,
15
- waitForNodeState,
16
- waitWritable
17
- } from '../../helpers/setupApplyTests.js';
18
- import {
19
- testKeyPair1,
20
- testKeyPair2,
21
- testKeyPair3,
22
- testKeyPair4,
23
- testKeyPair5,
24
- testKeyPair6
25
- } from '../../fixtures/apply.fixtures.js';
26
- import PartialStateMessageOperations from "../../../src/messages/partialStateMessages/PartialStateMessageOperations.js";
27
- import CompleteStateMessageOperations from '../../../src/messages/completeStateMessages/CompleteStateMessageOperations.js';
28
- import {ZERO_WK} from '../../../src/utils/buffer.js';
29
- import { $TNK } from '../../../src/core/state/utils/balance.js';
30
- import { config } from '../../helpers/config.js';
31
-
32
- const sendAddWriter = async (invoker, broadcaster) => {
33
- const validity = await invoker.msb.state.getIndexerSequenceState()
34
- const req = await new PartialStateMessageOperations(invoker.wallet, config)
35
- .assembleAddWriterMessage(
36
- b4a.toString(invoker.msb.state.writingKey, 'hex'),
37
- b4a.toString(validity, 'hex'));
38
-
39
- const raw = await new CompleteStateMessageOperations(broadcaster.wallet, config)
40
- .assembleAddWriterMessage(
41
- req.address,
42
- b4a.from(req.rao.tx, 'hex'),
43
- b4a.from(req.rao.txv, 'hex'),
44
- b4a.from(req.rao.iw, 'hex'),
45
- b4a.from(req.rao.in, 'hex'),
46
- b4a.from(req.rao.is, 'hex')
47
- )
48
- return await broadcaster.msb.state.append(raw)
49
- }
50
-
51
- let admin, writer1, writer2, writer3, writer4, indexer1, tmpDirectory;
52
-
53
- hook('Initialize nodes for addWriter tests', async t => {
54
- const randomChannel = randomBytes(32).toString('hex');
55
- const baseOptions = {
56
- enableTxApplyLogs: false,
57
- enableInteractiveMode: false,
58
- enableRoleRequester: false,
59
- channel: randomChannel,
60
- enableValidatorObserver: false
61
- }
62
- tmpDirectory = await initTemporaryDirectory();
63
- admin = await setupMsbAdmin(testKeyPair1, tmpDirectory, baseOptions);
64
- writer1 = await setupMsbPeer('writer1', testKeyPair2, tmpDirectory, admin.options); // just a peer, not a writer yet
65
- await fundPeer(admin, writer1, $TNK(10n))
66
- writer2 = await setupMsbPeer('writer2', testKeyPair3, tmpDirectory, admin.options); // just a peer, not a writer yet
67
- await fundPeer(admin, writer2, $TNK(10n))
68
- writer3 = await setupMsbPeer('writer3', testKeyPair4, tmpDirectory, admin.options); // just a peer, not a writer yet
69
- await fundPeer(admin, writer3, $TNK(10n))
70
- writer4 = await setupMsbPeer('writer4', testKeyPair5, tmpDirectory, admin.options); // just a peer, not a writer yet
71
- indexer1 = await setupMsbWriter(admin, 'indexer1', testKeyPair6, tmpDirectory, admin.options); // writer and cannot be funded
72
-
73
- const whitelistKeys = [writer1.wallet.address, writer2.wallet.address, writer3.wallet.address, indexer1.wallet.address];
74
- await setupWhitelist(admin, whitelistKeys);
75
- indexer1 = await setupMsbIndexer(indexer1, admin);
76
- });
77
-
78
- test('handleApplyAddWriterOperation (apply) - Append addWriter payload into the base - happy path for peer', async (t) => {
79
- // writer1 is not a writer yet, but it is whitelisted -> It will become a writer after the operation.
80
- // writer2 is not a writer yet, but it is whitelisted.
81
- // writer3 is not a writer yet, but it is whitelisted.
82
- // writer4 is reader
83
- // indexer1 is already an indexer.
84
- await admin.msb.state.append(null);
85
- await waitWritable(admin, writer1, async () => await sendAddWriter(writer1, admin))
86
- await waitForNodeState(admin, writer1.wallet.address, {
87
- wk: writer1.msb.state.writingKey,
88
- isWhitelisted: true,
89
- isWriter: true,
90
- isIndexer: false
91
- });
92
- await tryToSyncWriters(writer1, indexer1);
93
-
94
- const result = await admin.msb.state.getNodeEntry(writer1.wallet.address); // check if the writer entry was added successfully in the base
95
-
96
- t.ok(result, 'Result should not be null');
97
- t.ok(b4a.equals(result.wk, writer1.msb.state.writingKey), 'Result writing key should match writer writing key');
98
- t.ok(result.isWriter, 'Result should indicate that the peer is a valid writer');
99
- t.is(result.isIndexer, false, 'Result should not indicate that the peer is an indexer');
100
- });
101
-
102
- test('handleApplyAddWriterOperation (apply) - Append addWriter payload into the base - doesnt change the signed length', async (t) => {
103
- // writer1 is already a writer.
104
- // writer2 is not a writer yet, but it is whitelisted -> It will become a writer after the operation.
105
- // writer3 is not a writer yet, but it is whitelisted.
106
- // writer4 is reader
107
- // indexer1 is already an indexer.
108
- await admin.msb.state.append(null);
109
- await waitWritable(admin, writer2, async () => await sendAddWriter(writer2, admin))
110
- await tryToSyncWriters(writer2, admin, indexer1);
111
-
112
- await waitForNodeState(writer2, writer2.wallet.address, {
113
- wk: writer2.msb.state.writingKey,
114
- isWhitelisted: true,
115
- isWriter: true,
116
- isIndexer: false
117
- });
118
-
119
- const signedLengthAdminBefore = admin.msb.state.getSignedLength();
120
- const signedLengthWriter2Before = writer2.msb.state.getSignedLength();
121
-
122
- await sendAddWriter(writer2, admin)
123
-
124
- await tryToSyncWriters(admin, writer2, indexer1);
125
- await waitForNodeState(writer2, writer2.wallet.address, {
126
- wk: writer2.msb.state.writingKey,
127
- isWhitelisted: true,
128
- isWriter: true,
129
- isIndexer: false
130
- });
131
-
132
- const signedLengthAdminAfter = admin.msb.state.getSignedLength();
133
- const signedLengthWriter2After = writer2.msb.state.getSignedLength();
134
-
135
- t.is(signedLengthAdminBefore, signedLengthAdminAfter, 'Admin signed length should not change');
136
- t.is(signedLengthWriter2Before, signedLengthWriter2After, 'Writer2 signed length should not change');
137
- });
138
-
139
- test('handleApplyAddWriterOperation (apply) - Append addWriter payload into the base - ZERO_WK', async (t) => {
140
- // writer1 is already a writer.
141
- // writer2 is already a writer.
142
- // writer3 is not a writer yet, but it is whitelisted and won't become a writer.
143
- // writer4 is reader
144
- // indexer1 is already an indexer.
145
- const signedLengthAdminBefore = admin.msb.state.getSignedLength();
146
- const signedLengthWriter1Before = writer1.msb.state.getSignedLength();
147
-
148
- const validity = await writer3.msb.state.getIndexerSequenceState()
149
- const req = await new PartialStateMessageOperations(writer3.wallet, config)
150
- .assembleAddWriterMessage(
151
- b4a.toString(ZERO_WK, 'hex'),
152
- b4a.toString(validity, 'hex'));
153
-
154
- const raw = await new CompleteStateMessageOperations(admin.wallet, config)
155
- .assembleAddWriterMessage(
156
- admin.wallet.address,
157
- b4a.from(req.rao.tx, 'hex'),
158
- b4a.from(req.rao.txv, 'hex'),
159
- b4a.from(req.rao.iw, 'hex'),
160
- b4a.from(req.rao.in, 'hex'),
161
- b4a.from(req.rao.is, 'hex')
162
- )
163
- await admin.msb.state.append(raw)
164
- await tryToSyncWriters(writer3, admin, writer1, writer2, indexer1);
165
-
166
- const result = await writer1.msb.state.getNodeEntry(writer3.wallet.address);
167
- const signedLengthAdminAfter = admin.msb.state.getSignedLength();
168
- const signedLengthWriter1After = writer1.msb.state.getSignedLength();
169
-
170
- t.is(signedLengthAdminBefore, signedLengthAdminAfter, 'admin signed length should not change');
171
- t.is(signedLengthWriter1Before, signedLengthWriter1After, 'Writer1 signed length should not change');
172
- t.ok(result, 'Result should not be null');
173
- t.ok(!b4a.equals(result.wk, writer3.msb.state.writingKey), 'Result writing key should not match writer writing key');
174
- t.ok(b4a.equals(result.wk, ZERO_WK), 'Result writing key should be ZERO_WK');
175
- t.is(result.isWriter, false, 'Result should indicate that the peer is not a writer');
176
- //t.is(writer3.msb.state.isWritable(), false, 'peer should not be writable');
177
- });
178
-
179
- test('handleApplyAddWriterOperation (apply) - Append addWriter payload into the base - node is already an indexer', async (t) => {
180
- // writer1 is already a writer.
181
- // writer2 is already a writer.
182
- // writer3 is not a writer, but it is whitelisted.
183
- // writer4 is reader
184
- // indexer1 is already an indexer.
185
- const resultBefore = await admin.msb.state.getNodeEntry(indexer1.wallet.address);
186
- await indexer1.msb.state.append(null);
187
- const signedLengthAdminBefore = admin.msb.state.getSignedLength();
188
- const signedLengthIndexer1Before = indexer1.msb.state.getSignedLength();
189
-
190
- await sendAddWriter(indexer1, admin)
191
- await tryToSyncWriters(indexer1, admin, writer1, writer2);
192
-
193
- const result = await admin.msb.state.getNodeEntry(indexer1.wallet.address); // check if the writer entry was added successfully in the base
194
- const signedLengthAdminAfter = admin.msb.state.getSignedLength();
195
- const signedLengthIndexer1After = indexer1.msb.state.getSignedLength();
196
-
197
- t.is(signedLengthAdminBefore, signedLengthAdminAfter, 'Admin signed length should not change');
198
- t.is(signedLengthIndexer1Before, signedLengthIndexer1After, 'Indexer1 signed length should not change');
199
- t.ok(result, 'Result should not be null');
200
- t.ok(b4a.equals(result.wk, indexer1.msb.state.writingKey), 'Result writing key should match writer writing key');
201
- t.is(result.isWriter, true, 'Result should indicate that the peer is a writer');
202
- t.is(result.isIndexer, true, 'Result should indicate that the peer is an indexer');
203
- t.is(indexer1.msb.state.isWritable(), true, 'peer should still be writable');
204
- t.is(indexer1.msb.state.isIndexer(), true, 'peer should still be an indexer');
205
- });
206
-
207
- test('handleApplyAddWriterOperation (apply) - Append addWriter payload into the base - non-whitelisted peer', async (t) => {
208
- // writer1 is already a writer
209
- // writer2 is already a writer
210
- // writer3 is already a writer
211
- // writer4 is reader -> this node will not become a writer.
212
- // indexer1 is already an indexer
213
- await writer4.msb.state.append(null);
214
- const signedLengthAdminBefore = admin.msb.state.getSignedLength();
215
-
216
- await sendAddWriter(writer4, admin)
217
- await tryToSyncWriters(writer4, writer1, writer2, writer3, indexer1);
218
-
219
- const result = await writer2.msb.state.getNodeEntry(writer4.wallet.address); // check if the writer entry was added successfully in the base
220
- const signedLengthAdminAfter = admin.msb.state.getSignedLength();
221
-
222
- t.is(signedLengthAdminBefore, signedLengthAdminAfter, 'Admin signed length should not change');
223
- t.is(writer4.msb.state.isWritable(), false, 'peer should not be writable');
224
- t.is(writer4.msb.state.isIndexer(), false, 'peer should not be an indexer');
225
- t.is(result, null, 'Result should be null');
226
- });
227
-
228
- test('handleApplyAddWriterOperation (apply) - validator and invoker are the same', async (t) => {
229
- // TODO: Implement when apply tests are fixed
230
- t.pass('Skipping test: Placeholder. To be implemented later.');
231
- });
232
-
233
- hook('Clean up addWriter setup', async t => {
234
- const toClose = []
235
- // close msb instances and remove temp directory
236
- if (admin?.msb) toClose.push(admin.msb.close());
237
- if (writer1?.msb) toClose.push(writer1.msb.close());
238
- if (writer2?.msb) toClose.push(writer2.msb.close());
239
- if (writer3?.msb) toClose.push(writer3.msb.close());
240
- if (writer4?.msb) toClose.push(writer4.msb.close());
241
- if (indexer1?.msb) toClose.push(indexer1.msb.close());
242
-
243
- await Promise.all(toClose)
244
- if (tmpDirectory) await removeTemporaryDirectory(tmpDirectory);
245
- })
@@ -1,19 +0,0 @@
1
- import {default as test} from 'brittle';
2
-
3
- async function runCheckTests() {
4
- test.pause();
5
- await import('./addAdmin/addAdminBasic.test.js');
6
- await import('./addAdmin/addAdminRecovery.test.js');
7
- await import('./addWhitelist.test.js');
8
- await import('./addWriter.test.js');
9
- await import('./removeWriter.test.js');
10
- await import('./addIndexer.test.js');
11
- await import('./removeIndexer.test.js');
12
- await import('./postTx/postTx.test.js');
13
- await import('./transfer.test.js');
14
- await import('./postTx/invalidSubValues.test.js');
15
- await import('./banValidator.test.js')
16
- test.resume();
17
- }
18
-
19
- await runCheckTests();
@@ -1,116 +0,0 @@
1
- import { test, hook } from '../../helpers/wrapper.js';
2
-
3
- import {
4
- initTemporaryDirectory,
5
- removeTemporaryDirectory,
6
- setupMsbWriter,
7
- setupMsbIndexer,
8
- setupMsbAdmin,
9
- tryToSyncWriters
10
- } from '../../helpers/setupApplyTests.js';
11
- import { randomBytes } from '../../helpers/setupApplyTests.js';
12
- import CompleteStateMessageOperations from '../../../src/messages/completeStateMessages/CompleteStateMessageOperations.js';
13
-
14
- import { testKeyPair1, testKeyPair2, testKeyPair3, testKeyPair4 } from '../../fixtures/apply.fixtures.js';
15
- import { sleep } from '../../../src/utils/helpers.js';
16
- import b4a from 'b4a'
17
- import { config } from '../../helpers/config.js';
18
-
19
- let admin;
20
- let indexer, writer1, writer2;
21
- let tmpDirectory;
22
-
23
- hook('Initialize nodes for banValidator tests', async () => {
24
- const randomChannel = randomBytes(32).toString('hex');
25
- const baseOptions = {
26
- enableTxApplyLogs: false,
27
- enableInteractiveMode: false,
28
- enableRoleRequester: false,
29
- channel: randomChannel,
30
- enableValidatorObserver: false,
31
- }
32
- tmpDirectory = await initTemporaryDirectory();
33
- admin = await setupMsbAdmin(testKeyPair1, tmpDirectory, baseOptions);
34
-
35
- indexer = await setupMsbWriter(admin, 'indexer', testKeyPair2, tmpDirectory, admin.options);
36
- indexer = await setupMsbIndexer(indexer, admin);
37
-
38
- writer1 = await setupMsbWriter(admin, 'writer1', testKeyPair3, tmpDirectory, admin.options);
39
- writer2 = await setupMsbWriter(admin, 'writer2', testKeyPair4, tmpDirectory, admin.options);
40
- });
41
-
42
- test('handleApplyBanValidatorOperation (apply) - Append banValidator payload - ban indexer', async t => {
43
- const validity = await admin.msb.state.getIndexerSequenceState()
44
- const assembledBanWriter = await new CompleteStateMessageOperations(admin.wallet, config)
45
- .assembleBanWriterMessage(indexer.wallet.address, validity);
46
- await admin.msb.state.append(assembledBanWriter);
47
- await tryToSyncWriters(admin, indexer, writer1, writer2);
48
-
49
- const indexersEntry = await indexer.msb.state.getIndexersEntry();
50
- const nodeInfo = await indexer.msb.state.getNodeEntry(indexer.wallet.address);
51
-
52
- t.is(indexersEntry.length, 2, 'Indexers entry count should be 2');
53
- t.is(!!indexersEntry.find(({ key }) => b4a.equals(key, indexer.msb.state.writingKey)), true, 'Indexer address should be still included in the indexers entry');
54
- t.is(nodeInfo.isIndexer, true, 'Node info should indicate that the node is still an indexer');
55
- });
56
-
57
- test('handleApplyBanValidatorOperation (apply) - Append banValidator payload into the base by non-admin node', async t => {
58
- const validity = await admin.msb.state.getIndexerSequenceState()
59
- const assembledBanWriter = await new CompleteStateMessageOperations(writer1.wallet, config)
60
- .assembleBanWriterMessage(writer2.wallet.address, validity);
61
- await writer1.msb.state.append(assembledBanWriter);
62
- await sleep(5000); // wait for both peers to sync state
63
- await tryToSyncWriters(admin);
64
-
65
- const nodeInfo = await writer2.msb.state.getNodeEntry(writer2.wallet.address);
66
-
67
- t.is(nodeInfo.isWriter, true, 'Node info should indicate that the node is still a writer');
68
- t.is(nodeInfo.isWhitelisted, true, 'Node info should indicate that the node is still whitelisted');
69
- });
70
-
71
-
72
- test('handleApplyBanValidatorOperation (apply) - Append banValidator payload into the base - happy path', async t => {
73
- const validity = await admin.msb.state.getIndexerSequenceState()
74
- const assembledBanWriter = await new CompleteStateMessageOperations(admin.wallet, config)
75
- .assembleBanWriterMessage(writer1.wallet.address, validity);
76
- await admin.msb.state.append(assembledBanWriter);
77
- await sleep(5000); // wait for both peers to sync state
78
-
79
- const nodeInfo = await writer1.msb.state.getNodeEntry(writer1.wallet.address);
80
-
81
- t.is(nodeInfo.isWriter, false, 'Node info should indicate that the node is not a writer anymore');
82
- t.is(nodeInfo.isWhitelisted, false, 'Node info should indicate that the node is not whitelisted anymore');
83
- t.is(writer1.msb.state.isWritable(), false, 'Writer1 should not be a writer anymore');
84
- });
85
-
86
- test('handleApplyBanValidatorOperation (apply) - Append banValidator payload into the base - idempotence', async t => {
87
- const validity = await admin.msb.state.getIndexerSequenceState()
88
- const assembledBanWriter = await new CompleteStateMessageOperations(admin.wallet, config)
89
- .assembleBanWriterMessage(writer2.wallet.address, validity);
90
- await admin.msb.state.append(assembledBanWriter);
91
- await sleep(5000); // wait for both peers to sync state
92
-
93
- const nodeInfo = await writer2.msb.state.getNodeEntry(writer2.wallet.address);
94
-
95
- const validity2 = await admin.msb.state.getIndexerSequenceState()
96
- const assembledBanWriter2 = await new CompleteStateMessageOperations(admin.wallet, config)
97
- .assembleBanWriterMessage(writer2.wallet.address, validity2);
98
- await admin.msb.state.append(assembledBanWriter2);
99
- await sleep(5000); // wait for both peers to sync state
100
-
101
- t.is(nodeInfo.isIndexer, false, 'Node info should indicate that the node is not a writer anymore');
102
- t.is(nodeInfo.isWriter, false, 'Node info should indicate that the node is not a writer anymore');
103
- t.is(nodeInfo.isWhitelisted, false, 'Node info should indicate that the node is not whitelisted anymore');
104
- t.is(writer1.msb.state.isWritable(), false, 'Writer2 should not be a writer anymore');
105
- t.is(writer1.msb.state.isIndexer(), false, 'Writer2 should not be a writer anymore');
106
- });
107
-
108
- hook('Clean up banValidator setup', async t => {
109
- const toClose = [];
110
- if (admin?.msb) toClose.push(admin.msb.close());
111
- if (indexer?.msb) toClose.push(indexer.msb.close());
112
- if (writer1?.msb) toClose.push(writer1.msb.close());
113
- if (writer2?.msb) toClose.push(writer2.msb.close());
114
- await Promise.all(toClose);
115
- if (tmpDirectory) await removeTemporaryDirectory(tmpDirectory);
116
- });
@@ -1,103 +0,0 @@
1
- import {hook, test} from '../../../helpers/wrapper.js';
2
- import {
3
- generatePostTx,
4
- initTemporaryDirectory,
5
- randomBytes,
6
- removeTemporaryDirectory,
7
- setupMsbAdmin,
8
- setupMsbPeer,
9
- setupMsbWriter,
10
- fundPeer,
11
- deployExternalBootstrap,
12
- tryToSyncWriters,
13
- waitDemotion,
14
- promoteToWriter
15
- } from '../../../helpers/setupApplyTests.js';
16
- import {safeDecodeApplyOperation, safeEncodeApplyOperation} from '../../../../src/utils/protobuf/operationHelpers.js'
17
- import {testKeyPair1, testKeyPair2, testKeyPair4, testKeyPair5} from '../../../fixtures/apply.fixtures.js';
18
- import { $TNK } from '../../../../src/core/state/utils/balance.js';
19
- import { decode as decodeNodeEntry } from '../../../../src/core/state/utils/nodeEntry.js';
20
-
21
- let tmpDirectory, admin, writer, externalNode, externalBootstrap, maliciousPeer;
22
- // is and vs is already covered
23
- const testCases = [
24
- { name: 'modified tx', mod: (txPayload, maliciousValue) => { return { ...txPayload.txo, tx: maliciousValue }; } },
25
- { name: 'modified incoming writingKey', mod: (txPayload, maliciousValue) => { return { ...txPayload.txo, iw: maliciousValue }; } },
26
- { name: 'modified incoming nonce', mod: (txPayload, maliciousValue) => { return { ...txPayload.txo, in: maliciousValue }; } },
27
- { name: 'modified content hash', mod: (txPayload, maliciousValue) => { return { ...txPayload.txo, ch: maliciousValue }; } },
28
- { name: 'modified external bootstrap', mod: (txPayload, maliciousValue) => { return { ...txPayload.txo, bs: maliciousValue }; } },
29
- { name: 'modified MSB bootstrap', mod: (txPayload, maliciousValue) => { return { ...txPayload.txo, mbs: maliciousValue }; } },
30
- { name: 'modified validator nonce', mod: (txPayload, maliciousValue) => { return { ...txPayload.txo, vn: maliciousValue }; } },
31
- ];
32
-
33
- const close = async node => {
34
- await node.msb.close()
35
- }
36
-
37
- hook('Initialize nodes', async t => {
38
- const randomChannel = randomBytes(32).toString('hex');
39
-
40
- const baseOptions = {
41
- enableTxApplyLogs: false,
42
- enableInteractiveMode: false,
43
- enableRoleRequester: false,
44
- enableValidatorObserver: false,
45
- channel: randomChannel,
46
- }
47
-
48
- tmpDirectory = await initTemporaryDirectory();
49
- admin = await setupMsbAdmin(testKeyPair1, tmpDirectory, baseOptions);
50
- writer = await setupMsbWriter(admin, 'writer1', testKeyPair2, tmpDirectory, admin.options);
51
- externalNode = await setupMsbPeer('reader1', testKeyPair4, tmpDirectory, admin.options);
52
- await fundPeer(admin, externalNode, $TNK(1n)) // we fund it since it will deploy stuff
53
- externalBootstrap = await deployExternalBootstrap(writer, externalNode)
54
- await tryToSyncWriters(writer, admin)
55
- maliciousPeer = await setupMsbPeer('maliciousWriter', testKeyPair5, tmpDirectory, admin.options);
56
- await fundPeer(admin, maliciousPeer, $TNK(10n))
57
- });
58
-
59
- test('handleApplyTxOperation (apply) - invalid txo sub-values', async t => {
60
- const {postTx, txHash} = await generatePostTx(writer, externalNode, externalBootstrap)
61
- const decodedPostTx = safeDecodeApplyOperation(postTx);
62
-
63
- for (let i = 0; i < testCases.length; i++) {
64
- const testCase = testCases[i]
65
-
66
- const maliciousWriter = await promoteToWriter(admin, maliciousPeer)
67
- // all of these values are buffers 32 bytes long
68
- const maliciousValue = randomBytes(32);
69
-
70
- const modifiedTxo = testCase.mod(decodedPostTx, maliciousValue);
71
- const modifiedPostTx = {
72
- ...decodedPostTx,
73
- txo: modifiedTxo
74
- };
75
- const encodedMaliciousPostTx = safeEncodeApplyOperation(modifiedPostTx);
76
-
77
- await waitDemotion(maliciousWriter, async () => {
78
- await maliciousWriter.msb.state.append(encodedMaliciousPostTx);
79
- })
80
-
81
- if (testCase.name === 'modified tx') {
82
- const result = await admin.msb.state.get(maliciousValue.toString('hex'));
83
- t.absent(result, `post tx with ${testCase.name} should not be added to the base`);
84
- } else {
85
- const result = await admin.msb.state.get(txHash);
86
- t.absent(result, `post tx with ${testCase.name} should not be added to the base`);
87
- }
88
-
89
- // should be penalized
90
- const node = decodeNodeEntry(await maliciousWriter.msb.state.get(maliciousWriter.wallet.address))
91
- t.ok(node, 'Result should not be null');
92
- t.is(node.isWriter, false, 'Result should indicate that the peer is not a writer');
93
- }
94
- });
95
-
96
- hook('Clean up postTx setup', async t => {
97
- // close msbBoostrap and remove temp directory
98
- if (writer?.msb) await close(writer)
99
- if (externalNode?.msb) await close(externalNode)
100
- if (admin?.msb) await close(admin)
101
- if (maliciousPeer?.msb) await close(maliciousPeer)
102
- if (tmpDirectory) await removeTemporaryDirectory(tmpDirectory);
103
- });