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,450 @@
|
|
|
1
|
+
import sinon from "sinon";
|
|
2
|
+
import { hook, test } from 'brittle'
|
|
3
|
+
import { default as EventEmitter } from "bare-events"
|
|
4
|
+
import { testKeyPair1, testKeyPair2, testKeyPair3, testKeyPair4, testKeyPair5, testKeyPair6, testKeyPair7, testKeyPair8 } from "../../../fixtures/apply.fixtures.js";
|
|
5
|
+
import ConnectionManager, { ConnectionManagerError } from "../../../../src/core/network/services/ConnectionManager.js";
|
|
6
|
+
import { tick } from "../../../helpers/setupApplyTests.js";
|
|
7
|
+
import b4a from 'b4a'
|
|
8
|
+
import { createConfig, ENV } from "../../../../src/config/env.js";
|
|
9
|
+
import { EventType, ResultCode } from "../../../../src/utils/constants.js";
|
|
10
|
+
|
|
11
|
+
const createConnection = (key) => {
|
|
12
|
+
const emitter = new EventEmitter()
|
|
13
|
+
emitter.protocolSession = {
|
|
14
|
+
has: (name) => name === 'legacy',
|
|
15
|
+
send: sinon.stub().resolves(),
|
|
16
|
+
};
|
|
17
|
+
emitter.connected = true
|
|
18
|
+
emitter.remotePublicKey = b4a.from(key, 'hex')
|
|
19
|
+
|
|
20
|
+
return { key: b4a.from(key, 'hex'), connection: emitter }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const createV1Connection = (key, sendHealthCheckStub = sinon.stub().resolves(ResultCode.OK)) => {
|
|
24
|
+
const emitter = new EventEmitter()
|
|
25
|
+
emitter.protocolSession = {
|
|
26
|
+
sendHealthCheck: sendHealthCheckStub
|
|
27
|
+
};
|
|
28
|
+
emitter.connected = true
|
|
29
|
+
emitter.remotePublicKey = b4a.from(key, 'hex')
|
|
30
|
+
emitter.end = sinon.stub()
|
|
31
|
+
|
|
32
|
+
return { key: b4a.from(key, 'hex'), connection: emitter }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const makeHealthCheckService = () => {
|
|
36
|
+
const emitter = new EventEmitter();
|
|
37
|
+
emitter.has = sinon.stub().returns(true);
|
|
38
|
+
emitter.stop = sinon.stub();
|
|
39
|
+
return emitter;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const makeManager = (maxValidators = 6, conns = connections) => {
|
|
43
|
+
const merged = createConfig(ENV.DEVELOPMENT, { maxValidators })
|
|
44
|
+
const connectionManager = new ConnectionManager(merged)
|
|
45
|
+
|
|
46
|
+
conns.forEach(({ key, connection }) => {
|
|
47
|
+
connectionManager.addValidator(key, connection)
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return connectionManager
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const reset = () => {
|
|
54
|
+
sinon.restore()
|
|
55
|
+
connections.forEach(connection => {
|
|
56
|
+
connection.connection.protocolSession.send.resetHistory()
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let connections
|
|
61
|
+
hook('Initialize state', async () => {
|
|
62
|
+
connections = [
|
|
63
|
+
createConnection(testKeyPair1.publicKey),
|
|
64
|
+
createConnection(testKeyPair2.publicKey),
|
|
65
|
+
createConnection(testKeyPair3.publicKey),
|
|
66
|
+
createConnection(testKeyPair4.publicKey),
|
|
67
|
+
]
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('ConnectionManager', () => {
|
|
71
|
+
test('addValidator', async t => {
|
|
72
|
+
test('adds a validator', async t => {
|
|
73
|
+
reset()
|
|
74
|
+
const connectionManager = makeManager()
|
|
75
|
+
t.is(connectionManager.connectionCount(), connections.length, 'should have the same length')
|
|
76
|
+
const data = createConnection(testKeyPair5.publicKey)
|
|
77
|
+
connectionManager.addValidator(data.key, data.connection)
|
|
78
|
+
t.is(connectionManager.connectionCount(), connections.length + 1, 'should have the same length')
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test('dont surpass maxConnections', async t => {
|
|
82
|
+
reset()
|
|
83
|
+
const maxConnections = 5
|
|
84
|
+
const connectionManager = makeManager(maxConnections)
|
|
85
|
+
t.is(connectionManager.connectionCount(), connections.length, 'should have the same length')
|
|
86
|
+
|
|
87
|
+
const toAdd = createConnection(testKeyPair5.publicKey)
|
|
88
|
+
connectionManager.addValidator(toAdd.key, toAdd.connection)
|
|
89
|
+
t.is(connectionManager.connectionCount(), maxConnections, 'should match the max connections')
|
|
90
|
+
|
|
91
|
+
const toNotAdd = createConnection(testKeyPair6.publicKey)
|
|
92
|
+
connectionManager.addValidator(toNotAdd.key, toNotAdd.connection)
|
|
93
|
+
t.is(connectionManager.connectionCount(), maxConnections, 'should not increase length')
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
test('does not add new validator when pool is full', async t => {
|
|
97
|
+
reset()
|
|
98
|
+
const maxConnections = 2
|
|
99
|
+
const localConnections = [
|
|
100
|
+
createConnection(testKeyPair1.publicKey),
|
|
101
|
+
createConnection(testKeyPair2.publicKey),
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
const connectionManager = makeManager(maxConnections)
|
|
105
|
+
localConnections.forEach(({ key, connection }) => {
|
|
106
|
+
connectionManager.addValidator(key, connection)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
t.is(connectionManager.connectionCount(), maxConnections, 'pool should be full')
|
|
110
|
+
|
|
111
|
+
const newConn = createConnection(testKeyPair3.publicKey)
|
|
112
|
+
connectionManager.addValidator(newConn.key, newConn.connection)
|
|
113
|
+
|
|
114
|
+
t.is(connectionManager.connectionCount(), maxConnections, 'should stay at max size')
|
|
115
|
+
t.not(connectionManager.connected(newConn.key), 'new validator should not be in the pool')
|
|
116
|
+
|
|
117
|
+
const remainingOld = localConnections.filter(c => connectionManager.connected(c.key)).length
|
|
118
|
+
t.is(remainingOld, 2, 'all of the old validators should remain')
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('connected', async t => {
|
|
123
|
+
test('true', async t => {
|
|
124
|
+
reset()
|
|
125
|
+
const connectionManager = makeManager()
|
|
126
|
+
connections.forEach(con => {
|
|
127
|
+
t.ok(connectionManager.connected(con.key), 'should respond true')
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('false', async t => {
|
|
132
|
+
reset()
|
|
133
|
+
const connectionManager = makeManager()
|
|
134
|
+
t.ok(!connectionManager.connected(testKeyPair6.publicKey), 'should respond false')
|
|
135
|
+
})
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('sendSingleMessage', async t => {
|
|
139
|
+
test('returns exact resultCode from protocolSession.send', async t => {
|
|
140
|
+
reset()
|
|
141
|
+
const data = createConnection(testKeyPair1.publicKey)
|
|
142
|
+
data.connection.protocolSession.send = sinon.stub().resolves(ResultCode.TIMEOUT)
|
|
143
|
+
const connectionManager = makeManager(6, [data])
|
|
144
|
+
|
|
145
|
+
const result = await connectionManager.sendSingleMessage({ payload: 1 }, testKeyPair1.publicKey)
|
|
146
|
+
|
|
147
|
+
t.is(result, ResultCode.TIMEOUT, 'should return the exact result code from protocol session')
|
|
148
|
+
t.ok(data.connection.protocolSession.send.calledOnce, 'should invoke protocolSession.send')
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
test('throws ConnectionManagerError when validator is disconnected', async t => {
|
|
152
|
+
reset()
|
|
153
|
+
const connectionManager = makeManager()
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
await connectionManager.sendSingleMessage({ payload: 1 }, testKeyPair8.publicKey)
|
|
157
|
+
t.fail('expected sendSingleMessage to throw')
|
|
158
|
+
} catch (error) {
|
|
159
|
+
t.ok(error instanceof ConnectionManagerError, 'should throw ConnectionManagerError')
|
|
160
|
+
t.ok(error.message.includes('is not connected'), 'should include disconnected validator details')
|
|
161
|
+
}
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
test('throws ConnectionManagerError when protocolSession is missing', async t => {
|
|
165
|
+
reset()
|
|
166
|
+
const emitter = new EventEmitter()
|
|
167
|
+
emitter.connected = true
|
|
168
|
+
emitter.remotePublicKey = b4a.from(testKeyPair6.publicKey, 'hex')
|
|
169
|
+
emitter.end = sinon.stub()
|
|
170
|
+
const data = {
|
|
171
|
+
key: b4a.from(testKeyPair6.publicKey, 'hex'),
|
|
172
|
+
connection: emitter,
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const connectionManager = makeManager(6, [data])
|
|
176
|
+
|
|
177
|
+
try {
|
|
178
|
+
await connectionManager.sendSingleMessage({ payload: 1 }, testKeyPair6.publicKey)
|
|
179
|
+
t.fail('expected sendSingleMessage to throw')
|
|
180
|
+
} catch (error) {
|
|
181
|
+
t.ok(error instanceof ConnectionManagerError, 'should throw ConnectionManagerError')
|
|
182
|
+
t.ok(error.message.includes('no valid connection found'), 'should include protocol session details')
|
|
183
|
+
}
|
|
184
|
+
})
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
// Note: These tests were commented out because connectionManager.send is being deprecated. When it is completely removed, the tests should be deleted.
|
|
188
|
+
// test('send', async t => {
|
|
189
|
+
// // test('triggers send on messenger', async t => {
|
|
190
|
+
// // reset()
|
|
191
|
+
// // const connectionManager = makeManager()
|
|
192
|
+
|
|
193
|
+
// // const target = connectionManager.send([1,2,3,4])
|
|
194
|
+
|
|
195
|
+
// // const totalCalls = connections.reduce((sum, con) => sum + con.connection.protocolSession.send.callCount, 0)
|
|
196
|
+
// // t.is(totalCalls, 1, 'should send to exactly one validator')
|
|
197
|
+
// // t.ok(target, 'should return a target public key')
|
|
198
|
+
// // })
|
|
199
|
+
|
|
200
|
+
// test('does not throw on individual send errors', async t => {
|
|
201
|
+
// reset()
|
|
202
|
+
// const errorConnections = [
|
|
203
|
+
// createConnection(testKeyPair7.publicKey),
|
|
204
|
+
// createConnection(testKeyPair8.publicKey),
|
|
205
|
+
// ]
|
|
206
|
+
|
|
207
|
+
// errorConnections.forEach(con => {
|
|
208
|
+
// con.connection.protocolSession.send = sinon.stub().throws(new Error())
|
|
209
|
+
// })
|
|
210
|
+
|
|
211
|
+
// const connectionManager = makeManager(5, errorConnections)
|
|
212
|
+
|
|
213
|
+
// t.is(errorConnections.length, 2, 'should have two connections')
|
|
214
|
+
// connectionManager.send([1,2,3,4])
|
|
215
|
+
// t.ok(true, 'send should not throw even if individual sends fail')
|
|
216
|
+
// })
|
|
217
|
+
// })
|
|
218
|
+
|
|
219
|
+
test('on close', async t => {
|
|
220
|
+
test('removes from list', async t => {
|
|
221
|
+
reset()
|
|
222
|
+
const connectionManager = makeManager()
|
|
223
|
+
|
|
224
|
+
const connectionCount = connectionManager.connectionCount()
|
|
225
|
+
|
|
226
|
+
connections[1].connection.connected = false
|
|
227
|
+
connections[1].connection.emit('close')
|
|
228
|
+
await tick()
|
|
229
|
+
t.is(connectionCount, connectionManager.connectionCount() + 1, 'first on the list should have been called')
|
|
230
|
+
})
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
test('remove', async t => {
|
|
234
|
+
test('removes a validator by public key', async t => {
|
|
235
|
+
reset()
|
|
236
|
+
const connectionManager = makeManager()
|
|
237
|
+
const previousCount = connectionManager.connectionCount()
|
|
238
|
+
const lastValidator = connections.shift()
|
|
239
|
+
|
|
240
|
+
t.ok(connectionManager.connected(lastValidator.key), 'should be connected')
|
|
241
|
+
connectionManager.remove(lastValidator.key)
|
|
242
|
+
|
|
243
|
+
t.is(connectionManager.connectionCount(), previousCount - 1, 'should reduce the connection count')
|
|
244
|
+
t.ok(!connectionManager.connected(lastValidator.key), 'should be connected')
|
|
245
|
+
})
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
test('on close', async t => {
|
|
249
|
+
test('removes from list', async t => {
|
|
250
|
+
reset()
|
|
251
|
+
const connectionManager = makeManager()
|
|
252
|
+
|
|
253
|
+
const connectionCount = connectionManager.connectionCount()
|
|
254
|
+
|
|
255
|
+
connections[1].connection.connected = false
|
|
256
|
+
connections[1].connection.emit('close')
|
|
257
|
+
await tick()
|
|
258
|
+
t.is(connectionCount, connectionManager.connectionCount() + 1, 'first on the list should have been called')
|
|
259
|
+
})
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
test('health checks (strict)', async t => {
|
|
263
|
+
test('keeps validator on OK response', async t => {
|
|
264
|
+
try {
|
|
265
|
+
const v1Conn = createV1Connection(testKeyPair1.publicKey, sinon.stub().resolves(ResultCode.OK));
|
|
266
|
+
const connectionManager = makeManager(6, [v1Conn]);
|
|
267
|
+
const healthCheckService = makeHealthCheckService();
|
|
268
|
+
connectionManager.subscribeToHealthChecks(healthCheckService);
|
|
269
|
+
|
|
270
|
+
healthCheckService.emit(
|
|
271
|
+
EventType.VALIDATOR_HEALTH_CHECK,
|
|
272
|
+
testKeyPair1.publicKey,
|
|
273
|
+
"123456"
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
await tick();
|
|
277
|
+
t.ok(connectionManager.connected(v1Conn.key));
|
|
278
|
+
t.is(healthCheckService.stop.callCount, 0);
|
|
279
|
+
} finally {
|
|
280
|
+
sinon.restore();
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
test('removes validator on non-OK response', async t => {
|
|
285
|
+
try {
|
|
286
|
+
const v1Conn = createV1Connection(testKeyPair2.publicKey, sinon.stub().resolves(ResultCode.TIMEOUT));
|
|
287
|
+
const connectionManager = makeManager(6, [v1Conn]);
|
|
288
|
+
const healthCheckService = makeHealthCheckService();
|
|
289
|
+
connectionManager.subscribeToHealthChecks(healthCheckService);
|
|
290
|
+
|
|
291
|
+
healthCheckService.emit(
|
|
292
|
+
EventType.VALIDATOR_HEALTH_CHECK,
|
|
293
|
+
testKeyPair2.publicKey,
|
|
294
|
+
"123456"
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
await tick();
|
|
298
|
+
t.ok(!connectionManager.connected(v1Conn.key));
|
|
299
|
+
t.ok(healthCheckService.stop.callCount >= 1);
|
|
300
|
+
} finally {
|
|
301
|
+
sinon.restore();
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test('removes validator on send rejection', async t => {
|
|
306
|
+
try {
|
|
307
|
+
const v1Conn = createV1Connection(testKeyPair3.publicKey, sinon.stub().rejects(new Error('boom')));
|
|
308
|
+
const connectionManager = makeManager(6, [v1Conn]);
|
|
309
|
+
const healthCheckService = makeHealthCheckService();
|
|
310
|
+
connectionManager.subscribeToHealthChecks(healthCheckService);
|
|
311
|
+
|
|
312
|
+
healthCheckService.emit(
|
|
313
|
+
EventType.VALIDATOR_HEALTH_CHECK,
|
|
314
|
+
testKeyPair3.publicKey,
|
|
315
|
+
"123456"
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
await tick();
|
|
319
|
+
t.ok(!connectionManager.connected(v1Conn.key));
|
|
320
|
+
t.ok(healthCheckService.stop.callCount >= 1);
|
|
321
|
+
} finally {
|
|
322
|
+
sinon.restore();
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test('ignores malformed health check events', async t => {
|
|
327
|
+
try {
|
|
328
|
+
const v1Conn = createV1Connection(testKeyPair5.publicKey, sinon.stub().resolves(ResultCode.OK));
|
|
329
|
+
const connectionManager = makeManager(6, [v1Conn]);
|
|
330
|
+
let handler = null;
|
|
331
|
+
const healthCheckService = {
|
|
332
|
+
on: (_event, fn) => { handler = fn; },
|
|
333
|
+
off: () => {},
|
|
334
|
+
has: sinon.stub().returns(true),
|
|
335
|
+
stop: sinon.stub()
|
|
336
|
+
};
|
|
337
|
+
connectionManager.subscribeToHealthChecks(healthCheckService);
|
|
338
|
+
|
|
339
|
+
const cases = [
|
|
340
|
+
{ label: 'publicKey', publicKey: 123, requestId: 'abc' },
|
|
341
|
+
{ label: 'requestId', publicKey: testKeyPair5.publicKey, requestId: 456 },
|
|
342
|
+
{ label: 'undefined', publicKey: undefined, requestId: undefined },
|
|
343
|
+
];
|
|
344
|
+
|
|
345
|
+
for (const testCase of cases) {
|
|
346
|
+
await handler(testCase.publicKey, testCase.requestId);
|
|
347
|
+
t.pass(`ignored malformed payload: ${testCase.label}`);
|
|
348
|
+
}
|
|
349
|
+
} finally {
|
|
350
|
+
sinon.restore();
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
})
|
|
354
|
+
|
|
355
|
+
test('edge branches', async t => {
|
|
356
|
+
test('pickRandomValidator returns null for empty array', async t => {
|
|
357
|
+
reset()
|
|
358
|
+
const connectionManager = makeManager()
|
|
359
|
+
t.is(connectionManager.pickRandomValidator([]), null)
|
|
360
|
+
})
|
|
361
|
+
|
|
362
|
+
test('pickRandomConnectedValidator returns null when pool is empty', async t => {
|
|
363
|
+
reset()
|
|
364
|
+
const connectionManager = makeManager(6, [])
|
|
365
|
+
t.is(connectionManager.pickRandomConnectedValidator(), null)
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
test('remove missing validator keeps state unchanged', async t => {
|
|
369
|
+
reset()
|
|
370
|
+
const connectionManager = makeManager()
|
|
371
|
+
const before = connectionManager.connectionCount()
|
|
372
|
+
connectionManager.remove(testKeyPair8.publicKey)
|
|
373
|
+
t.is(connectionManager.connectionCount(), before)
|
|
374
|
+
})
|
|
375
|
+
|
|
376
|
+
test('remove handles connection.end throwing and still deletes validator', async t => {
|
|
377
|
+
reset()
|
|
378
|
+
const data = createConnection(testKeyPair7.publicKey)
|
|
379
|
+
data.connection.end = sinon.stub().throws(new Error('end boom'))
|
|
380
|
+
const connectionManager = makeManager(6, [data])
|
|
381
|
+
|
|
382
|
+
t.ok(connectionManager.connected(data.key))
|
|
383
|
+
connectionManager.remove(data.key)
|
|
384
|
+
t.absent(connectionManager.connected(data.key))
|
|
385
|
+
})
|
|
386
|
+
|
|
387
|
+
test('sent counters handle missing validators safely', async t => {
|
|
388
|
+
reset()
|
|
389
|
+
const connectionManager = makeManager()
|
|
390
|
+
t.is(connectionManager.getSentCount(testKeyPair8.publicKey), 0)
|
|
391
|
+
connectionManager.incrementSentCount(testKeyPair8.publicKey)
|
|
392
|
+
t.is(connectionManager.getSentCount(testKeyPair8.publicKey), 0)
|
|
393
|
+
})
|
|
394
|
+
|
|
395
|
+
test('subscribeToHealthChecks validates service interface', async t => {
|
|
396
|
+
reset()
|
|
397
|
+
const connectionManager = makeManager()
|
|
398
|
+
|
|
399
|
+
await t.exception(
|
|
400
|
+
() => connectionManager.subscribeToHealthChecks({ on() {} }),
|
|
401
|
+
/must implement on\/off/
|
|
402
|
+
)
|
|
403
|
+
})
|
|
404
|
+
|
|
405
|
+
test('health check removes validator when protocolSession is missing', async t => {
|
|
406
|
+
reset()
|
|
407
|
+
const emitter = new EventEmitter()
|
|
408
|
+
emitter.connected = true
|
|
409
|
+
emitter.remotePublicKey = b4a.from(testKeyPair6.publicKey, 'hex')
|
|
410
|
+
emitter.end = sinon.stub()
|
|
411
|
+
const data = {
|
|
412
|
+
key: b4a.from(testKeyPair6.publicKey, 'hex'),
|
|
413
|
+
connection: emitter
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const connectionManager = makeManager(6, [data])
|
|
417
|
+
const healthCheckService = {
|
|
418
|
+
on: (_event, fn) => { healthCheckService.handler = fn; },
|
|
419
|
+
off: () => {},
|
|
420
|
+
has: sinon.stub().returns(true),
|
|
421
|
+
stop: sinon.stub(),
|
|
422
|
+
handler: null,
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
connectionManager.subscribeToHealthChecks(healthCheckService)
|
|
426
|
+
await healthCheckService.handler(testKeyPair6.publicKey, 'hc-1')
|
|
427
|
+
|
|
428
|
+
t.absent(connectionManager.connected(data.key))
|
|
429
|
+
t.ok(healthCheckService.stop.called)
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
test('remove tolerates health check service errors', async t => {
|
|
433
|
+
reset()
|
|
434
|
+
const data = createConnection(testKeyPair5.publicKey)
|
|
435
|
+
const connectionManager = makeManager(6, [data])
|
|
436
|
+
const healthCheckService = {
|
|
437
|
+
on: (_event, fn) => { healthCheckService.handler = fn; },
|
|
438
|
+
off: () => {},
|
|
439
|
+
has: sinon.stub().throws(new Error('has boom')),
|
|
440
|
+
stop: sinon.stub(),
|
|
441
|
+
handler: null,
|
|
442
|
+
}
|
|
443
|
+
connectionManager.subscribeToHealthChecks(healthCheckService)
|
|
444
|
+
|
|
445
|
+
connectionManager.remove(data.key)
|
|
446
|
+
|
|
447
|
+
t.absent(connectionManager.connected(data.key))
|
|
448
|
+
})
|
|
449
|
+
})
|
|
450
|
+
})
|