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

fix(storagext): use subxt retry mechanism instead of hand written #611

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/polka-storage/storagext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ primitives-proofs = { workspace = true, features = ["serde"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sha2 = { workspace = true }
subxt = { workspace = true, features = ["jsonrpsee", "substrate-compat"] }
subxt = { workspace = true, features = ["jsonrpsee", "reconnecting-rpc-client", "substrate-compat"] }
subxt-signer = { workspace = true, features = ["subxt"] }
thiserror = { workspace = true, default-features = true }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
Expand Down
44 changes: 19 additions & 25 deletions cli/polka-storage/storagext/src/runtime/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ use std::time::Duration;

use codec::Encode;
use hex::ToHex;
use subxt::{blocks::Block, events::Events, utils::H256, OnlineClient};
use subxt::{
backend::rpc::reconnecting_rpc_client::{FixedInterval, RpcClient},
blocks::Block,
events::Events,
utils::H256,
OnlineClient,
};

use crate::PolkaStorageConfig;

Expand Down Expand Up @@ -41,30 +47,18 @@ impl Client {
) -> Result<Self, subxt::Error> {
let rpc_address = rpc_address.as_ref();

let mut current_retries = 0;
loop {
let client = if cfg!(feature = "insecure_url") {
jmg-duarte marked this conversation as resolved.
Show resolved Hide resolved
OnlineClient::<_>::from_insecure_url(rpc_address).await
} else {
OnlineClient::<_>::from_url(rpc_address).await
};

match client {
Ok(client) => return Ok(Self { client }),
Err(err) => {
tracing::error!(
attempt = current_retries,
"failed to connect to node, error: {}",
err
);
current_retries += 1;
if current_retries >= n_retries {
return Err(err);
}
tokio::time::sleep(retry_interval).await;
}
}
}
let rpc_client = RpcClient::builder()
// the cast should never pose an issue since storagext is target at 64bit systems
.retry_policy(FixedInterval::new(retry_interval).take(n_retries as usize))
.build(rpc_address)
// subxt-style conversion
// https://github.com/paritytech/subxt/blob/v0.38.0/subxt/src/backend/rpc/rpc_client.rs#L38
.await
.map_err(|e| subxt::error::RpcError::ClientError(Box::new(e)))?;

Ok(Self {
client: OnlineClient::<_>::from_rpc_client(rpc_client).await?,
})
}

pub(crate) async fn unsigned<Call>(
Expand Down