trac-msb 0.2.12 → 0.2.13
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.
- package/package.json +9 -4
- package/proto/network/v1/enums/message_type.proto +16 -0
- package/proto/network/v1/enums/result_code.proto +84 -0
- package/proto/network/v1/messages/broadcast_transaction_request.proto +9 -0
- package/proto/network/v1/messages/broadcast_transaction_response.proto +13 -0
- package/proto/network/v1/messages/liveness_request.proto +8 -0
- package/proto/network/v1/messages/liveness_response.proto +11 -0
- package/proto/network/v1/network_message.proto +22 -0
- package/rpc/rpc_services.js +22 -4
- package/scripts/generate-protobufs.js +37 -12
- package/src/config/config.js +26 -5
- package/src/config/env.js +25 -11
- package/src/core/network/Network.js +73 -36
- package/src/core/network/protocols/LegacyProtocol.js +21 -11
- package/src/core/network/protocols/NetworkMessages.js +38 -17
- package/src/core/network/protocols/ProtocolInterface.js +14 -2
- package/src/core/network/protocols/ProtocolSession.js +144 -17
- package/src/core/network/protocols/V1Protocol.js +37 -18
- package/src/core/network/protocols/connectionPolicies.js +88 -0
- package/src/core/network/protocols/legacy/NetworkMessageRouter.js +25 -19
- package/src/core/network/protocols/{shared/handlers/base/BaseOperationHandler.js → legacy/handlers/BaseStateOperationHandler.js} +23 -12
- package/src/core/network/protocols/legacy/handlers/{GetRequestHandler.js → LegacyGetRequestHandler.js} +6 -6
- package/src/core/network/protocols/legacy/handlers/LegacyResponseHandler.js +23 -0
- package/src/core/network/protocols/{shared/handlers/RoleOperationHandler.js → legacy/handlers/LegacyRoleOperationHandler.js} +18 -11
- package/src/core/network/protocols/{shared/handlers/SubnetworkOperationHandler.js → legacy/handlers/LegacySubnetworkOperationHandler.js} +28 -17
- package/src/core/network/protocols/{shared/handlers/TransferOperationHandler.js → legacy/handlers/LegacyTransferOperationHandler.js} +17 -11
- package/src/core/network/protocols/shared/errors/SharedValidatorRejectionError.js +27 -0
- package/src/core/network/protocols/shared/validators/{PartialBootstrapDeployment.js → PartialBootstrapDeploymentValidator.js} +9 -4
- package/src/core/network/protocols/shared/validators/{base/PartialOperation.js → PartialOperationValidator.js} +47 -25
- package/src/core/network/protocols/shared/validators/{PartialRoleAccess.js → PartialRoleAccessValidator.js} +51 -17
- package/src/core/network/protocols/shared/validators/{PartialTransaction.js → PartialTransactionValidator.js} +21 -7
- package/src/core/network/protocols/shared/validators/{PartialTransfer.js → PartialTransferValidator.js} +26 -9
- package/src/core/network/protocols/v1/NetworkMessageRouter.js +91 -7
- package/src/core/network/protocols/v1/V1ProtocolError.js +91 -0
- package/src/core/network/protocols/v1/handlers/V1BaseOperationHandler.js +65 -0
- package/src/core/network/protocols/v1/handlers/V1BroadcastTransactionOperationHandler.js +389 -0
- package/src/core/network/protocols/v1/handlers/V1LivenessOperationHandler.js +87 -0
- package/src/core/network/protocols/v1/validators/V1BaseOperation.js +211 -0
- package/src/core/network/protocols/v1/validators/V1BroadcastTransactionRequest.js +26 -0
- package/src/core/network/protocols/v1/validators/V1BroadcastTransactionResponse.js +276 -0
- package/src/core/network/protocols/v1/validators/V1LivenessRequest.js +15 -0
- package/src/core/network/protocols/v1/validators/V1LivenessResponse.js +17 -0
- package/src/core/network/protocols/v1/validators/V1ValidationSchema.js +210 -0
- package/src/core/network/services/ConnectionManager.js +146 -94
- package/src/core/network/services/MessageOrchestrator.js +151 -27
- package/src/core/network/services/PendingRequestService.js +172 -0
- package/src/core/network/services/TransactionCommitService.js +149 -0
- package/src/core/network/services/TransactionPoolService.js +129 -18
- package/src/core/network/services/TransactionRateLimiterService.js +52 -34
- package/src/core/network/services/ValidatorHealthCheckService.js +127 -0
- package/src/core/network/services/ValidatorObserverService.js +18 -26
- package/src/core/state/State.js +70 -19
- package/src/index.js +5 -4
- package/src/messages/network/v1/NetworkMessageBuilder.js +59 -79
- package/src/messages/network/v1/NetworkMessageDirector.js +16 -50
- package/src/utils/Scheduler.js +0 -8
- package/src/utils/constants.js +71 -5
- package/src/utils/deepEqualApplyPayload.js +40 -0
- package/src/utils/helpers.js +10 -1
- package/src/utils/logger.js +25 -0
- package/src/utils/normalizers.js +38 -0
- package/src/utils/protobuf/networkV1.generated.cjs +2460 -0
- package/src/utils/protobuf/operationHelpers.js +24 -3
- package/tests/acceptance/v1/account/account.test.mjs +8 -2
- package/tests/acceptance/v1/tx/tx.test.mjs +23 -1
- package/tests/acceptance/v1/tx-details/tx-details.test.mjs +34 -6
- package/tests/fixtures/networkV1.fixtures.js +2 -28
- package/tests/helpers/transactionPayloads.mjs +2 -2
- package/tests/unit/messages/network/NetworkMessageBuilder.test.js +239 -79
- package/tests/unit/messages/network/NetworkMessageDirector.test.js +223 -77
- package/tests/unit/network/LegacyNetworkMessageRouter.test.js +54 -0
- package/tests/unit/network/ProtocolSession.test.js +127 -0
- package/tests/unit/network/networkModule.test.js +4 -1
- package/tests/unit/network/services/ConnectionManager.test.js +450 -0
- package/tests/unit/network/services/MessageOrchestrator.test.js +445 -0
- package/tests/unit/network/services/PendingRequestService.test.js +431 -0
- package/tests/unit/network/services/TransactionCommitService.test.js +246 -0
- package/tests/unit/network/services/TransactionPoolService.test.js +489 -0
- package/tests/unit/network/services/TransactionRateLimiterService.test.js +139 -0
- package/tests/unit/network/services/ValidatorHealthCheckService.test.js +115 -0
- package/tests/unit/network/services/services.test.js +17 -0
- package/tests/unit/network/utils/v1TestUtils.js +153 -0
- package/tests/unit/network/v1/NetworkMessageRouterV1.test.js +151 -0
- package/tests/unit/network/v1/V1BaseOperation.test.js +356 -0
- package/tests/unit/network/v1/V1BroadcastTransactionOperationHandler.test.js +129 -0
- package/tests/unit/network/v1/V1BroadcastTransactionRequest.test.js +53 -0
- package/tests/unit/network/v1/V1BroadcastTransactionResponse.test.js +512 -0
- package/tests/unit/network/v1/V1LivenessRequest.test.js +32 -0
- package/tests/unit/network/v1/V1LivenessResponse.test.js +45 -0
- package/tests/unit/network/v1/V1ResultCode.test.js +84 -0
- package/tests/unit/network/v1/V1ValidationSchema.test.js +13 -0
- package/tests/unit/network/v1/connectionPolicies.test.js +49 -0
- package/tests/unit/network/v1/handlers/V1BaseOperationHandler.test.js +284 -0
- package/tests/unit/network/v1/handlers/V1BroadcastTransactionOperationHandler.test.js +794 -0
- package/tests/unit/network/v1/handlers/V1LivenessOperationHandler.test.js +193 -0
- package/tests/unit/network/v1/v1.handlers.test.js +15 -0
- package/tests/unit/network/v1/v1.test.js +19 -0
- package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionRequest.test.js +119 -0
- package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionResponse.test.js +136 -0
- package/tests/unit/network/v1/v1ValidationSchema/common.test.js +308 -0
- package/tests/unit/network/v1/v1ValidationSchema/livenessRequest.test.js +90 -0
- package/tests/unit/network/v1/v1ValidationSchema/livenessResponse.test.js +133 -0
- package/tests/unit/unit.test.js +2 -2
- package/tests/unit/utils/deepEqualApplyPayload/deepEqualApplyPayload.test.js +102 -0
- package/tests/unit/utils/protobuf/operationHelpers.test.js +2 -4
- package/tests/unit/utils/utils.test.js +1 -0
- package/.github/workflows/acceptance-tests.yml +0 -38
- package/.github/workflows/lint-pr-title.yml +0 -26
- package/.github/workflows/publish.yml +0 -33
- package/.github/workflows/unit-tests.yml +0 -34
- package/proto/network.proto +0 -74
- package/src/core/network/protocols/legacy/handlers/ResponseHandler.js +0 -37
- package/src/utils/protobuf/network.cjs +0 -840
- package/tests/unit/network/ConnectionManager.test.js +0 -191
|
@@ -1,37 +1,40 @@
|
|
|
1
1
|
// PoolService.js
|
|
2
2
|
import { BATCH_SIZE } from '../../../utils/constants.js';
|
|
3
3
|
import Scheduler from '../../../utils/Scheduler.js';
|
|
4
|
+
import Denque from "denque";
|
|
5
|
+
import b4a from "b4a";
|
|
4
6
|
|
|
5
7
|
class TransactionPoolService {
|
|
6
8
|
#state;
|
|
7
9
|
#address;
|
|
8
10
|
#config;
|
|
9
|
-
#
|
|
11
|
+
#txPool = new Denque();
|
|
12
|
+
#transactionCommitService
|
|
10
13
|
#scheduler = null;
|
|
14
|
+
#queuedTxHashes
|
|
11
15
|
|
|
12
16
|
/**
|
|
13
17
|
* @param {State} state
|
|
14
18
|
* @param {string} address
|
|
19
|
+
* @param {TransactionCommitService} transactionCommitService
|
|
15
20
|
* @param {Config} config
|
|
16
21
|
**/
|
|
17
|
-
constructor(state, address, config) {
|
|
22
|
+
constructor(state, address, transactionCommitService, config) {
|
|
18
23
|
this.#state = state;
|
|
19
24
|
this.#address = address;
|
|
25
|
+
this.#transactionCommitService = transactionCommitService;
|
|
26
|
+
this.#queuedTxHashes = new Set(); // to improve lookup performance when checking for duplicate transactions
|
|
20
27
|
this.#config = config;
|
|
21
28
|
}
|
|
22
29
|
|
|
23
|
-
get
|
|
24
|
-
return this.#
|
|
30
|
+
get txPool() {
|
|
31
|
+
return this.#txPool;
|
|
25
32
|
}
|
|
26
33
|
|
|
27
34
|
get state() {
|
|
28
35
|
return this.#state;
|
|
29
36
|
}
|
|
30
37
|
|
|
31
|
-
get address() {
|
|
32
|
-
return this.#address;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
38
|
async start() {
|
|
36
39
|
if (!this.#config.enableWallet) {
|
|
37
40
|
console.info('TransactionPoolService can not start. Wallet is not enabled');
|
|
@@ -49,7 +52,7 @@ class TransactionPoolService {
|
|
|
49
52
|
async #worker(next) {
|
|
50
53
|
try {
|
|
51
54
|
await this.#processTransactions();
|
|
52
|
-
if (this.#
|
|
55
|
+
if (this.#txPool.size() > 0) {
|
|
53
56
|
next(0);
|
|
54
57
|
} else {
|
|
55
58
|
next(this.#config.processIntervalMs);
|
|
@@ -65,35 +68,143 @@ class TransactionPoolService {
|
|
|
65
68
|
|
|
66
69
|
async #processTransactions() {
|
|
67
70
|
const canValidate = await this.#checkValidationPermissions();
|
|
68
|
-
if (canValidate
|
|
69
|
-
|
|
70
|
-
|
|
71
|
+
if (!canValidate || this.#txPool.size() === 0) return;
|
|
72
|
+
|
|
73
|
+
const batchItems = this.#prepareBatch();
|
|
74
|
+
const encodedBatch = batchItems.map(item => item.encodedTx);
|
|
75
|
+
const batchTxHashes = batchItems.map(item => item.txHash);
|
|
76
|
+
try {
|
|
77
|
+
const receipts = await this.#state.appendWithProofOfPublication(encodedBatch, batchTxHashes);
|
|
78
|
+
|
|
79
|
+
const receiptsByHash = new Map();
|
|
80
|
+
for (const receipt of receipts) {
|
|
81
|
+
if (receipt.txHash) receiptsByHash.set(receipt.txHash, receipt);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
for (const item of batchItems) {
|
|
85
|
+
const receipt = receiptsByHash.get(item.txHash);
|
|
86
|
+
|
|
87
|
+
if (!receipt) {
|
|
88
|
+
this.#transactionCommitService.rejectPendingCommit(
|
|
89
|
+
item.txHash,
|
|
90
|
+
new TransactionPoolMissingCommitReceiptError(item.txHash)
|
|
91
|
+
);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (!receipt.proof) {
|
|
96
|
+
this.#transactionCommitService.rejectPendingCommit(
|
|
97
|
+
item.txHash,
|
|
98
|
+
new TransactionPoolProofUnavailableError(
|
|
99
|
+
item.txHash,
|
|
100
|
+
receipt.blockNumber,
|
|
101
|
+
receipt.proofError,
|
|
102
|
+
receipt.timestamp
|
|
103
|
+
)
|
|
104
|
+
);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
this.#transactionCommitService.resolvePendingCommit(item.txHash, receipt);
|
|
109
|
+
}
|
|
110
|
+
} catch (error) {
|
|
111
|
+
for (const item of batchItems) {
|
|
112
|
+
this.#transactionCommitService.rejectPendingCommit(item.txHash, error);
|
|
113
|
+
}
|
|
114
|
+
console.error(
|
|
115
|
+
`TransactionPoolService: failed to process batch (size=${batchItems.length}): ${error?.message ?? 'unknown error'}`
|
|
116
|
+
);
|
|
71
117
|
}
|
|
72
118
|
}
|
|
73
119
|
|
|
74
120
|
async #checkValidationPermissions() {
|
|
75
121
|
const isAdminAllowedToValidate = await this.state.isAdminAllowedToValidate();
|
|
76
|
-
const isNodeAllowedToValidate = await this.state.allowedToValidate(this
|
|
122
|
+
const isNodeAllowedToValidate = await this.state.allowedToValidate(this.#address);
|
|
77
123
|
return isNodeAllowedToValidate || isAdminAllowedToValidate;
|
|
78
124
|
}
|
|
79
125
|
|
|
80
126
|
#prepareBatch() {
|
|
81
|
-
const
|
|
82
|
-
const
|
|
83
|
-
|
|
127
|
+
const batch = [];
|
|
128
|
+
const batchSize = Math.min(this.#txPool.size(), BATCH_SIZE);
|
|
129
|
+
|
|
130
|
+
for (let i = 0; i < batchSize; i++) {
|
|
131
|
+
const tx = this.#txPool.shift();
|
|
132
|
+
this.#queuedTxHashes.delete(tx.txHash);
|
|
133
|
+
batch.push(tx);
|
|
134
|
+
}
|
|
84
135
|
return batch;
|
|
85
136
|
}
|
|
86
137
|
|
|
87
|
-
addTransaction(
|
|
88
|
-
this.
|
|
138
|
+
addTransaction(txHash, encodedTx) {
|
|
139
|
+
this.validateEnqueue();
|
|
140
|
+
if (!txHash || !encodedTx || typeof txHash !== 'string' || !b4a.isBuffer(encodedTx)) {
|
|
141
|
+
throw new TransactionPoolInvalidIncomingDataError()
|
|
142
|
+
}
|
|
143
|
+
if (this.hasTransaction(txHash)) {
|
|
144
|
+
throw new TransactionPoolAlreadyQueuedError(txHash);
|
|
145
|
+
}
|
|
146
|
+
this.#queuedTxHashes.add(txHash);
|
|
147
|
+
const txData = { txHash, encodedTx };
|
|
148
|
+
this.txPool.push(txData);
|
|
89
149
|
}
|
|
90
150
|
|
|
91
151
|
async stopPool(waitForCurrent = true) {
|
|
92
152
|
if (!this.#scheduler) return;
|
|
93
153
|
await this.#scheduler.stop(waitForCurrent);
|
|
94
154
|
this.#scheduler = null;
|
|
155
|
+
this.#queuedTxHashes.clear();
|
|
156
|
+
this.#txPool.clear();
|
|
95
157
|
console.info('TransactionPoolService: closing gracefully...');
|
|
96
158
|
}
|
|
159
|
+
|
|
160
|
+
validateEnqueue() {
|
|
161
|
+
if (this.#txPool.size() >= this.#config.txPoolSize) {
|
|
162
|
+
throw new TransactionPoolFullError(this.#config.txPoolSize);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
hasTransaction(txHash) {
|
|
167
|
+
return this.#queuedTxHashes.has(txHash);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export class TransactionPoolProofUnavailableError extends Error {
|
|
172
|
+
constructor(txHash, blockNumber, reason = 'unknown', timestamp = 0) {
|
|
173
|
+
const timestampValue = timestamp instanceof Date ? timestamp.getTime() : timestamp;
|
|
174
|
+
const safeTimestamp = Number.isSafeInteger(timestampValue) ? timestampValue : 0;
|
|
175
|
+
super(`Proof unavailable for txHash ${txHash} at block ${blockNumber} at ${safeTimestamp}. Reason: ${reason}`);
|
|
176
|
+
this.txHash = txHash;
|
|
177
|
+
this.blockNumber = blockNumber;
|
|
178
|
+
this.timestamp = safeTimestamp;
|
|
179
|
+
this.reason = reason;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export class TransactionPoolMissingCommitReceiptError extends Error {
|
|
184
|
+
constructor(txHash) {
|
|
185
|
+
super(`Missing commit receipt for txHash ${txHash}`);
|
|
186
|
+
this.txHash = txHash;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export class TransactionPoolInvalidIncomingDataError extends Error {
|
|
191
|
+
constructor(message = 'Invalid transaction pool incoming data') {
|
|
192
|
+
super(message);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export class TransactionPoolFullError extends Error {
|
|
197
|
+
constructor(maxSize) {
|
|
198
|
+
super(`Transaction pool is full. Maximum size of ${maxSize} reached.`);
|
|
199
|
+
this.maxSize = maxSize
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export class TransactionPoolAlreadyQueuedError extends Error {
|
|
205
|
+
constructor(txHash) {
|
|
206
|
+
super(`Transaction with hash ${txHash} is already queued in the transaction pool.`);
|
|
207
|
+
}
|
|
97
208
|
}
|
|
98
209
|
|
|
99
210
|
export default TransactionPoolService;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
|
+
import {V1RateLimitedError} from "../protocols/v1/V1ProtocolError.js";
|
|
3
|
+
import {publicKeyToAddress} from "../../../utils/helpers.js";
|
|
2
4
|
|
|
3
5
|
class TransactionRateLimiterService {
|
|
4
6
|
#lastCleanup;
|
|
@@ -14,19 +16,23 @@ class TransactionRateLimiterService {
|
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
/*
|
|
17
|
-
Checks if the peer has exceeded the rate limit.
|
|
19
|
+
Checks if the peer has exceeded the rate limit for the current 1-second window.
|
|
18
20
|
A peer is considered to have exceeded the rate limit if:
|
|
19
|
-
- The
|
|
20
|
-
- The number of transactions in
|
|
21
|
-
|
|
21
|
+
- The request belongs to the same 1-second window as previous requests (tracked per peer)
|
|
22
|
+
- The number of transactions already seen in this window is >= rateLimitMaxTransactionsPerSecond
|
|
23
|
+
|
|
24
|
+
Important:
|
|
25
|
+
- This method assumes the caller increments transactionCount AFTER calling this method.
|
|
26
|
+
(So exactly rateLimitMaxTransactionsPerSecond are allowed; the next one is blocked.)
|
|
22
27
|
*/
|
|
23
|
-
#hasExceededRateLimit(peer) {
|
|
28
|
+
#hasExceededRateLimit(peer, currentTime) {
|
|
24
29
|
const peerData = this.#connectionsStatistics.get(peer);
|
|
25
|
-
const currentSecond = Math.floor((
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
const currentSecond = Math.floor((currentTime - peerData.sessionStartTime) / 1000);
|
|
31
|
+
const lastResetSecond = Math.floor((peerData.lastCounterReset - peerData.sessionStartTime) / 1000);
|
|
32
|
+
|
|
33
|
+
if (currentSecond > lastResetSecond) {
|
|
28
34
|
peerData.transactionCount = 0;
|
|
29
|
-
peerData.lastCounterReset =
|
|
35
|
+
peerData.lastCounterReset = currentTime;
|
|
30
36
|
this.#connectionsStatistics.set(peer, peerData);
|
|
31
37
|
}
|
|
32
38
|
|
|
@@ -34,23 +40,18 @@ class TransactionRateLimiterService {
|
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
/*
|
|
37
|
-
Handles
|
|
38
|
-
If the peer has exceeded the rate limit, it disconnects the peer.
|
|
39
|
-
Otherwise, it updates the connection info with the current timestamp.
|
|
43
|
+
Handles rate limiting for a peer connection (legacy protocol).
|
|
44
|
+
If the peer has exceeded the rate limit, it disconnects the peer and returns true.
|
|
45
|
+
Otherwise, it updates the connection info with the current timestamp and returns false.
|
|
40
46
|
*/
|
|
41
|
-
|
|
47
|
+
legacyHandleRateLimit(connection) {
|
|
42
48
|
const peer = b4a.toString(connection.remotePublicKey, 'hex');
|
|
43
49
|
const currentTime = Date.now();
|
|
44
50
|
|
|
45
51
|
this.#cleanUpOldConnections(currentTime);
|
|
46
52
|
this.#initializePeerConnectionInfoEntry(peer, currentTime);
|
|
47
53
|
|
|
48
|
-
if (this.#
|
|
49
|
-
this.#connectionsStatistics.delete(peer);
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (this.#hasExceededRateLimit(peer)) {
|
|
54
|
+
if (this.#hasExceededRateLimit(peer, currentTime)) {
|
|
54
55
|
console.warn(`Rate limit exceeded for peer ${peer}. Disconnecting...`);
|
|
55
56
|
this.#swarm.leavePeer(connection.remotePublicKey);
|
|
56
57
|
connection.end();
|
|
@@ -61,14 +62,31 @@ class TransactionRateLimiterService {
|
|
|
61
62
|
return false;
|
|
62
63
|
}
|
|
63
64
|
|
|
65
|
+
/*
|
|
66
|
+
Handles rate limiting for a peer connection (v1 protocol).
|
|
67
|
+
If the peer has exceeded the rate limit, it throws RateLimitedError.
|
|
68
|
+
Otherwise, it updates the connection info with the current timestamp.
|
|
69
|
+
*/
|
|
70
|
+
v1HandleRateLimit(connection) {
|
|
71
|
+
const peer = b4a.toString(connection.remotePublicKey, 'hex');
|
|
72
|
+
const currentTime = Date.now();
|
|
73
|
+
|
|
74
|
+
this.#cleanUpOldConnections(currentTime);
|
|
75
|
+
this.#initializePeerConnectionInfoEntry(peer, currentTime);
|
|
76
|
+
|
|
77
|
+
if (this.#hasExceededRateLimit(peer, currentTime)) {
|
|
78
|
+
throw new V1RateLimitedError(`Rate limit exceeded for peer ${publicKeyToAddress(connection.remotePublicKey, this.#config)}`);
|
|
79
|
+
}
|
|
80
|
+
this.#updatePeerConnectionInfo(peer, currentTime);
|
|
81
|
+
}
|
|
82
|
+
|
|
64
83
|
#shouldCleanupConnections(currentTime) {
|
|
65
84
|
return currentTime - this.#lastCleanup >= this.#config.rateLimitCleanupIntervalMs;
|
|
66
85
|
}
|
|
67
86
|
|
|
68
87
|
/**
|
|
69
|
-
Cleans up
|
|
70
|
-
|
|
71
|
-
- If the last cleanup was more than rateLimitCleanupIntervalMs ago
|
|
88
|
+
Cleans up per-peer statistics that have been inactive for more than rateLimitCleanupIntervalMs.
|
|
89
|
+
Runs at most once every rateLimitCleanupIntervalMs.
|
|
72
90
|
*/
|
|
73
91
|
#cleanUpOldConnections(currentTime) {
|
|
74
92
|
if (!this.#shouldCleanupConnections(currentTime)) {
|
|
@@ -76,8 +94,7 @@ class TransactionRateLimiterService {
|
|
|
76
94
|
}
|
|
77
95
|
|
|
78
96
|
for (const [peer, _] of this.#connectionsStatistics.entries()) {
|
|
79
|
-
if (this.#isConnectionExpired(peer)) {
|
|
80
|
-
//console.log(`Connection for peer ${peer} has expired. Removing...`);
|
|
97
|
+
if (this.#isConnectionExpired(peer, currentTime)) {
|
|
81
98
|
this.#connectionsStatistics.delete(peer);
|
|
82
99
|
}
|
|
83
100
|
}
|
|
@@ -87,19 +104,19 @@ class TransactionRateLimiterService {
|
|
|
87
104
|
|
|
88
105
|
/*
|
|
89
106
|
Initializes the connection statistics for a peer.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
sessionStartTime: timestamp,
|
|
93
|
-
lastActivityTime: timestamp,
|
|
94
|
-
|
|
107
|
+
Stored as a HashMap with the following structure:
|
|
108
|
+
peerPublicKeyHex: {
|
|
109
|
+
sessionStartTime: timestamp, // When we first saw this peer (start of local tracking session)
|
|
110
|
+
lastActivityTime: timestamp, // Timestamp of peer's most recent activity
|
|
111
|
+
lastCounterReset: timestamp, // Timestamp when the per-second counter was last reset
|
|
112
|
+
transactionCount: number // Transactions seen in the current 1-second window
|
|
95
113
|
}
|
|
96
|
-
|
|
97
114
|
*/
|
|
98
115
|
#initializePeerConnectionInfoEntry(peer, timestamp) {
|
|
99
116
|
if (!this.#connectionsStatistics.has(peer)) {
|
|
100
117
|
this.#connectionsStatistics.set(peer, {
|
|
101
118
|
sessionStartTime: timestamp,
|
|
102
|
-
lastActivityTime:
|
|
119
|
+
lastActivityTime: timestamp,
|
|
103
120
|
lastCounterReset: timestamp,
|
|
104
121
|
transactionCount: 0
|
|
105
122
|
});
|
|
@@ -118,11 +135,12 @@ class TransactionRateLimiterService {
|
|
|
118
135
|
}
|
|
119
136
|
|
|
120
137
|
/*
|
|
121
|
-
Checks if the
|
|
138
|
+
Checks if the stored statistics for a peer have expired due to inactivity.
|
|
139
|
+
Note: this is NOT a network-level connection timeout; it's only used to evict old Map entries.
|
|
122
140
|
*/
|
|
123
|
-
#isConnectionExpired(peer) {
|
|
141
|
+
#isConnectionExpired(peer, currentTime) {
|
|
124
142
|
const peerData = this.#connectionsStatistics.get(peer);
|
|
125
|
-
return
|
|
143
|
+
return currentTime - peerData.lastActivityTime >= this.#config.rateLimitConnectionTimeoutMs;
|
|
126
144
|
}
|
|
127
145
|
}
|
|
128
146
|
|
|
@@ -0,0 +1,127 @@
|
|
|
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;
|
|
@@ -4,20 +4,12 @@ import { bufferToAddress } from '../../state/utils/address.js';
|
|
|
4
4
|
import { sleep } from '../../../utils/helpers.js';
|
|
5
5
|
import Scheduler from "../../../utils/Scheduler.js";
|
|
6
6
|
import Network from "../Network.js";
|
|
7
|
+
import { Logger } from '../../../utils/logger.js';
|
|
7
8
|
|
|
8
9
|
const DELAY_INTERVAL = 50
|
|
9
10
|
const VALIDATOR_CANDIDATES_PER_CYCLE = 10
|
|
10
11
|
const POLL_INTERVAL = (VALIDATOR_CANDIDATES_PER_CYCLE + 1) * DELAY_INTERVAL // This is to avoid more than one instance of the worker running at the same time
|
|
11
12
|
|
|
12
|
-
// -- Debug Mode --
|
|
13
|
-
// TODO: Implement a better debug system in the future. This is just temporary.
|
|
14
|
-
const DEBUG = false;
|
|
15
|
-
const debugLog = (...args) => {
|
|
16
|
-
if (DEBUG) {
|
|
17
|
-
console.log('DEBUG [ValidatorObserverService] ==> ', ...args);
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
13
|
class ValidatorObserverService {
|
|
22
14
|
#config;
|
|
23
15
|
#state;
|
|
@@ -25,6 +17,7 @@ class ValidatorObserverService {
|
|
|
25
17
|
#scheduler;
|
|
26
18
|
#address;
|
|
27
19
|
#isInterrupted
|
|
20
|
+
#logger;
|
|
28
21
|
|
|
29
22
|
/**
|
|
30
23
|
* @param {Network} network
|
|
@@ -38,12 +31,11 @@ class ValidatorObserverService {
|
|
|
38
31
|
this.#state = state;
|
|
39
32
|
this.#address = address;
|
|
40
33
|
this.#isInterrupted = false;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
34
|
+
this.#logger = new Logger(config);
|
|
35
|
+
this.initTimestamp = Date.now();
|
|
36
|
+
this.reachedMax = false;
|
|
37
|
+
this.end = 0;
|
|
38
|
+
this.begin = 0;
|
|
47
39
|
}
|
|
48
40
|
|
|
49
41
|
get state() {
|
|
@@ -55,11 +47,11 @@ class ValidatorObserverService {
|
|
|
55
47
|
// OS CALLS, ACCUMULATORS, MAYBE THIS IS POSSIBLE TO CHECK I/O QUEUE IF IT COINTAIN IT. FOR NOW WE ARE USING SLEEP.
|
|
56
48
|
async start() {
|
|
57
49
|
if (!this.#shouldRun()) {
|
|
58
|
-
|
|
50
|
+
this.#logger.info('ValidatorObserverService can not start. Disabled by configuration.');
|
|
59
51
|
return;
|
|
60
52
|
}
|
|
61
53
|
if (this.#scheduler && this.#scheduler.isRunning) {
|
|
62
|
-
|
|
54
|
+
this.#logger.info('ValidatorObserverService is already started');
|
|
63
55
|
return;
|
|
64
56
|
}
|
|
65
57
|
|
|
@@ -73,12 +65,12 @@ class ValidatorObserverService {
|
|
|
73
65
|
this.#isInterrupted = true;
|
|
74
66
|
await this.#scheduler.stop(waitForCurrent);
|
|
75
67
|
this.#scheduler = null;
|
|
76
|
-
|
|
68
|
+
this.#logger.info('ValidatorObserverService: closing gracefully...');
|
|
77
69
|
}
|
|
78
70
|
|
|
79
71
|
async #worker(next) {
|
|
80
72
|
if (!this.#network.validatorConnectionManager.maxConnectionsReached()) {
|
|
81
|
-
|
|
73
|
+
this.begin = Date.now();
|
|
82
74
|
const length = await this.#lengthEntry()
|
|
83
75
|
|
|
84
76
|
const promises = [];
|
|
@@ -88,16 +80,16 @@ class ValidatorObserverService {
|
|
|
88
80
|
}
|
|
89
81
|
await Promise.all(promises);
|
|
90
82
|
|
|
91
|
-
|
|
92
|
-
|
|
83
|
+
this.end = Date.now();
|
|
84
|
+
this.#logger.debug(`Worker cycle completed in (ms): ${this.end - this.begin} | Validator Connections: ${this.#network.validatorConnectionManager.connectionCount()} | Pending: ${this.#network.pendingConnectionsCount()}`);
|
|
93
85
|
}
|
|
94
|
-
else
|
|
86
|
+
else {
|
|
95
87
|
if (!this.reachedMax) {
|
|
96
88
|
this.reachedMax = true;
|
|
97
|
-
|
|
89
|
+
this.#logger.debug('Max validator connections reached. Skipping this cycle.');
|
|
98
90
|
const now = Date.now();
|
|
99
91
|
const elapsed = now - this.initTimestamp;
|
|
100
|
-
|
|
92
|
+
this.#logger.debug(`>>> Time elapsed since start (ms): ${elapsed}`);
|
|
101
93
|
}
|
|
102
94
|
}
|
|
103
95
|
next(POLL_INTERVAL);
|
|
@@ -118,10 +110,10 @@ class ValidatorObserverService {
|
|
|
118
110
|
}
|
|
119
111
|
|
|
120
112
|
if (attempts >= maxAttempts) {
|
|
121
|
-
|
|
113
|
+
this.#logger.debug('Max attempts reached without finding a valid validator.');
|
|
122
114
|
}
|
|
123
115
|
else {
|
|
124
|
-
|
|
116
|
+
this.#logger.debug(`Found valid validator to connect after ${attempts} attempts.`);
|
|
125
117
|
}
|
|
126
118
|
|
|
127
119
|
if (!isValidatorValid) return;
|