Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

RPC: Get block #564

Merged
merged 4 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion substrate/rpc-servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand Down
14 changes: 11 additions & 3 deletions substrate/rpc/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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>;
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
43 changes: 42 additions & 1 deletion substrate/rpc/src/chain/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down