Merge branch 'master' into staking

This commit is contained in:
Demi M. Obenour
2020-08-05 12:57:15 -04:00
19 changed files with 872 additions and 498 deletions
+10
View File
@@ -1,3 +1,13 @@
# Version 0.11.0
* Fix build error, wabt 0.9.2 is yanked [#146](https://github.com/paritytech/substrate-subxt/pull/146)
* Rc5 [#143](https://github.com/paritytech/substrate-subxt/pull/143)
* Refactor: extract functions and types for creating extrinsics [#138](https://github.com/paritytech/substrate-subxt/pull/138)
* event subscription example [#140](https://github.com/paritytech/substrate-subxt/pull/140)
* Document the `Call` derive macro [#137](https://github.com/paritytech/substrate-subxt/pull/137)
* Document the #[module] macro [#135](https://github.com/paritytech/substrate-subxt/pull/135)
* Support authors api. [#134](https://github.com/paritytech/substrate-subxt/pull/134)
# Version 0.10.1 (2020-06-19) # Version 0.10.1 (2020-06-19)
* Release client v0.2.0 [#133](https://github.com/paritytech/substrate-subxt/pull/133) * Release client v0.2.0 [#133](https://github.com/paritytech/substrate-subxt/pull/133)
+29 -15
View File
@@ -3,7 +3,7 @@ members = [".", "client", "proc-macro", "test-node"]
[package] [package]
name = "substrate-subxt" name = "substrate-subxt"
version = "0.10.1" version = "0.11.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018" edition = "2018"
@@ -22,22 +22,23 @@ default = ["kusama"]
client = ["substrate-subxt-client"] client = ["substrate-subxt-client"]
[dependencies] [dependencies]
log = "0.4.8" log = "0.4.11"
thiserror = "1.0.20" thiserror = "1.0.20"
futures = { version = "0.3.5", package = "futures" } futures = { version = "0.3.5", package = "futures" }
jsonrpsee = { version = "0.1.0", features = ["ws"] } jsonrpsee = { version = "0.1.0", features = ["ws"] }
num-traits = { version = "0.2.12", default-features = false } num-traits = { version = "0.2.12", default-features = false }
serde = { version = "1.0.114", features = ["derive"] } serde = { version = "1.0.114", features = ["derive"] }
serde_json = "1.0.55" serde_json = "1.0.56"
url = "2.1.1" url = "2.1.1"
codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false, features = ["derive", "full"] } codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive", "full"] }
frame-metadata = { version = "11.0.0-rc4", package = "frame-metadata" } frame-metadata = { version = "11.0.0-rc5", package = "frame-metadata" }
frame-support = { version = "2.0.0-rc4", package = "frame-support" } frame-support = { version = "2.0.0-rc5", package = "frame-support" }
sp-runtime = { version = "2.0.0-rc4", package = "sp-runtime" } sp-runtime = { version = "2.0.0-rc5", package = "sp-runtime" }
sp-version = { version = "2.0.0-rc4", package = "sp-version" } sp-version = { version = "2.0.0-rc5", package = "sp-version" }
pallet-indices = { version = "2.0.0-rc4", package = "pallet-indices" } pallet-indices = { version = "2.0.0-rc5", package = "pallet-indices" }
hex = "0.4.2" hex = "0.4.2"
<<<<<<< HEAD
sp-rpc = { version = "2.0.0-rc4", package = "sp-rpc" } sp-rpc = { version = "2.0.0-rc4", package = "sp-rpc" }
sp-core = { version = "2.0.0-rc4", package = "sp-core" } sp-core = { version = "2.0.0-rc4", package = "sp-core" }
sc-rpc-api = { version = "0.8.0-rc4", package = "sc-rpc-api" } sc-rpc-api = { version = "0.8.0-rc4", package = "sc-rpc-api" }
@@ -54,15 +55,25 @@ pallet-staking = "2.0.0-rc4"
[dev-dependencies] [dev-dependencies]
async-std = { version = "1.5.0", features = ["attributes"] } async-std = { version = "1.5.0", features = ["attributes"] }
=======
sp-rpc = { version = "2.0.0-rc5", package = "sp-rpc" }
sp-core = { version = "2.0.0-rc5", package = "sp-core" }
sc-rpc-api = { version = "0.8.0-rc5", package = "sc-rpc-api" }
sp-transaction-pool = { version = "2.0.0-rc5", package = "sp-transaction-pool" }
substrate-subxt-client = { version = "0.3.0", path = "client", optional = true }
substrate-subxt-proc-macro = { version = "0.11.0", path = "proc-macro" }
[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
>>>>>>> master
env_logger = "0.7.1" env_logger = "0.7.1"
wabt = "0.9.2" frame-system = { version = "2.0.0-rc5", package = "frame-system" }
wabt-sys = "=0.7.1" # pinned because 0.7.2 fails to compile pallet-balances = { version = "2.0.0-rc5", package = "pallet-balances" }
frame-system = { version = "2.0.0-rc4", package = "frame-system" } sp-keyring = { version = "2.0.0-rc5", package = "sp-keyring" }
pallet-balances = { version = "2.0.0-rc4", package = "pallet-balances" } substrate-subxt-client = { version = "0.3.0", path = "client" }
sp-keyring = { version = "2.0.0-rc4", package = "sp-keyring" }
substrate-subxt-client = { version = "0.2.0", path = "client" }
tempdir = "0.3.7" tempdir = "0.3.7"
test-node = { path = "test-node" } test-node = { path = "test-node" }
<<<<<<< HEAD
[patch.crates-io] [patch.crates-io]
frame-metadata = { git = "https://github.com/paritytech/substrate", package = "frame-metadata" } frame-metadata = { git = "https://github.com/paritytech/substrate", package = "frame-metadata" }
@@ -85,3 +96,6 @@ sc-service = { git = "https://github.com/paritytech/substrate" }
sc-cli = { git = "https://github.com/paritytech/substrate" } sc-cli = { git = "https://github.com/paritytech/substrate" }
sc-transaction-pool = { git = "https://github.com/paritytech/substrate" } sc-transaction-pool = { git = "https://github.com/paritytech/substrate" }
jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee" } jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee" }
=======
wabt = "0.10.0"
>>>>>>> master
+9 -9
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "substrate-subxt-client" name = "substrate-subxt-client"
version = "0.2.0" version = "0.3.0"
authors = ["David Craven <david@craven.ch>", "Parity Technologies <admin@parity.io>"] authors = ["David Craven <david@craven.ch>", "Parity Technologies <admin@parity.io>"]
edition = "2018" edition = "2018"
@@ -12,19 +12,19 @@ description = "Embed a substrate node into your subxt application."
keywords = ["parity", "substrate", "blockchain"] keywords = ["parity", "substrate", "blockchain"]
[dependencies] [dependencies]
async-std = "=1.5.0" async-std = "1.6.2"
futures = { version = "0.3.5", features = ["compat"], package = "futures" } futures = { version = "0.3.5", features = ["compat"] }
futures01 = { package = "futures", version = "0.1.29" } futures01 = { package = "futures", version = "0.1.29" }
jsonrpsee = "0.1.0" jsonrpsee = "0.1.0"
log = "0.4.8" log = "0.4.11"
sc-network = { version = "0.8.0-rc4", default-features = false } sc-network = { version = "0.8.0-rc5", default-features = false }
sc-service = { version = "0.8.0-rc4", default-features = false } sc-service = { version = "0.8.0-rc5", default-features = false }
serde_json = "1.0.55" serde_json = "1.0.56"
sp-keyring = "2.0.0-rc4" sp-keyring = "2.0.0-rc5"
thiserror = "1.0.20" thiserror = "1.0.20"
[dev-dependencies] [dev-dependencies]
async-std = { version = "=1.5.0", features = ["attributes"] } async-std = { version = "1.6.2", features = ["attributes"] }
env_logger = "0.7.1" env_logger = "0.7.1"
substrate-subxt = { path = ".." } substrate-subxt = { path = ".." }
tempdir = "0.3.7" tempdir = "0.3.7"
+191 -180
View File
@@ -20,20 +20,20 @@
use async_std::task; use async_std::task;
use futures::{ use futures::{
channel::mpsc,
compat::{ compat::{
Compat01As03, Compat01As03,
Compat01As03Sink,
Sink01CompatExt, Sink01CompatExt,
Stream01CompatExt, Stream01CompatExt,
}, },
future::poll_fn, future::{
sink::SinkExt, select,
stream::{ FutureExt,
Stream,
StreamExt,
}, },
sink::SinkExt,
stream::StreamExt,
}; };
use futures01::sync::mpsc; use futures01::sync::mpsc as mpsc01;
use jsonrpsee::{ use jsonrpsee::{
common::{ common::{
Request, Request,
@@ -53,16 +53,18 @@ use sc_service::{
config::{ config::{
NetworkConfiguration, NetworkConfiguration,
TaskType, TaskType,
TelemetryEndpoints,
}, },
AbstractService,
ChainSpec, ChainSpec,
Configuration, Configuration,
RpcHandlers,
RpcSession, RpcSession,
TaskManager,
}; };
use std::{ use std::{
future::Future, future::Future,
pin::Pin, pin::Pin,
task::Poll, sync::Arc,
}; };
use thiserror::Error; use thiserror::Error;
@@ -74,81 +76,59 @@ pub enum SubxtClientError {
Json(#[from] serde_json::Error), Json(#[from] serde_json::Error),
/// Channel closed. /// Channel closed.
#[error("{0}")] #[error("{0}")]
Mpsc(#[from] mpsc::SendError<String>), Mpsc(#[from] mpsc::SendError),
}
/// Role of the node.
#[derive(Clone, Copy, Debug)]
pub enum Role {
/// Light client.
Light,
/// A full node (maninly used for testing purposes).
Authority(sp_keyring::AccountKeyring),
}
impl From<Role> for sc_service::Role {
fn from(role: Role) -> Self {
match role {
Role::Light => Self::Light,
Role::Authority(_) => {
Self::Authority {
sentry_nodes: Default::default(),
}
}
}
}
}
impl From<Role> for Option<String> {
fn from(role: Role) -> Self {
match role {
Role::Light => None,
Role::Authority(key) => Some(key.to_seed()),
}
}
}
/// Client configuration.
#[derive(Clone)]
pub struct SubxtClientConfig<C: ChainSpec + 'static, S: AbstractService> {
/// Name of the implementation.
pub impl_name: &'static str,
/// Version of the implementation.
pub impl_version: &'static str,
/// Author of the implementation.
pub author: &'static str,
/// Copyright start year.
pub copyright_start_year: i32,
/// Database configuration.
pub db: DatabaseConfig,
/// Keystore configuration.
pub keystore: KeystoreConfig,
/// Service builder.
pub builder: fn(Configuration) -> Result<S, sc_service::Error>,
/// Chain specification.
pub chain_spec: C,
/// Role of the node.
pub role: Role,
} }
/// Client for an embedded substrate node. /// Client for an embedded substrate node.
pub struct SubxtClient { pub struct SubxtClient {
to_back: Compat01As03Sink<mpsc::Sender<String>, String>, to_back: mpsc::Sender<String>,
from_back: Compat01As03<mpsc::Receiver<String>>, from_back: Compat01As03<mpsc01::Receiver<String>>,
} }
impl SubxtClient { impl SubxtClient {
/// Create a new client from a config. /// Create a new client.
pub fn new<C: ChainSpec + 'static, S: AbstractService>( pub fn new(mut task_manager: TaskManager, rpc: Arc<RpcHandlers>) -> Self {
config: SubxtClientConfig<C, S>,
) -> Result<Self, ServiceError> {
let (to_back, from_front) = mpsc::channel(4); let (to_back, from_front) = mpsc::channel(4);
let (to_front, from_back) = mpsc::channel(4); let (to_front, from_back) = mpsc01::channel(4);
start_subxt_client(config, from_front, to_front)?;
Ok(Self { let session = RpcSession::new(to_front.clone());
to_back: to_back.sink_compat(), let session2 = session.clone();
task::spawn(
select(
Box::pin(from_front.for_each(move |message: String| {
let rpc = rpc.clone();
let session = session2.clone();
let mut to_front = to_front.clone().sink_compat();
async move {
let response = rpc.rpc_query(&session, &message).await;
if let Some(response) = response {
to_front.send(response).await.ok();
}
}
})),
Box::pin(async move {
task_manager.future().await.ok();
}),
)
.map(drop),
);
Self {
to_back,
from_back: from_back.compat(), from_back: from_back.compat(),
}) }
}
/// Creates a new client from a config.
pub fn from_config<C: ChainSpec + 'static>(
config: SubxtClientConfig<C>,
builder: impl Fn(
Configuration,
) -> Result<(TaskManager, Arc<RpcHandlers>), ServiceError>,
) -> Result<Self, ServiceError> {
let config = config.to_service_config();
let (task_manager, rpc_handlers) = (builder)(config)?;
Ok(Self::new(task_manager, rpc_handlers))
} }
} }
@@ -188,113 +168,140 @@ impl From<SubxtClient> for jsonrpsee::Client {
} }
} }
fn start_subxt_client<C: ChainSpec + 'static, S: AbstractService>( /// Role of the node.
config: SubxtClientConfig<C, S>, #[derive(Clone, Copy, Debug)]
from_front: mpsc::Receiver<String>, pub enum Role {
to_front: mpsc::Sender<String>, /// Light client.
) -> Result<(), ServiceError> { Light,
let mut network = NetworkConfiguration::new( /// A full node (maninly used for testing purposes).
format!("{} (subxt client)", config.chain_spec.name()), Authority(sp_keyring::AccountKeyring),
"unknown", }
Default::default(),
None,
);
network.boot_nodes = config.chain_spec.boot_nodes().to_vec();
network.transport = TransportConfig::Normal {
enable_mdns: true,
allow_private_ipv4: true,
wasm_external_transport: None,
use_yamux_flow_control: true,
};
let service_config = Configuration {
network,
impl_name: config.impl_name,
impl_version: config.impl_version,
chain_spec: Box::new(config.chain_spec),
role: config.role.into(),
task_executor: (move |fut, ty| {
match ty {
TaskType::Async => task::spawn(fut),
TaskType::Blocking => task::spawn_blocking(|| task::block_on(fut)),
};
})
.into(),
database: config.db,
keystore: config.keystore,
max_runtime_instances: 8,
announce_block: true,
dev_key_seed: config.role.into(),
telemetry_endpoints: Default::default(), impl From<Role> for sc_service::Role {
telemetry_external_transport: Default::default(), fn from(role: Role) -> Self {
default_heap_pages: Default::default(), match role {
disable_grandpa: Default::default(), Role::Light => Self::Light,
execution_strategies: Default::default(), Role::Authority(_) => {
force_authoring: Default::default(), Self::Authority {
offchain_worker: Default::default(), sentry_nodes: Default::default(),
prometheus_config: Default::default(),
pruning: Default::default(),
rpc_cors: Default::default(),
rpc_http: Default::default(),
rpc_ipc: Default::default(),
rpc_ws: Default::default(),
rpc_ws_max_connections: Default::default(),
rpc_methods: Default::default(),
state_cache_child_ratio: Default::default(),
state_cache_size: Default::default(),
tracing_receiver: Default::default(),
tracing_targets: Default::default(),
transaction_pool: Default::default(),
wasm_method: Default::default(),
base_path: Default::default(),
informant_output_format: Default::default(),
};
log::info!("{}", service_config.impl_name);
log::info!("✌️ version {}", service_config.impl_version);
log::info!("❤️ by {}, {}", config.author, config.copyright_start_year);
log::info!(
"📋 Chain specification: {}",
service_config.chain_spec.name()
);
log::info!("🏷 Node name: {}", service_config.network.node_name);
log::info!("👤 Role: {:?}", service_config.role);
// Create the service. This is the most heavy initialization step.
let mut service = (config.builder)(service_config)?;
// Spawn background task.
let session = RpcSession::new(to_front.clone());
let mut from_front = from_front.compat();
task::spawn(poll_fn(move |cx| {
loop {
match Pin::new(&mut from_front).poll_next(cx) {
Poll::Ready(Some(message)) => {
let mut to_front = to_front.clone().sink_compat();
let message = message
.expect("v1 streams require an error type; Stream of String can't fail; qed");
let fut = service.rpc_query(&session, &message);
task::spawn(async move {
if let Some(response) = fut.await {
to_front.send(response).await.ok();
}
});
} }
Poll::Pending => break,
Poll::Ready(None) => return Poll::Ready(()),
} }
} }
}
}
loop { impl From<Role> for Option<String> {
match Pin::new(&mut service).poll(cx) { fn from(role: Role) -> Self {
Poll::Ready(Ok(())) => return Poll::Ready(()), match role {
Poll::Pending => return Poll::Pending, Role::Light => None,
Poll::Ready(Err(e)) => log::error!("{}", e), Role::Authority(key) => Some(key.to_seed()),
}
} }
})); }
}
Ok(()) /// Client configuration.
#[derive(Clone)]
pub struct SubxtClientConfig<C: ChainSpec + 'static> {
/// Name of the implementation.
pub impl_name: &'static str,
/// Version of the implementation.
pub impl_version: &'static str,
/// Author of the implementation.
pub author: &'static str,
/// Copyright start year.
pub copyright_start_year: i32,
/// Database configuration.
pub db: DatabaseConfig,
/// Keystore configuration.
pub keystore: KeystoreConfig,
/// Chain specification.
pub chain_spec: C,
/// Role of the node.
pub role: Role,
/// Enable telemetry.
pub enable_telemetry: bool,
}
impl<C: ChainSpec + 'static> SubxtClientConfig<C> {
/// Creates a service configuration.
pub fn to_service_config(self) -> Configuration {
let mut network = NetworkConfiguration::new(
format!("{} (subxt client)", self.chain_spec.name()),
"unknown",
Default::default(),
None,
);
network.boot_nodes = self.chain_spec.boot_nodes().to_vec();
network.transport = TransportConfig::Normal {
enable_mdns: true,
allow_private_ipv4: true,
wasm_external_transport: None,
use_yamux_flow_control: true,
};
let telemetry_endpoints = if self.enable_telemetry {
let endpoints =
TelemetryEndpoints::new(vec![("/ip4/127.0.0.1/tcp/99000/ws".into(), 0)])
.expect("valid config; qed");
Some(endpoints)
} else {
None
};
let service_config = Configuration {
network,
impl_name: self.impl_name.to_string(),
impl_version: self.impl_version.to_string(),
chain_spec: Box::new(self.chain_spec),
role: self.role.into(),
task_executor: (move |fut, ty| {
match ty {
TaskType::Async => task::spawn(fut),
TaskType::Blocking => task::spawn_blocking(|| task::block_on(fut)),
}
})
.into(),
database: self.db,
keystore: self.keystore,
max_runtime_instances: 8,
announce_block: true,
dev_key_seed: self.role.into(),
telemetry_endpoints,
telemetry_external_transport: Default::default(),
default_heap_pages: Default::default(),
disable_grandpa: Default::default(),
execution_strategies: Default::default(),
force_authoring: Default::default(),
offchain_worker: Default::default(),
prometheus_config: Default::default(),
pruning: Default::default(),
rpc_cors: Default::default(),
rpc_http: Default::default(),
rpc_ipc: Default::default(),
rpc_ws: Default::default(),
rpc_ws_max_connections: Default::default(),
rpc_methods: Default::default(),
state_cache_child_ratio: Default::default(),
state_cache_size: Default::default(),
tracing_receiver: Default::default(),
tracing_targets: Default::default(),
transaction_pool: Default::default(),
wasm_method: Default::default(),
base_path: Default::default(),
informant_output_format: Default::default(),
};
log::info!("{}", service_config.impl_name);
log::info!("✌️ version {}", service_config.impl_version);
log::info!("❤️ by {}, {}", self.author, self.copyright_start_year);
log::info!(
"📋 Chain specification: {}",
service_config.chain_spec.name()
);
log::info!("🏷 Node name: {}", service_config.network.node_name);
log::info!("👤 Role: {:?}", self.role);
service_config
}
} }
#[cfg(test)] #[cfg(test)]
@@ -347,12 +354,14 @@ mod tests {
cache_size: 64, cache_size: 64,
}, },
keystore: KeystoreConfig::InMemory, keystore: KeystoreConfig::InMemory,
builder: test_node::service::new_light,
chain_spec, chain_spec,
role: Role::Light, role: Role::Light,
enable_telemetry: false,
}; };
let client = ClientBuilder::<NodeTemplateRuntime>::new() let client = ClientBuilder::<NodeTemplateRuntime>::new()
.set_client(SubxtClient::new(config).unwrap()) .set_client(
SubxtClient::from_config(config, test_node::service::new_light).unwrap(),
)
.build() .build()
.await .await
.unwrap(); .unwrap();
@@ -378,12 +387,14 @@ mod tests {
cache_size: 128, cache_size: 128,
}, },
keystore: KeystoreConfig::InMemory, keystore: KeystoreConfig::InMemory,
builder: test_node::service::new_full, chain_spec: test_node::chain_spec::development_config().unwrap(),
chain_spec: test_node::chain_spec::development_config(),
role: Role::Authority(AccountKeyring::Alice), role: Role::Authority(AccountKeyring::Alice),
enable_telemetry: false,
}; };
let client = ClientBuilder::<NodeTemplateRuntime>::new() let client = ClientBuilder::<NodeTemplateRuntime>::new()
.set_client(SubxtClient::new(config).unwrap()) .set_client(
SubxtClient::from_config(config, test_node::service::new_full).unwrap(),
)
.build() .build()
.await .await
.unwrap(); .unwrap();
+33
View File
@@ -0,0 +1,33 @@
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// This file is part of substrate-subxt.
//
// subxt is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// subxt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
use substrate_subxt::{
system::AccountStoreExt,
ClientBuilder,
DefaultNodeRuntime,
};
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let client = ClientBuilder::<DefaultNodeRuntime>::new().build().await?;
let mut iter = client.account_iter(None).await?;
while let Some((key, account)) = iter.next().await? {
println!("{:?}: {}", key, account.data.free);
}
Ok(())
}
+8 -8
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "substrate-subxt-proc-macro" name = "substrate-subxt-proc-macro"
version = "0.9.0" version = "0.11.0"
authors = ["David Craven <david@craven.ch>", "Parity Technologies <admin@parity.io>"] authors = ["David Craven <david@craven.ch>", "Parity Technologies <admin@parity.io>"]
edition = "2018" edition = "2018"
autotests = false autotests = false
@@ -16,19 +16,19 @@ proc-macro = true
[dependencies] [dependencies]
heck = "0.3.1" heck = "0.3.1"
proc-macro2 = "1.0.18" proc-macro2 = "1.0.19"
proc-macro-crate = "0.1.4" proc-macro-crate = "0.1.5"
proc-macro-error = "1.0.2" proc-macro-error = "1.0.3"
quote = "1.0.7" quote = "1.0.7"
syn = "1.0.33" syn = "1.0.35"
synstructure = "0.12.4" synstructure = "0.12.4"
[dev-dependencies] [dev-dependencies]
async-std = { version = "=1.5.0", features = ["attributes"] } async-std = { version = "1.6.2", features = ["attributes"] }
codec = { package = "parity-scale-codec", version = "1.3.1", features = ["derive"] } codec = { package = "parity-scale-codec", version = "1.3.4", features = ["derive"] }
env_logger = "0.7.1" env_logger = "0.7.1"
pretty_assertions = "0.6.1" pretty_assertions = "0.6.1"
sp-keyring = "2.0.0-rc4" sp-keyring = "2.0.0-rc5"
substrate-subxt = { path = ".." } substrate-subxt = { path = ".." }
trybuild = "1.0.30" trybuild = "1.0.30"
+53 -6
View File
@@ -73,6 +73,7 @@ pub fn store(s: Structure) -> TokenStream {
let module = utils::module_name(generics); let module = utils::module_name(generics);
let store_name = utils::ident_to_name(ident, "Store").to_camel_case(); let store_name = utils::ident_to_name(ident, "Store").to_camel_case();
let store = format_ident!("{}", store_name.to_snake_case()); let store = format_ident!("{}", store_name.to_snake_case());
let store_iter = format_ident!("{}_iter", store_name.to_snake_case());
let store_trait = format_ident!("{}StoreExt", store_name); let store_trait = format_ident!("{}StoreExt", store_name);
let bindings = utils::bindings(&s); let bindings = utils::bindings(&s);
let fields = utils::fields(&bindings); let fields = utils::fields(&bindings);
@@ -110,12 +111,23 @@ pub fn store(s: Structure) -> TokenStream {
let keys = filtered_fields let keys = filtered_fields
.iter() .iter()
.map(|(field, _)| quote!(&self.#field)); .map(|(field, _)| quote!(&self.#field));
let key_iter = quote!(#subxt::KeyIter<T, #ident<#(#params),*>>);
quote! { quote! {
impl#generics #subxt::Store<T> for #ident<#(#params),*> { impl#generics #subxt::Store<T> for #ident<#(#params),*> {
const MODULE: &'static str = MODULE; const MODULE: &'static str = MODULE;
const FIELD: &'static str = #store_name; const FIELD: &'static str = #store_name;
type Returns = #store_ret; type Returns = #store_ret;
fn prefix(
metadata: &#subxt::Metadata,
) -> Result<#subxt::sp_core::storage::StorageKey, #subxt::MetadataError> {
Ok(metadata
.module(Self::MODULE)?
.storage(Self::FIELD)?
.prefix())
}
fn key( fn key(
&self, &self,
metadata: &#subxt::Metadata, metadata: &#subxt::Metadata,
@@ -129,13 +141,19 @@ pub fn store(s: Structure) -> TokenStream {
} }
/// Store extension trait. /// Store extension trait.
pub trait #store_trait<T: #module> { pub trait #store_trait<T: #subxt::Runtime + #module> {
/// Retrive the store element. /// Retrieve the store element.
fn #store<'a>( fn #store<'a>(
&'a self, &'a self,
#args #args
hash: Option<T::Hash>, hash: Option<T::Hash>,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#ret, #subxt::Error>> + Send + 'a>>; ) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#ret, #subxt::Error>> + Send + 'a>>;
/// Iterate over the store element.
fn #store_iter<'a>(
&'a self,
hash: Option<T::Hash>,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#key_iter, #subxt::Error>> + Send + 'a>>;
} }
impl<T: #subxt::Runtime + #module> #store_trait<T> for #subxt::Client<T> { impl<T: #subxt::Runtime + #module> #store_trait<T> for #subxt::Client<T> {
@@ -145,7 +163,14 @@ pub fn store(s: Structure) -> TokenStream {
hash: Option<T::Hash>, hash: Option<T::Hash>,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#ret, #subxt::Error>> + Send + 'a>> { ) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#ret, #subxt::Error>> + Send + 'a>> {
let #marker = core::marker::PhantomData::<T>; let #marker = core::marker::PhantomData::<T>;
Box::pin(self.#fetch(#build_struct, hash)) Box::pin(async move { self.#fetch(&#build_struct, hash).await })
}
fn #store_iter<'a>(
&'a self,
hash: Option<T::Hash>,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#key_iter, #subxt::Error>> + Send + 'a>> {
Box::pin(self.iter(hash))
} }
} }
} }
@@ -169,6 +194,16 @@ mod tests {
const MODULE: &'static str = MODULE; const MODULE: &'static str = MODULE;
const FIELD: &'static str = "Account"; const FIELD: &'static str = "Account";
type Returns = AccountData<T::Balance>; type Returns = AccountData<T::Balance>;
fn prefix(
metadata: &substrate_subxt::Metadata,
) -> Result<substrate_subxt::sp_core::storage::StorageKey, substrate_subxt::MetadataError> {
Ok(metadata
.module(Self::MODULE)?
.storage(Self::FIELD)?
.prefix())
}
fn key( fn key(
&self, &self,
metadata: &substrate_subxt::Metadata, metadata: &substrate_subxt::Metadata,
@@ -182,13 +217,18 @@ mod tests {
} }
/// Store extension trait. /// Store extension trait.
pub trait AccountStoreExt<T: Balances> { pub trait AccountStoreExt<T: substrate_subxt::Runtime + Balances> {
/// Retrive the store element. /// Retrieve the store element.
fn account<'a>( fn account<'a>(
&'a self, &'a self,
account_id: &'a <T as System>::AccountId, account_id: &'a <T as System>::AccountId,
hash: Option<T::Hash>, hash: Option<T::Hash>,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<AccountData<T::Balance>, substrate_subxt::Error>> + Send + 'a>>; ) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<AccountData<T::Balance>, substrate_subxt::Error>> + Send + 'a>>;
/// Iterate over the store element.
fn account_iter<'a>(
&'a self,
hash: Option<T::Hash>,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<substrate_subxt::KeyIter<T, AccountStore<'a, T>>, substrate_subxt::Error>> + Send + 'a>>;
} }
impl<T: substrate_subxt::Runtime + Balances> AccountStoreExt<T> for substrate_subxt::Client<T> { impl<T: substrate_subxt::Runtime + Balances> AccountStoreExt<T> for substrate_subxt::Client<T> {
@@ -199,7 +239,14 @@ mod tests {
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<AccountData<T::Balance>, substrate_subxt::Error>> + Send + 'a>> ) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<AccountData<T::Balance>, substrate_subxt::Error>> + Send + 'a>>
{ {
let _ = core::marker::PhantomData::<T>; let _ = core::marker::PhantomData::<T>;
Box::pin(self.fetch_or_default(AccountStore { account_id, }, hash)) Box::pin(async move { self.fetch_or_default(&AccountStore { account_id, }, hash).await })
}
fn account_iter<'a>(
&'a self,
hash: Option<T::Hash>,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<substrate_subxt::KeyIter<T, AccountStore<'a, T>>, substrate_subxt::Error>> + Send + 'a>> {
Box::pin(self.iter(hash))
} }
} }
}; };
+2 -2
View File
@@ -89,8 +89,8 @@ subxt_test!({
account: Alice, account: Alice,
step: { step: {
state: { state: {
alice: AccountStore { account_id: &alice }, alice: &AccountStore { account_id: &alice },
bob: AccountStore { account_id: &bob }, bob: &AccountStore { account_id: &bob },
}, },
call: TransferCall { call: TransferCall {
to: &bob, to: &bob,
+1 -1
View File
@@ -133,7 +133,7 @@ mod tests {
use sp_keyring::AccountKeyring; use sp_keyring::AccountKeyring;
#[async_std::test] #[async_std::test]
async fn test_transfer() { async fn test_basic_transfer() {
env_logger::try_init().ok(); env_logger::try_init().ok();
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair()); let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
let bob = PairSigner::<TestRuntime, _>::new(AccountKeyring::Bob.pair()); let bob = PairSigner::<TestRuntime, _>::new(AccountKeyring::Bob.pair());
+2
View File
@@ -44,6 +44,8 @@ pub trait Store<T>: Encode {
const FIELD: &'static str; const FIELD: &'static str;
/// Return type. /// Return type.
type Returns: Decode; type Returns: Decode;
/// Returns the key prefix for storage maps
fn prefix(metadata: &Metadata) -> Result<StorageKey, MetadataError>;
/// Returns the `StorageKey`. /// Returns the `StorageKey`.
fn key(&self, metadata: &Metadata) -> Result<StorageKey, MetadataError>; fn key(&self, metadata: &Metadata) -> Result<StorageKey, MetadataError>;
/// Returns the default value. /// Returns the default value.
+138 -17
View File
@@ -55,6 +55,7 @@ use sc_rpc_api::state::ReadProof;
use sp_core::{ use sp_core::{
storage::{ storage::{
StorageChangeSet, StorageChangeSet,
StorageData,
StorageKey, StorageKey,
}, },
Bytes, Bytes,
@@ -115,6 +116,7 @@ pub struct ClientBuilder<T: Runtime> {
_marker: std::marker::PhantomData<T>, _marker: std::marker::PhantomData<T>,
url: Option<String>, url: Option<String>,
client: Option<jsonrpsee::Client>, client: Option<jsonrpsee::Client>,
page_size: Option<u32>,
} }
impl<T: Runtime> ClientBuilder<T> { impl<T: Runtime> ClientBuilder<T> {
@@ -124,6 +126,7 @@ impl<T: Runtime> ClientBuilder<T> {
_marker: std::marker::PhantomData, _marker: std::marker::PhantomData,
url: None, url: None,
client: None, client: None,
page_size: None,
} }
} }
@@ -139,6 +142,12 @@ impl<T: Runtime> ClientBuilder<T> {
self self
} }
/// Set the page size.
pub fn set_page_size(mut self, size: u32) -> Self {
self.page_size = Some(size);
self
}
/// Creates a new Client. /// Creates a new Client.
pub async fn build(self) -> Result<Client<T>, Error> { pub async fn build(self) -> Result<Client<T>, Error> {
let client = if let Some(client) = self.client { let client = if let Some(client) = self.client {
@@ -164,6 +173,7 @@ impl<T: Runtime> ClientBuilder<T> {
metadata: metadata?, metadata: metadata?,
runtime_version: runtime_version?, runtime_version: runtime_version?,
_marker: PhantomData, _marker: PhantomData,
page_size: self.page_size.unwrap_or(10),
}) })
} }
} }
@@ -175,6 +185,7 @@ pub struct Client<T: Runtime> {
metadata: Metadata, metadata: Metadata,
runtime_version: RuntimeVersion, runtime_version: RuntimeVersion,
_marker: PhantomData<(fn() -> T::Signature, T::Extra)>, _marker: PhantomData<(fn() -> T::Signature, T::Extra)>,
page_size: u32,
} }
impl<T: Runtime> Clone for Client<T> { impl<T: Runtime> Clone for Client<T> {
@@ -185,6 +196,53 @@ impl<T: Runtime> Clone for Client<T> {
metadata: self.metadata.clone(), metadata: self.metadata.clone(),
runtime_version: self.runtime_version.clone(), runtime_version: self.runtime_version.clone(),
_marker: PhantomData, _marker: PhantomData,
page_size: self.page_size,
}
}
}
/// Iterates over key value pairs in a map.
pub struct KeyIter<T: Runtime, F: Store<T>> {
client: Client<T>,
_marker: PhantomData<F>,
count: u32,
hash: T::Hash,
start_key: Option<StorageKey>,
buffer: Vec<(StorageKey, StorageData)>,
}
impl<T: Runtime, F: Store<T>> KeyIter<T, F> {
/// Returns the next key value pair from a map.
pub async fn next(&mut self) -> Result<Option<(StorageKey, F::Returns)>, Error> {
loop {
if let Some((k, v)) = self.buffer.pop() {
return Ok(Some((k, Decode::decode(&mut &v.0[..])?)))
} else {
let keys = self
.client
.fetch_keys::<F>(self.count, self.start_key.take(), Some(self.hash))
.await?;
if keys.is_empty() {
return Ok(None)
}
self.start_key = keys.last().cloned();
let change_sets = self
.client
.rpc
.query_storage_at(&keys, Some(self.hash))
.await?;
for change_set in change_sets {
for (k, v) in change_set.changes {
if let Some(v) = v {
self.buffer.push((k, v));
}
}
}
debug_assert_eq!(self.buffer.len(), self.count as usize);
}
} }
} }
} }
@@ -200,32 +258,70 @@ impl<T: Runtime> Client<T> {
&self.metadata &self.metadata
} }
/// Fetch a StorageKey with default value. /// Fetch a StorageKey with an optional block hash.
pub async fn fetch<F: Store<T>>(
&self,
store: &F,
hash: Option<T::Hash>,
) -> Result<Option<F::Returns>, Error> {
let key = store.key(&self.metadata)?;
if let Some(data) = self.rpc.storage(&key, hash).await? {
Ok(Some(Decode::decode(&mut &data.0[..])?))
} else {
Ok(None)
}
}
/// Fetch a StorageKey that has a default value with an optional block hash.
pub async fn fetch_or_default<F: Store<T>>( pub async fn fetch_or_default<F: Store<T>>(
&self, &self,
store: F, store: &F,
hash: Option<T::Hash>, hash: Option<T::Hash>,
) -> Result<F::Returns, Error> { ) -> Result<F::Returns, Error> {
let key = store.key(&self.metadata)?; if let Some(data) = self.fetch(store, hash).await? {
if let Some(data) = self.rpc.storage(key, hash).await? { Ok(data)
Ok(Decode::decode(&mut &data.0[..])?)
} else { } else {
Ok(store.default(&self.metadata)?) Ok(store.default(&self.metadata)?)
} }
} }
/// Fetch a StorageKey an optional storage key. /// Returns an iterator of key value pairs.
pub async fn fetch<F: Store<T>>( pub async fn iter<F: Store<T>>(
&self, &self,
store: F,
hash: Option<T::Hash>, hash: Option<T::Hash>,
) -> Result<Option<F::Returns>, Error> { ) -> Result<KeyIter<T, F>, Error> {
let key = store.key(&self.metadata)?; let hash = if let Some(hash) = hash {
if let Some(data) = self.rpc.storage(key, hash).await? { hash
Ok(Some(Decode::decode(&mut &data.0[..])?))
} else { } else {
Ok(None) self.block_hash(None)
} .await?
.expect("didn't pass a block number; qed")
};
Ok(KeyIter {
client: self.clone(),
hash,
count: self.page_size,
start_key: None,
buffer: Default::default(),
_marker: PhantomData,
})
}
/// Fetch up to `count` keys for a storage map in lexicographic order.
///
/// Supports pagination by passing a value to `start_key`.
pub async fn fetch_keys<F: Store<T>>(
&self,
count: u32,
start_key: Option<StorageKey>,
hash: Option<T::Hash>,
) -> Result<Vec<StorageKey>, Error> {
let prefix = <F as Store<T>>::prefix(&self.metadata)?;
let keys = self
.rpc
.storage_keys_paged(Some(prefix), count, start_key, hash)
.await?;
Ok(keys)
} }
/// Query historical storage entries /// Query historical storage entries
@@ -490,12 +586,16 @@ mod tests {
path: tmp.path().join("keystore"), path: tmp.path().join("keystore"),
password: None, password: None,
}, },
builder: test_node::service::new_full, chain_spec: test_node::chain_spec::development_config().unwrap(),
chain_spec: test_node::chain_spec::development_config(),
role: Role::Authority(key), role: Role::Authority(key),
enable_telemetry: false,
}; };
let client = ClientBuilder::new() let client = ClientBuilder::new()
.set_client(SubxtClient::new(config).expect("Error creating subxt client")) .set_client(
SubxtClient::from_config(config, test_node::service::new_full)
.expect("Error creating subxt client"),
)
.set_page_size(2)
.build() .build()
.await .await
.expect("Error creating client"); .expect("Error creating client");
@@ -609,4 +709,25 @@ mod tests {
let mut blocks = client.subscribe_finalized_blocks().await.unwrap(); let mut blocks = client.subscribe_finalized_blocks().await.unwrap();
blocks.next().await; blocks.next().await;
} }
#[async_std::test]
async fn test_fetch_keys() {
let (client, _) = test_client().await;
let keys = client
.fetch_keys::<system::AccountStore<_>>(4, None, None)
.await
.unwrap();
assert_eq!(keys.len(), 4)
}
#[async_std::test]
async fn test_iter() {
let (client, _) = test_client().await;
let mut iter = client.iter::<system::AccountStore<_>>(None).await.unwrap();
let mut i = 0;
while let Some(_) = iter.next().await.unwrap() {
i += 1;
}
assert_eq!(i, 4);
}
} }
+5 -5
View File
@@ -249,10 +249,10 @@ pub struct StorageMetadata {
} }
impl StorageMetadata { impl StorageMetadata {
pub fn prefix(&self) -> Vec<u8> { pub fn prefix(&self) -> StorageKey {
let mut bytes = sp_core::twox_128(self.module_prefix.as_bytes()).to_vec(); let mut bytes = sp_core::twox_128(self.module_prefix.as_bytes()).to_vec();
bytes.extend(&sp_core::twox_128(self.storage_prefix.as_bytes())[..]); bytes.extend(&sp_core::twox_128(self.storage_prefix.as_bytes())[..]);
bytes StorageKey(bytes)
} }
pub fn default<V: Decode>(&self) -> Result<V, MetadataError> { pub fn default<V: Decode>(&self) -> Result<V, MetadataError> {
@@ -286,7 +286,7 @@ impl StorageMetadata {
match &self.ty { match &self.ty {
StorageEntryType::Plain(_) => { StorageEntryType::Plain(_) => {
Ok(StoragePlain { Ok(StoragePlain {
prefix: self.prefix(), prefix: self.prefix().0,
}) })
} }
_ => Err(MetadataError::StorageTypeError), _ => Err(MetadataError::StorageTypeError),
@@ -298,7 +298,7 @@ impl StorageMetadata {
StorageEntryType::Map { hasher, .. } => { StorageEntryType::Map { hasher, .. } => {
Ok(StorageMap { Ok(StorageMap {
_marker: PhantomData, _marker: PhantomData,
prefix: self.prefix(), prefix: self.prefix().0,
hasher: hasher.clone(), hasher: hasher.clone(),
}) })
} }
@@ -317,7 +317,7 @@ impl StorageMetadata {
} => { } => {
Ok(StorageDoubleMap { Ok(StorageDoubleMap {
_marker: PhantomData, _marker: PhantomData,
prefix: self.prefix(), prefix: self.prefix().0,
hasher1: hasher.clone(), hasher1: hasher.clone(),
hasher2: key2_hasher.clone(), hasher2: key2_hasher.clone(),
}) })
+35 -1
View File
@@ -122,7 +122,7 @@ impl<T: Runtime> Rpc<T> {
/// Fetch a storage key /// Fetch a storage key
pub async fn storage( pub async fn storage(
&self, &self,
key: StorageKey, key: &StorageKey,
hash: Option<T::Hash>, hash: Option<T::Hash>,
) -> Result<Option<StorageData>, Error> { ) -> Result<Option<StorageData>, Error> {
let params = Params::Array(vec![to_json_value(key)?, to_json_value(hash)?]); let params = Params::Array(vec![to_json_value(key)?, to_json_value(hash)?]);
@@ -131,6 +131,27 @@ impl<T: Runtime> Rpc<T> {
Ok(data) Ok(data)
} }
/// Returns the keys with prefix with pagination support.
/// Up to `count` keys will be returned.
/// If `start_key` is passed, return next keys in storage in lexicographic order.
pub async fn storage_keys_paged(
&self,
prefix: Option<StorageKey>,
count: u32,
start_key: Option<StorageKey>,
hash: Option<T::Hash>,
) -> Result<Vec<StorageKey>, Error> {
let params = Params::Array(vec![
to_json_value(prefix)?,
to_json_value(count)?,
to_json_value(start_key)?,
to_json_value(hash)?,
]);
let data = self.client.request("state_getKeysPaged", params).await?;
log::debug!("state_getKeysPaged {:?}", data);
Ok(data)
}
/// Query historical storage entries /// Query historical storage entries
pub async fn query_storage( pub async fn query_storage(
&self, &self,
@@ -149,6 +170,19 @@ impl<T: Runtime> Rpc<T> {
.map_err(Into::into) .map_err(Into::into)
} }
/// Query historical storage entries
pub async fn query_storage_at(
&self,
keys: &[StorageKey],
at: Option<T::Hash>,
) -> Result<Vec<StorageChangeSet<<T as System>::Hash>>, Error> {
let params = Params::Array(vec![to_json_value(keys)?, to_json_value(at)?]);
self.client
.request("state_queryStorage", params)
.await
.map_err(Into::into)
}
/// Fetch the genesis hash /// Fetch the genesis hash
pub async fn genesis_hash(&self) -> Result<T::Hash, Error> { pub async fn genesis_hash(&self) -> Result<T::Hash, Error> {
let block_zero = Some(ListOrValue::Value(NumberOrHex::Number(0))); let block_zero = Some(ListOrValue::Value(NumberOrHex::Number(0)));
+21 -21
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "test-node" name = "test-node"
version = "2.0.0-rc4" version = "2.0.0-rc5"
authors = ["Anonymous"] authors = ["Anonymous"]
description = "Substrate Node template" description = "Substrate Node template"
edition = "2018" edition = "2018"
@@ -14,29 +14,29 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies] [dependencies]
futures = "0.3.5" futures = "0.3.5"
log = "0.4.8" log = "0.4.11"
structopt = "0.3.15" structopt = "0.3.15"
parking_lot = "0.11.0" parking_lot = "0.11.0"
sc-cli = { version = "0.8.0-rc4", features = ["wasmtime"] } sc-cli = { version = "0.8.0-rc5", features = ["wasmtime"] }
sp-core = "2.0.0-rc4" sp-core = "2.0.0-rc5"
sc-executor = { version = "0.8.0-rc4", features = ["wasmtime"] } sc-executor = { version = "0.8.0-rc5", features = ["wasmtime"] }
sc-service = { version = "0.8.0-rc4", features = ["wasmtime"] } sc-service = { version = "0.8.0-rc5", features = ["wasmtime"] }
sp-inherents = "2.0.0-rc4" sp-inherents = "2.0.0-rc5"
sc-transaction-pool = "2.0.0-rc4" sc-transaction-pool = "2.0.0-rc5"
sp-transaction-pool = "2.0.0-rc4" sp-transaction-pool = "2.0.0-rc5"
sc-network = "0.8.0-rc4" sc-network = "0.8.0-rc5"
sc-consensus-aura = "0.8.0-rc4" sc-consensus-aura = "0.8.0-rc5"
sp-consensus-aura = "0.8.0-rc4" sp-consensus-aura = "0.8.0-rc5"
sp-consensus = "0.8.0-rc4" sp-consensus = "0.8.0-rc5"
sc-consensus = "0.8.0-rc4" sc-consensus = "0.8.0-rc5"
sc-finality-grandpa = "0.8.0-rc4" sc-finality-grandpa = "0.8.0-rc5"
sp-finality-grandpa = "2.0.0-rc4" sp-finality-grandpa = "2.0.0-rc5"
sc-client-api = "2.0.0-rc4" sc-client-api = "2.0.0-rc5"
sp-runtime = "2.0.0-rc4" sp-runtime = "2.0.0-rc5"
sc-basic-authorship = "0.8.0-rc4" sc-basic-authorship = "0.8.0-rc5"
test-node-runtime = { version = "2.0.0-rc4", path = "runtime" } test-node-runtime = { version = "2.0.0-rc5", path = "runtime" }
[build-dependencies] [build-dependencies]
substrate-build-script-utils = "2.0.0-rc4" substrate-build-script-utils = "2.0.0-rc5"
+24 -24
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "test-node-runtime" name = "test-node-runtime"
version = "2.0.0-rc4" version = "2.0.0-rc5"
authors = ["Anonymous"] authors = ["Anonymous"]
edition = "2018" edition = "2018"
license = "Unlicense" license = "Unlicense"
@@ -11,31 +11,31 @@ repository = "https://github.com/paritytech/substrate/"
targets = ["x86_64-unknown-linux-gnu"] targets = ["x86_64-unknown-linux-gnu"]
[dependencies] [dependencies]
codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false, features = ["derive"] } codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] }
aura = { version = "2.0.0-rc4", default-features = false, package = "pallet-aura" } aura = { version = "2.0.0-rc5", default-features = false, package = "pallet-aura" }
balances = { version = "2.0.0-rc4", default-features = false, package = "pallet-balances" } balances = { version = "2.0.0-rc5", default-features = false, package = "pallet-balances" }
frame-support = { version = "2.0.0-rc4", default-features = false } frame-support = { version = "2.0.0-rc5", default-features = false }
grandpa = { version = "2.0.0-rc4", default-features = false, package = "pallet-grandpa" } grandpa = { version = "2.0.0-rc5", default-features = false, package = "pallet-grandpa" }
randomness-collective-flip = { version = "2.0.0-rc4", default-features = false, package = "pallet-randomness-collective-flip" } randomness-collective-flip = { version = "2.0.0-rc5", default-features = false, package = "pallet-randomness-collective-flip" }
sudo = { version = "2.0.0-rc4", default-features = false, package = "pallet-sudo" } sudo = { version = "2.0.0-rc5", default-features = false, package = "pallet-sudo" }
system = { version = "2.0.0-rc4", default-features = false, package = "frame-system" } system = { version = "2.0.0-rc5", default-features = false, package = "frame-system" }
timestamp = { version = "2.0.0-rc4", default-features = false, package = "pallet-timestamp" } timestamp = { version = "2.0.0-rc5", default-features = false, package = "pallet-timestamp" }
transaction-payment = { version = "2.0.0-rc4", default-features = false, package = "pallet-transaction-payment" } transaction-payment = { version = "2.0.0-rc5", default-features = false, package = "pallet-transaction-payment" }
frame-executive = { version = "2.0.0-rc4", default-features = false } frame-executive = { version = "2.0.0-rc5", default-features = false }
serde = { version = "1.0.114", optional = true, features = ["derive"] } serde = { version = "1.0.114", optional = true, features = ["derive"] }
sp-api = { version = "2.0.0-rc4", default-features = false } sp-api = { version = "2.0.0-rc5", default-features = false }
sp-block-builder = { default-features = false, version = "2.0.0-rc4" } sp-block-builder = { version = "2.0.0-rc5", default-features = false }
sp-consensus-aura = { version = "0.8.0-rc4", default-features = false } sp-consensus-aura = { version = "0.8.0-rc5", default-features = false }
sp-core = { version = "2.0.0-rc4", default-features = false } sp-core = { version = "2.0.0-rc5", default-features = false }
sp-inherents = { default-features = false, version = "2.0.0-rc4" } sp-inherents = { version = "2.0.0-rc5", default-features = false }
sp-io = { version = "2.0.0-rc4", default-features = false } sp-io = { version = "2.0.0-rc5", default-features = false }
sp-offchain = { version = "2.0.0-rc4", default-features = false } sp-offchain = { version = "2.0.0-rc5", default-features = false }
sp-runtime = { version = "2.0.0-rc4", default-features = false } sp-runtime = { version = "2.0.0-rc5", default-features = false }
sp-session = { version = "2.0.0-rc4", default-features = false } sp-session = { version = "2.0.0-rc5", default-features = false }
sp-std = { version = "2.0.0-rc4", default-features = false } sp-std = { version = "2.0.0-rc5", default-features = false }
sp-transaction-pool = { version = "2.0.0-rc4", default-features = false } sp-transaction-pool = { version = "2.0.0-rc5", default-features = false }
sp-version = { version = "2.0.0-rc4", default-features = false } sp-version = { version = "2.0.0-rc5", default-features = false }
[build-dependencies] [build-dependencies]
wasm-builder-runner = { version = "1.0.6", package = "substrate-wasm-builder-runner" } wasm-builder-runner = { version = "1.0.6", package = "substrate-wasm-builder-runner" }
+6 -2
View File
@@ -235,6 +235,8 @@ impl system::Trait for Runtime {
type OnKilledAccount = (); type OnKilledAccount = ();
/// The data to be stored in an account. /// The data to be stored in an account.
type AccountData = balances::AccountData<Balance>; type AccountData = balances::AccountData<Balance>;
/// Weight information for the extrinsics of this pallet.
type SystemWeightInfo = ();
} }
impl aura::Trait for Runtime { impl aura::Trait for Runtime {
@@ -267,6 +269,7 @@ impl timestamp::Trait for Runtime {
type Moment = u64; type Moment = u64;
type OnTimestampSet = Aura; type OnTimestampSet = Aura;
type MinimumPeriod = MinimumPeriod; type MinimumPeriod = MinimumPeriod;
type WeightInfo = ();
} }
parameter_types! { parameter_types! {
@@ -281,6 +284,7 @@ impl balances::Trait for Runtime {
type DustRemoval = (); type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit; type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System; type AccountStore = System;
type WeightInfo = ();
} }
parameter_types! { parameter_types! {
@@ -309,7 +313,7 @@ construct_runtime!(
System: system::{Module, Call, Config, Storage, Event<T>}, System: system::{Module, Call, Config, Storage, Event<T>},
RandomnessCollectiveFlip: randomness_collective_flip::{Module, Call, Storage}, RandomnessCollectiveFlip: randomness_collective_flip::{Module, Call, Storage},
Timestamp: timestamp::{Module, Call, Storage, Inherent}, Timestamp: timestamp::{Module, Call, Storage, Inherent},
Aura: aura::{Module, Config<T>, Inherent(Timestamp)}, Aura: aura::{Module, Config<T>, Inherent},
Grandpa: grandpa::{Module, Call, Storage, Config, Event}, Grandpa: grandpa::{Module, Call, Storage, Config, Event},
Balances: balances::{Module, Call, Storage, Config<T>, Event<T>}, Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: transaction_payment::{Module, Storage}, TransactionPayment: transaction_payment::{Module, Storage},
@@ -439,7 +443,7 @@ impl_runtime_apis! {
Grandpa::grandpa_authorities() Grandpa::grandpa_authorities()
} }
fn submit_report_equivocation_extrinsic( fn submit_report_equivocation_unsigned_extrinsic(
_equivocation_proof: fg_primitives::EquivocationProof< _equivocation_proof: fg_primitives::EquivocationProof<
<Block as BlockT>::Hash, <Block as BlockT>::Hash,
NumberFor<Block>, NumberFor<Block>,
+47 -14
View File
@@ -38,13 +38,13 @@ use test_node_runtime::{
WASM_BINARY, WASM_BINARY,
}; };
// Note this is the URL for the telemetry server // The URL for the telemetry server.
// const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/"; // const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/";
/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type. /// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.
pub type ChainSpec = sc_service::GenericChainSpec<GenesisConfig>; pub type ChainSpec = sc_service::GenericChainSpec<GenesisConfig>;
/// Helper function to generate a crypto pair from seed /// Generate a crypto pair from seed.
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public { pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{}", seed), None) TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed") .expect("static values are valid; qed")
@@ -53,7 +53,7 @@ pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Pu
type AccountPublic = <Signature as Verify>::Signer; type AccountPublic = <Signature as Verify>::Signer;
/// Helper function to generate an account ID from seed /// Generate an account ID from seed.
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where where
AccountPublic: From<<TPublic::Pair as Pair>::Public>, AccountPublic: From<<TPublic::Pair as Pair>::Public>,
@@ -61,20 +61,28 @@ where
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account() AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
} }
/// Helper function to generate an authority key for Aura /// Generate an Aura authority key.
pub fn authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) { pub fn authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) {
(get_from_seed::<AuraId>(s), get_from_seed::<GrandpaId>(s)) (get_from_seed::<AuraId>(s), get_from_seed::<GrandpaId>(s))
} }
pub fn development_config() -> ChainSpec { pub fn development_config() -> Result<ChainSpec, String> {
ChainSpec::from_genesis( let wasm_binary = WASM_BINARY;
Ok(ChainSpec::from_genesis(
// Name
"Development", "Development",
// ID
"dev", "dev",
ChainType::Development, ChainType::Development,
|| { move || {
testnet_genesis( testnet_genesis(
wasm_binary,
// Initial PoA authorities
vec![authority_keys_from_seed("Alice")], vec![authority_keys_from_seed("Alice")],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"), get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
vec![ vec![
get_account_id_from_seed::<sr25519::Public>("Alice"), get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"), get_account_id_from_seed::<sr25519::Public>("Bob"),
@@ -84,26 +92,39 @@ pub fn development_config() -> ChainSpec {
true, true,
) )
}, },
// Bootnodes
vec![], vec![],
// Telemetry
None, None,
// Protocol ID
None, None,
// Properties
None, None,
// Extensions
None, None,
) ))
} }
pub fn local_testnet_config() -> ChainSpec { pub fn local_testnet_config() -> Result<ChainSpec, String> {
ChainSpec::from_genesis( let wasm_binary = WASM_BINARY;
Ok(ChainSpec::from_genesis(
// Name
"Local Testnet", "Local Testnet",
// ID
"local_testnet", "local_testnet",
ChainType::Local, ChainType::Local,
|| { move || {
testnet_genesis( testnet_genesis(
wasm_binary,
// Initial PoA authorities
vec![ vec![
authority_keys_from_seed("Alice"), authority_keys_from_seed("Alice"),
authority_keys_from_seed("Bob"), authority_keys_from_seed("Bob"),
], ],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"), get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
vec![ vec![
get_account_id_from_seed::<sr25519::Public>("Alice"), get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"), get_account_id_from_seed::<sr25519::Public>("Bob"),
@@ -121,15 +142,22 @@ pub fn local_testnet_config() -> ChainSpec {
true, true,
) )
}, },
// Bootnodes
vec![], vec![],
// Telemetry
None, None,
// Protocol ID
None, None,
// Properties
None, None,
// Extensions
None, None,
) ))
} }
/// Configure initial storage state for FRAME modules.
fn testnet_genesis( fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>, initial_authorities: Vec<(AuraId, GrandpaId)>,
root_key: AccountId, root_key: AccountId,
endowed_accounts: Vec<AccountId>, endowed_accounts: Vec<AccountId>,
@@ -137,10 +165,12 @@ fn testnet_genesis(
) -> GenesisConfig { ) -> GenesisConfig {
GenesisConfig { GenesisConfig {
system: Some(SystemConfig { system: Some(SystemConfig {
code: WASM_BINARY.to_vec(), // Add Wasm runtime to storage.
code: wasm_binary.to_vec(),
changes_trie_config: Default::default(), changes_trie_config: Default::default(),
}), }),
balances: Some(BalancesConfig { balances: Some(BalancesConfig {
// Configure endowed accounts with initial balance of 1 << 60.
balances: endowed_accounts balances: endowed_accounts
.iter() .iter()
.cloned() .cloned()
@@ -156,6 +186,9 @@ fn testnet_genesis(
.map(|x| (x.1.clone(), 1)) .map(|x| (x.1.clone(), 1))
.collect(), .collect(),
}), }),
sudo: Some(SudoConfig { key: root_key }), sudo: Some(SudoConfig {
// Assign network admin rights.
key: root_key,
}),
} }
} }
+44 -23
View File
@@ -18,42 +18,45 @@ use crate::{
chain_spec, chain_spec,
cli::Cli, cli::Cli,
service, service,
service::new_full_params,
}; };
use sc_cli::SubstrateCli; use sc_cli::{
ChainSpec,
Role,
RuntimeVersion,
SubstrateCli,
};
use sc_service::ServiceParams;
impl SubstrateCli for Cli { impl SubstrateCli for Cli {
fn impl_name() -> &'static str { fn impl_name() -> String {
"Substrate Node" "Substrate Node".into()
} }
fn impl_version() -> &'static str { fn impl_version() -> String {
env!("SUBSTRATE_CLI_IMPL_VERSION") env!("SUBSTRATE_CLI_IMPL_VERSION").into()
} }
fn description() -> &'static str { fn description() -> String {
env!("CARGO_PKG_DESCRIPTION") env!("CARGO_PKG_DESCRIPTION").into()
} }
fn author() -> &'static str { fn author() -> String {
env!("CARGO_PKG_AUTHORS") env!("CARGO_PKG_AUTHORS").into()
} }
fn support_url() -> &'static str { fn support_url() -> String {
"support.anonymous.an" "support.anonymous.an".into()
} }
fn copyright_start_year() -> i32 { fn copyright_start_year() -> i32 {
2017 2017
} }
fn executable_name() -> &'static str {
env!("CARGO_PKG_NAME")
}
fn load_spec(&self, id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> { fn load_spec(&self, id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {
Ok(match id { Ok(match id {
"dev" => Box::new(chain_spec::development_config()), "dev" => Box::new(chain_spec::development_config()?),
"" | "local" => Box::new(chain_spec::local_testnet_config()), "" | "local" => Box::new(chain_spec::local_testnet_config()?),
path => { path => {
Box::new(chain_spec::ChainSpec::from_json_file( Box::new(chain_spec::ChainSpec::from_json_file(
std::path::PathBuf::from(path), std::path::PathBuf::from(path),
@@ -61,6 +64,10 @@ impl SubstrateCli for Cli {
} }
}) })
} }
fn native_runtime_version(_: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {
&test_node_runtime::VERSION
}
} }
/// Parse and run command line arguments /// Parse and run command line arguments
@@ -70,15 +77,29 @@ pub fn run() -> sc_cli::Result<()> {
match &cli.subcommand { match &cli.subcommand {
Some(subcommand) => { Some(subcommand) => {
let runner = cli.create_runner(subcommand)?; let runner = cli.create_runner(subcommand)?;
runner.run_subcommand(subcommand, |config| Ok(new_full_start!(config).0)) runner.run_subcommand(subcommand, |config| {
let (
ServiceParams {
client,
backend,
task_manager,
import_queue,
..
},
..,
) = new_full_params(config)?;
Ok((client, backend, import_queue, task_manager))
})
} }
None => { None => {
let runner = cli.create_runner(&cli.run)?; let runner = cli.create_runner(&cli.run)?;
runner.run_node( runner.run_node_until_exit(|config| {
service::new_light, match config.role {
service::new_full, Role::Light => service::new_light(config),
test_node_runtime::VERSION, _ => service::new_full(config),
) }
.map(|service| service.0)
})
} }
} }
} }
+214 -170
View File
@@ -16,8 +16,10 @@
//! Service and ServiceFactory implementation. Specialized wrapper over substrate service. //! Service and ServiceFactory implementation. Specialized wrapper over substrate service.
use sc_client_api::ExecutorProvider; use sc_client_api::{
use sc_consensus::LongestChain; ExecutorProvider,
RemoteBackend,
};
use sc_executor::native_executor_instance; use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor; pub use sc_executor::NativeExecutor;
use sc_finality_grandpa::{ use sc_finality_grandpa::{
@@ -27,9 +29,10 @@ use sc_finality_grandpa::{
}; };
use sc_service::{ use sc_service::{
error::Error as ServiceError, error::Error as ServiceError,
AbstractService,
Configuration, Configuration,
ServiceBuilder, RpcHandlers,
ServiceComponents,
TaskManager,
}; };
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair; use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
use sp_inherents::InherentDataProviders; use sp_inherents::InherentDataProviders;
@@ -50,142 +53,184 @@ native_executor_instance!(
test_node_runtime::native_version, test_node_runtime::native_version,
); );
/// Starts a `ServiceBuilder` for a full service. type FullClient = sc_service::TFullClient<Block, RuntimeApi, Executor>;
/// type FullBackend = sc_service::TFullBackend<Block>;
/// Use this macro if you don't actually need the full service, but just the builder in order to type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
/// be able to perform chain operations.
macro_rules! new_full_start {
($config:expr) => {{
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
use std::sync::Arc;
let mut import_setup = None; pub fn new_full_params(
let inherent_data_providers = sp_inherents::InherentDataProviders::new(); config: Configuration,
) -> Result<
(
sc_service::ServiceParams<
Block,
FullClient,
sc_consensus_aura::AuraImportQueue<Block, FullClient>,
sc_transaction_pool::FullPool<Block, FullClient>,
(),
FullBackend,
>,
FullSelectChain,
sp_inherents::InherentDataProviders,
sc_finality_grandpa::GrandpaBlockImport<
FullBackend,
Block,
FullClient,
FullSelectChain,
>,
sc_finality_grandpa::LinkHalf<Block, FullClient, FullSelectChain>,
),
ServiceError,
> {
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
let builder = let (client, backend, keystore, task_manager) =
sc_service::ServiceBuilder::new_full::< sc_service::new_full_parts::<Block, RuntimeApi, Executor>(&config)?;
test_node_runtime::opaque::Block, let client = Arc::new(client);
test_node_runtime::RuntimeApi,
crate::service::Executor,
>($config)?
.with_select_chain(|_config, backend| {
Ok(sc_consensus::LongestChain::new(backend.clone()))
})?
.with_transaction_pool(|builder| {
let pool_api =
sc_transaction_pool::FullChainApi::new(builder.client().clone());
Ok(sc_transaction_pool::BasicPool::new(
builder.config().transaction_pool.clone(),
std::sync::Arc::new(pool_api),
builder.prometheus_registry(),
))
})?
.with_import_queue(
|_config,
client,
mut select_chain,
_transaction_pool,
spawn_task_handle,
registry| {
let select_chain = select_chain
.take()
.ok_or_else(|| sc_service::Error::SelectChainRequired)?;
let (grandpa_block_import, grandpa_link) = let select_chain = sc_consensus::LongestChain::new(backend.clone());
sc_finality_grandpa::block_import(
client.clone(),
&(client.clone() as Arc<_>),
select_chain,
)?;
let aura_block_import = let pool_api = sc_transaction_pool::FullChainApi::new(
sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new( client.clone(),
grandpa_block_import.clone(), config.prometheus_registry(),
client.clone(), );
); let transaction_pool = sc_transaction_pool::BasicPool::new_full(
config.transaction_pool.clone(),
std::sync::Arc::new(pool_api),
config.prometheus_registry(),
task_manager.spawn_handle(),
client.clone(),
);
let import_queue = let (grandpa_block_import, grandpa_link) = sc_finality_grandpa::block_import(
sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>( client.clone(),
sc_consensus_aura::slot_duration(&*client)?, &(client.clone() as Arc<_>),
aura_block_import, select_chain.clone(),
Some(Box::new(grandpa_block_import.clone())), )?;
None,
client,
inherent_data_providers.clone(),
spawn_task_handle,
registry,
)?;
import_setup = Some((grandpa_block_import, grandpa_link)); let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(
grandpa_block_import.clone(),
client.clone(),
);
Ok(import_queue) let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
}, sc_consensus_aura::slot_duration(&*client)?,
)?; aura_block_import,
Some(Box::new(grandpa_block_import.clone())),
None,
client.clone(),
inherent_data_providers.clone(),
&task_manager.spawn_handle(),
config.prometheus_registry(),
)?;
(builder, import_setup, inherent_data_providers) let provider = client.clone() as Arc<dyn StorageAndProofProvider<_, _>>;
}}; let finality_proof_provider =
Arc::new(GrandpaFinalityProofProvider::new(backend.clone(), provider));
let params = sc_service::ServiceParams {
backend,
client,
import_queue,
keystore,
task_manager,
transaction_pool,
config,
block_announce_validator_builder: None,
finality_proof_request_builder: None,
finality_proof_provider: Some(finality_proof_provider),
on_demand: None,
remote_blockchain: None,
rpc_extensions_builder: Box::new(|_| ()),
};
Ok((
params,
select_chain,
inherent_data_providers,
grandpa_block_import,
grandpa_link,
))
} }
/// Builds a new service for a full client. /// Builds a new service for a full client.
pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceError> { pub fn new_full(
let role = config.role.clone(); config: Configuration,
let force_authoring = config.force_authoring; ) -> Result<(TaskManager, Arc<RpcHandlers>), ServiceError> {
let name = config.network.node_name.clone(); let (params, select_chain, inherent_data_providers, block_import, grandpa_link) =
let disable_grandpa = config.disable_grandpa; new_full_params(config)?;
let (builder, mut import_setup, inherent_data_providers) = new_full_start!(config); let (
role,
force_authoring,
name,
enable_grandpa,
prometheus_registry,
client,
transaction_pool,
keystore,
) = {
let sc_service::ServiceParams {
config,
client,
transaction_pool,
keystore,
..
} = &params;
let (block_import, grandpa_link) = (
import_setup.take() config.role.clone(),
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed"); config.force_authoring,
config.network.node_name.clone(),
!config.disable_grandpa,
config.prometheus_registry().cloned(),
client.clone(),
transaction_pool.clone(),
keystore.clone(),
)
};
let service = builder let ServiceComponents {
.with_finality_proof_provider(|client, backend| { task_manager,
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider rpc_handlers,
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>; network,
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _) telemetry_on_connect_sinks,
})? ..
.build_full()?; } = sc_service::build(params)?;
if role.is_authority() { if role.is_authority() {
let proposer = sc_basic_authorship::ProposerFactory::new( let proposer = sc_basic_authorship::ProposerFactory::new(
service.client(), client.clone(),
service.transaction_pool(), transaction_pool,
service.prometheus_registry().as_ref(), prometheus_registry.as_ref(),
); );
let client = service.client();
let select_chain = service
.select_chain()
.ok_or(ServiceError::SelectChainRequired)?;
let can_author_with = let can_author_with =
sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()); sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone());
let aura = sc_consensus_aura::start_aura::<_, _, _, _, _, AuraPair, _, _, _>( let aura = sc_consensus_aura::start_aura::<_, _, _, _, _, AuraPair, _, _, _>(
sc_consensus_aura::slot_duration(&*client)?, sc_consensus_aura::slot_duration(&*client)?,
client, client.clone(),
select_chain, select_chain,
block_import, block_import,
proposer, proposer,
service.network(), network.clone(),
inherent_data_providers.clone(), inherent_data_providers.clone(),
force_authoring, force_authoring,
service.keystore(), keystore.clone(),
can_author_with, can_author_with,
)?; )?;
// the AURA authoring task is considered essential, i.e. if it // the AURA authoring task is considered essential, i.e. if it
// fails we take down the service with it. // fails we take down the service with it.
service task_manager
.spawn_essential_task_handle() .spawn_essential_handle()
.spawn_blocking("aura", aura); .spawn_blocking("aura", aura);
} }
// if the node isn't actively participating in consensus then it doesn't // if the node isn't actively participating in consensus then it doesn't
// need a keystore, regardless of which protocol we use below. // need a keystore, regardless of which protocol we use below.
let keystore = if role.is_authority() { let keystore = if role.is_authority() {
Some(service.keystore() as sp_core::traits::BareCryptoStorePtr) Some(keystore as sp_core::traits::BareCryptoStorePtr)
} else { } else {
None None
}; };
@@ -199,7 +244,6 @@ pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceEr
is_authority: role.is_network_authority(), is_authority: role.is_network_authority(),
}; };
let enable_grandpa = !disable_grandpa;
if enable_grandpa { if enable_grandpa {
// start the full GRANDPA voter // start the full GRANDPA voter
// NOTE: non-authorities could run the GRANDPA observer protocol, but at // NOTE: non-authorities could run the GRANDPA observer protocol, but at
@@ -210,95 +254,95 @@ pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceEr
let grandpa_config = sc_finality_grandpa::GrandpaParams { let grandpa_config = sc_finality_grandpa::GrandpaParams {
config: grandpa_config, config: grandpa_config,
link: grandpa_link, link: grandpa_link,
network: service.network(), network,
inherent_data_providers: inherent_data_providers.clone(), inherent_data_providers,
telemetry_on_connect: Some(service.telemetry_on_connect_stream()), telemetry_on_connect: Some(telemetry_on_connect_sinks.on_connect_stream()),
voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(), voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(),
prometheus_registry: service.prometheus_registry(), prometheus_registry,
shared_voter_state: SharedVoterState::empty(), shared_voter_state: SharedVoterState::empty(),
}; };
// the GRANDPA voter task is considered infallible, i.e. // the GRANDPA voter task is considered infallible, i.e.
// if it fails we take down the service with it. // if it fails we take down the service with it.
service.spawn_essential_task_handle().spawn_blocking( task_manager.spawn_essential_handle().spawn_blocking(
"grandpa-voter", "grandpa-voter",
sc_finality_grandpa::run_grandpa_voter(grandpa_config)?, sc_finality_grandpa::run_grandpa_voter(grandpa_config)?,
); );
} else { } else {
sc_finality_grandpa::setup_disabled_grandpa( sc_finality_grandpa::setup_disabled_grandpa(
service.client(), client,
&inherent_data_providers, &inherent_data_providers,
service.network(), network,
)?; )?;
} }
Ok(service) Ok((task_manager, rpc_handlers))
} }
/// Builds a new service for a light client. /// Builds a new service for a light client.
pub fn new_light(config: Configuration) -> Result<impl AbstractService, ServiceError> { pub fn new_light(
let inherent_data_providers = InherentDataProviders::new(); config: Configuration,
) -> Result<(TaskManager, Arc<RpcHandlers>), ServiceError> {
let (client, backend, keystore, task_manager, on_demand) =
sc_service::new_light_parts::<Block, RuntimeApi, Executor>(&config)?;
ServiceBuilder::new_light::<Block, RuntimeApi, Executor>(config)? let transaction_pool_api = Arc::new(sc_transaction_pool::LightChainApi::new(
.with_select_chain(|_config, backend| { client.clone(),
Ok(LongestChain::new(backend.clone())) on_demand.clone(),
})? ));
.with_transaction_pool(|builder| { let transaction_pool = sc_transaction_pool::BasicPool::new_light(
let fetcher = builder.fetcher() config.transaction_pool.clone(),
.ok_or_else(|| "Trying to start light transaction pool without active fetcher")?; transaction_pool_api,
config.prometheus_registry(),
task_manager.spawn_handle(),
);
let pool_api = sc_transaction_pool::LightChainApi::new( let grandpa_block_import = sc_finality_grandpa::light_block_import(
builder.client().clone(), client.clone(),
fetcher.clone(), backend.clone(),
); &(client.clone() as Arc<_>),
let pool = sc_transaction_pool::BasicPool::with_revalidation_type( Arc::new(on_demand.checker().clone()) as Arc<_>,
builder.config().transaction_pool.clone(), )?;
Arc::new(pool_api), let finality_proof_import = grandpa_block_import.clone();
builder.prometheus_registry(), let finality_proof_request_builder =
sc_transaction_pool::RevalidationType::Light, finality_proof_import.create_finality_proof_request_builder();
);
Ok(pool)
})?
.with_import_queue_and_fprb(|
_config,
client,
backend,
fetcher,
_select_chain,
_tx_pool,
spawn_task_handle,
prometheus_registry,
| {
let fetch_checker = fetcher
.map(|fetcher| fetcher.checker().clone())
.ok_or_else(|| "Trying to start light import queue without active fetch checker")?;
let grandpa_block_import = sc_finality_grandpa::light_block_import(
client.clone(),
backend,
&(client.clone() as Arc<_>),
Arc::new(fetch_checker),
)?;
let finality_proof_import = grandpa_block_import.clone();
let finality_proof_request_builder =
finality_proof_import.create_finality_proof_request_builder();
let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>( let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
sc_consensus_aura::slot_duration(&*client)?, sc_consensus_aura::slot_duration(&*client)?,
grandpa_block_import, grandpa_block_import,
None, None,
Some(Box::new(finality_proof_import)), Some(Box::new(finality_proof_import)),
client, client.clone(),
inherent_data_providers.clone(), InherentDataProviders::new(),
spawn_task_handle, &task_manager.spawn_handle(),
prometheus_registry, config.prometheus_registry(),
)?; )?;
Ok((import_queue, finality_proof_request_builder)) let finality_proof_provider = Arc::new(GrandpaFinalityProofProvider::new(
})? backend.clone(),
.with_finality_proof_provider(|client, backend| { client.clone() as Arc<_>,
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider ));
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _) sc_service::build(sc_service::ServiceParams {
})? block_announce_validator_builder: None,
.build_light() finality_proof_request_builder: Some(finality_proof_request_builder),
finality_proof_provider: Some(finality_proof_provider),
on_demand: Some(on_demand),
remote_blockchain: Some(backend.remote_blockchain()),
rpc_extensions_builder: Box::new(|_| ()),
transaction_pool: Arc::new(transaction_pool),
config,
client,
import_queue,
keystore,
backend,
task_manager,
})
.map(
|ServiceComponents {
task_manager,
rpc_handlers,
..
}| (task_manager, rpc_handlers),
)
} }