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 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
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 Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ build-polka-storage-provider-server:

# Build the storagext CLI binary
build-storagext-cli:
cargo build --release --features storagext/insecure_url -p storagext-cli
cargo build --release -p storagext-cli

# Build the mater CLI binary
build-mater-cli:
Expand Down
2 changes: 1 addition & 1 deletion cli/polka-storage-provider/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ polka-storage-proofs = { workspace = true, features = ["std", "substrate"] }
polka-storage-provider-common = { workspace = true }
primitives-commitment = { workspace = true }
primitives-proofs = { workspace = true, features = ["clap", "std"] }
storagext = { workspace = true, features = ["clap", "insecure_url"] }
storagext = { workspace = true, features = ["clap"] }

async-trait = { workspace = true }
bls12_381 = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion cli/polka-storage-provider/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ version = "0.1.0"
# "Homegrown" crates
primitives-commitment = { workspace = true }
primitives-proofs = { workspace = true }
storagext = { workspace = true, features = ["clap", "insecure_url"] }
storagext = { workspace = true, features = ["clap"] }

chrono = { workspace = true, features = ["serde"] }
cid = { workspace = true, features = ["serde", "std"] }
Expand Down
2 changes: 1 addition & 1 deletion cli/polka-storage-provider/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ polka-storage-proofs = { workspace = true, features = ["std", "substrate"] }
polka-storage-provider-common = { workspace = true }
primitives-commitment = { workspace = true, features = ["serde", "std"] }
primitives-proofs = { workspace = true, features = ["clap"] }
storagext = { workspace = true, features = ["clap", "insecure_url"] }
storagext = { workspace = true, features = ["clap"] }

async-trait = { workspace = true }
axum = { workspace = true, features = ["macros", "multipart"] }
Expand Down
2 changes: 0 additions & 2 deletions cli/polka-storage/storagext-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ name = "storagext-cli"
repository.workspace = true
version = "0.1.0"

[features]
insecure_url = ["storagext/insecure_url"]

[dependencies]
storagext = { workspace = true, features = ["clap"] }
Expand Down
3 changes: 1 addition & 2 deletions cli/polka-storage/storagext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ version = "0.1.0"
[features]
clap = ["dep:clap"]
default = []
insecure_url = []

[dependencies]
anyhow.workspace = true
Expand All @@ -25,7 +24,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
47 changes: 19 additions & 28 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 @@ -30,9 +36,6 @@ pub struct Client {

impl Client {
/// Create a new [`RuntimeClient`] from a target `rpc_address`.
///
/// By default, this function does not support insecure URLs,
/// to enable support for them, use the `insecure_url` feature.
#[tracing::instrument(skip_all, fields(rpc_address = rpc_address.as_ref()))]
pub async fn new(
rpc_address: impl AsRef<str>,
Expand All @@ -41,30 +44,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
2 changes: 1 addition & 1 deletion docker/dockerfiles/polka-storage-node.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .
RUN cargo build --release --features polka-storage-runtime/testnet -p polka-storage-node
RUN cargo build --release --features storagext/insecure_url -p storagext-cli
RUN cargo build --release -p storagext-cli

FROM debian:bookworm-slim AS runtime

Expand Down
2 changes: 1 addition & 1 deletion docker/dockerfiles/storagext-cli.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ COPY --from=planner /app/rust-toolchain.toml rust-toolchain.toml
RUN cargo chef cook --bin storagext-cli --release --recipe-path recipe.json
# Build application
COPY . .
RUN cargo build --release --features storagext/insecure_url --bin storagext-cli
RUN cargo build --release --bin storagext-cli

################
##### Runtime
Expand Down
1 change: 0 additions & 1 deletion docs/src/getting-started/building/source.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ After all this setup, it is time to start building the binaries, which you can d

When building `polka-storage-node` you should add `--features polka-storage-runtime/testnet` which enables the testnet configuration; all the code in the repo is currently targeting this feature, not including it may lead to unexpected behavior.

When building `storagext-cli` you may want to add `--features storagext/insecure_url` which enables using non-TLS HTTP and WebSockets.
</div>

```bash
Expand Down
2 changes: 1 addition & 1 deletion maat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

primitives-proofs = { workspace = true, features = ["serde"] }
storagext = { workspace = true, features = ["insecure_url"] }
storagext = { workspace = true }

zombienet-configuration.workspace = true
zombienet-sdk.workspace = true
Expand Down