Add LightClient::builder() (#1088)

* Add LightClient::build()

* newline

* fmt

* Tweaks to make LightClient more like OnlineClient in interface
This commit is contained in:
James Wilson
2023-07-24 15:59:45 +01:00
committed by GitHub
parent 560432b92b
commit 770483d9cd
4 changed files with 81 additions and 22 deletions
+1
View File
@@ -6,3 +6,4 @@ cargo-timing*
/examples/wasm-example/target
/examples/parachain-example/target
/examples/parachain-example/metadata/target
.vscode
@@ -1,9 +1,5 @@
use sp_keyring::AccountKeyring;
use subxt::{
client::{LightClient, LightClientBuilder, OfflineClientT},
tx::PairSigner,
PolkadotConfig,
};
use subxt::{client::LightClient, PolkadotConfig};
use subxt_signer::sr25519::dev;
// Generate an interface that we can use from the node's metadata.
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")]
@@ -11,6 +7,7 @@ pub mod polkadot {}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// The smoldot logs are informative:
tracing_subscriber::fmt::init();
// Create a light client by fetching the chain spec of a local running node.
@@ -20,7 +17,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// The `12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp` is the P2P address
// from a local polkadot node starting with
// `--node-key 0000000000000000000000000000000000000000000000000000000000000001`
let api: LightClient<PolkadotConfig> = LightClientBuilder::new()
let api = LightClient::<PolkadotConfig>::builder()
.bootnodes([
"/ip4/127.0.0.1/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp",
])
@@ -28,12 +25,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.await?;
// Build a balance transfer extrinsic.
let dest = AccountKeyring::Bob.to_account_id().into();
let dest = dev::bob().public_key().into();
let balance_transfer_tx = polkadot::tx().balances().transfer(dest, 10_000);
// Submit the balance transfer extrinsic from Alice, and wait for it to be successful
// and in a finalized block. We get back the extrinsic events if all is well.
let from = PairSigner::new(AccountKeyring::Alice.pair());
let from = dev::alice();
let events = api
.tx()
.sign_and_submit_then_watch_default(&balance_transfer_tx, &from)
+9 -10
View File
@@ -12,14 +12,15 @@ use std::sync::Arc;
/// Builder for [`LightClient`].
#[derive(Clone, Debug)]
pub struct LightClientBuilder {
pub struct LightClientBuilder<T: Config> {
max_pending_requests: NonZeroU32,
max_subscriptions: u32,
bootnodes: Option<Vec<serde_json::Value>>,
potential_relay_chains: Option<Vec<ChainId>>,
_marker: std::marker::PhantomData<T>,
}
impl Default for LightClientBuilder {
impl<T: Config> Default for LightClientBuilder<T> {
fn default() -> Self {
Self {
max_pending_requests: NonZeroU32::new(128)
@@ -27,13 +28,14 @@ impl Default for LightClientBuilder {
max_subscriptions: 1024,
bootnodes: None,
potential_relay_chains: None,
_marker: std::marker::PhantomData,
}
}
}
impl LightClientBuilder {
impl<T: Config> LightClientBuilder<T> {
/// Create a new [`LightClientBuilder`].
pub fn new() -> LightClientBuilder {
pub fn new() -> LightClientBuilder<T> {
LightClientBuilder::default()
}
@@ -90,10 +92,7 @@ impl LightClientBuilder {
///
/// Panics if being called outside of `tokio` runtime context.
#[cfg(feature = "jsonrpsee")]
pub async fn build_from_url<T: Config, Url: AsRef<str>>(
self,
url: Url,
) -> Result<LightClient<T>, Error> {
pub async fn build_from_url<Url: AsRef<str>>(self, url: Url) -> Result<LightClient<T>, Error> {
let chain_spec = fetch_url(url.as_ref()).await?;
self.build_client(chain_spec).await
@@ -120,7 +119,7 @@ impl LightClientBuilder {
/// ## Panics
///
/// Panics if being called outside of `tokio` runtime context.
pub async fn build<T: Config>(self, chain_spec: &str) -> Result<LightClient<T>, Error> {
pub async fn build(self, chain_spec: &str) -> Result<LightClient<T>, Error> {
let chain_spec = serde_json::from_str(chain_spec)
.map_err(|_| Error::LightClient(LightClientError::InvalidChainSpec))?;
@@ -128,7 +127,7 @@ impl LightClientBuilder {
}
/// Build the light client.
async fn build_client<T: Config>(
async fn build_client(
self,
mut chain_spec: serde_json::Value,
) -> Result<LightClient<T>, Error> {
+65 -3
View File
@@ -8,8 +8,14 @@ mod builder;
mod rpc;
use crate::{
blocks::BlocksClient,
client::{OfflineClientT, OnlineClientT},
config::Config,
constants::ConstantsClient,
events::EventsClient,
runtime_api::RuntimeApiClient,
storage::StorageClient,
tx::TxClient,
OnlineClient,
};
pub use builder::LightClientBuilder;
@@ -49,6 +55,62 @@ pub enum LightClientError {
#[derivative(Clone(bound = ""))]
pub struct LightClient<T: Config>(OnlineClient<T>);
impl<T: Config> LightClient<T> {
/// Construct a [`LightClient`] using its builder interface.
pub fn builder() -> LightClientBuilder<T> {
LightClientBuilder::new()
}
// We add the below impls so that we don't need to
// think about importing the OnlineClientT/OfflineClientT
// traits to use these things:
/// Return the [`crate::Metadata`] used in this client.
fn metadata(&self) -> crate::Metadata {
self.0.metadata()
}
/// Return the genesis hash.
fn genesis_hash(&self) -> <T as Config>::Hash {
self.0.genesis_hash()
}
/// Return the runtime version.
fn runtime_version(&self) -> crate::rpc::types::RuntimeVersion {
self.0.runtime_version()
}
/// Work with transactions.
pub fn tx(&self) -> TxClient<T, Self> {
<Self as OfflineClientT<T>>::tx(self)
}
/// Work with events.
pub fn events(&self) -> EventsClient<T, Self> {
<Self as OfflineClientT<T>>::events(self)
}
/// Work with storage.
pub fn storage(&self) -> StorageClient<T, Self> {
<Self as OfflineClientT<T>>::storage(self)
}
/// Access constants.
pub fn constants(&self) -> ConstantsClient<T, Self> {
<Self as OfflineClientT<T>>::constants(self)
}
/// Work with blocks.
pub fn blocks(&self) -> BlocksClient<T, Self> {
<Self as OfflineClientT<T>>::blocks(self)
}
/// Work with runtime API.
pub fn runtime_api(&self) -> RuntimeApiClient<T, Self> {
<Self as OfflineClientT<T>>::runtime_api(self)
}
}
impl<T: Config> OnlineClientT<T> for LightClient<T> {
fn rpc(&self) -> &crate::rpc::Rpc<T> {
self.0.rpc()
@@ -57,14 +119,14 @@ impl<T: Config> OnlineClientT<T> for LightClient<T> {
impl<T: Config> OfflineClientT<T> for LightClient<T> {
fn metadata(&self) -> crate::Metadata {
self.0.metadata()
self.metadata()
}
fn genesis_hash(&self) -> <T as Config>::Hash {
self.0.genesis_hash()
self.genesis_hash()
}
fn runtime_version(&self) -> crate::rpc::types::RuntimeVersion {
self.0.runtime_version()
self.runtime_version()
}
}