mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 13:21:10 +00:00
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:
committed by
Arkadiy Paronyan
parent
8fe5aa4c81
commit
1e01162505
@@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user