trac-msb 0.2.11 → 0.2.12
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/.github/workflows/acceptance-tests.yml +38 -0
- package/.github/workflows/lint-pr-title.yml +26 -0
- package/.github/workflows/publish.yml +33 -0
- package/.github/workflows/unit-tests.yml +34 -0
- package/package.json +5 -10
- package/proto/network.proto +74 -0
- package/rpc/rpc_services.js +4 -22
- package/scripts/generate-protobufs.js +12 -37
- package/src/config/config.js +5 -26
- package/src/config/env.js +11 -25
- package/src/core/network/Network.js +36 -73
- package/src/core/network/protocols/LegacyProtocol.js +11 -21
- package/src/core/network/protocols/NetworkMessages.js +17 -38
- package/src/core/network/protocols/ProtocolInterface.js +2 -14
- package/src/core/network/protocols/ProtocolSession.js +17 -144
- package/src/core/network/protocols/V1Protocol.js +18 -37
- package/src/core/network/protocols/legacy/NetworkMessageRouter.js +19 -25
- package/src/core/network/protocols/legacy/handlers/{LegacyGetRequestHandler.js → GetRequestHandler.js} +6 -6
- package/src/core/network/protocols/legacy/handlers/ResponseHandler.js +37 -0
- package/src/core/network/protocols/{legacy/handlers/LegacyRoleOperationHandler.js → shared/handlers/RoleOperationHandler.js} +11 -18
- package/src/core/network/protocols/{legacy/handlers/LegacySubnetworkOperationHandler.js → shared/handlers/SubnetworkOperationHandler.js} +17 -28
- package/src/core/network/protocols/{legacy/handlers/LegacyTransferOperationHandler.js → shared/handlers/TransferOperationHandler.js} +11 -17
- package/src/core/network/protocols/{legacy/handlers/BaseStateOperationHandler.js → shared/handlers/base/BaseOperationHandler.js} +12 -23
- package/src/core/network/protocols/shared/validators/{PartialBootstrapDeploymentValidator.js → PartialBootstrapDeployment.js} +4 -9
- package/src/core/network/protocols/shared/validators/{PartialRoleAccessValidator.js → PartialRoleAccess.js} +17 -51
- package/src/core/network/protocols/shared/validators/{PartialTransactionValidator.js → PartialTransaction.js} +7 -21
- package/src/core/network/protocols/shared/validators/{PartialTransferValidator.js → PartialTransfer.js} +9 -26
- package/src/core/network/protocols/shared/validators/{PartialOperationValidator.js → base/PartialOperation.js} +25 -47
- package/src/core/network/protocols/v1/NetworkMessageRouter.js +7 -91
- package/src/core/network/services/ConnectionManager.js +94 -146
- package/src/core/network/services/MessageOrchestrator.js +27 -151
- package/src/core/network/services/TransactionPoolService.js +18 -129
- package/src/core/network/services/TransactionRateLimiterService.js +34 -52
- package/src/core/network/services/ValidatorObserverService.js +26 -18
- package/src/core/state/State.js +19 -70
- package/src/index.js +4 -5
- package/src/messages/network/v1/NetworkMessageBuilder.js +79 -59
- package/src/messages/network/v1/NetworkMessageDirector.js +50 -16
- package/src/utils/Scheduler.js +8 -0
- package/src/utils/constants.js +5 -71
- package/src/utils/helpers.js +1 -10
- package/src/utils/normalizers.js +0 -38
- package/src/utils/protobuf/network.cjs +840 -0
- package/src/utils/protobuf/operationHelpers.js +3 -24
- package/tests/acceptance/v1/account/account.test.mjs +2 -8
- package/tests/acceptance/v1/tx/tx.test.mjs +1 -23
- package/tests/acceptance/v1/tx-details/tx-details.test.mjs +6 -34
- package/tests/fixtures/networkV1.fixtures.js +28 -2
- package/tests/helpers/transactionPayloads.mjs +2 -2
- package/tests/unit/messages/network/NetworkMessageBuilder.test.js +79 -239
- package/tests/unit/messages/network/NetworkMessageDirector.test.js +77 -223
- package/tests/unit/network/ConnectionManager.test.js +191 -0
- package/tests/unit/network/networkModule.test.js +1 -4
- package/tests/unit/unit.test.js +2 -2
- package/tests/unit/utils/protobuf/operationHelpers.test.js +4 -2
- package/tests/unit/utils/utils.test.js +0 -1
- package/proto/network/v1/enums/message_type.proto +0 -16
- package/proto/network/v1/enums/result_code.proto +0 -84
- package/proto/network/v1/messages/broadcast_transaction_request.proto +0 -9
- package/proto/network/v1/messages/broadcast_transaction_response.proto +0 -13
- package/proto/network/v1/messages/liveness_request.proto +0 -8
- package/proto/network/v1/messages/liveness_response.proto +0 -11
- package/proto/network/v1/network_message.proto +0 -22
- package/src/core/network/protocols/connectionPolicies.js +0 -88
- package/src/core/network/protocols/legacy/handlers/LegacyResponseHandler.js +0 -23
- package/src/core/network/protocols/shared/errors/SharedValidatorRejectionError.js +0 -27
- package/src/core/network/protocols/v1/V1ProtocolError.js +0 -91
- package/src/core/network/protocols/v1/handlers/V1BaseOperationHandler.js +0 -65
- package/src/core/network/protocols/v1/handlers/V1BroadcastTransactionOperationHandler.js +0 -389
- package/src/core/network/protocols/v1/handlers/V1LivenessOperationHandler.js +0 -87
- package/src/core/network/protocols/v1/validators/V1BaseOperation.js +0 -211
- package/src/core/network/protocols/v1/validators/V1BroadcastTransactionRequest.js +0 -26
- package/src/core/network/protocols/v1/validators/V1BroadcastTransactionResponse.js +0 -276
- package/src/core/network/protocols/v1/validators/V1LivenessRequest.js +0 -15
- package/src/core/network/protocols/v1/validators/V1LivenessResponse.js +0 -17
- package/src/core/network/protocols/v1/validators/V1ValidationSchema.js +0 -210
- package/src/core/network/services/PendingRequestService.js +0 -172
- package/src/core/network/services/TransactionCommitService.js +0 -149
- package/src/core/network/services/ValidatorHealthCheckService.js +0 -127
- package/src/utils/deepEqualApplyPayload.js +0 -40
- package/src/utils/logger.js +0 -25
- package/src/utils/protobuf/networkV1.generated.cjs +0 -2460
- package/tests/unit/network/LegacyNetworkMessageRouter.test.js +0 -54
- package/tests/unit/network/ProtocolSession.test.js +0 -127
- package/tests/unit/network/services/ConnectionManager.test.js +0 -450
- package/tests/unit/network/services/MessageOrchestrator.test.js +0 -445
- package/tests/unit/network/services/PendingRequestService.test.js +0 -431
- package/tests/unit/network/services/TransactionCommitService.test.js +0 -246
- package/tests/unit/network/services/TransactionPoolService.test.js +0 -489
- package/tests/unit/network/services/TransactionRateLimiterService.test.js +0 -139
- package/tests/unit/network/services/ValidatorHealthCheckService.test.js +0 -115
- package/tests/unit/network/services/services.test.js +0 -17
- package/tests/unit/network/utils/v1TestUtils.js +0 -153
- package/tests/unit/network/v1/NetworkMessageRouterV1.test.js +0 -151
- package/tests/unit/network/v1/V1BaseOperation.test.js +0 -356
- package/tests/unit/network/v1/V1BroadcastTransactionOperationHandler.test.js +0 -129
- package/tests/unit/network/v1/V1BroadcastTransactionRequest.test.js +0 -53
- package/tests/unit/network/v1/V1BroadcastTransactionResponse.test.js +0 -512
- package/tests/unit/network/v1/V1LivenessRequest.test.js +0 -32
- package/tests/unit/network/v1/V1LivenessResponse.test.js +0 -45
- package/tests/unit/network/v1/V1ResultCode.test.js +0 -84
- package/tests/unit/network/v1/V1ValidationSchema.test.js +0 -13
- package/tests/unit/network/v1/connectionPolicies.test.js +0 -49
- package/tests/unit/network/v1/handlers/V1BaseOperationHandler.test.js +0 -284
- package/tests/unit/network/v1/handlers/V1BroadcastTransactionOperationHandler.test.js +0 -794
- package/tests/unit/network/v1/handlers/V1LivenessOperationHandler.test.js +0 -193
- package/tests/unit/network/v1/v1.handlers.test.js +0 -15
- package/tests/unit/network/v1/v1.test.js +0 -19
- package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionRequest.test.js +0 -119
- package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionResponse.test.js +0 -136
- package/tests/unit/network/v1/v1ValidationSchema/common.test.js +0 -308
- package/tests/unit/network/v1/v1ValidationSchema/livenessRequest.test.js +0 -90
- package/tests/unit/network/v1/v1ValidationSchema/livenessResponse.test.js +0 -133
- package/tests/unit/utils/deepEqualApplyPayload/deepEqualApplyPayload.test.js +0 -102
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { test } from 'brittle';
|
|
2
|
-
import sinon from 'sinon';
|
|
3
|
-
import b4a from 'b4a';
|
|
4
|
-
import { config } from '../../helpers/config.js';
|
|
5
|
-
import NetworkMessageRouter from '../../../src/core/network/protocols/legacy/NetworkMessageRouter.js';
|
|
6
|
-
import LegacyGetRequestHandler from '../../../src/core/network/protocols/legacy/handlers/LegacyGetRequestHandler.js';
|
|
7
|
-
import LegacyResponseHandler from '../../../src/core/network/protocols/legacy/handlers/LegacyResponseHandler.js';
|
|
8
|
-
import { NETWORK_MESSAGE_TYPES } from '../../../src/utils/constants.js';
|
|
9
|
-
|
|
10
|
-
const makeConnection = (sandbox) => ({
|
|
11
|
-
remotePublicKey: b4a.alloc(32, 0x01),
|
|
12
|
-
protocolSession: {
|
|
13
|
-
setLegacyAsPreferredProtocol: sandbox.stub()
|
|
14
|
-
},
|
|
15
|
-
end: sandbox.stub()
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
const makeRouterContext = (t) => {
|
|
19
|
-
const sandbox = sinon.createSandbox();
|
|
20
|
-
t.teardown(() => sandbox.restore());
|
|
21
|
-
|
|
22
|
-
const getHandler = sandbox.stub(LegacyGetRequestHandler.prototype, 'handle').resolves();
|
|
23
|
-
const responseHandler = sandbox.stub(LegacyResponseHandler.prototype, 'handle').resolves();
|
|
24
|
-
const router = new NetworkMessageRouter({}, { address: 'test-wallet' }, {}, {}, config);
|
|
25
|
-
const connection = makeConnection(sandbox);
|
|
26
|
-
|
|
27
|
-
return { connection, getHandler, responseHandler, router };
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
test('LegacyNetworkMessageRouter', async (t) => {
|
|
31
|
-
await t.test('routes legacy string GET messages', async (t) => {
|
|
32
|
-
const { connection, getHandler, responseHandler, router } = makeRouterContext(t);
|
|
33
|
-
const message = NETWORK_MESSAGE_TYPES.GET.VALIDATOR;
|
|
34
|
-
|
|
35
|
-
await router.route(message, connection);
|
|
36
|
-
|
|
37
|
-
t.ok(connection.protocolSession.setLegacyAsPreferredProtocol.calledOnce, 'should prefer legacy protocol');
|
|
38
|
-
t.ok(getHandler.calledOnce, 'should route GET message');
|
|
39
|
-
t.ok(responseHandler.notCalled, 'should not route response handler');
|
|
40
|
-
t.is(getHandler.firstCall.args[0], message, 'passes GET message through to handler');
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
await t.test('routes legacy object response messages', async (t) => {
|
|
44
|
-
const { connection, getHandler, responseHandler, router } = makeRouterContext(t);
|
|
45
|
-
const message = { op: NETWORK_MESSAGE_TYPES.RESPONSE.VALIDATOR };
|
|
46
|
-
|
|
47
|
-
await router.route(message, connection);
|
|
48
|
-
|
|
49
|
-
t.ok(connection.protocolSession.setLegacyAsPreferredProtocol.calledOnce, 'should prefer legacy protocol');
|
|
50
|
-
t.ok(responseHandler.calledOnce, 'should route response message');
|
|
51
|
-
t.ok(getHandler.notCalled, 'should not route GET handler');
|
|
52
|
-
t.is(responseHandler.firstCall.args[0], message, 'passes response message through to handler');
|
|
53
|
-
});
|
|
54
|
-
});
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { test } from 'brittle';
|
|
2
|
-
import sinon from 'sinon';
|
|
3
|
-
import b4a from 'b4a';
|
|
4
|
-
|
|
5
|
-
import NetworkWalletFactory from '../../../src/core/network/identity/NetworkWalletFactory.js';
|
|
6
|
-
import ProtocolSession from '../../../src/core/network/protocols/ProtocolSession.js';
|
|
7
|
-
import { ResultCode } from '../../../src/utils/constants.js';
|
|
8
|
-
import { config } from '../../helpers/config.js';
|
|
9
|
-
import { testKeyPair1 } from '../../fixtures/apply.fixtures.js';
|
|
10
|
-
|
|
11
|
-
function createWallet() {
|
|
12
|
-
const keyPair = {
|
|
13
|
-
publicKey: b4a.from(testKeyPair1.publicKey, 'hex'),
|
|
14
|
-
secretKey: b4a.from(testKeyPair1.secretKey, 'hex')
|
|
15
|
-
};
|
|
16
|
-
return NetworkWalletFactory.provide({
|
|
17
|
-
enableWallet: false,
|
|
18
|
-
keyPair,
|
|
19
|
-
networkPrefix: config.addressPrefix
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function makeProtocol(sendStub) {
|
|
24
|
-
return {
|
|
25
|
-
send: sendStub ?? sinon.stub().resolves(ResultCode.OK),
|
|
26
|
-
sendAndForget: sinon.stub(),
|
|
27
|
-
decode: sinon.stub(),
|
|
28
|
-
close: sinon.stub()
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
test('ProtocolSession', (t) => {
|
|
33
|
-
t.teardown(() => sinon.restore());
|
|
34
|
-
|
|
35
|
-
test('probe sets preferred protocol to v1 on OK', async (t) => {
|
|
36
|
-
const v1Send = sinon.stub().resolves(ResultCode.OK);
|
|
37
|
-
const session = new ProtocolSession(
|
|
38
|
-
makeProtocol(),
|
|
39
|
-
makeProtocol(v1Send),
|
|
40
|
-
createWallet(),
|
|
41
|
-
config
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
await session.probe();
|
|
45
|
-
t.is(session.preferredProtocol, session.supportedProtocols.V1);
|
|
46
|
-
t.ok(v1Send.calledOnce);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test('probe sets preferred protocol to legacy on non-OK', async (t) => {
|
|
50
|
-
const v1Send = sinon.stub().resolves(ResultCode.TIMEOUT);
|
|
51
|
-
const session = new ProtocolSession(
|
|
52
|
-
makeProtocol(),
|
|
53
|
-
makeProtocol(v1Send),
|
|
54
|
-
createWallet(),
|
|
55
|
-
config
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
await session.probe();
|
|
59
|
-
t.is(session.preferredProtocol, session.supportedProtocols.LEGACY);
|
|
60
|
-
t.ok(v1Send.calledOnce);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test('probe sets preferred protocol to legacy on rejection', async (t) => {
|
|
64
|
-
const v1Send = sinon.stub().rejects(new Error('boom'));
|
|
65
|
-
const session = new ProtocolSession(
|
|
66
|
-
makeProtocol(),
|
|
67
|
-
makeProtocol(v1Send),
|
|
68
|
-
createWallet(),
|
|
69
|
-
config
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
await session.probe();
|
|
73
|
-
t.is(session.preferredProtocol, session.supportedProtocols.LEGACY);
|
|
74
|
-
t.ok(v1Send.calledOnce);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test('sendHealthCheck returns OK when preferred is v1', async (t) => {
|
|
78
|
-
const v1Send = sinon.stub().resolves(ResultCode.OK);
|
|
79
|
-
const session = new ProtocolSession(
|
|
80
|
-
makeProtocol(),
|
|
81
|
-
makeProtocol(v1Send),
|
|
82
|
-
createWallet(),
|
|
83
|
-
config
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
session.setV1AsPreferredProtocol();
|
|
87
|
-
const result = await session.sendHealthCheck();
|
|
88
|
-
t.is(result, ResultCode.OK);
|
|
89
|
-
t.ok(v1Send.calledOnce);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test('sendHealthCheck returns OK when preferred is legacy', async (t) => {
|
|
93
|
-
const session = new ProtocolSession(
|
|
94
|
-
makeProtocol(),
|
|
95
|
-
makeProtocol(),
|
|
96
|
-
createWallet(),
|
|
97
|
-
config
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
session.setLegacyAsPreferredProtocol();
|
|
101
|
-
const result = await session.sendHealthCheck();
|
|
102
|
-
t.is(result, ResultCode.OK);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test('sendHealthCheck returns UNSPECIFIED when not probed', async (t) => {
|
|
106
|
-
const session = new ProtocolSession(
|
|
107
|
-
makeProtocol(),
|
|
108
|
-
makeProtocol(),
|
|
109
|
-
createWallet(),
|
|
110
|
-
config
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
const result = await session.sendHealthCheck();
|
|
114
|
-
t.is(result, ResultCode.UNSPECIFIED);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
test('isHealthCheckSupported throws when not probed', async (t) => {
|
|
118
|
-
const session = new ProtocolSession(
|
|
119
|
-
makeProtocol(),
|
|
120
|
-
makeProtocol(),
|
|
121
|
-
createWallet(),
|
|
122
|
-
config
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
await t.exception.all(() => session.isHealthCheckSupported());
|
|
126
|
-
});
|
|
127
|
-
});
|
|
@@ -1,450 +0,0 @@
|
|
|
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
|
-
})
|