dash-platform-sdk 1.6.1 → 1.6.3
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 +14 -14
- package/bundle.min.js +1 -1
- package/package.json +1 -1
- package/src/DashPlatformSDK.d.ts +6 -0
- package/src/DashPlatformSDK.js +8 -2
- package/src/documents/index.d.ts +3 -3
- package/src/documents/index.js +3 -3
- package/src/documents/query.d.ts +2 -2
- package/src/documents/query.js +6 -4
- package/src/grpcConnectionPool.d.ts +6 -0
- package/src/grpcConnectionPool.js +168 -59
- package/src/names/index.d.ts +2 -0
- package/src/names/index.js +13 -2
- package/src/names/searchByIdentity.d.ts +2 -2
- package/src/names/searchByIdentity.js +2 -3
- package/src/names/searchByName.d.ts +2 -2
- package/src/names/searchByName.js +2 -3
- package/test/unit/Document.spec.js +8 -0
- package/test/unit/GRPCConnectionPool.spec.d.ts +1 -0
- package/test/unit/GRPCConnectionPool.spec.js +277 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import GRPCConnectionPool from '../../src/grpcConnectionPool.js';
|
|
2
|
+
import { DashPlatformSDK } from '../../src/DashPlatformSDK.js';
|
|
3
|
+
jest.mock('@protobuf-ts/grpcweb-transport', () => ({
|
|
4
|
+
GrpcWebFetchTransport: class {
|
|
5
|
+
options;
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.options = options;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
}));
|
|
11
|
+
jest.mock('../../proto/generated/platform.client.js', () => ({
|
|
12
|
+
PlatformClient: class {
|
|
13
|
+
url;
|
|
14
|
+
fetch;
|
|
15
|
+
signal;
|
|
16
|
+
constructor(transport) {
|
|
17
|
+
this.url = transport.options.baseUrl;
|
|
18
|
+
this.fetch = transport.options.fetch;
|
|
19
|
+
this.signal = transport.options.abort;
|
|
20
|
+
}
|
|
21
|
+
async getStatus() {
|
|
22
|
+
await this.fetch(`${this.url}/org.dash.platform.dapi.v0.Platform/getStatus`);
|
|
23
|
+
return {
|
|
24
|
+
response: {
|
|
25
|
+
version: {
|
|
26
|
+
oneofKind: 'v0',
|
|
27
|
+
v0: {
|
|
28
|
+
version: { protocol: { drive: { current: 14 } } },
|
|
29
|
+
chain: { catchingUp: false },
|
|
30
|
+
time: { local: BigInt(Math.floor(Date.now() / 1000)), block: BigInt(Date.now() - 1000) }
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}));
|
|
38
|
+
const NODE_A = 'https://10.0.0.1:1443';
|
|
39
|
+
const NODE_B = 'https://10.0.0.2:1443';
|
|
40
|
+
const METHOD_PATH = '/org.dash.platform.dapi.v0.Platform/getStatus';
|
|
41
|
+
const BROADCAST_PATH = '/org.dash.platform.dapi.v0.Platform/broadcastStateTransition';
|
|
42
|
+
const STREAM_PATH = '/org.dash.platform.dapi.v0.Platform/subscribeToBlockHeadersWithChainLocks';
|
|
43
|
+
const LONG_POLL_PATH = '/org.dash.platform.dapi.v0.Platform/waitForStateTransitionResult';
|
|
44
|
+
const OK_RESPONSE = { ok: true };
|
|
45
|
+
const TESTNET_SEED_A = 'https://153.80.206.232:1443';
|
|
46
|
+
const waitFor = async (condition) => {
|
|
47
|
+
for (let i = 0; i < 100; i++) {
|
|
48
|
+
if (condition()) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
await new Promise(resolve => setImmediate(resolve));
|
|
52
|
+
}
|
|
53
|
+
throw new Error('Condition was not met');
|
|
54
|
+
};
|
|
55
|
+
const abortError = () => Object.assign(new Error('The operation was aborted'), { name: 'AbortError' });
|
|
56
|
+
const connectError = () => Object.assign(new TypeError('fetch failed'), { cause: Object.assign(new Error('ECONNREFUSED'), { code: 'ECONNREFUSED' }) });
|
|
57
|
+
const hangingFetch = (fetchSpy) => {
|
|
58
|
+
fetchSpy.mockImplementation(async (_url, init) => await new Promise((_resolve, reject) => {
|
|
59
|
+
init?.signal?.addEventListener('abort', () => reject(abortError()));
|
|
60
|
+
}));
|
|
61
|
+
};
|
|
62
|
+
describe('GRPCConnectionPool', () => {
|
|
63
|
+
let fetchSpy;
|
|
64
|
+
beforeEach(() => {
|
|
65
|
+
fetchSpy = jest.spyOn(globalThis, 'fetch');
|
|
66
|
+
});
|
|
67
|
+
afterEach(() => {
|
|
68
|
+
jest.restoreAllMocks();
|
|
69
|
+
jest.useRealTimers();
|
|
70
|
+
});
|
|
71
|
+
describe('getClient', () => {
|
|
72
|
+
it('should round-robin over the nodes', async () => {
|
|
73
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: [NODE_A, NODE_B] });
|
|
74
|
+
const urls = [];
|
|
75
|
+
for (let i = 0; i < 4; i++) {
|
|
76
|
+
urls.push((await pool.getClient()).url);
|
|
77
|
+
}
|
|
78
|
+
expect(urls).toEqual([NODE_A, NODE_B, NODE_A, NODE_B]);
|
|
79
|
+
});
|
|
80
|
+
it('should pass the abort controller to the client', async () => {
|
|
81
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: NODE_A });
|
|
82
|
+
const abortController = new AbortController();
|
|
83
|
+
const client = await pool.getClient(abortController);
|
|
84
|
+
expect(client.signal).toBe(abortController.signal);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
describe('seed bootstrap', () => {
|
|
88
|
+
it('should resolve immediately with the primary seed and healthcheck in the background', async () => {
|
|
89
|
+
let release = () => { };
|
|
90
|
+
const gate = new Promise(resolve => { release = () => resolve(OK_RESPONSE); });
|
|
91
|
+
fetchSpy.mockImplementation(async () => await gate);
|
|
92
|
+
const pool = new GRPCConnectionPool('testnet');
|
|
93
|
+
await pool.getClient();
|
|
94
|
+
// no waiting for a healthcheck: the first request can start right away
|
|
95
|
+
expect(pool.dapiUrls).toEqual([TESTNET_SEED_A]);
|
|
96
|
+
expect(fetchSpy).toHaveBeenCalled();
|
|
97
|
+
release();
|
|
98
|
+
await waitFor(() => pool.dapiUrls.length >= 2);
|
|
99
|
+
expect(pool.dapiUrls[0]).toBe(TESTNET_SEED_A);
|
|
100
|
+
});
|
|
101
|
+
it('should promote a fallback seed when the active node is banned', async () => {
|
|
102
|
+
let release = () => { };
|
|
103
|
+
const gate = new Promise(resolve => { release = () => resolve(OK_RESPONSE); });
|
|
104
|
+
fetchSpy.mockImplementation(async () => await gate);
|
|
105
|
+
const pool = new GRPCConnectionPool('testnet');
|
|
106
|
+
const client = await pool.getClient();
|
|
107
|
+
expect(pool.dapiUrls).toEqual([TESTNET_SEED_A]);
|
|
108
|
+
pool.banNode(client);
|
|
109
|
+
const next = await pool.getClient();
|
|
110
|
+
expect(next.url).not.toBe(TESTNET_SEED_A);
|
|
111
|
+
expect(pool.dapiUrls).toContain(next.url);
|
|
112
|
+
release();
|
|
113
|
+
await new Promise(resolve => setImmediate(resolve));
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
describe('fetch retries', () => {
|
|
117
|
+
it('should retry a failed request on the next node and ban the failed one', async () => {
|
|
118
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: [NODE_A, NODE_B] });
|
|
119
|
+
const client = await pool.getClient();
|
|
120
|
+
fetchSpy
|
|
121
|
+
.mockRejectedValueOnce(connectError())
|
|
122
|
+
.mockResolvedValueOnce(OK_RESPONSE);
|
|
123
|
+
const response = await client.fetch(`${NODE_A}${METHOD_PATH}`, { method: 'POST' });
|
|
124
|
+
expect(response).toEqual({ ok: true });
|
|
125
|
+
expect(fetchSpy.mock.calls.map(([url]) => url)).toEqual([`${NODE_A}${METHOD_PATH}`, `${NODE_B}${METHOD_PATH}`]);
|
|
126
|
+
expect((await pool.getClient()).url).toBe(NODE_B);
|
|
127
|
+
});
|
|
128
|
+
it('should not retry abort errors', async () => {
|
|
129
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: [NODE_A, NODE_B] });
|
|
130
|
+
const client = await pool.getClient();
|
|
131
|
+
fetchSpy.mockRejectedValueOnce(abortError());
|
|
132
|
+
await expect(client.fetch(`${NODE_A}${METHOD_PATH}`)).rejects.toThrow('The operation was aborted');
|
|
133
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
describe('request deadline', () => {
|
|
137
|
+
it('should abort a silent node after requestTimeout and retry on the next node', async () => {
|
|
138
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: [NODE_A, NODE_B], requestTimeout: 30 });
|
|
139
|
+
const client = await pool.getClient();
|
|
140
|
+
fetchSpy.mockImplementation(async (url, init) => {
|
|
141
|
+
if (String(url).startsWith(NODE_A)) {
|
|
142
|
+
return await new Promise((_resolve, reject) => {
|
|
143
|
+
init?.signal?.addEventListener('abort', () => reject(abortError()));
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
return OK_RESPONSE;
|
|
147
|
+
});
|
|
148
|
+
const response = await client.fetch(`${NODE_A}${METHOD_PATH}`);
|
|
149
|
+
expect(response).toEqual({ ok: true });
|
|
150
|
+
expect(fetchSpy).toHaveBeenCalledTimes(2);
|
|
151
|
+
// the abandoned request is aborted, freeing the connection
|
|
152
|
+
expect(fetchSpy.mock.calls[0][1]?.signal?.aborted).toBe(true);
|
|
153
|
+
// the silent node is banned
|
|
154
|
+
expect((await pool.getClient()).url).toBe(NODE_B);
|
|
155
|
+
}, 2000);
|
|
156
|
+
it('should abort a silent node after the default 10s deadline', async () => {
|
|
157
|
+
jest.useFakeTimers();
|
|
158
|
+
try {
|
|
159
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: [NODE_A, NODE_B] });
|
|
160
|
+
const client = await pool.getClient();
|
|
161
|
+
fetchSpy.mockImplementation(async (url, init) => {
|
|
162
|
+
if (String(url).startsWith(NODE_A)) {
|
|
163
|
+
return await new Promise((_resolve, reject) => {
|
|
164
|
+
init?.signal?.addEventListener('abort', () => reject(abortError()));
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
return OK_RESPONSE;
|
|
168
|
+
});
|
|
169
|
+
const request = client.fetch(`${NODE_A}${METHOD_PATH}`);
|
|
170
|
+
await jest.advanceTimersByTimeAsync(10_000);
|
|
171
|
+
await jest.advanceTimersByTimeAsync(10);
|
|
172
|
+
await expect(request).resolves.toEqual({ ok: true });
|
|
173
|
+
}
|
|
174
|
+
finally {
|
|
175
|
+
jest.useRealTimers();
|
|
176
|
+
}
|
|
177
|
+
}, 2000);
|
|
178
|
+
it('should abort the underlying fetch when the caller aborts, without retrying or banning', async () => {
|
|
179
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: [NODE_A, NODE_B], requestTimeout: 5000 });
|
|
180
|
+
const client = await pool.getClient();
|
|
181
|
+
const abortController = new AbortController();
|
|
182
|
+
hangingFetch(fetchSpy);
|
|
183
|
+
const request = client.fetch(`${NODE_A}${METHOD_PATH}`, { signal: abortController.signal });
|
|
184
|
+
abortController.abort();
|
|
185
|
+
await expect(request).rejects.toThrow('The operation was aborted');
|
|
186
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
187
|
+
// the caller's signal is wrapped, but its abort still reaches the fetch
|
|
188
|
+
expect(fetchSpy.mock.calls[0][1]?.signal).not.toBe(abortController.signal);
|
|
189
|
+
expect(fetchSpy.mock.calls[0][1]?.signal?.aborted).toBe(true);
|
|
190
|
+
// NODE_A is not banned
|
|
191
|
+
expect((await pool.getClient()).url).toBe(NODE_B);
|
|
192
|
+
expect((await pool.getClient()).url).toBe(NODE_A);
|
|
193
|
+
}, 2000);
|
|
194
|
+
it('should not apply the deadline to streams', async () => {
|
|
195
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: NODE_A, requestTimeout: 30 });
|
|
196
|
+
const client = await pool.getClient();
|
|
197
|
+
let resolveStream = () => { };
|
|
198
|
+
fetchSpy.mockImplementation(async () => await new Promise(resolve => {
|
|
199
|
+
resolveStream = resolve;
|
|
200
|
+
}));
|
|
201
|
+
const request = client.fetch(`${NODE_A}${STREAM_PATH}`);
|
|
202
|
+
await new Promise(resolve => setTimeout(resolve, 80));
|
|
203
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
204
|
+
expect(fetchSpy.mock.calls[0][1]?.signal?.aborted ?? false).toBe(false);
|
|
205
|
+
resolveStream(OK_RESPONSE);
|
|
206
|
+
await expect(request).resolves.toEqual({ ok: true });
|
|
207
|
+
}, 2000);
|
|
208
|
+
it('should not apply the deadline to waitForStateTransitionResult', async () => {
|
|
209
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: NODE_A, requestTimeout: 30 });
|
|
210
|
+
const client = await pool.getClient();
|
|
211
|
+
let resolveLongPoll = () => { };
|
|
212
|
+
fetchSpy.mockImplementation(async () => await new Promise(resolve => {
|
|
213
|
+
resolveLongPoll = resolve;
|
|
214
|
+
}));
|
|
215
|
+
const request = client.fetch(`${NODE_A}${LONG_POLL_PATH}`);
|
|
216
|
+
await new Promise(resolve => setTimeout(resolve, 80));
|
|
217
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
218
|
+
expect(fetchSpy.mock.calls[0][1]?.signal?.aborted ?? false).toBe(false);
|
|
219
|
+
resolveLongPoll(OK_RESPONSE);
|
|
220
|
+
await expect(request).resolves.toEqual({ ok: true });
|
|
221
|
+
}, 2000);
|
|
222
|
+
it('should not retry a broadcast that hit the deadline', async () => {
|
|
223
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: [NODE_A, NODE_B], requestTimeout: 30 });
|
|
224
|
+
const client = await pool.getClient();
|
|
225
|
+
hangingFetch(fetchSpy);
|
|
226
|
+
await expect(client.fetch(`${NODE_A}${BROADCAST_PATH}`)).rejects.toMatchObject({ name: 'TimeoutError', message: 'Request timed out after 30ms' });
|
|
227
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
228
|
+
}, 2000);
|
|
229
|
+
it('should reject with a timeout error when the deadline exhausts the retries', async () => {
|
|
230
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: [NODE_A, NODE_B], retries: 1, requestTimeout: 30 });
|
|
231
|
+
const client = await pool.getClient();
|
|
232
|
+
hangingFetch(fetchSpy);
|
|
233
|
+
await expect(client.fetch(`${NODE_A}${METHOD_PATH}`)).rejects.toMatchObject({ name: 'TimeoutError', message: 'Request timed out after 30ms' });
|
|
234
|
+
}, 2000);
|
|
235
|
+
it('should reject with a timeout error when no other node is available', async () => {
|
|
236
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: NODE_A, requestTimeout: 30 });
|
|
237
|
+
const client = await pool.getClient();
|
|
238
|
+
hangingFetch(fetchSpy);
|
|
239
|
+
await expect(client.fetch(`${NODE_A}${METHOD_PATH}`)).rejects.toMatchObject({ name: 'TimeoutError', message: 'Request timed out after 30ms' });
|
|
240
|
+
}, 2000);
|
|
241
|
+
it('should reset the ban escalation after a successful request', async () => {
|
|
242
|
+
const now = jest.spyOn(Date, 'now').mockReturnValue(1_000_000);
|
|
243
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: [NODE_A, NODE_B] });
|
|
244
|
+
pool.banNode(await pool.getClient());
|
|
245
|
+
now.mockReturnValue(1_000_000 + 61_000);
|
|
246
|
+
await pool.getClient();
|
|
247
|
+
const recovered = await pool.getClient();
|
|
248
|
+
fetchSpy.mockResolvedValueOnce(OK_RESPONSE);
|
|
249
|
+
await recovered.fetch(`${NODE_A}${METHOD_PATH}`);
|
|
250
|
+
pool.banNode(recovered);
|
|
251
|
+
// the ban after a successful request lasts 60s again, not 60s * e
|
|
252
|
+
now.mockReturnValue(1_000_000 + 61_000 + 61_000);
|
|
253
|
+
const urls = [
|
|
254
|
+
(await pool.getClient()).url,
|
|
255
|
+
(await pool.getClient()).url
|
|
256
|
+
];
|
|
257
|
+
expect(urls).toContain(NODE_A);
|
|
258
|
+
});
|
|
259
|
+
it.each([0, -1, 1.5])('should reject requestTimeout = %s', (requestTimeout) => {
|
|
260
|
+
expect(() => new GRPCConnectionPool('testnet', { requestTimeout })).toThrow('GRPC requestTimeout must be a positive integer');
|
|
261
|
+
});
|
|
262
|
+
it('should default requestTimeout to 10 seconds', () => {
|
|
263
|
+
const pool = new GRPCConnectionPool('testnet', { dapiUrl: NODE_A });
|
|
264
|
+
expect(pool.requestTimeout).toBe(10_000);
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
describe('DashPlatformSDK options', () => {
|
|
268
|
+
it('should pass requestTimeout to the pool', () => {
|
|
269
|
+
const sdk = new DashPlatformSDK({ network: 'testnet', requestTimeout: 1234, grpc: { dapiUrl: NODE_A } });
|
|
270
|
+
expect(sdk.grpcPool.requestTimeout).toBe(1234);
|
|
271
|
+
});
|
|
272
|
+
it('should prefer grpc.requestTimeout over the top-level option', () => {
|
|
273
|
+
const sdk = new DashPlatformSDK({ network: 'testnet', requestTimeout: 1234, grpc: { dapiUrl: NODE_A, requestTimeout: 4321 } });
|
|
274
|
+
expect(sdk.grpcPool.requestTimeout).toBe(4321);
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
});
|