Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust wait_for_transaction_acceptance for slow indexer #2210

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading