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,172 +0,0 @@
1
- import {NetworkOperationType, ResultCode} from '../../../utils/constants.js';
2
- import {isHexString} from '../../../utils/helpers.js';
3
- import {V1ProtocolError, V1TimeoutError, V1UnexpectedError} from "../protocols/v1/V1ProtocolError.js";
4
- import {Config} from '../../../config/config.js';
5
- import b4a from 'b4a';
6
-
7
- const PEER_PUBLIC_KEY_HEX_LENGTH = 64;
8
-
9
- class PendingRequestService {
10
- #pendingRequests;
11
- #requestMessageTypes = [NetworkOperationType.LIVENESS_REQUEST, NetworkOperationType.BROADCAST_TRANSACTION_REQUEST];
12
- #config;
13
-
14
- constructor(config) {
15
- this.#pendingRequests = new Map(); // Map<id, pendingRequestEntry>
16
- this.#config = config;
17
- }
18
-
19
- has(id) {
20
- return this.#pendingRequests.has(id);
21
- }
22
-
23
- isProbePending(peerPubKeyHex) {
24
- for (const [, entry] of this.#pendingRequests) {
25
- if (entry.requestedTo === peerPubKeyHex && entry.requestType === NetworkOperationType.LIVENESS_REQUEST) {
26
- return true;
27
- }
28
- }
29
- return false;
30
- }
31
-
32
- #validateRegisterInput(peerPubKeyHex, message) {
33
- if (!isHexString(peerPubKeyHex) || peerPubKeyHex.length !== PEER_PUBLIC_KEY_HEX_LENGTH) {
34
- throw new Error('Invalid peer public key. Expected 32-byte hex string.');
35
- }
36
-
37
- if (!message || typeof message !== 'object') {
38
- throw new Error('Pending request message must be an object.');
39
- }
40
-
41
- if (typeof message.id !== 'string' || message.id.length === 0) {
42
- throw new Error('Pending request ID must be a non-empty string.');
43
- }
44
-
45
- if (!this.#requestMessageTypes.includes(message.type)) {
46
- throw new Error('Unsupported pending request type.');
47
- }
48
- }
49
-
50
- /*
51
- @returns {Promise}
52
- */
53
- registerPendingRequest(peerPubKeyHex, message) {
54
- this.#validateRegisterInput(peerPubKeyHex, message);
55
- const id = message.id;
56
- if (this.#pendingRequests.size >= this.#config.maxPendingRequestsInPendingRequestsService) {
57
- throw new Error('Maximum number of pending requests reached.');
58
- }
59
-
60
- if (this.#pendingRequests.has(id)) {
61
- throw new Error(`Pending request with ID ${id} from peer ${peerPubKeyHex} already exists.`);
62
- }
63
-
64
- const entry = {
65
- id: id,
66
- requestType: message.type,
67
- requestTxData: this.#extractRequestTxData(message),
68
- requestedTo: peerPubKeyHex,
69
- timeoutMs: this.#config.pendingRequestTimeout,
70
- timeoutId: null,
71
- resolve: null,
72
- reject: null,
73
- }
74
-
75
- const promise = new Promise((resolve, reject) => {
76
- entry.resolve = resolve;
77
- entry.reject = reject;
78
- });
79
-
80
- entry.timeoutId = setTimeout(() => {
81
- this.rejectPendingRequest(
82
- id,
83
- new V1TimeoutError(
84
- `Pending request with ID ${id} from peer ${peerPubKeyHex} timed out after ${entry.timeoutMs} ms.`,
85
- true
86
- ));
87
-
88
- }, entry.timeoutMs);
89
-
90
- this.#pendingRequests.set(id, entry);
91
- return promise;
92
- }
93
-
94
- #extractRequestTxData(message) {
95
- if (message.type !== NetworkOperationType.BROADCAST_TRANSACTION_REQUEST) return null;
96
- const txData = message.broadcast_transaction_request?.data;
97
- return b4a.isBuffer(txData) ? txData : null;
98
- }
99
-
100
- getAndDeletePendingRequest(id) {
101
- const entry = this.#pendingRequests.get(id);
102
- if (!entry) return null;
103
-
104
- clearTimeout(entry.timeoutId);
105
- this.#pendingRequests.delete(id);
106
- return entry;
107
- }
108
-
109
- getPendingRequest(id) {
110
- const entry = this.#pendingRequests.get(id);
111
- if (!entry) return null;
112
- return entry;
113
- }
114
-
115
- // for now, we are resolving only resultCode, but we can extend it in the future if needed...
116
- resolvePendingRequest(id, resultCode = ResultCode.OK) {
117
- const entry = this.getAndDeletePendingRequest(id);
118
- if (!entry) return false;
119
- entry.resolve(resultCode);
120
- return true;
121
- }
122
-
123
- rejectPendingRequest(id, error) {
124
- const entry = this.getAndDeletePendingRequest(id);
125
- if (!entry) return false;
126
- const err = error instanceof V1ProtocolError
127
- ? error
128
- : new V1UnexpectedError(error?.message ?? 'Unexpected error');
129
- entry.reject(err);
130
- return true;
131
- }
132
-
133
- rejectPendingRequestsForPeer(peerPubKeyHex, error) {
134
- const idsToReject = [];
135
- for (const [id, entry] of this.#pendingRequests) {
136
- if (entry.requestedTo === peerPubKeyHex) idsToReject.push(id);
137
- }
138
-
139
- for (const id of idsToReject) {
140
- this.rejectPendingRequest(id, error);
141
- }
142
-
143
- return idsToReject.length;
144
- }
145
-
146
- stopPendingRequestTimeout(id) {
147
- const entry = this.#pendingRequests.get(id);
148
- if (!entry) return false;
149
-
150
- clearTimeout(entry.timeoutId);
151
- entry.timeoutId = null;
152
- return true;
153
- }
154
-
155
- close() {
156
- for (const [id, entry] of this.#pendingRequests) {
157
- clearTimeout(entry.timeoutId);
158
- try {
159
- entry.reject(
160
- new V1UnexpectedError(
161
- `Pending request ${id} cancelled (shutdown).`,
162
- false)
163
- );
164
- } catch (error) {
165
- console.error(`PendingRequestService.close: failed to reject pending request ${id}:`, error);
166
- }
167
- }
168
- this.#pendingRequests.clear();
169
- }
170
- }
171
-
172
- export default PendingRequestService;
@@ -1,149 +0,0 @@
1
- import {isHexString} from '../../../utils/helpers.js';
2
- import {TRANSACTION_COMMIT_SERVICE_BUFFER_SIZE} from '../../../utils/constants.js';
3
-
4
- const TX_HASH_HEX_STRING_LENGTH = 64;
5
-
6
- class TransactionCommitService {
7
- #pendingCommits;
8
- #config;
9
-
10
- constructor(config) {
11
- this.#pendingCommits = new Map(); // Map<txHash, pendingCommitEntry>
12
- this.#config = config;
13
- }
14
-
15
- #assertTxHash(txHash) {
16
- if (!isHexString(txHash) || txHash.length !== TX_HASH_HEX_STRING_LENGTH) {
17
- throw new PendingCommitInvalidTxHashError(txHash);
18
- }
19
- }
20
-
21
- has(txHash) {
22
- this.#assertTxHash(txHash);
23
- return this.#pendingCommits.has(txHash);
24
- }
25
-
26
- /*
27
- @returns {Promise}
28
- */
29
- registerPendingCommit(txHash) {
30
- this.#assertTxHash(txHash);
31
-
32
- if (this.#pendingCommits.size >= TRANSACTION_COMMIT_SERVICE_BUFFER_SIZE) {
33
- throw new PendingCommitBufferFullError(TRANSACTION_COMMIT_SERVICE_BUFFER_SIZE);
34
- }
35
-
36
- if (this.#pendingCommits.has(txHash)) {
37
- throw new PendingCommitAlreadyExistsError(txHash);
38
- }
39
-
40
- const timeoutMs = this.#config.txCommitTimeout;
41
-
42
- const entry = {
43
- txHash,
44
- timeoutMs,
45
- timeoutId: null,
46
- resolve: null,
47
- reject: null,
48
- };
49
-
50
- const promise = new Promise((resolve, reject) => {
51
- entry.resolve = resolve;
52
- entry.reject = reject;
53
- });
54
-
55
- entry.timeoutId = setTimeout(() => {
56
- this.rejectPendingCommit(
57
- txHash,
58
- new PendingCommitTimeoutError(txHash, timeoutMs)
59
- );
60
- }, timeoutMs);
61
-
62
- this.#pendingCommits.set(txHash, entry);
63
- return promise;
64
- }
65
-
66
- getAndDeletePendingCommit(txHash) {
67
- this.#assertTxHash(txHash);
68
- const entry = this.#pendingCommits.get(txHash);
69
- if (!entry) return null;
70
-
71
- clearTimeout(entry.timeoutId);
72
- this.#pendingCommits.delete(txHash);
73
- return entry;
74
- }
75
-
76
- resolvePendingCommit(txHash, receipt = null) {
77
- this.#assertTxHash(txHash);
78
- const entry = this.getAndDeletePendingCommit(txHash);
79
- if (!entry) return false;
80
- entry.resolve(receipt);
81
- return true;
82
- }
83
-
84
- rejectPendingCommit(txHash, error) {
85
- this.#assertTxHash(txHash);
86
- const entry = this.getAndDeletePendingCommit(txHash);
87
- if (!entry) return false;
88
-
89
- entry.reject(
90
- error instanceof Error
91
- ? error
92
- : new PendingCommitUnexpectedError('Unexpected commit error')
93
- );
94
-
95
- return true;
96
- }
97
-
98
- close() {
99
- for (const [txHash, entry] of this.#pendingCommits) {
100
- clearTimeout(entry.timeoutId);
101
- try {
102
- entry.reject(
103
- new PendingCommitCancelledError(txHash)
104
- );
105
- } catch (error) {
106
- console.error(`TransactionCommitService.close: failed to reject pending commit ${txHash}:`, error);
107
- }
108
- }
109
- this.#pendingCommits.clear();
110
- }
111
- }
112
-
113
- export default TransactionCommitService;
114
-
115
- export class PendingCommitInvalidTxHashError extends Error {
116
- constructor(txHash) {
117
- super(`Invalid txHash format: ${txHash}`);
118
- }
119
- }
120
-
121
- export class PendingCommitBufferFullError extends Error {
122
- constructor(limit) {
123
- super(`Maximum number of pending commits reached (limit=${limit}).`);
124
- }
125
- }
126
-
127
- export class PendingCommitAlreadyExistsError extends Error {
128
- constructor(txHash) {
129
- super(`Pending commit for txHash ${txHash} already exists.`);
130
- }
131
- }
132
-
133
- export class PendingCommitTimeoutError extends Error {
134
- constructor(txHash, timeoutMs) {
135
- super(`Pending commit for txHash ${txHash} timed out after ${timeoutMs} ms.`);
136
- }
137
- }
138
-
139
- export class PendingCommitCancelledError extends Error {
140
- constructor(txHash) {
141
- super(`Pending commit ${txHash} cancelled (shutdown).`);
142
- }
143
- }
144
-
145
- export class PendingCommitUnexpectedError extends Error {
146
- constructor(message = 'Unexpected commit error') {
147
- super(message);
148
- }
149
- }
@@ -1,127 +0,0 @@
1
- import ReadyResource from 'ready-resource';
2
- import b4a from 'b4a';
3
- import { generateUUID } from '../../../utils/helpers.js';
4
- import { EventType } from '../../../utils/constants.js';
5
- import { Logger } from '../../../utils/logger.js';
6
-
7
- const DEFAULT_HEALTH_CHECK_INTERVAL_MS = 300000; // 5 minutes
8
- class ValidatorHealthCheckService extends ReadyResource {
9
- #config;
10
- #intervalMs;
11
- #timers;
12
- #logger;
13
-
14
- /**
15
- * @param {object} config
16
- */
17
- constructor(config = {}) {
18
- super();
19
- this.#config = config;
20
- this.#timers = new Map();
21
- this.#logger = new Logger(config);
22
-
23
- const interval = this.#config.validatorHealthCheckInterval;
24
- this.#intervalMs = interval ? this.#checkInterval(interval) : DEFAULT_HEALTH_CHECK_INTERVAL_MS;
25
-
26
- this.#logger.debug(`initialized with intervalMs ${this.#intervalMs}`);
27
- }
28
-
29
- get size() {
30
- return this.#timers.size;
31
- }
32
-
33
- async _open() {
34
- this.#logger.debug('open: health check service ready');
35
- }
36
-
37
- async _close() {
38
- this.#logger.debug('close: stopping all health checks');
39
- this.#stopAll();
40
- this.#timers.clear();
41
- }
42
-
43
- /**
44
- * Start periodic health checks for a validator.
45
- * @param {String} publicKey
46
- */
47
- start(publicKey) {
48
- if (!this.opened) {
49
- throw new Error('start: service not ready. Call ready() before start().');
50
- }
51
- const publicKeyHex = this.#normalizePublicKey(publicKey);
52
- if (this.#timers.has(publicKeyHex)) {
53
- this.#logger.debug(`start: already scheduled for ${publicKey}`);
54
- return false; // TODO: Implement better error handling
55
- }
56
-
57
- const timerId = setInterval(() => {
58
- this.#emitHealthCheck(publicKeyHex);
59
- }, this.#intervalMs);
60
-
61
- this.#timers.set(publicKeyHex, { timerId, intervalMs: this.#intervalMs });
62
- this.#logger.debug(`start: scheduled health checks for ${publicKeyHex} every ${this.#intervalMs} ms`);
63
-
64
- return true;
65
- }
66
-
67
- /**
68
- * Stop periodic health checks for a validator.
69
- * @param {String} publicKey
70
- * @returns {boolean} true if stopped, false if not scheduled
71
- */
72
- stop(publicKey) {
73
- const publicKeyHex = this.#normalizePublicKey(publicKey);
74
- const entry = this.#timers.get(publicKeyHex);
75
- if (!entry) {
76
- this.#logger.debug(`stop: did not find scheduled health check for public key ${publicKey}. Aborting`);
77
- return false;
78
- }
79
- clearInterval(entry.timerId);
80
- this.#timers.delete(publicKeyHex);
81
- this.#logger.debug(`stop: cancelled health checks for ${publicKeyHex}`);
82
- return true;
83
- }
84
-
85
- /**
86
- * Check if a validator is scheduled.
87
- * @param {String} publicKey
88
- * @returns {boolean}
89
- */
90
- has(publicKey) {
91
- return this.#timers.has(this.#normalizePublicKey(publicKey));
92
- }
93
-
94
- async #emitHealthCheck(publicKey) {
95
- try {
96
- const requestId = generateUUID();
97
- this.emit(EventType.VALIDATOR_HEALTH_CHECK, publicKey, requestId);
98
- this.#logger.debug(`Emitted health check event for ${publicKey} with requestId ${requestId}`);
99
- } catch (error) {
100
- this.#logger.error(`ValidatorHealthCheckService: Failed to emit health check for ${publicKey}: ${error?.message || error}`);
101
- }
102
- }
103
-
104
- #stopAll() {
105
- this.#logger.debug(`stopAll: cancelling health checks for ${this.#timers.size} validators`);
106
- for (const publicKey of this.#timers.keys()) {
107
- this.stop(publicKey);
108
- }
109
- }
110
-
111
- #checkInterval(intervalMs) {
112
- const ms = Number(intervalMs);
113
- if (!Number.isFinite(ms) || ms <= 0) {
114
- throw new RangeError(`ValidatorHealthCheckService: invalid intervalMs value: ${intervalMs}`);
115
- }
116
- return ms;
117
- }
118
-
119
- // TODO: This method is used in multiple places. Consider moving it to a utility file or exposing it from PeerWallet.
120
- #normalizePublicKey(publicKey) {
121
- if (b4a.isBuffer(publicKey)) return publicKey.toString('hex');
122
- if (typeof publicKey === 'string') return publicKey.toLowerCase();
123
- throw new TypeError('ValidatorHealthCheckService: publicKey must be a Buffer or hex string');
124
- }
125
- }
126
-
127
- export default ValidatorHealthCheckService;
@@ -1,40 +0,0 @@
1
- import b4a from 'b4a';
2
- import _ from 'lodash';
3
-
4
- export const isDeepEqualApplyPayload = (left, right) => {
5
- if (left === right) return true;
6
-
7
- const leftIsBuffer = b4a.isBuffer(left);
8
- const rightIsBuffer = b4a.isBuffer(right);
9
-
10
- if (leftIsBuffer || rightIsBuffer) {
11
- return leftIsBuffer && rightIsBuffer && b4a.equals(left, right);
12
- }
13
-
14
- const leftIsArray = Array.isArray(left);
15
- const rightIsArray = Array.isArray(right);
16
-
17
- if (leftIsArray || rightIsArray) {
18
- if (!leftIsArray || !rightIsArray) return false;
19
- if (left.length !== right.length) return false;
20
-
21
- for (let i = 0; i < left.length; i++) {
22
- if (!isDeepEqualApplyPayload(left[i], right[i])) return false;
23
- }
24
- return true;
25
- }
26
-
27
- if (!_.isObject(left) || !_.isObject(right)) return false;
28
-
29
- const leftKeys = Object.keys(left);
30
- const rightKeys = Object.keys(right);
31
-
32
- if (leftKeys.length !== rightKeys.length) return false;
33
-
34
- for (const key of leftKeys) {
35
- if (!Object.prototype.hasOwnProperty.call(right, key)) return false;
36
- if (!isDeepEqualApplyPayload(left[key], right[key])) return false;
37
- }
38
-
39
- return true;
40
- };
@@ -1,25 +0,0 @@
1
- export class Logger {
2
- #config
3
- constructor(config) {
4
- this.#config = config;
5
- }
6
-
7
- info(message) {
8
- console.log("i: " + message);
9
- }
10
-
11
- debug(message) {
12
- if (this.#config.debug) {
13
- console.debug("d: " + message);
14
- }
15
- }
16
-
17
- error(message) {
18
- console.error("e: " + message);
19
- }
20
-
21
- warn(message) {
22
- console.warn("w: " + message);
23
- }
24
-
25
- }