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)
* 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]
name = "substrate-subxt"
version = "0.10.1"
version = "0.11.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
@@ -22,22 +22,23 @@ default = ["kusama"]
client = ["substrate-subxt-client"]
[dependencies]
log = "0.4.8"
log = "0.4.11"
thiserror = "1.0.20"
futures = { version = "0.3.5", package = "futures" }
jsonrpsee = { version = "0.1.0", features = ["ws"] }
num-traits = { version = "0.2.12", default-features = false }
serde = { version = "1.0.114", features = ["derive"] }
serde_json = "1.0.55"
serde_json = "1.0.56"
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-support = { version = "2.0.0-rc4", package = "frame-support" }
sp-runtime = { version = "2.0.0-rc4", package = "sp-runtime" }
sp-version = { version = "2.0.0-rc4", package = "sp-version" }
pallet-indices = { version = "2.0.0-rc4", package = "pallet-indices" }
frame-metadata = { version = "11.0.0-rc5", package = "frame-metadata" }
frame-support = { version = "2.0.0-rc5", package = "frame-support" }
sp-runtime = { version = "2.0.0-rc5", package = "sp-runtime" }
sp-version = { version = "2.0.0-rc5", package = "sp-version" }
pallet-indices = { version = "2.0.0-rc5", package = "pallet-indices" }
hex = "0.4.2"
<<<<<<< HEAD
sp-rpc = { version = "2.0.0-rc4", package = "sp-rpc" }
sp-core = { version = "2.0.0-rc4", package = "sp-core" }
sc-rpc-api = { version = "0.8.0-rc4", package = "sc-rpc-api" }
@@ -54,15 +55,25 @@ pallet-staking = "2.0.0-rc4"
[dev-dependencies]
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"
wabt = "0.9.2"
wabt-sys = "=0.7.1" # pinned because 0.7.2 fails to compile
frame-system = { version = "2.0.0-rc4", package = "frame-system" }
pallet-balances = { version = "2.0.0-rc4", package = "pallet-balances" }
sp-keyring = { version = "2.0.0-rc4", package = "sp-keyring" }
substrate-subxt-client = { version = "0.2.0", path = "client" }
frame-system = { version = "2.0.0-rc5", package = "frame-system" }
pallet-balances = { version = "2.0.0-rc5", package = "pallet-balances" }
sp-keyring = { version = "2.0.0-rc5", package = "sp-keyring" }
substrate-subxt-client = { version = "0.3.0", path = "client" }
tempdir = "0.3.7"
test-node = { path = "test-node" }
<<<<<<< HEAD
[patch.crates-io]
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-transaction-pool = { git = "https://github.com/paritytech/substrate" }
jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee" }
=======
wabt = "0.10.0"
>>>>>>> master
+9 -9
View File
@@ -1,6 +1,6 @@
[package]
name = "substrate-subxt-client"
version = "0.2.0"
version = "0.3.0"
authors = ["David Craven <david@craven.ch>", "Parity Technologies <admin@parity.io>"]
edition = "2018"
@@ -12,19 +12,19 @@ description = "Embed a substrate node into your subxt application."
keywords = ["parity", "substrate", "blockchain"]
[dependencies]
async-std = "=1.5.0"
futures = { version = "0.3.5", features = ["compat"], package = "futures" }
async-std = "1.6.2"
futures = { version = "0.3.5", features = ["compat"] }
futures01 = { package = "futures", version = "0.1.29" }
jsonrpsee = "0.1.0"
log = "0.4.8"
sc-network = { version = "0.8.0-rc4", default-features = false }
sc-service = { version = "0.8.0-rc4", default-features = false }
serde_json = "1.0.55"
sp-keyring = "2.0.0-rc4"
log = "0.4.11"
sc-network = { version = "0.8.0-rc5", default-features = false }
sc-service = { version = "0.8.0-rc5", default-features = false }
serde_json = "1.0.56"
sp-keyring = "2.0.0-rc5"
thiserror = "1.0.20"
[dev-dependencies]
async-std = { version = "=1.5.0", features = ["attributes"] }
async-std = { version = "1.6.2", features = ["attributes"] }
env_logger = "0.7.1"
substrate-subxt = { path = ".." }
tempdir = "0.3.7"
+191 -180
View File
@@ -20,20 +20,20 @@
use async_std::task;
use futures::{
channel::mpsc,
compat::{
Compat01As03,
Compat01As03Sink,
Sink01CompatExt,
Stream01CompatExt,
},
future::poll_fn,
sink::SinkExt,
stream::{
Stream,
StreamExt,
future::{
select,
FutureExt,
},
sink::SinkExt,
stream::StreamExt,
};
use futures01::sync::mpsc;
use futures01::sync::mpsc as mpsc01;
use jsonrpsee::{
common::{
Request,
@@ -53,16 +53,18 @@ use sc_service::{
config::{
NetworkConfiguration,
TaskType,
TelemetryEndpoints,
},
AbstractService,
ChainSpec,
Configuration,
RpcHandlers,
RpcSession,
TaskManager,
};
use std::{
future::Future,
pin::Pin,
task::Poll,
sync::Arc,
};
use thiserror::Error;
@@ -74,81 +76,59 @@ pub enum SubxtClientError {
Json(#[from] serde_json::Error),
/// Channel closed.
#[error("{0}")]
Mpsc(#[from] mpsc::SendError<String>),
}
/// 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,
Mpsc(#[from] mpsc::SendError),
}
/// Client for an embedded substrate node.
pub struct SubxtClient {
to_back: Compat01As03Sink<mpsc::Sender<String>, String>,
from_back: Compat01As03<mpsc::Receiver<String>>,
to_back: mpsc::Sender<String>,
from_back: Compat01As03<mpsc01::Receiver<String>>,
}
impl SubxtClient {
/// Create a new client from a config.
pub fn new<C: ChainSpec + 'static, S: AbstractService>(
config: SubxtClientConfig<C, S>,
) -> Result<Self, ServiceError> {
/// Create a new client.
pub fn new(mut task_manager: TaskManager, rpc: Arc<RpcHandlers>) -> Self {
let (to_back, from_front) = mpsc::channel(4);
let (to_front, from_back) = mpsc::channel(4);
start_subxt_client(config, from_front, to_front)?;
Ok(Self {
to_back: to_back.sink_compat(),
let (to_front, from_back) = mpsc01::channel(4);
let session = RpcSession::new(to_front.clone());
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(),
})
}
}
/// 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>(
config: SubxtClientConfig<C, S>,
from_front: mpsc::Receiver<String>,
to_front: mpsc::Sender<String>,
) -> Result<(), ServiceError> {
let mut network = NetworkConfiguration::new(
format!("{} (subxt client)", config.chain_spec.name()),
"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(),
/// 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),
}
telemetry_endpoints: Default::default(),
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 {}, {}", 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();
}
});
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(),
}
Poll::Pending => break,
Poll::Ready(None) => return Poll::Ready(()),
}
}
}
}
loop {
match Pin::new(&mut service).poll(cx) {
Poll::Ready(Ok(())) => return Poll::Ready(()),
Poll::Pending => return Poll::Pending,
Poll::Ready(Err(e)) => log::error!("{}", e),
}
impl From<Role> for Option<String> {
fn from(role: Role) -> Self {
match role {
Role::Light => None,
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)]
@@ -347,12 +354,14 @@ mod tests {
cache_size: 64,
},
keystore: KeystoreConfig::InMemory,
builder: test_node::service::new_light,
chain_spec,
role: Role::Light,
enable_telemetry: false,
};
let client = ClientBuilder::<NodeTemplateRuntime>::new()
.set_client(SubxtClient::new(config).unwrap())
.set_client(
SubxtClient::from_config(config, test_node::service::new_light).unwrap(),
)
.build()
.await
.unwrap();
@@ -378,12 +387,14 @@ mod tests {
cache_size: 128,
},
keystore: KeystoreConfig::InMemory,
builder: test_node::service::new_full,
chain_spec: test_node::chain_spec::development_config(),
chain_spec: test_node::chain_spec::development_config().unwrap(),
role: Role::Authority(AccountKeyring::Alice),
enable_telemetry: false,
};
let client = ClientBuilder::<NodeTemplateRuntime>::new()
.set_client(SubxtClient::new(config).unwrap())
.set_client(
SubxtClient::from_config(config, test_node::service::new_full).unwrap(),
)
.build()
.await
.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]
name = "substrate-subxt-proc-macro"
version = "0.9.0"
version = "0.11.0"
authors = ["David Craven <david@craven.ch>", "Parity Technologies <admin@parity.io>"]
edition = "2018"
autotests = false
@@ -16,19 +16,19 @@ proc-macro = true
[dependencies]
heck = "0.3.1"
proc-macro2 = "1.0.18"
proc-macro-crate = "0.1.4"
proc-macro-error = "1.0.2"
proc-macro2 = "1.0.19"
proc-macro-crate = "0.1.5"
proc-macro-error = "1.0.3"
quote = "1.0.7"
syn = "1.0.33"
syn = "1.0.35"
synstructure = "0.12.4"
[dev-dependencies]
async-std = { version = "=1.5.0", features = ["attributes"] }
codec = { package = "parity-scale-codec", version = "1.3.1", features = ["derive"] }
async-std = { version = "1.6.2", features = ["attributes"] }
codec = { package = "parity-scale-codec", version = "1.3.4", features = ["derive"] }
env_logger = "0.7.1"
pretty_assertions = "0.6.1"
sp-keyring = "2.0.0-rc4"
sp-keyring = "2.0.0-rc5"
substrate-subxt = { path = ".." }
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 store_name = utils::ident_to_name(ident, "Store").to_camel_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 bindings = utils::bindings(&s);
let fields = utils::fields(&bindings);
@@ -110,12 +111,23 @@ pub fn store(s: Structure) -> TokenStream {
let keys = filtered_fields
.iter()
.map(|(field, _)| quote!(&self.#field));
let key_iter = quote!(#subxt::KeyIter<T, #ident<#(#params),*>>);
quote! {
impl#generics #subxt::Store<T> for #ident<#(#params),*> {
const MODULE: &'static str = MODULE;
const FIELD: &'static str = #store_name;
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(
&self,
metadata: &#subxt::Metadata,
@@ -129,13 +141,19 @@ pub fn store(s: Structure) -> TokenStream {
}
/// Store extension trait.
pub trait #store_trait<T: #module> {
/// Retrive the store element.
pub trait #store_trait<T: #subxt::Runtime + #module> {
/// Retrieve the store element.
fn #store<'a>(
&'a self,
#args
hash: Option<T::Hash>,
) -> 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> {
@@ -145,7 +163,14 @@ pub fn store(s: Structure) -> TokenStream {
hash: Option<T::Hash>,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#ret, #subxt::Error>> + Send + 'a>> {
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 FIELD: &'static str = "Account";
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(
&self,
metadata: &substrate_subxt::Metadata,
@@ -182,13 +217,18 @@ mod tests {
}
/// Store extension trait.
pub trait AccountStoreExt<T: Balances> {
/// Retrive the store element.
pub trait AccountStoreExt<T: substrate_subxt::Runtime + Balances> {
/// Retrieve the store element.
fn account<'a>(
&'a self,
account_id: &'a <T as System>::AccountId,
hash: Option<T::Hash>,
) -> 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> {
@@ -199,7 +239,14 @@ mod tests {
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<AccountData<T::Balance>, substrate_subxt::Error>> + Send + 'a>>
{
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,
step: {
state: {
alice: AccountStore { account_id: &alice },
bob: AccountStore { account_id: &bob },
alice: &AccountStore { account_id: &alice },
bob: &AccountStore { account_id: &bob },
},
call: TransferCall {
to: &bob,
+1 -1
View File
@@ -133,7 +133,7 @@ mod tests {
use sp_keyring::AccountKeyring;
#[async_std::test]
async fn test_transfer() {
async fn test_basic_transfer() {
env_logger::try_init().ok();
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.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;
/// Return type.
type Returns: Decode;
/// Returns the key prefix for storage maps
fn prefix(metadata: &Metadata) -> Result<StorageKey, MetadataError>;
/// Returns the `StorageKey`.
fn key(&self, metadata: &Metadata) -> Result<StorageKey, MetadataError>;
/// Returns the default value.
+138 -17
View File
@@ -55,6 +55,7 @@ use sc_rpc_api::state::ReadProof;
use sp_core::{
storage::{
StorageChangeSet,
StorageData,
StorageKey,
},
Bytes,
@@ -115,6 +116,7 @@ pub struct ClientBuilder<T: Runtime> {
_marker: std::marker::PhantomData<T>,
url: Option<String>,
client: Option<jsonrpsee::Client>,
page_size: Option<u32>,
}
impl<T: Runtime> ClientBuilder<T> {
@@ -124,6 +126,7 @@ impl<T: Runtime> ClientBuilder<T> {
_marker: std::marker::PhantomData,
url: None,
client: None,
page_size: None,
}
}
@@ -139,6 +142,12 @@ impl<T: Runtime> ClientBuilder<T> {
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.
pub async fn build(self) -> Result<Client<T>, Error> {
let client = if let Some(client) = self.client {
@@ -164,6 +173,7 @@ impl<T: Runtime> ClientBuilder<T> {
metadata: metadata?,
runtime_version: runtime_version?,
_marker: PhantomData,
page_size: self.page_size.unwrap_or(10),
})
}
}
@@ -175,6 +185,7 @@ pub struct Client<T: Runtime> {
metadata: Metadata,
runtime_version: RuntimeVersion,
_marker: PhantomData<(fn() -> T::Signature, T::Extra)>,
page_size: u32,
}
impl<T: Runtime> Clone for Client<T> {
@@ -185,6 +196,53 @@ impl<T: Runtime> Clone for Client<T> {
metadata: self.metadata.clone(),
runtime_version: self.runtime_version.clone(),
_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
}
/// 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>>(
&self,
store: F,
store: &F,
hash: Option<T::Hash>,
) -> Result<F::Returns, Error> {
let key = store.key(&self.metadata)?;
if let Some(data) = self.rpc.storage(key, hash).await? {
Ok(Decode::decode(&mut &data.0[..])?)
if let Some(data) = self.fetch(store, hash).await? {
Ok(data)
} else {
Ok(store.default(&self.metadata)?)
}
}
/// Fetch a StorageKey an optional storage key.
pub async fn fetch<F: Store<T>>(
/// Returns an iterator of key value pairs.
pub async fn iter<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[..])?))
) -> Result<KeyIter<T, F>, Error> {
let hash = if let Some(hash) = hash {
hash
} 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
@@ -490,12 +586,16 @@ mod tests {
path: tmp.path().join("keystore"),
password: None,
},
builder: test_node::service::new_full,
chain_spec: test_node::chain_spec::development_config(),
chain_spec: test_node::chain_spec::development_config().unwrap(),
role: Role::Authority(key),
enable_telemetry: false,
};
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()
.await
.expect("Error creating client");
@@ -609,4 +709,25 @@ mod tests {
let mut blocks = client.subscribe_finalized_blocks().await.unwrap();
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 {
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();
bytes.extend(&sp_core::twox_128(self.storage_prefix.as_bytes())[..]);
bytes
StorageKey(bytes)
}
pub fn default<V: Decode>(&self) -> Result<V, MetadataError> {
@@ -286,7 +286,7 @@ impl StorageMetadata {
match &self.ty {
StorageEntryType::Plain(_) => {
Ok(StoragePlain {
prefix: self.prefix(),
prefix: self.prefix().0,
})
}
_ => Err(MetadataError::StorageTypeError),
@@ -298,7 +298,7 @@ impl StorageMetadata {
StorageEntryType::Map { hasher, .. } => {
Ok(StorageMap {
_marker: PhantomData,
prefix: self.prefix(),
prefix: self.prefix().0,
hasher: hasher.clone(),
})
}
@@ -317,7 +317,7 @@ impl StorageMetadata {
} => {
Ok(StorageDoubleMap {
_marker: PhantomData,
prefix: self.prefix(),
prefix: self.prefix().0,
hasher1: hasher.clone(),
hasher2: key2_hasher.clone(),
})
+35 -1
View File
@@ -122,7 +122,7 @@ impl<T: Runtime> Rpc<T> {
/// Fetch a storage key
pub async fn storage(
&self,
key: StorageKey,
key: &StorageKey,
hash: Option<T::Hash>,
) -> Result<Option<StorageData>, Error> {
let params = Params::Array(vec![to_json_value(key)?, to_json_value(hash)?]);
@@ -131,6 +131,27 @@ impl<T: Runtime> Rpc<T> {
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
pub async fn query_storage(
&self,
@@ -149,6 +170,19 @@ impl<T: Runtime> Rpc<T> {
.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
pub async fn genesis_hash(&self) -> Result<T::Hash, Error> {
let block_zero = Some(ListOrValue::Value(NumberOrHex::Number(0)));
+21 -21
View File
@@ -1,6 +1,6 @@
[package]
name = "test-node"
version = "2.0.0-rc4"
version = "2.0.0-rc5"
authors = ["Anonymous"]
description = "Substrate Node template"
edition = "2018"
@@ -14,29 +14,29 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
futures = "0.3.5"
log = "0.4.8"
log = "0.4.11"
structopt = "0.3.15"
parking_lot = "0.11.0"
sc-cli = { version = "0.8.0-rc4", features = ["wasmtime"] }
sp-core = "2.0.0-rc4"
sc-executor = { version = "0.8.0-rc4", features = ["wasmtime"] }
sc-service = { version = "0.8.0-rc4", features = ["wasmtime"] }
sp-inherents = "2.0.0-rc4"
sc-transaction-pool = "2.0.0-rc4"
sp-transaction-pool = "2.0.0-rc4"
sc-network = "0.8.0-rc4"
sc-consensus-aura = "0.8.0-rc4"
sp-consensus-aura = "0.8.0-rc4"
sp-consensus = "0.8.0-rc4"
sc-consensus = "0.8.0-rc4"
sc-finality-grandpa = "0.8.0-rc4"
sp-finality-grandpa = "2.0.0-rc4"
sc-client-api = "2.0.0-rc4"
sp-runtime = "2.0.0-rc4"
sc-basic-authorship = "0.8.0-rc4"
sc-cli = { version = "0.8.0-rc5", features = ["wasmtime"] }
sp-core = "2.0.0-rc5"
sc-executor = { version = "0.8.0-rc5", features = ["wasmtime"] }
sc-service = { version = "0.8.0-rc5", features = ["wasmtime"] }
sp-inherents = "2.0.0-rc5"
sc-transaction-pool = "2.0.0-rc5"
sp-transaction-pool = "2.0.0-rc5"
sc-network = "0.8.0-rc5"
sc-consensus-aura = "0.8.0-rc5"
sp-consensus-aura = "0.8.0-rc5"
sp-consensus = "0.8.0-rc5"
sc-consensus = "0.8.0-rc5"
sc-finality-grandpa = "0.8.0-rc5"
sp-finality-grandpa = "2.0.0-rc5"
sc-client-api = "2.0.0-rc5"
sp-runtime = "2.0.0-rc5"
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]
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]
name = "test-node-runtime"
version = "2.0.0-rc4"
version = "2.0.0-rc5"
authors = ["Anonymous"]
edition = "2018"
license = "Unlicense"
@@ -11,31 +11,31 @@ repository = "https://github.com/paritytech/substrate/"
targets = ["x86_64-unknown-linux-gnu"]
[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" }
balances = { version = "2.0.0-rc4", default-features = false, package = "pallet-balances" }
frame-support = { version = "2.0.0-rc4", default-features = false }
grandpa = { version = "2.0.0-rc4", default-features = false, package = "pallet-grandpa" }
randomness-collective-flip = { version = "2.0.0-rc4", default-features = false, package = "pallet-randomness-collective-flip" }
sudo = { version = "2.0.0-rc4", default-features = false, package = "pallet-sudo" }
system = { version = "2.0.0-rc4", default-features = false, package = "frame-system" }
timestamp = { version = "2.0.0-rc4", default-features = false, package = "pallet-timestamp" }
transaction-payment = { version = "2.0.0-rc4", default-features = false, package = "pallet-transaction-payment" }
frame-executive = { version = "2.0.0-rc4", default-features = false }
aura = { version = "2.0.0-rc5", default-features = false, package = "pallet-aura" }
balances = { version = "2.0.0-rc5", default-features = false, package = "pallet-balances" }
frame-support = { version = "2.0.0-rc5", default-features = false }
grandpa = { version = "2.0.0-rc5", default-features = false, package = "pallet-grandpa" }
randomness-collective-flip = { version = "2.0.0-rc5", default-features = false, package = "pallet-randomness-collective-flip" }
sudo = { version = "2.0.0-rc5", default-features = false, package = "pallet-sudo" }
system = { version = "2.0.0-rc5", default-features = false, package = "frame-system" }
timestamp = { version = "2.0.0-rc5", default-features = false, package = "pallet-timestamp" }
transaction-payment = { version = "2.0.0-rc5", default-features = false, package = "pallet-transaction-payment" }
frame-executive = { version = "2.0.0-rc5", default-features = false }
serde = { version = "1.0.114", optional = true, features = ["derive"] }
sp-api = { version = "2.0.0-rc4", default-features = false }
sp-block-builder = { default-features = false, version = "2.0.0-rc4" }
sp-consensus-aura = { version = "0.8.0-rc4", default-features = false }
sp-core = { version = "2.0.0-rc4", default-features = false }
sp-inherents = { default-features = false, version = "2.0.0-rc4" }
sp-io = { version = "2.0.0-rc4", default-features = false }
sp-offchain = { version = "2.0.0-rc4", default-features = false }
sp-runtime = { version = "2.0.0-rc4", default-features = false }
sp-session = { version = "2.0.0-rc4", default-features = false }
sp-std = { version = "2.0.0-rc4", default-features = false }
sp-transaction-pool = { version = "2.0.0-rc4", default-features = false }
sp-version = { version = "2.0.0-rc4", default-features = false }
sp-api = { version = "2.0.0-rc5", default-features = false }
sp-block-builder = { version = "2.0.0-rc5", default-features = false }
sp-consensus-aura = { version = "0.8.0-rc5", default-features = false }
sp-core = { version = "2.0.0-rc5", default-features = false }
sp-inherents = { version = "2.0.0-rc5", default-features = false }
sp-io = { version = "2.0.0-rc5", default-features = false }
sp-offchain = { version = "2.0.0-rc5", default-features = false }
sp-runtime = { version = "2.0.0-rc5", default-features = false }
sp-session = { version = "2.0.0-rc5", default-features = false }
sp-std = { version = "2.0.0-rc5", default-features = false }
sp-transaction-pool = { version = "2.0.0-rc5", default-features = false }
sp-version = { version = "2.0.0-rc5", default-features = false }
[build-dependencies]
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 = ();
/// The data to be stored in an account.
type AccountData = balances::AccountData<Balance>;
/// Weight information for the extrinsics of this pallet.
type SystemWeightInfo = ();
}
impl aura::Trait for Runtime {
@@ -267,6 +269,7 @@ impl timestamp::Trait for Runtime {
type Moment = u64;
type OnTimestampSet = Aura;
type MinimumPeriod = MinimumPeriod;
type WeightInfo = ();
}
parameter_types! {
@@ -281,6 +284,7 @@ impl balances::Trait for Runtime {
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
}
parameter_types! {
@@ -309,7 +313,7 @@ construct_runtime!(
System: system::{Module, Call, Config, Storage, Event<T>},
RandomnessCollectiveFlip: randomness_collective_flip::{Module, Call, Storage},
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},
Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: transaction_payment::{Module, Storage},
@@ -439,7 +443,7 @@ impl_runtime_apis! {
Grandpa::grandpa_authorities()
}
fn submit_report_equivocation_extrinsic(
fn submit_report_equivocation_unsigned_extrinsic(
_equivocation_proof: fg_primitives::EquivocationProof<
<Block as BlockT>::Hash,
NumberFor<Block>,
+47 -14
View File
@@ -38,13 +38,13 @@ use test_node_runtime::{
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/";
/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.
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 {
TPublic::Pair::from_string(&format!("//{}", seed), None)
.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;
/// 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
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
@@ -61,20 +61,28 @@ where
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) {
(get_from_seed::<AuraId>(s), get_from_seed::<GrandpaId>(s))
}
pub fn development_config() -> ChainSpec {
ChainSpec::from_genesis(
pub fn development_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY;
Ok(ChainSpec::from_genesis(
// Name
"Development",
// ID
"dev",
ChainType::Development,
|| {
move || {
testnet_genesis(
wasm_binary,
// Initial PoA authorities
vec![authority_keys_from_seed("Alice")],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
@@ -84,26 +92,39 @@ pub fn development_config() -> ChainSpec {
true,
)
},
// Bootnodes
vec![],
// Telemetry
None,
// Protocol ID
None,
// Properties
None,
// Extensions
None,
)
))
}
pub fn local_testnet_config() -> ChainSpec {
ChainSpec::from_genesis(
pub fn local_testnet_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY;
Ok(ChainSpec::from_genesis(
// Name
"Local Testnet",
// ID
"local_testnet",
ChainType::Local,
|| {
move || {
testnet_genesis(
wasm_binary,
// Initial PoA authorities
vec![
authority_keys_from_seed("Alice"),
authority_keys_from_seed("Bob"),
],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
@@ -121,15 +142,22 @@ pub fn local_testnet_config() -> ChainSpec {
true,
)
},
// Bootnodes
vec![],
// Telemetry
None,
// Protocol ID
None,
// Properties
None,
// Extensions
None,
)
))
}
/// Configure initial storage state for FRAME modules.
fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
@@ -137,10 +165,12 @@ fn testnet_genesis(
) -> GenesisConfig {
GenesisConfig {
system: Some(SystemConfig {
code: WASM_BINARY.to_vec(),
// Add Wasm runtime to storage.
code: wasm_binary.to_vec(),
changes_trie_config: Default::default(),
}),
balances: Some(BalancesConfig {
// Configure endowed accounts with initial balance of 1 << 60.
balances: endowed_accounts
.iter()
.cloned()
@@ -156,6 +186,9 @@ fn testnet_genesis(
.map(|x| (x.1.clone(), 1))
.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,
cli::Cli,
service,
service::new_full_params,
};
use sc_cli::SubstrateCli;
use sc_cli::{
ChainSpec,
Role,
RuntimeVersion,
SubstrateCli,
};
use sc_service::ServiceParams;
impl SubstrateCli for Cli {
fn impl_name() -> &'static str {
"Substrate Node"
fn impl_name() -> String {
"Substrate Node".into()
}
fn impl_version() -> &'static str {
env!("SUBSTRATE_CLI_IMPL_VERSION")
fn impl_version() -> String {
env!("SUBSTRATE_CLI_IMPL_VERSION").into()
}
fn description() -> &'static str {
env!("CARGO_PKG_DESCRIPTION")
fn description() -> String {
env!("CARGO_PKG_DESCRIPTION").into()
}
fn author() -> &'static str {
env!("CARGO_PKG_AUTHORS")
fn author() -> String {
env!("CARGO_PKG_AUTHORS").into()
}
fn support_url() -> &'static str {
"support.anonymous.an"
fn support_url() -> String {
"support.anonymous.an".into()
}
fn copyright_start_year() -> i32 {
2017
}
fn executable_name() -> &'static str {
env!("CARGO_PKG_NAME")
}
fn load_spec(&self, id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {
Ok(match id {
"dev" => Box::new(chain_spec::development_config()),
"" | "local" => Box::new(chain_spec::local_testnet_config()),
"dev" => Box::new(chain_spec::development_config()?),
"" | "local" => Box::new(chain_spec::local_testnet_config()?),
path => {
Box::new(chain_spec::ChainSpec::from_json_file(
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
@@ -70,15 +77,29 @@ pub fn run() -> sc_cli::Result<()> {
match &cli.subcommand {
Some(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 => {
let runner = cli.create_runner(&cli.run)?;
runner.run_node(
service::new_light,
service::new_full,
test_node_runtime::VERSION,
)
runner.run_node_until_exit(|config| {
match config.role {
Role::Light => service::new_light(config),
_ => 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.
use sc_client_api::ExecutorProvider;
use sc_consensus::LongestChain;
use sc_client_api::{
ExecutorProvider,
RemoteBackend,
};
use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor;
use sc_finality_grandpa::{
@@ -27,9 +29,10 @@ use sc_finality_grandpa::{
};
use sc_service::{
error::Error as ServiceError,
AbstractService,
Configuration,
ServiceBuilder,
RpcHandlers,
ServiceComponents,
TaskManager,
};
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
use sp_inherents::InherentDataProviders;
@@ -50,142 +53,184 @@ native_executor_instance!(
test_node_runtime::native_version,
);
/// Starts a `ServiceBuilder` for a full service.
///
/// Use this macro if you don't actually need the full service, but just the builder in order to
/// 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;
type FullClient = sc_service::TFullClient<Block, RuntimeApi, Executor>;
type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
let mut import_setup = None;
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
pub fn new_full_params(
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 =
sc_service::ServiceBuilder::new_full::<
test_node_runtime::opaque::Block,
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 (client, backend, keystore, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, Executor>(&config)?;
let client = Arc::new(client);
let (grandpa_block_import, grandpa_link) =
sc_finality_grandpa::block_import(
client.clone(),
&(client.clone() as Arc<_>),
select_chain,
)?;
let select_chain = sc_consensus::LongestChain::new(backend.clone());
let aura_block_import =
sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(
grandpa_block_import.clone(),
client.clone(),
);
let pool_api = sc_transaction_pool::FullChainApi::new(
client.clone(),
config.prometheus_registry(),
);
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 =
sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
sc_consensus_aura::slot_duration(&*client)?,
aura_block_import,
Some(Box::new(grandpa_block_import.clone())),
None,
client,
inherent_data_providers.clone(),
spawn_task_handle,
registry,
)?;
let (grandpa_block_import, grandpa_link) = sc_finality_grandpa::block_import(
client.clone(),
&(client.clone() as Arc<_>),
select_chain.clone(),
)?;
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.
pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceError> {
let role = config.role.clone();
let force_authoring = config.force_authoring;
let name = config.network.node_name.clone();
let disable_grandpa = config.disable_grandpa;
pub fn new_full(
config: Configuration,
) -> Result<(TaskManager, Arc<RpcHandlers>), ServiceError> {
let (params, select_chain, inherent_data_providers, block_import, grandpa_link) =
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()
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed");
(
config.role.clone(),
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
.with_finality_proof_provider(|client, backend| {
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
})?
.build_full()?;
let ServiceComponents {
task_manager,
rpc_handlers,
network,
telemetry_on_connect_sinks,
..
} = sc_service::build(params)?;
if role.is_authority() {
let proposer = sc_basic_authorship::ProposerFactory::new(
service.client(),
service.transaction_pool(),
service.prometheus_registry().as_ref(),
client.clone(),
transaction_pool,
prometheus_registry.as_ref(),
);
let client = service.client();
let select_chain = service
.select_chain()
.ok_or(ServiceError::SelectChainRequired)?;
let can_author_with =
sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone());
let aura = sc_consensus_aura::start_aura::<_, _, _, _, _, AuraPair, _, _, _>(
sc_consensus_aura::slot_duration(&*client)?,
client,
client.clone(),
select_chain,
block_import,
proposer,
service.network(),
network.clone(),
inherent_data_providers.clone(),
force_authoring,
service.keystore(),
keystore.clone(),
can_author_with,
)?;
// the AURA authoring task is considered essential, i.e. if it
// fails we take down the service with it.
service
.spawn_essential_task_handle()
task_manager
.spawn_essential_handle()
.spawn_blocking("aura", aura);
}
// if the node isn't actively participating in consensus then it doesn't
// need a keystore, regardless of which protocol we use below.
let keystore = if role.is_authority() {
Some(service.keystore() as sp_core::traits::BareCryptoStorePtr)
Some(keystore as sp_core::traits::BareCryptoStorePtr)
} else {
None
};
@@ -199,7 +244,6 @@ pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceEr
is_authority: role.is_network_authority(),
};
let enable_grandpa = !disable_grandpa;
if enable_grandpa {
// start the full GRANDPA voter
// 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 {
config: grandpa_config,
link: grandpa_link,
network: service.network(),
inherent_data_providers: inherent_data_providers.clone(),
telemetry_on_connect: Some(service.telemetry_on_connect_stream()),
network,
inherent_data_providers,
telemetry_on_connect: Some(telemetry_on_connect_sinks.on_connect_stream()),
voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(),
prometheus_registry: service.prometheus_registry(),
prometheus_registry,
shared_voter_state: SharedVoterState::empty(),
};
// the GRANDPA voter task is considered infallible, i.e.
// 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",
sc_finality_grandpa::run_grandpa_voter(grandpa_config)?,
);
} else {
sc_finality_grandpa::setup_disabled_grandpa(
service.client(),
client,
&inherent_data_providers,
service.network(),
network,
)?;
}
Ok(service)
Ok((task_manager, rpc_handlers))
}
/// Builds a new service for a light client.
pub fn new_light(config: Configuration) -> Result<impl AbstractService, ServiceError> {
let inherent_data_providers = InherentDataProviders::new();
pub fn new_light(
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)?
.with_select_chain(|_config, backend| {
Ok(LongestChain::new(backend.clone()))
})?
.with_transaction_pool(|builder| {
let fetcher = builder.fetcher()
.ok_or_else(|| "Trying to start light transaction pool without active fetcher")?;
let transaction_pool_api = Arc::new(sc_transaction_pool::LightChainApi::new(
client.clone(),
on_demand.clone(),
));
let transaction_pool = sc_transaction_pool::BasicPool::new_light(
config.transaction_pool.clone(),
transaction_pool_api,
config.prometheus_registry(),
task_manager.spawn_handle(),
);
let pool_api = sc_transaction_pool::LightChainApi::new(
builder.client().clone(),
fetcher.clone(),
);
let pool = sc_transaction_pool::BasicPool::with_revalidation_type(
builder.config().transaction_pool.clone(),
Arc::new(pool_api),
builder.prometheus_registry(),
sc_transaction_pool::RevalidationType::Light,
);
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 grandpa_block_import = sc_finality_grandpa::light_block_import(
client.clone(),
backend.clone(),
&(client.clone() as Arc<_>),
Arc::new(on_demand.checker().clone()) as Arc<_>,
)?;
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, _>(
sc_consensus_aura::slot_duration(&*client)?,
grandpa_block_import,
None,
Some(Box::new(finality_proof_import)),
client,
inherent_data_providers.clone(),
spawn_task_handle,
prometheus_registry,
)?;
let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
sc_consensus_aura::slot_duration(&*client)?,
grandpa_block_import,
None,
Some(Box::new(finality_proof_import)),
client.clone(),
InherentDataProviders::new(),
&task_manager.spawn_handle(),
config.prometheus_registry(),
)?;
Ok((import_queue, finality_proof_request_builder))
})?
.with_finality_proof_provider(|client, backend| {
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
})?
.build_light()
let finality_proof_provider = Arc::new(GrandpaFinalityProofProvider::new(
backend.clone(),
client.clone() as Arc<_>,
));
sc_service::build(sc_service::ServiceParams {
block_announce_validator_builder: None,
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),
)
}