From e59eef21b4faf619f2afb973453f1475a91b0054 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Wed, 5 Mar 2025 10:43:54 +0000 Subject: [PATCH] Allow transaction timeout in ChainheadBackend to be configured (#1943) * Add configurable transaction timeout to ChainheadBackend * u64, not usize * Actually, stick with usize --- subxt/src/backend/chain_head/mod.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/subxt/src/backend/chain_head/mod.rs b/subxt/src/backend/chain_head/mod.rs index 9306daeb00..5bb1211270 100644 --- a/subxt/src/backend/chain_head/mod.rs +++ b/subxt/src/backend/chain_head/mod.rs @@ -47,6 +47,7 @@ pub use subxt_rpcs::methods::chain_head::ChainHeadRpcMethods; /// Configure and build an [`ChainHeadBackend`]. pub struct ChainHeadBackendBuilder { max_block_life: usize, + transaction_timeout_secs: usize, _marker: std::marker::PhantomData, } @@ -61,6 +62,7 @@ impl ChainHeadBackendBuilder { pub fn new() -> Self { Self { max_block_life: usize::MAX, + transaction_timeout_secs: 240, _marker: std::marker::PhantomData, } } @@ -77,6 +79,16 @@ impl ChainHeadBackendBuilder { self } + /// When a transaction is submitted, we wait for events indicating it's successfully made it into a finalized + /// block. If it takes too long for this to happen, we assume that something went wrong and that we should + /// give up waiting. + /// + /// Provide a value here to denote how long, in seconds, to wait before giving up. Defaults to 240 seconds. + pub fn transaction_timeout(mut self, timeout_secs: usize) -> Self { + self.transaction_timeout_secs = timeout_secs; + self + } + /// A low-level API to build the backend and driver which requires polling the driver for the backend /// to make progress. /// @@ -104,6 +116,7 @@ impl ChainHeadBackendBuilder { let backend = ChainHeadBackend { methods: rpc_methods, follow_handle: follow_stream_driver.handle(), + transaction_timeout_secs: self.transaction_timeout_secs, }; let driver = ChainHeadBackendDriver { driver: follow_stream_driver, @@ -172,6 +185,8 @@ pub struct ChainHeadBackend { methods: ChainHeadRpcMethods, // A handle to the chainHead_follow subscription: follow_handle: FollowStreamDriverHandle, + // How long to wait until giving up on transactions: + transaction_timeout_secs: usize, } impl ChainHeadBackend { @@ -543,6 +558,8 @@ impl Backend for ChainHeadBackend { &self, extrinsic: &[u8], ) -> Result>, Error> { + let transaction_timeout_secs = self.transaction_timeout_secs as u64; + // We care about new and finalized block hashes. enum SeenBlockMarker { New, @@ -581,7 +598,7 @@ impl Backend for ChainHeadBackend { } // Bail if we exceed 4 mins; something very likely went wrong. - if start_instant.elapsed().as_secs() > 240 { + if start_instant.elapsed().as_secs() > transaction_timeout_secs { return Poll::Ready(err_other( "Timeout waiting for the transaction to be finalized", ));