tendermint-rpc: Support order_by param in Tendermint v0.34 tx search

This commit is contained in:
willclarktech 2021-06-09 14:50:14 +02:00
parent 7bdc748f02
commit 5ee3f82b83
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
4 changed files with 39 additions and 9 deletions

View File

@ -89,6 +89,7 @@ interface RpcTxSearchParams {
readonly prove?: boolean;
readonly page?: string;
readonly per_page?: string;
readonly order_by?: string;
}
function encodeTxSearchParams(params: requests.TxSearchParams): RpcTxSearchParams {
return {
@ -96,6 +97,7 @@ function encodeTxSearchParams(params: requests.TxSearchParams): RpcTxSearchParam
prove: params.prove,
page: may(Integer.encode, params.page),
per_page: may(Integer.encode, params.per_page),
order_by: params.order_by,
};
}

View File

@ -174,6 +174,7 @@ export interface TxSearchParams {
readonly prove?: boolean;
readonly page?: number;
readonly per_page?: number;
readonly order_by?: string;
}
export interface ValidatorsRequest {

View File

@ -439,6 +439,41 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues)
}
});
it("returns transactions in ascending order by default", async () => {
// NOTE: The Tendermint docs claim the default ordering is "desc" but it is actually "asc"
// Docs: https://docs.tendermint.com/master/rpc/#/Info/tx_search
// Code: https://github.com/tendermint/tendermint/blob/v0.34.10/rpc/core/tx.go#L89
pendingWithoutTendermint();
const client = await Tendermint34Client.create(rpcFactory());
const query = buildQuery({ tags: [{ key: "app.key", value: key }] });
const s = await client.txSearch({ query: query });
expect(s.totalCount).toEqual(3);
s.txs.slice(1).reduce((lastHeight, { height }) => {
expect(height).toBeGreaterThanOrEqual(lastHeight);
return height;
}, s.txs[0].height);
client.disconnect();
});
it("can set the order", async () => {
pendingWithoutTendermint();
const client = await Tendermint34Client.create(rpcFactory());
const query = buildQuery({ tags: [{ key: "app.key", value: key }] });
const s1 = await client.txSearch({ query: query, order_by: "desc" });
const s2 = await client.txSearch({ query: query, order_by: "asc" });
expect(s1.totalCount).toEqual(s2.totalCount);
expect([...s1.txs].reverse()).toEqual(s2.txs);
client.disconnect();
});
it("can paginate over txSearch results", async () => {
pendingWithoutTendermint();
const client = await Tendermint34Client.create(rpcFactory());

View File

@ -257,12 +257,7 @@ export class Tendermint34Client {
*/
public async txSearch(params: requests.TxSearchParams): Promise<responses.TxSearchResponse> {
const query: requests.TxSearchRequest = { params: params, method: requests.Method.TxSearch };
const resp = await this.doCall(query, this.p.encodeTxSearch, this.r.decodeTxSearch);
return {
...resp,
// make sure we sort by height, as tendermint may be sorting by string value of the height
txs: [...resp.txs].sort((a, b) => a.height - b.height),
};
return this.doCall(query, this.p.encodeTxSearch, this.r.decodeTxSearch);
}
// this should paginate through all txSearch options to ensure it returns all results.
@ -281,9 +276,6 @@ export class Tendermint34Client {
done = true;
}
}
// make sure we sort by height, as tendermint may be sorting by string value of the height
// and the earlier items may be in a higher page than the later items
txs.sort((a, b) => a.height - b.height);
return {
totalCount: txs.length,