Phase 1 of repo reorg (#719)

* Remove unneeded script

* Rename Substrate Demo -> Substrate

* Rename demo -> node

* Build wasm from last rename.

* Merge ed25519 into substrate-primitives

* Minor tweak

* Rename substrate -> core

* Move substrate-runtime-support to core/runtime/support

* Rename/move substrate-runtime-version

* Move codec up a level

* Rename substrate-codec -> parity-codec

* Move environmental up a level

* Move pwasm-* up to top, ready for removal

* Remove requirement of s-r-support from s-r-primitives

* Move core/runtime/primitives into core/runtime-primitives

* Remove s-r-support dep from s-r-version

* Remove dep of s-r-support from bft

* Remove dep of s-r-support from node/consensus

* Sever all other core deps from s-r-support

* Forgot the no_std directive

* Rename non-SRML modules to sr-* to avoid match clashes

* Move runtime/* to srml/*

* Rename substrate-runtime-* -> srml-*

* Move srml to top-level
This commit is contained in:
Gav Wood
2018-09-12 11:13:31 +02:00
committed by Arkadiy Paronyan
parent 8fe5aa4c81
commit 1e01162505
374 changed files with 2845 additions and 2902 deletions
+68
View File
@@ -0,0 +1,68 @@
// Copyright 2017 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 client;
use extrinsic_pool;
use rpc;
use errors;
error_chain! {
links {
Pool(extrinsic_pool::Error, extrinsic_pool::ErrorKind) #[doc = "Pool error"];
Client(client::error::Error, client::error::ErrorKind) #[doc = "Client error"];
}
errors {
/// Not implemented yet
Unimplemented {
description("not yet implemented"),
display("Method Not Implemented"),
}
/// Incorrect extrinsic format.
BadFormat {
description("bad format"),
display("Invalid extrinsic format"),
}
/// Verification error
Verification(e: Box<::std::error::Error + Send>) {
description("extrinsic verification error"),
display("Extrinsic verification error: {}", e.description()),
}
}
}
const ERROR: i64 = 1000;
impl From<Error> for rpc::Error {
fn from(e: Error) -> Self {
match e {
Error(ErrorKind::Unimplemented, _) => errors::unimplemented(),
Error(ErrorKind::BadFormat, _) => rpc::Error {
code: rpc::ErrorCode::ServerError(ERROR + 1),
message: "Extrinsic has invalid format.".into(),
data: None,
},
Error(ErrorKind::Verification(e), _) => rpc::Error {
code: rpc::ErrorCode::ServerError(ERROR + 2),
message: e.description().into(),
data: Some(format!("{:?}", e).into()),
},
e => errors::internal(e),
}
}
}
+162
View File
@@ -0,0 +1,162 @@
// Copyright 2017 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.
use std::sync::Arc;
use client::{self, Client};
use codec::Decode;
use extrinsic_pool::{
Pool,
IntoPoolError,
ChainApi as PoolChainApi,
watcher::Status,
VerifiedTransaction,
AllExtrinsics,
ExHash,
ExtrinsicFor,
};
use jsonrpc_macros::pubsub;
use jsonrpc_pubsub::SubscriptionId;
use primitives::{Bytes, Blake2Hasher, RlpCodec};
use rpc::futures::{Sink, Stream, Future};
use runtime_primitives::{generic, traits};
use subscriptions::Subscriptions;
use tokio::runtime::TaskExecutor;
pub mod error;
#[cfg(test)]
mod tests;
use self::error::Result;
build_rpc_trait! {
/// Substrate authoring RPC API
pub trait AuthorApi<Hash, Extrinsic, PendingExtrinsics> {
type Metadata;
/// Submit extrinsic for inclusion in block.
#[rpc(name = "author_submitRichExtrinsic")]
fn submit_rich_extrinsic(&self, Extrinsic) -> Result<Hash>;
/// Submit hex-encoded extrinsic for inclusion in block.
#[rpc(name = "author_submitExtrinsic")]
fn submit_extrinsic(&self, Bytes) -> Result<Hash>;
/// Returns all pending extrinsics, potentially grouped by sender.
#[rpc(name = "author_pendingExtrinsics")]
fn pending_extrinsics(&self) -> Result<PendingExtrinsics>;
#[pubsub(name = "author_extrinsicUpdate")] {
/// Submit an extrinsic to watch.
#[rpc(name = "author_submitAndWatchExtrinsic")]
fn watch_extrinsic(&self, Self::Metadata, pubsub::Subscriber<Status<Hash>>, Bytes);
/// Unsubscribe from extrinsic watching.
#[rpc(name = "author_unwatchExtrinsic")]
fn unwatch_extrinsic(&self, SubscriptionId) -> Result<bool>;
}
}
}
/// Authoring API
pub struct Author<B, E, P> where
P: PoolChainApi + Sync + Send + 'static,
{
/// Substrate client
client: Arc<Client<B, E, <P as PoolChainApi>::Block>>,
/// Extrinsic pool
pool: Arc<Pool<P>>,
/// Subscriptions manager
subscriptions: Subscriptions,
}
impl<B, E, P> Author<B, E, P> where
P: PoolChainApi + Sync + Send + 'static,
{
/// Create new instance of Authoring API.
pub fn new(client: Arc<Client<B, E, <P as PoolChainApi>::Block>>, pool: Arc<Pool<P>>, executor: TaskExecutor) -> Self {
Author {
client,
pool,
subscriptions: Subscriptions::new(executor),
}
}
}
impl<B, E, P> AuthorApi<ExHash<P>, ExtrinsicFor<P>, AllExtrinsics<P>> for Author<B, E, P> where
B: client::backend::Backend<<P as PoolChainApi>::Block, Blake2Hasher, RlpCodec> + Send + Sync + 'static,
E: client::CallExecutor<<P as PoolChainApi>::Block, Blake2Hasher, RlpCodec> + Send + Sync + 'static,
P: PoolChainApi + Sync + Send + 'static,
P::Error: 'static,
{
type Metadata = ::metadata::Metadata;
fn submit_extrinsic(&self, xt: Bytes) -> Result<ExHash<P>> {
let dxt = Decode::decode(&mut &xt[..]).ok_or(error::Error::from(error::ErrorKind::BadFormat))?;
self.submit_rich_extrinsic(dxt)
}
fn submit_rich_extrinsic(&self, xt: <<P as PoolChainApi>::Block as traits::Block>::Extrinsic) -> Result<ExHash<P>> {
let best_block_hash = self.client.info()?.chain.best_hash;
self.pool
.submit_one(&generic::BlockId::hash(best_block_hash), xt)
.map_err(|e| e.into_pool_error()
.map(Into::into)
.unwrap_or_else(|e| error::ErrorKind::Verification(Box::new(e)).into())
)
.map(|ex| ex.hash().clone())
}
fn pending_extrinsics(&self) -> Result<AllExtrinsics<P>> {
Ok(self.pool.all())
}
fn watch_extrinsic(&self, _metadata: Self::Metadata, subscriber: pubsub::Subscriber<Status<ExHash<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[..]).ok_or(error::Error::from(error::ErrorKind::BadFormat))?;
self.pool
.submit_and_watch(&generic::BlockId::hash(best_block_hash), dxt)
.map_err(|e| e.into_pool_error()
.map(Into::into)
.unwrap_or_else(|e| error::ErrorKind::Verification(Box::new(e)).into())
)
};
let watcher = match submit() {
Ok(watcher) => watcher,
Err(err) => {
// reject the subscriber (ignore errors - we don't care if subscriber is no longer there).
let _ = subscriber.reject(err.into());
return;
},
};
self.subscriptions.add(subscriber, move |sink| {
sink
.sink_map_err(|e| warn!("Error sending notifications: {:?}", e))
.send_all(watcher.into_stream().map(Ok))
.map(|_| ())
})
}
fn unwatch_extrinsic(&self, id: SubscriptionId) -> Result<bool> {
Ok(self.subscriptions.cancel(id))
}
}
+178
View File
@@ -0,0 +1,178 @@
// Copyright 2017 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, result::Result};
use codec::Encode;
use extrinsic_pool::{VerifiedTransaction, scoring, Transaction, ChainApi, Error as PoolError,
Readiness, ExtrinsicFor, VerifiedFor};
use test_client::runtime::{Block, Extrinsic, Transfer};
use test_client;
use tokio::runtime;
use runtime_primitives::generic::BlockId;
#[derive(Clone, Debug)]
pub struct Verified
{
sender: u64,
hash: u64,
}
impl VerifiedTransaction for Verified {
type Hash = u64;
type Sender = u64;
fn hash(&self) -> &Self::Hash { &self.hash }
fn sender(&self) -> &Self::Sender { &self.sender }
fn mem_usage(&self) -> usize { 256 }
}
struct TestApi;
impl ChainApi for TestApi {
type Block = Block;
type Hash = u64;
type Sender = u64;
type Error = PoolError;
type VEx = Verified;
type Score = u64;
type Event = ();
type Ready = ();
fn verify_transaction(&self, _at: &BlockId<Block>, uxt: &ExtrinsicFor<Self>) -> Result<Self::VEx, Self::Error> {
Ok(Verified {
sender: uxt.transfer.from[31] as u64,
hash: uxt.transfer.nonce,
})
}
fn is_ready(&self, _at: &BlockId<Block>, _c: &mut Self::Ready, _xt: &VerifiedFor<Self>) -> Readiness {
Readiness::Ready
}
fn ready(&self) -> Self::Ready { }
fn compare(old: &VerifiedFor<Self>, other: &VerifiedFor<Self>) -> ::std::cmp::Ordering {
old.verified.hash().cmp(&other.verified.hash())
}
fn choose(_old: &VerifiedFor<Self>, _new: &VerifiedFor<Self>) -> scoring::Choice {
scoring::Choice::ReplaceOld
}
fn update_scores(xts: &[Transaction<VerifiedFor<Self>>], scores: &mut [Self::Score], _change: scoring::Change<()>) {
for i in 0..xts.len() {
scores[i] = xts[i].verified.sender
}
}
fn should_replace(_old: &VerifiedFor<Self>, _new: &VerifiedFor<Self>) -> scoring::Choice {
scoring::Choice::ReplaceOld
}
}
type DummyTxPool = Pool<TestApi>;
fn uxt(sender: u64, hash: u64) -> Extrinsic {
Extrinsic {
signature: Default::default(),
transfer: Transfer {
amount: Default::default(),
nonce: hash,
from: From::from(sender),
to: Default::default(),
}
}
}
#[test]
fn submit_transaction_should_not_cause_error() {
let runtime = runtime::Runtime::new().unwrap();
let p = Author {
client: Arc::new(test_client::new()),
pool: Arc::new(DummyTxPool::new(Default::default(), TestApi)),
subscriptions: Subscriptions::new(runtime.executor()),
};
assert_matches!(
AuthorApi::submit_extrinsic(&p, uxt(5, 1).encode().into()),
Ok(1)
);
assert!(
AuthorApi::submit_extrinsic(&p, uxt(5, 1).encode().into()).is_err()
);
}
#[test]
fn submit_rich_transaction_should_not_cause_error() {
let runtime = runtime::Runtime::new().unwrap();
let p = Author {
client: Arc::new(test_client::new()),
pool: Arc::new(DummyTxPool::new(Default::default(), TestApi)),
subscriptions: Subscriptions::new(runtime.executor()),
};
assert_matches!(
AuthorApi::submit_rich_extrinsic(&p, uxt(5, 0)),
Ok(0)
);
assert!(
AuthorApi::submit_rich_extrinsic(&p, uxt(5, 0)).is_err()
);
}
#[test]
fn should_watch_extrinsic() {
//given
let mut runtime = runtime::Runtime::new().unwrap();
let pool = Arc::new(DummyTxPool::new(Default::default(), TestApi));
let p = Author {
client: Arc::new(test_client::new()),
pool: pool.clone(),
subscriptions: Subscriptions::new(runtime.executor()),
};
let (subscriber, id_rx, data) = ::jsonrpc_macros::pubsub::Subscriber::new_test("test");
// when
p.watch_extrinsic(Default::default(), subscriber, uxt(5, 5).encode().into());
// then
assert_eq!(runtime.block_on(id_rx), Ok(Ok(0.into())));
// check notifications
AuthorApi::submit_rich_extrinsic(&p, uxt(5, 1)).unwrap();
assert_eq!(
runtime.block_on(data.into_future()).unwrap().0,
Some(r#"{"jsonrpc":"2.0","method":"test","params":{"result":{"usurped":1},"subscription":0}}"#.into())
);
}
#[test]
fn should_return_pending_extrinsics() {
let runtime = runtime::Runtime::new().unwrap();
let pool = Arc::new(DummyTxPool::new(Default::default(), TestApi));
let p = Author {
client: Arc::new(test_client::new()),
pool: pool.clone(),
subscriptions: Subscriptions::new(runtime.executor()),
};
let ex = uxt(5, 1);
AuthorApi::submit_rich_extrinsic(&p, ex.clone()).unwrap();
assert_matches!(
p.pending_extrinsics(),
Ok(ref expected) if expected.get(&5) == Some(&vec![ex])
);
}
+42
View File
@@ -0,0 +1,42 @@
// Copyright 2017 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 client;
use rpc;
use errors;
error_chain! {
links {
Client(client::error::Error, client::error::ErrorKind) #[doc = "Client error"];
}
errors {
/// Not implemented yet
Unimplemented {
description("not yet implemented"),
display("Method Not Implemented"),
}
}
}
impl From<Error> for rpc::Error {
fn from(e: Error) -> Self {
match e {
Error(ErrorKind::Unimplemented, _) => errors::unimplemented(),
e => errors::internal(e),
}
}
}
+165
View File
@@ -0,0 +1,165 @@
// Copyright 2017 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.
use std::sync::Arc;
use client::{self, Client, BlockchainEvents};
use jsonrpc_macros::{pubsub, Trailing};
use jsonrpc_pubsub::SubscriptionId;
use rpc::Result as RpcResult;
use rpc::futures::{stream, Future, Sink, Stream};
use runtime_primitives::generic::{BlockId, SignedBlock};
use runtime_primitives::traits::{Block as BlockT, Header, NumberFor};
use runtime_version::RuntimeVersion;
use tokio::runtime::TaskExecutor;
use primitives::{Blake2Hasher, RlpCodec};
use subscriptions::Subscriptions;
mod error;
#[cfg(test)]
mod tests;
use self::error::Result;
build_rpc_trait! {
/// Polkadot blockchain API
pub trait ChainApi<Hash, Header, Number, Extrinsic> {
type Metadata;
/// Get header of a relay chain block.
#[rpc(name = "chain_getHeader")]
fn header(&self, Trailing<Hash>) -> Result<Option<Header>>;
/// Get header and body of a relay chain block.
#[rpc(name = "chain_getBlock")]
fn block(&self, Trailing<Hash>) -> Result<Option<SignedBlock<Header, Extrinsic, Hash>>>;
/// 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, Trailing<Number>) -> Result<Option<Hash>>;
/// Get the runtime version.
#[rpc(name = "chain_getRuntimeVersion")]
fn runtime_version(&self, Trailing<Hash>) -> Result<RuntimeVersion>;
#[pubsub(name = "chain_newHead")] {
/// New head subscription
#[rpc(name = "chain_subscribeNewHead", alias = ["subscribe_newHead", ])]
fn subscribe_new_head(&self, Self::Metadata, pubsub::Subscriber<Header>);
/// Unsubscribe from new head subscription.
#[rpc(name = "chain_unsubscribeNewHead", alias = ["unsubscribe_newHead", ])]
fn unsubscribe_new_head(&self, SubscriptionId) -> RpcResult<bool>;
}
}
}
/// Chain API with subscriptions support.
pub struct Chain<B, E, Block: BlockT> {
/// Substrate client.
client: Arc<Client<B, E, Block>>,
/// Current subscriptions.
subscriptions: Subscriptions,
}
impl<B, E, Block: BlockT> Chain<B, E, Block> {
/// Create new Chain API RPC handler.
pub fn new(client: Arc<Client<B, E, Block>>, executor: TaskExecutor) -> Self {
Self {
client,
subscriptions: Subscriptions::new(executor),
}
}
}
impl<B, E, Block> Chain<B, E, Block> where
Block: BlockT + 'static,
B: client::backend::Backend<Block, Blake2Hasher, RlpCodec> + Send + Sync + 'static,
E: client::CallExecutor<Block, Blake2Hasher, RlpCodec> + Send + Sync + 'static,
{
fn unwrap_or_best(&self, hash: Trailing<Block::Hash>) -> Result<Block::Hash> {
Ok(match hash.into() {
None => self.client.info()?.chain.best_hash,
Some(hash) => hash,
})
}
}
impl<B, E, Block> ChainApi<Block::Hash, Block::Header, NumberFor<Block>, Block::Extrinsic> for Chain<B, E, Block> where
Block: BlockT + 'static,
B: client::backend::Backend<Block, Blake2Hasher, RlpCodec> + Send + Sync + 'static,
E: client::CallExecutor<Block, Blake2Hasher, RlpCodec> + Send + Sync + 'static,
{
type Metadata = ::metadata::Metadata;
fn header(&self, hash: Trailing<Block::Hash>) -> Result<Option<Block::Header>> {
let hash = self.unwrap_or_best(hash)?;
Ok(self.client.header(&BlockId::Hash(hash))?)
}
fn block(&self, hash: Trailing<Block::Hash>) -> Result<Option<SignedBlock<Block::Header, Block::Extrinsic, Block::Hash>>> {
let hash = self.unwrap_or_best(hash)?;
Ok(self.client.block(&BlockId::Hash(hash))?)
}
fn block_hash(&self, number: Trailing<NumberFor<Block>>) -> Result<Option<Block::Hash>> {
Ok(match number.into() {
None => Some(self.client.info()?.chain.best_hash),
Some(number) => self.client.header(&BlockId::number(number))?.map(|h| h.hash()),
})
}
fn runtime_version(&self, at: Trailing<Block::Hash>) -> Result<RuntimeVersion> {
let at = self.unwrap_or_best(at)?;
Ok(self.client.runtime_version_at(&BlockId::Hash(at))?)
}
fn subscribe_new_head(&self, _metadata: Self::Metadata, subscriber: pubsub::Subscriber<Block::Header>) {
self.subscriptions.add(subscriber, |sink| {
// send current head right at the start.
let header = self.block_hash(None.into())
.and_then(|hash| self.header(hash.into()))
.and_then(|header| {
header.ok_or_else(|| self::error::ErrorKind::Unimplemented.into())
})
.map_err(Into::into);
// send further subscriptions
let stream = self.client.import_notification_stream()
.filter(|notification| notification.is_new_best)
.map(|notification| Ok(notification.header))
.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 unsubscribe_new_head(&self, id: SubscriptionId) -> RpcResult<bool> {
Ok(self.subscriptions.cancel(id))
}
}
+207
View File
@@ -0,0 +1,207 @@
// Copyright 2017 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 jsonrpc_macros::pubsub;
use client::BlockOrigin;
use test_client::{self, TestClient};
use test_client::runtime::{Block, Header};
#[test]
fn should_return_header() {
let core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let client = Chain {
client: Arc::new(test_client::new()),
subscriptions: Subscriptions::new(remote),
};
assert_matches!(
client.header(Some(client.client.genesis_hash()).into()),
Ok(Some(ref x)) if x == &Header {
parent_hash: 0.into(),
number: 0,
state_root: x.state_root.clone(),
extrinsics_root: "45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0".into(),
digest: Default::default(),
}
);
assert_matches!(
client.header(None.into()),
Ok(Some(ref x)) if x == &Header {
parent_hash: 0.into(),
number: 0,
state_root: x.state_root.clone(),
extrinsics_root: "45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0".into(),
digest: Default::default(),
}
);
assert_matches!(
client.header(Some(5.into()).into()),
Ok(None)
);
}
#[test]
fn should_return_a_block() {
let core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let api = Chain {
client: Arc::new(test_client::new()),
subscriptions: Subscriptions::new(remote),
};
let block = api.client.new_block().unwrap().bake().unwrap();
let block_hash = block.hash();
api.client.justify_and_import(BlockOrigin::Own, block).unwrap();
// Genesis block is not justified, so we can't query it?
assert_matches!(
api.block(Some(api.client.genesis_hash()).into()),
Ok(None)
);
assert_matches!(
api.block(Some(block_hash).into()),
Ok(Some(ref x)) if x.block == Block {
header: Header {
parent_hash: api.client.genesis_hash(),
number: 1,
state_root: x.block.header.state_root.clone(),
extrinsics_root: "45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0".into(),
digest: Default::default(),
},
extrinsics: vec![],
}
);
assert_matches!(
api.block(None.into()),
Ok(Some(ref x)) if x.block == Block {
header: Header {
parent_hash: api.client.genesis_hash(),
number: 1,
state_root: x.block.header.state_root.clone(),
extrinsics_root: "45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0".into(),
digest: Default::default(),
},
extrinsics: vec![],
}
);
assert_matches!(
api.block(Some(5.into()).into()),
Ok(None)
);
}
#[test]
fn should_return_block_hash() {
let core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let client = Chain {
client: Arc::new(test_client::new()),
subscriptions: Subscriptions::new(remote),
};
assert_matches!(
client.block_hash(None.into()),
Ok(Some(ref x)) if x == &client.client.genesis_hash()
);
assert_matches!(
client.block_hash(Some(0u64).into()),
Ok(Some(ref x)) if x == &client.client.genesis_hash()
);
assert_matches!(
client.block_hash(Some(1u64).into()),
Ok(None)
);
let block = client.client.new_block().unwrap().bake().unwrap();
client.client.justify_and_import(BlockOrigin::Own, block.clone()).unwrap();
assert_matches!(
client.block_hash(Some(0u64).into()),
Ok(Some(ref x)) if x == &client.client.genesis_hash()
);
assert_matches!(
client.block_hash(Some(1u64).into()),
Ok(Some(ref x)) if x == &block.hash()
);
}
#[test]
fn should_notify_about_latest_block() {
let mut core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let (subscriber, id, transport) = pubsub::Subscriber::new_test("test");
{
let api = Chain {
client: Arc::new(test_client::new()),
subscriptions: Subscriptions::new(remote),
};
api.subscribe_new_head(Default::default(), subscriber);
// assert id assigned
assert_eq!(core.block_on(id), Ok(Ok(SubscriptionId::Number(0))));
let builder = api.client.new_block().unwrap();
api.client.justify_and_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_return_runtime_version() {
let core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let client = Chain {
client: Arc::new(test_client::new()),
subscriptions: Subscriptions::new(remote),
};
assert_matches!(
client.runtime_version(None.into()),
Ok(ref ver) if ver == &RuntimeVersion {
spec_name: "test".into(),
impl_name: "parity-test".into(),
authoring_version: 1,
spec_version: 1,
impl_version: 1,
}
);
}
+34
View File
@@ -0,0 +1,34 @@
// Copyright 2018 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 rpc;
pub fn unimplemented() -> rpc::Error {
rpc::Error {
code: rpc::ErrorCode::ServerError(1),
message: "Not implemented yet".into(),
data: None,
}
}
pub fn internal<E: ::std::fmt::Debug>(e: E) -> rpc::Error {
warn!("Unknown error: {:?}", e);
rpc::Error {
code: rpc::ErrorCode::InternalError,
message: "Unknown error occured".into(),
data: Some(format!("{:?}", e).into()),
}
}
+25
View File
@@ -0,0 +1,25 @@
// Copyright 2018 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/>.
/// Unwraps the trailing parameter or falls back with the closure result.
pub fn unwrap_or_else<F, H, E>(or_else: F, optional: ::jsonrpc_macros::Trailing<H>) -> Result<H, E> where
F: FnOnce() -> Result<H, E>,
{
match optional.into() {
None => or_else(),
Some(x) => Ok(x),
}
}
+59
View File
@@ -0,0 +1,59 @@
// Copyright 2017 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/>.
// tag::description[]
//! Substrate RPC interfaces.
// end::description[]
#![warn(missing_docs)]
extern crate jsonrpc_core as rpc;
extern crate jsonrpc_pubsub;
extern crate parking_lot;
extern crate parity_codec as codec;
extern crate substrate_client as client;
extern crate substrate_extrinsic_pool as extrinsic_pool;
extern crate substrate_primitives as primitives;
extern crate sr_primitives as runtime_primitives;
extern crate substrate_state_machine as state_machine;
extern crate sr_version as runtime_version;
extern crate tokio;
extern crate serde_json;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate jsonrpc_macros;
#[macro_use]
extern crate log;
#[cfg(test)]
#[macro_use]
extern crate assert_matches;
#[cfg(test)]
extern crate substrate_test_client as test_client;
#[cfg(test)]
extern crate rustc_hex;
mod errors;
mod helpers;
mod subscriptions;
pub mod author;
pub mod chain;
pub mod metadata;
pub mod state;
pub mod system;
+54
View File
@@ -0,0 +1,54 @@
// Copyright 2017-2018 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))
}
}
+54
View File
@@ -0,0 +1,54 @@
// Copyright 2017 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 client;
use rpc;
use errors;
use serde_json;
error_chain! {
links {
Client(client::error::Error, client::error::ErrorKind) #[doc = "Client error"];
}
foreign_links {
Json(serde_json::Error);
}
errors {
/// Provided block range couldn't be resolved to a list of blocks.
InvalidBlockRange(from: String, to: String, details: String) {
description("Invalid block range"),
display("Cannot resolve a block range ['{:?}' ... '{:?}]. {}", from, to, details),
}
/// Not implemented yet
Unimplemented {
description("not implemented yet"),
display("Method Not Implemented"),
}
}
}
impl From<Error> for rpc::Error {
fn from(e: Error) -> Self {
match e {
Error(ErrorKind::Unimplemented, _) => errors::unimplemented(),
e => errors::internal(e),
}
}
}
+277
View File
@@ -0,0 +1,277 @@
// Copyright 2017 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/>.
//! Polkadot state API.
use std::{
collections::HashMap,
sync::Arc,
};
use client::{self, Client, CallExecutor, BlockchainEvents};
use jsonrpc_macros::Trailing;
use jsonrpc_macros::pubsub;
use jsonrpc_pubsub::SubscriptionId;
use primitives::hexdisplay::HexDisplay;
use primitives::storage::{StorageKey, StorageData, StorageChangeSet};
use primitives::{Blake2Hasher, RlpCodec};
use rpc::Result as RpcResult;
use rpc::futures::{stream, Future, Sink, Stream};
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{Block as BlockT, Header};
use tokio::runtime::TaskExecutor;
use serde_json;
use subscriptions::Subscriptions;
mod error;
#[cfg(test)]
mod tests;
use self::error::Result;
build_rpc_trait! {
/// Polkadot state API
pub trait StateApi<Hash> {
type Metadata;
/// Call a contract at a block's state.
#[rpc(name = "state_call", alias = ["state_callAt", ])]
fn call(&self, String, Vec<u8>, Trailing<Hash>) -> Result<Vec<u8>>;
/// Returns a storage entry at a specific block's state.
#[rpc(name = "state_getStorage", alias = ["state_getStorageAt", ])]
fn storage(&self, StorageKey, Trailing<Hash>) -> Result<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, StorageKey, Trailing<Hash>) -> Result<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, StorageKey, Trailing<Hash>) -> Result<Option<u64>>;
/// Returns the runtime metadata as JSON.
#[rpc(name = "state_getMetadata")]
fn metadata(&self, Trailing<Hash>) -> Result<serde_json::Value>;
/// 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, Vec<StorageKey>, Hash, Trailing<Hash>) -> Result<Vec<StorageChangeSet<Hash>>>;
#[pubsub(name = "state_storage")] {
/// New storage subscription
#[rpc(name = "state_subscribeStorage")]
fn subscribe_storage(&self, Self::Metadata, pubsub::Subscriber<StorageChangeSet<Hash>>, Trailing<Vec<StorageKey>>);
/// Unsubscribe from storage subscription
#[rpc(name = "state_unsubscribeStorage")]
fn unsubscribe_storage(&self, SubscriptionId) -> RpcResult<bool>;
}
}
}
/// State API with subscriptions support.
pub struct State<B, E, Block: BlockT> {
/// Substrate client.
client: Arc<Client<B, E, Block>>,
/// Current subscriptions.
subscriptions: Subscriptions,
}
impl<B, E, Block: BlockT> State<B, E, Block> {
/// Create new State API RPC handler.
pub fn new(client: Arc<Client<B, E, Block>>, executor: TaskExecutor) -> Self {
Self {
client,
subscriptions: Subscriptions::new(executor),
}
}
}
impl<B, E, Block> State<B, E, Block> where
Block: BlockT,
B: client::backend::Backend<Block, Blake2Hasher, RlpCodec>,
E: CallExecutor<Block, Blake2Hasher, RlpCodec>,
{
fn unwrap_or_best(&self, hash: Trailing<Block::Hash>) -> Result<Block::Hash> {
::helpers::unwrap_or_else(|| Ok(self.client.info()?.chain.best_hash), hash)
}
}
impl<B, E, Block> StateApi<Block::Hash> for State<B, E, Block> where
Block: BlockT + 'static,
B: client::backend::Backend<Block, Blake2Hasher, RlpCodec> + Send + Sync + 'static,
E: CallExecutor<Block, Blake2Hasher, RlpCodec> + Send + Sync + 'static,
{
type Metadata = ::metadata::Metadata;
fn call(&self, method: String, data: Vec<u8>, block: Trailing<Block::Hash>) -> Result<Vec<u8>> {
let block = self.unwrap_or_best(block)?;
trace!(target: "rpc", "Calling runtime at {:?} for method {} ({})", block, method, HexDisplay::from(&data));
Ok(self.client.executor().call(&BlockId::Hash(block), &method, &data)?.return_data)
}
fn storage(&self, key: StorageKey, block: Trailing<Block::Hash>) -> Result<Option<StorageData>> {
let block = self.unwrap_or_best(block)?;
trace!(target: "rpc", "Querying storage at {:?} for key {}", block, HexDisplay::from(&key.0));
Ok(self.client.storage(&BlockId::Hash(block), &key)?)
}
fn storage_hash(&self, key: StorageKey, block: Trailing<Block::Hash>) -> Result<Option<Block::Hash>> {
use runtime_primitives::traits::{Hash, Header as HeaderT};
Ok(self.storage(key, block)?.map(|x| <Block::Header as HeaderT>::Hashing::hash(&x.0)))
}
fn storage_size(&self, key: StorageKey, block: Trailing<Block::Hash>) -> Result<Option<u64>> {
Ok(self.storage(key, block)?.map(|x| x.0.len() as u64))
}
fn metadata(&self, block: Trailing<Block::Hash>) -> Result<serde_json::Value> {
let block = self.unwrap_or_best(block)?;
let metadata = self.client.json_metadata(&BlockId::Hash(block))?;
serde_json::from_str(&metadata).map_err(Into::into)
}
fn query_storage(&self, keys: Vec<StorageKey>, from: Block::Hash, to: Trailing<Block::Hash>) -> Result<Vec<StorageChangeSet<Block::Hash>>> {
let to = self.unwrap_or_best(to)?;
let from_hdr = self.client.header(&BlockId::hash(from))?;
let to_hdr = self.client.header(&BlockId::hash(to))?;
match (from_hdr, to_hdr) {
(Some(ref from), Some(ref to)) if from.number() <= to.number() => {
let from = from.clone();
let to = to.clone();
// check if we can get from `to` to `from` by going through parent_hashes.
let blocks = {
let mut blocks = vec![to.hash()];
let mut last = to.clone();
while last.number() > from.number() {
if let Some(hdr) = self.client.header(&BlockId::hash(*last.parent_hash()))? {
blocks.push(hdr.hash());
last = hdr;
} else {
bail!(invalid_block_range(
Some(from),
Some(to),
format!("Parent of {} ({}) not found", last.number(), last.hash()),
))
}
}
if last.hash() != from.hash() {
bail!(invalid_block_range(
Some(from),
Some(to),
format!("Expected to reach `from`, got {} ({})", last.number(), last.hash()),
))
}
blocks.reverse();
blocks
};
let mut result = Vec::new();
let mut last_state: HashMap<_, Option<_>> = Default::default();
for block in blocks {
let mut changes = vec![];
let id = BlockId::hash(block.clone());
for key in &keys {
let (has_changed, data) = {
let curr_data = self.client.storage(&id, key)?;
let prev_data = last_state.get(key).and_then(|x| x.as_ref());
(curr_data.as_ref() != prev_data, curr_data)
};
if has_changed {
changes.push((key.clone(), data.clone()));
}
last_state.insert(key.clone(), data);
}
result.push(StorageChangeSet {
block,
changes,
});
}
Ok(result)
},
(from, to) => bail!(invalid_block_range(from, to, "Invalid range or unknown block".into())),
}
}
fn subscribe_storage(
&self,
_meta: Self::Metadata,
subscriber: pubsub::Subscriber<StorageChangeSet<Block::Hash>>,
keys: Trailing<Vec<StorageKey>>
) {
let keys = Into::<Option<Vec<_>>>::into(keys);
let stream = match self.client.storage_changes_notification_stream(keys.as_ref().map(|x| &**x)) {
Ok(stream) => stream,
Err(err) => {
let _ = subscriber.reject(error::Error::from(err).into());
return;
},
};
// initial values
let initial = stream::iter_result(keys
.map(|keys| {
let block = self.client.info().map(|info| info.chain.best_hash).unwrap_or_default();
let changes = keys
.into_iter()
.map(|key| self.storage(key.clone(), Some(block.clone()).into())
.map(|val| (key.clone(), val))
.unwrap_or_else(|_| (key, None))
)
.collect();
vec![Ok(Ok(StorageChangeSet { block, changes }))]
}).unwrap_or_default());
self.subscriptions.add(subscriber, |sink| {
let stream = stream
.map_err(|e| warn!("Error creating storage notification stream: {:?}", e))
.map(|(block, changes)| Ok(StorageChangeSet {
block,
changes: changes.iter().cloned().collect(),
}));
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, id: SubscriptionId) -> RpcResult<bool> {
Ok(self.subscriptions.cancel(id))
}
}
fn invalid_block_range<H: Header>(from: Option<H>, to: Option<H>, reason: String) -> error::ErrorKind {
let to_string = |x: Option<H>| match x {
None => "unknown hash".into(),
Some(h) => format!("{} ({})", h.number(), h.hash()),
};
error::ErrorKind::InvalidBlockRange(to_string(from), to_string(to), reason)
}
+186
View File
@@ -0,0 +1,186 @@
// Copyright 2017 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 self::error::{Error, ErrorKind};
use client::BlockOrigin;
use jsonrpc_macros::pubsub;
use rustc_hex::FromHex;
use test_client::{self, runtime, keyring::Keyring, TestClient, BlockBuilderExt};
#[test]
fn should_return_storage() {
let core = ::tokio::runtime::Runtime::new().unwrap();
let client = Arc::new(test_client::new());
let genesis_hash = client.genesis_hash();
let client = State::new(client, core.executor());
assert_matches!(
client.storage(StorageKey(vec![10]), Some(genesis_hash).into()),
Ok(None)
)
}
#[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 = State::new(client, core.executor());
assert_matches!(
client.call("balanceOf".into(), vec![1,2,3], Some(genesis_hash).into()),
Err(Error(ErrorKind::Client(client::error::ErrorKind::Execution(_)), _))
)
}
#[test]
fn should_notify_about_storage_changes() {
let mut core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let (subscriber, id, transport) = pubsub::Subscriber::new_test("test");
{
let api = State {
client: Arc::new(test_client::new()),
subscriptions: Subscriptions::new(remote),
};
api.subscribe_storage(Default::default(), subscriber, None.into());
// assert id assigned
assert_eq!(core.block_on(id), Ok(Ok(SubscriptionId::Number(0))));
let mut builder = api.client.new_block().unwrap();
builder.push_transfer(runtime::Transfer {
from: Keyring::Alice.to_raw_public().into(),
to: Keyring::Ferdie.to_raw_public().into(),
amount: 42,
nonce: 0,
}).unwrap();
api.client.justify_and_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) = pubsub::Subscriber::new_test("test");
{
let api = State {
client: Arc::new(test_client::new()),
subscriptions: Subscriptions::new(remote),
};
api.subscribe_storage(Default::default(), subscriber, Some(vec![
StorageKey("a52da2b7c269da1366b3ed1cdb7299ce".from_hex().unwrap()),
]).into());
// assert id assigned
assert_eq!(core.block_on(id), Ok(Ok(SubscriptionId::Number(0))));
let mut builder = api.client.new_block().unwrap();
builder.push_transfer(runtime::Transfer {
from: Keyring::Alice.to_raw_public().into(),
to: Keyring::Ferdie.to_raw_public().into(),
amount: 42,
nonce: 0,
}).unwrap();
api.client.justify_and_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() {
let core = ::tokio::runtime::Runtime::new().unwrap();
let client = Arc::new(test_client::new());
let api = State::new(client.clone(), core.executor());
let add_block = |nonce| {
let mut builder = client.new_block().unwrap();
builder.push_transfer(runtime::Transfer {
from: Keyring::Alice.to_raw_public().into(),
to: Keyring::Ferdie.to_raw_public().into(),
amount: 42,
nonce,
}).unwrap();
let block = builder.bake().unwrap();
let hash = block.header.hash();
client.justify_and_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("a52da2b7c269da1366b3ed1cdb7299ce".from_hex().unwrap()), Some(StorageData(vec![232, 3, 0, 0, 0, 0, 0, 0]))),
],
},
StorageChangeSet {
block: block1_hash,
changes: vec![
(StorageKey("a52da2b7c269da1366b3ed1cdb7299ce".from_hex().unwrap()), Some(StorageData(vec![190, 3, 0, 0, 0, 0, 0, 0]))),
],
},
];
// Query changes only up to block1
let result = api.query_storage(
vec![StorageKey("a52da2b7c269da1366b3ed1cdb7299ce".from_hex().unwrap())],
genesis_hash,
Some(block1_hash).into(),
);
assert_eq!(result.unwrap(), expected);
// Query all changes
let result = api.query_storage(
vec![StorageKey("a52da2b7c269da1366b3ed1cdb7299ce".from_hex().unwrap())],
genesis_hash,
None.into(),
);
expected.push(StorageChangeSet {
block: block2_hash,
changes: vec![
(StorageKey("a52da2b7c269da1366b3ed1cdb7299ce".from_hex().unwrap()), Some(StorageData(vec![148, 3, 0, 0, 0, 0, 0, 0]))),
],
});
assert_eq!(result.unwrap(), expected);
}
+85
View File
@@ -0,0 +1,85 @@
// Copyright 2017-2018 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::atomic::{self, AtomicUsize};
use jsonrpc_macros::pubsub;
use jsonrpc_pubsub::SubscriptionId;
use parking_lot::Mutex;
use rpc::futures::sync::oneshot;
use rpc::futures::{Future, future};
use tokio::runtime::TaskExecutor;
type Id = u64;
/// Subscriptions manager.
///
/// Takes care of assigning unique subscription ids and
/// driving the sinks into completion.
#[derive(Debug)]
pub struct Subscriptions {
next_id: AtomicUsize,
active_subscriptions: 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,
}
}
/// 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 bu underlying event loop
/// or will be cancelled in case #cancel is invoked.
pub fn add<T, E, G, R, F>(&self, subscriber: pubsub::Subscriber<T, E>, into_future: G) where
G: FnOnce(pubsub::Sink<T, E>) -> R,
R: future::IntoFuture<Future=F, Item=(), Error=()>,
F: future::Future<Item=(), Error=()> + Send + 'static,
{
let id = self.next_id.fetch_add(1, atomic::Ordering::AcqRel) as u64;
if let Ok(sink) = subscriber.assign_id(id.into()) {
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);
self.executor.spawn(future);
}
}
/// 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
}
}
+40
View File
@@ -0,0 +1,40 @@
// Copyright 2017 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 rpc;
use errors;
error_chain! {
errors {
/// Not implemented yet
Unimplemented {
description("not yet implemented"),
display("Method Not Implemented"),
}
}
}
impl From<Error> for rpc::Error {
fn from(e: Error) -> Self {
match e {
Error(ErrorKind::Unimplemented, _) => errors::unimplemented(),
e => errors::internal(e),
}
}
}
+41
View File
@@ -0,0 +1,41 @@
// Copyright 2017 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;
#[cfg(test)]
mod tests;
use self::error::Result;
build_rpc_trait! {
/// Substrate system RPC API
pub trait SystemApi {
/// 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>;
}
}
+54
View File
@@ -0,0 +1,54 @@
// Copyright 2017 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::error::*;
impl SystemApi for () {
fn system_name(&self) -> Result<String> {
Ok("testclient".into())
}
fn system_version(&self) -> Result<String> {
Ok("0.2.0".into())
}
fn system_chain(&self) -> Result<String> {
Ok("testchain".into())
}
}
#[test]
fn system_name_works() {
assert_eq!(
SystemApi::system_name(&()).unwrap(),
"testclient".to_owned()
);
}
#[test]
fn system_version_works() {
assert_eq!(
SystemApi::system_version(&()).unwrap(),
"0.2.0".to_owned()
);
}
#[test]
fn system_chain_works() {
assert_eq!(
SystemApi::system_chain(&()).unwrap(),
"testchain".to_owned()
);
}