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
2 changes: 2 additions & 0 deletions crates/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ provider = { path = "../provider" }
# to review the exports for neeeded types
support = { path = "../support" }
tokio = { workspace = true }
futures = { workspace = true }
subxt = { workspace = true }
18 changes: 13 additions & 5 deletions crates/examples/examples/simple_network_example.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// use std::time::Duration;

use configuration::NetworkConfig;
use futures::stream::StreamExt;
use orchestrator::Orchestrator;
use provider::NativeProvider;
use support::{fs::local::LocalFileSystem, process::os::OsProcessManager};
Expand All @@ -14,11 +15,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let pm = OsProcessManager;
let provider = NativeProvider::new(fs.clone(), pm);
let orchestrator = Orchestrator::new(fs, provider);
orchestrator.spawn(config).await?;
let network = orchestrator.spawn(config).await?;
println!("🚀🚀🚀🚀 network deployed");
// For now let just loop....
#[allow(clippy::empty_loop)]
loop {}

// Ok(())
let client = network
.get_node("alice")?
.client::<subxt::PolkadotConfig>()
.await?;
let mut blocks = client.blocks().subscribe_finalized().await?.take(3);

while let Some(block) = blocks.next().await {
println!("Block #{}", block?.header().number);
}

Ok(())
}
13 changes: 13 additions & 0 deletions crates/orchestrator/src/network/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{sync::Arc, time::Duration};
use anyhow::anyhow;
use prom_metrics_parser::MetricMap;
use provider::DynNode;
use subxt::{backend::rpc::RpcClient, OnlineClient};
use tokio::sync::RwLock;

use crate::network_spec::node::NodeSpec;
Expand Down Expand Up @@ -45,6 +46,18 @@ impl NetworkNode {
Ok(())
}

/// Get the rpc client for the node
pub async fn rpc(&self) -> Result<RpcClient, subxt::Error> {
RpcClient::from_url(&self.ws_uri).await
}

/// Get the online client for the node
pub async fn client<Config: subxt::Config>(
&self,
) -> Result<OnlineClient<Config>, subxt::Error> {
OnlineClient::from_url(&self.ws_uri).await
}

/// Resume the node, this is implemented by resuming the
/// actual process (e.g polkadot) with sendig `SIGCONT` signal
pub async fn resume(&self) -> Result<(), anyhow::Error> {
Expand Down
Loading