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
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import Validator from 'fastest-validator';
|
|
2
|
+
import b4a from 'b4a';
|
|
3
|
+
import {
|
|
4
|
+
NetworkOperationType,
|
|
5
|
+
NONCE_BYTE_LENGTH,
|
|
6
|
+
SIGNATURE_BYTE_LENGTH,
|
|
7
|
+
MAX_PARTIAL_TX_PAYLOAD_BYTE_SIZE,
|
|
8
|
+
ResultCode
|
|
9
|
+
} from '../../../../../utils/constants.js';
|
|
10
|
+
|
|
11
|
+
const ALLOWED_RESULT_CODES = Object.values(ResultCode);
|
|
12
|
+
|
|
13
|
+
class V1ValidationSchema {
|
|
14
|
+
#validator;
|
|
15
|
+
#validateV1LivenessRequest;
|
|
16
|
+
#validateV1LivenessResponse;
|
|
17
|
+
#validateV1BroadcastTransactionRequest;
|
|
18
|
+
#validateV1BroadcastTransactionResponse;
|
|
19
|
+
|
|
20
|
+
constructor() {
|
|
21
|
+
this.#validator = new Validator({
|
|
22
|
+
useNewCustomCheckerFunction: true,
|
|
23
|
+
messages: {
|
|
24
|
+
buffer: "The '{field}' field must be a Buffer! Actual: {actual}",
|
|
25
|
+
bufferLength: "The '{field}' field must be a Buffer with length {expected}! Actual: {actual}",
|
|
26
|
+
bufferMinLength: "The '{field}' field must be a Buffer with min length {expected}! Actual: {actual}",
|
|
27
|
+
bufferMaxLength: "The '{field}' field must be a Buffer with max length {expected}! Actual: {actual}",
|
|
28
|
+
nonZeroBuffer: "The '{field}' field must not be an empty or zero-filled Buffer!",
|
|
29
|
+
emptyBuffer: "The '{field}' field must not be an empty Buffer! Actual: {actual}",
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
const isBuffer = b4a.isBuffer;
|
|
33
|
+
this.#validator.add("buffer", function ({schema, messages}, path, context) {
|
|
34
|
+
const allowZero = schema.allowZero === true;
|
|
35
|
+
const allowEmpty = schema.allowEmpty === true;
|
|
36
|
+
const exactLength = Number.isInteger(schema.length) ? schema.length : null;
|
|
37
|
+
const minLength = Number.isInteger(schema.min) ? schema.min : null;
|
|
38
|
+
const maxLength = Number.isInteger(schema.max) ? schema.max : null;
|
|
39
|
+
return {
|
|
40
|
+
source:
|
|
41
|
+
`
|
|
42
|
+
if (!${isBuffer}(value)) {
|
|
43
|
+
${this.makeError({type: "buffer", actual: "value", messages})}
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
const len = value.length;
|
|
47
|
+
${exactLength === null ? '' : `
|
|
48
|
+
if (len !== ${exactLength}) {
|
|
49
|
+
${this.makeError({type: "bufferLength", expected: exactLength, actual: "len", messages})}
|
|
50
|
+
}`}
|
|
51
|
+
${minLength === null ? '' : `
|
|
52
|
+
if (len < ${minLength}) {
|
|
53
|
+
${this.makeError({type: "bufferMinLength", expected: minLength, actual: "len", messages})}
|
|
54
|
+
}`}
|
|
55
|
+
${maxLength === null ? '' : `
|
|
56
|
+
if (len > ${maxLength}) {
|
|
57
|
+
${this.makeError({type: "bufferMaxLength", expected: maxLength, actual: "len", messages})}
|
|
58
|
+
}`}
|
|
59
|
+
if (len === 0 && !${allowEmpty}) {
|
|
60
|
+
${this.makeError({type: "emptyBuffer", actual: "len", messages})}
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
if (!${allowZero}) {
|
|
64
|
+
let isZeroFilled = true;
|
|
65
|
+
for (let i = 0; i < len; i++) {
|
|
66
|
+
if (value[i] !== 0) {
|
|
67
|
+
isZeroFilled = false;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (isZeroFilled) {
|
|
72
|
+
${this.makeError({type: "nonZeroBuffer", actual: "value", messages})}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
`
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
this.#validateV1LivenessRequest = this.#compileV1LivenessRequestSchema();
|
|
81
|
+
this.#validateV1LivenessResponse = this.#compileV1LivenessResponseSchema();
|
|
82
|
+
this.#validateV1BroadcastTransactionRequest = this.#compileV1BroadcastTransactionRequestSchema();
|
|
83
|
+
this.#validateV1BroadcastTransactionResponse = this.#compileV1BroadcastTransactionResponseSchema();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
#compileV1LivenessRequestSchema() {
|
|
87
|
+
const schema = {
|
|
88
|
+
$$strict: true,
|
|
89
|
+
type: {type: 'number', integer: true, equal: NetworkOperationType.LIVENESS_REQUEST, required: true},
|
|
90
|
+
id: {type: 'string', min: 1, max: 64, required: true},
|
|
91
|
+
timestamp: {type: 'number', integer: true, min: 1, max: Number.MAX_SAFE_INTEGER, required: true},
|
|
92
|
+
liveness_request: {
|
|
93
|
+
strict: true,
|
|
94
|
+
type: 'object',
|
|
95
|
+
props: {
|
|
96
|
+
nonce: {type: 'buffer', length: NONCE_BYTE_LENGTH, required: true},
|
|
97
|
+
signature: {type: 'buffer', length: SIGNATURE_BYTE_LENGTH, required: true},
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
capabilities: {type: 'array', items: 'string', required: true},
|
|
101
|
+
|
|
102
|
+
};
|
|
103
|
+
return this.#validator.compile(schema);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
validateV1LivenessRequest(operation) {
|
|
107
|
+
return this.#validateV1LivenessRequest(operation) === true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#compileV1LivenessResponseSchema() {
|
|
111
|
+
const schema = {
|
|
112
|
+
$$strict: true,
|
|
113
|
+
type: {type: 'number', integer: true, equal: NetworkOperationType.LIVENESS_RESPONSE, required: true},
|
|
114
|
+
id: {type: 'string', min: 1, max: 64, required: true},
|
|
115
|
+
timestamp: {type: 'number', integer: true, min: 1, max: Number.MAX_SAFE_INTEGER, required: true},
|
|
116
|
+
liveness_response: {
|
|
117
|
+
strict: true,
|
|
118
|
+
type: 'object',
|
|
119
|
+
props: {
|
|
120
|
+
nonce: {type: 'buffer', length: NONCE_BYTE_LENGTH, required: true},
|
|
121
|
+
signature: {type: 'buffer', length: SIGNATURE_BYTE_LENGTH, required: true},
|
|
122
|
+
result: {type: 'enum', values: ALLOWED_RESULT_CODES, required: true},
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
capabilities: {type: 'array', items: 'string', required: true},
|
|
126
|
+
|
|
127
|
+
};
|
|
128
|
+
return this.#validator.compile(schema);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
validateV1LivenessResponse(operation) {
|
|
132
|
+
return this.#validateV1LivenessResponse(operation) === true;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
#compileV1BroadcastTransactionRequestSchema() {
|
|
136
|
+
const schema = {
|
|
137
|
+
$$strict: true,
|
|
138
|
+
type: {
|
|
139
|
+
type: 'number',
|
|
140
|
+
integer: true,
|
|
141
|
+
equal: NetworkOperationType.BROADCAST_TRANSACTION_REQUEST,
|
|
142
|
+
required: true
|
|
143
|
+
},
|
|
144
|
+
id: {type: 'string', min: 1, max: 64, required: true},
|
|
145
|
+
timestamp: {type: 'number', integer: true, min: 1, max: Number.MAX_SAFE_INTEGER, required: true},
|
|
146
|
+
broadcast_transaction_request: {
|
|
147
|
+
strict: true,
|
|
148
|
+
type: 'object',
|
|
149
|
+
props: {
|
|
150
|
+
data: {
|
|
151
|
+
type: 'buffer',
|
|
152
|
+
min: 1,
|
|
153
|
+
max: MAX_PARTIAL_TX_PAYLOAD_BYTE_SIZE,
|
|
154
|
+
allowZero: true,
|
|
155
|
+
required: true
|
|
156
|
+
},
|
|
157
|
+
nonce: {type: 'buffer', length: NONCE_BYTE_LENGTH, required: true},
|
|
158
|
+
signature: {type: 'buffer', length: SIGNATURE_BYTE_LENGTH, required: true},
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
capabilities: {type: 'array', items: 'string', required: true},
|
|
162
|
+
|
|
163
|
+
};
|
|
164
|
+
return this.#validator.compile(schema);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
validateV1BroadcastTransactionRequest(operation) {
|
|
168
|
+
return this.#validateV1BroadcastTransactionRequest(operation) === true;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
#compileV1BroadcastTransactionResponseSchema() {
|
|
172
|
+
const schema = {
|
|
173
|
+
$$strict: true,
|
|
174
|
+
type: {
|
|
175
|
+
type: 'number',
|
|
176
|
+
integer: true,
|
|
177
|
+
equal: NetworkOperationType.BROADCAST_TRANSACTION_RESPONSE,
|
|
178
|
+
required: true
|
|
179
|
+
},
|
|
180
|
+
id: {type: 'string', min: 1, max: 64, required: true},
|
|
181
|
+
timestamp: {type: 'number', integer: true, min: 1, max: Number.MAX_SAFE_INTEGER, required: true},
|
|
182
|
+
broadcast_transaction_response: {
|
|
183
|
+
strict: true,
|
|
184
|
+
type: 'object',
|
|
185
|
+
props: {
|
|
186
|
+
nonce: {type: 'buffer', length: NONCE_BYTE_LENGTH, required: true},
|
|
187
|
+
signature: {type: 'buffer', length: SIGNATURE_BYTE_LENGTH, required: true},
|
|
188
|
+
proof: {type: 'buffer', allowEmpty: true, allowZero: true, required: true},
|
|
189
|
+
timestamp: {
|
|
190
|
+
type: 'number',
|
|
191
|
+
integer: true,
|
|
192
|
+
min: 0,
|
|
193
|
+
max: Number.MAX_SAFE_INTEGER,
|
|
194
|
+
optional: true
|
|
195
|
+
},
|
|
196
|
+
result: {type: 'enum', values: ALLOWED_RESULT_CODES, required: true},
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
capabilities: {type: 'array', items: 'string', required: true},
|
|
200
|
+
|
|
201
|
+
};
|
|
202
|
+
return this.#validator.compile(schema);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
validateV1BroadcastTransactionResponse(operation) {
|
|
206
|
+
return this.#validateV1BroadcastTransactionResponse(operation) === true;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export default V1ValidationSchema;
|
|
@@ -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
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;
|