mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-10-23 04:06:20 +00:00
* WIP second pass over light client code for simpler API * First pass new light client * pub(crate) LightClientRpc::new_raw(), and fmt * Update examples and add back a way to configure boot nodes and fetch chainspec from a URL * Fix light client examples * remove unused deps and tidy lightclient feature flags * fix wasm error * LightClientRpc can be cloned * update light client tests * Other small fixes * exclude mod unless jsonrpsee * Fix wasm-lightclient-tests * add back docsrs bit and web+native feature flag compile error * update book and light client example names * fix docs
114 lines
4.0 KiB
Rust
114 lines
4.0 KiB
Rust
// Copyright 2019-2024 Parity Technologies (UK) Ltd.
|
|
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
|
|
// see LICENSE for license details.
|
|
|
|
use crate::macros::{cfg_jsonrpsee_native, cfg_jsonrpsee_web};
|
|
use serde_json::value::RawValue;
|
|
|
|
/// Possible errors encountered trying to fetch a chain spec from an RPC node.
|
|
#[derive(thiserror::Error, Debug)]
|
|
#[allow(missing_docs)]
|
|
pub enum FetchChainspecError {
|
|
#[error("Cannot fetch chain spec: RPC error: {0}.")]
|
|
RpcError(String),
|
|
#[error("Cannot fetch chain spec: Invalid URL.")]
|
|
InvalidUrl,
|
|
#[error("Cannot fetch chain spec: Invalid URL scheme.")]
|
|
InvalidScheme,
|
|
#[error("Cannot fetch chain spec: Handshake error establishing WS connection.")]
|
|
HandshakeError,
|
|
}
|
|
|
|
/// Fetch a chain spec from an RPC node at the given URL.
|
|
pub async fn fetch_chainspec_from_rpc_node(
|
|
url: impl AsRef<str>,
|
|
) -> Result<Box<RawValue>, FetchChainspecError> {
|
|
use jsonrpsee::core::client::{ClientT, SubscriptionClientT};
|
|
use jsonrpsee::rpc_params;
|
|
|
|
let client = jsonrpsee_helpers::client(url.as_ref()).await?;
|
|
|
|
let result = client
|
|
.request("sync_state_genSyncSpec", jsonrpsee::rpc_params![true])
|
|
.await
|
|
.map_err(|err| FetchChainspecError::RpcError(err.to_string()))?;
|
|
|
|
// Subscribe to the finalized heads of the chain.
|
|
let mut subscription = SubscriptionClientT::subscribe::<Box<RawValue>, _>(
|
|
&client,
|
|
"chain_subscribeFinalizedHeads",
|
|
rpc_params![],
|
|
"chain_unsubscribeFinalizedHeads",
|
|
)
|
|
.await
|
|
.map_err(|err| FetchChainspecError::RpcError(err.to_string()))?;
|
|
|
|
// 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! {
|
|
mod jsonrpsee_helpers {
|
|
use super::FetchChainspecError;
|
|
use tokio_util::compat::Compat;
|
|
|
|
pub use jsonrpsee::{
|
|
client_transport::ws::{self, EitherStream, Url, WsTransportClientBuilder},
|
|
core::client::Client,
|
|
};
|
|
|
|
pub type Sender = ws::Sender<Compat<EitherStream>>;
|
|
pub type Receiver = ws::Receiver<Compat<EitherStream>>;
|
|
|
|
/// Build WS RPC client from URL
|
|
pub async fn client(url: &str) -> Result<Client, FetchChainspecError> {
|
|
let url = Url::parse(url).map_err(|_| FetchChainspecError::InvalidUrl)?;
|
|
|
|
if url.scheme() != "ws" && url.scheme() != "wss" {
|
|
return Err(FetchChainspecError::InvalidScheme);
|
|
}
|
|
|
|
let (sender, receiver) = ws_transport(url).await?;
|
|
|
|
Ok(Client::builder()
|
|
.max_buffer_capacity_per_subscription(4096)
|
|
.build_with_tokio(sender, receiver))
|
|
}
|
|
|
|
async fn ws_transport(url: Url) -> Result<(Sender, Receiver), FetchChainspecError> {
|
|
WsTransportClientBuilder::default()
|
|
.build(url)
|
|
.await
|
|
.map_err(|_| FetchChainspecError::HandshakeError)
|
|
}
|
|
}
|
|
}
|
|
|
|
cfg_jsonrpsee_web! {
|
|
mod jsonrpsee_helpers {
|
|
use super::FetchChainspecError;
|
|
pub use jsonrpsee::{
|
|
client_transport::web,
|
|
core::client::{Client, ClientBuilder},
|
|
};
|
|
|
|
/// Build web RPC client from URL
|
|
pub async fn client(url: &str) -> Result<Client, FetchChainspecError> {
|
|
let (sender, receiver) = web::connect(url)
|
|
.await
|
|
.map_err(|_| FetchChainspecError::HandshakeError)?;
|
|
|
|
Ok(ClientBuilder::default()
|
|
.max_buffer_capacity_per_subscription(4096)
|
|
.build_with_wasm(sender, receiver))
|
|
}
|
|
}
|
|
}
|