trac-msb 0.2.9 → 0.2.11
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/CODE_OF_CONDUCT.md +128 -0
- package/README.md +33 -18
- package/docker-compose.yml +1 -0
- package/docs/trac_network_http_api.openapi.yaml +889 -0
- package/msb.mjs +4 -21
- package/package.json +16 -12
- 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/handlers.js +163 -90
- package/rpc/routes/v1.js +3 -1
- package/rpc/rpc_server.js +3 -3
- package/rpc/rpc_services.js +45 -31
- package/rpc/utils/helpers.js +82 -51
- package/scripts/generate-protobufs.js +37 -12
- package/src/config/args.js +46 -0
- package/src/config/config.js +99 -5
- package/src/config/env.js +86 -7
- package/src/core/network/Network.js +79 -46
- 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 +26 -20
- package/src/core/network/protocols/{shared/handlers/base/BaseOperationHandler.js → legacy/handlers/BaseStateOperationHandler.js} +25 -15
- 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} +20 -13
- package/src/core/network/protocols/{shared/handlers/SubnetworkOperationHandler.js → legacy/handlers/LegacySubnetworkOperationHandler.js} +29 -18
- package/src/core/network/protocols/{shared/handlers/TransferOperationHandler.js → legacy/handlers/LegacyTransferOperationHandler.js} +18 -12
- package/src/core/network/protocols/legacy/validators/base/BaseResponse.js +1 -1
- 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 +147 -95
- package/src/core/network/services/MessageOrchestrator.js +152 -28
- 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 +133 -22
- package/src/core/network/services/TransactionRateLimiterService.js +57 -42
- package/src/core/network/services/ValidatorHealthCheckService.js +127 -0
- package/src/core/network/services/ValidatorObserverService.js +23 -32
- package/src/core/state/State.js +72 -22
- package/src/index.js +8 -5
- package/src/messages/network/v1/NetworkMessageBuilder.js +61 -81
- package/src/messages/network/v1/NetworkMessageDirector.js +16 -50
- package/src/messages/state/ApplyStateMessageBuilder.js +1 -1
- package/src/utils/Scheduler.js +0 -8
- package/src/utils/check.js +1 -1
- package/src/utils/constants.js +68 -19
- package/src/utils/deepEqualApplyPayload.js +40 -0
- package/src/utils/fileUtils.js +13 -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/src/utils/type.js +26 -0
- package/tests/acceptance/v1/account/account.test.mjs +8 -2
- package/tests/acceptance/v1/balance/balance.test.mjs +1 -2
- package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +26 -30
- package/tests/acceptance/v1/health/health.test.mjs +33 -0
- package/tests/acceptance/v1/rpc.test.mjs +3 -2
- package/tests/acceptance/v1/tx/tx.test.mjs +50 -17
- package/tests/acceptance/v1/tx-details/tx-details.test.mjs +60 -18
- package/tests/fixtures/check.fixtures.js +33 -32
- package/tests/fixtures/networkV1.fixtures.js +2 -27
- package/tests/fixtures/protobuf.fixtures.js +33 -32
- package/tests/helpers/StateNetworkFactory.js +2 -2
- package/tests/helpers/address.js +6 -0
- package/tests/helpers/autobaseTestHelpers.js +2 -1
- package/tests/helpers/config.js +2 -1
- package/tests/helpers/setupApplyTests.js +6 -10
- package/tests/helpers/transactionPayloads.mjs +2 -2
- package/tests/unit/messages/network/NetworkMessageBuilder.test.js +241 -81
- package/tests/unit/messages/network/NetworkMessageDirector.test.js +225 -81
- 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/fileUtils/readAddressesFromWhitelistFile.test.js +4 -3
- package/tests/unit/utils/fileUtils/readBalanceMigrationFile.test.js +3 -2
- package/tests/unit/utils/migrationUtils/validateAddressFromIncomingFile.test.js +3 -2
- package/tests/unit/utils/protobuf/operationHelpers.test.js +2 -4
- package/tests/unit/utils/type/type.test.js +25 -0
- package/tests/unit/utils/utils.test.js +2 -0
- package/.github/workflows/acceptance-tests.yml +0 -42
- package/.github/workflows/publish.yml +0 -33
- package/.github/workflows/unit-tests.yml +0 -40
- 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,59 +1,57 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
CONNECTION_TIMEOUT_MS,
|
|
5
|
-
MAX_TRANSACTIONS_PER_SECOND
|
|
6
|
-
} from '../../../utils/constants.js';
|
|
2
|
+
import {V1RateLimitedError} from "../protocols/v1/V1ProtocolError.js";
|
|
3
|
+
import {publicKeyToAddress} from "../../../utils/helpers.js";
|
|
7
4
|
|
|
8
5
|
class TransactionRateLimiterService {
|
|
9
6
|
#lastCleanup;
|
|
10
7
|
#connectionsStatistics;
|
|
11
8
|
#swarm;
|
|
9
|
+
#config;
|
|
12
10
|
|
|
13
|
-
constructor(swarm) {
|
|
11
|
+
constructor(swarm, config) {
|
|
14
12
|
this.#lastCleanup = Date.now();
|
|
15
13
|
this.#connectionsStatistics = new Map();
|
|
16
14
|
this.#swarm = swarm
|
|
15
|
+
this.#config = config
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
/*
|
|
20
|
-
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.
|
|
21
20
|
A peer is considered to have exceeded the rate limit if:
|
|
22
|
-
- The
|
|
23
|
-
- The number of transactions in
|
|
24
|
-
|
|
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.)
|
|
25
27
|
*/
|
|
26
|
-
#hasExceededRateLimit(peer) {
|
|
28
|
+
#hasExceededRateLimit(peer, currentTime) {
|
|
27
29
|
const peerData = this.#connectionsStatistics.get(peer);
|
|
28
|
-
const currentSecond = Math.floor((
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
const currentSecond = Math.floor((currentTime - peerData.sessionStartTime) / 1000);
|
|
31
|
+
const lastResetSecond = Math.floor((peerData.lastCounterReset - peerData.sessionStartTime) / 1000);
|
|
32
|
+
|
|
33
|
+
if (currentSecond > lastResetSecond) {
|
|
31
34
|
peerData.transactionCount = 0;
|
|
32
|
-
peerData.lastCounterReset =
|
|
35
|
+
peerData.lastCounterReset = currentTime;
|
|
33
36
|
this.#connectionsStatistics.set(peer, peerData);
|
|
34
37
|
}
|
|
35
38
|
|
|
36
|
-
return peerData.transactionCount >=
|
|
39
|
+
return peerData.transactionCount >= this.#config.rateLimitMaxTransactionsPerSecond;
|
|
37
40
|
}
|
|
38
41
|
|
|
39
42
|
/*
|
|
40
|
-
Handles
|
|
41
|
-
If the peer has exceeded the rate limit, it disconnects the peer.
|
|
42
|
-
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.
|
|
43
46
|
*/
|
|
44
|
-
|
|
47
|
+
legacyHandleRateLimit(connection) {
|
|
45
48
|
const peer = b4a.toString(connection.remotePublicKey, 'hex');
|
|
46
49
|
const currentTime = Date.now();
|
|
47
50
|
|
|
48
51
|
this.#cleanUpOldConnections(currentTime);
|
|
49
52
|
this.#initializePeerConnectionInfoEntry(peer, currentTime);
|
|
50
53
|
|
|
51
|
-
if (this.#
|
|
52
|
-
this.#connectionsStatistics.delete(peer);
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (this.#hasExceededRateLimit(peer)) {
|
|
54
|
+
if (this.#hasExceededRateLimit(peer, currentTime)) {
|
|
57
55
|
console.warn(`Rate limit exceeded for peer ${peer}. Disconnecting...`);
|
|
58
56
|
this.#swarm.leavePeer(connection.remotePublicKey);
|
|
59
57
|
connection.end();
|
|
@@ -64,14 +62,31 @@ class TransactionRateLimiterService {
|
|
|
64
62
|
return false;
|
|
65
63
|
}
|
|
66
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
|
+
|
|
67
83
|
#shouldCleanupConnections(currentTime) {
|
|
68
|
-
return currentTime - this.#lastCleanup >=
|
|
84
|
+
return currentTime - this.#lastCleanup >= this.#config.rateLimitCleanupIntervalMs;
|
|
69
85
|
}
|
|
70
86
|
|
|
71
87
|
/**
|
|
72
|
-
Cleans up
|
|
73
|
-
|
|
74
|
-
- If the last cleanup was more than CLEANUP_INTERVAL_MS ago
|
|
88
|
+
Cleans up per-peer statistics that have been inactive for more than rateLimitCleanupIntervalMs.
|
|
89
|
+
Runs at most once every rateLimitCleanupIntervalMs.
|
|
75
90
|
*/
|
|
76
91
|
#cleanUpOldConnections(currentTime) {
|
|
77
92
|
if (!this.#shouldCleanupConnections(currentTime)) {
|
|
@@ -79,8 +94,7 @@ class TransactionRateLimiterService {
|
|
|
79
94
|
}
|
|
80
95
|
|
|
81
96
|
for (const [peer, _] of this.#connectionsStatistics.entries()) {
|
|
82
|
-
if (this.#isConnectionExpired(peer)) {
|
|
83
|
-
//console.log(`Connection for peer ${peer} has expired. Removing...`);
|
|
97
|
+
if (this.#isConnectionExpired(peer, currentTime)) {
|
|
84
98
|
this.#connectionsStatistics.delete(peer);
|
|
85
99
|
}
|
|
86
100
|
}
|
|
@@ -90,19 +104,19 @@ class TransactionRateLimiterService {
|
|
|
90
104
|
|
|
91
105
|
/*
|
|
92
106
|
Initializes the connection statistics for a peer.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
sessionStartTime: timestamp,
|
|
96
|
-
lastActivityTime: timestamp,
|
|
97
|
-
|
|
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
|
|
98
113
|
}
|
|
99
|
-
|
|
100
114
|
*/
|
|
101
115
|
#initializePeerConnectionInfoEntry(peer, timestamp) {
|
|
102
116
|
if (!this.#connectionsStatistics.has(peer)) {
|
|
103
117
|
this.#connectionsStatistics.set(peer, {
|
|
104
118
|
sessionStartTime: timestamp,
|
|
105
|
-
lastActivityTime:
|
|
119
|
+
lastActivityTime: timestamp,
|
|
106
120
|
lastCounterReset: timestamp,
|
|
107
121
|
transactionCount: 0
|
|
108
122
|
});
|
|
@@ -121,11 +135,12 @@ class TransactionRateLimiterService {
|
|
|
121
135
|
}
|
|
122
136
|
|
|
123
137
|
/*
|
|
124
|
-
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.
|
|
125
140
|
*/
|
|
126
|
-
#isConnectionExpired(peer) {
|
|
141
|
+
#isConnectionExpired(peer, currentTime) {
|
|
127
142
|
const peerData = this.#connectionsStatistics.get(peer);
|
|
128
|
-
return
|
|
143
|
+
return currentTime - peerData.lastActivityTime >= this.#config.rateLimitConnectionTimeoutMs;
|
|
129
144
|
}
|
|
130
145
|
}
|
|
131
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;
|
|
@@ -1,24 +1,15 @@
|
|
|
1
1
|
import PeerWallet from "trac-wallet";
|
|
2
2
|
import b4a from "b4a";
|
|
3
|
-
import { MAX_WRITERS_FOR_ADMIN_INDEXER_CONNECTION } from '../../../utils/constants.js';
|
|
4
3
|
import { bufferToAddress } from '../../state/utils/address.js';
|
|
5
4
|
import { sleep } from '../../../utils/helpers.js';
|
|
6
5
|
import Scheduler from "../../../utils/Scheduler.js";
|
|
7
6
|
import Network from "../Network.js";
|
|
7
|
+
import { Logger } from '../../../utils/logger.js';
|
|
8
8
|
|
|
9
9
|
const DELAY_INTERVAL = 50
|
|
10
10
|
const VALIDATOR_CANDIDATES_PER_CYCLE = 10
|
|
11
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
|
|
12
12
|
|
|
13
|
-
// -- Debug Mode --
|
|
14
|
-
// TODO: Implement a better debug system in the future. This is just temporary.
|
|
15
|
-
const DEBUG = false;
|
|
16
|
-
const debugLog = (...args) => {
|
|
17
|
-
if (DEBUG) {
|
|
18
|
-
console.log('DEBUG [ValidatorObserverService] ==> ', ...args);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
13
|
class ValidatorObserverService {
|
|
23
14
|
#config;
|
|
24
15
|
#state;
|
|
@@ -26,12 +17,13 @@ class ValidatorObserverService {
|
|
|
26
17
|
#scheduler;
|
|
27
18
|
#address;
|
|
28
19
|
#isInterrupted
|
|
20
|
+
#logger;
|
|
29
21
|
|
|
30
22
|
/**
|
|
31
23
|
* @param {Network} network
|
|
32
24
|
* @param {State} state
|
|
33
25
|
* @param {string} address
|
|
34
|
-
* @param {
|
|
26
|
+
* @param {Config} config
|
|
35
27
|
**/
|
|
36
28
|
constructor(network, state, address, config) {
|
|
37
29
|
this.#config = config
|
|
@@ -39,12 +31,11 @@ class ValidatorObserverService {
|
|
|
39
31
|
this.#state = state;
|
|
40
32
|
this.#address = address;
|
|
41
33
|
this.#isInterrupted = false;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
34
|
+
this.#logger = new Logger(config);
|
|
35
|
+
this.initTimestamp = Date.now();
|
|
36
|
+
this.reachedMax = false;
|
|
37
|
+
this.end = 0;
|
|
38
|
+
this.begin = 0;
|
|
48
39
|
}
|
|
49
40
|
|
|
50
41
|
get state() {
|
|
@@ -56,11 +47,11 @@ class ValidatorObserverService {
|
|
|
56
47
|
// OS CALLS, ACCUMULATORS, MAYBE THIS IS POSSIBLE TO CHECK I/O QUEUE IF IT COINTAIN IT. FOR NOW WE ARE USING SLEEP.
|
|
57
48
|
async start() {
|
|
58
49
|
if (!this.#shouldRun()) {
|
|
59
|
-
|
|
50
|
+
this.#logger.info('ValidatorObserverService can not start. Disabled by configuration.');
|
|
60
51
|
return;
|
|
61
52
|
}
|
|
62
53
|
if (this.#scheduler && this.#scheduler.isRunning) {
|
|
63
|
-
|
|
54
|
+
this.#logger.info('ValidatorObserverService is already started');
|
|
64
55
|
return;
|
|
65
56
|
}
|
|
66
57
|
|
|
@@ -74,12 +65,12 @@ class ValidatorObserverService {
|
|
|
74
65
|
this.#isInterrupted = true;
|
|
75
66
|
await this.#scheduler.stop(waitForCurrent);
|
|
76
67
|
this.#scheduler = null;
|
|
77
|
-
|
|
68
|
+
this.#logger.info('ValidatorObserverService: closing gracefully...');
|
|
78
69
|
}
|
|
79
70
|
|
|
80
71
|
async #worker(next) {
|
|
81
72
|
if (!this.#network.validatorConnectionManager.maxConnectionsReached()) {
|
|
82
|
-
|
|
73
|
+
this.begin = Date.now();
|
|
83
74
|
const length = await this.#lengthEntry()
|
|
84
75
|
|
|
85
76
|
const promises = [];
|
|
@@ -89,16 +80,16 @@ class ValidatorObserverService {
|
|
|
89
80
|
}
|
|
90
81
|
await Promise.all(promises);
|
|
91
82
|
|
|
92
|
-
|
|
93
|
-
|
|
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()}`);
|
|
94
85
|
}
|
|
95
|
-
else
|
|
86
|
+
else {
|
|
96
87
|
if (!this.reachedMax) {
|
|
97
88
|
this.reachedMax = true;
|
|
98
|
-
|
|
89
|
+
this.#logger.debug('Max validator connections reached. Skipping this cycle.');
|
|
99
90
|
const now = Date.now();
|
|
100
91
|
const elapsed = now - this.initTimestamp;
|
|
101
|
-
|
|
92
|
+
this.#logger.debug(`>>> Time elapsed since start (ms): ${elapsed}`);
|
|
102
93
|
}
|
|
103
94
|
}
|
|
104
95
|
next(POLL_INTERVAL);
|
|
@@ -119,10 +110,10 @@ class ValidatorObserverService {
|
|
|
119
110
|
}
|
|
120
111
|
|
|
121
112
|
if (attempts >= maxAttempts) {
|
|
122
|
-
|
|
113
|
+
this.#logger.debug('Max attempts reached without finding a valid validator.');
|
|
123
114
|
}
|
|
124
115
|
else {
|
|
125
|
-
|
|
116
|
+
this.#logger.debug(`Found valid validator to connect after ${attempts} attempts.`);
|
|
126
117
|
}
|
|
127
118
|
|
|
128
119
|
if (!isValidatorValid) return;
|
|
@@ -132,7 +123,7 @@ class ValidatorObserverService {
|
|
|
132
123
|
const validatorPubKeyHex = validatorPubKeyBuffer.toString('hex');
|
|
133
124
|
const adminEntry = await this.state.getAdminEntry();
|
|
134
125
|
|
|
135
|
-
if (validatorAddress !== adminEntry?.address || validatorListLength <
|
|
126
|
+
if (validatorAddress !== adminEntry?.address || validatorListLength < this.#config.maxWritersForAdminIndexerConnection) {
|
|
136
127
|
this.#network.tryConnect(validatorPubKeyHex, 'validator');
|
|
137
128
|
}
|
|
138
129
|
};
|
|
@@ -151,7 +142,7 @@ class ValidatorObserverService {
|
|
|
151
142
|
return false;
|
|
152
143
|
}
|
|
153
144
|
|
|
154
|
-
if (validatorAddress === adminEntry?.address && validatorListLength >=
|
|
145
|
+
if (validatorAddress === adminEntry?.address && validatorListLength >= this.#config.maxWritersForAdminIndexerConnection) {
|
|
155
146
|
if (this.#network.validatorConnectionManager.exists(validatorPubKeyBuffer)) {
|
|
156
147
|
this.#network.validatorConnectionManager.remove(validatorPubKeyBuffer)
|
|
157
148
|
}
|
|
@@ -161,12 +152,12 @@ class ValidatorObserverService {
|
|
|
161
152
|
// - Cannot connect if already connected to a validator
|
|
162
153
|
// - Validator must exist and be a writer
|
|
163
154
|
// - Cannot connect to indexers, except for admin-indexer
|
|
164
|
-
// - Admin-indexer connection is allowed only when writers length
|
|
155
|
+
// - Admin-indexer connection is allowed only when writers length is below maxWritersForAdminIndexerConnection
|
|
165
156
|
if (this.#network.validatorConnectionManager.connected(validatorPubKeyBuffer) ||
|
|
166
157
|
this.#network.validatorConnectionManager.maxConnectionsReached() ||
|
|
167
158
|
validatorEntry === null ||
|
|
168
159
|
!validatorEntry.isWriter ||
|
|
169
|
-
(validatorEntry.isIndexer && (validatorAddress !== adminEntry?.address || validatorListLength >=
|
|
160
|
+
(validatorEntry.isIndexer && (validatorAddress !== adminEntry?.address || validatorListLength >= this.#config.maxWritersForAdminIndexerConnection))
|
|
170
161
|
) {
|
|
171
162
|
return false;
|
|
172
163
|
}
|
package/src/core/state/State.js
CHANGED
|
@@ -12,7 +12,6 @@ import {
|
|
|
12
12
|
HYPERBEE_VALUE_ENCODING,
|
|
13
13
|
BATCH_SIZE,
|
|
14
14
|
ADMIN_INITIAL_STAKED_BALANCE,
|
|
15
|
-
MAX_WRITERS_FOR_ADMIN_INDEXER_CONNECTION,
|
|
16
15
|
TRAC_NAMESPACE,
|
|
17
16
|
CustomEventType
|
|
18
17
|
} from '../../utils/constants.js';
|
|
@@ -41,13 +40,15 @@ import { safeWriteUInt32BE } from '../../utils/buffer.js';
|
|
|
41
40
|
import deploymentEntryUtils from './utils/deploymentEntry.js';
|
|
42
41
|
import { deepCopyBuffer } from '../../utils/buffer.js';
|
|
43
42
|
import { Status } from './utils/transaction.js';
|
|
44
|
-
import
|
|
43
|
+
import remote from 'hypercore/lib/fully-remote-proof.js'
|
|
44
|
+
import PQueue from 'p-queue';
|
|
45
45
|
|
|
46
46
|
const OVERSIZED_BATCH_PENALTY_MULTIPLIER = BATCH_SIZE;
|
|
47
47
|
|
|
48
48
|
// TODO: #addWriter, #removeWriter, #transfer, #transferFeeTxOperation need to be refactored to get in arguments actor's nodeEntries in buffer format.
|
|
49
49
|
|
|
50
50
|
class State extends ReadyResource {
|
|
51
|
+
#writeQueue = new PQueue({ concurrency: 1 });
|
|
51
52
|
#base;
|
|
52
53
|
#bee;
|
|
53
54
|
#store;
|
|
@@ -58,7 +59,7 @@ class State extends ReadyResource {
|
|
|
58
59
|
/**
|
|
59
60
|
* @param {Corestore} store
|
|
60
61
|
* @param {PeerWallet} wallet
|
|
61
|
-
* @param {
|
|
62
|
+
* @param {Config} config
|
|
62
63
|
**/
|
|
63
64
|
constructor(store, wallet, config) {
|
|
64
65
|
super();
|
|
@@ -178,7 +179,7 @@ class State extends ReadyResource {
|
|
|
178
179
|
async isAdminAllowedToValidate() {
|
|
179
180
|
const isAdmin = this.writingKey.toString('hex') === this.#config.bootstrap.toString('hex');
|
|
180
181
|
const isIndexer = this.isIndexer();
|
|
181
|
-
const lengthCondition = await this.getWriterLength() <=
|
|
182
|
+
const lengthCondition = await this.getWriterLength() <= this.#config.maxWritersForAdminIndexerConnection;
|
|
182
183
|
return !!(isAdmin && isIndexer && lengthCondition);
|
|
183
184
|
}
|
|
184
185
|
|
|
@@ -188,18 +189,6 @@ class State extends ReadyResource {
|
|
|
188
189
|
return !!nodeEntry.isWhitelisted;
|
|
189
190
|
}
|
|
190
191
|
|
|
191
|
-
async isAddressWriter(address) {
|
|
192
|
-
const nodeEntry = await this.getNodeEntry(address);
|
|
193
|
-
if (nodeEntry === null) return false;
|
|
194
|
-
return !!nodeEntry.isWriter;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async isAddressIndexer(address) {
|
|
198
|
-
const nodeEntry = await this.getNodeEntry(address);
|
|
199
|
-
if (nodeEntry === null) return false;
|
|
200
|
-
return !!nodeEntry.isIndexer;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
192
|
async getIndexersEntry() {
|
|
204
193
|
return Object.values(this.#base.system.indexers);
|
|
205
194
|
}
|
|
@@ -244,7 +233,67 @@ class State extends ReadyResource {
|
|
|
244
233
|
}
|
|
245
234
|
|
|
246
235
|
async append(payload) {
|
|
247
|
-
|
|
236
|
+
return this.#writeQueue.add(() => this.#base.append(payload));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
async appendWithProofOfPublication(batch, batchTxHashes) {
|
|
240
|
+
return this.#writeQueue.add(async () => {
|
|
241
|
+
|
|
242
|
+
const core = this.#base.local;
|
|
243
|
+
const end = await this.#base.append(batch);
|
|
244
|
+
const start = end - batch.length;
|
|
245
|
+
const timestamp = new Date();
|
|
246
|
+
const snapshot = core.snapshot(); // consistent view while generating proofs.
|
|
247
|
+
await snapshot.ready();
|
|
248
|
+
// TODO: check state if specific tx has been appened THEN generate a proof.
|
|
249
|
+
try {
|
|
250
|
+
const receipts = [];
|
|
251
|
+
let failedProofs = 0;
|
|
252
|
+
for (let i = 0; i < batch.length; i++) {
|
|
253
|
+
const blockNumber = start + i;
|
|
254
|
+
const completeTx = batch[i];
|
|
255
|
+
const txHash = batchTxHashes[i];
|
|
256
|
+
|
|
257
|
+
let proof = null;
|
|
258
|
+
let proofError = null;
|
|
259
|
+
|
|
260
|
+
// wait:false makes get fail fast (null) instead of waiting for missing data/replication.
|
|
261
|
+
const rawBlock = await snapshot.get(blockNumber, { raw: true, wait: false });
|
|
262
|
+
if (!rawBlock) {
|
|
263
|
+
proofError = `Missing raw block after append (block=${blockNumber}, start=${start}, end=${end})`;
|
|
264
|
+
failedProofs++;
|
|
265
|
+
} else {
|
|
266
|
+
try {
|
|
267
|
+
proof = await remote.proof(snapshot, { index: blockNumber, block: rawBlock });
|
|
268
|
+
} catch (error) {
|
|
269
|
+
proofError = `Proof generation failed (block=${blockNumber}, start=${start}, end=${end}): ${error?.message ?? 'unknown error'}`;
|
|
270
|
+
failedProofs++;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
receipts.push({
|
|
274
|
+
txHash,
|
|
275
|
+
completeTx,
|
|
276
|
+
proof,
|
|
277
|
+
proofError,
|
|
278
|
+
timestamp,
|
|
279
|
+
blockNumber
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
if (failedProofs > 0) {
|
|
283
|
+
console.error(`appendWithProof completed with ${failedProofs} proof failures (batch=${batch.length})`);
|
|
284
|
+
}
|
|
285
|
+
return receipts;
|
|
286
|
+
} finally {
|
|
287
|
+
await snapshot.close();
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async verifyProofOfPublication(proof) {
|
|
293
|
+
// Valid concern. We currently rely on Hypercore’s internal fully-remote-proof helper, which requires low-level storage access
|
|
294
|
+
const out = await remote.verify(this.#store.storage, proof);
|
|
295
|
+
if (!out) throw new Error('Proof of publication verification failed');
|
|
296
|
+
return out;
|
|
248
297
|
}
|
|
249
298
|
|
|
250
299
|
async getIndexerSequenceState() {
|
|
@@ -265,6 +314,7 @@ class State extends ReadyResource {
|
|
|
265
314
|
return b4a.equals(initialization, safeWriteUInt32BE(0, 0))
|
|
266
315
|
}
|
|
267
316
|
}
|
|
317
|
+
|
|
268
318
|
async getTransactionConfirmedLength(hash) {
|
|
269
319
|
if (!isHexString(hash) || hash.length !== 64) {
|
|
270
320
|
throw new Error("Invalid hash format");
|
|
@@ -1794,9 +1844,9 @@ class State extends ReadyResource {
|
|
|
1794
1844
|
};
|
|
1795
1845
|
|
|
1796
1846
|
/**
|
|
1797
|
-
* Ensure that:
|
|
1798
|
-
* 1) writer key exists in registry (we can not unregister something that was not registered),
|
|
1799
|
-
* 2) matches the one in node entry ,
|
|
1847
|
+
* Ensure that:
|
|
1848
|
+
* 1) writer key exists in registry (we can not unregister something that was not registered),
|
|
1849
|
+
* 2) matches the one in node entry ,
|
|
1800
1850
|
* 3) belongs to the requester - this prevents unauthorized key removal
|
|
1801
1851
|
*/
|
|
1802
1852
|
const writerKeyHasBeenRegistered = await this.#getRegisteredWriterKeyApply(batch, op.rao.iw.toString('hex'))
|
|
@@ -2931,7 +2981,7 @@ class State extends ReadyResource {
|
|
|
2931
2981
|
batch,
|
|
2932
2982
|
node
|
|
2933
2983
|
);
|
|
2934
|
-
|
|
2984
|
+
|
|
2935
2985
|
// TODO: cover next 4 guards below with tests
|
|
2936
2986
|
if (transferFeeTxOperationResult === null) {
|
|
2937
2987
|
this.#safeLogApply(OperationType.TX, "Fee transfer operation failed completely.", node.from.key);
|
|
@@ -3389,7 +3439,7 @@ class State extends ReadyResource {
|
|
|
3389
3439
|
|
|
3390
3440
|
/**
|
|
3391
3441
|
* Retrieves the address assigned to a given writing key from the registry.
|
|
3392
|
-
*
|
|
3442
|
+
*
|
|
3393
3443
|
* @param {Object} batch - The current Hyperbee batch instance used for reading state.
|
|
3394
3444
|
* @param {string} writingKey - The writing key in hex string format.
|
|
3395
3445
|
* @returns {Buffer|null} The address buffer assigned to the writing key, or null if not registered.
|