Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove verified queries from AuthExtension and BankExtension #910

Merged
merged 1 commit into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ and this project adheres to
adding the fields `LaunchpadLedgerOptions.ledgerAppName` and
`.minLedgerAppVersion`.

### Changed

- @cosmjs/stargate: Remove verified queries from `AuthExtension` and
`BankExtension` as well as `StargateClient.getAccountVerified` because the
storage layout is not stable across multiple Cosmos SDK releases. Verified
queries remain available in the `IbcExtension` because for IBC the storage
layout is standardized. Such queries can still be implemented in CosmJS caller
code that only needs to support one backend. ([#865])

[#865]: https://github.com/cosmos/cosmjs/issues/865

## [0.26.2] - 2021-10-12

### Fixed
Expand Down
48 changes: 0 additions & 48 deletions packages/stargate/src/queries/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,52 +64,4 @@ describe("AuthExtension", () => {
tmClient.disconnect();
});
});

describe("verified", () => {
describe("account", () => {
it("works for unused account", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl);
const account = await client.auth.verified.account(unused.address);
assert(account);

expect(account.typeUrl).toEqual("/cosmos.auth.v1beta1.BaseAccount");
expect(BaseAccount.decode(account.value)).toEqual({
address: unused.address,
// pubKey not set
accountNumber: Long.fromNumber(unused.accountNumber, true),
sequence: Long.fromNumber(0, true),
});

tmClient.disconnect();
});

it("works for account with pubkey and non-zero sequence", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl);
const account = await client.auth.verified.account(validator.delegatorAddress);
assert(account);

expect(account.typeUrl).toEqual("/cosmos.auth.v1beta1.BaseAccount");
expect(BaseAccount.decode(account.value)).toEqual({
address: validator.delegatorAddress,
pubKey: Any.fromPartial(encodePubkey(validator.pubkey)),
accountNumber: Long.fromNumber(0, true),
sequence: Long.fromNumber(validator.sequence, true),
});

tmClient.disconnect();
});

it("returns null for non-existent address", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl);
const account = await client.auth.verified.account(nonExistentAddress);

expect(account).toBeNull();

tmClient.disconnect();
});
});
});
});
21 changes: 1 addition & 20 deletions packages/stargate/src/queries/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { QueryClientImpl } from "cosmjs-types/cosmos/auth/v1beta1/query";
import { Any } from "cosmjs-types/google/protobuf/any";

import { QueryClient } from "./queryclient";
import { createProtobufRpcClient, toAccAddress } from "./utils";
import { createProtobufRpcClient } from "./utils";

export interface AuthExtension {
readonly auth: {
Expand All @@ -14,16 +14,6 @@ export interface AuthExtension {
* `typeUrl` and decode the `value` using its own type decoder.
*/
readonly account: (address: string) => Promise<Any | null>;
readonly verified: {
/**
* Returns an account if it exists and `null` otherwise.
*
* The account is a protobuf Any in order to be able to support many different
* account types in one API. The caller needs to switch over the expected and supported
* `typeUrl` and decode the `value` using its own type decoder.
*/
readonly account: (address: string) => Promise<Any | null>;
};
};
}

Expand All @@ -39,15 +29,6 @@ export function setupAuthExtension(base: QueryClient): AuthExtension {
const { account } = await queryService.Account({ address: address });
return account ?? null;
},
verified: {
account: async (address: string) => {
// https://github.com/cosmos/cosmos-sdk/blob/8cab43c8120fec5200c3459cbf4a92017bb6f287/x/auth/types/keys.go#L29-L32
const key = Uint8Array.from([0x01, ...toAccAddress(address)]);
const responseData = await base.queryVerified("acc", key);
if (responseData.length === 0) return null;
return Any.decode(responseData);
},
},
},
};
}
42 changes: 0 additions & 42 deletions packages/stargate/src/queries/bank.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,46 +143,4 @@ describe("BankExtension", () => {
tmClient.disconnect();
});
});

describe("verified", () => {
describe("balance", () => {
it("works for different existing balances", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);

const response1 = await client.bank.verified.balance(unused.address, simapp.denomFee);
expect(response1).toEqual({
amount: unused.balanceFee,
denom: simapp.denomFee,
});
const response2 = await client.bank.verified.balance(unused.address, simapp.denomStaking);
expect(response2).toEqual({
amount: unused.balanceStaking,
denom: simapp.denomStaking,
});

tmClient.disconnect();
});

it("returns null for non-existent balance", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);

const response = await client.bank.verified.balance(unused.address, "gintonic");
expect(response).toBeNull();

tmClient.disconnect();
});

it("returns null for non-existent address", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);

const response = await client.bank.verified.balance(nonExistentAddress, simapp.denomFee);
expect(response).toBeNull();

tmClient.disconnect();
});
});
});
});
19 changes: 1 addition & 18 deletions packages/stargate/src/queries/bank.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { toAscii } from "@cosmjs/encoding";
import { assert } from "@cosmjs/utils";
import { QueryClientImpl } from "cosmjs-types/cosmos/bank/v1beta1/query";
import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin";

import { QueryClient } from "./queryclient";
import { createProtobufRpcClient, toAccAddress } from "./utils";
import { createProtobufRpcClient } from "./utils";

export interface BankExtension {
readonly bank: {
readonly balance: (address: string, denom: string) => Promise<Coin>;
readonly allBalances: (address: string) => Promise<Coin[]>;
readonly totalSupply: () => Promise<Coin[]>;
readonly supplyOf: (denom: string) => Promise<Coin>;
readonly verified: {
readonly balance: (address: string, denom: string) => Promise<Coin | null>;
};
};
}

Expand Down Expand Up @@ -45,19 +41,6 @@ export function setupBankExtension(base: QueryClient): BankExtension {
assert(amount);
return amount;
},
verified: {
balance: async (address: string, denom: string) => {
// balance key is a bit tricker, using some prefix stores
// https://github.com/cosmwasm/cosmos-sdk/blob/80f7ff62f79777a487d0c7a53c64b0f7e43c47b9/x/bank/keeper/view.go#L74-L77
// ("balances", binAddress, denom)
// it seem like prefix stores just do a dumb concat with the keys (no tricks to avoid overlap)
// https://github.com/cosmos/cosmos-sdk/blob/2879c0702c87dc9dd828a8c42b9224dc054e28ad/store/prefix/store.go#L61-L64
// https://github.com/cosmos/cosmos-sdk/blob/2879c0702c87dc9dd828a8c42b9224dc054e28ad/store/prefix/store.go#L37-L43
const key = Uint8Array.from([...toAscii("balances"), ...toAccAddress(address), ...toAscii(denom)]);
const responseData = await base.queryVerified("bank", key);
return responseData.length ? Coin.decode(responseData) : null;
},
},
},
};
}
5 changes: 0 additions & 5 deletions packages/stargate/src/stargateclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,6 @@ export class StargateClient {
}
}

public async getAccountVerified(searchAddress: string): Promise<Account | null> {
const account = await this.forceGetQueryClient().auth.verified.account(searchAddress);
return account ? accountFromAny(account) : null;
}

public async getSequence(address: string): Promise<SequenceResponse> {
const account = await this.getAccount(address);
if (!account) {
Expand Down