@onekeyfe/hd-transport 1.2.0-alpha.3 → 1.2.0-alpha.31
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/README.md +1 -1
- package/__tests__/messages.test.js +142 -0
- package/__tests__/protocol-v2-ble-frame-writer.test.js +119 -0
- package/__tests__/protocol-v2-link-manager.test.js +389 -0
- package/__tests__/protocol-v2-usb-transport-base.test.js +330 -0
- package/__tests__/protocol-v2.test.js +555 -119
- package/dist/constants.d.ts +6 -3
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.d.ts +981 -278
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +759 -247
- package/dist/protocols/index.d.ts +14 -5
- package/dist/protocols/index.d.ts.map +1 -1
- package/dist/protocols/v2/ble-frame-writer.d.ts +15 -0
- package/dist/protocols/v2/ble-frame-writer.d.ts.map +1 -0
- package/dist/protocols/v2/constants.d.ts +1 -0
- package/dist/protocols/v2/constants.d.ts.map +1 -1
- package/dist/protocols/v2/decode.d.ts +13 -2
- package/dist/protocols/v2/decode.d.ts.map +1 -1
- package/dist/protocols/v2/encode.d.ts +2 -3
- package/dist/protocols/v2/encode.d.ts.map +1 -1
- package/dist/protocols/v2/errors.d.ts +8 -0
- package/dist/protocols/v2/errors.d.ts.map +1 -0
- package/dist/protocols/v2/frame-assembler.d.ts.map +1 -1
- package/dist/protocols/v2/index.d.ts +2 -1
- package/dist/protocols/v2/index.d.ts.map +1 -1
- package/dist/protocols/v2/link-manager.d.ts +38 -0
- package/dist/protocols/v2/link-manager.d.ts.map +1 -0
- package/dist/protocols/v2/sequence-cursor.d.ts +5 -0
- package/dist/protocols/v2/sequence-cursor.d.ts.map +1 -0
- package/dist/protocols/v2/session.d.ts +19 -5
- package/dist/protocols/v2/session.d.ts.map +1 -1
- package/dist/protocols/v2/usb-transport-base.d.ts +32 -0
- package/dist/protocols/v2/usb-transport-base.d.ts.map +1 -0
- package/dist/serialization/protobuf/decode.d.ts.map +1 -1
- package/dist/types/messages.d.ts +523 -158
- package/dist/types/messages.d.ts.map +1 -1
- package/dist/types/transport.d.ts +13 -3
- package/dist/types/transport.d.ts.map +1 -1
- package/messages-protocol-v2.json +796 -620
- package/package.json +3 -3
- package/scripts/protobuf-build.sh +96 -310
- package/scripts/protobuf-patches/index.js +1 -0
- package/scripts/protobuf-types.js +19 -1
- package/src/constants.ts +29 -15
- package/src/index.ts +11 -1
- package/src/protocols/index.ts +39 -40
- package/src/protocols/v2/ble-frame-writer.ts +77 -0
- package/src/protocols/v2/constants.ts +1 -0
- package/src/protocols/v2/crc8.ts +1 -1
- package/src/protocols/v2/decode.ts +71 -37
- package/src/protocols/v2/encode.ts +2 -28
- package/src/protocols/v2/errors.ts +25 -0
- package/src/protocols/v2/frame-assembler.ts +6 -4
- package/src/protocols/v2/index.ts +2 -1
- package/src/protocols/v2/link-manager.ts +186 -0
- package/src/protocols/v2/sequence-cursor.ts +10 -0
- package/src/protocols/v2/session.ts +157 -201
- package/src/protocols/v2/usb-transport-base.ts +213 -0
- package/src/serialization/protobuf/decode.ts +4 -1
- package/src/types/messages.ts +623 -181
- package/src/types/transport.ts +13 -3
- package/dist/protocols/v2/debug.d.ts +0 -13
- package/dist/protocols/v2/debug.d.ts.map +0 -1
- package/src/protocols/v2/debug.ts +0 -26
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ In order to be able to use new features of onekey-firmware you need to update pr
|
|
|
20
20
|
1. `yarn update-submodules` to update firmware submodule
|
|
21
21
|
1. `yarn update-protobuf` to generate new `./messages.json`, `./messages-protocol-v2.json` and `./src/types/messages.ts`
|
|
22
22
|
|
|
23
|
-
The same task can be run from the repository root with `yarn update-protobuf`. The Protocol V2 schema requires the `firmware-pro2` submodule checked out on branch `
|
|
23
|
+
The same task can be run from the repository root with `yarn update-protobuf`. The Protocol V2 schema requires the `firmware-pro2` submodule checked out on branch `dev`.
|
|
24
24
|
|
|
25
25
|
## Docs
|
|
26
26
|
|
|
@@ -3,6 +3,11 @@ const {
|
|
|
3
3
|
createMessageFromType,
|
|
4
4
|
parseConfigure,
|
|
5
5
|
} = require('../src/serialization/protobuf/messages');
|
|
6
|
+
const v1Messages = require('../../core/src/data/messages/messages.json');
|
|
7
|
+
const v2Messages = require('../messages-protocol-v2.json');
|
|
8
|
+
const coreV2Messages = require('../../core/src/data/messages/messages-protocol-v2.json');
|
|
9
|
+
const generatedTypes = require('../src/types/messages');
|
|
10
|
+
|
|
6
11
|
// const json = require('./data/messages.json');
|
|
7
12
|
|
|
8
13
|
const json = {
|
|
@@ -58,6 +63,143 @@ const json = {
|
|
|
58
63
|
};
|
|
59
64
|
|
|
60
65
|
describe('messages', () => {
|
|
66
|
+
test('V1 GetPassphraseState matches the current firmware schema', () => {
|
|
67
|
+
const { fields } = v1Messages.nested.GetPassphraseState;
|
|
68
|
+
|
|
69
|
+
expect(fields.passphrase_state).toMatchObject({ id: 1, type: 'string' });
|
|
70
|
+
expect(fields).not.toHaveProperty('_only_main_pin');
|
|
71
|
+
expect(fields).not.toHaveProperty('allow_create_attach_pin');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('Protocol V2 firmware targets match the current firmware-pro2 enum', () => {
|
|
75
|
+
expect(v2Messages.nested.DeviceFirmwareTargetType.values).toEqual({
|
|
76
|
+
FW_MGMT_TARGET_INVALID: 0,
|
|
77
|
+
FW_MGMT_TARGET_CRATE: 1,
|
|
78
|
+
FW_MGMT_TARGET_ROMLOADER: 2,
|
|
79
|
+
FW_MGMT_TARGET_BOOTLOADER: 3,
|
|
80
|
+
FW_MGMT_TARGET_APPLICATION_P1: 4,
|
|
81
|
+
FW_MGMT_TARGET_APPLICATION_P2: 5,
|
|
82
|
+
FW_MGMT_TARGET_COPROCESSOR: 6,
|
|
83
|
+
FW_MGMT_TARGET_SE01: 7,
|
|
84
|
+
FW_MGMT_TARGET_SE02: 8,
|
|
85
|
+
FW_MGMT_TARGET_SE03: 9,
|
|
86
|
+
FW_MGMT_TARGET_SE04: 10,
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('Protocol V2 conflicting enums keep their own wire values', () => {
|
|
91
|
+
expect(generatedTypes.ProtocolV2FailureType).toMatchObject({
|
|
92
|
+
Failure_DataError: 4,
|
|
93
|
+
Failure_ProcessError: 5,
|
|
94
|
+
});
|
|
95
|
+
expect(generatedTypes.Enum_ProtocolV2Capability).toMatchObject({
|
|
96
|
+
Capability_AttachToPin: 18,
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('Protocol V2 device status and session messages match firmware-pro2', () => {
|
|
101
|
+
expect(v2Messages.nested.MessageType.values).toMatchObject({
|
|
102
|
+
MessageType_DeviceStatusGet: 60602,
|
|
103
|
+
MessageType_DeviceStatus: 60603,
|
|
104
|
+
MessageType_DeviceSessionGet: 60606,
|
|
105
|
+
MessageType_DeviceSession: 60607,
|
|
106
|
+
MessageType_DeviceSessionAskPin: 60608,
|
|
107
|
+
MessageType_DeviceSessionAskPassphrase: 60609,
|
|
108
|
+
});
|
|
109
|
+
expect(v2Messages.nested.DeviceStatusGet).toEqual({ fields: {} });
|
|
110
|
+
expect(v2Messages.nested.ProtocolInfoRequest.fields.eventless_wallet_session).toMatchObject({
|
|
111
|
+
id: 1,
|
|
112
|
+
type: 'bool',
|
|
113
|
+
options: { default: false },
|
|
114
|
+
});
|
|
115
|
+
expect(v2Messages.nested.DeviceSessionGet.fields.session_id).toMatchObject({
|
|
116
|
+
id: 1,
|
|
117
|
+
type: 'bytes',
|
|
118
|
+
});
|
|
119
|
+
expect(v2Messages.nested.DeviceSessionPinType.values).toEqual({
|
|
120
|
+
Any: 1,
|
|
121
|
+
Main: 2,
|
|
122
|
+
AttachToPin: 3,
|
|
123
|
+
});
|
|
124
|
+
expect(v2Messages.nested.DeviceSessionErrorCode.values).toEqual({
|
|
125
|
+
DeviceSessionError_None: 0,
|
|
126
|
+
DeviceSessionError_UserCancelled: 1,
|
|
127
|
+
DeviceSessionError_InvalidSession: 2,
|
|
128
|
+
DeviceSessionError_AttachPinUnavailable: 3,
|
|
129
|
+
DeviceSessionError_PassphraseDisabled: 4,
|
|
130
|
+
DeviceSessionError_Busy: 5,
|
|
131
|
+
});
|
|
132
|
+
expect(v2Messages.nested).not.toHaveProperty('DeviceWalletSelect');
|
|
133
|
+
expect(v2Messages.nested).not.toHaveProperty('DeviceWalletType');
|
|
134
|
+
expect(v2Messages.nested).not.toHaveProperty('DeviceHiddenWalletSelect');
|
|
135
|
+
expect(v2Messages.nested.DeviceSession.fields).toMatchObject({
|
|
136
|
+
session_id: { id: 1, type: 'bytes' },
|
|
137
|
+
btc_test_address: { id: 2, type: 'string' },
|
|
138
|
+
});
|
|
139
|
+
expect(v2Messages.nested.DeviceSessionAskPin.fields.type).toMatchObject({
|
|
140
|
+
id: 1,
|
|
141
|
+
type: 'DeviceSessionPinType',
|
|
142
|
+
});
|
|
143
|
+
expect(v2Messages.nested.DeviceSessionAskPassphrase).toEqual({
|
|
144
|
+
fields: {
|
|
145
|
+
passphrase: {
|
|
146
|
+
type: 'string',
|
|
147
|
+
id: 1,
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
expect(v2Messages.nested.DeviceSessionAskPin_FailureSubCodes.values).toEqual({
|
|
152
|
+
UserCancel: 1,
|
|
153
|
+
});
|
|
154
|
+
expect(v2Messages.nested.MessageType.values).not.toHaveProperty(
|
|
155
|
+
'MessageType_DeviceSessionPinResult'
|
|
156
|
+
);
|
|
157
|
+
expect(v2Messages.nested.MessageType.values).not.toHaveProperty(
|
|
158
|
+
'MessageType_DeviceSessionOpen'
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('Protocol V2 onboarding status matches the current firmware-pro2 schema', () => {
|
|
163
|
+
expect(v2Messages.nested.DevOnboardingStep.values).toMatchObject({
|
|
164
|
+
DEV_ONBOARDING_STEP_UNKNOWN: 0,
|
|
165
|
+
DEV_ONBOARDING_STEP_CHECKING: 1,
|
|
166
|
+
DEV_ONBOARDING_STEP_PERSONALIZATION: 2,
|
|
167
|
+
DEV_ONBOARDING_STEP_PIN: 3,
|
|
168
|
+
DEV_ONBOARDING_STEP_SETUP: 4,
|
|
169
|
+
DEV_ONBOARDING_STEP_DONE: 5,
|
|
170
|
+
});
|
|
171
|
+
expect(v2Messages.nested.DevOnboardingPhase.values).toBeDefined();
|
|
172
|
+
expect(v2Messages.nested.DevOnboardingSetupKind.values).toBeDefined();
|
|
173
|
+
expect(v2Messages.nested.DevOnboardingSetupMethod.values).toBeDefined();
|
|
174
|
+
expect(v2Messages.nested.DevOnboardingSetupStatus.fields).toMatchObject({
|
|
175
|
+
kind: { id: 1, type: 'DevOnboardingSetupKind' },
|
|
176
|
+
method: { id: 2, type: 'DevOnboardingSetupMethod' },
|
|
177
|
+
});
|
|
178
|
+
expect(v2Messages.nested.DevOnboardingStatus.fields).toMatchObject({
|
|
179
|
+
step: { id: 1, type: 'DevOnboardingStep' },
|
|
180
|
+
phase: { id: 2, type: 'DevOnboardingPhase' },
|
|
181
|
+
setup: { id: 3, type: 'DevOnboardingSetupStatus' },
|
|
182
|
+
pin_set: { id: 4, type: 'bool' },
|
|
183
|
+
wallet_initialized: { id: 5, type: 'bool' },
|
|
184
|
+
});
|
|
185
|
+
expect(v2Messages.nested).not.toHaveProperty('DevOnboardingStage');
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test('Protocol V2 does not restore retired unlock or passphrase ids', () => {
|
|
189
|
+
expect(v2Messages.nested.MessageType.values).not.toHaveProperty('MessageType_UnLockDevice');
|
|
190
|
+
expect(v2Messages.nested.MessageType.values).not.toHaveProperty(
|
|
191
|
+
'MessageType_UnLockDeviceResponse'
|
|
192
|
+
);
|
|
193
|
+
expect(v2Messages.nested.MessageType.values).not.toHaveProperty(
|
|
194
|
+
'MessageType_GetPassphraseState'
|
|
195
|
+
);
|
|
196
|
+
expect(v2Messages.nested.MessageType.values).not.toHaveProperty('MessageType_PassphraseState');
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test('Protocol V2 transport and core schemas stay identical', () => {
|
|
200
|
+
expect(coreV2Messages).toEqual(v2Messages);
|
|
201
|
+
});
|
|
202
|
+
|
|
61
203
|
test('createMessageFromName (common case)', () => {
|
|
62
204
|
const messages = parseConfigure(json);
|
|
63
205
|
const name = 'Initialize';
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
const { writeProtocolV2BleFrame } = require('../src');
|
|
3
|
+
|
|
4
|
+
describe('Protocol V2 BLE frame writer', () => {
|
|
5
|
+
test('splits one complete Protocol V2 frame without changing packet order', async () => {
|
|
6
|
+
const packets = [];
|
|
7
|
+
|
|
8
|
+
await writeProtocolV2BleFrame({
|
|
9
|
+
frame: new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
|
|
10
|
+
packetCapacity: 4,
|
|
11
|
+
writePacket: packet => {
|
|
12
|
+
packets.push(Array.from(packet));
|
|
13
|
+
return Promise.resolve();
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
expect(packets).toEqual([
|
|
18
|
+
[0, 1, 2, 3],
|
|
19
|
+
[4, 5, 6, 7],
|
|
20
|
+
[8, 9],
|
|
21
|
+
]);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('stops before the next packet when the connection generation changes', async () => {
|
|
25
|
+
const packets = [];
|
|
26
|
+
let active = true;
|
|
27
|
+
|
|
28
|
+
await expect(
|
|
29
|
+
writeProtocolV2BleFrame({
|
|
30
|
+
frame: new Uint8Array(9),
|
|
31
|
+
packetCapacity: 4,
|
|
32
|
+
assertActive: () => {
|
|
33
|
+
if (!active) throw new Error('stale BLE generation');
|
|
34
|
+
},
|
|
35
|
+
writePacket: packet => {
|
|
36
|
+
packets.push(packet);
|
|
37
|
+
active = false;
|
|
38
|
+
return Promise.resolve();
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
).rejects.toThrow('stale BLE generation');
|
|
42
|
+
|
|
43
|
+
expect(packets).toHaveLength(1);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('does not write when the call is already aborted', async () => {
|
|
47
|
+
const controller = new AbortController();
|
|
48
|
+
const writePacket = jest.fn();
|
|
49
|
+
controller.abort();
|
|
50
|
+
|
|
51
|
+
await expect(
|
|
52
|
+
writeProtocolV2BleFrame({
|
|
53
|
+
frame: new Uint8Array(4),
|
|
54
|
+
packetCapacity: 4,
|
|
55
|
+
signal: controller.signal,
|
|
56
|
+
abortMessage: 'Protocol V2 BLE write aborted for Ping',
|
|
57
|
+
writePacket,
|
|
58
|
+
})
|
|
59
|
+
).rejects.toThrow('Protocol V2 BLE write aborted for Ping');
|
|
60
|
+
|
|
61
|
+
expect(writePacket).not.toHaveBeenCalled();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('surfaces an abort that occurs while the final packet write is completing', async () => {
|
|
65
|
+
const controller = new AbortController();
|
|
66
|
+
const writePacket = jest.fn().mockImplementation(() => {
|
|
67
|
+
controller.abort();
|
|
68
|
+
return Promise.resolve();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
await expect(
|
|
72
|
+
writeProtocolV2BleFrame({
|
|
73
|
+
frame: new Uint8Array(4),
|
|
74
|
+
packetCapacity: 4,
|
|
75
|
+
signal: controller.signal,
|
|
76
|
+
abortMessage: 'Protocol V2 BLE write aborted for Ping',
|
|
77
|
+
writePacket,
|
|
78
|
+
})
|
|
79
|
+
).rejects.toThrow('Protocol V2 BLE write aborted for Ping');
|
|
80
|
+
|
|
81
|
+
expect(writePacket).toHaveBeenCalledTimes(1);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('applies bounded burst pauses and one final flush delay', async () => {
|
|
85
|
+
const waits = [];
|
|
86
|
+
const writePacket = jest.fn().mockResolvedValue(undefined);
|
|
87
|
+
|
|
88
|
+
await writeProtocolV2BleFrame({
|
|
89
|
+
frame: new Uint8Array(10),
|
|
90
|
+
packetCapacity: 3,
|
|
91
|
+
initialDelayMs: 3,
|
|
92
|
+
burstSize: 2,
|
|
93
|
+
burstPauseMs: 7,
|
|
94
|
+
flushDelayMs: 11,
|
|
95
|
+
wait: ms => {
|
|
96
|
+
waits.push(ms);
|
|
97
|
+
return Promise.resolve();
|
|
98
|
+
},
|
|
99
|
+
writePacket,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
expect(writePacket).toHaveBeenCalledTimes(4);
|
|
103
|
+
expect(waits).toEqual([3, 7, 11]);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('rejects an invalid packet capacity before writing', async () => {
|
|
107
|
+
const writePacket = jest.fn();
|
|
108
|
+
|
|
109
|
+
await expect(
|
|
110
|
+
writeProtocolV2BleFrame({
|
|
111
|
+
frame: new Uint8Array(4),
|
|
112
|
+
packetCapacity: 0,
|
|
113
|
+
writePacket,
|
|
114
|
+
})
|
|
115
|
+
).rejects.toThrow('Protocol V2 BLE packet capacity must be a positive integer');
|
|
116
|
+
|
|
117
|
+
expect(writePacket).not.toHaveBeenCalled();
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
const { ProtocolV2, ProtocolV2LinkManager } = require('../src');
|
|
3
|
+
const { parseConfigure } = require('../src/serialization/protobuf/messages');
|
|
4
|
+
const protocolV2 = require('../src/protocols/v2');
|
|
5
|
+
|
|
6
|
+
const protocolV1Messages = parseConfigure({
|
|
7
|
+
nested: {
|
|
8
|
+
Failure: {
|
|
9
|
+
fields: {
|
|
10
|
+
code: { type: 'uint32', id: 1 },
|
|
11
|
+
message: { type: 'string', id: 2 },
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
MessageType: {
|
|
15
|
+
values: {
|
|
16
|
+
MessageType_Failure: 3,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const protocolV2Messages = parseConfigure({
|
|
23
|
+
nested: {
|
|
24
|
+
Ping: {
|
|
25
|
+
fields: {
|
|
26
|
+
message: { type: 'string', id: 1 },
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
Success: {
|
|
30
|
+
fields: {
|
|
31
|
+
message: { type: 'string', id: 1 },
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
MessageType: {
|
|
35
|
+
values: {
|
|
36
|
+
MessageType_Ping: 60206,
|
|
37
|
+
MessageType_Success: 60207,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const schemas = {
|
|
44
|
+
protocolV1: protocolV1Messages,
|
|
45
|
+
protocolV2: protocolV2Messages,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const rewriteSeq = (frame, seq) => {
|
|
49
|
+
const copy = new Uint8Array(frame);
|
|
50
|
+
copy[6] = seq;
|
|
51
|
+
copy[copy.length - 1] = protocolV2.crc8(copy, copy.length - 1);
|
|
52
|
+
return copy;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const createAdapterFactory = sentSeqs => {
|
|
56
|
+
const adapters = [];
|
|
57
|
+
let generation = 0;
|
|
58
|
+
const success = ProtocolV2.encodeFrame(
|
|
59
|
+
schemas,
|
|
60
|
+
'Success',
|
|
61
|
+
{ message: 'ok' },
|
|
62
|
+
{ router: 1, packetSrc: 0, seq: 1 }
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
adapters,
|
|
67
|
+
createAdapter: () => {
|
|
68
|
+
let requestSeq = 0;
|
|
69
|
+
const adapter = {
|
|
70
|
+
router: 1,
|
|
71
|
+
generation: ++generation,
|
|
72
|
+
prepareCall: jest.fn(),
|
|
73
|
+
writeFrame: jest.fn(frame => {
|
|
74
|
+
const [, , , , , , seq] = frame;
|
|
75
|
+
requestSeq = seq;
|
|
76
|
+
sentSeqs.push(requestSeq);
|
|
77
|
+
return Promise.resolve();
|
|
78
|
+
}),
|
|
79
|
+
readFrame: jest.fn(() => Promise.resolve(rewriteSeq(success, requestSeq))),
|
|
80
|
+
reset: jest.fn(() => Promise.resolve()),
|
|
81
|
+
};
|
|
82
|
+
adapters.push(adapter);
|
|
83
|
+
return adapter;
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
describe('ProtocolV2LinkManager', () => {
|
|
89
|
+
test('retains the device sequence cursor when an active link is rebuilt', async () => {
|
|
90
|
+
const sentSeqs = [];
|
|
91
|
+
const { adapters, createAdapter } = createAdapterFactory(sentSeqs);
|
|
92
|
+
const manager = new ProtocolV2LinkManager({
|
|
93
|
+
getSchemas: () => schemas,
|
|
94
|
+
classifyError: () => 'recoverable',
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
await manager.call('device-a', createAdapter, 'Ping', { message: '1' });
|
|
98
|
+
await manager.invalidateLink('device-a', 'reconnect');
|
|
99
|
+
await manager.call('device-a', createAdapter, 'Ping', { message: '2' });
|
|
100
|
+
|
|
101
|
+
expect(sentSeqs).toEqual([1, 2]);
|
|
102
|
+
expect(adapters).toHaveLength(2);
|
|
103
|
+
expect(adapters[0].reset).toHaveBeenCalledWith('reconnect');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('prepares the active adapter with the same call context used for frame IO', async () => {
|
|
107
|
+
const sentSeqs = [];
|
|
108
|
+
const { adapters, createAdapter } = createAdapterFactory(sentSeqs);
|
|
109
|
+
const manager = new ProtocolV2LinkManager({
|
|
110
|
+
getSchemas: () => schemas,
|
|
111
|
+
classifyError: () => 'recoverable',
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
await manager.call('device-a', createAdapter, 'Ping', { message: '1' }, { timeoutMs: 123 });
|
|
115
|
+
|
|
116
|
+
expect(adapters[0].prepareCall).toHaveBeenCalledWith(
|
|
117
|
+
expect.objectContaining({
|
|
118
|
+
messageName: 'Ping',
|
|
119
|
+
timeoutMs: 123,
|
|
120
|
+
highVolume: false,
|
|
121
|
+
generation: 1,
|
|
122
|
+
signal: expect.any(AbortSignal),
|
|
123
|
+
})
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test('invalidates a link-fatal call before the next call creates a new link', async () => {
|
|
128
|
+
const sentSeqs = [];
|
|
129
|
+
const onLinkInvalidated = jest.fn();
|
|
130
|
+
const { adapters, createAdapter: createWorkingAdapter } = createAdapterFactory(sentSeqs);
|
|
131
|
+
const createAdapter = jest
|
|
132
|
+
.fn()
|
|
133
|
+
.mockImplementationOnce(() => {
|
|
134
|
+
const adapter = createWorkingAdapter();
|
|
135
|
+
adapter.writeFrame.mockImplementationOnce(frame => {
|
|
136
|
+
sentSeqs.push(frame[6]);
|
|
137
|
+
return Promise.reject(new Error('transport write failed'));
|
|
138
|
+
});
|
|
139
|
+
return adapter;
|
|
140
|
+
})
|
|
141
|
+
.mockImplementation(() => createWorkingAdapter());
|
|
142
|
+
const manager = new ProtocolV2LinkManager({
|
|
143
|
+
getSchemas: () => schemas,
|
|
144
|
+
classifyError: () => 'link-fatal',
|
|
145
|
+
onLinkInvalidated,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
await expect(manager.call('device-a', createAdapter, 'Ping', { message: '1' })).rejects.toThrow(
|
|
149
|
+
'transport write failed'
|
|
150
|
+
);
|
|
151
|
+
await expect(
|
|
152
|
+
manager.call('device-a', createAdapter, 'Ping', { message: '2' })
|
|
153
|
+
).resolves.toEqual({
|
|
154
|
+
type: 'Success',
|
|
155
|
+
message: { message: 'ok' },
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
expect(createAdapter).toHaveBeenCalledTimes(2);
|
|
159
|
+
expect(adapters[0].reset).toHaveBeenCalledWith(
|
|
160
|
+
expect.stringContaining('transport write failed')
|
|
161
|
+
);
|
|
162
|
+
expect(onLinkInvalidated).toHaveBeenCalledWith(
|
|
163
|
+
'device-a',
|
|
164
|
+
expect.stringContaining('transport write failed')
|
|
165
|
+
);
|
|
166
|
+
expect(sentSeqs).toEqual([1, 2]);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('invalidates every active link while retaining per-device cursors', async () => {
|
|
170
|
+
const sentSeqs = [];
|
|
171
|
+
const { adapters, createAdapter } = createAdapterFactory(sentSeqs);
|
|
172
|
+
const manager = new ProtocolV2LinkManager({
|
|
173
|
+
getSchemas: () => schemas,
|
|
174
|
+
classifyError: () => 'recoverable',
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
await manager.call('device-a', createAdapter, 'Ping', { message: 'a1' });
|
|
178
|
+
await manager.call('device-b', createAdapter, 'Ping', { message: 'b1' });
|
|
179
|
+
await manager.invalidateAllLinks('schema changed');
|
|
180
|
+
await manager.call('device-a', createAdapter, 'Ping', { message: 'a2' });
|
|
181
|
+
await manager.call('device-b', createAdapter, 'Ping', { message: 'b2' });
|
|
182
|
+
|
|
183
|
+
expect(sentSeqs).toEqual([1, 1, 2, 2]);
|
|
184
|
+
expect(adapters).toHaveLength(4);
|
|
185
|
+
expect(adapters[0].reset).toHaveBeenCalledWith('schema changed');
|
|
186
|
+
expect(adapters[1].reset).toHaveBeenCalledWith('schema changed');
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('dispose clears active links and sequence cursors', async () => {
|
|
190
|
+
const sentSeqs = [];
|
|
191
|
+
const { adapters, createAdapter } = createAdapterFactory(sentSeqs);
|
|
192
|
+
const manager = new ProtocolV2LinkManager({
|
|
193
|
+
getSchemas: () => schemas,
|
|
194
|
+
classifyError: () => 'recoverable',
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
await manager.call('device-a', createAdapter, 'Ping', { message: '1' });
|
|
198
|
+
await manager.dispose('transport disposed');
|
|
199
|
+
await manager.call('device-a', createAdapter, 'Ping', { message: '2' });
|
|
200
|
+
|
|
201
|
+
expect(sentSeqs).toEqual([1, 1]);
|
|
202
|
+
expect(adapters[0].reset).toHaveBeenCalledWith('transport disposed');
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test('serializes calls per device without blocking another device', async () => {
|
|
206
|
+
const events = [];
|
|
207
|
+
let releaseDeviceA;
|
|
208
|
+
const deviceABlocked = new Promise(resolve => {
|
|
209
|
+
releaseDeviceA = resolve;
|
|
210
|
+
});
|
|
211
|
+
const success = ProtocolV2.encodeFrame(
|
|
212
|
+
schemas,
|
|
213
|
+
'Success',
|
|
214
|
+
{ message: 'ok' },
|
|
215
|
+
{ router: 1, packetSrc: 0, seq: 1 }
|
|
216
|
+
);
|
|
217
|
+
const createAdapter = key => {
|
|
218
|
+
let requestSeq = 0;
|
|
219
|
+
return {
|
|
220
|
+
router: 1,
|
|
221
|
+
generation: 1,
|
|
222
|
+
prepareCall: jest.fn(),
|
|
223
|
+
writeFrame: jest.fn(frame => {
|
|
224
|
+
const [, , , , , , seq] = frame;
|
|
225
|
+
requestSeq = seq;
|
|
226
|
+
events.push(`write:${key}:${requestSeq}`);
|
|
227
|
+
return Promise.resolve();
|
|
228
|
+
}),
|
|
229
|
+
readFrame: jest.fn(async () => {
|
|
230
|
+
if (key === 'device-a' && requestSeq === 1) {
|
|
231
|
+
await deviceABlocked;
|
|
232
|
+
}
|
|
233
|
+
events.push(`read:${key}:${requestSeq}`);
|
|
234
|
+
return rewriteSeq(success, requestSeq);
|
|
235
|
+
}),
|
|
236
|
+
reset: jest.fn(),
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
const manager = new ProtocolV2LinkManager({
|
|
240
|
+
getSchemas: () => schemas,
|
|
241
|
+
classifyError: () => 'recoverable',
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
const firstA = manager.call('device-a', () => createAdapter('device-a'), 'Ping', {
|
|
245
|
+
message: 'a1',
|
|
246
|
+
});
|
|
247
|
+
const secondA = manager.call('device-a', () => createAdapter('device-a'), 'Ping', {
|
|
248
|
+
message: 'a2',
|
|
249
|
+
});
|
|
250
|
+
const firstB = manager.call('device-b', () => createAdapter('device-b'), 'Ping', {
|
|
251
|
+
message: 'b1',
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
await firstB;
|
|
255
|
+
expect(events).toEqual(['write:device-a:1', 'write:device-b:1', 'read:device-b:1']);
|
|
256
|
+
releaseDeviceA();
|
|
257
|
+
await Promise.all([firstA, secondA]);
|
|
258
|
+
expect(events).toEqual([
|
|
259
|
+
'write:device-a:1',
|
|
260
|
+
'write:device-b:1',
|
|
261
|
+
'read:device-b:1',
|
|
262
|
+
'read:device-a:1',
|
|
263
|
+
'write:device-a:2',
|
|
264
|
+
'read:device-a:2',
|
|
265
|
+
]);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test('keeps a recoverable link after a call error', async () => {
|
|
269
|
+
const sentSeqs = [];
|
|
270
|
+
const { adapters, createAdapter } = createAdapterFactory(sentSeqs);
|
|
271
|
+
const manager = new ProtocolV2LinkManager({
|
|
272
|
+
getSchemas: () => schemas,
|
|
273
|
+
classifyError: () => 'recoverable',
|
|
274
|
+
});
|
|
275
|
+
const adapter = createAdapter();
|
|
276
|
+
adapter.writeFrame.mockImplementationOnce(frame => {
|
|
277
|
+
sentSeqs.push(frame[6]);
|
|
278
|
+
return Promise.reject(new Error('application retryable error'));
|
|
279
|
+
});
|
|
280
|
+
const factory = jest.fn(() => adapter);
|
|
281
|
+
|
|
282
|
+
await expect(manager.call('device-a', factory, 'Ping', { message: '1' })).rejects.toThrow(
|
|
283
|
+
'application retryable error'
|
|
284
|
+
);
|
|
285
|
+
await manager.call('device-a', factory, 'Ping', { message: '2' });
|
|
286
|
+
|
|
287
|
+
expect(factory).toHaveBeenCalledTimes(1);
|
|
288
|
+
expect(adapter.reset).not.toHaveBeenCalled();
|
|
289
|
+
expect(sentSeqs).toEqual([1, 2]);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test('removes a settled per-device call queue', async () => {
|
|
293
|
+
const sentSeqs = [];
|
|
294
|
+
const { createAdapter } = createAdapterFactory(sentSeqs);
|
|
295
|
+
const manager = new ProtocolV2LinkManager({
|
|
296
|
+
getSchemas: () => schemas,
|
|
297
|
+
classifyError: () => 'recoverable',
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
await manager.call('device-a', createAdapter, 'Ping', { message: 'done' });
|
|
301
|
+
|
|
302
|
+
expect(manager.callQueues.size).toBe(0);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test('stops an in-flight call before reading when its link is invalidated during write', async () => {
|
|
306
|
+
let releaseWrite;
|
|
307
|
+
let markWriteStarted;
|
|
308
|
+
const writeStarted = new Promise(resolve => {
|
|
309
|
+
markWriteStarted = resolve;
|
|
310
|
+
});
|
|
311
|
+
const writeBlocked = new Promise(resolve => {
|
|
312
|
+
releaseWrite = resolve;
|
|
313
|
+
});
|
|
314
|
+
const success = ProtocolV2.encodeFrame(
|
|
315
|
+
schemas,
|
|
316
|
+
'Success',
|
|
317
|
+
{ message: 'ok' },
|
|
318
|
+
{ router: 1, packetSrc: 0, seq: 1 }
|
|
319
|
+
);
|
|
320
|
+
const adapter = {
|
|
321
|
+
router: 1,
|
|
322
|
+
generation: 1,
|
|
323
|
+
prepareCall: jest.fn(),
|
|
324
|
+
writeFrame: jest.fn(async () => {
|
|
325
|
+
markWriteStarted();
|
|
326
|
+
await writeBlocked;
|
|
327
|
+
}),
|
|
328
|
+
readFrame: jest.fn(() => Promise.resolve(success)),
|
|
329
|
+
reset: jest.fn(),
|
|
330
|
+
};
|
|
331
|
+
const manager = new ProtocolV2LinkManager({
|
|
332
|
+
getSchemas: () => schemas,
|
|
333
|
+
classifyError: () => 'recoverable',
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
const call = manager.call('device-a', () => adapter, 'Ping', { message: 'pending' });
|
|
337
|
+
await writeStarted;
|
|
338
|
+
await manager.invalidateLink('device-a', 'device disconnected');
|
|
339
|
+
releaseWrite();
|
|
340
|
+
|
|
341
|
+
await expect(call).rejects.toThrow('device disconnected');
|
|
342
|
+
expect(adapter.readFrame).not.toHaveBeenCalled();
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
test('rejects calls queued before their link generation is invalidated', async () => {
|
|
346
|
+
let releaseWrite;
|
|
347
|
+
let markWriteStarted;
|
|
348
|
+
const writeStarted = new Promise(resolve => {
|
|
349
|
+
markWriteStarted = resolve;
|
|
350
|
+
});
|
|
351
|
+
const writeBlocked = new Promise(resolve => {
|
|
352
|
+
releaseWrite = resolve;
|
|
353
|
+
});
|
|
354
|
+
const success = ProtocolV2.encodeFrame(
|
|
355
|
+
schemas,
|
|
356
|
+
'Success',
|
|
357
|
+
{ message: 'ok' },
|
|
358
|
+
{ router: 1, packetSrc: 0, seq: 1 }
|
|
359
|
+
);
|
|
360
|
+
const adapter = {
|
|
361
|
+
router: 1,
|
|
362
|
+
generation: 1,
|
|
363
|
+
prepareCall: jest.fn(),
|
|
364
|
+
writeFrame: jest.fn(async () => {
|
|
365
|
+
markWriteStarted();
|
|
366
|
+
await writeBlocked;
|
|
367
|
+
}),
|
|
368
|
+
readFrame: jest.fn(() => Promise.resolve(success)),
|
|
369
|
+
reset: jest.fn(),
|
|
370
|
+
};
|
|
371
|
+
const createAdapter = jest.fn(() => adapter);
|
|
372
|
+
const manager = new ProtocolV2LinkManager({
|
|
373
|
+
getSchemas: () => schemas,
|
|
374
|
+
classifyError: () => 'recoverable',
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
const activeCall = manager.call('device-a', createAdapter, 'Ping', { message: 'active' });
|
|
378
|
+
await writeStarted;
|
|
379
|
+
const queuedCall = manager.call('device-a', createAdapter, 'Ping', { message: 'queued' });
|
|
380
|
+
await manager.invalidateLink('device-a', 'device released');
|
|
381
|
+
releaseWrite();
|
|
382
|
+
|
|
383
|
+
await expect(activeCall).rejects.toThrow('device released');
|
|
384
|
+
await expect(queuedCall).rejects.toThrow('device released');
|
|
385
|
+
expect(createAdapter).toHaveBeenCalledTimes(1);
|
|
386
|
+
expect(adapter.writeFrame).toHaveBeenCalledTimes(1);
|
|
387
|
+
expect(adapter.readFrame).not.toHaveBeenCalled();
|
|
388
|
+
});
|
|
389
|
+
});
|