Test and fix RestClient.authAccounts

This commit is contained in:
Simon Warta 2020-02-04 08:53:53 +01:00
parent b7877d136d
commit 4b91ed8f0c
7 changed files with 33 additions and 28 deletions

View File

@ -309,7 +309,7 @@ export class CosmWasmConnection implements BlockchainConnection {
const accountForHeight = await this.restClient.authAccounts(sender, response.height);
// this is technically not the proper nonce. maybe this causes issues for sig validation?
// leaving for now unless it causes issues
const sequence = (parseInt(accountForHeight.result.value.sequence, 10) - 1) as Nonce;
const sequence = (accountForHeight.result.value.sequence - 1) as Nonce;
return parseTxsResponse(chainId, parseInt(response.height, 10), sequence, response, this.tokenInfo);
}
}

View File

@ -4,8 +4,8 @@ import { accountToNonce, nonceToAccountNumber, nonceToSequence } from "./types";
describe("nonceEncoding", () => {
it("works for input in range", () => {
const nonce = accountToNonce({
account_number: "1234",
sequence: "7890",
account_number: 1234,
sequence: 7890,
});
expect(nonceToAccountNumber(nonce)).toEqual("1234");
expect(nonceToSequence(nonce)).toEqual("7890");
@ -14,14 +14,14 @@ describe("nonceEncoding", () => {
it("errors on input too large", () => {
expect(() =>
accountToNonce({
account_number: "1234567890",
sequence: "7890",
account_number: 1234567890,
sequence: 7890,
}),
).toThrow();
expect(() =>
accountToNonce({
account_number: "178",
sequence: "97320247923",
account_number: 178,
sequence: 97320247923,
}),
).toThrow();
});

View File

@ -1,3 +1,4 @@
import { types } from "@cosmwasm/sdk";
import { Nonce } from "@iov/bcp";
export interface TokenInfo {
@ -24,28 +25,21 @@ const maxSeq = 1 << 20;
// NonceInfo is the data we need from account to create a nonce
// Use this so no confusion about order of arguments
export interface NonceInfo {
readonly account_number: string;
readonly sequence: string;
}
export type NonceInfo = Pick<types.BaseAccount, "account_number" | "sequence">;
// this (lossily) encodes the two pieces of info (uint64) needed to sign into
// one (53-bit) number. Cross your fingers.
/* eslint-disable-next-line @typescript-eslint/camelcase */
export function accountToNonce({ account_number, sequence }: NonceInfo): Nonce {
const acct = parseInt(account_number, 10);
const seq = parseInt(sequence, 10);
export function accountToNonce({ account_number: account, sequence }: NonceInfo): Nonce {
// we allow 23 bits (8 million) for accounts, and 20 bits (1 million) for tx/account
// let's fix this soon
if (acct > maxAcct) {
if (account > maxAcct) {
throw new Error("Account number is greater than 2^23, must update Nonce handler");
}
if (seq > maxSeq) {
if (sequence > maxSeq) {
throw new Error("Sequence is greater than 2^20, must update Nonce handler");
}
const val = acct * maxSeq + seq;
const val = account * maxSeq + sequence;
return val as Nonce;
}

View File

@ -1,3 +1,4 @@
import { types } from "@cosmwasm/sdk";
import { Nonce } from "@iov/bcp";
export interface TokenInfo {
readonly denom: string;
@ -14,10 +15,7 @@ export interface TokenInfo {
readonly fractionalDigits: number;
}
export declare type TokenInfos = ReadonlyArray<TokenInfo>;
export interface NonceInfo {
readonly account_number: string;
readonly sequence: string;
}
export declare function accountToNonce({ account_number, sequence }: NonceInfo): Nonce;
export declare type NonceInfo = Pick<types.BaseAccount, "account_number" | "sequence">;
export declare function accountToNonce({ account_number: account, sequence }: NonceInfo): Nonce;
export declare function nonceToAccountNumber(nonce: Nonce): string;
export declare function nonceToSequence(nonce: Nonce): string;

View File

@ -9,6 +9,8 @@ const { fromBase64 } = Encoding;
const httpUrl = "http://localhost:1317";
const defaultNetworkId = "testing";
const faucetAddress = "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6";
function pendingWithoutCosmos(): void {
if (!process.env.COSMOS_ENABLED) {
return pending("Set COSMOS_ENABLED to enable Cosmos node-based tests");
@ -30,6 +32,17 @@ describe("RestClient", () => {
});
});
describe("authAccounts", () => {
it("works", async () => {
pendingWithoutCosmos();
const client = new RestClient(httpUrl);
const { result } = await client.authAccounts(faucetAddress);
const account = result.value;
expect(account.account_number).toEqual(4);
expect(account.sequence).toBeGreaterThanOrEqual(0);
});
});
describe("encodeTx", () => {
it("works for cosmoshub example", async () => {
pendingWithoutCosmos();

View File

@ -64,6 +64,6 @@ export interface BaseAccount {
readonly address: string;
readonly coins: ReadonlyArray<Coin>;
readonly public_key: AccountPubKey;
readonly account_number: string;
readonly sequence: string;
readonly account_number: number;
readonly sequence: number;
}

View File

@ -45,6 +45,6 @@ export interface BaseAccount {
readonly address: string;
readonly coins: ReadonlyArray<Coin>;
readonly public_key: AccountPubKey;
readonly account_number: string;
readonly sequence: string;
readonly account_number: number;
readonly sequence: number;
}