Convert to jsonrpc-derive, use jsonrpc-* from crates.io (#1679)

* use local version of jsonrpc to test

* Convert rpcs to test out in progress jsonrpc-derive api

* Revert "Convert rpcs to test out in progress jsonrpc-derive api"

This reverts commit 69231784171180d9bdb1bde1fcfd20f233357b17.

* Convert to new jsonrpc-derive macro

* Add RPC trait Metadata comment

* Break up long pubsub attr lines

* Update to 10.0.1 jsonrpc-* from crates.io

* Remove typed::Subscriber prefixes

* Remove empty comment
This commit is contained in:
Andrew Jones
2019-02-04 19:18:24 +00:00
committed by Bastian Köcher
parent 57ac31a0aa
commit 822c7ded45
13 changed files with 250 additions and 241 deletions
+58 -48
View File
@@ -19,14 +19,13 @@
use std::sync::Arc;
use client::{self, Client, BlockchainEvents};
use jsonrpc_macros::{pubsub, Trailing};
use jsonrpc_pubsub::SubscriptionId;
use jsonrpc_derive::rpc;
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use primitives::{H256, Blake2Hasher};
use rpc::Result as RpcResult;
use rpc::futures::{stream, Future, Sink, Stream};
use runtime_primitives::generic::{BlockId, SignedBlock};
use runtime_primitives::traits::{Block as BlockT, Header, NumberFor};
use serde::Serialize;
use subscriptions::Subscriptions;
@@ -37,52 +36,63 @@ mod number;
use self::error::Result;
build_rpc_trait! {
/// Substrate blockchain API
pub trait ChainApi<Number, Hash> where
Header: Serialize,
SignedBlock: Serialize,
{
type Metadata;
/// Substrate blockchain API
#[rpc]
pub trait ChainApi<Number, Hash, Header, SignedBlock> {
/// RPC metadata
type Metadata;
/// Get header of a relay chain block.
#[rpc(name = "chain_getHeader")]
fn header(&self, Trailing<Hash>) -> Result<Option<Header>>;
/// Get header of a relay chain block.
#[rpc(name = "chain_getHeader")]
fn header(&self, Option<Hash>) -> Result<Option<Header>>;
/// Get header and body of a relay chain block.
#[rpc(name = "chain_getBlock")]
fn block(&self, Trailing<Hash>) -> Result<Option<SignedBlock>>;
/// Get header and body of a relay chain block.
#[rpc(name = "chain_getBlock")]
fn block(&self, Option<Hash>) -> Result<Option<SignedBlock>>;
/// Get hash of the n-th block in the canon chain.
///
/// By default returns latest block hash.
#[rpc(name = "chain_getBlockHash", alias = ["chain_getHead", ])]
fn block_hash(&self, Trailing<number::NumberOrHex<Number>>) -> Result<Option<Hash>>;
/// Get hash of the n-th block in the canon chain.
///
/// By default returns latest block hash.
#[rpc(name = "chain_getBlockHash", alias("chain_getHead"))]
fn block_hash(&self, Option<number::NumberOrHex<Number>>) -> Result<Option<Hash>>;
/// Get hash of the last finalised block in the canon chain.
#[rpc(name = "chain_getFinalisedHead")]
fn finalised_head(&self) -> Result<Hash>;
/// Get hash of the last finalised block in the canon chain.
#[rpc(name = "chain_getFinalisedHead")]
fn finalised_head(&self) -> Result<Hash>;
#[pubsub(name = "chain_newHead")] {
/// New head subscription
#[rpc(name = "chain_subscribeNewHead", alias = ["subscribe_newHead", ])]
fn subscribe_new_head(&self, Self::Metadata, pubsub::Subscriber<Header>);
/// New head subscription
#[pubsub(
subscription = "chain_newHead",
subscribe,
name = "chain_subscribeNewHead",
alias("subscribe_newHead")
)]
fn subscribe_new_head(&self, Self::Metadata, Subscriber<Header>);
/// Unsubscribe from new head subscription.
#[rpc(name = "chain_unsubscribeNewHead", alias = ["unsubscribe_newHead", ])]
fn unsubscribe_new_head(&self, Option<Self::Metadata>, SubscriptionId) -> RpcResult<bool>;
}
/// Unsubscribe from new head subscription.
#[pubsub(
subscription = "chain_newHead",
unsubscribe,
name = "chain_unsubscribeNewHead",
alias("unsubscribe_newHead")
)]
fn unsubscribe_new_head(&self, Option<Self::Metadata>, SubscriptionId) -> RpcResult<bool>;
#[pubsub(name = "chain_finalisedHead")] {
/// New head subscription
#[rpc(name = "chain_subscribeFinalisedHeads")]
fn subscribe_finalised_heads(&self, Self::Metadata, pubsub::Subscriber<Header>);
/// New head subscription
#[pubsub(
subscription = "chain_finalisedHead",
subscribe,
name = "chain_subscribeFinalisedHeads"
)]
fn subscribe_finalised_heads(&self, Self::Metadata, Subscriber<Header>);
/// Unsubscribe from new head subscription.
#[rpc(name = "chain_unsubscribeFinalisedHeads")]
fn unsubscribe_finalised_heads(&self, Option<Self::Metadata>, SubscriptionId) -> RpcResult<bool>;
}
}
/// Unsubscribe from new head subscription.
#[pubsub(
subscription = "chain_finalisedHead",
unsubscribe,
name = "chain_unsubscribeFinalisedHeads"
)]
fn unsubscribe_finalised_heads(&self, Option<Self::Metadata>, SubscriptionId) -> RpcResult<bool>;
}
/// Chain API with subscriptions support.
@@ -109,7 +119,7 @@ impl<B, E, Block, RA> Chain<B, E, Block, RA> where
E: client::CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static,
RA: Send + Sync + 'static
{
fn unwrap_or_best(&self, hash: Trailing<Block::Hash>) -> Result<Block::Hash> {
fn unwrap_or_best(&self, hash: Option<Block::Hash>) -> Result<Block::Hash> {
Ok(match hash.into() {
None => self.client.info()?.chain.best_hash,
Some(hash) => hash,
@@ -118,7 +128,7 @@ impl<B, E, Block, RA> Chain<B, E, Block, RA> where
fn subscribe_headers<F, G, S, ERR>(
&self,
subscriber: pubsub::Subscriber<Block::Header>,
subscriber: Subscriber<Block::Header>,
best_block_hash: G,
stream: F,
) where
@@ -161,19 +171,19 @@ impl<B, E, Block, RA> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, Sig
{
type Metadata = ::metadata::Metadata;
fn header(&self, hash: Trailing<Block::Hash>) -> Result<Option<Block::Header>> {
fn header(&self, hash: Option<Block::Hash>) -> Result<Option<Block::Header>> {
let hash = self.unwrap_or_best(hash)?;
Ok(self.client.header(&BlockId::Hash(hash))?)
}
fn block(&self, hash: Trailing<Block::Hash>)
fn block(&self, hash: Option<Block::Hash>)
-> Result<Option<SignedBlock<Block>>>
{
let hash = self.unwrap_or_best(hash)?;
Ok(self.client.block(&BlockId::Hash(hash))?)
}
fn block_hash(&self, number: Trailing<number::NumberOrHex<NumberFor<Block>>>) -> Result<Option<Block::Hash>> {
fn block_hash(&self, number: Option<number::NumberOrHex<NumberFor<Block>>>) -> Result<Option<Block::Hash>> {
let number: Option<number::NumberOrHex<NumberFor<Block>>> = number.into();
Ok(match number {
None => Some(self.client.info()?.chain.best_hash),
@@ -185,7 +195,7 @@ impl<B, E, Block, RA> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, Sig
Ok(self.client.info()?.chain.finalized_hash)
}
fn subscribe_new_head(&self, _metadata: Self::Metadata, subscriber: pubsub::Subscriber<Block::Header>) {
fn subscribe_new_head(&self, _metadata: Self::Metadata, subscriber: Subscriber<Block::Header>) {
self.subscribe_headers(
subscriber,
|| self.block_hash(None.into()),
@@ -199,7 +209,7 @@ impl<B, E, Block, RA> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, Sig
Ok(self.subscriptions.cancel(id))
}
fn subscribe_finalised_heads(&self, _meta: Self::Metadata, subscriber: pubsub::Subscriber<Block::Header>) {
fn subscribe_finalised_heads(&self, _meta: Self::Metadata, subscriber: Subscriber<Block::Header>) {
self.subscribe_headers(
subscriber,
|| Ok(Some(self.client.info()?.chain.finalized_hash)),
+2 -3
View File
@@ -15,7 +15,6 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use jsonrpc_macros::pubsub;
use test_client::{self, TestClient};
use test_client::runtime::{H256, Block, Header};
use consensus::BlockOrigin;
@@ -192,7 +191,7 @@ fn should_return_finalised_hash() {
fn should_notify_about_latest_block() {
let mut core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let (subscriber, id, transport) = pubsub::Subscriber::new_test("test");
let (subscriber, id, transport) = Subscriber::new_test("test");
{
let api = Chain {
@@ -223,7 +222,7 @@ fn should_notify_about_latest_block() {
fn should_notify_about_finalised_block() {
let mut core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let (subscriber, id, transport) = pubsub::Subscriber::new_test("test");
let (subscriber, id, transport) = Subscriber::new_test("test");
{
let api = Chain {