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,89 +1,154 @@
|
|
|
1
1
|
import b4a from 'b4a'
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const debugLog = (...args) => {
|
|
9
|
-
if (DEBUG) {
|
|
10
|
-
console.log('DEBUG [ConnectionManager] ==> ', ...args);
|
|
11
|
-
}
|
|
12
|
-
};
|
|
2
|
+
import {EventType, ResultCode} from '../../../utils/constants.js';
|
|
3
|
+
import {publicKeyToAddress} from "../../../utils/helpers.js";
|
|
4
|
+
import {Logger} from "../../../utils/logger.js";
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {import('hyperswarm').Connection} Connection
|
|
7
|
+
*/
|
|
13
8
|
|
|
14
9
|
class ConnectionManager {
|
|
15
10
|
#validators
|
|
16
11
|
#maxValidators
|
|
17
12
|
#config
|
|
18
|
-
|
|
13
|
+
#healthCheckService
|
|
14
|
+
#boundedHealthCheckHandler
|
|
15
|
+
#logger
|
|
19
16
|
// Note: #validators is using publicKey (Buffer) as key
|
|
20
17
|
// As Buffers are objects, we will rely on internal conversions done by JS to compare them.
|
|
21
18
|
// It would be better to handle these conversions manually by using hex strings as keys to avoid issues
|
|
22
19
|
/**
|
|
23
|
-
* @param {
|
|
20
|
+
* @param {Config} config
|
|
24
21
|
**/
|
|
25
|
-
constructor(config)
|
|
22
|
+
constructor(config) {
|
|
26
23
|
this.#validators = new Map();
|
|
27
24
|
this.#config = config
|
|
28
25
|
this.#maxValidators = config.maxValidators
|
|
26
|
+
this.#boundedHealthCheckHandler = this.#healthCheckHandler.bind(this);
|
|
27
|
+
this.#logger = new Logger(config)
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* if the specified validator is unavailable.
|
|
35
|
-
* @param {Object} message - The message to send to the validator
|
|
36
|
-
* @returns {String} - The public key of the validator used
|
|
31
|
+
* Subscribes to periodic validator health checks.
|
|
32
|
+
* @param {ReadyResource} healthCheckService
|
|
37
33
|
*/
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
// TODO: We should consider moving this to ValidatorObserver instead.
|
|
35
|
+
// Keep here only if we forsee having health checks for non-validator connections in the future.
|
|
36
|
+
// For now, it seems that it would be better to keep this logic here.
|
|
37
|
+
subscribeToHealthChecks(healthCheckService) {
|
|
38
|
+
this.#logger.debug('subscribeToHealthChecks: subscribing to health check events');
|
|
39
|
+
if (!healthCheckService || typeof healthCheckService.on !== 'function' || typeof healthCheckService.off !== 'function') {
|
|
40
|
+
throw new Error('ConnectionManager: health check service must implement on/off');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (this.#healthCheckService && this.#boundedHealthCheckHandler) {
|
|
44
|
+
this.#logger.debug('subscribeToHealthChecks: removing previous health check handler');
|
|
45
|
+
// Unsubscribe from previous health check service if already subscribed
|
|
46
|
+
// TODO: Maybe we should not allow switching to a new health check service
|
|
47
|
+
this.#healthCheckService.off(EventType.VALIDATOR_HEALTH_CHECK, this.#boundedHealthCheckHandler);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.#healthCheckService = healthCheckService; // TODO: Maybe this should be handled in the constructor directly?
|
|
51
|
+
// TODO: declare this method outside this function to avoid redeclaring it every time we subscribe to health checks. We can just bind it to 'this' in the constructor.
|
|
52
|
+
|
|
53
|
+
this.#healthCheckService.on(EventType.VALIDATOR_HEALTH_CHECK, this.#boundedHealthCheckHandler);
|
|
54
|
+
this.#logger.debug('subscribeToHealthChecks: subscribed to health check events');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async #healthCheckHandler(publicKey, requestId) {
|
|
58
|
+
if (typeof publicKey !== 'string' || typeof requestId !== 'string') {
|
|
59
|
+
// We can't throw here because this is an event handler, but we should at least log the error and return early to avoid further issues.
|
|
60
|
+
this.#logger.error(`healthCheck: malformed event payload. Typeof publicKey = ${typeof publicKey}. Typeof requestId = ${typeof requestId}`);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
40
63
|
|
|
41
|
-
|
|
42
|
-
|
|
64
|
+
const targetAddress = publicKeyToAddress(publicKey, this.#config);
|
|
65
|
+
|
|
66
|
+
if (!this.exists(publicKey) || !this.connected(publicKey)) {
|
|
67
|
+
this.#logger.debug(`healthCheck: validator not connected, stopping checks. Address = ${targetAddress}; Request ID = ${requestId}`);
|
|
68
|
+
this.#stopHealthCheck(publicKey);
|
|
69
|
+
return;
|
|
43
70
|
}
|
|
44
71
|
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
72
|
+
const connection = this.getConnection(publicKey);
|
|
73
|
+
if (!connection || !connection.protocolSession || typeof connection.protocolSession.sendHealthCheck !== 'function') {
|
|
74
|
+
this.#logger.debug(`healthCheck: missing protocol session, removing validator. Address = ${targetAddress}; Request ID = ${requestId}`);
|
|
75
|
+
this.#stopHealthCheck(publicKey);
|
|
76
|
+
this.remove(publicKey);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
48
79
|
|
|
80
|
+
let success = false;
|
|
49
81
|
try {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
82
|
+
this.#logger.debug(`healthCheck: sending liveness request. Address = ${targetAddress}; Request ID = ${requestId}`);
|
|
83
|
+
|
|
84
|
+
const resultCode = await connection.protocolSession.sendHealthCheck();
|
|
85
|
+
success = resultCode === ResultCode.OK;
|
|
86
|
+
if (!success) {
|
|
87
|
+
this.#logger.debug(`healthCheck: non-OK result code. Address = ${targetAddress}; Request ID = ${requestId}`);
|
|
88
|
+
}
|
|
89
|
+
} catch {
|
|
90
|
+
success = false;
|
|
54
91
|
}
|
|
55
92
|
|
|
56
|
-
|
|
57
|
-
|
|
93
|
+
if (!success) {
|
|
94
|
+
this.#logger.debug(`healthCheck: liveness request failed, removing validator. Address = ${targetAddress}; Request ID = ${requestId}`);
|
|
95
|
+
this.remove(publicKey);
|
|
96
|
+
this.#stopHealthCheck(publicKey);
|
|
97
|
+
} else {
|
|
98
|
+
this.#logger.debug(`healthCheck: success. Address = ${targetAddress}; Request ID = ${requestId}`);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
58
101
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
* @param {Object} message - The message to send to the validator
|
|
62
|
-
* @param {String | Buffer} publicKey - A validator public key hex string to be fetched from the pool.
|
|
63
|
-
* @returns {Boolean} True if the message was sent, false otherwise.
|
|
64
|
-
*/
|
|
65
|
-
sendSingleMessage(message, publicKey) {
|
|
66
|
-
let publicKeyHex = this.#toHexString(publicKey);
|
|
67
|
-
if (!this.exists(publicKeyHex) || !this.connected(publicKeyHex)) return false; // Fail silently
|
|
102
|
+
#stopHealthCheck(publicKeyHex) {
|
|
103
|
+
const targetAddress = publicKeyToAddress(publicKeyHex, this.#config);
|
|
68
104
|
|
|
69
|
-
|
|
70
|
-
|
|
105
|
+
if (!this.#healthCheckService) {
|
|
106
|
+
this.#logger.debug(`stopHealthCheck: no health check service, cannot stop checks for ${targetAddress}`);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
71
109
|
try {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
110
|
+
if (this.#healthCheckService.has(publicKeyHex)) {
|
|
111
|
+
this.#logger.debug(`stopHealthCheck: stopping scheduled checks for ${targetAddress}`);
|
|
112
|
+
this.#healthCheckService.stop(publicKeyHex);
|
|
113
|
+
}
|
|
114
|
+
} catch (error) {
|
|
115
|
+
this.#logger.debug(`stopHealthCheck: failed to stop health check for validator ${targetAddress}. Error: ${error.message}`);
|
|
75
116
|
}
|
|
76
|
-
return true; // TODO: Implement better success/failure reporting
|
|
77
117
|
}
|
|
78
118
|
|
|
79
119
|
/**
|
|
80
|
-
*
|
|
81
|
-
* @param {String | Buffer} publicKey - The public key hex string of the validator
|
|
120
|
+
* Retrieves the Hyperswarm connection object for a given validator public key.
|
|
121
|
+
* @param {String | Buffer} publicKey - The public key (Buffer or hex string) of the validator.
|
|
122
|
+
* @returns {Connection|undefined} - The connection object if found, otherwise undefined.
|
|
123
|
+
*/
|
|
124
|
+
getConnection(publicKey) {
|
|
125
|
+
const publicKeyHex = this.#toHexString(publicKey);
|
|
126
|
+
const entry = this.#validators.get(publicKeyHex);
|
|
127
|
+
return entry ? entry.connection : undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Sends a message through a specific validator without increasing sent messages count.
|
|
132
|
+
* @param {Object} message - The message to send to the validator.
|
|
133
|
+
* @param {String | Buffer} publicKey - A validator public key hex string to be fetched from the pool.
|
|
134
|
+
* @returns {Promise<*>} A promise returned by `validator.connection.protocolSession.send(message)`.
|
|
135
|
+
* @throws {ConnectionManagerError} If the validator is not connected.
|
|
136
|
+
* @throws {ConnectionManagerError} If the validator has no valid connection or protocol session.
|
|
82
137
|
*/
|
|
83
|
-
|
|
84
|
-
whiteList(publicKey) {
|
|
138
|
+
async sendSingleMessage(message, publicKey) {
|
|
85
139
|
let publicKeyHex = this.#toHexString(publicKey);
|
|
86
|
-
this
|
|
140
|
+
if (!this.connected(publicKeyHex)) {
|
|
141
|
+
throw new ConnectionManagerError(
|
|
142
|
+
`Cannot send message: validator ${publicKeyToAddress(publicKey, this.#config)} is not connected.`
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
const validator = this.#validators.get(publicKeyHex);
|
|
146
|
+
if (!validator || !validator.connection || !validator.connection.protocolSession) {
|
|
147
|
+
throw new ConnectionManagerError(
|
|
148
|
+
`Cannot send message: no valid connection found for validator ${publicKeyToAddress(publicKey, this.#config)}.`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
return validator.connection.protocolSession.send(message)
|
|
87
152
|
}
|
|
88
153
|
|
|
89
154
|
/**
|
|
@@ -95,20 +160,20 @@ class ConnectionManager {
|
|
|
95
160
|
addValidator(publicKey, connection) {
|
|
96
161
|
let publicKeyHex = this.#toHexString(publicKey);
|
|
97
162
|
if (this.maxConnectionsReached()) {
|
|
98
|
-
|
|
163
|
+
this.#logger.debug('addValidator: max connections reached.');
|
|
99
164
|
return false;
|
|
100
165
|
}
|
|
101
|
-
|
|
166
|
+
this.#logger.debug(`addValidator: adding validator ${publicKeyToAddress(publicKeyHex, this.#config)}`);
|
|
102
167
|
if (!this.exists(publicKeyHex)) {
|
|
103
|
-
|
|
168
|
+
this.#logger.debug(`addValidator: appending validator ${publicKeyToAddress(publicKeyHex, this.#config)}`);
|
|
104
169
|
this.#append(publicKeyHex, connection);
|
|
105
170
|
return true;
|
|
106
171
|
} else if (!this.connected(publicKeyHex)) {
|
|
107
|
-
|
|
172
|
+
this.#logger.debug(`addValidator: updating validator ${publicKeyToAddress(publicKeyHex, this.#config)}`);
|
|
108
173
|
this.#update(publicKeyHex, connection);
|
|
109
174
|
return true;
|
|
110
175
|
}
|
|
111
|
-
|
|
176
|
+
this.#logger.debug(`addValidator: didn't add validator ${publicKeyToAddress(publicKeyHex, this.#config)}`);
|
|
112
177
|
return false; // TODO: Implement better success/failure reporting
|
|
113
178
|
}
|
|
114
179
|
|
|
@@ -117,8 +182,9 @@ class ConnectionManager {
|
|
|
117
182
|
* @param {String | Buffer} publicKey - The public key hex string of the validator to remove
|
|
118
183
|
*/
|
|
119
184
|
remove(publicKey) {
|
|
120
|
-
|
|
185
|
+
this.#logger.debug(`remove: removing validator ${publicKeyToAddress(publicKey, this.#config)}`);
|
|
121
186
|
const publicKeyHex = this.#toHexString(publicKey);
|
|
187
|
+
this.#stopHealthCheck(publicKeyHex);
|
|
122
188
|
if (this.exists(publicKeyHex)) {
|
|
123
189
|
// Close the connection socket
|
|
124
190
|
const entry = this.#validators.get(publicKeyHex);
|
|
@@ -127,12 +193,13 @@ class ConnectionManager {
|
|
|
127
193
|
entry.connection.end();
|
|
128
194
|
} catch (e) {
|
|
129
195
|
// Ignore errors on connection end
|
|
196
|
+
this.#logger.debug(`remove: failed to end connection: ${e.message}`);
|
|
130
197
|
// TODO: Consider logging these errors here in verbose mode
|
|
131
198
|
}
|
|
132
199
|
}
|
|
133
|
-
|
|
200
|
+
this.#logger.debug(`remove: removing validator from map: ${publicKeyToAddress(publicKeyHex, this.#config)}. Map size before removal: ${this.#validators.size}.`);
|
|
134
201
|
this.#validators.delete(publicKeyHex);
|
|
135
|
-
|
|
202
|
+
this.#logger.debug(`remove: validator removed successfully. Map size is now ${this.#validators.size}.`);
|
|
136
203
|
}
|
|
137
204
|
}
|
|
138
205
|
|
|
@@ -206,26 +273,12 @@ class ConnectionManager {
|
|
|
206
273
|
}
|
|
207
274
|
|
|
208
275
|
prettyPrint() {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
// just to fetch a small subset of it (most times, 1 element).
|
|
216
|
-
// There are more efficient ways to pick a small subset of validators. Consider optimizing.
|
|
217
|
-
// Note 2: This method is unused now, but will be kept here for future reference
|
|
218
|
-
// TODO: Deprecated/Unused - remove if not needed
|
|
219
|
-
pickRandomSubset(validators, maxTargets) {
|
|
220
|
-
const copy = validators.slice();
|
|
221
|
-
const count = Math.min(maxTargets, copy.length);
|
|
222
|
-
|
|
223
|
-
for (let i = copy.length - 1; i > 0; i--) {
|
|
224
|
-
const j = Math.floor(Math.random() * (i + 1));
|
|
225
|
-
[copy[i], copy[j]] = [copy[j], copy[i]];
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return copy.slice(0, count);
|
|
276
|
+
this.#logger.info(`Connection count: ${this.connectionCount()}`);
|
|
277
|
+
this.#logger.info(`Validator map keys count: ${this.#validators.size}`);
|
|
278
|
+
this.#logger.info(`Validator map keys:\n${Array.from(this.#validators.entries()).map(([publicKey, val]) => {
|
|
279
|
+
const protocols = val.connection?.protocolSession?.preferredProtocol || 'none';
|
|
280
|
+
return `${publicKeyToAddress(publicKey, this.#config)}: ${protocols}`;
|
|
281
|
+
}).join('\n')}`);
|
|
229
282
|
}
|
|
230
283
|
|
|
231
284
|
/**
|
|
@@ -257,18 +310,18 @@ class ConnectionManager {
|
|
|
257
310
|
* @param {Object} connection - The connection object
|
|
258
311
|
*/
|
|
259
312
|
#append(publicKey, connection) {
|
|
260
|
-
|
|
313
|
+
this.#logger.debug(`#append: appending validator ${publicKeyToAddress(publicKey, this.#config)}`);
|
|
261
314
|
const publicKeyHex = this.#toHexString(publicKey);
|
|
262
315
|
if (this.#validators.has(publicKeyHex)) {
|
|
263
316
|
// This should never happen, but just in case, we log it
|
|
264
|
-
|
|
317
|
+
this.#logger.debug(`#append: tried to append existing validator: ${publicKeyToAddress(publicKey, this.#config)}`);
|
|
265
318
|
return;
|
|
266
319
|
}
|
|
267
|
-
this.#validators.set(publicKeyHex, {
|
|
320
|
+
this.#validators.set(publicKeyHex, {connection, sent: 0});
|
|
268
321
|
connection.on('close', () => {
|
|
269
|
-
|
|
322
|
+
this.#logger.debug(`#append: connection closing for validator ${publicKeyToAddress(publicKey, this.#config)}`);
|
|
270
323
|
this.remove(publicKeyHex);
|
|
271
|
-
|
|
324
|
+
this.#logger.debug(`#append: connection closed for validator ${publicKeyToAddress(publicKey, this.#config)}`);
|
|
272
325
|
});
|
|
273
326
|
}
|
|
274
327
|
|
|
@@ -283,25 +336,24 @@ class ConnectionManager {
|
|
|
283
336
|
// It would be preferable to keep them separated though, but we would need to review all usages to ensure correctness.
|
|
284
337
|
// Also, we should remove the 'else' branch below if we decide to keep 'update' and 'append' separated.
|
|
285
338
|
const publicKeyHex = this.#toHexString(publicKey);
|
|
286
|
-
|
|
339
|
+
this.#logger.debug(`#update: updating validator ${publicKeyToAddress(publicKey, this.#config)}`);
|
|
287
340
|
if (this.#validators.has(publicKeyHex)) {
|
|
288
341
|
this.#validators.get(publicKeyHex).connection = connection;
|
|
289
342
|
} else {
|
|
290
|
-
this.#validators.set(publicKeyHex, {
|
|
343
|
+
this.#validators.set(publicKeyHex, {connection, sent: 0});
|
|
291
344
|
}
|
|
292
345
|
}
|
|
293
346
|
|
|
294
|
-
#toAddress(publicKey) {
|
|
295
|
-
const keyHex = b4a.isBuffer(publicKey) ? publicKey : b4a.from(publicKey, 'hex');
|
|
296
|
-
return PeerWallet.encodeBech32m(
|
|
297
|
-
this.#config.addressPrefix,
|
|
298
|
-
keyHex
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
347
|
#toHexString(publicKey) {
|
|
303
348
|
return b4a.isBuffer(publicKey) ? publicKey.toString('hex') : publicKey;
|
|
304
349
|
}
|
|
305
350
|
}
|
|
306
351
|
|
|
352
|
+
export class ConnectionManagerError extends Error {
|
|
353
|
+
constructor(message) {
|
|
354
|
+
super(message);
|
|
355
|
+
this.name = this.constructor.name;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
307
359
|
export default ConnectionManager;
|
|
@@ -1,61 +1,185 @@
|
|
|
1
|
-
import { sleep } from '../../../utils/helpers.js';
|
|
1
|
+
import { generateUUID, publicKeyToAddress, sleep } from '../../../utils/helpers.js';
|
|
2
2
|
import { operationToPayload } from '../../../utils/applyOperations.js';
|
|
3
|
+
import { networkMessageFactory } from "../../../messages/network/v1/networkMessageFactory.js";
|
|
4
|
+
import { NETWORK_CAPABILITIES } from "../../../utils/constants.js";
|
|
5
|
+
import {
|
|
6
|
+
unsafeEncodeApplyOperation
|
|
7
|
+
} from "../../../utils/protobuf/operationHelpers.js";
|
|
8
|
+
import { normalizeMessageByOperationType } from "../../../utils/normalizers.js";
|
|
9
|
+
import { resultToValidatorAction, SENDER_ACTION } from "../protocols/connectionPolicies.js";
|
|
10
|
+
import { ConnectionManagerError } from './ConnectionManager.js';
|
|
3
11
|
/**
|
|
4
12
|
* MessageOrchestrator coordinates message submission, retry, and validator management.
|
|
5
13
|
* It works with ConnectionManager and ledger state to ensure reliable message delivery.
|
|
6
14
|
*/
|
|
7
15
|
class MessageOrchestrator {
|
|
8
16
|
#config;
|
|
17
|
+
#wallet;
|
|
9
18
|
/**
|
|
10
19
|
* Attempts to send a message to validators with retries and state checks.
|
|
11
20
|
* @param {ConnectionManager} connectionManager - The connection manager instance
|
|
12
21
|
* @param {object} state - The state to look for the message outcome
|
|
13
|
-
* @param {
|
|
22
|
+
* @param {Config} config - Configuration options:
|
|
14
23
|
*/
|
|
15
24
|
constructor(connectionManager, state, config) {
|
|
16
25
|
this.connectionManager = connectionManager;
|
|
17
26
|
this.state = state;
|
|
18
27
|
this.#config = config;
|
|
28
|
+
this.#wallet = null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
setWallet(wallet) {
|
|
32
|
+
this.#wallet = wallet;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Picks a validator for an outgoing message while avoiding requester self-validation.
|
|
37
|
+
*
|
|
38
|
+
* ValidatorObserverService already prevents connecting to the local node itself.
|
|
39
|
+
* This method handles a different case: for a given message, we avoid selecting
|
|
40
|
+
* a validator whose address equals `message.address` (requester), because
|
|
41
|
+
* validator-side checks reject that flow.
|
|
42
|
+
*
|
|
43
|
+
* @param {object} [message] Outgoing operation payload.
|
|
44
|
+
* @param {string} [message.address] Requester address (bech32m).
|
|
45
|
+
* @returns {string|null} Selected validator public key hex, or null when unavailable.
|
|
46
|
+
*/
|
|
47
|
+
#pickValidatorForMessage(message) {
|
|
48
|
+
const requesterAddress = message?.address;
|
|
49
|
+
if (!requesterAddress || typeof this.connectionManager.connectedValidators !== 'function') {
|
|
50
|
+
return this.connectionManager.pickRandomConnectedValidator();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const connected = this.connectionManager.connectedValidators();
|
|
54
|
+
if (!Array.isArray(connected) || connected.length === 0) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const eligible = connected.filter((publicKey) => {
|
|
59
|
+
return publicKeyToAddress(publicKey, this.#config) !== requesterAddress;
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const pool = eligible.length > 0 ? eligible : connected;
|
|
63
|
+
if (typeof this.connectionManager.pickRandomValidator === 'function') {
|
|
64
|
+
return this.connectionManager.pickRandomValidator(pool);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const index = Math.floor(Math.random() * pool.length);
|
|
68
|
+
return pool[index] ?? null;
|
|
19
69
|
}
|
|
20
70
|
|
|
21
71
|
/**
|
|
22
72
|
* Sends a message to a single randomly selected connected validator.
|
|
23
73
|
* @param {object} message - The message object to be sent
|
|
74
|
+
* @param retries - The current retry count for this message
|
|
24
75
|
* @returns {Promise<boolean>} - true if successful, false otherwise
|
|
25
76
|
*/
|
|
26
|
-
async send(message) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
77
|
+
async send(message, retries = 0) {
|
|
78
|
+
if (retries > this.#config.maxRetries) {
|
|
79
|
+
console.warn(`MessageOrchestrator: Max retries reached for message ${JSON.stringify(message)}. Aborting send.`);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const validatorPublicKey = this.#pickValidatorForMessage(message);
|
|
84
|
+
if (!validatorPublicKey) return false;
|
|
85
|
+
console.log("Sending message to validator:", publicKeyToAddress(validatorPublicKey, this.#config));
|
|
86
|
+
|
|
87
|
+
/* NOTE: Since the retry logic for Legacy is handled here, and is very unique to the protocol,
|
|
88
|
+
* it was decided to not change MessageOrchestrator send method in the refactor to make protocols transparent.
|
|
89
|
+
* As the Legacy protocol is going to be deprecated soon, it was decided to keep the retry logic
|
|
90
|
+
* here instead of abstracting it in the protocol implementation.
|
|
91
|
+
* If we were to abstract it, we would need to add protocol-specific logic in the ProtocolSession
|
|
92
|
+
* or ProtocolInterface, which would make them less clean and more coupled with the specifics of the protocols.
|
|
93
|
+
* The parts to be refactored in the future are marked with TODO comments.
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
// TODO: After Legacy is deprecated, we don't need to check preferred protocol here.
|
|
97
|
+
const validatorConnection = this.connectionManager.getConnection(validatorPublicKey);
|
|
98
|
+
const preferredProtocol = validatorConnection.protocolSession.preferredProtocol;
|
|
99
|
+
let success = false;
|
|
100
|
+
if (preferredProtocol === validatorConnection.protocolSession.supportedProtocols.LEGACY) {
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
success = await this.#attemptSendMessageForLegacy(validatorPublicKey, message);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
success = await this.send(message, retries + 1);
|
|
35
106
|
}
|
|
107
|
+
if (!success) {
|
|
108
|
+
// Remove validator and retry
|
|
109
|
+
this.connectionManager.remove(validatorPublicKey);
|
|
110
|
+
success = await this.send(message, retries + 1);
|
|
111
|
+
}
|
|
112
|
+
} else if (preferredProtocol === validatorConnection.protocolSession.supportedProtocols.V1) {
|
|
113
|
+
// TODO: This is probably better placed inside the V1 protocol definition.
|
|
114
|
+
// Both protocols should receive a 'canonical' message and solve the encodings internally
|
|
115
|
+
// Refactor
|
|
116
|
+
const normalizedMessage = normalizeMessageByOperationType(message, this.#config)
|
|
117
|
+
const encodedTransaction = unsafeEncodeApplyOperation(normalizedMessage)
|
|
118
|
+
const v1Message = await networkMessageFactory(this.#wallet, this.#config)
|
|
119
|
+
.buildBroadcastTransactionRequest(
|
|
120
|
+
generateUUID(),
|
|
121
|
+
encodedTransaction,
|
|
122
|
+
NETWORK_CAPABILITIES
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
await this.connectionManager.sendSingleMessage(v1Message, validatorPublicKey)
|
|
126
|
+
.then(
|
|
127
|
+
(resultCode) => {
|
|
128
|
+
// TODO: When we will deprecate the legacy protocol, we should refactor this scope, to propagate domain-error with result code.
|
|
129
|
+
const action = resultToValidatorAction(resultCode);
|
|
130
|
+
switch (action) {
|
|
131
|
+
case SENDER_ACTION.SUCCESS:
|
|
132
|
+
success = true;
|
|
133
|
+
//TODO: Create a function for action below, and replace it also in legacy flow.
|
|
134
|
+
this.incrementSentCount(validatorPublicKey);
|
|
135
|
+
if (this.shouldRemove(validatorPublicKey)) {
|
|
136
|
+
this.connectionManager.remove(validatorPublicKey);
|
|
137
|
+
}
|
|
138
|
+
break;
|
|
139
|
+
case SENDER_ACTION.ROTATE:
|
|
140
|
+
this.connectionManager.remove(validatorPublicKey);
|
|
141
|
+
break;
|
|
142
|
+
case SENDER_ACTION.NO_ROTATE:
|
|
143
|
+
// ignore
|
|
144
|
+
break;
|
|
145
|
+
default:
|
|
146
|
+
this.connectionManager.remove(validatorPublicKey);
|
|
147
|
+
console.warn(
|
|
148
|
+
`MessageOrchestrator: Unrecognized action from connectionPolicies: ${action}.
|
|
149
|
+
ResultCode was: ${resultCode}. Removing validator ${publicKeyToAddress(validatorPublicKey, this.#config)}`
|
|
150
|
+
);
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
)
|
|
155
|
+
.catch(
|
|
156
|
+
async (err) => {
|
|
157
|
+
if (err instanceof ConnectionManagerError) {
|
|
158
|
+
success = await this.send(message, retries + 1);
|
|
159
|
+
console.warn(`MessageOrchestrator: Connection Error: ${err.message}`);
|
|
160
|
+
} else {
|
|
161
|
+
this.connectionManager.remove(validatorPublicKey);
|
|
162
|
+
success = await this.send(message, retries + 1);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
)
|
|
166
|
+
|
|
36
167
|
}
|
|
37
|
-
return
|
|
168
|
+
return success;
|
|
38
169
|
}
|
|
39
170
|
|
|
40
|
-
|
|
41
|
-
|
|
171
|
+
// TODO: Delete this function after legacy protocol is deprecated
|
|
172
|
+
async #attemptSendMessageForLegacy(validatorPublicKey, message) {
|
|
42
173
|
const deductedTxType = operationToPayload(message.type);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (
|
|
48
|
-
this.
|
|
49
|
-
if (this.shouldRemove(validator)) {
|
|
50
|
-
this.connectionManager.remove(validator);
|
|
51
|
-
}
|
|
52
|
-
return true;
|
|
174
|
+
await this.connectionManager.sendSingleMessage(message, validatorPublicKey);
|
|
175
|
+
const appeared = await this.waitForUnsignedState(message[deductedTxType].tx, this.#config.messageValidatorResponseTimeout);
|
|
176
|
+
if (appeared) {
|
|
177
|
+
this.incrementSentCount(validatorPublicKey);
|
|
178
|
+
if (this.shouldRemove(validatorPublicKey)) {
|
|
179
|
+
this.connectionManager.remove(validatorPublicKey);
|
|
53
180
|
}
|
|
54
|
-
|
|
181
|
+
return true;
|
|
55
182
|
}
|
|
56
|
-
|
|
57
|
-
// If all retries fail, remove validator from pool
|
|
58
|
-
this.connectionManager.remove(validator);
|
|
59
183
|
return false;
|
|
60
184
|
}
|
|
61
185
|
|