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,24 +1,23 @@
1
1
  import b4a from 'b4a';
2
- import {
3
- CLEANUP_INTERVAL_MS,
4
- CONNECTION_TIMEOUT_MS,
5
- MAX_TRANSACTIONS_PER_SECOND
6
- } from '../../../utils/constants.js';
7
2
 
8
3
  class TransactionRateLimiterService {
9
4
  #lastCleanup;
10
5
  #connectionsStatistics;
6
+ #swarm;
7
+ #config;
11
8
 
12
- constructor() {
9
+ constructor(swarm, config) {
13
10
  this.#lastCleanup = Date.now();
14
11
  this.#connectionsStatistics = new Map();
12
+ this.#swarm = swarm
13
+ this.#config = config
15
14
  }
16
15
 
17
16
  /*
18
17
  Checks if the peer has exceeded the rate limit.
19
18
  A peer is considered to have exceeded the rate limit if:
20
19
  - The time since the last activity is greater than or equal to 1000 ms (1 second)
21
- - The number of transactions in the current session is greater than or equal to MAX_TRANSACTIONS_PER_SECOND
20
+ - The number of transactions in the current session is greater than or equal to rateLimitMaxTransactionsPerSecond
22
21
  If the rate limit is exceeded, the peer is disconnected.
23
22
  */
24
23
  #hasExceededRateLimit(peer) {
@@ -31,7 +30,7 @@ class TransactionRateLimiterService {
31
30
  this.#connectionsStatistics.set(peer, peerData);
32
31
  }
33
32
 
34
- return peerData.transactionCount >= MAX_TRANSACTIONS_PER_SECOND;
33
+ return peerData.transactionCount >= this.#config.rateLimitMaxTransactionsPerSecond;
35
34
  }
36
35
 
37
36
  /*
@@ -39,7 +38,7 @@ class TransactionRateLimiterService {
39
38
  If the peer has exceeded the rate limit, it disconnects the peer.
40
39
  Otherwise, it updates the connection info with the current timestamp.
41
40
  */
42
- handleRateLimit(connection, network) {
41
+ handleRateLimit(connection) {
43
42
  const peer = b4a.toString(connection.remotePublicKey, 'hex');
44
43
  const currentTime = Date.now();
45
44
 
@@ -53,7 +52,7 @@ class TransactionRateLimiterService {
53
52
 
54
53
  if (this.#hasExceededRateLimit(peer)) {
55
54
  console.warn(`Rate limit exceeded for peer ${peer}. Disconnecting...`);
56
- network.swarm.leavePeer(connection.remotePublicKey);
55
+ this.#swarm.leavePeer(connection.remotePublicKey);
57
56
  connection.end();
58
57
  return true;
59
58
  }
@@ -63,13 +62,13 @@ class TransactionRateLimiterService {
63
62
  }
64
63
 
65
64
  #shouldCleanupConnections(currentTime) {
66
- return currentTime - this.#lastCleanup >= CLEANUP_INTERVAL_MS;
65
+ return currentTime - this.#lastCleanup >= this.#config.rateLimitCleanupIntervalMs;
67
66
  }
68
67
 
69
68
  /**
70
69
  Cleans up old connections that have timed out.
71
70
  Condition for cleanup based on #shouldCleanupConnections:
72
- - If the last cleanup was more than CLEANUP_INTERVAL_MS ago
71
+ - If the last cleanup was more than rateLimitCleanupIntervalMs ago
73
72
  */
74
73
  #cleanUpOldConnections(currentTime) {
75
74
  if (!this.#shouldCleanupConnections(currentTime)) {
@@ -123,7 +122,7 @@ class TransactionRateLimiterService {
123
122
  */
124
123
  #isConnectionExpired(peer) {
125
124
  const peerData = this.#connectionsStatistics.get(peer);
126
- return peerData.lastActivityTime - peerData.sessionStartTime >= CONNECTION_TIMEOUT_MS;
125
+ return peerData.lastActivityTime - peerData.sessionStartTime >= this.#config.rateLimitConnectionTimeoutMs;
127
126
  }
128
127
  }
129
128
 
@@ -1,6 +1,5 @@
1
1
  import PeerWallet from "trac-wallet";
2
2
  import b4a from "b4a";
3
- import { MAX_WRITERS_FOR_ADMIN_INDEXER_CONNECTION } from '../../../utils/constants.js';
4
3
  import { bufferToAddress } from '../../state/utils/address.js';
5
4
  import { sleep } from '../../../utils/helpers.js';
6
5
  import Scheduler from "../../../utils/Scheduler.js";
@@ -31,7 +30,7 @@ class ValidatorObserverService {
31
30
  * @param {Network} network
32
31
  * @param {State} state
33
32
  * @param {string} address
34
- * @param {object} config
33
+ * @param {Config} config
35
34
  **/
36
35
  constructor(network, state, address, config) {
37
36
  this.#config = config
@@ -132,7 +131,7 @@ class ValidatorObserverService {
132
131
  const validatorPubKeyHex = validatorPubKeyBuffer.toString('hex');
133
132
  const adminEntry = await this.state.getAdminEntry();
134
133
 
135
- if (validatorAddress !== adminEntry?.address || validatorListLength < MAX_WRITERS_FOR_ADMIN_INDEXER_CONNECTION) {
134
+ if (validatorAddress !== adminEntry?.address || validatorListLength < this.#config.maxWritersForAdminIndexerConnection) {
136
135
  this.#network.tryConnect(validatorPubKeyHex, 'validator');
137
136
  }
138
137
  };
@@ -151,7 +150,7 @@ class ValidatorObserverService {
151
150
  return false;
152
151
  }
153
152
 
154
- if (validatorAddress === adminEntry?.address && validatorListLength >= MAX_WRITERS_FOR_ADMIN_INDEXER_CONNECTION) {
153
+ if (validatorAddress === adminEntry?.address && validatorListLength >= this.#config.maxWritersForAdminIndexerConnection) {
155
154
  if (this.#network.validatorConnectionManager.exists(validatorPubKeyBuffer)) {
156
155
  this.#network.validatorConnectionManager.remove(validatorPubKeyBuffer)
157
156
  }
@@ -161,12 +160,12 @@ class ValidatorObserverService {
161
160
  // - Cannot connect if already connected to a validator
162
161
  // - Validator must exist and be a writer
163
162
  // - Cannot connect to indexers, except for admin-indexer
164
- // - Admin-indexer connection is allowed only when writers length has less than 10 writers
163
+ // - Admin-indexer connection is allowed only when writers length is below maxWritersForAdminIndexerConnection
165
164
  if (this.#network.validatorConnectionManager.connected(validatorPubKeyBuffer) ||
166
165
  this.#network.validatorConnectionManager.maxConnectionsReached() ||
167
166
  validatorEntry === null ||
168
167
  !validatorEntry.isWriter ||
169
- (validatorEntry.isIndexer && (validatorAddress !== adminEntry?.address || validatorListLength >= MAX_WRITERS_FOR_ADMIN_INDEXER_CONNECTION))
168
+ (validatorEntry.isIndexer && (validatorAddress !== adminEntry?.address || validatorListLength >= this.#config.maxWritersForAdminIndexerConnection))
170
169
  ) {
171
170
  return false;
172
171
  }
@@ -12,7 +12,6 @@ import {
12
12
  HYPERBEE_VALUE_ENCODING,
13
13
  BATCH_SIZE,
14
14
  ADMIN_INITIAL_STAKED_BALANCE,
15
- MAX_WRITERS_FOR_ADMIN_INDEXER_CONNECTION,
16
15
  TRAC_NAMESPACE,
17
16
  CustomEventType
18
17
  } from '../../utils/constants.js';
@@ -58,7 +57,7 @@ class State extends ReadyResource {
58
57
  /**
59
58
  * @param {Corestore} store
60
59
  * @param {PeerWallet} wallet
61
- * @param {object} config
60
+ * @param {Config} config
62
61
  **/
63
62
  constructor(store, wallet, config) {
64
63
  super();
@@ -178,7 +177,7 @@ class State extends ReadyResource {
178
177
  async isAdminAllowedToValidate() {
179
178
  const isAdmin = this.writingKey.toString('hex') === this.#config.bootstrap.toString('hex');
180
179
  const isIndexer = this.isIndexer();
181
- const lengthCondition = await this.getWriterLength() <= MAX_WRITERS_FOR_ADMIN_INDEXER_CONNECTION;
180
+ const lengthCondition = await this.getWriterLength() <= this.#config.maxWritersForAdminIndexerConnection;
182
181
  return !!(isAdmin && isIndexer && lengthCondition);
183
182
  }
184
183
 
@@ -201,8 +200,7 @@ class State extends ReadyResource {
201
200
  }
202
201
 
203
202
  async getIndexersEntry() {
204
- const indexersEntry = Object.values(this.#base.system.indexers);
205
- return indexersEntry
203
+ return Object.values(this.#base.system.indexers);
206
204
  }
207
205
 
208
206
  async isWkInIndexersEntry(wk) {