Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
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
27 changes: 22 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 11 additions & 15 deletions core/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,28 @@ authors = ["Parity Technologies <[email protected]>"]
edition = "2018"

[dependencies]
derive_more = "0.14.0"
futures = "0.1"
api = { package = "substrate-rpc-api", path = "./api" }
client = { package = "substrate-client", path = "../client" }
codec = { package = "parity-scale-codec", version = "1.0.0" }
futures03 = { package = "futures-preview", version = "0.3.0-alpha.17", features = ["compat"] }
jsonrpc-core = "13.1.0"
jsonrpc-core-client = "13.1.0"
jsonrpc-pubsub = "13.1.0"
jsonrpc-derive = "13.1.0"
log = "0.4"
parking_lot = "0.9.0"
codec = { package = "parity-scale-codec", version = "1.0.0" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
client = { package = "substrate-client", path = "../client" }
network = { package = "substrate-network", path = "../network" }
primitives = { package = "substrate-primitives", path = "../primitives" }
rpc = { package = "jsonrpc-core", version = "13.0.0" }
runtime_version = { package = "sr-version", path = "../sr-version" }
serde_json = "1.0"
session = { package = "substrate-session", path = "../session" }
state_machine = { package = "substrate-state-machine", path = "../state-machine" }
transaction_pool = { package = "substrate-transaction-pool", path = "../transaction-pool" }
sr-primitives = { path = "../sr-primitives" }
runtime_version = { package = "sr-version", path = "../sr-version" }
state_machine = { package = "substrate-state-machine", path = "../state-machine" }
substrate-executor = { path = "../executor" }
substrate-keystore = { path = "../keystore" }
transaction_pool = { package = "substrate-transaction-pool", path = "../transaction-pool" }

[dev-dependencies]
assert_matches = "1.1"
futures = "0.1.17"
network = { package = "substrate-network", path = "../network" }
rustc-hex = "2.0"
sr-io = { path = "../sr-io" }
test-client = { package = "substrate-test-runtime-client", path = "../test-runtime/client" }
rustc-hex = "2.0"
tokio = "0.1.17"
21 changes: 21 additions & 0 deletions core/rpc/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "substrate-rpc-api"
version = "2.0.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"

[dependencies]
codec = { package = "parity-scale-codec", version = "1.0.0" }
derive_more = "0.14.0"
futures03 = { package = "futures-preview", version = "0.3.0-alpha.17", features = ["compat"] }
jsonrpc-core = "13.0.0"
jsonrpc-core-client = "13.0.0"
jsonrpc-derive = "13.0.0"
jsonrpc-pubsub = "13.0.0"
log = "0.4"
parking_lot = "0.9.0"
primitives = { package = "substrate-primitives", path = "../../primitives" }
runtime_version = { package = "sr-version", path = "../../sr-version" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
txpool = { package = "substrate-transaction-graph", path = "../../transaction-pool/graph" }
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

//! Authoring RPC module errors.

use client;
use transaction_pool::txpool;
use crate::rpc;
use crate::errors;
use jsonrpc_core as rpc;

/// Author RPC Result type.
pub type Result<T> = std::result::Result<T, Error>;
Expand All @@ -28,8 +26,10 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
/// Client error.
Client(client::error::Error),
#[display(fmt="Client error: {}", _0)]
Client(Box<dyn std::error::Error + Send>),
/// Transaction pool error,
#[display(fmt="Transaction pool error: {}", _0)]
Pool(txpool::error::Error),
/// Verification error
#[display(fmt="Extrinsic verification error: {}", _0)]
Expand All @@ -54,7 +54,7 @@ pub enum Error {
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Client(ref err) => Some(err),
Error::Client(ref err) => Some(&**err),
Error::Pool(ref err) => Some(err),
Error::Verification(ref err) => Some(&**err),
_ => None,
Expand Down
87 changes: 87 additions & 0 deletions core/rpc/api/src/author/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Substrate block-author/full-node API.

pub mod error;
pub mod hash;

use jsonrpc_derive::rpc;
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use primitives::{
Bytes
};
use self::error::Result;
use txpool::watcher::Status;

pub use self::gen_client::Client as AuthorClient;

/// Substrate authoring RPC API
#[rpc]
pub trait AuthorApi<Hash, BlockHash> {
/// RPC metadata
type Metadata;

/// Submit hex-encoded extrinsic for inclusion in block.
#[rpc(name = "author_submitExtrinsic")]
fn submit_extrinsic(&self, extrinsic: Bytes) -> Result<Hash>;

/// Insert a key into the keystore.
#[rpc(name = "author_insertKey")]
fn insert_key(&self,
key_type: String,
suri: String,
maybe_public: Option<Bytes>
) -> Result<Bytes>;

/// Generate new session keys and returns the corresponding public keys.
#[rpc(name = "author_rotateKeys")]
fn rotate_keys(&self) -> Result<Bytes>;

/// Returns all pending extrinsics, potentially grouped by sender.
#[rpc(name = "author_pendingExtrinsics")]
fn pending_extrinsics(&self) -> Result<Vec<Bytes>>;

/// Remove given extrinsic from the pool and temporarily ban it to prevent reimporting.
#[rpc(name = "author_removeExtrinsic")]
fn remove_extrinsic(&self,
bytes_or_hash: Vec<hash::ExtrinsicOrHash<Hash>>
) -> Result<Vec<Hash>>;

/// Submit an extrinsic to watch.
#[pubsub(
subscription = "author_extrinsicUpdate",
subscribe,
name = "author_submitAndWatchExtrinsic"
)]
fn watch_extrinsic(&self,
metadata: Self::Metadata,
subscriber: Subscriber<Status<Hash, BlockHash>>,
bytes: Bytes
);

/// Unsubscribe from extrinsic watching.
#[pubsub(
subscription = "author_extrinsicUpdate",
unsubscribe,
name = "author_unwatchExtrinsic"
)]
fn unwatch_extrinsic(&self,
metadata: Option<Self::Metadata>,
id: SubscriptionId
) -> Result<bool>;
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@

//! Error helpers for Chain RPC module.

use client;
use crate::rpc;
use crate::errors;
use jsonrpc_core as rpc;

/// Chain RPC Result type.
pub type Result<T> = std::result::Result<T, Error>;
Expand All @@ -28,15 +27,16 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
/// Client error.
Client(client::error::Error),
#[display(fmt="Client error: {}", _0)]
Client(Box<dyn std::error::Error + Send>),
/// Other error type.
Other(String),
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Client(ref err) => Some(err),
Error::Client(ref err) => Some(&**err),
_ => None,
}
}
Expand Down
89 changes: 89 additions & 0 deletions core/rpc/api/src/chain/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Substrate blockchain API.

pub mod error;
pub mod number;

use jsonrpc_core::Result as RpcResult;
use jsonrpc_core::futures::Future;
use jsonrpc_derive::rpc;
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use self::error::Result;

pub use self::gen_client::Client as ChainClient;

/// 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, hash: Option<Hash>) -> Result<Option<Header>>;

/// Get header and body of a relay chain block.
#[rpc(name = "chain_getBlock")]
fn block(&self, hash: 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, hash: Option<number::NumberOrHex<Number>>) -> Result<Option<Hash>>;

/// Get hash of the last finalized block in the canon chain.
#[rpc(name = "chain_getFinalizedHead", alias("chain_getFinalisedHead"))]
fn finalized_head(&self) -> Result<Hash>;

/// New head subscription
#[pubsub(
subscription = "chain_newHead",
subscribe,
name = "chain_subscribeNewHeads",
alias("subscribe_newHead", "chain_subscribeNewHead")
)]
fn subscribe_new_heads(&self, metadata: Self::Metadata, subscriber: Subscriber<Header>);

/// Unsubscribe from new head subscription.
#[pubsub(
subscription = "chain_newHead",
unsubscribe,
name = "chain_unsubscribeNewHeads",
alias("unsubscribe_newHead", "chain_unsubscribeNewHead")
)]
fn unsubscribe_new_heads(&self, metadata: Option<Self::Metadata>, id: SubscriptionId) -> RpcResult<bool>;

/// New head subscription
#[pubsub(
subscription = "chain_finalizedHead",
subscribe,
name = "chain_subscribeFinalizedHeads",
alias("chain_subscribeFinalisedHeads")
)]
fn subscribe_finalized_heads(&self, metadata: Self::Metadata, subscriber: Subscriber<Header>);

/// Unsubscribe from new head subscription.
#[pubsub(
subscription = "chain_finalizedHead",
unsubscribe,
name = "chain_unsubscribeFinalizedHeads",
alias("chain_unsubscribeFinalisedHeads")
)]
fn unsubscribe_finalized_heads(&self, metadata: Option<Self::Metadata>, id: SubscriptionId) -> RpcResult<bool>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ impl<Number: TryFrom<u64> + From<u32> + Debug + PartialOrd> NumberOrHex<Number>
}
}

#[cfg(test)]
impl From<u64> for NumberOrHex<u64> {
fn from(n: u64) -> Self {
NumberOrHex::Number(n)
}
}

#[cfg(test)]
impl<Number> From<U256> for NumberOrHex<Number> {
fn from(n: U256) -> Self {
NumberOrHex::Hex(n)
Expand Down
7 changes: 3 additions & 4 deletions core/rpc/src/errors.rs → core/rpc/api/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use crate::rpc;
use log::warn;

pub fn internal<E: ::std::fmt::Debug>(e: E) -> rpc::Error {
pub fn internal<E: ::std::fmt::Debug>(e: E) -> jsonrpc_core::Error {
warn!("Unknown error: {:?}", e);
rpc::Error {
code: rpc::ErrorCode::InternalError,
jsonrpc_core::Error {
code: jsonrpc_core::ErrorCode::InternalError,
message: "Unknown error occured".into(),
data: Some(format!("{:?}", e).into()),
}
Expand Down
Loading