mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-24 17:05:47 +00:00
@@ -48,7 +48,7 @@ pub fn rpc_handler<Block, PendingExtrinsics, S, C, A, Y>(
|
|||||||
Block: BlockT + 'static,
|
Block: BlockT + 'static,
|
||||||
PendingExtrinsics: serde::Serialize + serde::de::DeserializeOwned + Send + Sync + 'static,
|
PendingExtrinsics: serde::Serialize + serde::de::DeserializeOwned + Send + Sync + 'static,
|
||||||
S: apis::state::StateApi<Block::Hash, Metadata=Metadata>,
|
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>,
|
A: apis::author::AuthorApi<Block::Hash, Block::Extrinsic, PendingExtrinsics, Metadata=Metadata>,
|
||||||
Y: apis::system::SystemApi,
|
Y: apis::system::SystemApi,
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ use jsonrpc_macros::pubsub;
|
|||||||
use jsonrpc_pubsub::SubscriptionId;
|
use jsonrpc_pubsub::SubscriptionId;
|
||||||
use rpc::Result as RpcResult;
|
use rpc::Result as RpcResult;
|
||||||
use rpc::futures::{stream, Future, Sink, Stream};
|
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 runtime_primitives::traits::Block as BlockT;
|
||||||
use tokio::runtime::TaskExecutor;
|
use tokio::runtime::TaskExecutor;
|
||||||
|
|
||||||
@@ -37,13 +37,17 @@ use self::error::Result;
|
|||||||
|
|
||||||
build_rpc_trait! {
|
build_rpc_trait! {
|
||||||
/// Polkadot blockchain API
|
/// Polkadot blockchain API
|
||||||
pub trait ChainApi<Hash, Header> {
|
pub trait ChainApi<Hash, Header, Extrinsic> {
|
||||||
type Metadata;
|
type Metadata;
|
||||||
|
|
||||||
/// Get header of a relay chain block.
|
/// Get header of a relay chain block.
|
||||||
#[rpc(name = "chain_getHeader")]
|
#[rpc(name = "chain_getHeader")]
|
||||||
fn header(&self, Hash) -> Result<Option<Header>>;
|
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.
|
/// Get hash of the head.
|
||||||
#[rpc(name = "chain_getHead")]
|
#[rpc(name = "chain_getHead")]
|
||||||
fn head(&self) -> Result<Hash>;
|
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,
|
Block: BlockT + 'static,
|
||||||
B: client::backend::Backend<Block> + Send + Sync + 'static,
|
B: client::backend::Backend<Block> + Send + Sync + 'static,
|
||||||
E: client::CallExecutor<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))?)
|
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> {
|
fn head(&self) -> Result<Block::Hash> {
|
||||||
Ok(self.client.info()?.chain.best_hash)
|
Ok(self.client.info()?.chain.best_hash)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ use super::*;
|
|||||||
use jsonrpc_macros::pubsub;
|
use jsonrpc_macros::pubsub;
|
||||||
use client::BlockOrigin;
|
use client::BlockOrigin;
|
||||||
use test_client::{self, TestClient};
|
use test_client::{self, TestClient};
|
||||||
use test_client::runtime::Header;
|
use test_client::runtime::{Block, Header};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_return_header() {
|
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]
|
#[test]
|
||||||
fn should_notify_about_latest_block() {
|
fn should_notify_about_latest_block() {
|
||||||
let mut core = ::tokio::runtime::Runtime::new().unwrap();
|
let mut core = ::tokio::runtime::Runtime::new().unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user