Add height parameter to QueryClient.queryUnverified

This commit is contained in:
Simon Warta 2022-09-05 12:27:25 +02:00
parent 155ed33dd6
commit 771829f928
3 changed files with 75 additions and 3 deletions

View File

@ -26,6 +26,8 @@ and this project adheres to
- @cosmjs/stargate: Add Amino JSON support for `MsgCreateVestingAccount`. - @cosmjs/stargate: Add Amino JSON support for `MsgCreateVestingAccount`.
- @cosmjs/stargate and @cosmjs/cosmwasm-stargate: Create and use - @cosmjs/stargate and @cosmjs/cosmwasm-stargate: Create and use
BroadcastTxError ([#1096]). BroadcastTxError ([#1096]).
- @cosmjs/stargate: Add height parameter to `QueryClient.queryUnverified`
([#1250]).
- @cosmjs/faucet: Allow configuring the cooldown value via - @cosmjs/faucet: Allow configuring the cooldown value via
`FAUCET_COOLDOWN_TIME` environment variable. `FAUCET_COOLDOWN_TIME` environment variable.
@ -35,6 +37,7 @@ and this project adheres to
[#1176]: https://github.com/cosmos/cosmjs/pull/1176 [#1176]: https://github.com/cosmos/cosmjs/pull/1176
[#1224]: https://github.com/cosmos/cosmjs/pull/1224 [#1224]: https://github.com/cosmos/cosmjs/pull/1224
[#1225]: https://github.com/cosmos/cosmjs/issues/1225 [#1225]: https://github.com/cosmos/cosmjs/issues/1225
[#1250]: https://github.com/cosmos/cosmjs/issues/1250
### Fixed ### Fixed

View File

@ -1,10 +1,27 @@
/* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/naming-convention */
import { coin } from "@cosmjs/amino";
import { toAscii } from "@cosmjs/encoding"; import { toAscii } from "@cosmjs/encoding";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc"; import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { assert } from "@cosmjs/utils";
import { Metadata } from "cosmjs-types/cosmos/bank/v1beta1/bank"; import { Metadata } from "cosmjs-types/cosmos/bank/v1beta1/bank";
import { QueryAllBalancesRequest, QueryAllBalancesResponse } from "cosmjs-types/cosmos/bank/v1beta1/query"; import {
QueryAllBalancesRequest,
QueryAllBalancesResponse,
QueryBalanceRequest,
QueryBalanceResponse,
} from "cosmjs-types/cosmos/bank/v1beta1/query";
import { pendingWithoutSimapp, simapp, simapp44Enabled, unused } from "../testutils.spec"; import { SigningStargateClient } from "../signingstargateclient";
import {
defaultSigningClientOptions,
faucet,
makeRandomAddress,
pendingWithoutSimapp,
simapp,
simapp44Enabled,
unused,
} from "../testutils.spec";
import { QueryClient } from "./queryclient"; import { QueryClient } from "./queryclient";
async function makeClient(rpcUrl: string): Promise<[QueryClient, Tendermint34Client]> { async function makeClient(rpcUrl: string): Promise<[QueryClient, Tendermint34Client]> {
@ -101,5 +118,52 @@ describe("QueryClient", () => {
tmClient.disconnect(); tmClient.disconnect();
}); });
it("works for height", async () => {
pendingWithoutSimapp();
const [queryClient, tmClient] = await makeClient(simapp.tendermintUrlHttp);
const joe = makeRandomAddress();
const h1 = (await tmClient.status()).syncInfo.latestBlockHeight;
// Send tokens to `recipient`
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const client = await SigningStargateClient.connectWithSigner(
simapp.tendermintUrlHttp,
wallet,
defaultSigningClientOptions,
);
const amount = coin(332211, simapp.denomFee);
await client.sendTokens(faucet.address0, joe, [amount], "auto");
const h2 = (await tmClient.status()).syncInfo.latestBlockHeight;
assert(h1 < h2);
// Query with no height
{
const requestData = QueryBalanceRequest.encode({ address: joe, denom: simapp.denomFee }).finish();
const data = await queryClient.queryUnverified(`/cosmos.bank.v1beta1.Query/Balance`, requestData);
const response = QueryBalanceResponse.decode(data);
expect(response.balance).toEqual(amount);
}
// Query at h2 (after send)
{
const requestData = QueryBalanceRequest.encode({ address: joe, denom: simapp.denomFee }).finish();
const data = await queryClient.queryUnverified(`/cosmos.bank.v1beta1.Query/Balance`, requestData, h2);
const response = QueryBalanceResponse.decode(data);
expect(response.balance).toEqual(amount);
}
// Query at h1 (before send)
{
const requestData = QueryBalanceRequest.encode({ address: joe, denom: simapp.denomFee }).finish();
const data = await queryClient.queryUnverified(`/cosmos.bank.v1beta1.Query/Balance`, requestData, h1);
const response = QueryBalanceResponse.decode(data);
expect(response.balance).toEqual({ amount: "0", denom: simapp.denomFee });
}
tmClient.disconnect();
});
}); });
}); });

View File

@ -570,11 +570,16 @@ export class QueryClient {
}; };
} }
public async queryUnverified(path: string, request: Uint8Array): Promise<Uint8Array> { public async queryUnverified(
path: string,
request: Uint8Array,
desiredHeight?: number,
): Promise<Uint8Array> {
const response = await this.tmClient.abciQuery({ const response = await this.tmClient.abciQuery({
path: path, path: path,
data: request, data: request,
prove: false, prove: false,
height: desiredHeight,
}); });
if (response.code) { if (response.code) {