From b9ca8cb38f9f535b31f0def4d3ce4c8a3170dd14 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 2 Mar 2023 09:49:53 +0100 Subject: [PATCH] Add tx search tests --- .../tendermint34/tendermint34client.spec.ts | 110 ++++++++++++------ .../tendermint37/tendermint37client.spec.ts | 110 ++++++++++++------ 2 files changed, 154 insertions(+), 66 deletions(-) diff --git a/packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts b/packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts index 8f41e813..cf2d861c 100644 --- a/packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts +++ b/packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { toAscii } from "@cosmjs/encoding"; +import { toAscii, toHex } from "@cosmjs/encoding"; import { firstEvent, toListPromise } from "@cosmjs/stream"; -import { sleep } from "@cosmjs/utils"; +import { assert, sleep } from "@cosmjs/utils"; import { ReadonlyDate } from "readonly-date"; import { Stream } from "xstream"; @@ -435,25 +435,6 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) expect(r.height).toEqual(height); expect(r.proof).toBeTruthy(); - // txSearch - you must enable the indexer when running - // tendermint, else you get empty results - const query = buildQuery({ tags: [{ key: "app.key", value: find }] }); - - const s = await client.txSearch({ query: query, page: 1, per_page: 30 }); - // should find the tx - expect(s.totalCount).toEqual(1); - // should return same info as querying directly, - // except without the proof - expect(s.txs[0]).toEqual({ ...r, proof: undefined }); - - // ensure txSearchAll works as well - const sall = await client.txSearchAll({ query: query }); - // should find the tx - expect(sall.totalCount).toEqual(1); - // should return same info as querying directly, - // except without the proof - expect(sall.txs[0]).toEqual({ ...r, proof: undefined }); - // and let's query the block itself to see this transaction const block = await client.block(height); expect(block.block.txs.length).toEqual(1); @@ -464,25 +445,28 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) }); describe("txSearch", () => { - const key = randomString(); + const txKey = randomString(); // a key used for multiple transactions + let tx1: Uint8Array | undefined; + let broadcast1: responses.BroadcastTxCommitResponse | undefined; beforeAll(async () => { if (tendermintEnabled()) { const client = await Tendermint34Client.create(rpcFactory()); // eslint-disable-next-line no-inner-declarations - async function sendTx(): Promise { + async function sendTx(): Promise<[Uint8Array, responses.BroadcastTxCommitResponse]> { const me = randomString(); - const tx = buildKvTx(key, me); + const tx = buildKvTx(txKey, me); const txRes = await client.broadcastTxCommit({ tx: tx }); expect(responses.broadcastTxCommitSuccess(txRes)).toEqual(true); expect(txRes.height).toBeTruthy(); - expect(txRes.hash.length).not.toEqual(0); + expect(txRes.hash.length).toEqual(32); + return [tx, txRes]; } // send 3 txs - await sendTx(); + [tx1, broadcast1] = await sendTx(); await sendTx(); await sendTx(); @@ -492,6 +476,67 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) } }); + it("finds a single tx by hash", async () => { + pendingWithoutTendermint(); + assert(tx1 && broadcast1); + const client = await Tendermint34Client.create(rpcFactory()); + + const result = await client.txSearch({ query: `tx.hash='${toHex(broadcast1.hash)}'` }); + expect(result.totalCount).toEqual(1); + expect(result.txs[0]).toEqual({ + hash: broadcast1.hash, + height: broadcast1.height, + index: 0, + tx: tx1, + result: broadcast1.deliverTx!, + proof: undefined, + }); + + client.disconnect(); + }); + + it("finds a single tx by tags", async () => { + pendingWithoutTendermint(); + const client = await Tendermint34Client.create(rpcFactory()); + + const txKey2 = randomString(); + const txValue2 = randomString(); + const tx = buildKvTx(txKey2, txValue2); + + const txRes = await client.broadcastTxCommit({ tx: tx }); + expect(responses.broadcastTxCommitSuccess(txRes)).toEqual(true); + + // txSearch - you must enable the indexer when running + // tendermint, else you get empty results + const query = buildQuery({ tags: [{ key: "app.key", value: txKey2 }] }); + + const search = await client.txSearch({ query: query, page: 1, per_page: 30 }); + // should find the tx + expect(search.totalCount).toEqual(1); + // should return same info as querying directly, + // except without the proof + expect(search.txs[0]).toEqual({ + hash: txRes.hash, + height: txRes.height, + index: 0, + tx: tx, + result: txRes.deliverTx!, + proof: undefined, + }); + + // Ensure txSearchAll works as well. This should be moved in a dedicated "txSearchAll" test block. + const searchAll = await client.txSearchAll({ query: query }); + expect(searchAll.totalCount).toEqual(1); + expect(searchAll.txs[0]).toEqual({ + hash: txRes.hash, + height: txRes.height, + index: 0, + tx: tx, + result: txRes.deliverTx!, + proof: undefined, + }); + }); + 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 @@ -499,7 +544,7 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) pendingWithoutTendermint(); const client = await Tendermint34Client.create(rpcFactory()); - const query = buildQuery({ tags: [{ key: "app.key", value: key }] }); + const query = buildQuery({ tags: [{ key: "app.key", value: txKey }] }); const result = await client.txSearch({ query: query }); @@ -516,7 +561,7 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) pendingWithoutTendermint(); const client = await Tendermint34Client.create(rpcFactory()); - const query = buildQuery({ tags: [{ key: "app.key", value: key }] }); + const query = buildQuery({ tags: [{ key: "app.key", value: txKey }] }); const s1 = await client.txSearch({ query: query, order_by: "desc" }); const s2 = await client.txSearch({ query: query, order_by: "asc" }); @@ -531,7 +576,7 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) pendingWithoutTendermint(); const client = await Tendermint34Client.create(rpcFactory()); - const query = buildQuery({ tags: [{ key: "app.key", value: key }] }); + const query = buildQuery({ tags: [{ key: "app.key", value: txKey }] }); // expect one page of results const s1 = await client.txSearch({ query: query, page: 1, per_page: 2 }); @@ -550,15 +595,14 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) pendingWithoutTendermint(); const client = await Tendermint34Client.create(rpcFactory()); - const query = buildQuery({ tags: [{ key: "app.key", value: key }] }); + const query = buildQuery({ tags: [{ key: "app.key", value: txKey }] }); const sall = await client.txSearchAll({ query: query, per_page: 2 }); expect(sall.totalCount).toEqual(3); expect(sall.txs.length).toEqual(3); // make sure there are in order from lowest to highest height - const [tx1, tx2, tx3] = sall.txs; - expect(tx2.height).toEqual(tx1.height + 1); - expect(tx3.height).toEqual(tx2.height + 1); + expect(sall.txs[1].height).toEqual(sall.txs[0].height + 1); + expect(sall.txs[2].height).toEqual(sall.txs[1].height + 1); client.disconnect(); }); diff --git a/packages/tendermint-rpc/src/tendermint37/tendermint37client.spec.ts b/packages/tendermint-rpc/src/tendermint37/tendermint37client.spec.ts index c37d1247..d43b1d11 100644 --- a/packages/tendermint-rpc/src/tendermint37/tendermint37client.spec.ts +++ b/packages/tendermint-rpc/src/tendermint37/tendermint37client.spec.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { toAscii } from "@cosmjs/encoding"; +import { toAscii, toHex } from "@cosmjs/encoding"; import { firstEvent, toListPromise } from "@cosmjs/stream"; -import { sleep } from "@cosmjs/utils"; +import { assert, sleep } from "@cosmjs/utils"; import { ReadonlyDate } from "readonly-date"; import { Stream } from "xstream"; @@ -436,25 +436,6 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) expect(r.height).toEqual(height); expect(r.proof).toBeTruthy(); - // txSearch - you must enable the indexer when running - // tendermint, else you get empty results - const query = buildQuery({ tags: [{ key: "app.key", value: find }] }); - - const s = await client.txSearch({ query: query, page: 1, per_page: 30 }); - // should find the tx - expect(s.totalCount).toEqual(1); - // should return same info as querying directly, - // except without the proof - expect(s.txs[0]).toEqual({ ...r, proof: undefined }); - - // ensure txSearchAll works as well - const sall = await client.txSearchAll({ query: query }); - // should find the tx - expect(sall.totalCount).toEqual(1); - // should return same info as querying directly, - // except without the proof - expect(sall.txs[0]).toEqual({ ...r, proof: undefined }); - // and let's query the block itself to see this transaction const block = await client.block(height); expect(block.block.txs.length).toEqual(1); @@ -465,25 +446,28 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) }); describe("txSearch", () => { - const key = randomString(); + const txKey = randomString(); // a key used for multiple transactions + let tx1: Uint8Array | undefined; + let broadcast1: responses.BroadcastTxCommitResponse | undefined; beforeAll(async () => { if (tendermintEnabled()) { const client = await Tendermint37Client.create(rpcFactory()); // eslint-disable-next-line no-inner-declarations - async function sendTx(): Promise { + async function sendTx(): Promise<[Uint8Array, responses.BroadcastTxCommitResponse]> { const me = randomString(); - const tx = buildKvTx(key, me); + const tx = buildKvTx(txKey, me); const txRes = await client.broadcastTxCommit({ tx: tx }); expect(responses.broadcastTxCommitSuccess(txRes)).toEqual(true); expect(txRes.height).toBeTruthy(); - expect(txRes.hash.length).not.toEqual(0); + expect(txRes.hash.length).toEqual(32); + return [tx, txRes]; } // send 3 txs - await sendTx(); + [tx1, broadcast1] = await sendTx(); await sendTx(); await sendTx(); @@ -493,6 +477,67 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) } }); + it("finds a single tx by hash", async () => { + pendingWithoutTendermint(); + assert(tx1 && broadcast1); + const client = await Tendermint37Client.create(rpcFactory()); + + const result = await client.txSearch({ query: `tx.hash='${toHex(broadcast1.hash)}'` }); + expect(result.totalCount).toEqual(1); + expect(result.txs[0]).toEqual({ + hash: broadcast1.hash, + height: broadcast1.height, + index: 0, + tx: tx1, + result: broadcast1.deliverTx!, + proof: undefined, + }); + + client.disconnect(); + }); + + it("finds a single tx by tags", async () => { + pendingWithoutTendermint(); + const client = await Tendermint37Client.create(rpcFactory()); + + const txKey2 = randomString(); + const txValue2 = randomString(); + const tx = buildKvTx(txKey2, txValue2); + + const txRes = await client.broadcastTxCommit({ tx: tx }); + expect(responses.broadcastTxCommitSuccess(txRes)).toEqual(true); + + // txSearch - you must enable the indexer when running + // tendermint, else you get empty results + const query = buildQuery({ tags: [{ key: "app.key", value: txKey2 }] }); + + const search = await client.txSearch({ query: query, page: 1, per_page: 30 }); + // should find the tx + expect(search.totalCount).toEqual(1); + // should return same info as querying directly, + // except without the proof + expect(search.txs[0]).toEqual({ + hash: txRes.hash, + height: txRes.height, + index: 0, + tx: tx, + result: txRes.deliverTx!, + proof: undefined, + }); + + // Ensure txSearchAll works as well. This should be moved in a dedicated "txSearchAll" test block. + const searchAll = await client.txSearchAll({ query: query }); + expect(searchAll.totalCount).toEqual(1); + expect(searchAll.txs[0]).toEqual({ + hash: txRes.hash, + height: txRes.height, + index: 0, + tx: tx, + result: txRes.deliverTx!, + proof: undefined, + }); + }); + it("returns transactions in ascending order by default", async () => { // NOTE: The Tendermint docs states the default ordering is "desc". Until // 0.35 it was actually "asc" but from 0.35 on it is "desc". @@ -504,7 +549,7 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) pendingWithoutTendermint(); const client = await Tendermint37Client.create(rpcFactory()); - const query = buildQuery({ tags: [{ key: "app.key", value: key }] }); + const query = buildQuery({ tags: [{ key: "app.key", value: txKey }] }); const result = await client.txSearch({ query: query }); @@ -521,7 +566,7 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) pendingWithoutTendermint(); const client = await Tendermint37Client.create(rpcFactory()); - const query = buildQuery({ tags: [{ key: "app.key", value: key }] }); + const query = buildQuery({ tags: [{ key: "app.key", value: txKey }] }); const result1 = await client.txSearch({ query: query, order_by: "desc" }); const result2 = await client.txSearch({ query: query, order_by: "asc" }); @@ -536,7 +581,7 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) pendingWithoutTendermint(); const client = await Tendermint37Client.create(rpcFactory()); - const query = buildQuery({ tags: [{ key: "app.key", value: key }] }); + const query = buildQuery({ tags: [{ key: "app.key", value: txKey }] }); // expect one page of results const s1 = await client.txSearch({ query: query, page: 1, per_page: 2 }); @@ -555,15 +600,14 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues) pendingWithoutTendermint(); const client = await Tendermint37Client.create(rpcFactory()); - const query = buildQuery({ tags: [{ key: "app.key", value: key }] }); + const query = buildQuery({ tags: [{ key: "app.key", value: txKey }] }); const sall = await client.txSearchAll({ query: query, per_page: 2 }); expect(sall.totalCount).toEqual(3); expect(sall.txs.length).toEqual(3); // make sure there are in order from highest to lowest height - const [tx1, tx2, tx3] = sall.txs; - expect(tx2.height).toBeGreaterThan(tx1.height); - expect(tx3.height).toBeGreaterThan(tx2.height); + expect(sall.txs[1].height).toEqual(sall.txs[0].height + 1); + expect(sall.txs[2].height).toEqual(sall.txs[1].height + 1); client.disconnect(); });