trac-msb 0.2.8 → 0.2.9

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 (123) hide show
  1. package/msb.mjs +3 -3
  2. package/package.json +8 -3
  3. package/proto/network.proto +74 -0
  4. package/rpc/create_server.js +2 -2
  5. package/rpc/handlers.js +2 -2
  6. package/rpc/rpc_server.js +2 -2
  7. package/rpc/rpc_services.js +44 -3
  8. package/rpc/utils/helpers.js +1 -1
  9. package/src/config/env.js +2 -0
  10. package/src/core/network/Network.js +29 -61
  11. package/src/core/network/identity/NetworkWalletFactory.js +2 -2
  12. package/src/core/network/protocols/LegacyProtocol.js +67 -0
  13. package/src/core/network/protocols/NetworkMessages.js +48 -0
  14. package/src/core/network/protocols/ProtocolInterface.js +31 -0
  15. package/src/core/network/protocols/ProtocolSession.js +59 -0
  16. package/src/core/network/protocols/V1Protocol.js +64 -0
  17. package/src/core/network/protocols/legacy/NetworkMessageRouter.js +84 -0
  18. package/src/core/network/protocols/legacy/handlers/GetRequestHandler.js +53 -0
  19. package/src/core/network/protocols/legacy/handlers/ResponseHandler.js +37 -0
  20. package/src/core/network/{messaging → protocols/legacy}/validators/base/BaseResponse.js +1 -2
  21. package/src/core/network/protocols/shared/handlers/RoleOperationHandler.js +88 -0
  22. package/src/core/network/protocols/shared/handlers/SubnetworkOperationHandler.js +93 -0
  23. package/src/core/network/{messaging → protocols/shared}/handlers/TransferOperationHandler.js +16 -15
  24. package/src/core/network/{messaging → protocols/shared}/handlers/base/BaseOperationHandler.js +7 -11
  25. package/src/core/network/{messaging → protocols/shared}/validators/PartialBootstrapDeployment.js +2 -2
  26. package/src/core/network/{messaging → protocols/shared}/validators/PartialRoleAccess.js +5 -5
  27. package/src/core/network/{messaging → protocols/shared}/validators/PartialTransaction.js +4 -4
  28. package/src/core/network/{messaging → protocols/shared}/validators/PartialTransfer.js +4 -4
  29. package/src/core/network/{messaging → protocols/shared}/validators/base/PartialOperation.js +14 -12
  30. package/src/core/network/protocols/v1/NetworkMessageRouter.js +15 -0
  31. package/src/core/network/services/ConnectionManager.js +4 -4
  32. package/src/core/network/services/MessageOrchestrator.js +1 -1
  33. package/src/core/network/services/TransactionPoolService.js +1 -2
  34. package/src/core/network/services/TransactionRateLimiterService.js +5 -3
  35. package/src/core/state/State.js +1 -2
  36. package/src/index.js +153 -180
  37. package/src/messages/network/v1/NetworkMessageBuilder.js +325 -0
  38. package/src/messages/network/v1/NetworkMessageDirector.js +137 -0
  39. package/src/messages/network/v1/networkMessageFactory.js +12 -0
  40. package/src/messages/state/ApplyStateMessageBuilder.js +661 -0
  41. package/src/messages/state/ApplyStateMessageDirector.js +516 -0
  42. package/src/messages/state/applyStateMessageFactory.js +12 -0
  43. package/src/utils/buffer.js +53 -1
  44. package/src/utils/cli.js +0 -8
  45. package/src/utils/constants.js +34 -14
  46. package/src/utils/normalizers.js +84 -2
  47. package/src/utils/protobuf/network.cjs +840 -0
  48. package/src/utils/protobuf/operationHelpers.js +10 -0
  49. package/tests/acceptance/v1/rpc.test.mjs +1 -1
  50. package/tests/fixtures/networkV1.fixtures.js +84 -0
  51. package/tests/fixtures/protobuf.fixtures.js +83 -0
  52. package/tests/helpers/config.js +1 -1
  53. package/tests/helpers/setupApplyTests.js +53 -46
  54. package/tests/unit/messages/messages.test.js +12 -0
  55. package/tests/unit/messages/network/NetworkMessageBuilder.test.js +276 -0
  56. package/tests/unit/messages/network/NetworkMessageDirector.test.js +203 -0
  57. package/tests/unit/messages/state/applyStateMessageBuilder.complete.test.js +521 -0
  58. package/tests/unit/messages/state/applyStateMessageBuilder.partial.test.js +233 -0
  59. package/tests/unit/network/ConnectionManager.test.js +6 -5
  60. package/tests/unit/network/networkModule.test.js +3 -2
  61. package/tests/unit/state/apply/addAdmin/addAdminHappyPathScenario.js +10 -6
  62. package/tests/unit/state/apply/addAdmin/addAdminScenarioHelpers.js +9 -6
  63. package/tests/unit/state/apply/addAdmin/state.apply.addAdmin.test.js +10 -7
  64. package/tests/unit/state/apply/addIndexer/addIndexerScenarioHelpers.js +18 -21
  65. package/tests/unit/state/apply/addWriter/addWriterScenarioHelpers.js +53 -38
  66. package/tests/unit/state/apply/adminRecovery/adminRecoveryScenarioHelpers.js +46 -35
  67. package/tests/unit/state/apply/appendWhitelist/appendWhitelistScenarioHelpers.js +13 -16
  68. package/tests/unit/state/apply/balanceInitialization/balanceInitializationScenarioHelpers.js +17 -11
  69. package/tests/unit/state/apply/banValidator/banValidatorScenarioHelpers.js +11 -12
  70. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentScenarioHelpers.js +9 -7
  71. package/tests/unit/state/apply/common/commonScenarioHelper.js +15 -14
  72. package/tests/unit/state/apply/common/payload-structure/initializationDisabledScenario.js +9 -4
  73. package/tests/unit/state/apply/disableInitialization/disableInitializationScenarioHelpers.js +17 -11
  74. package/tests/unit/state/apply/removeWriter/removeWriterScenarioHelpers.js +19 -14
  75. package/tests/unit/state/apply/transfer/transferDoubleSpendAcrossValidatorsScenario.js +37 -29
  76. package/tests/unit/state/apply/transfer/transferScenarioHelpers.js +9 -7
  77. package/tests/unit/state/apply/txOperation/txOperationScenarioHelpers.js +11 -9
  78. package/tests/unit/unit.test.js +1 -1
  79. package/tests/unit/utils/buffer/buffer.test.js +62 -1
  80. package/tests/unit/utils/normalizers/normalizers.test.js +469 -0
  81. package/tests/unit/utils/protobuf/operationHelpers.test.js +120 -2
  82. package/docs/networking-dualstack-plan.md +0 -75
  83. package/docs/networking-layer-redesign.md +0 -155
  84. package/src/core/network/messaging/NetworkMessages.js +0 -64
  85. package/src/core/network/messaging/handlers/GetRequestHandler.js +0 -113
  86. package/src/core/network/messaging/handlers/ResponseHandler.js +0 -107
  87. package/src/core/network/messaging/handlers/RoleOperationHandler.js +0 -114
  88. package/src/core/network/messaging/handlers/SubnetworkOperationHandler.js +0 -149
  89. package/src/core/network/messaging/routes/NetworkMessageRouter.js +0 -98
  90. package/src/core/network/messaging/validators/AdminResponse.js +0 -58
  91. package/src/core/network/messaging/validators/CustomNodeResponse.js +0 -46
  92. package/src/messages/base/StateBuilder.js +0 -25
  93. package/src/messages/completeStateMessages/CompleteStateMessageBuilder.js +0 -425
  94. package/src/messages/completeStateMessages/CompleteStateMessageDirector.js +0 -252
  95. package/src/messages/completeStateMessages/CompleteStateMessageOperations.js +0 -296
  96. package/src/messages/partialStateMessages/PartialStateMessageBuilder.js +0 -272
  97. package/src/messages/partialStateMessages/PartialStateMessageDirector.js +0 -137
  98. package/src/messages/partialStateMessages/PartialStateMessageOperations.js +0 -138
  99. package/tests/integration/apply/addAdmin/addAdminBasic.test.js +0 -69
  100. package/tests/integration/apply/addAdmin/addAdminRecovery.test.js +0 -126
  101. package/tests/integration/apply/addIndexer.test.js +0 -239
  102. package/tests/integration/apply/addWhitelist.test.js +0 -53
  103. package/tests/integration/apply/addWriter.test.js +0 -245
  104. package/tests/integration/apply/apply.test.js +0 -19
  105. package/tests/integration/apply/banValidator.test.js +0 -116
  106. package/tests/integration/apply/postTx/invalidSubValues.test.js +0 -103
  107. package/tests/integration/apply/postTx/postTx.test.js +0 -196
  108. package/tests/integration/apply/removeIndexer.test.js +0 -132
  109. package/tests/integration/apply/removeWriter.test.js +0 -168
  110. package/tests/integration/apply/transfer.test.js +0 -83
  111. package/tests/integration/integration.test.js +0 -9
  112. package/tests/unit/messageOperations/assembleAddIndexerMessage.test.js +0 -21
  113. package/tests/unit/messageOperations/assembleAddWriterMessage.test.js +0 -17
  114. package/tests/unit/messageOperations/assembleAdminMessage.test.js +0 -68
  115. package/tests/unit/messageOperations/assembleBanWriterMessage.test.js +0 -17
  116. package/tests/unit/messageOperations/assemblePostTransaction.test.js +0 -424
  117. package/tests/unit/messageOperations/assembleRemoveIndexerMessage.test.js +0 -19
  118. package/tests/unit/messageOperations/assembleRemoveWriterMessage.test.js +0 -17
  119. package/tests/unit/messageOperations/assembleWhitelistMessages.test.js +0 -59
  120. package/tests/unit/messageOperations/commonsStateMessageOperationsTest.js +0 -278
  121. package/tests/unit/messageOperations/stateMessageOperations.test.js +0 -19
  122. /package/src/core/network/{messaging → protocols/legacy}/validators/ValidatorResponse.js +0 -0
  123. /package/src/utils/{operations.js → applyOperations.js} +0 -0
@@ -1,12 +1,12 @@
1
1
  import b4a from 'b4a';
2
2
  import PeerWallet from 'trac-wallet';
3
- import Check from '../../../../../utils/check.js';
4
- import {bufferToAddress} from "../../../../state/utils/address.js";
5
- import {createMessage} from "../../../../../utils/buffer.js";
6
- import {OperationType} from "../../../../../utils/constants.js";
7
- import {bufferToBigInt} from "../../../../../utils/amountSerialization.js";
8
- import {FEE} from "../../../../state/utils/transaction.js";
9
- import * as operationsUtils from '../../../../../utils/operations.js';
3
+ import Check from '../../../../../../utils/check.js';
4
+ import {bufferToAddress} from "../../../../../state/utils/address.js";
5
+ import {createMessage} from "../../../../../../utils/buffer.js";
6
+ import {OperationType} from "../../../../../../utils/constants.js";
7
+ import {bufferToBigInt} from "../../../../../../utils/amountSerialization.js";
8
+ import {FEE} from "../../../../../state/utils/transaction.js";
9
+ import * as operationsUtils from '../../../../../../utils/applyOperations.js';
10
10
 
11
11
  const MAX_AMOUNT = BigInt('0xffffffffffffffffffffffffffffffff');
12
12
  const FEE_BIGINT = bufferToBigInt(FEE);
@@ -16,15 +16,15 @@ class PartialOperation {
16
16
  #state;
17
17
  #check;
18
18
  #config
19
- #wallet
19
+ #selfAddress
20
20
 
21
- constructor(state, wallet, config) {
21
+ constructor(state, selfAddress, config) {
22
22
  this.#state = state;
23
23
  this.#config = config;
24
24
  this.#check = new Check(this.#config);
25
25
  this.max_amount = MAX_AMOUNT;
26
26
  this.fee = FEE_BIGINT;
27
- this.#wallet = wallet;
27
+ this.#selfAddress = selfAddress;
28
28
  }
29
29
 
30
30
  get state() {
@@ -176,7 +176,7 @@ class PartialOperation {
176
176
  isOperationNotCompleted(payload) {
177
177
  const operationKey = operationsUtils.operationToPayload(payload.type);
178
178
  const operation = payload[operationKey];
179
- const {va, vn, vs} = operation;
179
+ const { va, vn, vs } = operation;
180
180
 
181
181
  const condition = va === undefined && vn === undefined && vs === undefined
182
182
  if (!condition) {
@@ -219,8 +219,10 @@ class PartialOperation {
219
219
  * Flow: Validator -> submits tx with tap-wallet -> RPC-> Validator -validates tx-> REJECT (self-validation)
220
220
  */
221
221
  validateNoSelfValidation(payload) {
222
+ if (!this.#selfAddress) return;
223
+
222
224
  const requesterAddress = bufferToAddress(payload.address, this.#config.addressPrefix);
223
- if (this.#wallet.address === requesterAddress) {
225
+ if (this.#selfAddress === requesterAddress) {
224
226
  throw new Error('Requester address cannot be the same as the validator wallet address.');
225
227
  }
226
228
  }
@@ -0,0 +1,15 @@
1
+ import { MessageHeader } from '../../../../utils/protobuf/network.cjs';
2
+
3
+ class NetworkMessageRouterV1 {
4
+ #config;
5
+
6
+ constructor(config) {
7
+ this.#config = config;
8
+ }
9
+
10
+ async route(incomingMessage) {
11
+ MessageHeader.decode(incomingMessage);
12
+ }
13
+ }
14
+
15
+ export default NetworkMessageRouterV1;
@@ -44,10 +44,10 @@ class ConnectionManager {
44
44
 
45
45
  const target = this.pickRandomValidator(connectedValidators);
46
46
  const entry = this.#validators.get(target);
47
- if (!entry || !entry.connection || !entry.connection.messenger) return null;
47
+ if (!entry || !entry.connection || !entry.connection.protocolSession?.has('legacy')) return null;
48
48
 
49
49
  try {
50
- entry.connection.messenger.send(message);
50
+ entry.connection.protocolSession.send(message);
51
51
  entry.sent = (entry.sent || 0) + 1;
52
52
  } catch (e) {
53
53
  // Swallow individual send errors.
@@ -67,9 +67,9 @@ class ConnectionManager {
67
67
  if (!this.exists(publicKeyHex) || !this.connected(publicKeyHex)) return false; // Fail silently
68
68
 
69
69
  const validator = this.#validators.get(publicKeyHex);
70
- if (!validator || !validator.connection || !validator.connection.messenger) return false;
70
+ if (!validator || !validator.connection || !validator.connection.protocolSession) return false;
71
71
  try {
72
- validator.connection.messenger.send(message);
72
+ validator.connection.protocolSession.send(message);
73
73
  } catch (e) {
74
74
  // Swallow individual send errors.
75
75
  }
@@ -1,5 +1,5 @@
1
1
  import { sleep } from '../../../utils/helpers.js';
2
- import { operationToPayload } from '../../../utils/operations.js';
2
+ import { operationToPayload } from '../../../utils/applyOperations.js';
3
3
  /**
4
4
  * MessageOrchestrator coordinates message submission, retry, and validator management.
5
5
  * It works with ConnectionManager and ledger state to ensure reliable message delivery.
@@ -37,7 +37,7 @@ class TransactionPoolService {
37
37
  console.info('TransactionPoolService can not start. Wallet is not enabled');
38
38
  return;
39
39
  }
40
- if (this.scheduler && this.scheduler.isRunning) {
40
+ if (this.#scheduler && this.#scheduler.isRunning) {
41
41
  console.info('TransactionPoolService is already started');
42
42
  return;
43
43
  }
@@ -65,7 +65,6 @@ class TransactionPoolService {
65
65
 
66
66
  async #processTransactions() {
67
67
  const canValidate = await this.#checkValidationPermissions();
68
-
69
68
  if (canValidate && this.#tx_pool.length > 0) {
70
69
  const batch = this.#prepareBatch();
71
70
  await this.#state.append(batch);
@@ -8,10 +8,12 @@ import {
8
8
  class TransactionRateLimiterService {
9
9
  #lastCleanup;
10
10
  #connectionsStatistics;
11
+ #swarm;
11
12
 
12
- constructor() {
13
+ constructor(swarm) {
13
14
  this.#lastCleanup = Date.now();
14
15
  this.#connectionsStatistics = new Map();
16
+ this.#swarm = swarm
15
17
  }
16
18
 
17
19
  /*
@@ -39,7 +41,7 @@ class TransactionRateLimiterService {
39
41
  If the peer has exceeded the rate limit, it disconnects the peer.
40
42
  Otherwise, it updates the connection info with the current timestamp.
41
43
  */
42
- handleRateLimit(connection, network) {
44
+ handleRateLimit(connection) {
43
45
  const peer = b4a.toString(connection.remotePublicKey, 'hex');
44
46
  const currentTime = Date.now();
45
47
 
@@ -53,7 +55,7 @@ class TransactionRateLimiterService {
53
55
 
54
56
  if (this.#hasExceededRateLimit(peer)) {
55
57
  console.warn(`Rate limit exceeded for peer ${peer}. Disconnecting...`);
56
- network.swarm.leavePeer(connection.remotePublicKey);
58
+ this.#swarm.leavePeer(connection.remotePublicKey);
57
59
  connection.end();
58
60
  return true;
59
61
  }
@@ -201,8 +201,7 @@ class State extends ReadyResource {
201
201
  }
202
202
 
203
203
  async getIndexersEntry() {
204
- const indexersEntry = Object.values(this.#base.system.indexers);
205
- return indexersEntry
204
+ return Object.values(this.#base.system.indexers);
206
205
  }
207
206
 
208
207
  async isWkInIndexersEntry(wk) {