-
Notifications
You must be signed in to change notification settings - Fork 331
/
stargateclient.spec.ts
552 lines (486 loc) · 17.9 KB
/
stargateclient.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/* eslint-disable @typescript-eslint/naming-convention */
import { fromBase64, toBase64 } from "@cosmjs/encoding";
import {
coins,
DirectSecp256k1HdWallet,
encodePubkey,
makeAuthInfoBytes,
makeSignDoc,
Registry,
TxBodyEncodeObject,
} from "@cosmjs/proto-signing";
import { assert, sleep } from "@cosmjs/utils";
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import { ReadonlyDate } from "readonly-date";
import {
assertIsDeliverTxSuccess,
BroadcastTxError,
isDeliverTxFailure,
isDeliverTxSuccess,
PrivateStargateClient,
StargateClient,
TimeoutError,
} from "./stargateclient";
import {
faucet,
makeRandomAddress,
nonExistentAddress,
pendingWithoutSimapp,
pendingWithoutSlowSimapp,
simapp,
simapp44Enabled,
slowSimapp,
tendermintIdMatcher,
unused,
validator,
} from "./testutils.spec";
const resultFailure = {
code: 5,
height: 219901,
rawLog:
"failed to execute message; message index: 0: 1855527000ufct is smaller than 20000000000000000000000ufct: insufficient funds",
transactionHash: "FDC4FB701AABD465935F7D04AE490D1EF5F2BD4B227601C4E98B57EB077D9B7D",
events: [],
gasUsed: 54396,
gasWanted: 200000,
};
const resultSuccess = {
code: 0,
height: 219894,
rawLog:
'[{"events":[{"type":"message","attributes":[{"key":"action","value":"send"},{"key":"sender","value":"firma1trqyle9m2nvyafc2n25frkpwed2504y6avgfzr"},{"key":"module","value":"bank"}]},{"type":"transfer","attributes":[{"key":"recipient","value":"firma12er8ls2sf5zess3jgjxz59xat9xtf8hz0hk6n4"},{"key":"sender","value":"firma1trqyle9m2nvyafc2n25frkpwed2504y6avgfzr"},{"key":"amount","value":"2000000ufct"}]}]}]',
transactionHash: "C0B416CA868C55C2B8C1BBB8F3CFA233854F13A5CB15D3E9599F50CAF7B3D161",
events: [],
gasUsed: 61556,
gasWanted: 200000,
};
describe("isDeliverTxFailure", () => {
it("works", () => {
expect(isDeliverTxFailure(resultFailure)).toEqual(true);
expect(isDeliverTxFailure(resultSuccess)).toEqual(false);
});
});
describe("isDeliverTxSuccess", () => {
it("works", () => {
expect(isDeliverTxSuccess(resultFailure)).toEqual(false);
expect(isDeliverTxSuccess(resultSuccess)).toEqual(true);
});
});
describe("StargateClient", () => {
describe("connect", () => {
it("works", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
expect(client).toBeTruthy();
client.disconnect();
});
});
describe("getChainId", () => {
it("works", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
expect(await client.getChainId()).toEqual(simapp.chainId);
client.disconnect();
});
it("caches chain ID", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const openedClient = client as unknown as PrivateStargateClient;
const getCodeSpy = spyOn(openedClient.tmClient!, "status").and.callThrough();
expect(await client.getChainId()).toEqual(simapp.chainId); // from network
expect(await client.getChainId()).toEqual(simapp.chainId); // from cache
expect(getCodeSpy).toHaveBeenCalledTimes(1);
client.disconnect();
});
});
describe("getHeight", () => {
it("works", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const height1 = await client.getHeight();
expect(height1).toBeGreaterThan(0);
await sleep(simapp.blockTime * 1.4); // tolerate chain being 40% slower than expected
const height2 = await client.getHeight();
expect(height2).toBeGreaterThanOrEqual(height1 + 1);
expect(height2).toBeLessThanOrEqual(height1 + 2);
client.disconnect();
});
});
describe("getAccount", () => {
it("works for unused account", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const account = await client.getAccount(unused.address);
assert(account);
expect(account).toEqual({
address: unused.address,
pubkey: null,
accountNumber: unused.accountNumber,
sequence: unused.sequence,
});
client.disconnect();
});
it("works for account with pubkey and non-zero sequence", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const account = await client.getAccount(validator.delegatorAddress);
assert(account);
expect(account).toEqual({
address: validator.delegatorAddress,
pubkey: validator.pubkey,
accountNumber: validator.accountNumber,
sequence: validator.sequence,
});
client.disconnect();
});
it("returns null for non-existent address", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const account = await client.getAccount(nonExistentAddress);
expect(account).toBeNull();
client.disconnect();
});
});
describe("getSequence", () => {
it("works for unused account", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const account = await client.getSequence(unused.address);
assert(account);
expect(account).toEqual({
accountNumber: unused.accountNumber,
sequence: unused.sequence,
});
client.disconnect();
});
it("rejects for non-existent address", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
await expectAsync(client.getSequence(nonExistentAddress)).toBeRejectedWithError(
/account '([a-z0-9]{10,90})' does not exist on chain/i,
);
client.disconnect();
});
});
describe("getBlock", () => {
it("works for latest block", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const response = await client.getBlock();
expect(response).toEqual(
jasmine.objectContaining({
id: jasmine.stringMatching(tendermintIdMatcher),
header: jasmine.objectContaining({
chainId: await client.getChainId(),
}),
txs: jasmine.arrayContaining([]),
}),
);
expect(response.header.height).toBeGreaterThanOrEqual(1);
expect(new ReadonlyDate(response.header.time).getTime()).toBeLessThan(ReadonlyDate.now());
expect(new ReadonlyDate(response.header.time).getTime()).toBeGreaterThanOrEqual(
ReadonlyDate.now() - 5_000,
);
client.disconnect();
});
it("works for block by height", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const height = (await client.getBlock()).header.height;
const response = await client.getBlock(height - 1);
expect(response).toEqual(
jasmine.objectContaining({
id: jasmine.stringMatching(tendermintIdMatcher),
header: jasmine.objectContaining({
height: height - 1,
chainId: await client.getChainId(),
}),
txs: jasmine.arrayContaining([]),
}),
);
expect(new ReadonlyDate(response.header.time).getTime()).toBeLessThan(ReadonlyDate.now());
expect(new ReadonlyDate(response.header.time).getTime()).toBeGreaterThanOrEqual(
ReadonlyDate.now() - 5_000,
);
client.disconnect();
});
});
describe("getBalance", () => {
it("works for different existing balances", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const response1 = await client.getBalance(unused.address, simapp.denomFee);
expect(response1).toEqual({
amount: unused.balanceFee,
denom: simapp.denomFee,
});
const response2 = await client.getBalance(unused.address, simapp.denomStaking);
expect(response2).toEqual({
amount: unused.balanceStaking,
denom: simapp.denomStaking,
});
client.disconnect();
});
it("returns 0 for non-existent balance", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const response = await client.getBalance(unused.address, "gintonic");
expect(response).toEqual({
denom: "gintonic",
amount: "0",
});
client.disconnect();
});
it("returns 0 for non-existent address", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const response = await client.getBalance(nonExistentAddress, simapp.denomFee);
expect(response).toEqual({
denom: simapp.denomFee,
amount: "0",
});
client.disconnect();
});
});
describe("getAllBalances", () => {
it("returns all balances for unused account", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const balances = await client.getAllBalances(unused.address);
expect(balances).toEqual([
{
amount: unused.balanceFee,
denom: simapp.denomFee,
},
{
amount: unused.balanceStaking,
denom: simapp.denomStaking,
},
]);
client.disconnect();
});
it("returns an empty list for non-existent account", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const balances = await client.getAllBalances(nonExistentAddress);
expect(balances).toEqual([]);
client.disconnect();
});
});
describe("getBalanceStaked", () => {
it("works", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const response = await client.getBalanceStaked(faucet.address0);
expect(response).toEqual({ denom: "ustake", amount: "63474" });
client.disconnect();
});
});
describe("broadcastTx", () => {
it("broadcasts a transaction", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const [{ address, pubkey: pubkeyBytes }] = await wallet.getAccounts();
const pubkey = encodePubkey({
type: "tendermint/PubKeySecp256k1",
value: toBase64(pubkeyBytes),
});
const registry = new Registry();
const txBodyFields: TxBodyEncodeObject = {
typeUrl: "/cosmos.tx.v1beta1.TxBody",
value: {
messages: [
{
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: {
fromAddress: address,
toAddress: makeRandomAddress(),
amount: [
{
denom: "ucosm",
amount: "1234567",
},
],
},
},
],
},
};
const txBodyBytes = registry.encode(txBodyFields);
const { accountNumber, sequence } = (await client.getSequence(address))!;
const feeAmount = coins(2000, "ucosm");
const gasLimit = 200000;
const feeGranter = undefined;
const feePayer = undefined;
const authInfoBytes = makeAuthInfoBytes(
[{ pubkey, sequence }],
feeAmount,
gasLimit,
feeGranter,
feePayer,
);
const chainId = await client.getChainId();
const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber);
const { signature } = await wallet.signDirect(address, signDoc);
const txRaw = TxRaw.fromPartial({
bodyBytes: txBodyBytes,
authInfoBytes: authInfoBytes,
signatures: [fromBase64(signature.signature)],
});
const txRawBytes = Uint8Array.from(TxRaw.encode(txRaw).finish());
const txResult = await client.broadcastTx(txRawBytes);
assertIsDeliverTxSuccess(txResult);
const { gasUsed, rawLog, transactionHash } = txResult;
expect(gasUsed).toBeGreaterThan(0);
expect(rawLog).toMatch(/{"key":"amount","value":"1234567ucosm"}/);
expect(transactionHash).toMatch(/^[0-9A-F]{64}$/);
client.disconnect();
});
it("errors immediately for a CheckTx failure", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const [{ address, pubkey: pubkeyBytes }] = await wallet.getAccounts();
const pubkey = encodePubkey({
type: "tendermint/PubKeySecp256k1",
value: toBase64(pubkeyBytes),
});
const registry = new Registry();
const invalidRecipientAddress = "tgrade1z363ulwcrxged4z5jswyt5dn5v3lzsemwz9ewj"; // wrong bech32 prefix
const txBodyFields: TxBodyEncodeObject = {
typeUrl: "/cosmos.tx.v1beta1.TxBody",
value: {
messages: [
{
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: {
fromAddress: address,
toAddress: invalidRecipientAddress,
amount: [
{
denom: "ucosm",
amount: "1234567",
},
],
},
},
],
},
};
const txBodyBytes = registry.encode(txBodyFields);
const { accountNumber, sequence } = (await client.getSequence(address))!;
const feeAmount = coins(2000, "ucosm");
const gasLimit = 200000;
const feeGranter = undefined;
const feePayer = undefined;
const authInfoBytes = makeAuthInfoBytes(
[{ pubkey, sequence }],
feeAmount,
gasLimit,
feeGranter,
feePayer,
sequence,
);
const chainId = await client.getChainId();
const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber);
const { signature } = await wallet.signDirect(address, signDoc);
const txRaw = TxRaw.fromPartial({
bodyBytes: txBodyBytes,
authInfoBytes: authInfoBytes,
signatures: [fromBase64(signature.signature)],
});
const txRawBytes = Uint8Array.from(TxRaw.encode(txRaw).finish());
try {
await client.broadcastTx(txRawBytes);
assert(false, "Expected broadcastTx to throw");
} catch (error: any) {
expect(error).toMatch(
simapp44Enabled() ? /invalid recipient address/i : /Broadcasting transaction failed with code 7/i,
);
assert(error instanceof BroadcastTxError);
expect(error.code).toEqual(7);
expect(error.codespace).toEqual("sdk");
}
client.disconnect();
});
it("respects user timeouts rather than RPC timeouts", async () => {
pendingWithoutSlowSimapp();
const client = await StargateClient.connect(slowSimapp.tendermintUrl);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const [{ address, pubkey: pubkeyBytes }] = await wallet.getAccounts();
const pubkey = encodePubkey({
type: "tendermint/PubKeySecp256k1",
value: toBase64(pubkeyBytes),
});
const registry = new Registry();
const txBodyFields: TxBodyEncodeObject = {
typeUrl: "/cosmos.tx.v1beta1.TxBody",
value: {
messages: [
{
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: {
fromAddress: address,
toAddress: makeRandomAddress(),
amount: [
{
denom: "ucosm",
amount: "1234567",
},
],
},
},
],
},
};
const txBodyBytes = registry.encode(txBodyFields);
const chainId = await client.getChainId();
const feeAmount = coins(2000, "ucosm");
const gasLimit = 200000;
const feeGranter = undefined;
const feePayer = undefined;
const { accountNumber: accountNumber1, sequence: sequence1 } = (await client.getSequence(address))!;
const authInfoBytes1 = makeAuthInfoBytes(
[{ pubkey, sequence: sequence1 }],
feeAmount,
gasLimit,
feeGranter,
feePayer,
);
const signDoc1 = makeSignDoc(txBodyBytes, authInfoBytes1, chainId, accountNumber1);
const { signature: signature1 } = await wallet.signDirect(address, signDoc1);
const txRaw1 = TxRaw.fromPartial({
bodyBytes: txBodyBytes,
authInfoBytes: authInfoBytes1,
signatures: [fromBase64(signature1.signature)],
});
const txRawBytes1 = Uint8Array.from(TxRaw.encode(txRaw1).finish());
const largeTimeoutMs = 30_000;
const txResult = await client.broadcastTx(txRawBytes1, largeTimeoutMs);
assertIsDeliverTxSuccess(txResult);
const { accountNumber: accountNumber2, sequence: sequence2 } = (await client.getSequence(address))!;
const authInfoBytes2 = makeAuthInfoBytes(
[{ pubkey, sequence: sequence2 }],
feeAmount,
gasLimit,
feeGranter,
feePayer,
);
const signDoc2 = makeSignDoc(txBodyBytes, authInfoBytes2, chainId, accountNumber2);
const { signature: signature2 } = await wallet.signDirect(address, signDoc2);
const txRaw2 = TxRaw.fromPartial({
bodyBytes: txBodyBytes,
authInfoBytes: authInfoBytes2,
signatures: [fromBase64(signature2.signature)],
});
const txRawBytes2 = Uint8Array.from(TxRaw.encode(txRaw2).finish());
const smallTimeoutMs = 1_000;
await expectAsync(client.broadcastTx(txRawBytes2, smallTimeoutMs)).toBeRejectedWithError(
TimeoutError,
/transaction with id .+ was submitted but was not yet found on the chain/i,
);
client.disconnect();
}, 30_000);
});
});