Skip to content

Commit

Permalink
testing(fix): Ensure lightclient chainSpec is at least one block old
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <[email protected]>
  • Loading branch information
lexnv committed Jan 17, 2024
1 parent 5533435 commit 0eafcb2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions testing/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ futures = { workspace = true }
hex = { workspace = true }
regex = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
jsonrpsee = { workspace = true, features = ["async-client", "client-ws-transport-native-tls"] }
scale-info = { workspace = true, features = ["bit-vec"] }
sp-core = { workspace = true }
syn = { workspace = true }
Expand Down
35 changes: 34 additions & 1 deletion testing/integration-tests/src/light_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,43 @@ async fn dynamic_events(api: &Client) -> Result<(), subxt::Error> {
Ok(())
}

/// Fetch the chainSpec from the given url.
async fn fetch_spec_from_url(url: &str) -> serde_json::Value {
// use jsonrpsee::core::client::ClientT;
// let client = jsonrpsee_helpers::client(url.as_ref()).await?;

pub use jsonrpsee::{
client_transport::ws::{self, EitherStream, Url, WsTransportClientBuilder},
core::client::{Client, ClientT},
};

let url = Url::parse(url).expect("Failed to parse url");
let (sender, receiver) = WsTransportClientBuilder::default()
.build(url)
.await
.expect("Failed to connect to the node");

let client = Client::builder()
.max_buffer_capacity_per_subscription(4096)
.build_with_tokio(sender, receiver);

client
.request("sync_state_genSyncSpec", jsonrpsee::rpc_params![true])
.await
.expect("Failed to fetch the chainSpec")
}

#[tokio::test]
async fn light_client_testing() -> Result<(), subxt::Error> {
let chain_spec = fetch_spec_from_url("wss://rpc.polkadot.io:443").await;

// Sleep 8 seconds to ensure that a new block is produced for the substrate test node.
// Although the block production should be 6 seconds, the CI environment is sometimes slow.
// This is a temporary workaround for: https://github.com/smol-dot/smoldot/issues/1562.
tokio::time::sleep(std::time::Duration::from_secs(8)).await;

let api: LightClient<PolkadotConfig> = LightClientBuilder::new()
.build_from_url("wss://rpc.polkadot.io:443")
.build(&chain_spec.to_string())
.await?;

non_finalized_headers_subscription(&api).await?;
Expand Down

0 comments on commit 0eafcb2

Please sign in to comment.