Merge pull request #823 from cosmos/748-tx-search-order-by

Support order_by param in Tendermint tx search
This commit is contained in:
Simon Warta 2021-06-09 15:20:51 +02:00 committed by GitHub
commit bd9601c56a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 78 additions and 18 deletions

View File

@ -74,6 +74,7 @@ interface RpcTxSearchParams {
readonly prove?: boolean;
readonly page?: string;
readonly per_page?: string;
readonly order_by?: string;
}
function encodeTxSearchParams(params: requests.TxSearchParams): RpcTxSearchParams {
return {
@ -81,6 +82,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

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

View File

@ -377,6 +377,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.33.9/rpc/core/tx.go#L84
pendingWithoutTendermint();
const client = await Tendermint33Client.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 Tendermint33Client.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 Tendermint33Client.create(rpcFactory());

View File

@ -210,12 +210,7 @@ export class Tendermint33Client {
*/
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.
@ -234,9 +229,6 @@ export class Tendermint33Client {
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,

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,