Skip to content
Snippets Groups Projects
Commit 5f124e2e authored by Tomasz Drwięga's avatar Tomasz Drwięga Committed by Gav Wood
Browse files

RPC: Get block (#564)

* Get block RPC.

* Fix rpc-servers.
parent a0a5ef5f
No related merge requests found
......@@ -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,
{
......
......@@ -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)
}
......
......@@ -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();
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment