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

RPCs for versioning #175

Merged
merged 7 commits into from
May 29, 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
4 changes: 2 additions & 2 deletions Cargo.lock

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

15 changes: 14 additions & 1 deletion demo/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ impl substrate_rpc::author::AuthorApi for DummyPool {
}
}

struct DummySystem;
impl substrate_rpc::system::SystemApi for DummySystem {
fn system_name(&self) -> substrate_rpc::system::error::Result<String> {
Ok("substrate-demo".into())
}
fn system_version(&self) -> substrate_rpc::system::error::Result<String> {
Ok(crate_version!().into())
}
fn system_chain(&self) -> substrate_rpc::system::error::Result<String> {
Ok("default".into())
}
}

/// Parse command line arguments and start the node.
///
/// IANA unassigned port ranges that we could use:
Expand Down Expand Up @@ -142,7 +155,7 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
let _rpc_servers = {
let handler = || {
let chain = rpc::apis::chain::Chain::new(client.clone(), core.remote());
rpc::rpc_handler(client.clone(), chain, DummyPool)
rpc::rpc_handler(client.clone(), chain, DummyPool, DummySystem)
};
let http_address = "127.0.0.1:9933".parse().unwrap();
let ws_address = "127.0.0.1:9944".parse().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion polkadot/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "polkadot-cli"
version = "0.1.0"
version = "0.2.0"
authors = ["Parity Technologies <[email protected]>"]
description = "Polkadot node implementation in Rust."

Expand Down
28 changes: 24 additions & 4 deletions polkadot/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ impl substrate_rpc::author::AuthorApi for RpcTransactionPool {
}
}

struct Configuration(service::Configuration);

impl substrate_rpc::system::SystemApi for Configuration {
fn system_name(&self) -> substrate_rpc::system::error::Result<String> {
Ok("parity-polkadot".into())
}

fn system_version(&self) -> substrate_rpc::system::error::Result<String> {
Ok(crate_version!().into())
}

fn system_chain(&self) -> substrate_rpc::system::error::Result<String> {
Ok(match self.0.chain_spec {
ChainSpec::Development => "dev",
ChainSpec::LocalTestnet => "local",
ChainSpec::PoC1Testnet => "poc-1",
}.into())
}
}

/// Parse command line arguments and start the node.
///
/// IANA unassigned port ranges that we could use:
Expand Down Expand Up @@ -189,12 +209,12 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
config.keys = matches.values_of("key").unwrap_or_default().map(str::to_owned).collect();

match role == service::Role::LIGHT {
true => run_until_exit(core, service::new_light(config)?, &matches),
false => run_until_exit(core, service::new_full(config)?, &matches),
true => run_until_exit(core, service::new_light(config.clone())?, &matches, config),
false => run_until_exit(core, service::new_full(config.clone())?, &matches, config),
}
}

fn run_until_exit<B, E>(mut core: reactor::Core, service: service::Service<B, E>, matches: &clap::ArgMatches) -> error::Result<()>
fn run_until_exit<B, E>(mut core: reactor::Core, service: service::Service<B, E>, matches: &clap::ArgMatches, config: service::Configuration) -> error::Result<()>
where
B: client::backend::Backend + Send + Sync + 'static,
E: client::CallExecutor + Send + Sync + 'static,
Expand Down Expand Up @@ -222,7 +242,7 @@ fn run_until_exit<B, E>(mut core: reactor::Core, service: service::Service<B, E>
inner: service.transaction_pool(),
network: service.network(),
};
rpc::rpc_handler(service.client(), chain, pool)
rpc::rpc_handler(service.client(), chain, pool, Configuration(config.clone()))
};
(
start_server(http_address, |address| rpc::start_http(address, handler())),
Expand Down
19 changes: 19 additions & 0 deletions polkadot/service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub use network::NetworkConfiguration;

/// The chain specification (this should eventually be replaced by a more general JSON-based chain
/// specification).
#[derive(Clone)]
pub enum ChainSpec {
/// Whatever the current runtime is, with just Alice as an auth.
Development,
Expand Down Expand Up @@ -62,3 +63,21 @@ impl Default for Configuration {
}
}
}

impl Clone for Configuration {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't that be derived?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sadly not since the transaction_pool::Options doesn't derive Clone and it's in a different repo so can't be fixed in this PR.

fn clone(&self) -> Configuration {
Configuration {
roles: self.roles.clone(),
transaction_pool: transaction_pool::Options {
max_count: self.transaction_pool.max_count.clone(),
max_mem_usage: self.transaction_pool.max_mem_usage.clone(),
max_per_sender: self.transaction_pool.max_per_sender.clone(),
},
network: self.network.clone(),
keystore_path: self.keystore_path.clone(),
database_path: self.database_path.clone(),
keys: self.keys.clone(),
chain_spec: self.chain_spec.clone(),
}
}
}
5 changes: 4 additions & 1 deletion substrate/rpc-servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ type Metadata = apis::metadata::Metadata;
type RpcHandler = pubsub::PubSubHandler<Metadata>;

/// Construct rpc `IoHandler`
pub fn rpc_handler<S, C, A>(
pub fn rpc_handler<S, C, A, Y>(
state: S,
chain: C,
author: A,
system: Y,
) -> RpcHandler where
S: apis::state::StateApi,
C: apis::chain::ChainApi<Metadata=Metadata>,
A: apis::author::AuthorApi,
Y: apis::system::SystemApi,
{
let mut io = pubsub::PubSubHandler::default();
io.extend_with(state.to_delegate());
io.extend_with(chain.to_delegate());
io.extend_with(author.to_delegate());
io.extend_with(system.to_delegate());
io
}

Expand Down
1 change: 1 addition & 0 deletions substrate/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ pub mod author;
pub mod chain;
pub mod metadata;
pub mod state;
pub mod system;
42 changes: 42 additions & 0 deletions substrate/rpc/src/system/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2017 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/>.

//! System RPC module errors.

use rpc;

error_chain! {
errors {
/// Not implemented yet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think the errors are needed at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i removed BadFormat, but Unimplemented may not be used for polkadot and demo, but could reasonably be used in other substrate-based chains.

Unimplemented {
description("not yet implemented"),
display("Method Not Implemented"),
}
}
}

impl From<Error> for rpc::Error {
fn from(e: Error) -> Self {
match e {
Error(ErrorKind::Unimplemented, _) => rpc::Error {
code: rpc::ErrorCode::ServerError(-1),
message: "Not implemented yet".into(),
data: None,
},
_ => rpc::Error::internal_error(),
}
}
}
41 changes: 41 additions & 0 deletions substrate/rpc/src/system/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2017 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 system API.

pub mod error;

#[cfg(test)]
mod tests;

use self::error::Result;

build_rpc_trait! {
/// Substrate system RPC API
pub trait SystemApi {
/// Get the node's implementation name. Plain old string.
#[rpc(name = "system_name")]
fn system_name(&self) -> Result<String>;

/// Get the node implementation's version. Should be a semver string.
#[rpc(name = "system_version")]
fn system_version(&self) -> Result<String>;

/// Get the chain's type. Given as a string identifier.
#[rpc(name = "system_chain")]
fn system_chain(&self) -> Result<String>;
}
}
54 changes: 54 additions & 0 deletions substrate/rpc/src/system/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2017 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/>.

use super::*;
use super::error::*;

impl SystemApi for () {
fn system_name(&self) -> Result<String> {
Ok("testclient".into())
}
fn system_version(&self) -> Result<String> {
Ok("0.2.0".into())
}
fn system_chain(&self) -> Result<String> {
Ok("testchain".into())
}
}

#[test]
fn system_name_works() {
assert_eq!(
SystemApi::system_name(&()).unwrap(),
"testclient".to_owned()
);
}

#[test]
fn system_version_works() {
assert_eq!(
SystemApi::system_version(&()).unwrap(),
"0.2.0".to_owned()
);
}

#[test]
fn system_chain_works() {
assert_eq!(
SystemApi::system_chain(&()).unwrap(),
"testchain".to_owned()
);
}