Disconnect Tendermint clients properly

This commit is contained in:
Simon Warta 2020-08-13 15:51:00 +02:00
parent 7c1c3d6f3a
commit 15b4bb0468
2 changed files with 55 additions and 23 deletions

View File

@ -8,17 +8,16 @@ import { AuthExtension, setupAuthExtension } from "./auth";
import { QueryClient } from "./queryclient";
import { toAccAddress } from "./utils";
async function makeAuthClient(rpcUrl: string): Promise<QueryClient & AuthExtension> {
// TODO: tmClient is not owned by QueryClient but should be disconnected somehow (once we use WebSockets)
async function makeAuthClient(rpcUrl: string): Promise<[QueryClient & AuthExtension, TendermintClient]> {
const tmClient = await TendermintClient.connect(rpcUrl);
return QueryClient.withExtensions(tmClient, setupAuthExtension);
return [QueryClient.withExtensions(tmClient, setupAuthExtension), tmClient];
}
describe("AuthExtension", () => {
describe("account", () => {
it("works for unused account", async () => {
pendingWithoutSimapp();
const client = await makeAuthClient(simapp.tendermintUrl);
const [client, tmClient] = await makeAuthClient(simapp.tendermintUrl);
const account = await client.auth.account(unused.address);
assert(account);
@ -29,11 +28,13 @@ describe("AuthExtension", () => {
accountNumber: Long.fromNumber(unused.accountNumber, true),
// sequence not set
});
tmClient.disconnect();
});
it("works for account with pubkey and non-zero sequence", async () => {
pendingWithoutSimapp();
const client = await makeAuthClient(simapp.tendermintUrl);
const [client, tmClient] = await makeAuthClient(simapp.tendermintUrl);
const account = await client.auth.account(validator.address);
assert(account);
@ -43,14 +44,18 @@ describe("AuthExtension", () => {
// accountNumber not set
sequence: Long.fromNumber(validator.sequence, true),
});
tmClient.disconnect();
});
it("returns null for non-existent address", async () => {
pendingWithoutSimapp();
const client = await makeAuthClient(simapp.tendermintUrl);
const [client, tmClient] = await makeAuthClient(simapp.tendermintUrl);
const account = await client.auth.account(nonExistentAddress);
expect(account).toBeNull();
tmClient.disconnect();
});
});
@ -58,7 +63,7 @@ describe("AuthExtension", () => {
describe("account", () => {
it("works for unused account", async () => {
pendingWithoutSimapp();
const client = await makeAuthClient(simapp.tendermintUrl);
const [client, tmClient] = await makeAuthClient(simapp.tendermintUrl);
const account = await client.auth.unverified.account(unused.address);
assert(account);
@ -68,11 +73,13 @@ describe("AuthExtension", () => {
accountNumber: Long.fromNumber(unused.accountNumber, true),
// sequence not set
});
tmClient.disconnect();
});
it("works for account with pubkey and non-zero sequence", async () => {
pendingWithoutSimapp();
const client = await makeAuthClient(simapp.tendermintUrl);
const [client, tmClient] = await makeAuthClient(simapp.tendermintUrl);
const account = await client.auth.unverified.account(validator.address);
assert(account);
@ -82,15 +89,19 @@ describe("AuthExtension", () => {
// accountNumber not set
sequence: Long.fromNumber(validator.sequence, true),
});
tmClient.disconnect();
});
it("returns null for non-existent address", async () => {
pending("This fails with Error: Query failed with (1): internal");
pendingWithoutSimapp();
const client = await makeAuthClient(simapp.tendermintUrl);
const [client, tmClient] = await makeAuthClient(simapp.tendermintUrl);
const account = await client.auth.unverified.account(nonExistentAddress);
expect(account).toBeNull();
tmClient.disconnect();
});
});
});

View File

@ -10,17 +10,16 @@ import {
import { BankExtension, setupBankExtension } from "./bank";
import { QueryClient } from "./queryclient";
async function makeBankClient(rpcUrl: string): Promise<QueryClient & BankExtension> {
// TODO: tmClient is not owned by QueryClient but should be disconnected somehow (once we use WebSockets)
async function makeBankClient(rpcUrl: string): Promise<[QueryClient & BankExtension, TendermintClient]> {
const tmClient = await TendermintClient.connect(rpcUrl);
return QueryClient.withExtensions(tmClient, setupBankExtension);
return [QueryClient.withExtensions(tmClient, setupBankExtension), tmClient];
}
describe("BankExtension", () => {
describe("balance", () => {
it("works for different existing balances", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const [client, tmClient] = await makeBankClient(simapp.tendermintUrl);
const response1 = await client.bank.balance(unused.address, simapp.denomFee);
expect(response1).toEqual({
@ -32,22 +31,28 @@ describe("BankExtension", () => {
amount: unused.balanceStaking,
denom: simapp.denomStaking,
});
tmClient.disconnect();
});
it("returns null for non-existent balance", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const [client, tmClient] = await makeBankClient(simapp.tendermintUrl);
const response = await client.bank.balance(unused.address, "gintonic");
expect(response).toBeNull();
tmClient.disconnect();
});
it("returns null for non-existent address", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const [client, tmClient] = await makeBankClient(simapp.tendermintUrl);
const response = await client.bank.balance(nonExistentAddress, simapp.denomFee);
expect(response).toBeNull();
tmClient.disconnect();
});
});
@ -55,7 +60,7 @@ describe("BankExtension", () => {
describe("balance", () => {
it("works for different existing balances", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const [client, tmClient] = await makeBankClient(simapp.tendermintUrl);
const response1 = await client.bank.unverified.balance(unused.address, simapp.denomFee);
expect(response1).toEqual({
@ -67,35 +72,41 @@ describe("BankExtension", () => {
amount: unused.balanceStaking,
denom: simapp.denomStaking,
});
tmClient.disconnect();
});
it("returns zero for non-existent balance", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const [client, tmClient] = await makeBankClient(simapp.tendermintUrl);
const response = await client.bank.unverified.balance(unused.address, "gintonic");
expect(response).toEqual({
amount: "0",
denom: "gintonic",
});
tmClient.disconnect();
});
it("returns zero for non-existent address", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const [client, tmClient] = await makeBankClient(simapp.tendermintUrl);
const response = await client.bank.unverified.balance(nonExistentAddress, simapp.denomFee);
expect(response).toEqual({
amount: "0",
denom: simapp.denomFee,
});
tmClient.disconnect();
});
});
describe("allBalances", () => {
it("returns all balances for unused account", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const [client, tmClient] = await makeBankClient(simapp.tendermintUrl);
const balances = await client.bank.unverified.allBalances(unused.address);
expect(balances).toEqual([
@ -108,21 +119,25 @@ describe("BankExtension", () => {
denom: simapp.denomStaking,
},
]);
tmClient.disconnect();
});
it("returns an empty list for non-existent account", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const [client, tmClient] = await makeBankClient(simapp.tendermintUrl);
const balances = await client.bank.unverified.allBalances(nonExistentAddress);
expect(balances).toEqual([]);
tmClient.disconnect();
});
});
describe("totalSupply", () => {
it("works", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const [client, tmClient] = await makeBankClient(simapp.tendermintUrl);
const response = await client.bank.unverified.totalSupply();
expect(response).toEqual([
@ -135,30 +150,36 @@ describe("BankExtension", () => {
denom: simapp.denomStaking,
},
]);
tmClient.disconnect();
});
});
describe("supplyOf", () => {
it("works for existing denom", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const [client, tmClient] = await makeBankClient(simapp.tendermintUrl);
const response = await client.bank.unverified.supplyOf(simapp.denomFee);
expect(response).toEqual({
amount: "18000000000",
denom: simapp.denomFee,
});
tmClient.disconnect();
});
it("returns zero for non-existent denom", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const [client, tmClient] = await makeBankClient(simapp.tendermintUrl);
const response = await client.bank.unverified.supplyOf("gintonic");
expect(response).toEqual({
amount: "0",
denom: "gintonic",
});
tmClient.disconnect();
});
});
});