Skip to content

Commit

Permalink
cosmos-sdk-rs: make Tx::find_by_hash use the /tx endpoint
Browse files Browse the repository at this point in the history
Notably this endpoint provides read-your-writes consistency with regard
to transactions, whereas `/tx_search` does not.

See also: tendermint/tendermint#6359
  • Loading branch information
tony-iqlusion committed Sep 27, 2021
1 parent 000e3f6 commit 8c72a5c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 33 deletions.
19 changes: 1 addition & 18 deletions cosmrs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
//! This module contains support for integration testing against a
//! Cosmos SDK-compatible full node (gaia) running inside of Docker.

use crate::{
rpc::{self, Client},
tx::{self, Tx},
};
use crate::rpc::{self, Client};
use std::{ffi::OsStr, panic, process, str, time::Duration};
use tokio::time;

Expand Down Expand Up @@ -97,17 +94,3 @@ pub async fn poll_for_first_block(rpc_client: &rpc::HttpClient) {
time::sleep(Duration::from_millis(200)).await;
}
}

/// Wait for a transaction with the given hash to appear in the blockchain
pub async fn poll_for_tx(rpc_client: &rpc::HttpClient, tx_hash: &tx::Hash) -> Tx {
let attempts = 5;

for _ in 0..attempts {
// TODO(tarcieri): handle not found errors
if let Ok(tx) = Tx::find_by_hash(rpc_client, tx_hash).await {
return tx;
}
}

panic!("couldn't find transaction after {} attempts!", attempts);
}
16 changes: 3 additions & 13 deletions cosmrs/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,12 @@ impl Tx {
/// Use RPC to find a transaction by its hash.
#[cfg(feature = "rpc")]
#[cfg_attr(docsrs, doc(cfg(feature = "rpc")))]
pub async fn find_by_hash<C>(rpc_client: &C, tx_hash: &Hash) -> Result<Tx>
pub async fn find_by_hash<C>(rpc_client: &C, tx_hash: Hash) -> Result<Tx>
where
C: rpc::Client + Send + Sync,
{
let query = rpc::query::Query::from(rpc::query::EventType::Tx)
.and_eq("tx.hash", tx_hash.to_string());

let response = rpc_client
.tx_search(query, false, 1, 1, rpc::Order::Ascending)
.await?;

if response.total_count == 1 {
Tx::from_bytes(response.txs[0].tx.as_bytes())
} else {
Err(Error::TxNotFound { hash: *tx_hash }.into())
}
let response = rpc_client.tx(tx_hash, false).await?;
Tx::from_bytes(response.tx.as_bytes())
}
}

Expand Down
6 changes: 4 additions & 2 deletions cosmrs/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cosmrs::{
bank::MsgSend,
crypto::secp256k1,
dev, rpc,
tx::{self, AccountNumber, Fee, MsgType, SignDoc, SignerInfo},
tx::{self, AccountNumber, Fee, MsgType, SignDoc, SignerInfo, Tx},
Coin,
};
use std::{panic, str};
Expand Down Expand Up @@ -93,7 +93,9 @@ fn msg_send() {
panic!("deliver_tx failed: {:?}", tx_commit_response.deliver_tx);
}

let tx = dev::poll_for_tx(&rpc_client, &tx_commit_response.hash).await;
let tx = Tx::find_by_hash(&rpc_client, tx_commit_response.hash)
.await
.unwrap();
assert_eq!(&tx_body, &tx.body);
assert_eq!(&auth_info, &tx.auth_info);
})
Expand Down

0 comments on commit 8c72a5c

Please sign in to comment.