chore(deps): Update smoldot to the latest version (#1400)

* Update smoldot to 0.17 and smoldot-light to 0.15

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update cargo lock

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Add generic platform for AddedChain

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* debug: Finalized heads

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Use generic TPlat for chainSuccess

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Trim response for logs

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Backup

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests/lightclient: Switch to localnode for testing

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cargo: Point smoldot to crates.io

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Solve merge conflicts

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Add subxt macro for tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient/wasm: Impl log of the PlatformRef

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Use git dep

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert "tests/lightclient: Switch to localnode for testing" + max log
size

This reverts commit 74dd9d7cff.

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Comment chainspec

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient/wasm: Import IpAddr from core::net

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Enable all tests again

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests/wasm: Update cargo lock

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Add trace logs to easily reproduce problems

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cargo: Use released smoldot version

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Use chainspec and optionally make use of unstable backend

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Fix clippy

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Better trimming for log messages

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Remove max log size

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Use both backends for testing

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update Cargo.toml

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Update Cargo.toml

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Update testing/integration-tests/src/light_client/mod.rs

* Update testing/integration-tests/src/light_client/mod.rs

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
This commit is contained in:
Alexandru Vasile
2024-09-09 10:30:51 +03:00
committed by GitHub
parent 246865da1f
commit 1cf206f671
7 changed files with 237 additions and 85 deletions
@@ -29,26 +29,47 @@
use crate::utils::node_runtime;
use codec::Compact;
use futures::StreamExt;
use std::sync::Arc;
use subxt::backend::rpc::RpcClient;
use subxt::backend::unstable::UnstableBackend;
use subxt::{client::OnlineClient, config::PolkadotConfig, lightclient::LightClient};
use subxt_metadata::Metadata;
type Client = OnlineClient<PolkadotConfig>;
/// The Polkadot chainspec.
const POLKADOT_SPEC: &str = include_str!("../../../../artifacts/demo_chain_specs/polkadot.json");
// 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();
tracing::trace!("Check non_finalized_headers_subscription");
let mut sub = api.blocks().subscribe_best().await?;
let _block = sub.next().await.unwrap()?;
tracing::trace!("First block took {:?}", now.elapsed());
let _block = sub.next().await.unwrap()?;
tracing::trace!("Second block took {:?}", now.elapsed());
let _block = sub.next().await.unwrap()?;
tracing::trace!("Third block took {:?}", 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();
tracing::trace!("Check finalized_headers_subscription");
let mut sub = api.blocks().subscribe_finalized().await?;
let header = sub.next().await.unwrap()?;
tracing::trace!("First block took {:?}", now.elapsed());
let finalized_hash = api
.backend()
.latest_finalized_block_ref()
@@ -56,20 +77,34 @@ async fn finalized_headers_subscription(api: &Client) -> Result<(), subxt::Error
.unwrap()
.hash();
tracing::trace!(
"Finalized hash: {:?} took {:?}",
finalized_hash,
now.elapsed()
);
assert_eq!(header.hash(), finalized_hash);
tracing::trace!("Check progress {:?}", now.elapsed());
let _block = sub.next().await.unwrap()?;
tracing::trace!("Second block took {:?}", now.elapsed());
let _block = sub.next().await.unwrap()?;
tracing::trace!("Third block took {:?}", now.elapsed());
let _block = sub.next().await.unwrap()?;
tracing::trace!("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();
tracing::trace!("Check runtime_api_call");
let mut sub = api.blocks().subscribe_best().await?;
let block = sub.next().await.unwrap()?;
tracing::trace!("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.
@@ -77,11 +112,16 @@ async fn runtime_api_call(api: &Client) -> Result<(), subxt::Error> {
.call_raw::<(Compact<u32>, Metadata)>("Metadata_metadata", None)
.await?;
tracing::trace!("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();
tracing::trace!("Check storage_plain_lookup");
let addr = node_runtime::storage().timestamp().now();
let entry = api
.storage()
@@ -90,6 +130,8 @@ async fn storage_plain_lookup(api: &Client) -> Result<(), subxt::Error> {
.fetch_or_default(&addr)
.await?;
tracing::trace!("Storage lookup took {:?}\n", now.elapsed());
assert!(entry > 0);
Ok(())
@@ -97,30 +139,75 @@ async fn storage_plain_lookup(api: &Client) -> Result<(), subxt::Error> {
// Make a dynamic constant query for `System::BlockLenght`.
async fn dynamic_constant_query(api: &Client) -> Result<(), subxt::Error> {
let now = std::time::Instant::now();
tracing::trace!("Check dynamic_constant_query");
let constant_query = subxt::dynamic::constant("System", "BlockLength");
let _value = api.constants().at(&constant_query)?;
tracing::trace!("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();
tracing::trace!("Check dynamic_events");
let events = api.events().at_latest().await?;
for event in events.iter() {
let _event = event?;
tracing::trace!("Event decoding took {:?}", now.elapsed());
}
tracing::trace!("Dynamic events took {:?}\n", now.elapsed());
Ok(())
}
#[tokio::test]
async fn light_client_testing() -> Result<(), subxt::Error> {
let chainspec = subxt::utils::fetch_chainspec_from_rpc_node("wss://rpc.polkadot.io:443")
.await
.unwrap();
let (_lc, rpc) = LightClient::relay_chain(chainspec.get())?;
let api = Client::from_rpc_client(rpc).await?;
async fn run_test(backend: BackendType) -> Result<(), subxt::Error> {
// Note: This code fetches the chainspec from the Polkadot public RPC node.
// This is not recommended for production use, as it may be slow and unreliable.
// However, this can come in handy for testing purposes.
//
// let chainspec = subxt::utils::fetch_chainspec_from_rpc_node("wss://rpc.polkadot.io:443")
// .await
// .unwrap();
// let chain_config = chainspec.get();
tracing::trace!("Init light clinet");
let now = std::time::Instant::now();
let (_lc, rpc) = LightClient::relay_chain(POLKADOT_SPEC)?;
let api = match backend {
BackendType::Unstable => {
let (backend, mut driver) = UnstableBackend::builder().build(RpcClient::new(rpc));
tokio::spawn(async move {
while let Some(val) = driver.next().await {
if let Err(e) = val {
if e.is_disconnected_will_reconnect() {
tracing::info!(
"The RPC connection was lost and we may have missed a few blocks"
);
continue;
}
tracing::error!("Error driving unstable backend: {e}");
}
}
});
let api: OnlineClient<PolkadotConfig> =
OnlineClient::from_backend(Arc::new(backend)).await?;
api
}
BackendType::Legacy => Client::from_rpc_client(rpc).await?,
};
tracing::trace!("Light client initialization took {:?}", now.elapsed());
non_finalized_headers_subscription(&api).await?;
finalized_headers_subscription(&api).await?;
@@ -129,5 +216,25 @@ async fn light_client_testing() -> Result<(), subxt::Error> {
dynamic_constant_query(&api).await?;
dynamic_events(&api).await?;
tracing::trace!("Light complete testing took {:?}", now.elapsed());
Ok(())
}
/// Backend type for light client testing.
enum BackendType {
/// Use the unstable backend (ie chainHead).
Unstable,
/// Use the legacy backend.
Legacy,
}
#[tokio::test]
async fn light_client_testing() -> Result<(), subxt::Error> {
tracing_subscriber::fmt::init();
// Run light client test with both backends.
run_test(BackendType::Unstable).await?;
run_test(BackendType::Legacy).await?;
Ok(())
}