From 5f124e2e2ba9a38a655587297cdfe83e30fe556a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= <tomusdrw@users.noreply.github.com>
Date: Tue, 14 Aug 2018 21:41:58 +0200
Subject: [PATCH] RPC: Get block (#564)

* Get block RPC.

* Fix rpc-servers.
---
 substrate/substrate/rpc-servers/src/lib.rs |  2 +-
 substrate/substrate/rpc/src/chain/mod.rs   | 14 +++++--
 substrate/substrate/rpc/src/chain/tests.rs | 43 +++++++++++++++++++++-
 3 files changed, 54 insertions(+), 5 deletions(-)

diff --git a/substrate/substrate/rpc-servers/src/lib.rs b/substrate/substrate/rpc-servers/src/lib.rs
index 7344a0453c1..e42996ff6cb 100644
--- a/substrate/substrate/rpc-servers/src/lib.rs
+++ b/substrate/substrate/rpc-servers/src/lib.rs
@@ -48,7 +48,7 @@ pub fn rpc_handler<Block, PendingExtrinsics, S, C, A, Y>(
 	Block: BlockT + 'static,
 	PendingExtrinsics: serde::Serialize + serde::de::DeserializeOwned + Send + Sync + 'static,
 	S: apis::state::StateApi<Block::Hash, Metadata=Metadata>,
-	C: apis::chain::ChainApi<Block::Hash, Block::Header, Metadata=Metadata>,
+	C: apis::chain::ChainApi<Block::Hash, Block::Header, Block::Extrinsic, Metadata=Metadata>,
 	A: apis::author::AuthorApi<Block::Hash, Block::Extrinsic, PendingExtrinsics, Metadata=Metadata>,
 	Y: apis::system::SystemApi,
 {
diff --git a/substrate/substrate/rpc/src/chain/mod.rs b/substrate/substrate/rpc/src/chain/mod.rs
index 3c0491c4226..5e00c7f80ab 100644
--- a/substrate/substrate/rpc/src/chain/mod.rs
+++ b/substrate/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<Hash, Header> {
+	pub trait ChainApi<Hash, Header, Extrinsic> {
 		type Metadata;
 
 		/// Get header of a relay chain block.
 		#[rpc(name = "chain_getHeader")]
 		fn header(&self, Hash) -> Result<Option<Header>>;
 
+		/// Get header and body of a relay chain block.
+		#[rpc(name = "chain_getBlock")]
+		fn block(&self, Hash) -> Result<Option<SignedBlock<Header, Extrinsic, Hash>>>;
+
 		/// Get hash of the head.
 		#[rpc(name = "chain_getHead")]
 		fn head(&self) -> Result<Hash>;
@@ -78,7 +82,7 @@ impl<B, E, Block: BlockT> Chain<B, E, Block> {
 	}
 }
 
-impl<B, E, Block> ChainApi<Block::Hash, Block::Header> for Chain<B, E, Block> where
+impl<B, E, Block> ChainApi<Block::Hash, Block::Header, Block::Extrinsic> for Chain<B, E, Block> where
 	Block: BlockT + 'static,
 	B: client::backend::Backend<Block> + Send + Sync + 'static,
 	E: client::CallExecutor<Block> + Send + Sync + 'static,
@@ -89,6 +93,10 @@ impl<B, E, Block> ChainApi<Block::Hash, Block::Header> for Chain<B, E, Block> wh
 		Ok(self.client.header(&BlockId::Hash(hash))?)
 	}
 
+	fn block(&self, hash: Block::Hash) -> Result<Option<SignedBlock<Block::Header, Block::Extrinsic, Block::Hash>>> {
+		Ok(self.client.block(&BlockId::Hash(hash))?)
+	}
+
 	fn head(&self) -> Result<Block::Hash> {
 		Ok(self.client.info()?.chain.best_hash)
 	}
diff --git a/substrate/substrate/rpc/src/chain/tests.rs b/substrate/substrate/rpc/src/chain/tests.rs
index 1f0a3a9d010..eac9abd5c4a 100644
--- a/substrate/substrate/rpc/src/chain/tests.rs
+++ b/substrate/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();
-- 
GitLab