From 92a9e77b92f95698b6e5cbf0214b34d799ddc919 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> Date: Wed, 17 Jan 2024 14:49:30 +0200 Subject: [PATCH] lightclient(fix): Ensure lightclient chainSpec is at least one block old (#1372) * testing(fix): Ensure lightclient chainSpec is at least one block old Signed-off-by: Alexandru Vasile * Revert "testing(fix): Ensure lightclient chainSpec is at least one block old" This reverts commit 0eafcb2ca59d1f1cd2cef86b770f5a0401cce59f. * lightclient(fix): Ensure lightclient chainSpec is at least one block old Signed-off-by: Alexandru Vasile * lightclient: Link smoldot issue Signed-off-by: Alexandru Vasile * subxt: Use tokio under lightclient feature flag Signed-off-by: Alexandru Vasile * lightclient: Do not sleep on errors to fetch the chainSpec Signed-off-by: Alexandru Vasile * artifacts: Remove test file Signed-off-by: Alexandru Vasile * lightclient: Subscribe to two finalized blocks Signed-off-by: Alexandru Vasile * subxt: Revert cargo toml Signed-off-by: Alexandru Vasile --------- Signed-off-by: Alexandru Vasile --- subxt/src/client/light_client/builder.rs | 29 +++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/subxt/src/client/light_client/builder.rs b/subxt/src/client/light_client/builder.rs index ce0f7c6aca..2733fd4bb4 100644 --- a/subxt/src/client/light_client/builder.rs +++ b/subxt/src/client/light_client/builder.rs @@ -248,13 +248,36 @@ async fn build_client_from_rpc( /// Fetch the chain spec from the URL. #[cfg(feature = "jsonrpsee")] async fn fetch_url(url: impl AsRef) -> Result { - use jsonrpsee::core::client::ClientT; + use jsonrpsee::core::client::{ClientT, SubscriptionClientT}; + use jsonrpsee::rpc_params; + use serde_json::value::RawValue; + let client = jsonrpsee_helpers::client(url.as_ref()).await?; - client + let result = client .request("sync_state_genSyncSpec", jsonrpsee::rpc_params![true]) .await - .map_err(|err| Error::Rpc(crate::error::RpcError::ClientError(Box::new(err)))) + .map_err(|err| Error::Rpc(crate::error::RpcError::ClientError(Box::new(err))))?; + + // Subscribe to the finalized heads of the chain. + let mut subscription = SubscriptionClientT::subscribe::, _>( + &client, + "chain_subscribeFinalizedHeads", + rpc_params![], + "chain_unsubscribeFinalizedHeads", + ) + .await + .map_err(|err| Error::Rpc(crate::error::RpcError::ClientError(Box::new(err))))?; + + // We must ensure that the finalized block of the chain is not the block included + // in the chainSpec. + // This is a temporary workaround for: https://github.com/smol-dot/smoldot/issues/1562. + // The first finalized block that is received might by the finalized block could be the one + // included in the chainSpec. Decoding the chainSpec for this purpose is too complex. + let _ = subscription.next().await; + let _ = subscription.next().await; + + Ok(result) } cfg_jsonrpsee_native! {