Skip to content

Commit 7ff2133

Browse files
committed
cosmos-sdk-rs: make Tx::find_by_hash use the /tx endpoint
Notably this endpoint provides read-your-writes consistency with regard to transactions, whereas `/tx_search` does not. See also: tendermint/tendermint#6359
1 parent ba012bd commit 7ff2133

File tree

3 files changed

+8
-33
lines changed

3 files changed

+8
-33
lines changed

cosmos-sdk-rs/src/dev.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
//! This module contains support for integration testing against a
44
//! Cosmos SDK-compatible full node (gaia) running inside of Docker.
55
6-
use crate::{
7-
rpc::{self, Client},
8-
tx::{self, Tx},
9-
};
6+
use crate::rpc::{self, Client};
107
use std::{ffi::OsStr, panic, process, str, time::Duration};
118
use tokio::time;
129

@@ -97,17 +94,3 @@ pub async fn poll_for_first_block(rpc_client: &rpc::HttpClient) {
9794
time::sleep(Duration::from_millis(200)).await;
9895
}
9996
}
100-
101-
/// Wait for a transaction with the given hash to appear in the blockchain
102-
pub async fn poll_for_tx(rpc_client: &rpc::HttpClient, tx_hash: &tx::Hash) -> Tx {
103-
let attempts = 5;
104-
105-
for _ in 0..attempts {
106-
// TODO(tarcieri): handle not found errors
107-
if let Ok(tx) = Tx::find_by_hash(rpc_client, tx_hash).await {
108-
return tx;
109-
}
110-
}
111-
112-
panic!("couldn't find transaction after {} attempts!", attempts);
113-
}

cosmos-sdk-rs/src/tx.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,12 @@ impl Tx {
162162
/// Use RPC to find a transaction by its hash.
163163
#[cfg(feature = "rpc")]
164164
#[cfg_attr(docsrs, doc(cfg(feature = "rpc")))]
165-
pub async fn find_by_hash<C>(rpc_client: &C, tx_hash: &Hash) -> Result<Tx>
165+
pub async fn find_by_hash<C>(rpc_client: &C, tx_hash: Hash) -> Result<Tx>
166166
where
167167
C: rpc::Client + Send + Sync,
168168
{
169-
let query = rpc::query::Query::from(rpc::query::EventType::Tx)
170-
.and_eq("tx.hash", tx_hash.to_string());
171-
172-
let response = rpc_client
173-
.tx_search(query, false, 1, 1, rpc::Order::Ascending)
174-
.await?;
175-
176-
if response.total_count == 1 {
177-
Tx::from_bytes(response.txs[0].tx.as_bytes())
178-
} else {
179-
Err(Error::TxNotFound { hash: *tx_hash }.into())
180-
}
169+
let response = rpc_client.tx(tx_hash, false).await?;
170+
Tx::from_bytes(response.tx.as_bytes())
181171
}
182172
}
183173

cosmos-sdk-rs/tests/integration.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use cosmos_sdk::{
88
bank::MsgSend,
99
crypto::secp256k1,
1010
dev, rpc,
11-
tx::{self, AccountNumber, Fee, MsgType, SignDoc, SignerInfo},
11+
tx::{self, AccountNumber, Fee, MsgType, SignDoc, SignerInfo, Tx},
1212
Coin,
1313
};
1414
use std::{panic, str};
@@ -93,7 +93,9 @@ fn msg_send() {
9393
panic!("deliver_tx failed: {:?}", tx_commit_response.deliver_tx);
9494
}
9595

96-
let tx = dev::poll_for_tx(&rpc_client, &tx_commit_response.hash).await;
96+
let tx = Tx::find_by_hash(&rpc_client, tx_commit_response.hash)
97+
.await
98+
.unwrap();
9799
assert_eq!(&tx_body, &tx.body);
98100
assert_eq!(&auth_info, &tx.auth_info);
99101
})

0 commit comments

Comments
 (0)