Reorganising the repository - external renames and moves (#4074)

* Adding first rough ouline of the repository structure

* Remove old CI stuff

* add title

* formatting fixes

* move node-exits job's script to scripts dir

* Move docs into subdir

* move to bin

* move maintainence scripts, configs and helpers into its own dir

* add .local to ignore

* move core->client

* start up 'test' area

* move test client

* move test runtime

* make test move compile

* Add dependencies rule enforcement.

* Fix indexing.

* Update docs to reflect latest changes

* Moving /srml->/paint

* update docs

* move client/sr-* -> primitives/

* clean old readme

* remove old broken code in rhd

* update lock

* Step 1.

* starting to untangle client

* Fix after merge.

* start splitting out client interfaces

* move children and blockchain interfaces

* Move trie and state-machine to primitives.

* Fix WASM builds.

* fixing broken imports

* more interface moves

* move backend and light to interfaces

* move CallExecutor

* move cli off client

* moving around more interfaces

* re-add consensus crates into the mix

* fix subkey path

* relieve client from executor

* starting to pull out client from grandpa

* move is_decendent_of out of client

* grandpa still depends on client directly

* lemme tests pass

* rename srml->paint

* Make it compile.

* rename interfaces->client-api

* Move keyring to primitives.

* fixup libp2p dep

* fix broken use

* allow dependency enforcement to fail

* move fork-tree

* Moving wasm-builder

* make env

* move build-script-utils

* fixup broken crate depdencies and names

* fix imports for authority discovery

* fix typo

* update cargo.lock

* fixing imports

* Fix paths and add missing crates

* re-add missing crates
This commit is contained in:
Benjamin Kampmann
2019-11-14 21:51:17 +01:00
committed by Bastian Köcher
parent becc3b0a4f
commit 60e5011c72
809 changed files with 7801 additions and 6464 deletions
+37
View File
@@ -0,0 +1,37 @@
[package]
name = "substrate-rpc"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
api = { package = "substrate-rpc-api", path = "./api" }
client-api = { package = "substrate-client-api", path = "../api" }
client = { package = "substrate-client", path = "../" }
sr-api = { path = "../../primitives/sr-api" }
codec = { package = "parity-scale-codec", version = "1.0.0" }
futures03 = { package = "futures-preview", version = "0.3.0-alpha.19", features = ["compat"] }
jsonrpc-pubsub = "14.0.3"
log = "0.4.8"
primitives = { package = "substrate-primitives", path = "../../primitives/core" }
rpc = { package = "jsonrpc-core", version = "14.0.3" }
runtime_version = { package = "sr-version", path = "../../primitives/sr-version" }
serde_json = "1.0.41"
session = { package = "substrate-session", path = "../../primitives/session" }
sr-primitives = { path = "../../primitives/sr-primitives" }
rpc-primitives = { package = "substrate-rpc-primitives", path = "../../primitives/rpc" }
state_machine = { package = "substrate-state-machine", path = "../../primitives/state-machine" }
substrate-executor = { path = "../executor" }
substrate-keystore = { path = "../keystore" }
transaction_pool = { package = "substrate-transaction-pool", path = "../transaction-pool" }
hash-db = { version = "0.15.2", default-features = false }
parking_lot = { version = "0.9.0" }
[dev-dependencies]
assert_matches = "1.3.0"
futures = "0.1.29"
network = { package = "substrate-network", path = "../network" }
rustc-hex = "2.0.1"
sr-io = { path = "../../primitives/sr-io" }
test-client = { package = "substrate-test-runtime-client", path = "../../test/utils/runtime/client" }
tokio = "0.1.22"
+22
View File
@@ -0,0 +1,22 @@
[package]
name = "substrate-rpc-api"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
codec = { package = "parity-scale-codec", version = "1.0.0" }
derive_more = "0.15.0"
futures03 = { package = "futures-preview", version = "0.3.0-alpha.19", features = ["compat"] }
jsonrpc-core = "14.0.3"
jsonrpc-core-client = "14.0.3"
jsonrpc-derive = "14.0.3"
jsonrpc-pubsub = "14.0.3"
log = "0.4.8"
parking_lot = "0.9.0"
primitives = { package = "substrate-primitives", path = "../../../primitives/core" }
runtime_version = { package = "sr-version", path = "../../../primitives/sr-version" }
serde = { version = "1.0.101", features = ["derive"] }
serde_json = "1.0.41"
txpool = { package = "substrate-transaction-graph", path = "../../transaction-pool/graph" }
rpc-primitives = { package = "substrate-rpc-primitives", path = "../../../primitives/rpc" }
@@ -0,0 +1,153 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Authoring RPC module errors.
use crate::errors;
use jsonrpc_core as rpc;
/// Author RPC Result type.
pub type Result<T> = std::result::Result<T, Error>;
/// Author RPC future Result type.
pub type FutureResult<T> = Box<dyn rpc::futures::Future<Item = T, Error = Error> + Send>;
/// Author RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
/// Client error.
#[display(fmt="Client error: {}", _0)]
Client(Box<dyn std::error::Error + Send>),
/// Transaction pool error,
#[display(fmt="Transaction pool error: {}", _0)]
Pool(txpool::error::Error),
/// Verification error
#[display(fmt="Extrinsic verification error: {}", _0)]
Verification(Box<dyn std::error::Error + Send>),
/// Incorrect extrinsic format.
#[display(fmt="Invalid extrinsic format: {}", _0)]
BadFormat(codec::Error),
/// Incorrect seed phrase.
#[display(fmt="Invalid seed phrase/SURI")]
BadSeedPhrase,
/// Key type ID has an unknown format.
#[display(fmt="Invalid key type ID format (should be of length four)")]
BadKeyType,
/// Key type ID has some unsupported crypto.
#[display(fmt="The crypto of key type ID is unknown")]
UnsupportedKeyType,
/// Some random issue with the key store. Shouldn't happen.
#[display(fmt="The key store is unavailable")]
KeyStoreUnavailable,
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Client(ref err) => Some(&**err),
Error::Pool(ref err) => Some(err),
Error::Verification(ref err) => Some(&**err),
_ => None,
}
}
}
/// Base code for all authorship errors.
const BASE_ERROR: i64 = 1000;
/// Extrinsic has an invalid format.
const BAD_FORMAT: i64 = BASE_ERROR + 1;
/// Error during transaction verification in runtime.
const VERIFICATION_ERROR: i64 = BASE_ERROR + 2;
/// Pool rejected the transaction as invalid
const POOL_INVALID_TX: i64 = BASE_ERROR + 10;
/// Cannot determine transaction validity.
const POOL_UNKNOWN_VALIDITY: i64 = POOL_INVALID_TX + 1;
/// The transaction is temporarily banned.
const POOL_TEMPORARILY_BANNED: i64 = POOL_INVALID_TX + 2;
/// The transaction is already in the pool
const POOL_ALREADY_IMPORTED: i64 = POOL_INVALID_TX + 3;
/// Transaction has too low priority to replace existing one in the pool.
const POOL_TOO_LOW_PRIORITY: i64 = POOL_INVALID_TX + 4;
/// Including this transaction would cause a dependency cycle.
const POOL_CYCLE_DETECTED: i64 = POOL_INVALID_TX + 5;
/// The transaction was not included to the pool because of the limits.
const POOL_IMMEDIATELY_DROPPED: i64 = POOL_INVALID_TX + 6;
/// The key type crypto is not known.
const UNSUPPORTED_KEY_TYPE: i64 = POOL_INVALID_TX + 7;
impl From<Error> for rpc::Error {
fn from(e: Error) -> Self {
use txpool::error::{Error as PoolError};
match e {
Error::BadFormat(e) => rpc::Error {
code: rpc::ErrorCode::ServerError(BAD_FORMAT),
message: format!("Extrinsic has invalid format: {}", e).into(),
data: None,
},
Error::Verification(e) => rpc::Error {
code: rpc::ErrorCode::ServerError(VERIFICATION_ERROR),
message: format!("Verification Error: {}", e).into(),
data: Some(format!("{:?}", e).into()),
},
Error::Pool(PoolError::InvalidTransaction(e)) => rpc::Error {
code: rpc::ErrorCode::ServerError(POOL_INVALID_TX),
message: "Invalid Transaction".into(),
data: serde_json::to_value(e).ok(),
},
Error::Pool(PoolError::UnknownTransaction(e)) => rpc::Error {
code: rpc::ErrorCode::ServerError(POOL_UNKNOWN_VALIDITY),
message: "Unknown Transaction Validity".into(),
data: serde_json::to_value(e).ok(),
},
Error::Pool(PoolError::TemporarilyBanned) => rpc::Error {
code: rpc::ErrorCode::ServerError(POOL_TEMPORARILY_BANNED),
message: "Transaction is temporarily banned".into(),
data: None,
},
Error::Pool(PoolError::AlreadyImported(hash)) => rpc::Error {
code: rpc::ErrorCode::ServerError(POOL_ALREADY_IMPORTED),
message: "Transaction Already Imported".into(),
data: Some(format!("{:?}", hash).into()),
},
Error::Pool(PoolError::TooLowPriority { old, new }) => rpc::Error {
code: rpc::ErrorCode::ServerError(POOL_TOO_LOW_PRIORITY),
message: format!("Priority is too low: ({} vs {})", old, new),
data: Some("The transaction has too low priority to replace another transaction already in the pool.".into()),
},
Error::Pool(PoolError::CycleDetected) => rpc::Error {
code: rpc::ErrorCode::ServerError(POOL_CYCLE_DETECTED),
message: "Cycle Detected".into(),
data: None,
},
Error::Pool(PoolError::ImmediatelyDropped) => rpc::Error {
code: rpc::ErrorCode::ServerError(POOL_IMMEDIATELY_DROPPED),
message: "Immediately Dropped".into(),
data: Some("The transaction couldn't enter the pool because of the limit".into()),
},
Error::UnsupportedKeyType => rpc::Error {
code: rpc::ErrorCode::ServerError(UNSUPPORTED_KEY_TYPE),
message: "Unknown key type crypto" .into(),
data: Some(
"The crypto for the given key type is unknown, please add the public key to the \
request to insert the key successfully.".into()
),
},
e => errors::internal(e),
}
}
}
@@ -0,0 +1,32 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Extrinsic helpers for author RPC module.
use primitives::Bytes;
use serde::{Serialize, Deserialize};
/// RPC Extrinsic or hash
///
/// Allows to refer to extrinsic either by its raw representation or its hash.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ExtrinsicOrHash<Hash> {
/// The hash of the extrinsic.
Hash(Hash),
/// Raw extrinsic bytes.
Extrinsic(Bytes),
}
@@ -0,0 +1,86 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Substrate block-author/full-node API.
pub mod error;
pub mod hash;
use jsonrpc_derive::rpc;
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use primitives::{
Bytes
};
use self::error::{FutureResult, Result};
use txpool::watcher::Status;
pub use self::gen_client::Client as AuthorClient;
/// Substrate authoring RPC API
#[rpc]
pub trait AuthorApi<Hash, BlockHash> {
/// RPC metadata
type Metadata;
/// Submit hex-encoded extrinsic for inclusion in block.
#[rpc(name = "author_submitExtrinsic")]
fn submit_extrinsic(&self, extrinsic: Bytes) -> FutureResult<Hash>;
/// Insert a key into the keystore.
#[rpc(name = "author_insertKey")]
fn insert_key(&self,
key_type: String,
suri: String,
public: Bytes,
) -> Result<()>;
/// Generate new session keys and returns the corresponding public keys.
#[rpc(name = "author_rotateKeys")]
fn rotate_keys(&self) -> Result<Bytes>;
/// Returns all pending extrinsics, potentially grouped by sender.
#[rpc(name = "author_pendingExtrinsics")]
fn pending_extrinsics(&self) -> Result<Vec<Bytes>>;
/// Remove given extrinsic from the pool and temporarily ban it to prevent reimporting.
#[rpc(name = "author_removeExtrinsic")]
fn remove_extrinsic(&self,
bytes_or_hash: Vec<hash::ExtrinsicOrHash<Hash>>
) -> Result<Vec<Hash>>;
/// Submit an extrinsic to watch.
#[pubsub(
subscription = "author_extrinsicUpdate",
subscribe,
name = "author_submitAndWatchExtrinsic"
)]
fn watch_extrinsic(&self,
metadata: Self::Metadata,
subscriber: Subscriber<Status<Hash, BlockHash>>,
bytes: Bytes
);
/// Unsubscribe from extrinsic watching.
#[pubsub(
subscription = "author_extrinsicUpdate",
unsubscribe,
name = "author_unwatchExtrinsic"
)]
fn unwatch_extrinsic(&self,
metadata: Option<Self::Metadata>,
id: SubscriptionId
) -> Result<bool>;
}
@@ -0,0 +1,62 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Error helpers for Chain RPC module.
use crate::errors;
use jsonrpc_core as rpc;
/// Chain RPC Result type.
pub type Result<T> = std::result::Result<T, Error>;
/// Chain RPC future Result type.
pub type FutureResult<T> = Box<dyn rpc::futures::Future<Item = T, Error = Error> + Send>;
/// Chain RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
/// Client error.
#[display(fmt="Client error: {}", _0)]
Client(Box<dyn std::error::Error + Send>),
/// Other error type.
Other(String),
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Client(ref err) => Some(&**err),
_ => None,
}
}
}
/// Base error code for all chain errors.
const BASE_ERROR: i64 = 3000;
impl From<Error> for rpc::Error {
fn from(e: Error) -> Self {
match e {
Error::Other(message) => rpc::Error {
code: rpc::ErrorCode::ServerError(BASE_ERROR + 1),
message,
data: None,
},
e => errors::internal(e),
}
}
}
+89
View File
@@ -0,0 +1,89 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Substrate blockchain API.
pub mod error;
use jsonrpc_core::Result as RpcResult;
use jsonrpc_core::futures::Future;
use jsonrpc_derive::rpc;
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use rpc_primitives::number;
use self::error::{FutureResult, Result};
pub use self::gen_client::Client as ChainClient;
/// Substrate blockchain API
#[rpc]
pub trait ChainApi<Number, Hash, Header, SignedBlock> {
/// RPC metadata
type Metadata;
/// Get header of a relay chain block.
#[rpc(name = "chain_getHeader")]
fn header(&self, hash: Option<Hash>) -> FutureResult<Option<Header>>;
/// Get header and body of a relay chain block.
#[rpc(name = "chain_getBlock")]
fn block(&self, hash: Option<Hash>) -> FutureResult<Option<SignedBlock>>;
/// Get hash of the n-th block in the canon chain.
///
/// By default returns latest block hash.
#[rpc(name = "chain_getBlockHash", alias("chain_getHead"))]
fn block_hash(&self, hash: Option<number::NumberOrHex<Number>>) -> Result<Option<Hash>>;
/// Get hash of the last finalized block in the canon chain.
#[rpc(name = "chain_getFinalizedHead", alias("chain_getFinalisedHead"))]
fn finalized_head(&self) -> Result<Hash>;
/// New head subscription
#[pubsub(
subscription = "chain_newHead",
subscribe,
name = "chain_subscribeNewHeads",
alias("subscribe_newHead", "chain_subscribeNewHead")
)]
fn subscribe_new_heads(&self, metadata: Self::Metadata, subscriber: Subscriber<Header>);
/// Unsubscribe from new head subscription.
#[pubsub(
subscription = "chain_newHead",
unsubscribe,
name = "chain_unsubscribeNewHeads",
alias("unsubscribe_newHead", "chain_unsubscribeNewHead")
)]
fn unsubscribe_new_heads(&self, metadata: Option<Self::Metadata>, id: SubscriptionId) -> RpcResult<bool>;
/// New head subscription
#[pubsub(
subscription = "chain_finalizedHead",
subscribe,
name = "chain_subscribeFinalizedHeads",
alias("chain_subscribeFinalisedHeads")
)]
fn subscribe_finalized_heads(&self, metadata: Self::Metadata, subscriber: Subscriber<Header>);
/// Unsubscribe from new head subscription.
#[pubsub(
subscription = "chain_finalizedHead",
unsubscribe,
name = "chain_unsubscribeFinalizedHeads",
alias("chain_unsubscribeFinalisedHeads")
)]
fn unsubscribe_finalized_heads(&self, metadata: Option<Self::Metadata>, id: SubscriptionId) -> RpcResult<bool>;
}
+26
View File
@@ -0,0 +1,26 @@
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
use log::warn;
pub fn internal<E: ::std::fmt::Debug>(e: E) -> jsonrpc_core::Error {
warn!("Unknown error: {:?}", e);
jsonrpc_core::Error {
code: jsonrpc_core::ErrorCode::InternalError,
message: "Unknown error occured".into(),
data: Some(format!("{:?}", e).into()),
}
}
+31
View File
@@ -0,0 +1,31 @@
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
use jsonrpc_core::futures::prelude::*;
use futures03::{channel::oneshot, compat::Compat};
/// Wraps around `oneshot::Receiver` and adjusts the error type to produce an internal error if the
/// sender gets dropped.
pub struct Receiver<T>(pub Compat<oneshot::Receiver<T>>);
impl<T> Future for Receiver<T> {
type Item = T;
type Error = jsonrpc_core::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.0.poll().map_err(|_| jsonrpc_core::Error::internal_error())
}
}
+34
View File
@@ -0,0 +1,34 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Substrate RPC interfaces.
//!
//! A collection of RPC methods and subscriptions supported by all substrate clients.
#![warn(missing_docs)]
mod errors;
mod helpers;
mod subscriptions;
pub use jsonrpc_core::IoHandlerExtension as RpcExtension;
pub use subscriptions::{Subscriptions, TaskExecutor};
pub use helpers::Receiver;
pub mod author;
pub mod chain;
pub mod state;
pub mod system;
@@ -0,0 +1,69 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! State RPC errors.
use crate::errors;
use jsonrpc_core as rpc;
/// State RPC Result type.
pub type Result<T> = std::result::Result<T, Error>;
/// State RPC future Result type.
pub type FutureResult<T> = Box<dyn rpc::futures::Future<Item = T, Error = Error> + Send>;
/// State RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
/// Client error.
#[display(fmt="Client error: {}", _0)]
Client(Box<dyn std::error::Error + Send>),
/// Provided block range couldn't be resolved to a list of blocks.
#[display(fmt = "Cannot resolve a block range ['{:?}' ... '{:?}]. {}", from, to, details)]
InvalidBlockRange {
/// Beginning of the block range.
from: String,
/// End of the block range.
to: String,
/// Details of the error message.
details: String,
},
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Client(ref err) => Some(&**err),
_ => None,
}
}
}
/// Base code for all state errors.
const BASE_ERROR: i64 = 4000;
impl From<Error> for rpc::Error {
fn from(e: Error) -> Self {
match e {
Error::InvalidBlockRange { .. } => rpc::Error {
code: rpc::ErrorCode::ServerError(BASE_ERROR + 1),
message: format!("{}", e),
data: None,
},
e => errors::internal(e),
}
}
}
+143
View File
@@ -0,0 +1,143 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Substrate state API.
pub mod error;
use jsonrpc_core::Result as RpcResult;
use jsonrpc_core::futures::Future;
use jsonrpc_derive::rpc;
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use primitives::Bytes;
use primitives::storage::{StorageKey, StorageData, StorageChangeSet};
use runtime_version::RuntimeVersion;
use self::error::FutureResult;
pub use self::gen_client::Client as StateClient;
/// Substrate state API
#[rpc]
pub trait StateApi<Hash> {
/// RPC Metadata
type Metadata;
/// Call a contract at a block's state.
#[rpc(name = "state_call", alias("state_callAt"))]
fn call(&self, name: String, bytes: Bytes, hash: Option<Hash>) -> FutureResult<Bytes>;
/// Returns the keys with prefix, leave empty to get all the keys
#[rpc(name = "state_getKeys")]
fn storage_keys(&self, prefix: StorageKey, hash: Option<Hash>) -> FutureResult<Vec<StorageKey>>;
/// Returns a storage entry at a specific block's state.
#[rpc(name = "state_getStorage", alias("state_getStorageAt"))]
fn storage(&self, key: StorageKey, hash: Option<Hash>) -> FutureResult<Option<StorageData>>;
/// Returns the hash of a storage entry at a block's state.
#[rpc(name = "state_getStorageHash", alias("state_getStorageHashAt"))]
fn storage_hash(&self, key: StorageKey, hash: Option<Hash>) -> FutureResult<Option<Hash>>;
/// Returns the size of a storage entry at a block's state.
#[rpc(name = "state_getStorageSize", alias("state_getStorageSizeAt"))]
fn storage_size(&self, key: StorageKey, hash: Option<Hash>) -> FutureResult<Option<u64>>;
/// Returns the keys with prefix from a child storage, leave empty to get all the keys
#[rpc(name = "state_getChildKeys")]
fn child_storage_keys(
&self,
child_storage_key: StorageKey,
prefix: StorageKey,
hash: Option<Hash>
) -> FutureResult<Vec<StorageKey>>;
/// Returns a child storage entry at a specific block's state.
#[rpc(name = "state_getChildStorage")]
fn child_storage(
&self,
child_storage_key: StorageKey,
key: StorageKey,
hash: Option<Hash>
) -> FutureResult<Option<StorageData>>;
/// Returns the hash of a child storage entry at a block's state.
#[rpc(name = "state_getChildStorageHash")]
fn child_storage_hash(
&self,
child_storage_key: StorageKey,
key: StorageKey,
hash: Option<Hash>
) -> FutureResult<Option<Hash>>;
/// Returns the size of a child storage entry at a block's state.
#[rpc(name = "state_getChildStorageSize")]
fn child_storage_size(
&self,
child_storage_key: StorageKey,
key: StorageKey,
hash: Option<Hash>
) -> FutureResult<Option<u64>>;
/// Returns the runtime metadata as an opaque blob.
#[rpc(name = "state_getMetadata")]
fn metadata(&self, hash: Option<Hash>) -> FutureResult<Bytes>;
/// Get the runtime version.
#[rpc(name = "state_getRuntimeVersion", alias("chain_getRuntimeVersion"))]
fn runtime_version(&self, hash: Option<Hash>) -> FutureResult<RuntimeVersion>;
/// Query historical storage entries (by key) starting from a block given as the second parameter.
///
/// NOTE This first returned result contains the initial state of storage for all keys.
/// Subsequent values in the vector represent changes to the previous state (diffs).
#[rpc(name = "state_queryStorage")]
fn query_storage(
&self,
keys: Vec<StorageKey>,
block: Hash,
hash: Option<Hash>
) -> FutureResult<Vec<StorageChangeSet<Hash>>>;
/// New runtime version subscription
#[pubsub(
subscription = "state_runtimeVersion",
subscribe,
name = "state_subscribeRuntimeVersion",
alias("chain_subscribeRuntimeVersion")
)]
fn subscribe_runtime_version(&self, metadata: Self::Metadata, subscriber: Subscriber<RuntimeVersion>);
/// Unsubscribe from runtime version subscription
#[pubsub(
subscription = "state_runtimeVersion",
unsubscribe,
name = "state_unsubscribeRuntimeVersion",
alias("chain_unsubscribeRuntimeVersion")
)]
fn unsubscribe_runtime_version(&self, metadata: Option<Self::Metadata>, id: SubscriptionId) -> RpcResult<bool>;
/// New storage subscription
#[pubsub(subscription = "state_storage", subscribe, name = "state_subscribeStorage")]
fn subscribe_storage(
&self, metadata: Self::Metadata, subscriber: Subscriber<StorageChangeSet<Hash>>, keys: Option<Vec<StorageKey>>
);
/// Unsubscribe from storage subscription
#[pubsub(subscription = "state_storage", unsubscribe, name = "state_unsubscribeStorage")]
fn unsubscribe_storage(
&self, metadata: Option<Self::Metadata>, id: SubscriptionId
) -> RpcResult<bool>;
}
@@ -0,0 +1,119 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
use std::collections::HashMap;
use std::sync::{Arc, atomic::{self, AtomicUsize}};
use log::{error, warn};
use jsonrpc_pubsub::{SubscriptionId, typed::{Sink, Subscriber}};
use parking_lot::Mutex;
use jsonrpc_core::futures::sync::oneshot;
use jsonrpc_core::futures::{Future, future};
type Id = u64;
/// Alias for a an implementation of `futures::future::Executor`.
pub type TaskExecutor = Arc<dyn future::Executor<Box<dyn Future<Item = (), Error = ()> + Send>> + Send + Sync>;
/// Generate unique ids for subscriptions.
#[derive(Clone, Debug)]
pub struct IdProvider {
next_id: Arc<AtomicUsize>,
}
impl Default for IdProvider {
fn default() -> Self {
IdProvider {
next_id: Arc::new(AtomicUsize::new(1)),
}
}
}
impl IdProvider {
/// Returns next id for the subscription.
pub fn next_id(&self) -> Id {
self.next_id.fetch_add(1, atomic::Ordering::AcqRel) as u64
}
}
/// Subscriptions manager.
///
/// Takes care of assigning unique subscription ids and
/// driving the sinks into completion.
#[derive(Clone)]
pub struct Subscriptions {
next_id: IdProvider,
active_subscriptions: Arc<Mutex<HashMap<Id, oneshot::Sender<()>>>>,
executor: TaskExecutor,
}
impl Subscriptions {
/// Creates new `Subscriptions` object.
pub fn new(executor: TaskExecutor) -> Self {
Subscriptions {
next_id: Default::default(),
active_subscriptions: Default::default(),
executor,
}
}
/// Borrows the internal task executor.
///
/// This can be used to spawn additional tasks on the underyling event loop.
pub fn executor(&self) -> &TaskExecutor {
&self.executor
}
/// Creates new subscription for given subscriber.
///
/// Second parameter is a function that converts Subscriber sink into a future.
/// This future will be driven to completion by the underlying event loop
/// or will be cancelled in case #cancel is invoked.
pub fn add<T, E, G, R, F>(&self, subscriber: Subscriber<T, E>, into_future: G) -> SubscriptionId where
G: FnOnce(Sink<T, E>) -> R,
R: future::IntoFuture<Future=F, Item=(), Error=()>,
F: future::Future<Item=(), Error=()> + Send + 'static,
{
let id = self.next_id.next_id();
let subscription_id: SubscriptionId = id.into();
if let Ok(sink) = subscriber.assign_id(subscription_id.clone()) {
let (tx, rx) = oneshot::channel();
let future = into_future(sink)
.into_future()
.select(rx.map_err(|e| warn!("Error timeing out: {:?}", e)))
.then(|_| Ok(()));
self.active_subscriptions.lock().insert(id, tx);
if self.executor.execute(Box::new(future)).is_err() {
error!("Failed to spawn RPC subscription task");
}
}
subscription_id
}
/// Cancel subscription.
///
/// Returns true if subscription existed or false otherwise.
pub fn cancel(&self, id: SubscriptionId) -> bool {
if let SubscriptionId::Number(id) = id {
if let Some(tx) = self.active_subscriptions.lock().remove(&id) {
let _ = tx.send(());
return true;
}
}
false
}
}
@@ -0,0 +1,48 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! System RPC module errors.
use crate::system::helpers::Health;
use jsonrpc_core as rpc;
/// System RPC Result type.
pub type Result<T> = std::result::Result<T, Error>;
/// System RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
/// Provided block range couldn't be resolved to a list of blocks.
#[display(fmt = "Node is not fully functional: {}", _0)]
NotHealthy(Health),
}
impl std::error::Error for Error {}
/// Base code for all system errors.
const BASE_ERROR: i64 = 2000;
impl From<Error> for rpc::Error {
fn from(e: Error) -> Self {
match e {
Error::NotHealthy(ref h) => rpc::Error {
code: rpc::ErrorCode::ServerError(BASE_ERROR + 1),
message: format!("{}", e),
data: serde_json::to_value(h).ok(),
},
}
}
}
@@ -0,0 +1,119 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Substrate system API helpers.
use std::fmt;
use serde::{Serialize, Deserialize};
use serde_json::{Value, map::Map};
/// Node properties
pub type Properties = Map<String, Value>;
/// Running node's static details.
#[derive(Clone, Debug)]
pub struct SystemInfo {
/// Implementation name.
pub impl_name: String,
/// Implementation version.
pub impl_version: String,
/// Chain name.
pub chain_name: String,
/// A custom set of properties defined in the chain spec.
pub properties: Properties,
}
/// Health struct returned by the RPC
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Health {
/// Number of connected peers
pub peers: usize,
/// Is the node syncing
pub is_syncing: bool,
/// Should this node have any peers
///
/// Might be false for local chains or when running without discovery.
pub should_have_peers: bool,
}
impl fmt::Display for Health {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{} peers ({})", self.peers, if self.is_syncing {
"syncing"
} else { "idle" })
}
}
/// Network Peer information
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PeerInfo<Hash, Number> {
/// Peer ID
pub peer_id: String,
/// Roles
pub roles: String,
/// Protocol version
pub protocol_version: u32,
/// Peer best block hash
pub best_hash: Hash,
/// Peer best block number
pub best_number: Number,
}
/// The role the node is running as
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub enum NodeRole {
/// The node is a full node
Full,
/// The node is a light client
LightClient,
/// The node is an authority
Authority,
/// An unknown role with a bit number
UnknownRole(u8)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_serialize_health() {
assert_eq!(
::serde_json::to_string(&Health {
peers: 1,
is_syncing: false,
should_have_peers: true,
}).unwrap(),
r#"{"peers":1,"isSyncing":false,"shouldHavePeers":true}"#,
);
}
#[test]
fn should_serialize_peer_info() {
assert_eq!(
::serde_json::to_string(&PeerInfo {
peer_id: "2".into(),
roles: "a".into(),
protocol_version: 2,
best_hash: 5u32,
best_number: 6u32,
}).unwrap(),
r#"{"peerId":"2","roles":"a","protocolVersion":2,"bestHash":5,"bestNumber":6}"#,
);
}
}
@@ -0,0 +1,71 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Substrate system API.
pub mod error;
pub mod helpers;
use crate::helpers::Receiver;
use jsonrpc_derive::rpc;
use self::error::Result;
pub use self::helpers::{Properties, SystemInfo, Health, PeerInfo, NodeRole};
pub use self::gen_client::Client as SystemClient;
/// Substrate system RPC API
#[rpc]
pub trait SystemApi<Hash, Number> {
/// Get the node's implementation name. Plain old string.
#[rpc(name = "system_name")]
fn system_name(&self) -> Result<String>;
/// Get the node implementation's version. Should be a semver string.
#[rpc(name = "system_version")]
fn system_version(&self) -> Result<String>;
/// Get the chain's type. Given as a string identifier.
#[rpc(name = "system_chain")]
fn system_chain(&self) -> Result<String>;
/// Get a custom set of properties as a JSON object, defined in the chain spec.
#[rpc(name = "system_properties")]
fn system_properties(&self) -> Result<Properties>;
/// Return health status of the node.
///
/// Node is considered healthy if it is:
/// - connected to some peers (unless running in dev mode)
/// - not performing a major sync
#[rpc(name = "system_health", returns = "Health")]
fn system_health(&self) -> Receiver<Health>;
/// Returns currently connected peers
#[rpc(name = "system_peers", returns = "Vec<PeerInfo<Hash, Number>>")]
fn system_peers(&self) -> Receiver<Vec<PeerInfo<Hash, Number>>>;
/// Returns current state of the network.
///
/// **Warning**: This API is not stable.
// TODO: make this stable and move structs https://github.com/paritytech/substrate/issues/1890
#[rpc(name = "system_networkState", returns = "jsonrpc_core::Value")]
fn system_network_state(&self) -> Receiver<jsonrpc_core::Value>;
/// Returns the roles the node is running as.
#[rpc(name = "system_nodeRoles", returns = "Vec<NodeRole>")]
fn system_node_roles(&self) -> Receiver<Vec<NodeRole>>;
}
+211
View File
@@ -0,0 +1,211 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Substrate block-author/full-node API.
#[cfg(test)]
mod tests;
use std::{sync::Arc, convert::TryInto};
use futures03::future::{FutureExt, TryFutureExt};
use log::warn;
use client::Client;
use client_api::error::Error as ClientError;
use rpc::futures::{
Sink, Future,
future::result,
};
use futures03::{StreamExt as _, compat::Compat, future::ready};
use api::Subscriptions;
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use codec::{Encode, Decode};
use primitives::{Bytes, Blake2Hasher, H256, traits::BareCryptoStorePtr};
use sr_primitives::{generic, traits::{self, ProvideRuntimeApi}};
use transaction_pool::{
txpool::{
ChainApi as PoolChainApi,
BlockHash,
ExHash,
IntoPoolError,
Pool,
watcher::Status,
},
};
use session::SessionKeys;
/// Re-export the API for backward compatibility.
pub use api::author::*;
use self::error::{Error, FutureResult, Result};
/// Authoring API
pub struct Author<B, E, P, RA> where P: PoolChainApi + Sync + Send + 'static {
/// Substrate client
client: Arc<Client<B, E, <P as PoolChainApi>::Block, RA>>,
/// Transactions pool
pool: Arc<Pool<P>>,
/// Subscriptions manager
subscriptions: Subscriptions,
/// The key store.
keystore: BareCryptoStorePtr,
}
impl<B, E, P, RA> Author<B, E, P, RA> where P: PoolChainApi + Sync + Send + 'static {
/// Create new instance of Authoring API.
pub fn new(
client: Arc<Client<B, E, <P as PoolChainApi>::Block, RA>>,
pool: Arc<Pool<P>>,
subscriptions: Subscriptions,
keystore: BareCryptoStorePtr,
) -> Self {
Author {
client,
pool,
subscriptions,
keystore,
}
}
}
impl<B, E, P, RA> AuthorApi<ExHash<P>, BlockHash<P>> for Author<B, E, P, RA> where
B: client_api::backend::Backend<<P as PoolChainApi>::Block, Blake2Hasher> + Send + Sync + 'static,
E: client_api::CallExecutor<<P as PoolChainApi>::Block, Blake2Hasher> + Send + Sync + 'static,
P: PoolChainApi + Sync + Send + 'static,
P::Block: traits::Block<Hash=H256>,
P::Error: 'static,
RA: Send + Sync + 'static,
Client<B, E, P::Block, RA>: ProvideRuntimeApi,
<Client<B, E, P::Block, RA> as ProvideRuntimeApi>::Api:
SessionKeys<P::Block, Error = ClientError>,
{
type Metadata = crate::metadata::Metadata;
fn insert_key(
&self,
key_type: String,
suri: String,
public: Bytes,
) -> Result<()> {
let key_type = key_type.as_str().try_into().map_err(|_| Error::BadKeyType)?;
let mut keystore = self.keystore.write();
keystore.insert_unknown(key_type, &suri, &public[..])
.map_err(|_| Error::KeyStoreUnavailable)?;
Ok(())
}
fn rotate_keys(&self) -> Result<Bytes> {
let best_block_hash = self.client.info().chain.best_hash;
self.client.runtime_api().generate_session_keys(
&generic::BlockId::Hash(best_block_hash),
None,
).map(Into::into).map_err(|e| Error::Client(Box::new(e)))
}
fn submit_extrinsic(&self, ext: Bytes) -> FutureResult<ExHash<P>> {
let xt = match Decode::decode(&mut &ext[..]) {
Ok(xt) => xt,
Err(err) => return Box::new(result(Err(err.into()))),
};
let best_block_hash = self.client.info().chain.best_hash;
Box::new(self.pool
.submit_one(&generic::BlockId::hash(best_block_hash), xt)
.compat()
.map_err(|e| e.into_pool_error()
.map(Into::into)
.unwrap_or_else(|e| error::Error::Verification(Box::new(e)).into()))
)
}
fn pending_extrinsics(&self) -> Result<Vec<Bytes>> {
Ok(self.pool.ready().map(|tx| tx.data.encode().into()).collect())
}
fn remove_extrinsic(
&self,
bytes_or_hash: Vec<hash::ExtrinsicOrHash<ExHash<P>>>,
) -> Result<Vec<ExHash<P>>> {
let hashes = bytes_or_hash.into_iter()
.map(|x| match x {
hash::ExtrinsicOrHash::Hash(h) => Ok(h),
hash::ExtrinsicOrHash::Extrinsic(bytes) => {
let xt = Decode::decode(&mut &bytes[..])?;
Ok(self.pool.hash_of(&xt))
},
})
.collect::<Result<Vec<_>>>()?;
Ok(
self.pool.remove_invalid(&hashes)
.into_iter()
.map(|tx| tx.hash.clone())
.collect()
)
}
fn watch_extrinsic(&self,
_metadata: Self::Metadata,
subscriber: Subscriber<Status<ExHash<P>, BlockHash<P>>>,
xt: Bytes,
) {
let submit = || -> Result<_> {
let best_block_hash = self.client.info().chain.best_hash;
let dxt = <<P as PoolChainApi>::Block as traits::Block>::Extrinsic::decode(&mut &xt[..])
.map_err(error::Error::from)?;
Ok(
self.pool
.submit_and_watch(&generic::BlockId::hash(best_block_hash), dxt)
.map_err(|e| e.into_pool_error()
.map(error::Error::from)
.unwrap_or_else(|e| error::Error::Verification(Box::new(e)).into())
)
)
};
let subscriptions = self.subscriptions.clone();
let future = ready(submit())
.and_then(|res| res)
// convert the watcher into a `Stream`
.map(|res| res.map(|watcher| watcher.into_stream().map(|v| Ok::<_, ()>(Ok(v)))))
// now handle the import result,
// start a new subscrition
.map(move |result| match result {
Ok(watcher) => {
subscriptions.add(subscriber, move |sink| {
sink
.sink_map_err(|_| unimplemented!())
.send_all(Compat::new(watcher))
.map(|_| ())
});
},
Err(err) => {
warn!("Failed to submit extrinsic: {}", err);
// reject the subscriber (ignore errors - we don't care if subscriber is no longer there).
let _ = subscriber.reject(err.into());
},
});
let res = self.subscriptions.executor()
.execute(Box::new(Compat::new(future.map(|_| Ok(())))));
if res.is_err() {
warn!("Error spawning subscription RPC task.");
}
}
fn unwatch_extrinsic(&self, _metadata: Option<Self::Metadata>, id: SubscriptionId) -> Result<bool> {
Ok(self.subscriptions.cancel(id))
}
}
+237
View File
@@ -0,0 +1,237 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use std::sync::Arc;
use assert_matches::assert_matches;
use codec::Encode;
use primitives::{
H256, blake2_256, hexdisplay::HexDisplay, testing::{ED25519, SR25519, KeyStore}, traits::BareCryptoStorePtr, ed25519,
crypto::Pair,
};
use rpc::futures::Stream as _;
use test_client::{
self, AccountKeyring, runtime::{Extrinsic, Transfer, SessionKeys, RuntimeApi, Block}, DefaultTestClientBuilderExt,
TestClientBuilderExt, Backend, Client, Executor
};
use transaction_pool::{
txpool::Pool,
FullChainApi,
};
use tokio::runtime;
fn uxt(sender: AccountKeyring, nonce: u64) -> Extrinsic {
let tx = Transfer {
amount: Default::default(),
nonce,
from: sender.into(),
to: Default::default(),
};
tx.into_signed_tx()
}
struct TestSetup {
pub runtime: runtime::Runtime,
pub client: Arc<Client<Backend>>,
pub keystore: BareCryptoStorePtr,
pub pool: Arc<Pool<FullChainApi<Client<Backend>, Block>>>,
}
impl Default for TestSetup {
fn default() -> Self {
let keystore = KeyStore::new();
let client = Arc::new(test_client::TestClientBuilder::new().set_keystore(keystore.clone()).build());
let pool = Arc::new(Pool::new(Default::default(), FullChainApi::new(client.clone())));
TestSetup {
runtime: runtime::Runtime::new().expect("Failed to create runtime in test setup"),
client,
keystore,
pool,
}
}
}
impl TestSetup {
fn author(&self) -> Author<Backend, Executor, FullChainApi<Client<Backend>, Block>, RuntimeApi> {
Author {
client: self.client.clone(),
pool: self.pool.clone(),
subscriptions: Subscriptions::new(Arc::new(self.runtime.executor())),
keystore: self.keystore.clone(),
}
}
}
#[test]
fn submit_transaction_should_not_cause_error() {
let p = TestSetup::default().author();
let xt = uxt(AccountKeyring::Alice, 1).encode();
let h: H256 = blake2_256(&xt).into();
assert_matches!(
AuthorApi::submit_extrinsic(&p, xt.clone().into()).wait(),
Ok(h2) if h == h2
);
assert!(
AuthorApi::submit_extrinsic(&p, xt.into()).wait().is_err()
);
}
#[test]
fn submit_rich_transaction_should_not_cause_error() {
let p = TestSetup::default().author();
let xt = uxt(AccountKeyring::Alice, 0).encode();
let h: H256 = blake2_256(&xt).into();
assert_matches!(
AuthorApi::submit_extrinsic(&p, xt.clone().into()).wait(),
Ok(h2) if h == h2
);
assert!(
AuthorApi::submit_extrinsic(&p, xt.into()).wait().is_err()
);
}
#[test]
fn should_watch_extrinsic() {
//given
let mut setup = TestSetup::default();
let p = setup.author();
let (subscriber, id_rx, data) = jsonrpc_pubsub::typed::Subscriber::new_test("test");
// when
p.watch_extrinsic(Default::default(), subscriber, uxt(AccountKeyring::Alice, 0).encode().into());
// then
assert_eq!(setup.runtime.block_on(id_rx), Ok(Ok(1.into())));
// check notifications
let replacement = {
let tx = Transfer {
amount: 5,
nonce: 0,
from: AccountKeyring::Alice.into(),
to: Default::default(),
};
tx.into_signed_tx()
};
AuthorApi::submit_extrinsic(&p, replacement.encode().into()).wait().unwrap();
let (res, data) = setup.runtime.block_on(data.into_future()).unwrap();
assert_eq!(
res,
Some(r#"{"jsonrpc":"2.0","method":"test","params":{"result":"ready","subscription":1}}"#.into())
);
let h = blake2_256(&replacement.encode());
assert_eq!(
setup.runtime.block_on(data.into_future()).unwrap().0,
Some(format!(r#"{{"jsonrpc":"2.0","method":"test","params":{{"result":{{"usurped":"0x{}"}},"subscription":1}}}}"#, HexDisplay::from(&h)))
);
}
#[test]
fn should_return_watch_validation_error() {
//given
let mut setup = TestSetup::default();
let p = setup.author();
let (subscriber, id_rx, _data) = jsonrpc_pubsub::typed::Subscriber::new_test("test");
// when
p.watch_extrinsic(Default::default(), subscriber, uxt(AccountKeyring::Alice, 179).encode().into());
// then
let res = setup.runtime.block_on(id_rx).unwrap();
assert!(res.is_err(), "Expected the transaction to be rejected as invalid.");
}
#[test]
fn should_return_pending_extrinsics() {
let p = TestSetup::default().author();
let ex = uxt(AccountKeyring::Alice, 0);
AuthorApi::submit_extrinsic(&p, ex.encode().into()).wait().unwrap();
assert_matches!(
p.pending_extrinsics(),
Ok(ref expected) if *expected == vec![Bytes(ex.encode())]
);
}
#[test]
fn should_remove_extrinsics() {
let setup = TestSetup::default();
let p = setup.author();
let ex1 = uxt(AccountKeyring::Alice, 0);
p.submit_extrinsic(ex1.encode().into()).wait().unwrap();
let ex2 = uxt(AccountKeyring::Alice, 1);
p.submit_extrinsic(ex2.encode().into()).wait().unwrap();
let ex3 = uxt(AccountKeyring::Bob, 0);
let hash3 = p.submit_extrinsic(ex3.encode().into()).wait().unwrap();
assert_eq!(setup.pool.status().ready, 3);
// now remove all 3
let removed = p.remove_extrinsic(vec![
hash::ExtrinsicOrHash::Hash(hash3),
// Removing this one will also remove ex2
hash::ExtrinsicOrHash::Extrinsic(ex1.encode().into()),
]).unwrap();
assert_eq!(removed.len(), 3);
}
#[test]
fn should_insert_key() {
let setup = TestSetup::default();
let p = setup.author();
let suri = "//Alice";
let key_pair = ed25519::Pair::from_string(suri, None).expect("Generates keypair");
p.insert_key(
String::from_utf8(ED25519.0.to_vec()).expect("Keytype is a valid string"),
suri.to_string(),
key_pair.public().0.to_vec().into(),
).expect("Insert key");
let store_key_pair = setup.keystore.read()
.ed25519_key_pair(ED25519, &key_pair.public()).expect("Key exists in store");
assert_eq!(key_pair.public(), store_key_pair.public());
}
#[test]
fn should_rotate_keys() {
let setup = TestSetup::default();
let p = setup.author();
let new_public_keys = p.rotate_keys().expect("Rotates the keys");
let session_keys = SessionKeys::decode(&mut &new_public_keys[..])
.expect("SessionKeys decode successfully");
let ed25519_key_pair = setup.keystore.read().ed25519_key_pair(
ED25519,
&session_keys.ed25519.clone().into(),
).expect("ed25519 key exists in store");
let sr25519_key_pair = setup.keystore.read().sr25519_key_pair(
SR25519,
&session_keys.sr25519.clone().into(),
).expect("sr25519 key exists in store");
assert_eq!(session_keys.ed25519, ed25519_key_pair.public().into());
assert_eq!(session_keys.sr25519, sr25519_key_pair.public().into());
}
@@ -0,0 +1,80 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Blockchain API backend for full nodes.
use std::sync::Arc;
use rpc::futures::future::result;
use api::Subscriptions;
use client_api::{CallExecutor, backend::Backend};
use client::Client;
use primitives::{H256, Blake2Hasher};
use sr_primitives::{
generic::{BlockId, SignedBlock},
traits::{Block as BlockT},
};
use super::{ChainBackend, client_err, error::FutureResult};
/// Blockchain API backend for full nodes. Reads all the data from local database.
pub struct FullChain<B, E, Block: BlockT, RA> {
/// Substrate client.
client: Arc<Client<B, E, Block, RA>>,
/// Current subscriptions.
subscriptions: Subscriptions,
}
impl<B, E, Block: BlockT, RA> FullChain<B, E, Block, RA> {
/// Create new Chain API RPC handler.
pub fn new(client: Arc<Client<B, E, Block, RA>>, subscriptions: Subscriptions) -> Self {
Self {
client,
subscriptions,
}
}
}
impl<B, E, Block, RA> ChainBackend<B, E, Block, RA> for FullChain<B, E, Block, RA> where
Block: BlockT<Hash=H256> + 'static,
B: Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static,
RA: Send + Sync + 'static,
{
fn client(&self) -> &Arc<Client<B, E, Block, RA>> {
&self.client
}
fn subscriptions(&self) -> &Subscriptions {
&self.subscriptions
}
fn header(&self, hash: Option<Block::Hash>) -> FutureResult<Option<Block::Header>> {
Box::new(result(self.client
.header(&BlockId::Hash(self.unwrap_or_best(hash)))
.map_err(client_err)
))
}
fn block(&self, hash: Option<Block::Hash>)
-> FutureResult<Option<SignedBlock<Block>>>
{
Box::new(result(self.client
.block(&BlockId::Hash(self.unwrap_or_best(hash)))
.map_err(client_err)
))
}
}
@@ -0,0 +1,123 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Blockchain API backend for light nodes.
use std::sync::Arc;
use futures03::{future::ready, FutureExt, TryFutureExt};
use rpc::futures::future::{result, Future, Either};
use api::Subscriptions;
use client::{
self, Client,
light::{
fetcher::{Fetcher, RemoteBodyRequest},
blockchain::RemoteBlockchain,
},
};
use primitives::{H256, Blake2Hasher};
use sr_primitives::{
generic::{BlockId, SignedBlock},
traits::{Block as BlockT},
};
use super::{ChainBackend, client_err, error::FutureResult};
/// Blockchain API backend for light nodes. Reads all the data from local
/// database, if available, or fetches it from remote node otherwise.
pub struct LightChain<B, E, Block: BlockT, RA, F> {
/// Substrate client.
client: Arc<Client<B, E, Block, RA>>,
/// Current subscriptions.
subscriptions: Subscriptions,
/// Remote blockchain reference
remote_blockchain: Arc<dyn RemoteBlockchain<Block>>,
/// Remote fetcher reference.
fetcher: Arc<F>,
}
impl<B, E, Block: BlockT, RA, F: Fetcher<Block>> LightChain<B, E, Block, RA, F> {
/// Create new Chain API RPC handler.
pub fn new(
client: Arc<Client<B, E, Block, RA>>,
subscriptions: Subscriptions,
remote_blockchain: Arc<dyn RemoteBlockchain<Block>>,
fetcher: Arc<F>,
) -> Self {
Self {
client,
subscriptions,
remote_blockchain,
fetcher,
}
}
}
impl<B, E, Block, RA, F> ChainBackend<B, E, Block, RA> for LightChain<B, E, Block, RA, F> where
Block: BlockT<Hash=H256> + 'static,
B: client_api::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: client::CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static,
RA: Send + Sync + 'static,
F: Fetcher<Block> + Send + Sync + 'static,
{
fn client(&self) -> &Arc<Client<B, E, Block, RA>> {
&self.client
}
fn subscriptions(&self) -> &Subscriptions {
&self.subscriptions
}
fn header(&self, hash: Option<Block::Hash>) -> FutureResult<Option<Block::Header>> {
let hash = self.unwrap_or_best(hash);
let fetcher = self.fetcher.clone();
let maybe_header = client::light::blockchain::future_header(
&*self.remote_blockchain,
&*fetcher,
BlockId::Hash(hash),
);
Box::new(maybe_header.then(move |result|
ready(result.map_err(client_err)),
).boxed().compat())
}
fn block(&self, hash: Option<Block::Hash>)
-> FutureResult<Option<SignedBlock<Block>>>
{
let fetcher = self.fetcher.clone();
let block = self.header(hash)
.and_then(move |header| match header {
Some(header) => Either::A(fetcher
.remote_body(RemoteBodyRequest {
header: header.clone(),
retry_count: Default::default(),
})
.boxed()
.compat()
.map(move |body| Some(SignedBlock {
block: Block::new(header, body),
justification: None,
}))
.map_err(client_err)
),
None => Either::B(result(Ok(None))),
});
Box::new(block)
}
}
+282
View File
@@ -0,0 +1,282 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Substrate blockchain API.
mod chain_full;
mod chain_light;
#[cfg(test)]
mod tests;
use std::sync::Arc;
use futures03::{future, StreamExt as _, TryStreamExt as _};
use log::warn;
use rpc::{
Result as RpcResult,
futures::{stream, Future, Sink, Stream},
};
use api::Subscriptions;
use client::{
self, Client, BlockchainEvents,
light::{fetcher::Fetcher, blockchain::RemoteBlockchain},
};
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use primitives::{H256, Blake2Hasher};
use rpc_primitives::number;
use sr_primitives::{
generic::{BlockId, SignedBlock},
traits::{Block as BlockT, Header, NumberFor},
};
use self::error::{Result, Error, FutureResult};
pub use api::chain::*;
/// Blockchain backend API
trait ChainBackend<B, E, Block: BlockT, RA>: Send + Sync + 'static
where
Block: BlockT<Hash=H256> + 'static,
B: client_api::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: client::CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static,
{
/// Get client reference.
fn client(&self) -> &Arc<Client<B, E, Block, RA>>;
/// Get subscriptions reference.
fn subscriptions(&self) -> &Subscriptions;
/// Tries to unwrap passed block hash, or uses best block hash otherwise.
fn unwrap_or_best(&self, hash: Option<Block::Hash>) -> Block::Hash {
match hash.into() {
None => self.client().info().chain.best_hash,
Some(hash) => hash,
}
}
/// Get header of a relay chain block.
fn header(&self, hash: Option<Block::Hash>) -> FutureResult<Option<Block::Header>>;
/// Get header and body of a relay chain block.
fn block(&self, hash: Option<Block::Hash>) -> FutureResult<Option<SignedBlock<Block>>>;
/// Get hash of the n-th block in the canon chain.
///
/// By default returns latest block hash.
fn block_hash(
&self,
number: Option<number::NumberOrHex<NumberFor<Block>>>,
) -> Result<Option<Block::Hash>> {
Ok(match number {
None => Some(self.client().info().chain.best_hash),
Some(num_or_hex) => self.client()
.header(&BlockId::number(num_or_hex.to_number()?))
.map_err(client_err)?
.map(|h| h.hash()),
})
}
/// Get hash of the last finalized block in the canon chain.
fn finalized_head(&self) -> Result<Block::Hash> {
Ok(self.client().info().chain.finalized_hash)
}
/// New head subscription
fn subscribe_new_heads(
&self,
_metadata: crate::metadata::Metadata,
subscriber: Subscriber<Block::Header>,
) {
subscribe_headers(
self.client(),
self.subscriptions(),
subscriber,
|| self.client().info().chain.best_hash,
|| self.client().import_notification_stream()
.filter(|notification| future::ready(notification.is_new_best))
.map(|notification| Ok::<_, ()>(notification.header))
.compat(),
)
}
/// Unsubscribe from new head subscription.
fn unsubscribe_new_heads(
&self,
_metadata: Option<crate::metadata::Metadata>,
id: SubscriptionId,
) -> RpcResult<bool> {
Ok(self.subscriptions().cancel(id))
}
/// New head subscription
fn subscribe_finalized_heads(
&self,
_metadata: crate::metadata::Metadata,
subscriber: Subscriber<Block::Header>,
) {
subscribe_headers(
self.client(),
self.subscriptions(),
subscriber,
|| self.client().info().chain.finalized_hash,
|| self.client().finality_notification_stream()
.map(|notification| Ok::<_, ()>(notification.header))
.compat(),
)
}
/// Unsubscribe from new head subscription.
fn unsubscribe_finalized_heads(
&self,
_metadata: Option<crate::metadata::Metadata>,
id: SubscriptionId,
) -> RpcResult<bool> {
Ok(self.subscriptions().cancel(id))
}
}
/// Create new state API that works on full node.
pub fn new_full<B, E, Block: BlockT, RA>(
client: Arc<Client<B, E, Block, RA>>,
subscriptions: Subscriptions,
) -> Chain<B, E, Block, RA>
where
Block: BlockT<Hash=H256> + 'static,
B: client_api::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: client::CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
{
Chain {
backend: Box::new(self::chain_full::FullChain::new(client, subscriptions)),
}
}
/// Create new state API that works on light node.
pub fn new_light<B, E, Block: BlockT, RA, F: Fetcher<Block>>(
client: Arc<Client<B, E, Block, RA>>,
subscriptions: Subscriptions,
remote_blockchain: Arc<dyn RemoteBlockchain<Block>>,
fetcher: Arc<F>,
) -> Chain<B, E, Block, RA>
where
Block: BlockT<Hash=H256> + 'static,
B: client_api::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: client::CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
F: Send + Sync + 'static,
{
Chain {
backend: Box::new(self::chain_light::LightChain::new(
client,
subscriptions,
remote_blockchain,
fetcher,
)),
}
}
/// Chain API with subscriptions support.
pub struct Chain<B, E, Block: BlockT, RA> {
backend: Box<dyn ChainBackend<B, E, Block, RA>>,
}
impl<B, E, Block, RA> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, SignedBlock<Block>> for Chain<B, E, Block, RA> where
Block: BlockT<Hash=H256> + 'static,
B: client_api::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: client::CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static,
RA: Send + Sync + 'static
{
type Metadata = crate::metadata::Metadata;
fn header(&self, hash: Option<Block::Hash>) -> FutureResult<Option<Block::Header>> {
self.backend.header(hash)
}
fn block(&self, hash: Option<Block::Hash>) -> FutureResult<Option<SignedBlock<Block>>>
{
self.backend.block(hash)
}
fn block_hash(&self, number: Option<number::NumberOrHex<NumberFor<Block>>>) -> Result<Option<Block::Hash>> {
self.backend.block_hash(number)
}
fn finalized_head(&self) -> Result<Block::Hash> {
self.backend.finalized_head()
}
fn subscribe_new_heads(&self, metadata: Self::Metadata, subscriber: Subscriber<Block::Header>) {
self.backend.subscribe_new_heads(metadata, subscriber)
}
fn unsubscribe_new_heads(&self, metadata: Option<Self::Metadata>, id: SubscriptionId) -> RpcResult<bool> {
self.backend.unsubscribe_new_heads(metadata, id)
}
fn subscribe_finalized_heads(&self, metadata: Self::Metadata, subscriber: Subscriber<Block::Header>) {
self.backend.subscribe_finalized_heads(metadata, subscriber)
}
fn unsubscribe_finalized_heads(&self, metadata: Option<Self::Metadata>, id: SubscriptionId) -> RpcResult<bool> {
self.backend.unsubscribe_finalized_heads(metadata, id)
}
}
/// Subscribe to new headers.
fn subscribe_headers<B, E, Block, RA, F, G, S, ERR>(
client: &Arc<Client<B, E, Block, RA>>,
subscriptions: &Subscriptions,
subscriber: Subscriber<Block::Header>,
best_block_hash: G,
stream: F,
) where
Block: BlockT<Hash=H256> + 'static,
B: client_api::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: client::CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static,
F: FnOnce() -> S,
G: FnOnce() -> Block::Hash,
ERR: ::std::fmt::Debug,
S: Stream<Item=Block::Header, Error=ERR> + Send + 'static,
{
subscriptions.add(subscriber, |sink| {
// send current head right at the start.
let header = client.header(&BlockId::Hash(best_block_hash()))
.map_err(client_err)
.and_then(|header| {
header.ok_or_else(|| "Best header missing.".to_owned().into())
})
.map_err(Into::into);
// send further subscriptions
let stream = stream()
.map(|res| Ok(res))
.map_err(|e| warn!("Block notification stream error: {:?}", e));
sink
.sink_map_err(|e| warn!("Error sending notifications: {:?}", e))
.send_all(
stream::iter_result(vec![Ok(header)])
.chain(stream)
)
// we ignore the resulting Stream (if the first stream is over we are unsubscribed)
.map(|_| ())
});
}
fn client_err(err: client::error::Error) -> Error {
Error::Client(Box::new(err))
}
+242
View File
@@ -0,0 +1,242 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use assert_matches::assert_matches;
use test_client::{
prelude::*,
consensus::BlockOrigin,
runtime::{H256, Block, Header},
};
#[test]
fn should_return_header() {
let core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let client = Arc::new(test_client::new());
let api = new_full(client.clone(), Subscriptions::new(Arc::new(remote)));
assert_matches!(
api.header(Some(client.genesis_hash()).into()).wait(),
Ok(Some(ref x)) if x == &Header {
parent_hash: H256::from_low_u64_be(0),
number: 0,
state_root: x.state_root.clone(),
extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314".parse().unwrap(),
digest: Default::default(),
}
);
assert_matches!(
api.header(None.into()).wait(),
Ok(Some(ref x)) if x == &Header {
parent_hash: H256::from_low_u64_be(0),
number: 0,
state_root: x.state_root.clone(),
extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314".parse().unwrap(),
digest: Default::default(),
}
);
assert_matches!(
api.header(Some(H256::from_low_u64_be(5)).into()).wait(),
Ok(None)
);
}
#[test]
fn should_return_a_block() {
let core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let client = Arc::new(test_client::new());
let api = new_full(client.clone(), Subscriptions::new(Arc::new(remote)));
let block = client.new_block(Default::default()).unwrap().bake().unwrap();
let block_hash = block.hash();
client.import(BlockOrigin::Own, block).unwrap();
// Genesis block is not justified
assert_matches!(
api.block(Some(client.genesis_hash()).into()).wait(),
Ok(Some(SignedBlock { justification: None, .. }))
);
assert_matches!(
api.block(Some(block_hash).into()).wait(),
Ok(Some(ref x)) if x.block == Block {
header: Header {
parent_hash: client.genesis_hash(),
number: 1,
state_root: x.block.header.state_root.clone(),
extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314".parse().unwrap(),
digest: Default::default(),
},
extrinsics: vec![],
}
);
assert_matches!(
api.block(None.into()).wait(),
Ok(Some(ref x)) if x.block == Block {
header: Header {
parent_hash: client.genesis_hash(),
number: 1,
state_root: x.block.header.state_root.clone(),
extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314".parse().unwrap(),
digest: Default::default(),
},
extrinsics: vec![],
}
);
assert_matches!(
api.block(Some(H256::from_low_u64_be(5)).into()).wait(),
Ok(None)
);
}
#[test]
fn should_return_block_hash() {
let core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let client = Arc::new(test_client::new());
let api = new_full(client.clone(), Subscriptions::new(Arc::new(remote)));
assert_matches!(
api.block_hash(None.into()),
Ok(Some(ref x)) if x == &client.genesis_hash()
);
assert_matches!(
api.block_hash(Some(0u64.into()).into()),
Ok(Some(ref x)) if x == &client.genesis_hash()
);
assert_matches!(
api.block_hash(Some(1u64.into()).into()),
Ok(None)
);
let block = client.new_block(Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, block.clone()).unwrap();
assert_matches!(
api.block_hash(Some(0u64.into()).into()),
Ok(Some(ref x)) if x == &client.genesis_hash()
);
assert_matches!(
api.block_hash(Some(1u64.into()).into()),
Ok(Some(ref x)) if x == &block.hash()
);
assert_matches!(
api.block_hash(Some(::primitives::U256::from(1u64).into()).into()),
Ok(Some(ref x)) if x == &block.hash()
);
}
#[test]
fn should_return_finalized_hash() {
let core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let client = Arc::new(test_client::new());
let api = new_full(client.clone(), Subscriptions::new(Arc::new(remote)));
assert_matches!(
api.finalized_head(),
Ok(ref x) if x == &client.genesis_hash()
);
// import new block
let builder = client.new_block(Default::default()).unwrap();
client.import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
// no finalization yet
assert_matches!(
api.finalized_head(),
Ok(ref x) if x == &client.genesis_hash()
);
// finalize
client.finalize_block(BlockId::number(1), None).unwrap();
assert_matches!(
api.finalized_head(),
Ok(ref x) if x == &client.block_hash(1).unwrap().unwrap()
);
}
#[test]
fn should_notify_about_latest_block() {
let mut core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let (subscriber, id, transport) = Subscriber::new_test("test");
{
let client = Arc::new(test_client::new());
let api = new_full(client.clone(), Subscriptions::new(Arc::new(remote)));
api.subscribe_new_heads(Default::default(), subscriber);
// assert id assigned
assert_eq!(core.block_on(id), Ok(Ok(SubscriptionId::Number(1))));
let builder = client.new_block(Default::default()).unwrap();
client.import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
}
// assert initial head sent.
let (notification, next) = core.block_on(transport.into_future()).unwrap();
assert!(notification.is_some());
// assert notification sent to transport
let (notification, next) = core.block_on(next.into_future()).unwrap();
assert!(notification.is_some());
// no more notifications on this channel
assert_eq!(core.block_on(next.into_future()).unwrap().0, None);
}
#[test]
fn should_notify_about_finalized_block() {
let mut core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let (subscriber, id, transport) = Subscriber::new_test("test");
{
let client = Arc::new(test_client::new());
let api = new_full(client.clone(), Subscriptions::new(Arc::new(remote)));
api.subscribe_finalized_heads(Default::default(), subscriber);
// assert id assigned
assert_eq!(core.block_on(id), Ok(Ok(SubscriptionId::Number(1))));
let builder = client.new_block(Default::default()).unwrap();
client.import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
client.finalize_block(BlockId::number(1), None).unwrap();
}
// assert initial head sent.
let (notification, next) = core.block_on(transport.into_future()).unwrap();
assert!(notification.is_some());
// assert notification sent to transport
let (notification, next) = core.block_on(next.into_future()).unwrap();
assert!(notification.is_some());
// no more notifications on this channel
assert_eq!(core.block_on(next.into_future()).unwrap().0, None);
}
+32
View File
@@ -0,0 +1,32 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Substrate RPC implementation.
//!
//! A core implementation of Substrate RPC interfaces.
#![warn(missing_docs)]
mod metadata;
pub use api::Subscriptions;
pub use self::metadata::Metadata;
pub use rpc::IoHandlerExtension as RpcExtension;
pub mod author;
pub mod chain;
pub mod state;
pub mod system;
+60
View File
@@ -0,0 +1,60 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! RPC Metadata
use std::sync::Arc;
use jsonrpc_pubsub::{Session, PubSubMetadata};
use rpc::futures::sync::mpsc;
/// RPC Metadata.
///
/// Manages persistent session for transports that support it
/// and may contain some additional info extracted from specific transports
/// (like remote client IP address, request headers, etc)
#[derive(Default, Clone)]
pub struct Metadata {
session: Option<Arc<Session>>,
}
impl rpc::Metadata for Metadata {}
impl PubSubMetadata for Metadata {
fn session(&self) -> Option<Arc<Session>> {
self.session.clone()
}
}
impl Metadata {
/// Create new `Metadata` with session (Pub/Sub) support.
pub fn new(transport: mpsc::Sender<String>) -> Self {
Metadata {
session: Some(Arc::new(Session::new(transport))),
}
}
/// Create new `Metadata` for tests.
#[cfg(test)]
pub fn new_test() -> (mpsc::Receiver<String>, Self) {
let (tx, rx) = mpsc::channel(1);
(rx, Self::new(tx))
}
}
impl From<mpsc::Sender<String>> for Metadata {
fn from(sender: mpsc::Sender<String>) -> Self {
Self::new(sender)
}
}
+337
View File
@@ -0,0 +1,337 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Substrate state API.
mod state_full;
mod state_light;
#[cfg(test)]
mod tests;
use std::sync::Arc;
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use rpc::{Result as RpcResult, futures::Future};
use api::Subscriptions;
use client::{Client, CallExecutor, light::{blockchain::RemoteBlockchain, fetcher::Fetcher}};
use primitives::{
Blake2Hasher, Bytes, H256,
storage::{StorageKey, StorageData, StorageChangeSet},
};
use runtime_version::RuntimeVersion;
use sr_primitives::{
traits::{Block as BlockT, ProvideRuntimeApi},
};
use sr_api::Metadata;
use self::error::{Error, FutureResult};
pub use api::state::*;
/// State backend API.
pub trait StateBackend<B, E, Block: BlockT, RA>: Send + Sync + 'static
where
Block: BlockT<Hash=H256> + 'static,
B: client_api::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: client::CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static,
RA: Send + Sync + 'static,
{
/// Call runtime method at given block.
fn call(
&self,
block: Option<Block::Hash>,
method: String,
call_data: Bytes,
) -> FutureResult<Bytes>;
/// Returns the keys with prefix, leave empty to get all the keys.
fn storage_keys(
&self,
block: Option<Block::Hash>,
prefix: StorageKey,
) -> FutureResult<Vec<StorageKey>>;
/// Returns a storage entry at a specific block's state.
fn storage(
&self,
block: Option<Block::Hash>,
key: StorageKey,
) -> FutureResult<Option<StorageData>>;
/// Returns the hash of a storage entry at a block's state.
fn storage_hash(
&self,
block: Option<Block::Hash>,
key: StorageKey,
) -> FutureResult<Option<Block::Hash>>;
/// Returns the size of a storage entry at a block's state.
fn storage_size(
&self,
block: Option<Block::Hash>,
key: StorageKey,
) -> FutureResult<Option<u64>> {
Box::new(self.storage(block, key)
.map(|x| x.map(|x| x.0.len() as u64)))
}
/// Returns the keys with prefix from a child storage, leave empty to get all the keys
fn child_storage_keys(
&self,
block: Option<Block::Hash>,
child_storage_key: StorageKey,
prefix: StorageKey,
) -> FutureResult<Vec<StorageKey>>;
/// Returns a child storage entry at a specific block's state.
fn child_storage(
&self,
block: Option<Block::Hash>,
child_storage_key: StorageKey,
key: StorageKey,
) -> FutureResult<Option<StorageData>>;
/// Returns the hash of a child storage entry at a block's state.
fn child_storage_hash(
&self,
block: Option<Block::Hash>,
child_storage_key: StorageKey,
key: StorageKey,
) -> FutureResult<Option<Block::Hash>>;
/// Returns the size of a child storage entry at a block's state.
fn child_storage_size(
&self,
block: Option<Block::Hash>,
child_storage_key: StorageKey,
key: StorageKey,
) -> FutureResult<Option<u64>> {
Box::new(self.child_storage(block, child_storage_key, key)
.map(|x| x.map(|x| x.0.len() as u64)))
}
/// Returns the runtime metadata as an opaque blob.
fn metadata(&self, block: Option<Block::Hash>) -> FutureResult<Bytes>;
/// Get the runtime version.
fn runtime_version(&self, block: Option<Block::Hash>) -> FutureResult<RuntimeVersion>;
/// Query historical storage entries (by key) starting from a block given as the second parameter.
///
/// NOTE This first returned result contains the initial state of storage for all keys.
/// Subsequent values in the vector represent changes to the previous state (diffs).
fn query_storage(
&self,
from: Block::Hash,
to: Option<Block::Hash>,
keys: Vec<StorageKey>,
) -> FutureResult<Vec<StorageChangeSet<Block::Hash>>>;
/// New runtime version subscription
fn subscribe_runtime_version(
&self,
_meta: crate::metadata::Metadata,
subscriber: Subscriber<RuntimeVersion>,
);
/// Unsubscribe from runtime version subscription
fn unsubscribe_runtime_version(
&self,
_meta: Option<crate::metadata::Metadata>,
id: SubscriptionId,
) -> RpcResult<bool>;
/// New storage subscription
fn subscribe_storage(
&self,
_meta: crate::metadata::Metadata,
subscriber: Subscriber<StorageChangeSet<Block::Hash>>,
keys: Option<Vec<StorageKey>>,
);
/// Unsubscribe from storage subscription
fn unsubscribe_storage(
&self,
_meta: Option<crate::metadata::Metadata>,
id: SubscriptionId,
) -> RpcResult<bool>;
}
/// Create new state API that works on full node.
pub fn new_full<B, E, Block: BlockT, RA>(
client: Arc<Client<B, E, Block, RA>>,
subscriptions: Subscriptions,
) -> State<B, E, Block, RA>
where
Block: BlockT<Hash=H256> + 'static,
B: client_api::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
Client<B, E, Block, RA>: ProvideRuntimeApi,
<Client<B, E, Block, RA> as ProvideRuntimeApi>::Api:
Metadata<Block, Error = client::error::Error>,
{
State {
backend: Box::new(self::state_full::FullState::new(client, subscriptions)),
}
}
/// Create new state API that works on light node.
pub fn new_light<B, E, Block: BlockT, RA, F: Fetcher<Block>>(
client: Arc<Client<B, E, Block, RA>>,
subscriptions: Subscriptions,
remote_blockchain: Arc<dyn RemoteBlockchain<Block>>,
fetcher: Arc<F>,
) -> State<B, E, Block, RA>
where
Block: BlockT<Hash=H256> + 'static,
B: client_api::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
F: Send + Sync + 'static,
{
State {
backend: Box::new(self::state_light::LightState::new(
client,
subscriptions,
remote_blockchain,
fetcher,
)),
}
}
/// State API with subscriptions support.
pub struct State<B, E, Block, RA> {
backend: Box<dyn StateBackend<B, E, Block, RA>>,
}
impl<B, E, Block, RA> StateApi<Block::Hash> for State<B, E, Block, RA>
where
Block: BlockT<Hash=H256> + 'static,
B: client_api::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
{
type Metadata = crate::metadata::Metadata;
fn call(&self, method: String, data: Bytes, block: Option<Block::Hash>) -> FutureResult<Bytes> {
self.backend.call(block, method, data)
}
fn storage_keys(
&self,
key_prefix: StorageKey,
block: Option<Block::Hash>,
) -> FutureResult<Vec<StorageKey>> {
self.backend.storage_keys(block, key_prefix)
}
fn storage(&self, key: StorageKey, block: Option<Block::Hash>) -> FutureResult<Option<StorageData>> {
self.backend.storage(block, key)
}
fn storage_hash(&self, key: StorageKey, block: Option<Block::Hash>) -> FutureResult<Option<Block::Hash>> {
self.backend.storage_hash(block, key)
}
fn storage_size(&self, key: StorageKey, block: Option<Block::Hash>) -> FutureResult<Option<u64>> {
self.backend.storage_size(block, key)
}
fn child_storage(
&self,
child_storage_key: StorageKey,
key: StorageKey,
block: Option<Block::Hash>
) -> FutureResult<Option<StorageData>> {
self.backend.child_storage(block, child_storage_key, key)
}
fn child_storage_keys(
&self,
child_storage_key: StorageKey,
key_prefix: StorageKey,
block: Option<Block::Hash>
) -> FutureResult<Vec<StorageKey>> {
self.backend.child_storage_keys(block, child_storage_key, key_prefix)
}
fn child_storage_hash(
&self,
child_storage_key: StorageKey,
key: StorageKey,
block: Option<Block::Hash>
) -> FutureResult<Option<Block::Hash>> {
self.backend.child_storage_hash(block, child_storage_key, key)
}
fn child_storage_size(
&self,
child_storage_key: StorageKey,
key: StorageKey,
block: Option<Block::Hash>
) -> FutureResult<Option<u64>> {
self.backend.child_storage_size(block, child_storage_key, key)
}
fn metadata(&self, block: Option<Block::Hash>) -> FutureResult<Bytes> {
self.backend.metadata(block)
}
fn query_storage(
&self,
keys: Vec<StorageKey>,
from: Block::Hash,
to: Option<Block::Hash>
) -> FutureResult<Vec<StorageChangeSet<Block::Hash>>> {
self.backend.query_storage(from, to, keys)
}
fn subscribe_storage(
&self,
meta: Self::Metadata,
subscriber: Subscriber<StorageChangeSet<Block::Hash>>,
keys: Option<Vec<StorageKey>>
) {
self.backend.subscribe_storage(meta, subscriber, keys);
}
fn unsubscribe_storage(&self, meta: Option<Self::Metadata>, id: SubscriptionId) -> RpcResult<bool> {
self.backend.unsubscribe_storage(meta, id)
}
fn runtime_version(&self, at: Option<Block::Hash>) -> FutureResult<RuntimeVersion> {
self.backend.runtime_version(at)
}
fn subscribe_runtime_version(&self, meta: Self::Metadata, subscriber: Subscriber<RuntimeVersion>) {
self.backend.subscribe_runtime_version(meta, subscriber);
}
fn unsubscribe_runtime_version(
&self,
meta: Option<Self::Metadata>,
id: SubscriptionId,
) -> RpcResult<bool> {
self.backend.unsubscribe_runtime_version(meta, id)
}
}
fn client_err(err: client::error::Error) -> Error {
Error::Client(Box::new(err))
}
@@ -0,0 +1,514 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! State API backend for full nodes.
use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;
use std::ops::Range;
use futures03::{future, StreamExt as _, TryStreamExt as _};
use log::warn;
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use rpc::{
Result as RpcResult,
futures::{stream, Future, Sink, Stream, future::result},
};
use api::Subscriptions;
use client_api::{backend::Backend, error::Result as ClientResult};
use client::{
Client, CallExecutor, BlockchainEvents,
};
use primitives::{
H256, Blake2Hasher, Bytes, storage::{well_known_keys, StorageKey, StorageData, StorageChangeSet},
};
use runtime_version::RuntimeVersion;
use state_machine::ExecutionStrategy;
use sr_primitives::{
generic::BlockId,
traits::{Block as BlockT, Header, NumberFor, ProvideRuntimeApi, SaturatedConversion},
};
use sr_api::Metadata;
use super::{StateBackend, error::{FutureResult, Error, Result}, client_err};
/// Ranges to query in state_queryStorage.
struct QueryStorageRange<Block: BlockT> {
/// Hashes of all the blocks in the range.
pub hashes: Vec<Block::Hash>,
/// Number of the first block in the range.
pub first_number: NumberFor<Block>,
/// Blocks subrange ([begin; end) indices within `hashes`) where we should read keys at
/// each state to get changes.
pub unfiltered_range: Range<usize>,
/// Blocks subrange ([begin; end) indices within `hashes`) where we could pre-filter
/// blocks-with-changes by using changes tries.
pub filtered_range: Option<Range<usize>>,
}
/// State API backend for full nodes.
pub struct FullState<B, E, Block: BlockT, RA> {
client: Arc<Client<B, E, Block, RA>>,
subscriptions: Subscriptions,
}
impl<B, E, Block: BlockT, RA> FullState<B, E, Block, RA>
where
Block: BlockT<Hash=H256> + 'static,
B: Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static + Clone,
{
/// Create new state API backend for full nodes.
pub fn new(client: Arc<Client<B, E, Block, RA>>, subscriptions: Subscriptions) -> Self {
Self { client, subscriptions }
}
/// Returns given block hash or best block hash if None is passed.
fn block_or_best(&self, hash: Option<Block::Hash>) -> ClientResult<Block::Hash> {
Ok(hash.unwrap_or_else(|| self.client.info().chain.best_hash))
}
/// Splits the `query_storage` block range into 'filtered' and 'unfiltered' subranges.
/// Blocks that contain changes within filtered subrange could be filtered using changes tries.
/// Blocks that contain changes within unfiltered subrange must be filtered manually.
fn split_query_storage_range(
&self,
from: Block::Hash,
to: Option<Block::Hash>
) -> Result<QueryStorageRange<Block>> {
let to = self.block_or_best(to).map_err(client_err)?;
let from_hdr = self.client.header(&BlockId::hash(from)).map_err(client_err)?;
let to_hdr = self.client.header(&BlockId::hash(to)).map_err(client_err)?;
match (from_hdr, to_hdr) {
(Some(ref from), Some(ref to)) if from.number() <= to.number() => {
// check if we can get from `to` to `from` by going through parent_hashes.
let from_number = *from.number();
let blocks = {
let mut blocks = vec![to.hash()];
let mut last = to.clone();
while *last.number() > from_number {
let hdr = self.client
.header(&BlockId::hash(*last.parent_hash()))
.map_err(client_err)?;
if let Some(hdr) = hdr {
blocks.push(hdr.hash());
last = hdr;
} else {
return Err(invalid_block_range(
Some(from),
Some(to),
format!("Parent of {} ({}) not found", last.number(), last.hash()),
))
}
}
if last.hash() != from.hash() {
return Err(invalid_block_range(
Some(from),
Some(to),
format!("Expected to reach `from`, got {} ({})", last.number(), last.hash()),
))
}
blocks.reverse();
blocks
};
// check if we can filter blocks-with-changes from some (sub)range using changes tries
let changes_trie_range = self.client
.max_key_changes_range(from_number, BlockId::Hash(to.hash()))
.map_err(client_err)?;
let filtered_range_begin = changes_trie_range
.map(|(begin, _)| (begin - from_number).saturated_into::<usize>());
let (unfiltered_range, filtered_range) = split_range(blocks.len(), filtered_range_begin);
Ok(QueryStorageRange {
hashes: blocks,
first_number: from_number,
unfiltered_range,
filtered_range,
})
},
(from, to) => Err(
invalid_block_range(from.as_ref(), to.as_ref(), "Invalid range or unknown block".into())
),
}
}
/// Iterates through range.unfiltered_range and check each block for changes of keys' values.
fn query_storage_unfiltered(
&self,
range: &QueryStorageRange<Block>,
keys: &[StorageKey],
last_values: &mut HashMap<StorageKey, Option<StorageData>>,
changes: &mut Vec<StorageChangeSet<Block::Hash>>,
) -> Result<()> {
for block in range.unfiltered_range.start..range.unfiltered_range.end {
let block_hash = range.hashes[block].clone();
let mut block_changes = StorageChangeSet { block: block_hash.clone(), changes: Vec::new() };
let id = BlockId::hash(block_hash);
for key in keys {
let (has_changed, data) = {
let curr_data = self.client.storage(&id, key).map_err(client_err)?;
match last_values.get(key) {
Some(prev_data) => (curr_data != *prev_data, curr_data),
None => (true, curr_data),
}
};
if has_changed {
block_changes.changes.push((key.clone(), data.clone()));
}
last_values.insert(key.clone(), data);
}
if !block_changes.changes.is_empty() {
changes.push(block_changes);
}
}
Ok(())
}
/// Iterates through all blocks that are changing keys within range.filtered_range and collects these changes.
fn query_storage_filtered(
&self,
range: &QueryStorageRange<Block>,
keys: &[StorageKey],
last_values: &HashMap<StorageKey, Option<StorageData>>,
changes: &mut Vec<StorageChangeSet<Block::Hash>>,
) -> Result<()> {
let (begin, end) = match range.filtered_range {
Some(ref filtered_range) => (
range.first_number + filtered_range.start.saturated_into(),
BlockId::Hash(range.hashes[filtered_range.end - 1].clone())
),
None => return Ok(()),
};
let mut changes_map: BTreeMap<NumberFor<Block>, StorageChangeSet<Block::Hash>> = BTreeMap::new();
for key in keys {
let mut last_block = None;
let mut last_value = last_values.get(key).cloned().unwrap_or_default();
let key_changes = self.client.key_changes(begin, end, None, key).map_err(client_err)?;
for (block, _) in key_changes.into_iter().rev() {
if last_block == Some(block) {
continue;
}
let block_hash = range.hashes[(block - range.first_number).saturated_into::<usize>()].clone();
let id = BlockId::Hash(block_hash);
let value_at_block = self.client.storage(&id, key).map_err(client_err)?;
if last_value == value_at_block {
continue;
}
changes_map.entry(block)
.or_insert_with(|| StorageChangeSet { block: block_hash, changes: Vec::new() })
.changes.push((key.clone(), value_at_block.clone()));
last_block = Some(block);
last_value = value_at_block;
}
}
if let Some(additional_capacity) = changes_map.len().checked_sub(changes.len()) {
changes.reserve(additional_capacity);
}
changes.extend(changes_map.into_iter().map(|(_, cs)| cs));
Ok(())
}
}
impl<B, E, Block, RA> StateBackend<B, E, Block, RA> for FullState<B, E, Block, RA>
where
Block: BlockT<Hash=H256> + 'static,
B: Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
Client<B, E, Block, RA>: ProvideRuntimeApi,
<Client<B, E, Block, RA> as ProvideRuntimeApi>::Api:
Metadata<Block, Error = client::error::Error>,
{
fn call(
&self,
block: Option<Block::Hash>,
method: String,
call_data: Bytes,
) -> FutureResult<Bytes> {
Box::new(result(
self.block_or_best(block)
.and_then(|block|
self
.client
.executor()
.call(
&BlockId::Hash(block),
&method,
&*call_data,
ExecutionStrategy::NativeElseWasm,
None,
)
.map(Into::into))
.map_err(client_err)))
}
fn storage_keys(
&self,
block: Option<Block::Hash>,
prefix: StorageKey,
) -> FutureResult<Vec<StorageKey>> {
Box::new(result(
self.block_or_best(block)
.and_then(|block| self.client.storage_keys(&BlockId::Hash(block), &prefix))
.map_err(client_err)))
}
fn storage(
&self,
block: Option<Block::Hash>,
key: StorageKey,
) -> FutureResult<Option<StorageData>> {
Box::new(result(
self.block_or_best(block)
.and_then(|block| self.client.storage(&BlockId::Hash(block), &key))
.map_err(client_err)))
}
fn storage_hash(
&self,
block: Option<Block::Hash>,
key: StorageKey,
) -> FutureResult<Option<Block::Hash>> {
Box::new(result(
self.block_or_best(block)
.and_then(|block| self.client.storage_hash(&BlockId::Hash(block), &key))
.map_err(client_err)))
}
fn child_storage_keys(
&self,
block: Option<Block::Hash>,
child_storage_key: StorageKey,
prefix: StorageKey,
) -> FutureResult<Vec<StorageKey>> {
Box::new(result(
self.block_or_best(block)
.and_then(|block| self.client.child_storage_keys(&BlockId::Hash(block), &child_storage_key, &prefix))
.map_err(client_err)))
}
fn child_storage(
&self,
block: Option<Block::Hash>,
child_storage_key: StorageKey,
key: StorageKey,
) -> FutureResult<Option<StorageData>> {
Box::new(result(
self.block_or_best(block)
.and_then(|block| self.client.child_storage(&BlockId::Hash(block), &child_storage_key, &key))
.map_err(client_err)))
}
fn child_storage_hash(
&self,
block: Option<Block::Hash>,
child_storage_key: StorageKey,
key: StorageKey,
) -> FutureResult<Option<Block::Hash>> {
Box::new(result(
self.block_or_best(block)
.and_then(|block| self.client.child_storage_hash(&BlockId::Hash(block), &child_storage_key, &key))
.map_err(client_err)))
}
fn metadata(&self, block: Option<Block::Hash>) -> FutureResult<Bytes> {
Box::new(result(
self.block_or_best(block)
.and_then(|block|
self.client.runtime_api().metadata(&BlockId::Hash(block)).map(Into::into)
)
.map_err(client_err)))
}
fn runtime_version(&self, block: Option<Block::Hash>) -> FutureResult<RuntimeVersion> {
Box::new(result(
self.block_or_best(block)
.and_then(|block| self.client.runtime_version_at(&BlockId::Hash(block)))
.map_err(client_err)))
}
fn query_storage(
&self,
from: Block::Hash,
to: Option<Block::Hash>,
keys: Vec<StorageKey>,
) -> FutureResult<Vec<StorageChangeSet<Block::Hash>>> {
let call_fn = move || {
let range = self.split_query_storage_range(from, to)?;
let mut changes = Vec::new();
let mut last_values = HashMap::new();
self.query_storage_unfiltered(&range, &keys, &mut last_values, &mut changes)?;
self.query_storage_filtered(&range, &keys, &last_values, &mut changes)?;
Ok(changes)
};
Box::new(result(call_fn()))
}
fn subscribe_runtime_version(
&self,
_meta: crate::metadata::Metadata,
subscriber: Subscriber<RuntimeVersion>,
) {
let stream = match self.client.storage_changes_notification_stream(
Some(&[StorageKey(well_known_keys::CODE.to_vec())]),
None,
) {
Ok(stream) => stream,
Err(err) => {
let _ = subscriber.reject(Error::from(client_err(err)).into());
return;
}
};
self.subscriptions.add(subscriber, |sink| {
let version = self.runtime_version(None.into())
.map_err(Into::into)
.wait();
let client = self.client.clone();
let mut previous_version = version.clone();
let stream = stream
.filter_map(move |_| {
let info = client.info();
let version = client
.runtime_version_at(&BlockId::hash(info.chain.best_hash))
.map_err(client_err)
.map_err(Into::into);
if previous_version != version {
previous_version = version.clone();
future::ready(Some(Ok::<_, ()>(version)))
} else {
future::ready(None)
}
})
.compat();
sink
.sink_map_err(|e| warn!("Error sending notifications: {:?}", e))
.send_all(
stream::iter_result(vec![Ok(version)])
.chain(stream)
)
// we ignore the resulting Stream (if the first stream is over we are unsubscribed)
.map(|_| ())
});
}
fn unsubscribe_runtime_version(
&self,
_meta: Option<crate::metadata::Metadata>,
id: SubscriptionId,
) -> RpcResult<bool> {
Ok(self.subscriptions.cancel(id))
}
fn subscribe_storage(
&self,
_meta: crate::metadata::Metadata,
subscriber: Subscriber<StorageChangeSet<Block::Hash>>,
keys: Option<Vec<StorageKey>>,
) {
let keys = Into::<Option<Vec<_>>>::into(keys);
let stream = match self.client.storage_changes_notification_stream(
keys.as_ref().map(|x| &**x),
None
) {
Ok(stream) => stream,
Err(err) => {
let _ = subscriber.reject(client_err(err).into());
return;
},
};
// initial values
let initial = stream::iter_result(keys
.map(|keys| {
let block = self.client.info().chain.best_hash;
let changes = keys
.into_iter()
.map(|key| self.storage(Some(block.clone()).into(), key.clone())
.map(|val| (key.clone(), val))
.wait()
.unwrap_or_else(|_| (key, None))
)
.collect();
vec![Ok(Ok(StorageChangeSet { block, changes }))]
}).unwrap_or_default());
self.subscriptions.add(subscriber, |sink| {
let stream = stream
.map(|(block, changes)| Ok::<_, ()>(Ok(StorageChangeSet {
block,
changes: changes.iter()
.filter_map(|(o_sk, k, v)| if o_sk.is_none() {
Some((k.clone(),v.cloned()))
} else { None }).collect(),
})))
.compat();
sink
.sink_map_err(|e| warn!("Error sending notifications: {:?}", e))
.send_all(initial.chain(stream))
// we ignore the resulting Stream (if the first stream is over we are unsubscribed)
.map(|_| ())
});
}
fn unsubscribe_storage(
&self,
_meta: Option<crate::metadata::Metadata>,
id: SubscriptionId,
) -> RpcResult<bool> {
Ok(self.subscriptions.cancel(id))
}
}
/// Splits passed range into two subranges where:
/// - first range has at least one element in it;
/// - second range (optionally) starts at given `middle` element.
pub(crate) fn split_range(size: usize, middle: Option<usize>) -> (Range<usize>, Option<Range<usize>>) {
// check if we can filter blocks-with-changes from some (sub)range using changes tries
let range2_begin = match middle {
// some of required changes tries are pruned => use available tries
Some(middle) if middle != 0 => Some(middle),
// all required changes tries are available, but we still want values at first block
// => do 'unfiltered' read for the first block and 'filtered' for the rest
Some(_) if size > 1 => Some(1),
// range contains single element => do not use changes tries
Some(_) => None,
// changes tries are not available => do 'unfiltered' read for the whole range
None => None,
};
let range1 = 0..range2_begin.unwrap_or(size);
let range2 = range2_begin.map(|begin| begin..size);
(range1, range2)
}
fn invalid_block_range<H: Header>(from: Option<&H>, to: Option<&H>, reason: String) -> Error {
let to_string = |x: Option<&H>| match x {
None => "unknown hash".into(),
Some(h) => format!("{} ({})", h.number(), h.hash()),
};
Error::InvalidBlockRange {
from: to_string(from),
to: to_string(to),
details: reason,
}
}
@@ -0,0 +1,785 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! State API backend for light nodes.
use std::{
sync::Arc,
collections::{HashSet, HashMap, hash_map::Entry},
};
use codec::Decode;
use futures03::{
future::{ready, Either},
channel::oneshot::{channel, Sender},
FutureExt, TryFutureExt,
StreamExt as _, TryStreamExt as _,
};
use hash_db::Hasher;
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use log::warn;
use parking_lot::Mutex;
use rpc::{
Result as RpcResult,
futures::Sink,
futures::future::{result, Future},
futures::stream::Stream,
};
use api::Subscriptions;
use client_api::backend::Backend;
use client::{
BlockchainEvents, Client, CallExecutor,
error::Error as ClientError,
light::{
blockchain::{future_header, RemoteBlockchain},
fetcher::{Fetcher, RemoteCallRequest, RemoteReadRequest, RemoteReadChildRequest},
},
};
use primitives::{
H256, Blake2Hasher, Bytes, OpaqueMetadata,
storage::{StorageKey, StorageData, StorageChangeSet},
};
use runtime_version::RuntimeVersion;
use sr_primitives::{
generic::BlockId,
traits::Block as BlockT,
};
use super::{StateBackend, error::{FutureResult, Error}, client_err};
/// Storage data map of storage keys => (optional) storage value.
type StorageMap = HashMap<StorageKey, Option<StorageData>>;
/// State API backend for light nodes.
pub struct LightState<Block: BlockT, F: Fetcher<Block>, B, E, RA> {
client: Arc<Client<B, E, Block, RA>>,
subscriptions: Subscriptions,
version_subscriptions: SimpleSubscriptions<Block::Hash, RuntimeVersion>,
storage_subscriptions: Arc<Mutex<StorageSubscriptions<Block>>>,
remote_blockchain: Arc<dyn RemoteBlockchain<Block>>,
fetcher: Arc<F>,
}
/// Shared requests container.
trait SharedRequests<Hash, V>: Clone + Send + Sync {
/// Tries to listen for already issued request, or issues request.
///
/// Returns true if requests has been issued.
fn listen_request(
&self,
block: Hash,
sender: Sender<Result<V, ()>>,
) -> bool;
/// Returns (and forgets) all listeners for given request.
fn on_response_received(&self, block: Hash) -> Vec<Sender<Result<V, ()>>>;
}
/// Storage subscriptions data.
struct StorageSubscriptions<Block: BlockT> {
/// Active storage requests.
active_requests: HashMap<Block::Hash, Vec<Sender<Result<StorageMap, ()>>>>,
/// Map of subscription => keys that this subscription watch for.
keys_by_subscription: HashMap<SubscriptionId, HashSet<StorageKey>>,
/// Map of key => set of subscriptions that watch this key.
subscriptions_by_key: HashMap<StorageKey, HashSet<SubscriptionId>>,
}
impl<Block: BlockT> SharedRequests<Block::Hash, StorageMap> for Arc<Mutex<StorageSubscriptions<Block>>> {
fn listen_request(
&self,
block: Block::Hash,
sender: Sender<Result<StorageMap, ()>>,
) -> bool {
let mut subscriptions = self.lock();
let active_requests_at = subscriptions.active_requests.entry(block).or_default();
active_requests_at.push(sender);
active_requests_at.len() == 1
}
fn on_response_received(&self, block: Block::Hash) -> Vec<Sender<Result<StorageMap, ()>>> {
self.lock().active_requests.remove(&block).unwrap_or_default()
}
}
/// Simple, maybe shared, subscription data that shares per block requests.
type SimpleSubscriptions<Hash, V> = Arc<Mutex<HashMap<Hash, Vec<Sender<Result<V, ()>>>>>>;
impl<Hash, V> SharedRequests<Hash, V> for SimpleSubscriptions<Hash, V> where
Hash: Send + Eq + std::hash::Hash,
V: Send,
{
fn listen_request(
&self,
block: Hash,
sender: Sender<Result<V, ()>>,
) -> bool {
let mut subscriptions = self.lock();
let active_requests_at = subscriptions.entry(block).or_default();
active_requests_at.push(sender);
active_requests_at.len() == 1
}
fn on_response_received(&self, block: Hash) -> Vec<Sender<Result<V, ()>>> {
self.lock().remove(&block).unwrap_or_default()
}
}
impl<Block: BlockT, F: Fetcher<Block> + 'static, B, E, RA> LightState<Block, F, B, E, RA>
where
Block: BlockT<Hash=H256>,
B: Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
{
/// Create new state API backend for light nodes.
pub fn new(
client: Arc<Client<B, E, Block, RA>>,
subscriptions: Subscriptions,
remote_blockchain: Arc<dyn RemoteBlockchain<Block>>,
fetcher: Arc<F>,
) -> Self {
Self {
client,
subscriptions,
version_subscriptions: Arc::new(Mutex::new(HashMap::new())),
storage_subscriptions: Arc::new(Mutex::new(StorageSubscriptions {
active_requests: HashMap::new(),
keys_by_subscription: HashMap::new(),
subscriptions_by_key: HashMap::new(),
})),
remote_blockchain,
fetcher,
}
}
/// Returns given block hash or best block hash if None is passed.
fn block_or_best(&self, hash: Option<Block::Hash>) -> Block::Hash {
hash.unwrap_or_else(|| self.client.info().chain.best_hash)
}
}
impl<Block, F, B, E, RA> StateBackend<B, E, Block, RA> for LightState<Block, F, B, E, RA>
where
Block: BlockT<Hash=H256>,
B: Backend<Block, Blake2Hasher> + Send + Sync + 'static,
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
F: Fetcher<Block> + 'static
{
fn call(
&self,
block: Option<Block::Hash>,
method: String,
call_data: Bytes,
) -> FutureResult<Bytes> {
Box::new(call(
&*self.remote_blockchain,
self.fetcher.clone(),
self.block_or_best(block),
method,
call_data,
).boxed().compat())
}
fn storage_keys(
&self,
_block: Option<Block::Hash>,
_prefix: StorageKey,
) -> FutureResult<Vec<StorageKey>> {
Box::new(result(Err(client_err(ClientError::NotAvailableOnLightClient))))
}
fn storage(
&self,
block: Option<Block::Hash>,
key: StorageKey,
) -> FutureResult<Option<StorageData>> {
Box::new(storage(
&*self.remote_blockchain,
self.fetcher.clone(),
self.block_or_best(block),
vec![key.0.clone()],
).boxed().compat().map(move |mut values| values
.remove(&key)
.expect("successful request has entries for all requested keys; qed")
))
}
fn storage_hash(
&self,
block: Option<Block::Hash>,
key: StorageKey,
) -> FutureResult<Option<Block::Hash>> {
Box::new(self
.storage(block, key)
.and_then(|maybe_storage|
result(Ok(maybe_storage.map(|storage| Blake2Hasher::hash(&storage.0))))
)
)
}
fn child_storage_keys(
&self,
_block: Option<Block::Hash>,
_child_storage_key: StorageKey,
_prefix: StorageKey,
) -> FutureResult<Vec<StorageKey>> {
Box::new(result(Err(client_err(ClientError::NotAvailableOnLightClient))))
}
fn child_storage(
&self,
block: Option<Block::Hash>,
child_storage_key: StorageKey,
key: StorageKey,
) -> FutureResult<Option<StorageData>> {
let block = self.block_or_best(block);
let fetcher = self.fetcher.clone();
let child_storage = resolve_header(&*self.remote_blockchain, &*self.fetcher, block)
.then(move |result| match result {
Ok(header) => Either::Left(fetcher.remote_read_child(RemoteReadChildRequest {
block,
header,
storage_key: child_storage_key.0,
keys: vec![key.0.clone()],
retry_count: Default::default(),
}).then(move |result| ready(result
.map(|mut data| data
.remove(&key.0)
.expect("successful result has entry for all keys; qed")
.map(StorageData)
)
.map_err(client_err)
))),
Err(error) => Either::Right(ready(Err(error))),
});
Box::new(child_storage.boxed().compat())
}
fn child_storage_hash(
&self,
block: Option<Block::Hash>,
child_storage_key: StorageKey,
key: StorageKey,
) -> FutureResult<Option<Block::Hash>> {
Box::new(self
.child_storage(block, child_storage_key, key)
.and_then(|maybe_storage|
result(Ok(maybe_storage.map(|storage| Blake2Hasher::hash(&storage.0))))
)
)
}
fn metadata(&self, block: Option<Block::Hash>) -> FutureResult<Bytes> {
let metadata = self.call(block, "Metadata_metadata".into(), Bytes(Vec::new()))
.and_then(|metadata| OpaqueMetadata::decode(&mut &metadata.0[..])
.map(Into::into)
.map_err(|decode_err| client_err(ClientError::CallResultDecode(
"Unable to decode metadata",
decode_err,
))));
Box::new(metadata)
}
fn runtime_version(&self, block: Option<Block::Hash>) -> FutureResult<RuntimeVersion> {
Box::new(runtime_version(
&*self.remote_blockchain,
self.fetcher.clone(),
self.block_or_best(block),
).boxed().compat())
}
fn query_storage(
&self,
_from: Block::Hash,
_to: Option<Block::Hash>,
_keys: Vec<StorageKey>,
) -> FutureResult<Vec<StorageChangeSet<Block::Hash>>> {
Box::new(result(Err(client_err(ClientError::NotAvailableOnLightClient))))
}
fn subscribe_storage(
&self,
_meta: crate::metadata::Metadata,
subscriber: Subscriber<StorageChangeSet<Block::Hash>>,
keys: Option<Vec<StorageKey>>
) {
let keys = match keys {
Some(keys) => keys,
None => {
warn!("Cannot subscribe to all keys on light client. Subscription rejected.");
return;
}
};
let keys = keys.iter().cloned().collect::<HashSet<_>>();
let keys_to_check = keys.iter().map(|k| k.0.clone()).collect::<HashSet<_>>();
let subscription_id = self.subscriptions.add(subscriber, move |sink| {
let fetcher = self.fetcher.clone();
let remote_blockchain = self.remote_blockchain.clone();
let storage_subscriptions = self.storage_subscriptions.clone();
let initial_block = self.block_or_best(None);
let initial_keys = keys_to_check.iter().cloned().collect::<Vec<_>>();
let changes_stream = subscription_stream::<Block, _, _, _, _, _, _, _, _>(
storage_subscriptions.clone(),
self.client
.import_notification_stream()
.map(|notification| Ok::<_, ()>(notification.hash))
.compat(),
display_error(storage(
&*remote_blockchain,
fetcher.clone(),
initial_block,
initial_keys,
).map(move |r| r.map(|r| (initial_block, r)))),
move |block| {
// there'll be single request per block for all active subscriptions
// with all subscribed keys
let keys = storage_subscriptions
.lock()
.subscriptions_by_key
.keys()
.map(|k| k.0.clone())
.collect();
storage(
&*remote_blockchain,
fetcher.clone(),
block,
keys,
)
},
move |block, old_value, new_value| {
// let's only select keys which are valid for this subscription
let new_value = new_value
.iter()
.filter(|(k, _)| keys_to_check.contains(&k.0))
.map(|(k, v)| (k.clone(), v.clone()))
.collect::<HashMap<_, _>>();
let value_differs = old_value
.as_ref()
.map(|old_value| **old_value != new_value)
.unwrap_or(true);
match value_differs {
true => Some(StorageChangeSet {
block,
changes: new_value
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
}),
false => None,
}
}
);
sink
.sink_map_err(|e| warn!("Error sending notifications: {:?}", e))
.send_all(changes_stream.map(|changes| Ok(changes)))
// we ignore the resulting Stream (if the first stream is over we are unsubscribed)
.map(|_| ())
});
// remember keys associated with this subscription
let mut storage_subscriptions = self.storage_subscriptions.lock();
storage_subscriptions.keys_by_subscription.insert(subscription_id.clone(), keys.clone());
for key in keys {
storage_subscriptions
.subscriptions_by_key
.entry(key)
.or_default()
.insert(subscription_id.clone());
}
}
fn unsubscribe_storage(
&self,
_meta: Option<crate::metadata::Metadata>,
id: SubscriptionId,
) -> RpcResult<bool> {
if !self.subscriptions.cancel(id.clone()) {
return Ok(false);
}
// forget subscription keys
let mut storage_subscriptions = self.storage_subscriptions.lock();
let keys = storage_subscriptions.keys_by_subscription.remove(&id);
for key in keys.into_iter().flat_map(|keys| keys.into_iter()) {
match storage_subscriptions.subscriptions_by_key.entry(key) {
Entry::Vacant(_) => unreachable!("every key from keys_by_subscription has\
corresponding entry in subscriptions_by_key; qed"),
Entry::Occupied(mut entry) => {
entry.get_mut().remove(&id);
if entry.get().is_empty() {
entry.remove();
}
}
}
}
Ok(true)
}
fn subscribe_runtime_version(
&self,
_meta: crate::metadata::Metadata,
subscriber: Subscriber<RuntimeVersion>,
) {
self.subscriptions.add(subscriber, move |sink| {
let fetcher = self.fetcher.clone();
let remote_blockchain = self.remote_blockchain.clone();
let version_subscriptions = self.version_subscriptions.clone();
let initial_block = self.block_or_best(None);
let versions_stream = subscription_stream::<Block, _, _, _, _, _, _, _, _>(
version_subscriptions,
self.client
.import_notification_stream()
.map(|notification| Ok::<_, ()>(notification.hash))
.compat(),
display_error(runtime_version(
&*remote_blockchain,
fetcher.clone(),
initial_block,
).map(move |r| r.map(|r| (initial_block, r)))),
move |block| runtime_version(
&*remote_blockchain,
fetcher.clone(),
block,
),
|_, old_version, new_version| {
let version_differs = old_version
.as_ref()
.map(|old_version| *old_version != new_version)
.unwrap_or(true);
match version_differs {
true => Some(new_version.clone()),
false => None,
}
}
);
sink
.sink_map_err(|e| warn!("Error sending notifications: {:?}", e))
.send_all(versions_stream.map(|version| Ok(version)))
// we ignore the resulting Stream (if the first stream is over we are unsubscribed)
.map(|_| ())
});
}
fn unsubscribe_runtime_version(
&self,
_meta: Option<crate::metadata::Metadata>,
id: SubscriptionId,
) -> RpcResult<bool> {
Ok(self.subscriptions.cancel(id))
}
}
/// Resolve header by hash.
fn resolve_header<Block: BlockT, F: Fetcher<Block>>(
remote_blockchain: &dyn RemoteBlockchain<Block>,
fetcher: &F,
block: Block::Hash,
) -> impl std::future::Future<Output = Result<Block::Header, Error>> {
let maybe_header = future_header(
remote_blockchain,
fetcher,
BlockId::Hash(block),
);
maybe_header.then(move |result|
ready(result.and_then(|maybe_header|
maybe_header.ok_or(ClientError::UnknownBlock(format!("{}", block)))
).map_err(client_err)),
)
}
/// Call runtime method at given block
fn call<Block: BlockT, F: Fetcher<Block>>(
remote_blockchain: &dyn RemoteBlockchain<Block>,
fetcher: Arc<F>,
block: Block::Hash,
method: String,
call_data: Bytes,
) -> impl std::future::Future<Output = Result<Bytes, Error>> {
resolve_header(remote_blockchain, &*fetcher, block)
.then(move |result| match result {
Ok(header) => Either::Left(fetcher.remote_call(RemoteCallRequest {
block,
header,
method,
call_data: call_data.0,
retry_count: Default::default(),
}).then(|result| ready(result.map(Bytes).map_err(client_err)))),
Err(error) => Either::Right(ready(Err(error))),
})
}
/// Get runtime version at given block.
fn runtime_version<Block: BlockT, F: Fetcher<Block>>(
remote_blockchain: &dyn RemoteBlockchain<Block>,
fetcher: Arc<F>,
block: Block::Hash,
) -> impl std::future::Future<Output = Result<RuntimeVersion, Error>> {
call(
remote_blockchain,
fetcher,
block,
"Core_version".into(),
Bytes(Vec::new()),
)
.then(|version| ready(version.and_then(|version|
Decode::decode(&mut &version.0[..]).map_err(|_| client_err(ClientError::VersionInvalid))
)))
}
/// Get storage value at given key at given block.
fn storage<Block: BlockT, F: Fetcher<Block>>(
remote_blockchain: &dyn RemoteBlockchain<Block>,
fetcher: Arc<F>,
block: Block::Hash,
keys: Vec<Vec<u8>>,
) -> impl std::future::Future<Output = Result<HashMap<StorageKey, Option<StorageData>>, Error>> {
resolve_header(remote_blockchain, &*fetcher, block)
.then(move |result| match result {
Ok(header) => Either::Left(fetcher.remote_read(RemoteReadRequest {
block,
header,
keys,
retry_count: Default::default(),
}).then(|result| ready(result
.map(|result| result
.into_iter()
.map(|(key, value)| (StorageKey(key), value.map(StorageData)))
.collect()
).map_err(client_err)
))),
Err(error) => Either::Right(ready(Err(error))),
})
}
/// Returns subscription stream that issues request on every imported block and
/// if value has changed from previous block, emits (stream) item.
fn subscription_stream<
Block,
Requests,
FutureBlocksStream,
V, N,
InitialRequestFuture,
IssueRequest, IssueRequestFuture,
CompareValues,
>(
shared_requests: Requests,
future_blocks_stream: FutureBlocksStream,
initial_request: InitialRequestFuture,
issue_request: IssueRequest,
compare_values: CompareValues,
) -> impl Stream<Item=N, Error=()> where
Block: BlockT<Hash=H256>,
Requests: 'static + SharedRequests<Block::Hash, V>,
FutureBlocksStream: Stream<Item=Block::Hash, Error=()>,
V: Send + 'static + Clone,
InitialRequestFuture: std::future::Future<Output = Result<(Block::Hash, V), ()>> + Send + 'static,
IssueRequest: 'static + Fn(Block::Hash) -> IssueRequestFuture,
IssueRequestFuture: std::future::Future<Output = Result<V, Error>> + Send + 'static,
CompareValues: Fn(Block::Hash, Option<&V>, &V) -> Option<N>,
{
// we need to send initial value first, then we'll only be sending if value has changed
let previous_value = Arc::new(Mutex::new(None));
// prepare 'stream' of initial values
let initial_value_stream = ignore_error(initial_request)
.boxed()
.compat()
.into_stream();
// prepare stream of future values
//
// we do not want to stop stream if single request fails
// (the warning should have been already issued by the request issuer)
let future_values_stream = future_blocks_stream
.and_then(move |block| ignore_error(maybe_share_remote_request::<Block, _, _, _, _>(
shared_requests.clone(),
block,
&issue_request,
).map(move |r| r.map(|v| (block, v)))).boxed().compat());
// now let's return changed values for selected blocks
initial_value_stream
.chain(future_values_stream)
.filter_map(move |block_and_new_value| block_and_new_value.and_then(|(block, new_value)| {
let mut previous_value = previous_value.lock();
compare_values(block, previous_value.as_ref(), &new_value)
.map(|notification_value| {
*previous_value = Some(new_value);
notification_value
})
}))
.map_err(|_| ())
}
/// Request some data from remote node, probably reusing response from already
/// (in-progress) existing request.
fn maybe_share_remote_request<Block: BlockT, Requests, V, IssueRequest, IssueRequestFuture>(
shared_requests: Requests,
block: Block::Hash,
issue_request: &IssueRequest,
) -> impl std::future::Future<Output = Result<V, ()>> where
V: Clone,
Requests: SharedRequests<Block::Hash, V>,
IssueRequest: Fn(Block::Hash) -> IssueRequestFuture,
IssueRequestFuture: std::future::Future<Output = Result<V, Error>>,
{
let (sender, receiver) = channel();
let need_issue_request = shared_requests.listen_request(block, sender);
// if that isn't the first request - just listen for existing request' response
if !need_issue_request {
return Either::Right(receiver.then(|r| ready(r.unwrap_or(Err(())))));
}
// that is the first request - issue remote request + notify all listeners on
// completion
Either::Left(
display_error(issue_request(block))
.then(move |remote_result| {
let listeners = shared_requests.on_response_received(block);
// skip first element, because this future is the first element
for receiver in listeners.into_iter().skip(1) {
if let Err(_) = receiver.send(remote_result.clone()) {
// we don't care if receiver has been dropped already
}
}
ready(remote_result)
})
)
}
/// Convert successful future result into Ok(result) and error into Err(()),
/// displaying warning.
fn display_error<F, T>(future: F) -> impl std::future::Future<Output=Result<T, ()>> where
F: std::future::Future<Output=Result<T, Error>>
{
future.then(|result| ready(match result {
Ok(result) => Ok(result),
Err(err) => {
warn!("Remote request for subscription data has failed with: {:?}", err);
Err(())
},
}))
}
/// Convert successful future result into Ok(Some(result)) and error into Ok(None),
/// displaying warning.
fn ignore_error<F, T>(future: F) -> impl std::future::Future<Output=Result<Option<T>, ()>> where
F: std::future::Future<Output=Result<T, ()>>
{
future.then(|result| ready(match result {
Ok(result) => Ok(Some(result)),
Err(()) => Ok(None),
}))
}
#[cfg(test)]
mod tests {
use rpc::futures::stream::futures_ordered;
use test_client::runtime::Block;
use super::*;
#[test]
fn subscription_stream_works() {
let stream = subscription_stream::<Block, _, _, _, _, _, _, _, _>(
SimpleSubscriptions::default(),
futures_ordered(vec![result(Ok(H256::from([2; 32]))), result(Ok(H256::from([3; 32])))]),
ready(Ok((H256::from([1; 32]), 100))),
|block| match block[0] {
2 => ready(Ok(100)),
3 => ready(Ok(200)),
_ => unreachable!("should not issue additional requests"),
},
|_, old_value, new_value| match old_value == Some(new_value) {
true => None,
false => Some(new_value.clone()),
}
);
assert_eq!(
stream.collect().wait(),
Ok(vec![100, 200])
);
}
#[test]
fn subscription_stream_ignores_failed_requests() {
let stream = subscription_stream::<Block, _, _, _, _, _, _, _, _>(
SimpleSubscriptions::default(),
futures_ordered(vec![result(Ok(H256::from([2; 32]))), result(Ok(H256::from([3; 32])))]),
ready(Ok((H256::from([1; 32]), 100))),
|block| match block[0] {
2 => ready(Err(client_err(ClientError::NotAvailableOnLightClient))),
3 => ready(Ok(200)),
_ => unreachable!("should not issue additional requests"),
},
|_, old_value, new_value| match old_value == Some(new_value) {
true => None,
false => Some(new_value.clone()),
}
);
assert_eq!(
stream.collect().wait(),
Ok(vec![100, 200])
);
}
#[test]
fn maybe_share_remote_request_shares_request() {
type UnreachableFuture = futures03::future::Ready<Result<u32, Error>>;
let shared_requests = SimpleSubscriptions::default();
// let's 'issue' requests for B1
shared_requests.lock().insert(
H256::from([1; 32]),
vec![channel().0],
);
// make sure that no additional requests are issued when we're asking for B1
let _ = maybe_share_remote_request::<Block, _, _, _, UnreachableFuture>(
shared_requests.clone(),
H256::from([1; 32]),
&|_| unreachable!("no duplicate requests issued"),
);
// make sure that additional requests is issued when we're asking for B2
let request_issued = Arc::new(Mutex::new(false));
let _ = maybe_share_remote_request::<Block, _, _, _, UnreachableFuture>(
shared_requests.clone(),
H256::from([2; 32]),
&|_| {
*request_issued.lock() = true;
ready(Ok(Default::default()))
},
);
assert!(*request_issued.lock());
}
}
+323
View File
@@ -0,0 +1,323 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use super::state_full::split_range;
use self::error::Error;
use std::sync::Arc;
use assert_matches::assert_matches;
use futures::stream::Stream;
use primitives::storage::well_known_keys;
use sr_io::hashing::blake2_256;
use test_client::{
prelude::*,
consensus::BlockOrigin,
runtime,
};
#[test]
fn should_return_storage() {
const KEY: &[u8] = b":mock";
const VALUE: &[u8] = b"hello world";
const STORAGE_KEY: &[u8] = b":child_storage:default:child";
const CHILD_VALUE: &[u8] = b"hello world !";
let mut core = tokio::runtime::Runtime::new().unwrap();
let client = TestClientBuilder::new()
.add_extra_storage(KEY.to_vec(), VALUE.to_vec())
.add_extra_child_storage(STORAGE_KEY.to_vec(), KEY.to_vec(), CHILD_VALUE.to_vec())
.build();
let genesis_hash = client.genesis_hash();
let client = new_full(Arc::new(client), Subscriptions::new(Arc::new(core.executor())));
let key = StorageKey(KEY.to_vec());
let storage_key = StorageKey(STORAGE_KEY.to_vec());
assert_eq!(
client.storage(key.clone(), Some(genesis_hash).into()).wait()
.map(|x| x.map(|x| x.0.len())).unwrap().unwrap() as usize,
VALUE.len(),
);
assert_matches!(
client.storage_hash(key.clone(), Some(genesis_hash).into()).wait()
.map(|x| x.is_some()),
Ok(true)
);
assert_eq!(
client.storage_size(key.clone(), None).wait().unwrap().unwrap() as usize,
VALUE.len(),
);
assert_eq!(
core.block_on(
client.child_storage(storage_key, key, Some(genesis_hash).into())
.map(|x| x.map(|x| x.0.len()))
).unwrap().unwrap() as usize,
CHILD_VALUE.len(),
);
}
#[test]
fn should_return_child_storage() {
let core = tokio::runtime::Runtime::new().unwrap();
let client = Arc::new(test_client::TestClientBuilder::new()
.add_child_storage("test", "key", vec![42_u8])
.build());
let genesis_hash = client.genesis_hash();
let client = new_full(client, Subscriptions::new(Arc::new(core.executor())));
let child_key = StorageKey(well_known_keys::CHILD_STORAGE_KEY_PREFIX.iter().chain(b"test").cloned().collect());
let key = StorageKey(b"key".to_vec());
assert_matches!(
client.child_storage(child_key.clone(), key.clone(), Some(genesis_hash).into()).wait(),
Ok(Some(StorageData(ref d))) if d[0] == 42 && d.len() == 1
);
assert_matches!(
client.child_storage_hash(child_key.clone(), key.clone(), Some(genesis_hash).into())
.wait().map(|x| x.is_some()),
Ok(true)
);
assert_matches!(
client.child_storage_size(child_key.clone(), key.clone(), None).wait(),
Ok(Some(1))
);
}
#[test]
fn should_call_contract() {
let core = tokio::runtime::Runtime::new().unwrap();
let client = Arc::new(test_client::new());
let genesis_hash = client.genesis_hash();
let client = new_full(client, Subscriptions::new(Arc::new(core.executor())));
assert_matches!(
client.call("balanceOf".into(), Bytes(vec![1,2,3]), Some(genesis_hash).into()).wait(),
Err(Error::Client(_))
)
}
#[test]
fn should_notify_about_storage_changes() {
let mut core = tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let (subscriber, id, transport) = Subscriber::new_test("test");
{
let client = Arc::new(test_client::new());
let api = new_full(client.clone(), Subscriptions::new(Arc::new(remote)));
api.subscribe_storage(Default::default(), subscriber, None.into());
// assert id assigned
assert_eq!(core.block_on(id), Ok(Ok(SubscriptionId::Number(1))));
let mut builder = client.new_block(Default::default()).unwrap();
builder.push_transfer(runtime::Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 42,
nonce: 0,
}).unwrap();
client.import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
}
// assert notification sent to transport
let (notification, next) = core.block_on(transport.into_future()).unwrap();
assert!(notification.is_some());
// no more notifications on this channel
assert_eq!(core.block_on(next.into_future()).unwrap().0, None);
}
#[test]
fn should_send_initial_storage_changes_and_notifications() {
let mut core = tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let (subscriber, id, transport) = Subscriber::new_test("test");
{
let client = Arc::new(test_client::new());
let api = new_full(client.clone(), Subscriptions::new(Arc::new(remote)));
let alice_balance_key = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Alice.into()));
api.subscribe_storage(Default::default(), subscriber, Some(vec![
StorageKey(alice_balance_key.to_vec()),
]).into());
// assert id assigned
assert_eq!(core.block_on(id), Ok(Ok(SubscriptionId::Number(1))));
let mut builder = client.new_block(Default::default()).unwrap();
builder.push_transfer(runtime::Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 42,
nonce: 0,
}).unwrap();
client.import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
}
// assert initial values sent to transport
let (notification, next) = core.block_on(transport.into_future()).unwrap();
assert!(notification.is_some());
// assert notification sent to transport
let (notification, next) = core.block_on(next.into_future()).unwrap();
assert!(notification.is_some());
// no more notifications on this channel
assert_eq!(core.block_on(next.into_future()).unwrap().0, None);
}
#[test]
fn should_query_storage() {
fn run_tests(client: Arc<TestClient>) {
let core = tokio::runtime::Runtime::new().unwrap();
let api = new_full(client.clone(), Subscriptions::new(Arc::new(core.executor())));
let add_block = |nonce| {
let mut builder = client.new_block(Default::default()).unwrap();
// fake change: None -> None -> None
builder.push_storage_change(vec![1], None).unwrap();
// fake change: None -> Some(value) -> Some(value)
builder.push_storage_change(vec![2], Some(vec![2])).unwrap();
// actual change: None -> Some(value) -> None
builder.push_storage_change(vec![3], if nonce == 0 { Some(vec![3]) } else { None }).unwrap();
// actual change: None -> Some(value)
builder.push_storage_change(vec![4], if nonce == 0 { None } else { Some(vec![4]) }).unwrap();
// actual change: Some(value1) -> Some(value2)
builder.push_storage_change(vec![5], Some(vec![nonce as u8])).unwrap();
let block = builder.bake().unwrap();
let hash = block.header.hash();
client.import(BlockOrigin::Own, block).unwrap();
hash
};
let block1_hash = add_block(0);
let block2_hash = add_block(1);
let genesis_hash = client.genesis_hash();
let mut expected = vec![
StorageChangeSet {
block: genesis_hash,
changes: vec![
(StorageKey(vec![1]), None),
(StorageKey(vec![2]), None),
(StorageKey(vec![3]), None),
(StorageKey(vec![4]), None),
(StorageKey(vec![5]), None),
],
},
StorageChangeSet {
block: block1_hash,
changes: vec![
(StorageKey(vec![2]), Some(StorageData(vec![2]))),
(StorageKey(vec![3]), Some(StorageData(vec![3]))),
(StorageKey(vec![5]), Some(StorageData(vec![0]))),
],
},
];
// Query changes only up to block1
let keys = (1..6).map(|k| StorageKey(vec![k])).collect::<Vec<_>>();
let result = api.query_storage(
keys.clone(),
genesis_hash,
Some(block1_hash).into(),
);
assert_eq!(result.wait().unwrap(), expected);
// Query all changes
let result = api.query_storage(
keys.clone(),
genesis_hash,
None.into(),
);
expected.push(StorageChangeSet {
block: block2_hash,
changes: vec![
(StorageKey(vec![3]), None),
(StorageKey(vec![4]), Some(StorageData(vec![4]))),
(StorageKey(vec![5]), Some(StorageData(vec![1]))),
],
});
assert_eq!(result.wait().unwrap(), expected);
}
run_tests(Arc::new(test_client::new()));
run_tests(Arc::new(TestClientBuilder::new().set_support_changes_trie(true).build()));
}
#[test]
fn should_split_ranges() {
assert_eq!(split_range(1, None), (0..1, None));
assert_eq!(split_range(100, None), (0..100, None));
assert_eq!(split_range(1, Some(0)), (0..1, None));
assert_eq!(split_range(100, Some(50)), (0..50, Some(50..100)));
assert_eq!(split_range(100, Some(99)), (0..99, Some(99..100)));
}
#[test]
fn should_return_runtime_version() {
let core = tokio::runtime::Runtime::new().unwrap();
let client = Arc::new(test_client::new());
let api = new_full(client.clone(), Subscriptions::new(Arc::new(core.executor())));
let result = "{\"specName\":\"test\",\"implName\":\"parity-test\",\"authoringVersion\":1,\
\"specVersion\":1,\"implVersion\":1,\"apis\":[[\"0xdf6acb689907609b\",2],\
[\"0x37e397fc7c91f5e4\",1],[\"0xd2bc9897eed08f15\",1],[\"0x40fe3ad401f8959a\",3],\
[\"0xc6e9a76309f39b09\",1],[\"0xdd718d5cc53262d4\",1],[\"0xcbca25e39f142387\",1],\
[\"0xf78b278be53f454c\",1],[\"0xab3c0572291feb8b\",1],[\"0xbc9d89904f5b923f\",1]]}";
let runtime_version = api.runtime_version(None.into()).wait().unwrap();
let serialized = serde_json::to_string(&runtime_version).unwrap();
assert_eq!(serialized, result);
let deserialized: RuntimeVersion = serde_json::from_str(result).unwrap();
assert_eq!(deserialized, runtime_version);
}
#[test]
fn should_notify_on_runtime_version_initially() {
let mut core = tokio::runtime::Runtime::new().unwrap();
let (subscriber, id, transport) = Subscriber::new_test("test");
{
let client = Arc::new(test_client::new());
let api = new_full(client.clone(), Subscriptions::new(Arc::new(core.executor())));
api.subscribe_runtime_version(Default::default(), subscriber);
// assert id assigned
assert_eq!(core.block_on(id), Ok(Ok(SubscriptionId::Number(1))));
}
// assert initial version sent.
let (notification, next) = core.block_on(transport.into_future()).unwrap();
assert!(notification.is_some());
// no more notifications on this channel
assert_eq!(core.block_on(next.into_future()).unwrap().0, None);
}
#[test]
fn should_deserialize_storage_key() {
let k = "\"0x7f864e18e3dd8b58386310d2fe0919eef27c6e558564b7f67f22d99d20f587b\"";
let k: StorageKey = serde_json::from_str(k).unwrap();
assert_eq!(k.0.len(), 32);
}
+105
View File
@@ -0,0 +1,105 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
//! Substrate system API.
#[cfg(test)]
mod tests;
use futures03::{channel::{mpsc, oneshot}, compat::Compat};
use api::Receiver;
use sr_primitives::traits::{self, Header as HeaderT};
use self::error::Result;
pub use api::system::*;
pub use self::helpers::{Properties, SystemInfo, Health, PeerInfo, NodeRole};
pub use self::gen_client::Client as SystemClient;
/// System API implementation
pub struct System<B: traits::Block> {
info: SystemInfo,
send_back: mpsc::UnboundedSender<Request<B>>,
}
/// Request to be processed.
pub enum Request<B: traits::Block> {
/// Must return the health of the network.
Health(oneshot::Sender<Health>),
/// Must return information about the peers we are connected to.
Peers(oneshot::Sender<Vec<PeerInfo<B::Hash, <B::Header as HeaderT>::Number>>>),
/// Must return the state of the network.
NetworkState(oneshot::Sender<rpc::Value>),
/// Must return the node role.
NodeRoles(oneshot::Sender<Vec<NodeRole>>)
}
impl<B: traits::Block> System<B> {
/// Creates new `System`.
///
/// The `send_back` will be used to transmit some of the requests. The user is responsible for
/// reading from that channel and answering the requests.
pub fn new(
info: SystemInfo,
send_back: mpsc::UnboundedSender<Request<B>>
) -> Self {
System {
info,
send_back,
}
}
}
impl<B: traits::Block> SystemApi<B::Hash, <B::Header as HeaderT>::Number> for System<B> {
fn system_name(&self) -> Result<String> {
Ok(self.info.impl_name.clone())
}
fn system_version(&self) -> Result<String> {
Ok(self.info.impl_version.clone())
}
fn system_chain(&self) -> Result<String> {
Ok(self.info.chain_name.clone())
}
fn system_properties(&self) -> Result<Properties> {
Ok(self.info.properties.clone())
}
fn system_health(&self) -> Receiver<Health> {
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::Health(tx));
Receiver(Compat::new(rx))
}
fn system_peers(&self) -> Receiver<Vec<PeerInfo<B::Hash, <B::Header as HeaderT>::Number>>> {
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::Peers(tx));
Receiver(Compat::new(rx))
}
fn system_network_state(&self) -> Receiver<rpc::Value> {
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::NetworkState(tx));
Receiver(Compat::new(rx))
}
fn system_node_roles(&self) -> Receiver<Vec<NodeRole>> {
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::NodeRoles(tx));
Receiver(Compat::new(rx))
}
}
+234
View File
@@ -0,0 +1,234 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate 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.
// Substrate 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. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use network::{self, PeerId};
use network::config::Roles;
use test_client::runtime::Block;
use assert_matches::assert_matches;
use futures03::{prelude::*, channel::mpsc};
use std::thread;
struct Status {
pub peers: usize,
pub is_syncing: bool,
pub is_dev: bool,
pub peer_id: PeerId,
}
impl Default for Status {
fn default() -> Status {
Status {
peer_id: PeerId::random(),
peers: 0,
is_syncing: false,
is_dev: false,
}
}
}
fn api<T: Into<Option<Status>>>(sync: T) -> System<Block> {
let status = sync.into().unwrap_or_default();
let should_have_peers = !status.is_dev;
let (tx, rx) = mpsc::unbounded();
thread::spawn(move || {
futures03::executor::block_on(rx.for_each(move |request| {
match request {
Request::Health(sender) => {
let _ = sender.send(Health {
peers: status.peers,
is_syncing: status.is_syncing,
should_have_peers,
});
},
Request::Peers(sender) => {
let mut peers = vec![];
for _peer in 0..status.peers {
peers.push(PeerInfo {
peer_id: status.peer_id.to_base58(),
roles: format!("{:?}", Roles::FULL),
protocol_version: 1,
best_hash: Default::default(),
best_number: 1,
});
}
let _ = sender.send(peers);
}
Request::NetworkState(sender) => {
let _ = sender.send(serde_json::to_value(&network::NetworkState {
peer_id: String::new(),
listened_addresses: Default::default(),
external_addresses: Default::default(),
connected_peers: Default::default(),
not_connected_peers: Default::default(),
average_download_per_sec: 0,
average_upload_per_sec: 0,
peerset: serde_json::Value::Null,
}).unwrap());
},
Request::NodeRoles(sender) => {
let _ = sender.send(vec![NodeRole::Authority]);
}
};
future::ready(())
}))
});
System::new(SystemInfo {
impl_name: "testclient".into(),
impl_version: "0.2.0".into(),
chain_name: "testchain".into(),
properties: Default::default(),
}, tx)
}
fn wait_receiver<T>(rx: Receiver<T>) -> T {
let mut runtime = tokio::runtime::current_thread::Runtime::new().unwrap();
runtime.block_on(rx).unwrap()
}
#[test]
fn system_name_works() {
assert_eq!(
api(None).system_name().unwrap(),
"testclient".to_owned()
);
}
#[test]
fn system_version_works() {
assert_eq!(
api(None).system_version().unwrap(),
"0.2.0".to_owned()
);
}
#[test]
fn system_chain_works() {
assert_eq!(
api(None).system_chain().unwrap(),
"testchain".to_owned()
);
}
#[test]
fn system_properties_works() {
assert_eq!(
api(None).system_properties().unwrap(),
serde_json::map::Map::new()
);
}
#[test]
fn system_health() {
assert_matches!(
wait_receiver(api(None).system_health()),
Health {
peers: 0,
is_syncing: false,
should_have_peers: true,
}
);
assert_matches!(
wait_receiver(api(Status {
peer_id: PeerId::random(),
peers: 5,
is_syncing: true,
is_dev: true,
}).system_health()),
Health {
peers: 5,
is_syncing: true,
should_have_peers: false,
}
);
assert_eq!(
wait_receiver(api(Status {
peer_id: PeerId::random(),
peers: 5,
is_syncing: false,
is_dev: false,
}).system_health()),
Health {
peers: 5,
is_syncing: false,
should_have_peers: true,
}
);
assert_eq!(
wait_receiver(api(Status {
peer_id: PeerId::random(),
peers: 0,
is_syncing: false,
is_dev: true,
}).system_health()),
Health {
peers: 0,
is_syncing: false,
should_have_peers: false,
}
);
}
#[test]
fn system_peers() {
let peer_id = PeerId::random();
assert_eq!(
wait_receiver(api(Status {
peer_id: peer_id.clone(),
peers: 1,
is_syncing: false,
is_dev: true,
}).system_peers()),
vec![PeerInfo {
peer_id: peer_id.to_base58(),
roles: "FULL".into(),
protocol_version: 1,
best_hash: Default::default(),
best_number: 1u64,
}]
);
}
#[test]
fn system_network_state() {
let res = wait_receiver(api(None).system_network_state());
assert_eq!(
serde_json::from_value::<network::NetworkState>(res).unwrap(),
network::NetworkState {
peer_id: String::new(),
listened_addresses: Default::default(),
external_addresses: Default::default(),
connected_peers: Default::default(),
not_connected_peers: Default::default(),
average_download_per_sec: 0,
average_upload_per_sec: 0,
peerset: serde_json::Value::Null,
}
);
}
#[test]
fn system_node_roles() {
assert_eq!(
wait_receiver(api(None).system_node_roles()),
vec![NodeRole::Authority]
);
}