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

Expose subxt onclineclient / rpc #125

Merged
merged 13 commits into from
Oct 24, 2023
Prev Previous commit
Next Next commit
remove retry / create client lazyly
  • Loading branch information
pgherveou committed Oct 23, 2023
commit 0480e1050e308c1c26ca9d1a8ecd012f404e53b0
2 changes: 1 addition & 1 deletion crates/examples/examples/simple_network_example.rs
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let network = orchestrator.spawn(config).await?;
println!("🚀🚀🚀🚀 network deployed");

let client = network.get_node("alice")?.client();
let client = network.get_node("alice")?.client().await?;
let mut blocks = client.blocks().subscribe_finalized().await?.take(3);

while let Some(block) = blocks.next().await {
14 changes: 4 additions & 10 deletions crates/orchestrator/src/network/node.rs
Original file line number Diff line number Diff line change
@@ -16,8 +16,6 @@ pub struct NetworkNode {
pub(crate) spec: NodeSpec,
pub(crate) name: String,
pub(crate) ws_uri: String,
rpc: RpcClient,
client: OnlineClient<PolkadotConfig>,
pub(crate) prometheus_uri: String,
metrics_cache: Arc<RwLock<MetricMap>>,
}
@@ -27,17 +25,13 @@ impl NetworkNode {
pub(crate) fn new<T: Into<String>>(
name: T,
ws_uri: T,
rpc: RpcClient,
client: OnlineClient<PolkadotConfig>,
prometheus_uri: T,
spec: NodeSpec,
inner: DynNode,
) -> Self {
Self {
name: name.into(),
ws_uri: ws_uri.into(),
rpc,
client,
prometheus_uri: prometheus_uri.into(),
inner,
spec,
@@ -52,12 +46,12 @@ impl NetworkNode {
Ok(())
}

pub fn rpc(&self) -> RpcClient {
self.rpc.clone()
pub async fn rpc(&self) -> Result<RpcClient, subxt::Error> {
RpcClient::from_url(&self.ws_uri).await
}

pub fn client(&self) -> OnlineClient<PolkadotConfig> {
self.client.clone()
pub async fn client(&self) -> Result<OnlineClient<PolkadotConfig>, subxt::Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does make sense to allow users to also specify the concrete Config type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm good point. Using moonbeam for example you would need to specify a custom config.
A bit of a shame that Rust does not let you specify a default value for the generic trait here.

I mean you could but then rustc complains with

defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions  this was previously accepted by the compiler but is being phased out;

OnlineClient::from_url(&self.ws_uri).await
}

/// Resume the node, this is implemented by resuming the
12 changes: 0 additions & 12 deletions crates/orchestrator/src/spawner.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::path::PathBuf;

use anyhow::Context;
use futures::Future;
pgherveou marked this conversation as resolved.
Show resolved Hide resolved
use provider::{
constants::LOCALHOST,
types::{SpawnNodeOptions, TransferedFile},
DynNamespace,
};
use subxt::{backend::rpc::RpcClient, OnlineClient};
use support::fs::FileSystem;

use crate::{
@@ -163,19 +161,9 @@ where
println!("📓 logs cmd: tail -f {}/{}.log", base_dir, node.name);
println!("\n");

pgherveou marked this conversation as resolved.
Show resolved Hide resolved
let client = retry(5, || async { OnlineClient::from_url(&ws_uri).await })
.await
.context(format!("Failed to connect to node rpc at {ws_uri}"))?;

let rpc = RpcClient::from_url(&ws_uri)
.await
.context(format!("Failed to connect to rpc client at {ws_uri}"))?;

Ok(NetworkNode::new(
node.name.clone(),
ws_uri,
rpc,
client,
prometheus_uri,
node.clone(),
running_node,