Add light client platform WASM compatible (#1026)

* Cargo update in prep for wasm build

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

* Add light client test

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

* Implement low level socket

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

* Add native platform primitives

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

* Add wasm platform primitives

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

* Implement smoldot platform

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

* Adjust code to use custom platform

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

* Adjust feature flags

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

* tests: Adjust wasm endpoint to accept ws for p2p

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

* Adjust wasm socket

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

* Book mention of wasm

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

* ci: Propagate env variable properly

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

* subxt: Revert to native feature flags

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

* cli: Use tokio rt-multi-thread feature

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

* subxt: Add tokio feature flags for native platform

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

* wasm: Use polkadot live for wasm testing

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

* wasm: Add support for DNS p2p addresses

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

* wasm: Disable logs

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

* wasm: Run wasm test for firefox driver

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

* wasm: Reenable chrome driver

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

* Move lightclient RPC to dedicated crate for better feature flags and modularity

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

* Use subxt-lightclient low level RPC crate

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

* Apply cargo fmt

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

* Enable default:native feature for cargo check

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

* ci: Extra step for subxt-lightclient similar to signer crate

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

* Remove native platform code and use smoldot instead

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

* codegen: Enable tokio/multi-threads

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

* lightclient: Refactor modules

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

* Adjust testing crates

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

* ci: Run light-client WASM tests

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

* wasm-rpc: Remove light-client imports

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

* testing: Update wasm cargo.lock files

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

* ci: Spawn substrate node with deterministic p2p address for WASM tests

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

* wasm_socket: Use rc and refcell

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

* wasm_socket: Switch back to Arc<Mutex<>>

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

* Add comments

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

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2023-07-18 14:27:16 +03:00
committed by GitHub
parent ab2f2a8cdf
commit 4bda673847
28 changed files with 5280 additions and 305 deletions
+21 -3
View File
@@ -20,6 +20,8 @@ env:
CARGO_TERM_COLOR: always
# TODO: Currently pointing at latest substrate; is there a suitable binary we can pin to here?
SUBSTRATE_URL: https://releases.parity.io/substrate/x86_64-debian:bullseye/latest/substrate/substrate
# Increase wasm test timeout from 20 seconds (default) to 1 minute.
WASM_BINDGEN_TEST_TIMEOUT: 60
jobs:
build:
@@ -70,9 +72,13 @@ jobs:
- name: Cargo check subxt-signer
run: cargo check -p subxt-signer
# We can't enable web features here, so no cargo hack.
- name: Cargo check subxt-lightclient
run: cargo check -p subxt-lightclient
# Next, check each other package in isolation.
- name: Cargo hack; check each feature/crate on its own
run: cargo hack --exclude subxt --exclude subxt-signer --exclude-all-features --each-feature check --workspace
run: cargo hack --exclude subxt --exclude subxt-signer --exclude subxt-lightclient --exclude-all-features --each-feature check --workspace
fmt:
name: Cargo fmt
@@ -259,11 +265,23 @@ jobs:
- name: Run subxt WASM tests
run: |
substrate --dev --tmp > /dev/null 2>&1 &
# `listen-addr` is used to configure p2p to accept websocket connections instead of TCP.
# `node-key` provides a deterministic p2p address.
substrate --dev --node-key 0000000000000000000000000000000000000000000000000000000000000001 --listen-addr /ip4/0.0.0.0/tcp/30333/ws > /dev/null 2>&1 &
wasm-pack test --headless --firefox
wasm-pack test --headless --chrome
pkill substrate
working-directory: testing/wasm-tests
working-directory: testing/wasm-rpc-tests
- name: Run subxt-lightclient WASM tests
run: |
# `listen-addr` is used to configure p2p to accept websocket connections instead of TCP.
# `node-key` provides a deterministic p2p address.
substrate --dev --node-key 0000000000000000000000000000000000000000000000000000000000000001 --listen-addr /ip4/0.0.0.0/tcp/30333/ws > /dev/null 2>&1 &
wasm-pack test --headless --firefox
wasm-pack test --headless --chrome
pkill substrate
working-directory: testing/wasm-lightclient-tests
- name: Run subxt-signer WASM tests
run: |
Generated
+37 -3
View File
@@ -1481,7 +1481,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c"
dependencies = [
"gloo-timers",
"send_wrapper",
"send_wrapper 0.4.0",
]
[[package]]
@@ -1938,6 +1938,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
dependencies = [
"cfg-if",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
@@ -3367,6 +3370,12 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f638d531eccd6e23b980caf34876660d38e265409d8e99b397ab71eb3612fad0"
[[package]]
name = "send_wrapper"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73"
[[package]]
name = "serde"
version = "1.0.171"
@@ -4149,7 +4158,6 @@ dependencies = [
"either",
"frame-metadata 16.0.0",
"futures",
"futures-util",
"getrandom 0.2.10",
"hex",
"impl-serde",
@@ -4163,12 +4171,12 @@ dependencies = [
"scale-value",
"serde",
"serde_json",
"smoldot-light",
"sp-core",
"sp-core-hashing",
"sp-keyring",
"sp-runtime",
"sp-version",
"subxt-lightclient",
"subxt-macro",
"subxt-metadata",
"subxt-signer",
@@ -4220,6 +4228,32 @@ dependencies = [
"tokio",
]
[[package]]
name = "subxt-lightclient"
version = "0.29.0"
dependencies = [
"either",
"futures",
"futures-timer",
"futures-util",
"getrandom 0.2.10",
"instant",
"js-sys",
"send_wrapper 0.6.0",
"serde",
"serde_json",
"smoldot",
"smoldot-light",
"thiserror",
"tokio",
"tokio-stream",
"tokio-util",
"tracing",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
]
[[package]]
name = "subxt-macro"
version = "0.29.0"
+16 -2
View File
@@ -2,6 +2,7 @@
members = [
"cli",
"codegen",
"lightclient",
"testing/substrate-runner",
"testing/test-runtime",
"testing/integration-tests",
@@ -16,7 +17,8 @@ members = [
# exclusive feature flags and thus can't compile with the
# workspace:
exclude = [
"testing/wasm-tests",
"testing/wasm-rpc-tests",
"testing/wasm-lightclient-tests",
"signer/wasm-tests",
"examples/wasm-example"
]
@@ -67,7 +69,7 @@ serde = { version = "1.0.171" }
serde_json = { version = "1.0.100" }
syn = { version = "2.0.15", features = ["full", "extra-traits"] }
thiserror = "1.0.40"
tokio = { version = "1.29", features = ["macros", "time", "rt-multi-thread"] }
tokio = { version = "1.29", default-features = false }
tracing = "0.1.34"
tracing-wasm = "0.2.1"
tracing-subscriber = "0.3.17"
@@ -77,10 +79,21 @@ wasm-bindgen-test = "0.3.24"
which = "4.4.0"
# Light client support:
smoldot = { version = "0.8.0", default-features = false }
smoldot-light = { version = "0.6.0", default-features = false }
tokio-stream = "0.1.14"
futures-util = "0.3.28"
# Light client wasm:
web-sys = { version = "0.3.61", features = ["BinaryType", "CloseEvent", "MessageEvent", "WebSocket"] }
wasm-bindgen = "0.2.84"
send_wrapper = "0.6.0"
js-sys = "0.3.61"
wasm-bindgen-futures = "0.4.19"
futures-timer = "3"
instant = { version = "0.1.12", default-features = false }
tokio-util = "0.7.8"
# Substrate crates:
sp-core = { version = "21.0.0", default-features = false }
sp-core-hashing = "9.0.0"
@@ -94,6 +107,7 @@ subxt-macro = { version = "0.29.0", path = "macro" }
subxt-metadata = { version = "0.29.0", path = "metadata" }
subxt-codegen = { version = "0.29.0", path = "codegen" }
subxt-signer = { version = "0.29.0", path = "signer" }
subxt-lightclient = { version = "0.29.0", path = "lightclient", default-features = false }
test-runtime = { path = "testing/test-runtime" }
substrate-runner = { path = "testing/substrate-runner" }
+1 -1
View File
@@ -31,4 +31,4 @@ scale-info = { workspace = true }
scale-value = { workspace = true }
syn = { workspace = true }
jsonrpsee = { workspace = true, features = ["async-client", "client-ws-transport", "http-client"] }
tokio = { workspace = true }
tokio = { workspace = true, features = ["rt-multi-thread"] }
+1 -1
View File
@@ -23,7 +23,7 @@ scale-info = { workspace = true }
subxt-metadata = { workspace = true }
jsonrpsee = { workspace = true, features = ["async-client", "client-ws-transport", "http-client"] }
hex = { workspace = true }
tokio = { workspace = true }
tokio = { workspace = true, features = ["rt-multi-thread"] }
thiserror = { workspace = true }
[dev-dependencies]
+86
View File
@@ -0,0 +1,86 @@
[package]
name = "subxt-lightclient"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
publish = true
license.workspace = true
readme = "../README.md"
repository.workspace = true
documentation.workspace = true
homepage.workspace = true
description = "Light Client for chain interaction"
keywords = ["parity", "substrate", "blockchain"]
[features]
default = ["native"]
# Enable this for native (ie non web/wasm builds).
# Exactly 1 of "web" and "native" is expected.
native = [
"smoldot-light/std",
"tokio-stream",
"tokio/sync",
"tokio/rt",
"futures-util",
]
# Enable this for web/wasm builds.
# Exactly 1 of "web" and "native" is expected.
web = [
"getrandom/js",
"smoldot",
"smoldot-light",
"tokio-stream",
"tokio/sync",
"futures-util",
# For the light-client platform.
"wasm-bindgen-futures",
"futures-timer/wasm-bindgen",
"instant/wasm-bindgen",
# For websocket.
"js-sys",
"send_wrapper",
"web-sys",
"wasm-bindgen",
]
[dependencies]
futures = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["raw_value"] }
thiserror = { workspace = true }
tracing = { workspace = true }
# Light client support:
smoldot = { workspace = true, optional = true }
smoldot-light = { workspace = true, optional = true }
either = { workspace = true, optional = true }
tokio = { workspace = true, optional = true }
tokio-stream = { workspace = true, optional = true }
futures-util = { workspace = true, optional = true }
js-sys = { workspace = true, optional = true }
send_wrapper = { workspace = true, optional = true }
web-sys = { workspace = true, optional = true }
wasm-bindgen = { workspace = true, optional = true }
wasm-bindgen-futures = { workspace = true, optional = true }
futures-timer = { workspace = true, optional = true }
instant = { workspace = true, optional = true }
tokio-util = { workspace = true, optional = true }
# Included if "web" feature is enabled, to enable its js feature.
getrandom = { workspace = true, optional = true }
[profile.dev.package.smoldot-light]
opt-level = 2
[profile.test.package.smoldot-light]
opt-level = 2
[profile.dev.package.smoldot]
opt-level = 2
[profile.test.package.smoldot]
opt-level = 2
@@ -6,16 +6,17 @@ use futures::stream::StreamExt;
use futures_util::future::{self, Either};
use serde::Deserialize;
use serde_json::value::RawValue;
use std::{collections::HashMap, str::FromStr, sync::Arc};
use std::{collections::HashMap, str::FromStr};
use tokio::sync::{mpsc, oneshot};
use super::LightClientError;
use smoldot_light::{platform::default::DefaultPlatform as Platform, ChainId};
use super::platform::PlatformType;
use super::LightClientRpcError;
use smoldot_light::ChainId;
const LOG_TARGET: &str = "light-client-background";
/// The response of an RPC method.
pub type MethodResponse = Result<Box<RawValue>, LightClientError>;
pub type MethodResponse = Result<Box<RawValue>, LightClientRpcError>;
/// Message protocol between the front-end client that submits the RPC requests
/// and the backend handler that produces responses from the chain.
@@ -50,7 +51,7 @@ pub enum FromSubxt {
/// Background task data.
pub struct BackgroundTask {
/// Smoldot light client implementation that leverages the exposed platform.
client: smoldot_light::Client<Arc<Platform>>,
client: smoldot_light::Client<PlatformType>,
/// The ID of the chain used to identify the chain protocol (ie. substrate).
///
/// Note: A single chain is supported for a client. This aligns with the subxt's
@@ -79,7 +80,7 @@ pub struct BackgroundTask {
impl BackgroundTask {
/// Constructs a new [`BackgroundTask`].
pub fn new(client: smoldot_light::Client<Arc<Platform>>, chain_id: ChainId) -> BackgroundTask {
pub fn new(client: smoldot_light::Client<PlatformType>, chain_id: ChainId) -> BackgroundTask {
BackgroundTask {
client,
chain_id,
@@ -127,7 +128,7 @@ impl BackgroundTask {
// Send the error back to frontend.
if sender
.send(Err(LightClientError::Request(err.to_string())))
.send(Err(LightClientRpcError::Request(err.to_string())))
.is_err()
{
tracing::warn!(
@@ -167,7 +168,7 @@ impl BackgroundTask {
// Send the error back to frontend.
if sub_id
.send(Err(LightClientError::Request(err.to_string())))
.send(Err(LightClientRpcError::Request(err.to_string())))
.is_err()
{
tracing::warn!(
@@ -191,7 +192,7 @@ impl BackgroundTask {
if let Some(sender) = self.requests.remove(&id) {
if sender
.send(Err(LightClientError::Request(error.to_string())))
.send(Err(LightClientRpcError::Request(error.to_string())))
.is_err()
{
tracing::warn!(
@@ -201,7 +202,7 @@ impl BackgroundTask {
}
} else if let Some((sub_id_sender, _)) = self.id_to_subscription.remove(&id) {
if sub_id_sender
.send(Err(LightClientError::Request(error.to_string())))
.send(Err(LightClientRpcError::Request(error.to_string())))
.is_err()
{
tracing::warn!(
+114
View File
@@ -0,0 +1,114 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use super::{
background::{BackgroundTask, FromSubxt, MethodResponse},
LightClientRpcError,
};
use serde_json::value::RawValue;
use tokio::sync::{mpsc, mpsc::error::SendError, oneshot};
use super::platform::build_platform;
pub const LOG_TARGET: &str = "light-client";
/// The light-client RPC implementation that is used to connect with the chain.
#[derive(Clone)]
pub struct LightClientRpc {
/// Communicate with the backend task that multiplexes the responses
/// back to the frontend.
to_backend: mpsc::UnboundedSender<FromSubxt>,
}
impl LightClientRpc {
/// Constructs a new [`LightClientRpc`], providing the chain specification.
///
/// The chain specification can be downloaded from a trusted network via
/// the `sync_state_genSyncSpec` RPC method. This parameter expects the
/// chain spec in text format (ie not in hex-encoded scale-encoded as RPC methods
/// will provide).
///
/// ## Panics
///
/// Panics if being called outside of `tokio` runtime context.
pub fn new(
config: smoldot_light::AddChainConfig<'_, (), impl Iterator<Item = smoldot_light::ChainId>>,
) -> Result<LightClientRpc, LightClientRpcError> {
tracing::trace!(target: LOG_TARGET, "Create light client");
let mut client = smoldot_light::Client::new(build_platform());
let smoldot_light::AddChainSuccess {
chain_id,
json_rpc_responses,
} = client
.add_chain(config)
.map_err(|err| LightClientRpcError::AddChainError(err.to_string()))?;
let (to_backend, backend) = mpsc::unbounded_channel();
// `json_rpc_responses` can only be `None` if we had passed `json_rpc: Disabled`.
let rpc_responses = json_rpc_responses.expect("Light client RPC configured; qed");
let future = async move {
let mut task = BackgroundTask::new(client, chain_id);
task.start_task(backend, rpc_responses).await;
};
#[cfg(feature = "native")]
tokio::spawn(future);
#[cfg(feature = "web")]
wasm_bindgen_futures::spawn_local(future);
Ok(LightClientRpc { to_backend })
}
/// Submits an RPC method request to the light-client.
///
/// This method sends a request to the light-client to execute an RPC method with the provided parameters.
/// The parameters are parsed into a valid JSON object in the background.
pub fn method_request(
&self,
method: String,
params: String,
) -> Result<oneshot::Receiver<MethodResponse>, SendError<FromSubxt>> {
let (sender, receiver) = oneshot::channel();
self.to_backend.send(FromSubxt::Request {
method,
params,
sender,
})?;
Ok(receiver)
}
/// Makes an RPC subscription call to the light-client.
///
/// This method sends a request to the light-client to establish an RPC subscription with the provided parameters.
/// The parameters are parsed into a valid JSON object in the background.
pub fn subscription_request(
&self,
method: String,
params: String,
) -> Result<
(
oneshot::Receiver<MethodResponse>,
mpsc::UnboundedReceiver<Box<RawValue>>,
),
SendError<FromSubxt>,
> {
let (sub_id, sub_id_rx) = oneshot::channel();
let (sender, receiver) = mpsc::unbounded_channel();
self.to_backend.send(FromSubxt::Subscription {
method,
params,
sub_id,
sender,
})?;
Ok((sub_id_rx, receiver))
}
}
+48
View File
@@ -0,0 +1,48 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
//! Low level light client implementation for RPC method and
//! subscriptions requests.
//!
//! The client implementation supports both native and wasm
//! environments.
//!
//! This leverages the smoldot crate to connect to the chain.
#![deny(
missing_docs,
unused_crate_dependencies,
unused_extern_crates,
clippy::all
)]
#![allow(clippy::type_complexity)]
#[cfg(any(
all(feature = "web", feature = "native"),
not(any(feature = "web", feature = "native"))
))]
compile_error!("subxt: exactly one of the 'web' and 'native' features should be used.");
mod background;
mod client;
mod platform;
// Used to enable the js feature for wasm.
#[cfg(feature = "web")]
#[allow(unused_imports)]
pub use getrandom as _;
pub use client::LightClientRpc;
pub use smoldot_light::{AddChainConfig, AddChainConfigJsonRpc, ChainId};
/// Light client error.
#[derive(Debug, thiserror::Error)]
pub enum LightClientRpcError {
/// Error encountered while adding the chain to the light-client.
#[error("Failed to add the chain to the light client: {0}.")]
AddChainError(String),
/// Error originated while trying to submit a RPC request.
#[error("RPC request cannot be sent: {0}.")]
Request(String),
}
+40
View File
@@ -0,0 +1,40 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
//! Default platform for WASM environments.
#[cfg(feature = "web")]
mod wasm_helpers;
#[cfg(feature = "web")]
mod wasm_platform;
#[cfg(feature = "web")]
mod wasm_socket;
pub use helpers::{build_platform, PlatformType};
#[cfg(feature = "native")]
mod helpers {
use smoldot_light::platform::default::DefaultPlatform as Platform;
use std::sync::Arc;
pub type PlatformType = Arc<Platform>;
pub fn build_platform() -> PlatformType {
Platform::new(
"subxt-light-client".into(),
env!("CARGO_PKG_VERSION").into(),
)
}
}
#[cfg(feature = "web")]
mod helpers {
use super::wasm_platform::SubxtPlatform as Platform;
pub type PlatformType = Platform;
pub fn build_platform() -> PlatformType {
Platform::new()
}
}
+126
View File
@@ -0,0 +1,126 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
//! Wasm implementation for the light client's platform using
//! custom websockets.
use core::time::Duration;
use futures_util::{future, FutureExt};
use smoldot::libp2p::multiaddr::ProtocolRef;
use smoldot_light::platform::{ConnectError, PlatformConnection};
use std::{
collections::VecDeque,
net::{IpAddr, SocketAddr},
};
use super::wasm_socket::WasmSocket;
pub fn spawn(task: future::BoxFuture<'static, ()>) {
wasm_bindgen_futures::spawn_local(task);
}
pub fn now_from_unix_epoch() -> Duration {
instant::SystemTime::now()
.duration_since(instant::SystemTime::UNIX_EPOCH)
.unwrap_or_else(|_| {
panic!("Invalid systime cannot be configured earlier than `UNIX_EPOCH`")
})
}
pub type Instant = instant::Instant;
pub fn now() -> Instant {
instant::Instant::now()
}
pub type Delay = future::BoxFuture<'static, ()>;
pub fn sleep(duration: Duration) -> Delay {
futures_timer::Delay::new(duration).boxed()
}
pub struct Stream {
pub socket: WasmSocket,
/// Read and write buffers of the connection, or `None` if the socket has been reset.
pub buffers: Option<(StreamReadBuffer, StreamWriteBuffer)>,
}
pub enum StreamReadBuffer {
Open {
buffer: Vec<u8>,
cursor: std::ops::Range<usize>,
},
Closed,
}
pub enum StreamWriteBuffer {
Open {
buffer: VecDeque<u8>,
must_flush: bool,
must_close: bool,
},
Closed,
}
pub async fn connect<'a>(
proto1: ProtocolRef<'a>,
proto2: ProtocolRef<'a>,
proto3: Option<ProtocolRef<'a>>,
) -> Result<PlatformConnection<Stream, std::convert::Infallible>, ConnectError> {
// Ensure ahead of time that the multiaddress is supported.
let addr = match (&proto1, &proto2, &proto3) {
(ProtocolRef::Ip4(ip), ProtocolRef::Tcp(port), Some(ProtocolRef::Ws)) => {
let addr = SocketAddr::new(IpAddr::V4((*ip).into()), *port);
format!("ws://{}", addr.to_string())
}
(ProtocolRef::Ip6(ip), ProtocolRef::Tcp(port), Some(ProtocolRef::Ws)) => {
let addr = SocketAddr::new(IpAddr::V6((*ip).into()), *port);
format!("ws://{}", addr.to_string())
}
(
ProtocolRef::Dns(addr) | ProtocolRef::Dns4(addr) | ProtocolRef::Dns6(addr),
ProtocolRef::Tcp(port),
Some(ProtocolRef::Ws),
) => {
format!("ws://{}:{}", addr.to_string(), port)
}
(
ProtocolRef::Dns(addr) | ProtocolRef::Dns4(addr) | ProtocolRef::Dns6(addr),
ProtocolRef::Tcp(port),
Some(ProtocolRef::Wss),
) => {
format!("wss://{}:{}", addr.to_string(), port)
}
_ => {
return Err(ConnectError {
is_bad_addr: true,
message: "Unknown protocols combination".to_string(),
})
}
};
tracing::debug!("Connecting to addr={addr}");
let socket = WasmSocket::new(addr.as_str()).map_err(|err| ConnectError {
is_bad_addr: false,
message: format!("Failed to reach peer: {err}"),
})?;
Ok(PlatformConnection::SingleStreamMultistreamSelectNoiseYamux(
Stream {
socket,
buffers: Some((
StreamReadBuffer::Open {
buffer: vec![0; 16384],
cursor: 0..0,
},
StreamWriteBuffer::Open {
buffer: VecDeque::with_capacity(16384),
must_close: false,
must_flush: false,
},
)),
},
))
}
+302
View File
@@ -0,0 +1,302 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use core::time::Duration;
use futures::{prelude::*, task::Poll};
use smoldot::libp2p::multiaddr::Multiaddr;
use smoldot_light::platform::{
ConnectError, PlatformConnection, PlatformRef, PlatformSubstreamDirection, ReadBuffer,
};
use std::{io::IoSlice, pin::Pin};
use super::wasm_helpers::{StreamReadBuffer, StreamWriteBuffer};
/// Subxt plaform implementation for wasm.
///
/// This implementation is a conversion of the implementation from the smoldot:
/// https://github.com/smol-dot/smoldot/blob/f49ce4ea6a325c444ab6ad37d3ab5558edf0d541/light-base/src/platform/default.rs#L52.
///
/// This platform will evolve over time and we'll need to keep this code in sync.
#[derive(Clone)]
pub struct SubxtPlatform {}
impl SubxtPlatform {
pub fn new() -> Self {
SubxtPlatform {}
}
}
impl PlatformRef for SubxtPlatform {
type Delay = super::wasm_helpers::Delay;
type Yield = future::Ready<()>;
type Instant = super::wasm_helpers::Instant;
type Connection = std::convert::Infallible;
type Stream = super::wasm_helpers::Stream;
type ConnectFuture = future::BoxFuture<
'static,
Result<PlatformConnection<Self::Stream, Self::Connection>, ConnectError>,
>;
type StreamUpdateFuture<'a> = future::BoxFuture<'a, ()>;
type NextSubstreamFuture<'a> =
future::Pending<Option<(Self::Stream, PlatformSubstreamDirection)>>;
fn now_from_unix_epoch(&self) -> Duration {
super::wasm_helpers::now_from_unix_epoch()
}
fn now(&self) -> Self::Instant {
super::wasm_helpers::now()
}
fn sleep(&self, duration: Duration) -> Self::Delay {
super::wasm_helpers::sleep(duration)
}
fn sleep_until(&self, when: Self::Instant) -> Self::Delay {
self.sleep(when.saturating_duration_since(self.now()))
}
fn yield_after_cpu_intensive(&self) -> Self::Yield {
// No-op.
future::ready(())
}
fn connect(&self, multiaddr: &str) -> Self::ConnectFuture {
// We simply copy the address to own it. We could be more zero-cost here, but doing so
// would considerably complicate the implementation.
let multiaddr = multiaddr.to_owned();
tracing::debug!("Connecting to multiaddress={:?}", multiaddr);
Box::pin(async move {
let addr = multiaddr.parse::<Multiaddr>().map_err(|_| ConnectError {
is_bad_addr: true,
message: "Failed to parse address".to_string(),
})?;
let mut iter = addr.iter().fuse();
let proto1 = iter.next().ok_or(ConnectError {
is_bad_addr: true,
message: "Unknown protocols combination".to_string(),
})?;
let proto2 = iter.next().ok_or(ConnectError {
is_bad_addr: true,
message: "Unknown protocols combination".to_string(),
})?;
let proto3 = iter.next();
if iter.next().is_some() {
return Err(ConnectError {
is_bad_addr: true,
message: "Unknown protocols combination".to_string(),
});
}
super::wasm_helpers::connect(proto1, proto2, proto3).await
})
}
fn open_out_substream(&self, c: &mut Self::Connection) {
// This function can only be called with so-called "multi-stream" connections. We never
// open such connection.
match *c {}
}
fn next_substream<'a>(&self, c: &'a mut Self::Connection) -> Self::NextSubstreamFuture<'a> {
// This function can only be called with so-called "multi-stream" connections. We never
// open such connection.
match *c {}
}
fn update_stream<'a>(&self, stream: &'a mut Self::Stream) -> Self::StreamUpdateFuture<'a> {
Box::pin(future::poll_fn(|cx| {
// The `connect` is expected to be called before this method and would populate
// the buffers properly. When the buffers are empty, this future is shortly dropped.
let Some((read_buffer, write_buffer)) = stream.buffers.as_mut() else { return Poll::Pending };
// Whether the future returned by `update_stream` should return `Ready` or `Pending`.
let mut update_stream_future_ready = false;
if let StreamReadBuffer::Open {
buffer: ref mut buf,
ref mut cursor,
} = read_buffer
{
// When reading data from the socket, `poll_read` might return "EOF". In that
// situation, we transition to the `Closed` state, which would discard the data
// currently in the buffer. For this reason, we only try to read if there is no
// data left in the buffer.
if cursor.start == cursor.end {
if let Poll::Ready(result) = Pin::new(&mut stream.socket).poll_read(cx, buf) {
update_stream_future_ready = true;
match result {
Err(_) => {
// End the stream.
stream.buffers = None;
return Poll::Ready(());
}
Ok(0) => {
// EOF.
*read_buffer = StreamReadBuffer::Closed;
}
Ok(bytes) => {
*cursor = 0..bytes;
}
}
}
}
}
if let StreamWriteBuffer::Open {
buffer: ref mut buf,
must_flush,
must_close,
} = write_buffer
{
while !buf.is_empty() {
let write_queue_slices = buf.as_slices();
if let Poll::Ready(result) = Pin::new(&mut stream.socket).poll_write_vectored(
cx,
&[
IoSlice::new(write_queue_slices.0),
IoSlice::new(write_queue_slices.1),
],
) {
if !*must_close {
// In the situation where the API user wants to close the writing
// side, simply sending the buffered data isn't enough to justify
// making the future ready.
update_stream_future_ready = true;
}
match result {
Err(_) => {
// End the stream.
stream.buffers = None;
return Poll::Ready(());
}
Ok(bytes) => {
*must_flush = true;
for _ in 0..bytes {
buf.pop_front();
}
}
}
} else {
break;
}
}
if buf.is_empty() && *must_close {
if let Poll::Ready(result) = Pin::new(&mut stream.socket).poll_close(cx) {
update_stream_future_ready = true;
match result {
Err(_) => {
// End the stream.
stream.buffers = None;
return Poll::Ready(());
}
Ok(()) => {
*write_buffer = StreamWriteBuffer::Closed;
}
}
}
} else if *must_flush {
if let Poll::Ready(result) = Pin::new(&mut stream.socket).poll_flush(cx) {
update_stream_future_ready = true;
match result {
Err(_) => {
// End the stream.
stream.buffers = None;
return Poll::Ready(());
}
Ok(()) => {
*must_flush = false;
}
}
}
}
}
if update_stream_future_ready {
Poll::Ready(())
} else {
// Progress cannot be made since poll_read, poll_write, poll_close, poll_flush
// are not ready yet. Smoldot drops this future and calls it again with the
// next processing iteration.
Poll::Pending
}
}))
}
fn read_buffer<'a>(&self, stream: &'a mut Self::Stream) -> ReadBuffer<'a> {
match stream.buffers.as_ref().map(|(r, _)| r) {
None => ReadBuffer::Reset,
Some(StreamReadBuffer::Closed) => ReadBuffer::Closed,
Some(StreamReadBuffer::Open { buffer, cursor }) => {
ReadBuffer::Open(&buffer[cursor.clone()])
}
}
}
fn advance_read_cursor(&self, stream: &mut Self::Stream, extra_bytes: usize) {
let Some(StreamReadBuffer::Open { ref mut cursor, .. }) =
stream.buffers.as_mut().map(|(r, _)| r)
else {
assert_eq!(extra_bytes, 0);
return
};
assert!(cursor.start + extra_bytes <= cursor.end);
cursor.start += extra_bytes;
}
fn writable_bytes(&self, stream: &mut Self::Stream) -> usize {
let Some(StreamWriteBuffer::Open { ref mut buffer, must_close: false, ..}) =
stream.buffers.as_mut().map(|(_, w)| w) else { return 0 };
buffer.capacity() - buffer.len()
}
fn send(&self, stream: &mut Self::Stream, data: &[u8]) {
debug_assert!(!data.is_empty());
// Because `writable_bytes` returns 0 if the writing side is closed, and because `data`
// must always have a size inferior or equal to `writable_bytes`, we know for sure that
// the writing side isn't closed.
let Some(StreamWriteBuffer::Open { ref mut buffer, .. } )=
stream.buffers.as_mut().map(|(_, w)| w) else { panic!() };
buffer.reserve(data.len());
buffer.extend(data.iter().copied());
}
fn close_send(&self, stream: &mut Self::Stream) {
// It is not illegal to call this on an already-reset stream.
let Some((_, write_buffer)) = stream.buffers.as_mut() else { return };
match write_buffer {
StreamWriteBuffer::Open {
must_close: must_close @ false,
..
} => *must_close = true,
_ => {
// However, it is illegal to call this on a stream that was already close
// attempted.
panic!()
}
}
}
fn spawn_task(&self, _: std::borrow::Cow<str>, task: future::BoxFuture<'static, ()>) {
super::wasm_helpers::spawn(task);
}
fn client_name(&self) -> std::borrow::Cow<str> {
"subxt-light-client".into()
}
fn client_version(&self) -> std::borrow::Cow<str> {
env!("CARGO_PKG_VERSION").into()
}
}
+237
View File
@@ -0,0 +1,237 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use futures::{io, prelude::*};
use send_wrapper::SendWrapper;
use wasm_bindgen::{prelude::*, JsCast};
use std::{
collections::VecDeque,
pin::Pin,
sync::{Arc, Mutex},
task::Poll,
task::{Context, Waker},
};
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Failed to connect {0}")]
ConnectionError(String),
}
/// Websocket for WASM environments.
///
/// This is a rust-based wrapper around browser's WebSocket API.
///
// Warning: It is not safe to have `Clone` on this structure.
pub struct WasmSocket {
/// Inner data shared between `poll` and web_sys callbacks.
inner: Arc<Mutex<InnerWasmSocket>>,
/// This implements `Send` and panics if the value is accessed
/// or dropped from another thread.
///
/// This is safe in wasm environments.
socket: SendWrapper<web_sys::WebSocket>,
/// In memory callbacks to handle messages from the browser socket.
_callbacks: SendWrapper<Callbacks>,
}
/// The state of the [`WasmSocket`].
#[derive(PartialEq, Eq, Clone, Copy)]
enum ConnectionState {
/// Initial state of the socket.
Connecting,
/// Socket is fully opened.
Opened,
/// Socket is closed.
Closed,
/// Error reported by callbacks.
Error,
}
struct InnerWasmSocket {
/// The state of the connection.
state: ConnectionState,
/// Data buffer for the socket.
data: VecDeque<u8>,
/// Waker from `poll_read` / `poll_write`.
waker: Option<Waker>,
}
/// Registered callbacks of the [`WasmSocket`].
///
/// These need to be kept around until the socket is dropped.
type Callbacks = (
Closure<dyn FnMut()>,
Closure<dyn FnMut(web_sys::MessageEvent)>,
Closure<dyn FnMut(web_sys::Event)>,
Closure<dyn FnMut(web_sys::CloseEvent)>,
);
impl WasmSocket {
/// Establish a WebSocket connection.
///
/// The error is a string representing the browser error.
/// Visit [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket#exceptions_thrown)
/// for more info.
pub fn new(addr: &str) -> Result<Self, Error> {
let socket = match web_sys::WebSocket::new(addr) {
Ok(socket) => socket,
Err(err) => return Err(Error::ConnectionError(format!("{:?}", err))),
};
socket.set_binary_type(web_sys::BinaryType::Arraybuffer);
let inner = Arc::new(Mutex::new(InnerWasmSocket {
state: ConnectionState::Connecting,
data: VecDeque::with_capacity(16384),
waker: None,
}));
let open_callback = Closure::<dyn FnMut()>::new({
let inner = inner.clone();
move || {
let mut inner = inner.lock().expect("Mutex is poised; qed");
inner.state = ConnectionState::Opened;
if let Some(waker) = inner.waker.take() {
waker.wake();
}
}
});
socket.set_onopen(Some(open_callback.as_ref().unchecked_ref()));
let message_callback = Closure::<dyn FnMut(_)>::new({
let inner = inner.clone();
move |event: web_sys::MessageEvent| {
let Ok(buffer) = event.data().dyn_into::<js_sys::ArrayBuffer>() else {
panic!("Unexpected data format {:?}", event.data());
};
let mut inner = inner.lock().expect("Mutex is poised; qed");
let bytes = js_sys::Uint8Array::new(&buffer).to_vec();
inner.data.extend(bytes.into_iter());
if let Some(waker) = inner.waker.take() {
waker.wake();
}
}
});
socket.set_onmessage(Some(message_callback.as_ref().unchecked_ref()));
let error_callback = Closure::<dyn FnMut(_)>::new({
let inner = inner.clone();
move |_| {
// Callback does not provide useful information, signal it back to the stream.
let mut inner = inner.lock().expect("Mutex is poised; qed");
inner.state = ConnectionState::Error;
if let Some(waker) = inner.waker.take() {
waker.wake();
}
}
});
socket.set_onerror(Some(error_callback.as_ref().unchecked_ref()));
let close_callback = Closure::<dyn FnMut(_)>::new({
let inner = inner.clone();
move |_| {
let mut inner = inner.lock().expect("Mutex is poised; qed");
inner.state = ConnectionState::Closed;
if let Some(waker) = inner.waker.take() {
waker.wake();
}
}
});
socket.set_onclose(Some(close_callback.as_ref().unchecked_ref()));
let callbacks = (
open_callback,
message_callback,
error_callback,
close_callback,
);
Ok(Self {
inner,
socket: SendWrapper::new(socket),
_callbacks: SendWrapper::new(callbacks),
})
}
}
impl AsyncRead for WasmSocket {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize, io::Error>> {
let mut inner = self.inner.lock().expect("Mutex is poised; qed");
inner.waker = Some(cx.waker().clone());
match inner.state {
ConnectionState::Error => {
Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, "Socket error")))
}
ConnectionState::Closed => Poll::Ready(Err(io::ErrorKind::BrokenPipe.into())),
ConnectionState::Connecting => Poll::Pending,
ConnectionState::Opened => {
if inner.data.is_empty() {
return Poll::Pending;
}
let n = inner.data.len().min(buf.len());
for k in buf.iter_mut().take(n) {
*k = inner.data.pop_front().expect("Buffer non empty; qed");
}
Poll::Ready(Ok(n))
}
}
}
}
impl AsyncWrite for WasmSocket {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
let mut inner = self.inner.lock().expect("Mutex is poised; qed");
inner.waker = Some(cx.waker().clone());
match inner.state {
ConnectionState::Error => {
Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, "Socket error")))
}
ConnectionState::Closed => Poll::Ready(Err(io::ErrorKind::BrokenPipe.into())),
ConnectionState::Connecting => Poll::Pending,
ConnectionState::Opened => match self.socket.send_with_u8_array(buf) {
Ok(()) => Poll::Ready(Ok(buf.len())),
Err(err) => Poll::Ready(Err(io::Error::new(
io::ErrorKind::Other,
format!("Write error: {err:?}"),
))),
},
}
}
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Poll::Ready(Ok(()))
}
}
impl Drop for WasmSocket {
fn drop(&mut self) {
let inner = self.inner.lock().expect("Mutex is poised; qed");
if inner.state == ConnectionState::Opened {
let _ = self.socket.close();
}
}
}
+7 -15
View File
@@ -23,7 +23,8 @@ default = ["jsonrpsee", "native", "substrate-compat"]
# Exactly 1 of "web" and "native" is expected.
native = [
"jsonrpsee?/async-client",
"jsonrpsee?/client-ws-transport"
"jsonrpsee?/client-ws-transport",
"subxt-lightclient?/native"
]
# Enable this for web/wasm builds.
@@ -31,7 +32,8 @@ native = [
web = [
"jsonrpsee?/async-wasm-client",
"jsonrpsee?/client-web-transport",
"getrandom/js"
"getrandom/js",
"subxt-lightclient?/web"
]
# Enable this to use jsonrpsee (allowing for example `OnlineClient::from_url`).
@@ -56,11 +58,8 @@ unstable-metadata = []
# Activate this to expose the Light Client functionality.
# Note that this feature is experimental and things may break or not work as expected.
unstable-light-client = [
"smoldot-light/std",
"tokio-stream",
"tokio/sync",
"tokio/rt",
"futures-util",
"subxt-lightclient",
"tokio-stream"
]
[dependencies]
@@ -99,12 +98,10 @@ sp-runtime = { workspace = true, optional = true }
# Other subxt crates we depend on.
subxt-macro = { workspace = true }
subxt-metadata = { workspace = true }
subxt-lightclient = { workspace = true, optional = true, default-features = false }
# Light client support:
smoldot-light = { workspace = true, optional = true }
tokio = { workspace = true, optional = true }
tokio-stream = { workspace = true, optional = true }
futures-util = { workspace = true, optional = true }
# Included if "web" feature is enabled, to enable its js feature.
getrandom = { workspace = true, optional = true }
@@ -130,8 +127,3 @@ tracing-subscriber = { workspace = true }
name = "unstable_light_client_tx_basic"
path = "examples/unstable_light_client_tx_basic.rs"
required-features = ["unstable-light-client", "jsonrpsee"]
[profile.dev.package.smoldot-light]
opt-level = 2
[profile.test.package.smoldot-light]
opt-level = 2
+1
View File
@@ -8,6 +8,7 @@
//! to the P2P network and behaving similarly to a full node.
//!
//! To enable this functionality, the unstable-light-client feature flag needs to be enabled.
//! To enable light client for WASM environments, also enable the web feature flag.
//!
//! To connect to a blockchain network, the Light Client requires a trusted sync state of the network, named "chain spec".
//! This can be obtained by making a `sync_state_genSyncSpec` RPC call to a trusted node.
+60 -28
View File
@@ -5,14 +5,8 @@
use super::{rpc::LightClientRpc, LightClient, LightClientError};
use crate::{config::Config, error::Error, OnlineClient};
#[cfg(feature = "jsonrpsee")]
use jsonrpsee::{
async_client::ClientBuilder,
client_transport::ws::{Uri, WsTransportClientBuilder},
core::client::ClientT,
rpc_params,
};
use smoldot_light::ChainId;
use subxt_lightclient::{AddChainConfig, AddChainConfigJsonRpc, ChainId};
use std::num::NonZeroU32;
use std::sync::Arc;
@@ -145,9 +139,9 @@ impl LightClientBuilder {
}
}
let config = smoldot_light::AddChainConfig {
let config = AddChainConfig {
specification: &chain_spec.to_string(),
json_rpc: smoldot_light::AddChainConfigJsonRpc::Enabled {
json_rpc: AddChainConfigJsonRpc::Enabled {
max_pending_requests: self.max_pending_requests,
max_subscriptions: self.max_subscriptions,
},
@@ -165,27 +159,65 @@ impl LightClientBuilder {
/// Fetch the chain spec from the URL.
#[cfg(feature = "jsonrpsee")]
async fn fetch_url(url: impl AsRef<str>) -> Result<serde_json::Value, Error> {
let url = url
.as_ref()
.parse::<Uri>()
.map_err(|_| Error::LightClient(LightClientError::InvalidUrl))?;
use jsonrpsee::core::client::ClientT;
if url.scheme_str() != Some("ws") && url.scheme_str() != Some("wss") {
return Err(Error::LightClient(LightClientError::InvalidScheme));
}
let (sender, receiver) = WsTransportClientBuilder::default()
.build(url)
.await
.map_err(|_| LightClientError::Handshake)?;
let client = ClientBuilder::default()
.request_timeout(core::time::Duration::from_secs(180))
.max_notifs_per_subscription(4096)
.build_with_tokio(sender, receiver);
let client = jsonrpsee_helpers::client(url.as_ref()).await?;
client
.request("sync_state_genSyncSpec", rpc_params![true])
.request("sync_state_genSyncSpec", jsonrpsee::rpc_params![true])
.await
.map_err(|err| Error::Rpc(crate::error::RpcError::ClientError(Box::new(err))))
}
#[cfg(all(feature = "jsonrpsee", feature = "native"))]
mod jsonrpsee_helpers {
use crate::error::{Error, LightClientError};
pub use jsonrpsee::{
client_transport::ws::{InvalidUri, Receiver, Sender, Uri, WsTransportClientBuilder},
core::client::{Client, ClientBuilder},
};
/// Build WS RPC client from URL
pub async fn client(url: &str) -> Result<Client, Error> {
let url = url
.parse::<Uri>()
.map_err(|_| Error::LightClient(LightClientError::InvalidUrl))?;
if url.scheme_str() != Some("ws") && url.scheme_str() != Some("wss") {
return Err(Error::LightClient(LightClientError::InvalidScheme));
}
let (sender, receiver) = ws_transport(url).await?;
Ok(ClientBuilder::default()
.max_notifs_per_subscription(4096)
.build_with_tokio(sender, receiver))
}
async fn ws_transport(url: Uri) -> Result<(Sender, Receiver), Error> {
WsTransportClientBuilder::default()
.build(url)
.await
.map_err(|_| Error::LightClient(LightClientError::Handshake))
}
}
#[cfg(all(feature = "jsonrpsee", feature = "web"))]
mod jsonrpsee_helpers {
use crate::error::{Error, LightClientError};
pub use jsonrpsee::{
client_transport::web,
core::client::{Client, ClientBuilder},
};
/// Build web RPC client from URL
pub async fn client(url: &str) -> Result<Client, Error> {
let (sender, receiver) = web::connect(url)
.await
.map_err(|_| Error::LightClient(LightClientError::Handshake))?;
Ok(ClientBuilder::default()
.max_notifs_per_subscription(4096)
.build_with_wasm(sender, receiver))
}
}
+5 -10
View File
@@ -4,35 +4,30 @@
//! This module provides support for light clients.
mod background;
mod builder;
mod rpc;
use derivative::Derivative;
use crate::{
client::{OfflineClientT, OnlineClientT},
config::Config,
OnlineClient,
};
pub use builder::LightClientBuilder;
use derivative::Derivative;
use subxt_lightclient::LightClientRpcError;
/// Light client error.
#[derive(Debug, thiserror::Error)]
pub enum LightClientError {
/// Error encountered while adding the chain to the light-client.
#[error("Failed to add the chain to the light client: {0}.")]
AddChainError(String),
/// Error originated from the low-level RPC layer.
#[error("Rpc error: {0}")]
Rpc(LightClientRpcError),
/// The background task is closed.
#[error("Failed to communicate with the background task.")]
BackgroundClosed,
/// Invalid RPC parameters cannot be serialized as JSON string.
#[error("RPC parameters cannot be serialized as JSON string.")]
InvalidParams,
/// Error originated while trying to submit a RPC request.
#[error("RPC request cannot be sent: {0}.")]
Request(String),
/// The provided URL scheme is invalid.
///
/// Supported versions: WS, WSS.
+13 -86
View File
@@ -2,30 +2,22 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use super::{
background::{BackgroundTask, FromSubxt, MethodResponse},
LightClientError,
};
use super::LightClientError;
use crate::{
error::{Error, RpcError},
rpc::{RpcClientT, RpcFuture, RpcSubscription},
};
use futures::{stream::StreamExt, Stream};
use futures::{Stream, StreamExt};
use serde_json::value::RawValue;
use smoldot_light::{platform::default::DefaultPlatform as Platform, ChainId};
use std::pin::Pin;
use tokio::sync::{mpsc, mpsc::error::SendError, oneshot};
use subxt_lightclient::{AddChainConfig, ChainId, LightClientRpcError};
use tokio_stream::wrappers::UnboundedReceiverStream;
pub const LOG_TARGET: &str = "light-client";
/// The light-client RPC implementation that is used to connect with the chain.
#[derive(Clone)]
pub struct LightClientRpc {
/// Communicate with the backend task that multiplexes the responses
/// back to the frontend.
to_backend: mpsc::UnboundedSender<FromSubxt>,
}
pub struct LightClientRpc(subxt_lightclient::LightClientRpc);
impl LightClientRpc {
/// Constructs a new [`LightClientRpc`], providing the chain specification.
@@ -39,81 +31,12 @@ impl LightClientRpc {
///
/// Panics if being called outside of `tokio` runtime context.
pub fn new(
config: smoldot_light::AddChainConfig<'_, (), impl Iterator<Item = ChainId>>,
config: AddChainConfig<'_, (), impl Iterator<Item = ChainId>>,
) -> Result<LightClientRpc, Error> {
tracing::trace!(target: LOG_TARGET, "Create light client");
let rpc = subxt_lightclient::LightClientRpc::new(config)
.map_err(|err| LightClientError::Rpc(err))?;
let mut client = smoldot_light::Client::new(Platform::new(
env!("CARGO_PKG_NAME").into(),
env!("CARGO_PKG_VERSION").into(),
));
let smoldot_light::AddChainSuccess {
chain_id,
json_rpc_responses,
} = client
.add_chain(config)
.map_err(|err| LightClientError::AddChainError(err.to_string()))?;
let (to_backend, backend) = mpsc::unbounded_channel();
// `json_rpc_responses` can only be `None` if we had passed `json_rpc: Disabled`.
let rpc_responses = json_rpc_responses.expect("Light client RPC configured; qed");
tokio::spawn(async move {
let mut task = BackgroundTask::new(client, chain_id);
task.start_task(backend, rpc_responses).await;
});
Ok(LightClientRpc { to_backend })
}
/// Submits an RPC method request to the light-client.
///
/// This method sends a request to the light-client to execute an RPC method with the provided parameters.
/// The parameters are parsed into a valid JSON object in the background.
fn method_request(
&self,
method: String,
params: String,
) -> Result<oneshot::Receiver<MethodResponse>, SendError<FromSubxt>> {
let (sender, receiver) = oneshot::channel();
self.to_backend.send(FromSubxt::Request {
method,
params,
sender,
})?;
Ok(receiver)
}
/// Makes an RPC subscription call to the light-client.
///
/// This method sends a request to the light-client to establish an RPC subscription with the provided parameters.
/// The parameters are parsed into a valid JSON object in the background.
fn subscription_request(
&self,
method: String,
params: String,
) -> Result<
(
oneshot::Receiver<MethodResponse>,
mpsc::UnboundedReceiver<Box<RawValue>>,
),
SendError<FromSubxt>,
> {
let (sub_id, sub_id_rx) = oneshot::channel();
let (sender, receiver) = mpsc::unbounded_channel();
self.to_backend.send(FromSubxt::Subscription {
method,
params,
sub_id,
sender,
})?;
Ok((sub_id_rx, receiver))
Ok(LightClientRpc(rpc))
}
}
@@ -135,6 +58,7 @@ impl RpcClientT for LightClientRpc {
// Fails if the background is closed.
let rx = client
.0
.method_request(method.to_string(), params)
.map_err(|_| RpcError::ClientError(Box::new(LightClientError::BackgroundClosed)))?;
@@ -174,6 +98,7 @@ impl RpcClientT for LightClientRpc {
// Fails if the background is closed.
let (sub_id, notif) = client
.0
.subscription_request(sub.to_string(), params)
.map_err(|_| RpcError::ClientError(Box::new(LightClientError::BackgroundClosed)))?;
@@ -182,7 +107,9 @@ impl RpcClientT for LightClientRpc {
.await
.map_err(|_| RpcError::ClientError(Box::new(LightClientError::BackgroundClosed)))?
.map_err(|err| {
RpcError::ClientError(Box::new(LightClientError::Request(err.to_string())))
RpcError::ClientError(Box::new(LightClientError::Rpc(
LightClientRpcError::Request(err.to_string()),
)))
})?;
let sub_id = result
-2
View File
@@ -227,8 +227,6 @@ pub type SystemProperties = serde_json::Map<String, serde_json::Value>;
///
/// This is copied from `sp-transaction-pool` to avoid a dependency on that crate. Therefore it
/// must be kept compatible with that type from the target substrate version.
///
/// Substrate produces `camelCase` events, while smoldot produces `CamelCase` events.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SubstrateTxStatus<Hash, BlockHash> {
+1 -1
View File
@@ -11,7 +11,7 @@ subxt = { workspace = true, features = ["native"] }
substrate-runner = { workspace = true }
impl-serde = { workspace = true }
serde = { workspace = true }
tokio = { workspace = true }
tokio = { workspace = true, features = ["rt-multi-thread"] }
which = { workspace = true }
jsonrpsee = { workspace = true, features = ["async-client", "client-ws-transport"] }
hex = { workspace = true }
File diff suppressed because it is too large Load Diff
+18
View File
@@ -0,0 +1,18 @@
[package]
name = "wasm-lightclient-tests"
version = "0.1.0"
edition = "2021"
publish = false
[dev-dependencies]
wasm-bindgen-test = "0.3.24"
tracing-wasm = "0.2.1"
tracing = "0.1.34"
console_error_panic_hook = "0.1.7"
serde_json = "1"
futures-util = "0.3.28"
# This crate is not a part of the workspace, because it
# requires the "jsonrpsee web unstable-light-client" features to be enabled, which we don't
# want enabled for workspace builds in general.
subxt = { path = "../../subxt", default-features = false, features = ["web", "jsonrpsee", "unstable-light-client"] }
@@ -0,0 +1,56 @@
#![cfg(target_arch = "wasm32")]
use subxt::{config::PolkadotConfig,
client::{LightClient, OfflineClientT, LightClientBuilder},
};
use futures_util::StreamExt;
use wasm_bindgen_test::*;
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
// Run the tests by calling:
//
// ```text
// wasm-pack test --firefox --headless`
// ```
//
// You'll need to have a substrate/polkadot node running:
//
// ```bash
// # Polkadot does not accept by default WebSocket connections to the P2P network.
// # Ensure `--listen-addr` is provided with valid ws adddress endpoint.
// # The `--node-key` provides a deterministic p2p address for the node.
// ./polkadot --dev --node-key 0000000000000000000000000000000000000000000000000000000000000001 --listen-addr /ip4/0.0.0.0/tcp/30333/ws
// ```
//
// Use the following to enable logs:
// ```
// console_error_panic_hook::set_once();
// tracing_wasm::set_as_global_default();
// ```
#[wasm_bindgen_test]
async fn light_client_works() {
// Use a polkadot trusted DNS.
let api: LightClient<PolkadotConfig> = LightClientBuilder::new()
.bootnodes(
["/dns/polkadot-connect-0.parity.io/tcp/443/wss/p2p/12D3KooWEPmjoRpDSUuiTjvyNDd8fejZ9eNWH5bE965nyBMDrB4o"]
)
.build_from_url("wss://rpc.polkadot.io:443")
.await
.expect("Cannot construct light client");
tracing::info!("Subscribe to latest finalized blocks: ");
let mut blocks_sub = api.blocks().subscribe_finalized().await.expect("Cannot subscribe to finalized hashes").take(3);
// For each block, print a bunch of information about it:
while let Some(block) = blocks_sub.next().await {
let block = block.expect("Block not valid");
let block_number = block.header().number;
let block_hash = block.hash();
tracing::info!("Block #{block_number}:");
tracing::info!(" Hash: {block_hash}");
}
}
+1
View File
@@ -0,0 +1 @@
/target
File diff suppressed because it is too large Load Diff
@@ -1,5 +1,5 @@
[package]
name = "wasm-tests"
name = "wasm-rpc-tests"
version = "0.1.0"
edition = "2021"
publish = false
@@ -7,8 +7,10 @@ publish = false
[dev-dependencies]
wasm-bindgen-test = "0.3.24"
tracing-wasm = "0.2.1"
tracing = "0.1.34"
console_error_panic_hook = "0.1.7"
serde_json = "1"
futures-util = "0.3.28"
# This crate is not a part of the workspace, because it
# requires the "jsonrpsee web" features to be enabled, which we don't
@@ -1,6 +1,6 @@
#![cfg(target_arch = "wasm32")]
use subxt::config::PolkadotConfig;
use subxt::{config::PolkadotConfig};
use wasm_bindgen_test::*;
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
@@ -11,7 +11,17 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
// wasm-pack test --firefox --headless`
// ```
//
// You'll need to have a substrate/polkadot node running too with eg `substrate --dev`.
// You'll need to have a substrate/polkadot node running:
//
// ```bash
// ./polkadot --dev
// ```
//
// Use the following to enable logs:
// ```
// console_error_panic_hook::set_once();
// tracing_wasm::set_as_global_default();
// ```
#[wasm_bindgen_test]
async fn wasm_ws_transport_works() {