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
@@ -0,0 +1,201 @@
1
+ import { test } from 'brittle';
2
+ import b4a from 'b4a';
3
+ import PeerWallet from 'trac-wallet';
4
+ import NetworkWalletFactory from '../../../../src/core/network/identity/NetworkWalletFactory.js';
5
+ import NetworkMessageDirector from '../../../../src/messages/network/v1/NetworkMessageDirector.js';
6
+ import NetworkMessageBuilder from '../../../../src/messages/network/v1/NetworkMessageBuilder.js';
7
+ import { NetworkOperationType, ResultCode as NetworkResultCode } from '../../../../src/utils/constants.js';
8
+ import { decodeV1networkOperation, encodeV1networkOperation } from '../../../../src/utils/protobuf/operationHelpers.js';
9
+ import {
10
+ createMessage,
11
+ encodeCapabilities,
12
+ safeWriteUInt32BE,
13
+ idToBuffer,
14
+ timestampToBuffer
15
+ } from '../../../../src/utils/buffer.js';
16
+ import { addressToBuffer } from '../../../../src/core/state/utils/address.js';
17
+ import { config } from '../../../helpers/config.js';
18
+ import { asAddress } from '../../../helpers/address.js';
19
+ import { testKeyPair1 } from '../../../fixtures/apply.fixtures.js';
20
+
21
+ function createWallet() {
22
+ const keyPair = {
23
+ publicKey: b4a.from(testKeyPair1.publicKey, 'hex'),
24
+ secretKey: b4a.from(testKeyPair1.secretKey, 'hex')
25
+ };
26
+ return NetworkWalletFactory.provide({
27
+ enableWallet: false,
28
+ keyPair,
29
+ networkPrefix: config.addressPrefix
30
+ });
31
+ }
32
+
33
+ function uniqueResultCodes() {
34
+ return [...new Set(Object.values(NetworkResultCode))].sort((a, b) => a - b);
35
+ }
36
+
37
+ test('NetworkMessageDirector builds validator connection request and verifies signature', async t => {
38
+ const wallet = createWallet();
39
+ const director = new NetworkMessageDirector(new NetworkMessageBuilder(wallet, config));
40
+
41
+ const id = '1';
42
+ const caps = ['cap:b', 'cap:a'];
43
+
44
+ const payload = await director.buildValidatorConnectionRequest(id, wallet.address, caps);
45
+ t.is(payload.type, NetworkOperationType.VALIDATOR_CONNECTION_REQUEST);
46
+ t.is(payload.id, id);
47
+ t.alike(payload.capabilities, caps);
48
+
49
+ const msg = createMessage(
50
+ payload.type,
51
+ idToBuffer(payload.id),
52
+ timestampToBuffer(payload.timestamp),
53
+ addressToBuffer(wallet.address, config.addressPrefix),
54
+ payload.validator_connection_request.nonce,
55
+ encodeCapabilities(caps)
56
+ );
57
+ const hash = await PeerWallet.blake3(msg);
58
+ t.ok(wallet.verify(payload.validator_connection_request.signature, hash, wallet.publicKey));
59
+ });
60
+
61
+ test('NetworkMessageDirector builds liveness request and verifies signature', async t => {
62
+ const wallet = createWallet();
63
+ const director = new NetworkMessageDirector(new NetworkMessageBuilder(wallet, config));
64
+
65
+ const id = '1';
66
+ const caps = ['cap:b', 'cap:a'];
67
+ const data = b4a.from('ping', 'utf8');
68
+
69
+ const payload = await director.buildLivenessRequest(id, data, caps);
70
+ t.is(payload.type, NetworkOperationType.LIVENESS_REQUEST);
71
+ t.is(payload.id, id);
72
+ t.alike(payload.capabilities, caps);
73
+
74
+ const msg = createMessage(
75
+ payload.type,
76
+ idToBuffer(payload.id),
77
+ timestampToBuffer(payload.timestamp),
78
+ payload.liveness_request.nonce,
79
+ encodeCapabilities(caps)
80
+ );
81
+ const hash = await PeerWallet.blake3(msg);
82
+ t.ok(wallet.verify(payload.liveness_request.signature, hash, wallet.publicKey));
83
+ });
84
+
85
+ test('NetworkMessageDirector iterates liveness response ResultCode values', async t => {
86
+ const wallet = createWallet();
87
+ const director = new NetworkMessageDirector(new NetworkMessageBuilder(wallet, config));
88
+
89
+ const id = '1';
90
+ const caps = ['cap:b', 'cap:a'];
91
+ const data = b4a.from('ping', 'utf8');
92
+
93
+ for (const code of uniqueResultCodes()) {
94
+ const payload = await director.buildLivenessResponse(id, data, caps, code);
95
+ t.is(payload.type, NetworkOperationType.LIVENESS_RESPONSE);
96
+ t.is(payload.liveness_response.result, code);
97
+
98
+ const msg = createMessage(
99
+ payload.type,
100
+ idToBuffer(payload.id),
101
+ timestampToBuffer(payload.timestamp),
102
+ payload.liveness_response.nonce,
103
+ safeWriteUInt32BE(code, 0),
104
+ encodeCapabilities(caps)
105
+ );
106
+ const hash = await PeerWallet.blake3(msg);
107
+ t.ok(wallet.verify(payload.liveness_response.signature, hash, wallet.publicKey));
108
+
109
+ const decoded = decodeV1networkOperation(encodeV1networkOperation(payload));
110
+ t.is(decoded.liveness_response.result, code);
111
+ }
112
+ });
113
+
114
+ test('NetworkMessageDirector builds broadcast transaction request and verifies signature', async t => {
115
+ const wallet = createWallet();
116
+ const director = new NetworkMessageDirector(new NetworkMessageBuilder(wallet, config));
117
+
118
+ const id = '1';
119
+ const data = b4a.from('deadbeef', 'hex');
120
+ const caps = ['cap:b', 'cap:a'];
121
+
122
+ const payload = await director.buildBroadcastTransactionRequest(id, data, caps);
123
+ t.is(payload.type, NetworkOperationType.BROADCAST_TRANSACTION_REQUEST);
124
+ t.is(payload.id, id);
125
+ t.alike(payload.capabilities, caps);
126
+ t.alike(payload.broadcast_transaction_request.data, data);
127
+
128
+ const message = createMessage(
129
+ payload.type,
130
+ idToBuffer(id),
131
+ timestampToBuffer(payload.timestamp),
132
+ data,
133
+ payload.broadcast_transaction_request.nonce,
134
+ encodeCapabilities(caps)
135
+ );
136
+ const hash = await PeerWallet.blake3(message);
137
+ t.ok(wallet.verify(payload.broadcast_transaction_request.signature, hash, wallet.publicKey));
138
+
139
+ const decoded = decodeV1networkOperation(encodeV1networkOperation(payload));
140
+ t.is(decoded.type, NetworkOperationType.BROADCAST_TRANSACTION_REQUEST);
141
+ });
142
+
143
+ test('NetworkMessageDirector iterates broadcast transaction response ResultCode values', async t => {
144
+ const wallet = createWallet();
145
+ const director = new NetworkMessageDirector(new NetworkMessageBuilder(wallet, config));
146
+
147
+ const id = '1';
148
+ const caps = ['cap:b', 'cap:a'];
149
+
150
+ for (const code of uniqueResultCodes()) {
151
+ const payload = await director.buildBroadcastTransactionResponse(id, caps, code);
152
+ t.is(payload.type, NetworkOperationType.BROADCAST_TRANSACTION_RESPONSE);
153
+ t.is(payload.broadcast_transaction_response.result, code);
154
+
155
+ const msg = createMessage(
156
+ payload.type,
157
+ idToBuffer(payload.id),
158
+ timestampToBuffer(payload.timestamp),
159
+ payload.broadcast_transaction_response.nonce,
160
+ safeWriteUInt32BE(code, 0),
161
+ encodeCapabilities(caps)
162
+ );
163
+ const hash = await PeerWallet.blake3(msg);
164
+ t.ok(wallet.verify(payload.broadcast_transaction_response.signature, hash, wallet.publicKey));
165
+
166
+ const decoded = decodeV1networkOperation(encodeV1networkOperation(payload));
167
+ t.is(decoded.broadcast_transaction_response.result, code);
168
+ }
169
+ });
170
+
171
+ test('NetworkMessageDirector iterates validator connection response ResultCode values', async t => {
172
+ const wallet = createWallet();
173
+ const director = new NetworkMessageDirector(new NetworkMessageBuilder(wallet, config));
174
+
175
+ const id = '1';
176
+ const caps = ['cap:b', 'cap:a'];
177
+ const otherAddress = asAddress('36fdaf941de4afe602cbb1e2f56dc582466ef23fad1da55c09fd6dd841cbd117');
178
+
179
+ for (const code of uniqueResultCodes()) {
180
+ const payload = await director.buildValidatorConnectionResponse(
181
+ id,
182
+ otherAddress,
183
+ caps,
184
+ code
185
+ );
186
+ t.is(payload.type, NetworkOperationType.VALIDATOR_CONNECTION_RESPONSE);
187
+ t.is(payload.validator_connection_response.result, code);
188
+
189
+ const msg = createMessage(
190
+ payload.type,
191
+ idToBuffer(payload.id),
192
+ timestampToBuffer(payload.timestamp),
193
+ addressToBuffer(otherAddress, config.addressPrefix),
194
+ payload.validator_connection_response.nonce,
195
+ safeWriteUInt32BE(code, 0),
196
+ encodeCapabilities(caps)
197
+ );
198
+ const hash = await PeerWallet.blake3(msg);
199
+ t.ok(wallet.verify(payload.validator_connection_response.signature, hash, wallet.publicKey));
200
+ }
201
+ });