Skip to content

Commit

Permalink
Adjust wait_for_transaction_acceptance for slow indexer (#2210)
Browse files Browse the repository at this point in the history
* Adjust wait_for_transaction_acceptance for slow indexer :sadcat:

* Update sdk/src/client/api/wait_for_tx_acceptance.rs

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Thoralf-M and thibault-martinez authored Apr 2, 2024
1 parent 2f9d0e7 commit 2ab2750
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions sdk/src/client/api/wait_for_tx_acceptance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
use std::time::Duration;

use crate::{
client::{Client, ClientError},
types::{api::core::TransactionState, block::payload::signed_transaction::TransactionId},
client::{node_api::indexer::query_parameters::OutputQueryParameters, Client, ClientError},
types::{
api::core::TransactionState,
block::{address::ToBech32Ext, output::OutputId, payload::signed_transaction::TransactionId},
},
};

pub(crate) const DEFAULT_WAIT_FOR_TX_ACCEPTANCE_INTERVAL: Duration = Duration::from_millis(500);
Expand All @@ -30,6 +33,41 @@ impl Client {
Ok(transaction_metadata) => {
match transaction_metadata.transaction_state {
TransactionState::Accepted | TransactionState::Committed | TransactionState::Finalized => {
let slot_index = self.get_slot_index().await?;
let protocol_parameters = self.get_protocol_parameters().await?;
if let Ok(output) = self.get_output(&OutputId::new(*transaction_id, 0)).await {
if let Some(required_address) = output
.output
.required_address(slot_index, protocol_parameters.committable_age_range())?
{
// Even though the output was created already, the indexer might take some time
// until it returns the output id for the address, that's why we wait for this here.
for _ in 0..20 {
if let Ok(output_ids) = self
.output_ids(OutputQueryParameters::new().unlockable_by_address(
required_address.clone().to_bech32(protocol_parameters.bech32_hrp),
))
.await
{
if output_ids.contains(&OutputId::new(*transaction_id, 0)) {
return Ok(());
}
}
let duration = std::time::Duration::from_millis(50);
#[cfg(target_family = "wasm")]
gloo_timers::future::TimeoutFuture::new(duration.as_millis() as u32).await;
#[cfg(not(target_family = "wasm"))]
tokio::time::sleep(duration).await;
}
}
}
// Just wait a second if the output was not returned or the required_address is None, so
// that the output should then be available from the indexer.
let duration = std::time::Duration::from_secs(1);
#[cfg(target_family = "wasm")]
gloo_timers::future::TimeoutFuture::new(duration.as_millis() as u32).await;
#[cfg(not(target_family = "wasm"))]
tokio::time::sleep(duration).await;
return Ok(());
}
TransactionState::Failed => {
Expand Down

0 comments on commit 2ab2750

Please sign in to comment.