Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Allow integrated relay chain light client #2270

Merged
merged 57 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
f6eda5b
Add embedded light client to cli
skunert Feb 21, 2023
d5c7791
Prepare for light-client-worker
skunert Feb 23, 2023
efc9ebc
First working version
skunert Feb 27, 2023
a316e51
Clean up
skunert Feb 28, 2023
5c0af7e
Remove unwanted logs
skunert Feb 28, 2023
cd9ebe0
Simplify subscription code
skunert Feb 28, 2023
19c82f9
Let jsonrpsee handle rpc management
skunert Mar 1, 2023
30490cc
Simplify implementation
skunert Mar 1, 2023
3a807c1
Reorganize crate structure
skunert Mar 1, 2023
1aee380
Use relay chain arg chainspec for light-client
skunert Mar 1, 2023
fbcd69d
Clean up command line
skunert Mar 1, 2023
27c0ef9
Add light client worker file
skunert Mar 2, 2023
7368580
Merge branch 'master' into smoldot-integration
skunert Mar 2, 2023
007a699
Use smoldot master to avoid wasmtime conflict
skunert Mar 2, 2023
85c6156
Remove sleep
skunert Mar 2, 2023
489f1ae
Improve naming of cli option
skunert Mar 2, 2023
45e5b45
Remove conflict with `validator`
skunert Mar 3, 2023
35f9649
Improve docs
skunert Mar 3, 2023
164d430
Merge branch 'master' into smoldot-integration
skunert Mar 3, 2023
08efa4b
Update smoldot, remove unwanted change
skunert Mar 3, 2023
e450413
Apply suggestions from code review
skunert Mar 3, 2023
7997f2b
Merge branch 'master' into smoldot-integration
skunert Mar 7, 2023
9ce2ddd
Disable collation
skunert Mar 7, 2023
33a1b4c
Reviewer comments
skunert Mar 9, 2023
79d440d
Merge branch 'master' into smoldot-integration
skunert Mar 27, 2023
7d0635c
Update smoldot and tokio-platform
skunert Mar 27, 2023
181ce72
Update smoldot
skunert Mar 31, 2023
0faada2
Merge remote-tracking branch 'origin/master' into smoldot-integration
skunert Mar 31, 2023
595fd4a
Merge branch 'master' into smoldot-integration
skunert Jun 6, 2023
2dbed4b
Update smoldot
skunert Jun 6, 2023
4982892
Adjust to new version
skunert Jun 6, 2023
197a77d
Patch substrate
skunert Jun 7, 2023
9eba856
Use constants
skunert Jun 7, 2023
410be7c
Add readme entry, improve zombienet tests
skunert Jun 7, 2023
a1d5b3d
Apply suggestions from code review
skunert Jun 7, 2023
26fce06
Make execution mode an enum
skunert Jun 7, 2023
d11800c
Merge branch 'master' into smoldot-integration
skunert Jun 7, 2023
b4f372b
Update smoldot, remove substrate patch
skunert Jun 8, 2023
660bd56
Merge branch 'master' into smoldot-integration
skunert Jun 8, 2023
0c79ece
Update client/relay-chain-rpc-interface/src/rpc_client.rs
skunert Jun 19, 2023
4bba54e
Reduce duplicate code
skunert Jun 19, 2023
8a560bf
Merge branch 'master' into smoldot-integration
skunert Jun 19, 2023
b98a70f
Update smoldot
skunert Jun 19, 2023
6cffdb9
Merge branch 'master' into smoldot-integration
skunert Jul 4, 2023
b96de21
Merge branch 'master' into smoldot-integration
skunert Jul 19, 2023
3779ab1
Update smoldot
skunert Jul 19, 2023
e826941
Merge branch 'master' into smoldot-integration
skunert Aug 21, 2023
145f396
Fix build
skunert Aug 21, 2023
59c275d
Update smoldot
skunert Aug 21, 2023
74c54b5
Make platform compile
skunert Aug 22, 2023
145d2c5
Clean up dependencies
skunert Aug 22, 2023
4be2a3e
Use crates.io instead of github for smoldot
skunert Aug 22, 2023
0708c63
Apply suggestions from code review
skunert Aug 22, 2023
e58e322
Docs
skunert Aug 22, 2023
0bbc284
Improve docs
skunert Aug 22, 2023
3e481e6
Remove `RpcFrontend`
skunert Aug 22, 2023
4ba2920
Merge branch 'master' into smoldot-integration
skunert Aug 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
895 changes: 626 additions & 269 deletions Cargo.lock
skunert marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ opt-level = 3
inherits = "release"
lto = true
codegen-units = 1

24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ To operate a parachain node, a connection to the corresponding relay chain is ne
achieved in one of two ways:
1. Run a full relay chain node within the parachain node (default)
2. Connect to an external relay chain node via WebSocket RPC
2. Run a light client for the relay chain
skunert marked this conversation as resolved.
Show resolved Hide resolved

#### In-process Relay Chain Node
If an external relay chain node is not specified (default behavior), then a full relay chain node is
Expand All @@ -51,7 +52,7 @@ This node has all of the typical components of a regular Polkadot node and will
with the relay chain to work.

##### Example command
```shell=
```bash
polkadot-parachain \
--chain parachain-chainspec.json \
--tmp \
Expand All @@ -72,7 +73,7 @@ they will use fewer system resources.
relay chain node in-process. Even though they lack the majority of normal Polkadot subsystems, they
will still need to connect directly to the relay chain network.
##### Example command
```shell=
```bash
polkadot-parachain \
--chain parachain-chainspec.json \
--tmp \
Expand All @@ -83,6 +84,25 @@ polkadot-parachain \
--chain relaychain-chainspec.json
```

#### Relay Chain Light Client
An internal relay chain light client provides a fast and lightweight approach for connecting to the relay chain network. It provides relay chain notifications and facilitates runtime calls.
skunert marked this conversation as resolved.
Show resolved Hide resolved

To specify which chain the light client should connect to, users need to supply a relay chain chain-spec as part of the relay chain arguments.

**Note:** At this time, any parachain nodes using this feature will still spawn a significantly cut-down
relay chain node in-process. Even though they lack the majority of normal Polkadot subsystems, they
will still need to connect directly to the relay chain network.

##### Example command
```bash
polkadot-parachain \
--chain parachain-chainspec.json \
--tmp \
--relay-chain-light-client \
-- \
--chain relaychain-chainspec.json
```

## Installation and Setup
Before building Cumulus SDK based nodes / runtimes prepare your environment by following Substrate
[installation instructions](https://docs.substrate.io/main-docs/install/).
Expand Down
29 changes: 26 additions & 3 deletions client/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ pub struct RunCmd {
alias = "relay-chain-rpc-url"
)]
pub relay_chain_rpc_urls: Vec<Url>,

/// EXPERIMENTAL: Embed a light client for the relay chain. Only supported for full-nodes.
/// Will use the specified relay chain chainspec.
#[arg(long, conflicts_with_all = ["relay_chain_rpc_urls", "collator"])]
pub relay_chain_light_client: bool,
}

impl RunCmd {
Expand All @@ -312,15 +317,33 @@ impl RunCmd {

/// Create [`CollatorOptions`] representing options only relevant to parachain collator nodes
pub fn collator_options(&self) -> CollatorOptions {
CollatorOptions { relay_chain_rpc_urls: self.relay_chain_rpc_urls.clone() }
let relay_chain_mode =
match (self.relay_chain_light_client, !self.relay_chain_rpc_urls.is_empty()) {
(true, _) => RelayChainMode::LightClient,
(_, true) => RelayChainMode::ExternalRpc(self.relay_chain_rpc_urls.clone()),
_ => RelayChainMode::Embedded,
};

CollatorOptions { relay_chain_mode }
}
}

/// Possible modes for the relay chain to operate in.
#[derive(Clone, Debug)]
pub enum RelayChainMode {
/// Spawn embedded relay chain node
Embedded,
/// Connect to remote relay chain node via websocket RPC
ExternalRpc(Vec<Url>),
/// Spawn embedded relay chain light client
LightClient,
}

/// Options only relevant for collator nodes
#[derive(Clone, Debug)]
pub struct CollatorOptions {
dmitry-markin marked this conversation as resolved.
Show resolved Hide resolved
/// Location of relay chain full node
pub relay_chain_rpc_urls: Vec<Url>,
/// How this collator retrieves relay chain information
pub relay_chain_mode: RelayChainMode,
}
skunert marked this conversation as resolved.
Show resolved Hide resolved

/// A non-redundant version of the `RunCmd` that sets the `validator` field when the
Expand Down
43 changes: 38 additions & 5 deletions client/relay-chain-minimal-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ use sp_runtime::{app_crypto::Pair, traits::Block as BlockT};
use futures::StreamExt;
use std::sync::Arc;

mod blockchain_rpc_client;
mod collator_overseer;

mod network;

mod blockchain_rpc_client;
pub use blockchain_rpc_client::BlockChainRpcClient;

const LOG_TARGET: &str = "minimal-relaychain-node";

fn build_authority_discovery_service<Block: BlockT>(
task_manager: &TaskManager,
client: Arc<BlockChainRpcClient>,
Expand Down Expand Up @@ -80,7 +81,7 @@ fn build_authority_discovery_service<Block: BlockT>(
service
}

pub async fn build_minimal_relay_chain_node(
pub async fn build_minimal_relay_chain_node_with_rpc(
polkadot_config: Configuration,
task_manager: &mut TaskManager,
relay_chain_url: Vec<Url>,
Expand All @@ -104,6 +105,40 @@ pub async fn build_minimal_relay_chain_node(
))
}

pub async fn build_minimal_relay_chain_node_light_client(
skunert marked this conversation as resolved.
Show resolved Hide resolved
polkadot_config: Configuration,
task_manager: &mut TaskManager,
) -> RelayChainResult<(Arc<(dyn RelayChainInterface + 'static)>, Option<CollatorPair>)> {
tracing::info!(
target: LOG_TARGET,
chain_name = polkadot_config.chain_spec.name(),
chain_id = polkadot_config.chain_spec.id(),
"Initializing embedded light client with chain spec."
);

let spec = polkadot_config
.chain_spec
.as_json(false)
.map_err(RelayChainError::GenericError)?;

let client = cumulus_relay_chain_rpc_interface::create_client_and_start_light_client_worker(
spec,
task_manager,
)
.await?;
let collator_pair = CollatorPair::generate().0;
let collator_node = new_minimal_relay_chain(
polkadot_config,
collator_pair.clone(),
Arc::new(BlockChainRpcClient::new(client.clone())),
)
.await?;
task_manager.add_child(collator_node.task_manager);
Ok((
Arc::new(RelayChainRpcInterface::new(client, collator_node.overseer_handle)),
Some(collator_pair),
))
}
/// Builds a minimal relay chain node. Chain data is fetched
/// via [`BlockChainRpcClient`] and fed into the overseer and its subsystems.
///
Expand All @@ -115,8 +150,6 @@ pub async fn build_minimal_relay_chain_node(
/// - NetworkBridgeRx
/// - NetworkBridgeTx
/// - RuntimeApi
/// - ChainApi
/// - AvailabilityDistribution
#[sc_tracing::logging::prefix_logs_with("Relaychain")]
async fn new_minimal_relay_chain(
config: Configuration,
Expand Down
10 changes: 9 additions & 1 deletion client/relay-chain-rpc-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ sp-consensus-babe = { git = "https://github.com/paritytech/substrate", branch =
sp-authority-discovery = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-storage = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-rpc-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-service = { git = "https://github.com/paritytech/substrate", branch = "master" }

tokio = { version = "1.28.2", features = ["sync"] }

tokio-util = { version = "0.7.8", features = ["compat"] }
futures = "0.3.28"
futures-timer = "3.0.2"
parity-scale-codec = "3.5.0"
Expand All @@ -33,3 +34,10 @@ url = "2.4.0"
serde_json = "1.0.96"
serde = "1.0.163"
lru = "0.9.0"
smoldot = { git = "https://github.com/smol-dot/smoldot", rev = "cf211107" , default_features = false, features = ["std"]}
smoldot-light = { git = "https://github.com/smol-dot/smoldot", rev = "cf211107", default_features = false, features = ["std"] }
skunert marked this conversation as resolved.
Show resolved Hide resolved
parking_lot = "0.12.1"
either = "1.8.1"
event-listener = "2.5.3"
thiserror = "1.0.38"
soketto = "0.7.1"
skunert marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 7 additions & 1 deletion client/relay-chain-rpc-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ use std::pin::Pin;

pub use url::Url;

mod light_client_worker;
mod reconnecting_ws_client;
mod rpc_client;
pub use rpc_client::{create_client_and_start_worker, RelayChainRpcClient};
mod tokio_platform;

pub use rpc_client::{
create_client_and_start_light_client_worker, create_client_and_start_worker,
RelayChainRpcClient,
};

const TIMEOUT_IN_SECONDS: u64 = 6;

Expand Down
Loading