trac-msb 0.2.13 → 0.2.15

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 (121) hide show
  1. package/.github/workflows/acceptance-tests.yml +38 -0
  2. package/.github/workflows/lint-pr-title.yml +26 -0
  3. package/.github/workflows/publish.yml +33 -0
  4. package/.github/workflows/unit-tests.yml +34 -0
  5. package/package.json +7 -12
  6. package/proto/network.proto +74 -0
  7. package/rpc/rpc_services.js +4 -22
  8. package/scripts/generate-protobufs.js +12 -37
  9. package/src/config/config.js +9 -26
  10. package/src/config/env.js +17 -27
  11. package/src/core/network/Network.js +36 -73
  12. package/src/core/network/protocols/LegacyProtocol.js +11 -21
  13. package/src/core/network/protocols/NetworkMessages.js +17 -38
  14. package/src/core/network/protocols/ProtocolInterface.js +2 -14
  15. package/src/core/network/protocols/ProtocolSession.js +17 -144
  16. package/src/core/network/protocols/V1Protocol.js +18 -37
  17. package/src/core/network/protocols/legacy/NetworkMessageRouter.js +19 -25
  18. package/src/core/network/protocols/legacy/handlers/{LegacyGetRequestHandler.js → GetRequestHandler.js} +6 -6
  19. package/src/core/network/protocols/legacy/handlers/ResponseHandler.js +37 -0
  20. package/src/core/network/protocols/{legacy/handlers/LegacyRoleOperationHandler.js → shared/handlers/RoleOperationHandler.js} +11 -18
  21. package/src/core/network/protocols/{legacy/handlers/LegacySubnetworkOperationHandler.js → shared/handlers/SubnetworkOperationHandler.js} +17 -28
  22. package/src/core/network/protocols/{legacy/handlers/LegacyTransferOperationHandler.js → shared/handlers/TransferOperationHandler.js} +11 -17
  23. package/src/core/network/protocols/{legacy/handlers/BaseStateOperationHandler.js → shared/handlers/base/BaseOperationHandler.js} +12 -23
  24. package/src/core/network/protocols/shared/validators/{PartialBootstrapDeploymentValidator.js → PartialBootstrapDeployment.js} +4 -9
  25. package/src/core/network/protocols/shared/validators/{PartialRoleAccessValidator.js → PartialRoleAccess.js} +17 -51
  26. package/src/core/network/protocols/shared/validators/{PartialTransactionValidator.js → PartialTransaction.js} +7 -21
  27. package/src/core/network/protocols/shared/validators/{PartialTransferValidator.js → PartialTransfer.js} +9 -26
  28. package/src/core/network/protocols/shared/validators/{PartialOperationValidator.js → base/PartialOperation.js} +25 -47
  29. package/src/core/network/protocols/v1/NetworkMessageRouter.js +7 -91
  30. package/src/core/network/services/ConnectionManager.js +94 -146
  31. package/src/core/network/services/MessageOrchestrator.js +27 -151
  32. package/src/core/network/services/TransactionPoolService.js +18 -129
  33. package/src/core/network/services/TransactionRateLimiterService.js +34 -52
  34. package/src/core/network/services/ValidatorObserverService.js +26 -18
  35. package/src/core/state/State.js +19 -70
  36. package/src/index.js +8 -6
  37. package/src/messages/network/v1/NetworkMessageBuilder.js +79 -59
  38. package/src/messages/network/v1/NetworkMessageDirector.js +50 -16
  39. package/src/utils/Scheduler.js +8 -0
  40. package/src/utils/constants.js +5 -71
  41. package/src/utils/helpers.js +1 -10
  42. package/src/utils/normalizers.js +0 -38
  43. package/src/utils/protobuf/network.cjs +840 -0
  44. package/src/utils/protobuf/operationHelpers.js +3 -24
  45. package/tests/acceptance/v1/account/account.test.mjs +2 -8
  46. package/tests/acceptance/v1/tx/tx.test.mjs +1 -23
  47. package/tests/acceptance/v1/tx-details/tx-details.test.mjs +6 -34
  48. package/tests/fixtures/assembleMessage.fixtures.js +8 -7
  49. package/tests/fixtures/networkV1.fixtures.js +28 -2
  50. package/tests/helpers/autobaseTestHelpers.js +5 -2
  51. package/tests/helpers/createTestSignature.js +3 -2
  52. package/tests/helpers/transactionPayloads.mjs +2 -2
  53. package/tests/unit/messages/network/NetworkMessageBuilder.test.js +79 -239
  54. package/tests/unit/messages/network/NetworkMessageDirector.test.js +77 -223
  55. package/tests/unit/messages/state/applyStateMessageBuilder.complete.test.js +5 -1
  56. package/tests/unit/messages/state/applyStateMessageBuilder.partial.test.js +5 -1
  57. package/tests/unit/network/ConnectionManager.test.js +191 -0
  58. package/tests/unit/network/networkModule.test.js +1 -4
  59. package/tests/unit/unit.test.js +2 -2
  60. package/tests/unit/utils/fileUtils/readAddressesFromWhitelistFile.test.js +2 -2
  61. package/tests/unit/utils/fileUtils/readBalanceMigrationFile.test.js +2 -2
  62. package/tests/unit/utils/protobuf/operationHelpers.test.js +4 -2
  63. package/tests/unit/utils/utils.test.js +0 -1
  64. package/proto/network/v1/enums/message_type.proto +0 -16
  65. package/proto/network/v1/enums/result_code.proto +0 -84
  66. package/proto/network/v1/messages/broadcast_transaction_request.proto +0 -9
  67. package/proto/network/v1/messages/broadcast_transaction_response.proto +0 -13
  68. package/proto/network/v1/messages/liveness_request.proto +0 -8
  69. package/proto/network/v1/messages/liveness_response.proto +0 -11
  70. package/proto/network/v1/network_message.proto +0 -22
  71. package/src/core/network/protocols/connectionPolicies.js +0 -88
  72. package/src/core/network/protocols/legacy/handlers/LegacyResponseHandler.js +0 -23
  73. package/src/core/network/protocols/shared/errors/SharedValidatorRejectionError.js +0 -27
  74. package/src/core/network/protocols/v1/V1ProtocolError.js +0 -91
  75. package/src/core/network/protocols/v1/handlers/V1BaseOperationHandler.js +0 -65
  76. package/src/core/network/protocols/v1/handlers/V1BroadcastTransactionOperationHandler.js +0 -389
  77. package/src/core/network/protocols/v1/handlers/V1LivenessOperationHandler.js +0 -87
  78. package/src/core/network/protocols/v1/validators/V1BaseOperation.js +0 -211
  79. package/src/core/network/protocols/v1/validators/V1BroadcastTransactionRequest.js +0 -26
  80. package/src/core/network/protocols/v1/validators/V1BroadcastTransactionResponse.js +0 -276
  81. package/src/core/network/protocols/v1/validators/V1LivenessRequest.js +0 -15
  82. package/src/core/network/protocols/v1/validators/V1LivenessResponse.js +0 -17
  83. package/src/core/network/protocols/v1/validators/V1ValidationSchema.js +0 -210
  84. package/src/core/network/services/PendingRequestService.js +0 -172
  85. package/src/core/network/services/TransactionCommitService.js +0 -149
  86. package/src/core/network/services/ValidatorHealthCheckService.js +0 -127
  87. package/src/utils/deepEqualApplyPayload.js +0 -40
  88. package/src/utils/logger.js +0 -25
  89. package/src/utils/protobuf/networkV1.generated.cjs +0 -2460
  90. package/tests/unit/network/LegacyNetworkMessageRouter.test.js +0 -54
  91. package/tests/unit/network/ProtocolSession.test.js +0 -127
  92. package/tests/unit/network/services/ConnectionManager.test.js +0 -450
  93. package/tests/unit/network/services/MessageOrchestrator.test.js +0 -445
  94. package/tests/unit/network/services/PendingRequestService.test.js +0 -431
  95. package/tests/unit/network/services/TransactionCommitService.test.js +0 -246
  96. package/tests/unit/network/services/TransactionPoolService.test.js +0 -489
  97. package/tests/unit/network/services/TransactionRateLimiterService.test.js +0 -139
  98. package/tests/unit/network/services/ValidatorHealthCheckService.test.js +0 -115
  99. package/tests/unit/network/services/services.test.js +0 -17
  100. package/tests/unit/network/utils/v1TestUtils.js +0 -153
  101. package/tests/unit/network/v1/NetworkMessageRouterV1.test.js +0 -151
  102. package/tests/unit/network/v1/V1BaseOperation.test.js +0 -356
  103. package/tests/unit/network/v1/V1BroadcastTransactionOperationHandler.test.js +0 -129
  104. package/tests/unit/network/v1/V1BroadcastTransactionRequest.test.js +0 -53
  105. package/tests/unit/network/v1/V1BroadcastTransactionResponse.test.js +0 -512
  106. package/tests/unit/network/v1/V1LivenessRequest.test.js +0 -32
  107. package/tests/unit/network/v1/V1LivenessResponse.test.js +0 -45
  108. package/tests/unit/network/v1/V1ResultCode.test.js +0 -84
  109. package/tests/unit/network/v1/V1ValidationSchema.test.js +0 -13
  110. package/tests/unit/network/v1/connectionPolicies.test.js +0 -49
  111. package/tests/unit/network/v1/handlers/V1BaseOperationHandler.test.js +0 -284
  112. package/tests/unit/network/v1/handlers/V1BroadcastTransactionOperationHandler.test.js +0 -794
  113. package/tests/unit/network/v1/handlers/V1LivenessOperationHandler.test.js +0 -193
  114. package/tests/unit/network/v1/v1.handlers.test.js +0 -15
  115. package/tests/unit/network/v1/v1.test.js +0 -19
  116. package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionRequest.test.js +0 -119
  117. package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionResponse.test.js +0 -136
  118. package/tests/unit/network/v1/v1ValidationSchema/common.test.js +0 -308
  119. package/tests/unit/network/v1/v1ValidationSchema/livenessRequest.test.js +0 -90
  120. package/tests/unit/network/v1/v1ValidationSchema/livenessResponse.test.js +0 -133
  121. package/tests/unit/utils/deepEqualApplyPayload/deepEqualApplyPayload.test.js +0 -102
@@ -1,17 +1,7 @@
1
1
  import applyOperations from './applyOperations.cjs';
2
- import networkV1Generated from './networkV1.generated.cjs';
2
+ import networkV1Operations from './network.cjs';
3
3
  import b4a from 'b4a';
4
4
 
5
- const networkV1Operations = networkV1Generated.network.v1;
6
- const NETWORK_TO_OBJECT_OPTIONS = Object.freeze({
7
- enums: Number,
8
- longs: Number,
9
- bytes: Buffer,
10
- defaults: true,
11
- arrays: true,
12
- oneofs: false
13
- });
14
-
15
5
  /**
16
6
  * Safely encodes an operation using `applyOperations.Operation.encode`.
17
7
  * If the encoding fails (e.g., due to an invalid payload), returns an empty Buffer.
@@ -46,14 +36,6 @@ export const safeDecodeApplyOperation = (payload) => {
46
36
  return null;
47
37
  }
48
38
 
49
- export const unsafeDecodeApplyOperation= (payload) => {
50
- return applyOperations.Operation.decode(payload);
51
- }
52
-
53
- export const unsafeEncodeApplyOperation = (payload) => {
54
- return applyOperations.Operation.encode(payload);
55
- }
56
-
57
39
  export const normalizeIncomingMessage = (message) => {
58
40
  if (!message) return null;
59
41
  if (b4a.isBuffer(message)) {
@@ -69,13 +51,10 @@ export const normalizeIncomingMessage = (message) => {
69
51
  };
70
52
 
71
53
  export const encodeV1networkOperation = (payload) => {
72
- return b4a.from(networkV1Operations.MessageHeader.encode(payload).finish());
54
+ return networkV1Operations.MessageHeader.encode(payload);
73
55
  }
74
56
 
75
57
 
76
58
  export const decodeV1networkOperation = (payload) => {
77
- return networkV1Operations.MessageHeader.toObject(
78
- networkV1Operations.MessageHeader.decode(payload),
79
- NETWORK_TO_OBJECT_OPTIONS
80
- );
59
+ return networkV1Operations.MessageHeader.decode(payload);
81
60
  }
@@ -108,17 +108,11 @@ export const registerAccountTests = (context) => {
108
108
 
109
109
  it("returns 500 on internal error", async () => {
110
110
  const originalGetNodeEntry = context.rpcMsb.state.getNodeEntry
111
- const failingAddress = randomAddress(context.rpcMsb.config.addressPrefix)
112
111
 
113
- context.rpcMsb.state.getNodeEntry = async (address) => {
114
- if (address === failingAddress) {
115
- throw new Error("test")
116
- }
117
- return originalGetNodeEntry.call(context.rpcMsb.state, address)
118
- }
112
+ context.rpcMsb.state.getNodeEntry = async () => { throw new Error("test") }
119
113
 
120
114
  try {
121
- const res = await request(context.server).get(`/v1/account/${failingAddress}`)
115
+ const res = await request(context.server).get(`/v1/account/${context.wallet.address}`)
122
116
  expect(res.statusCode).toBe(500)
123
117
  expect(res.body).toEqual({ error: 'An error occurred processing the request.' })
124
118
  } finally {
@@ -1,24 +1,5 @@
1
1
  import request from "supertest"
2
2
  import { buildRpcSelfTransferPayload, waitForConnection } from "../../../helpers/transactionPayloads.mjs"
3
- import { sleep } from "../../../../src/utils/helpers.js"
4
-
5
- const TX_ENDPOINT_TIMEOUT_MS = 4000
6
- const TX_ENDPOINT_RETRY_INTERVAL_MS = 100
7
-
8
- const waitForStatusCode = async (requestFactory, expectedStatusCode, timeoutMs = TX_ENDPOINT_TIMEOUT_MS) => {
9
- const startedAt = Date.now()
10
- let response = null
11
-
12
- while ((Date.now() - startedAt) < timeoutMs) {
13
- response = await requestFactory()
14
- if (response.statusCode === expectedStatusCode) {
15
- return response
16
- }
17
- await sleep(TX_ENDPOINT_RETRY_INTERVAL_MS)
18
- }
19
-
20
- return response
21
- }
22
3
 
23
4
  export const registerTxTests = (context) => {
24
5
  describe("GET /v1/tx/:hash", () => {
@@ -36,10 +17,7 @@ export const registerTxTests = (context) => {
36
17
  .send(JSON.stringify({ payload }))
37
18
  expect(broadcastRes.statusCode).toBe(200)
38
19
 
39
- const res = await waitForStatusCode(
40
- () => request(context.server).get(`/v1/tx/${txHashHex}`),
41
- 200
42
- )
20
+ const res = await request(context.server).get(`/v1/tx/${txHashHex}`)
43
21
  expect(res.statusCode).toBe(200)
44
22
  expect(res.body).toMatchObject({ txDetails: expect.any(Object) })
45
23
  })
@@ -1,24 +1,5 @@
1
1
  import request from "supertest"
2
2
  import { buildRpcSelfTransferPayload, waitForConnection } from "../../../helpers/transactionPayloads.mjs"
3
- import { sleep } from "../../../../src/utils/helpers.js"
4
-
5
- const TX_DETAILS_TIMEOUT_MS = 4000
6
- const TX_DETAILS_RETRY_INTERVAL_MS = 100
7
-
8
- const waitForStatusCode = async (requestFactory, expectedStatusCode, timeoutMs = TX_DETAILS_TIMEOUT_MS) => {
9
- const startedAt = Date.now()
10
- let response = null
11
-
12
- while ((Date.now() - startedAt) < timeoutMs) {
13
- response = await requestFactory()
14
- if (response.statusCode === expectedStatusCode) {
15
- return response
16
- }
17
- await sleep(TX_DETAILS_RETRY_INTERVAL_MS)
18
- }
19
-
20
- return response
21
- }
22
3
 
23
4
  export const registerTxDetailsTests = (context) => {
24
5
  describe("GET /v1/tx/details", () => {
@@ -36,11 +17,8 @@ export const registerTxDetailsTests = (context) => {
36
17
  .send(JSON.stringify({ payload }))
37
18
  expect(broadcastRes.statusCode).toBe(200)
38
19
 
39
- const resConfirmed = await waitForStatusCode(
40
- () => request(context.server)
41
- .get(`/v1/tx/details/${txHashHex}?confirmed=true`),
42
- 200
43
- )
20
+ const resConfirmed = await request(context.server)
21
+ .get(`/v1/tx/details/${txHashHex}?confirmed=true`)
44
22
  expect(resConfirmed.statusCode).toBe(200)
45
23
 
46
24
  expect(resConfirmed.body).toMatchObject({
@@ -49,11 +27,8 @@ export const registerTxDetailsTests = (context) => {
49
27
  fee: expect.any(String)
50
28
  })
51
29
 
52
- const resUnconfirmed = await waitForStatusCode(
53
- () => request(context.server)
54
- .get(`/v1/tx/details/${txHashHex}?confirmed=false`),
55
- 200
56
- )
30
+ const resUnconfirmed = await request(context.server)
31
+ .get(`/v1/tx/details/${txHashHex}?confirmed=false`)
57
32
  expect(resUnconfirmed.statusCode).toBe(200)
58
33
 
59
34
  expect(resUnconfirmed.body).toMatchObject({
@@ -82,11 +57,8 @@ export const registerTxDetailsTests = (context) => {
82
57
  .send(JSON.stringify({ payload }))
83
58
  expect(broadcastRes.statusCode).toBe(200)
84
59
 
85
- const res = await waitForStatusCode(
86
- () => request(context.server)
87
- .get(`/v1/tx/details/${txHashHex}?confirmed=false`),
88
- 200
89
- )
60
+ const res = await request(context.server)
61
+ .get(`/v1/tx/details/${txHashHex}?confirmed=false`)
90
62
  expect(res.statusCode).toBe(200)
91
63
 
92
64
  expect(res.body).toMatchObject({
@@ -1,5 +1,6 @@
1
1
  import PeerWallet from 'trac-wallet';
2
2
  import b4a from 'b4a';
3
+ import { config } from '../helpers/config.js';
3
4
 
4
5
  export const mnemonicAdmin = "science edit ankle purity treat unable first express scatter depend also case nose regular rally area carbon wait power corn sibling metal crop farm";
5
6
 
@@ -8,9 +9,9 @@ export const writingKeyNonAdmin = b4a.from("1768953b234c79eccc6306fdcba2d7c1f0b0
8
9
 
9
10
  export const bootstrapAdmin = writingKeyAdmin;
10
11
 
11
- export const walletAdmin = new PeerWallet();
12
- export const walletNonAdmin = new PeerWallet();
13
- export const walletPeer = new PeerWallet();
12
+ export const walletAdmin = new PeerWallet({ derivationPath: config.derivationPath });
13
+ export const walletNonAdmin = new PeerWallet({ derivationPath: config.derivationPath });
14
+ export const walletPeer = new PeerWallet({ derivationPath: config.derivationPath });
14
15
 
15
16
  export const adminEntry = {
16
17
  address: null,
@@ -18,9 +19,9 @@ export const adminEntry = {
18
19
  }
19
20
 
20
21
  export const initAll = async () => {
21
- if (!walletAdmin.publicKey) await walletAdmin.generateKeyPair(mnemonicAdmin);
22
- if (!walletNonAdmin.publicKey) await walletNonAdmin.generateKeyPair(walletNonAdmin.generateMnemonic());
23
- if (!walletPeer.publicKey) await walletPeer.generateKeyPair(walletPeer.generateMnemonic());
22
+ if (!walletAdmin.publicKey) await walletAdmin.generateKeyPair(mnemonicAdmin, config.derivationPath);
23
+ if (!walletNonAdmin.publicKey) await walletNonAdmin.generateKeyPair(walletNonAdmin.generateMnemonic(), config.derivationPath);
24
+ if (!walletPeer.publicKey) await walletPeer.generateKeyPair(walletPeer.generateMnemonic(), config.derivationPath);
24
25
  if (!adminEntry.address && !adminEntry.wk) {
25
26
  adminEntry.address = walletAdmin.address;
26
27
  adminEntry.wk = writingKeyAdmin;
@@ -37,4 +38,4 @@ export default {
37
38
  walletPeer,
38
39
  adminEntry,
39
40
  initAll,
40
- }
41
+ }
@@ -1,6 +1,32 @@
1
1
  import b4a from 'b4a';
2
2
  import { v7 as uuidv7 } from 'uuid';
3
3
  import { NetworkOperationType, ResultCode as NetworkResultCode } from '../../src/utils/constants.js';
4
+ import { asAddress } from '../helpers/address.js';
5
+
6
+ const payloadValidatorConnectionRequest = {
7
+ type: NetworkOperationType.VALIDATOR_CONNECTION_REQUEST,
8
+ id: uuidv7(),
9
+ timestamp: 123,
10
+ validator_connection_request: {
11
+ issuer_address: asAddress('36fdaf941de4afe602cbb1e2f56dc582466ef23fad1da55c09fd6dd841cbd117'),
12
+ nonce: b4a.from('00', 'hex'),
13
+ signature: b4a.from('01', 'hex')
14
+ },
15
+ capabilities: ['cap:a', 'cap:b']
16
+ };
17
+
18
+ const payloadValidatorConnectionResponse = {
19
+ type: NetworkOperationType.VALIDATOR_CONNECTION_RESPONSE,
20
+ id: uuidv7(),
21
+ timestamp: 456,
22
+ validator_connection_response: {
23
+ issuer_address: asAddress('36fdaf941de4afe602cbb1e2f56dc582466ef23fad1da55c09fd6dd841cbd117'),
24
+ nonce: b4a.from('02', 'hex'),
25
+ signature: b4a.from('03', 'hex'),
26
+ result: NetworkResultCode.OK
27
+ },
28
+ capabilities: ['cap:a']
29
+ };
4
30
 
5
31
  const payloadLivenessRequest = {
6
32
  type: NetworkOperationType.LIVENESS_REQUEST,
@@ -44,14 +70,14 @@ const payloadBroadcastTransactionResponse = {
44
70
  broadcast_transaction_response: {
45
71
  nonce: b4a.from('0a', 'hex'),
46
72
  signature: b4a.from('0b', 'hex'),
47
- proof: b4a.from('0c', 'hex'),
48
- timestamp: 161719,
49
73
  result: NetworkResultCode.OK
50
74
  },
51
75
  capabilities: ['cap:b']
52
76
  };
53
77
 
54
78
  export default {
79
+ payloadValidatorConnectionRequest,
80
+ payloadValidatorConnectionResponse,
55
81
  payloadLivenessRequest,
56
82
  payloadLivenessResponse,
57
83
  payloadBroadcastTransactionRequest,
@@ -170,8 +170,11 @@ export function defaultOpenHyperbeeView(store) {
170
170
  }
171
171
 
172
172
  export async function createWallet(mnemonic = null) {
173
- const wallet = new PeerWallet({ networkPrefix: config.addressPrefix });
174
- await wallet.generateKeyPair(mnemonic ?? undefined);
173
+ const wallet = new PeerWallet({
174
+ networkPrefix: config.addressPrefix,
175
+ derivationPath: config.derivationPath
176
+ });
177
+ await wallet.generateKeyPair(mnemonic, config.derivationPath);
175
178
  return wallet;
176
179
  }
177
180
 
@@ -1,8 +1,9 @@
1
1
  import PeerWallet from 'trac-wallet';
2
+ import { config } from './config.js';
2
3
 
3
4
  export async function createSignature(payloadBuffer) {
4
- const wallet = new PeerWallet();
5
- await wallet.generateKeyPair();
5
+ const wallet = new PeerWallet({ derivationPath: config.derivationPath });
6
+ await wallet.generateKeyPair(undefined, config.derivationPath);
6
7
  const signature = wallet.sign(payloadBuffer);
7
8
  return { signature, wallet };
8
9
  }
@@ -23,10 +23,10 @@ export const waitForConnection = async node => {
23
23
 
24
24
  /**
25
25
  * Build a base64-encoded transfer payload and matching tx hash
26
- * that are compatible with MSB's PartialTransferValidator validator.
26
+ * that are compatible with MSB's PartialTransfer validator.
27
27
  *
28
28
  * This helper mirrors the hashing/signing logic used by
29
- * PartialOperationValidator.validateSignature, so that tests broadcast
29
+ * PartialOperation.validateSignature, so that tests broadcast
30
30
  * transactions the node will accept without touching consensus code.
31
31
  *
32
32
  * @param {object} context - General context