mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Merge pull request #823 from cosmos/748-tx-search-order-by
Support order_by param in Tendermint tx search
This commit is contained in:
commit
bd9601c56a
@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -157,6 +157,7 @@ export interface TxSearchParams {
|
||||
readonly prove?: boolean;
|
||||
readonly page?: number;
|
||||
readonly per_page?: number;
|
||||
readonly order_by?: string;
|
||||
}
|
||||
|
||||
export interface ValidatorsRequest {
|
||||
|
@ -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());
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -174,6 +174,7 @@ export interface TxSearchParams {
|
||||
readonly prove?: boolean;
|
||||
readonly page?: number;
|
||||
readonly per_page?: number;
|
||||
readonly order_by?: string;
|
||||
}
|
||||
|
||||
export interface ValidatorsRequest {
|
||||
|
@ -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());
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user