From cfd617a2bd44d796c52f6bc81391d626d720a489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Tue, 14 Aug 2018 12:34:05 +0200 Subject: [PATCH 1/2] Get block RPC. --- substrate/rpc/src/chain/mod.rs | 14 ++++++++--- substrate/rpc/src/chain/tests.rs | 43 +++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/substrate/rpc/src/chain/mod.rs b/substrate/rpc/src/chain/mod.rs index 3c0491c4226e9..5e00c7f80ab49 100644 --- a/substrate/rpc/src/chain/mod.rs +++ b/substrate/rpc/src/chain/mod.rs @@ -23,7 +23,7 @@ use jsonrpc_macros::pubsub; use jsonrpc_pubsub::SubscriptionId; use rpc::Result as RpcResult; use rpc::futures::{stream, Future, Sink, Stream}; -use runtime_primitives::generic::BlockId; +use runtime_primitives::generic::{BlockId, SignedBlock}; use runtime_primitives::traits::Block as BlockT; use tokio::runtime::TaskExecutor; @@ -37,13 +37,17 @@ use self::error::Result; build_rpc_trait! { /// Polkadot blockchain API - pub trait ChainApi { + pub trait ChainApi { type Metadata; /// Get header of a relay chain block. #[rpc(name = "chain_getHeader")] fn header(&self, Hash) -> Result>; + /// Get header and body of a relay chain block. + #[rpc(name = "chain_getBlock")] + fn block(&self, Hash) -> Result>>; + /// Get hash of the head. #[rpc(name = "chain_getHead")] fn head(&self) -> Result; @@ -78,7 +82,7 @@ impl Chain { } } -impl ChainApi for Chain where +impl ChainApi for Chain where Block: BlockT + 'static, B: client::backend::Backend + Send + Sync + 'static, E: client::CallExecutor + Send + Sync + 'static, @@ -89,6 +93,10 @@ impl ChainApi for Chain wh Ok(self.client.header(&BlockId::Hash(hash))?) } + fn block(&self, hash: Block::Hash) -> Result>> { + Ok(self.client.block(&BlockId::Hash(hash))?) + } + fn head(&self) -> Result { Ok(self.client.info()?.chain.best_hash) } diff --git a/substrate/rpc/src/chain/tests.rs b/substrate/rpc/src/chain/tests.rs index 1f0a3a9d0108c..eac9abd5c4ac2 100644 --- a/substrate/rpc/src/chain/tests.rs +++ b/substrate/rpc/src/chain/tests.rs @@ -18,7 +18,7 @@ use super::*; use jsonrpc_macros::pubsub; use client::BlockOrigin; use test_client::{self, TestClient}; -use test_client::runtime::Header; +use test_client::runtime::{Block, Header}; #[test] fn should_return_header() { @@ -46,6 +46,47 @@ fn should_return_header() { ); } +#[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(api.client.genesis_hash()), + Ok(None) + ); + + assert_matches!( + api.block(block_hash), + 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: "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421".into(), + digest: Default::default(), + }, + extrinsics: vec![], + } + ); + + assert_matches!( + api.block(5.into()), + Ok(None) + ); +} + #[test] fn should_notify_about_latest_block() { let mut core = ::tokio::runtime::Runtime::new().unwrap(); From cb9aaff588e31c6356d2bcf925d118a06125dbb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Tue, 14 Aug 2018 14:54:13 +0200 Subject: [PATCH 2/2] Fix rpc-servers. --- substrate/rpc-servers/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/rpc-servers/src/lib.rs b/substrate/rpc-servers/src/lib.rs index 11fa9aa9b60af..0979e405219c0 100644 --- a/substrate/rpc-servers/src/lib.rs +++ b/substrate/rpc-servers/src/lib.rs @@ -46,7 +46,7 @@ pub fn rpc_handler( ) -> RpcHandler where Block: 'static, S: apis::state::StateApi, - C: apis::chain::ChainApi, + C: apis::chain::ChainApi, A: apis::author::AuthorApi, Y: apis::system::SystemApi, {