Skip to content

Commit

Permalink
tests/lightclient: Switch to localnode for testing
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv committed Apr 9, 2024
1 parent 6c9088e commit 74dd9d7
Showing 1 changed file with 11 additions and 80 deletions.
91 changes: 11 additions & 80 deletions testing/integration-tests/src/light_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,30 @@
//! For more context see: https://github.com/tokio-rs/tokio/issues/2374.
//!
use crate::utils::node_runtime;
use crate::{test_context, utils::node_runtime};
use codec::Compact;
use subxt::{client::OnlineClient, config::PolkadotConfig, lightclient::LightClient};
use subxt::{client::OnlineClient, config::SubstrateConfig};
use subxt_metadata::Metadata;

type Client = OnlineClient<PolkadotConfig>;
type Client = OnlineClient<SubstrateConfig>;

// Check that we can subscribe to non-finalized blocks.
async fn non_finalized_headers_subscription(api: &Client) -> Result<(), subxt::Error> {
let now = std::time::Instant::now();

println!("In non_finalized_headers_subscription");
let mut sub = api.blocks().subscribe_best().await?;

let _block = sub.next().await.unwrap()?;
println!("First block took {:?}", now.elapsed());

let _block = sub.next().await.unwrap()?;
println!("Second block took {:?}", now.elapsed());

let _block = sub.next().await.unwrap()?;
println!("Third block took {:?}\n", now.elapsed());

Ok(())
}

// Check that we can subscribe to finalized blocks.
async fn finalized_headers_subscription(api: &Client) -> Result<(), subxt::Error> {
let now = std::time::Instant::now();

println!("In finalized_headers_subscription");

let mut sub = api.blocks().subscribe_finalized().await?;
let header = sub.next().await.unwrap()?;
println!("First block took {:?}", now.elapsed());

let finalized_hash = api
.backend()
Expand All @@ -70,51 +59,32 @@ async fn finalized_headers_subscription(api: &Client) -> Result<(), subxt::Error
.unwrap()
.hash();

println!(
"Finalized hash: {:?} took {:?}",
finalized_hash,
now.elapsed()
);

assert_eq!(header.hash(), finalized_hash);
println!("Check progress {:?}", now.elapsed());

let _block = sub.next().await.unwrap()?;
println!("Second block took {:?}", now.elapsed());
let _block = sub.next().await.unwrap()?;
println!("Third block took {:?}", now.elapsed());
let _block = sub.next().await.unwrap()?;
println!("Fourth block took {:?}\n", now.elapsed());

Ok(())
}

// Check that we can subscribe to non-finalized blocks.
async fn runtime_api_call(api: &Client) -> Result<(), subxt::Error> {
let now = std::time::Instant::now();
println!("In runtime_api_call");

let mut sub = api.blocks().subscribe_best().await?;

let block = sub.next().await.unwrap()?;
println!("First block took {:?}", now.elapsed());
let rt = block.runtime_api().await?;

// get metadata via state_call. if it decodes ok, it's probably all good.
let _ = rt
.call_raw::<(Compact<u32>, Metadata)>("Metadata_metadata", None)
.await?;

println!("Made runtime API call in {:?}\n", now.elapsed());

Ok(())
}

// Lookup for the `Timestamp::now` plain storage entry.
async fn storage_plain_lookup(api: &Client) -> Result<(), subxt::Error> {
let now = std::time::Instant::now();
println!("In storage_plain_lookup");

let addr = node_runtime::storage().timestamp().now();
let entry = api
.storage()
Expand All @@ -123,82 +93,43 @@ async fn storage_plain_lookup(api: &Client) -> Result<(), subxt::Error> {
.fetch_or_default(&addr)
.await?;

println!("Storage lookup took {:?}\n", now.elapsed());

assert!(entry > 0);

Ok(())
}

// Make a dynamic constant query for `System::BlockLenght`.
async fn dynamic_constant_query(api: &Client) -> Result<(), subxt::Error> {
let now = std::time::Instant::now();
println!("In dynamic_constant_query");

let constant_query = subxt::dynamic::constant("System", "BlockLength");
let _value = api.constants().at(&constant_query)?;

println!("Dynamic constant query took {:?}\n", now.elapsed());

Ok(())
}

// Fetch a few all events from the latest block and decode them dynamically.
async fn dynamic_events(api: &Client) -> Result<(), subxt::Error> {
let now = std::time::Instant::now();
println!("In dynamic_events");

let events = api.events().at_latest().await?;

for event in events.iter() {
let _event = event?;

println!("Event decoding took {:?}", now.elapsed());
}

println!("Dynamic events took {:?}\n", now.elapsed());

Ok(())
}

#[tokio::test]
#[subxt_test]
async fn light_client_testing() -> Result<(), subxt::Error> {
tracing_subscriber::fmt::init();
let now = std::time::Instant::now();

println!("Init lightclient");
// let chainspec = subxt::utils::fetch_chainspec_from_rpc_node("wss://rpc.polkadot.io:443")

// let chainspec = subxt::utils::fetch_chainspec_from_rpc_node("ws://127.0.0.1:9944")
// .await
// .unwrap();
// println!("Fetch spec took {:?}\n", now.elapsed());
// let bootnode = format!(
// "/ip4/127.0.0.1/tcp/30333/p2p/12D3KooWHdiAxVd8uMQR1hGWXccidmfCwLqcMpGwR6QcTP6QRMuD",
// );
// let chain_config = subxt::lightclient::ChainConfig::chain_spec(chainspec.get())
// .set_bootnodes([bootnode.as_str()])
// .map_err(|e| format!("Light client: cannot update boot nodes: {e}"))?;

let chainspec = subxt::utils::fetch_chainspec_from_rpc_node("wss://rpc.polkadot.io:443")
.await
.unwrap();

println!("Chain Spec: {:?}", chainspec);

let chain_config = chainspec.get();

let (_lc, rpc) = LightClient::relay_chain(chain_config)?;
let api = Client::from_rpc_client(rpc).await?;

println!("Light client initialization took {:?}\n", now.elapsed());
let ctx = test_context().await;
let api = ctx.client();

// non_finalized_headers_subscription(&api).await?;
non_finalized_headers_subscription(&api).await?;
finalized_headers_subscription(&api).await?;
// runtime_api_call(&api).await?;
// storage_plain_lookup(&api).await?;
// dynamic_constant_query(&api).await?;
// dynamic_events(&api).await?;
runtime_api_call(&api).await?;
storage_plain_lookup(&api).await?;
dynamic_constant_query(&api).await?;
dynamic_events(&api).await?;

Ok(())
}

0 comments on commit 74dd9d7

Please sign in to comment.