From 4eac2d89526b9dba7fc7499796187f25236bda6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thoralf=20M=C3=BCller?= Date: Thu, 28 Mar 2024 21:32:22 +0100 Subject: [PATCH 1/2] Adjust wait_for_transaction_acceptance for slow indexer :sadcat: --- sdk/src/client/api/wait_for_tx_acceptance.rs | 42 +++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/sdk/src/client/api/wait_for_tx_acceptance.rs b/sdk/src/client/api/wait_for_tx_acceptance.rs index 778a6a2f38..ba8a2e53f3 100644 --- a/sdk/src/client/api/wait_for_tx_acceptance.rs +++ b/sdk/src/client/api/wait_for_tx_acceptance.rs @@ -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); @@ -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 thought 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 => { From 2b6c46638b6685d60a981574ea546a93eb6c760e Mon Sep 17 00:00:00 2001 From: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> Date: Fri, 29 Mar 2024 21:19:57 +0100 Subject: [PATCH 2/2] Update sdk/src/client/api/wait_for_tx_acceptance.rs --- sdk/src/client/api/wait_for_tx_acceptance.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/client/api/wait_for_tx_acceptance.rs b/sdk/src/client/api/wait_for_tx_acceptance.rs index ba8a2e53f3..84b84bbe3f 100644 --- a/sdk/src/client/api/wait_for_tx_acceptance.rs +++ b/sdk/src/client/api/wait_for_tx_acceptance.rs @@ -40,7 +40,7 @@ impl Client { .output .required_address(slot_index, protocol_parameters.committable_age_range())? { - // Even thought the output was created already, the indexer might take some time + // 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