Ethcore-specific RPC methods (#923)

* Ethcore-specific rpc methods

* Initializing ethcore-rpc
This commit is contained in:
Tomasz Drwięga 2016-04-11 21:06:32 +02:00 committed by Gav Wood
parent c48374dbc6
commit 3fe21f5931
11 changed files with 168 additions and 19 deletions

View File

@ -82,6 +82,9 @@ pub trait MinerService : Send + Sync {
/// Get the extra_data that we will seal blocks wuth. /// Get the extra_data that we will seal blocks wuth.
fn extra_data(&self) -> Bytes { vec![] } fn extra_data(&self) -> Bytes { vec![] }
/// Get the gas limit we wish to target when sealing a new block.
fn gas_floor_target(&self) -> U256;
/// Imports transactions to transaction queue. /// Imports transactions to transaction queue.
fn import_transactions<T>(&self, transactions: Vec<SignedTransaction>, fetch_account: T) -> Vec<Result<(), Error>> fn import_transactions<T>(&self, transactions: Vec<SignedTransaction>, fetch_account: T) -> Vec<Result<(), Error>>
where T: Fn(&Address) -> AccountDetails; where T: Fn(&Address) -> AccountDetails;

View File

@ -69,21 +69,6 @@ impl Miner {
}) })
} }
/// Get the author that we will seal blocks as.
fn author(&self) -> Address {
*self.author.read().unwrap()
}
/// Get the extra_data that we will seal blocks wuth.
fn extra_data(&self) -> Bytes {
self.extra_data.read().unwrap().clone()
}
/// Get the extra_data that we will seal blocks wuth.
fn gas_floor_target(&self) -> U256 {
*self.gas_floor_target.read().unwrap()
}
/// Set the author that we will seal blocks as. /// Set the author that we will seal blocks as.
pub fn set_author(&self, author: Address) { pub fn set_author(&self, author: Address) {
*self.author.write().unwrap() = author; *self.author.write().unwrap() = author;
@ -215,14 +200,21 @@ impl MinerService for Miner {
*self.transaction_queue.lock().unwrap().minimal_gas_price() * x!(110) / x!(100) *self.transaction_queue.lock().unwrap().minimal_gas_price() * x!(110) / x!(100)
} }
/// Get the author that we will seal blocks as.
fn author(&self) -> Address { fn author(&self) -> Address {
*self.author.read().unwrap() *self.author.read().unwrap()
} }
/// Get the extra_data that we will seal blocks with.
fn extra_data(&self) -> Bytes { fn extra_data(&self) -> Bytes {
self.extra_data.read().unwrap().clone() self.extra_data.read().unwrap().clone()
} }
/// Get the gas limit we wish to target when sealing a new block.
fn gas_floor_target(&self) -> U256 {
*self.gas_floor_target.read().unwrap()
}
fn import_transactions<T>(&self, transactions: Vec<SignedTransaction>, fetch_account: T) -> Vec<Result<(), Error>> fn import_transactions<T>(&self, transactions: Vec<SignedTransaction>, fetch_account: T) -> Vec<Result<(), Error>>
where T: Fn(&Address) -> AccountDetails { where T: Fn(&Address) -> AccountDetails {
let mut transaction_queue = self.transaction_queue.lock().unwrap(); let mut transaction_queue = self.transaction_queue.lock().unwrap();

View File

@ -136,7 +136,7 @@ API and Console Options:
--jsonrpc-apis APIS Specify the APIs available through the JSONRPC --jsonrpc-apis APIS Specify the APIs available through the JSONRPC
interface. APIS is a comma-delimited list of API interface. APIS is a comma-delimited list of API
name. Possible name are web3, eth and net. name. Possible name are web3, eth and net.
[default: web3,eth,net,personal]. [default: web3,eth,net,personal,ethcore].
-w --webapp Enable the web applications server (e.g. -w --webapp Enable the web applications server (e.g.
status page). status page).
--webapp-port PORT Specify the port portion of the WebApps server --webapp-port PORT Specify the port portion of the WebApps server
@ -312,11 +312,12 @@ fn setup_rpc_server(
"eth" => { "eth" => {
server.add_delegate(EthClient::new(&client, &sync, &secret_store, &miner).to_delegate()); server.add_delegate(EthClient::new(&client, &sync, &secret_store, &miner).to_delegate());
server.add_delegate(EthFilterClient::new(&client, &miner).to_delegate()); server.add_delegate(EthFilterClient::new(&client, &miner).to_delegate());
} },
"personal" => server.add_delegate(PersonalClient::new(&secret_store).to_delegate()), "personal" => server.add_delegate(PersonalClient::new(&secret_store).to_delegate()),
"ethcore" => server.add_delegate(EthcoreClient::new(&miner).to_delegate()),
_ => { _ => {
die!("{}: Invalid API name to be enabled.", api); die!("{}: Invalid API name to be enabled.", api);
} },
} }
} }
let start_result = server.start_http(url, cors_domain); let start_result = server.start_http(url, cors_domain);
@ -344,6 +345,7 @@ fn setup_webapp_server(
server.add_delegate(EthClient::new(&client, &sync, &secret_store, &miner).to_delegate()); server.add_delegate(EthClient::new(&client, &sync, &secret_store, &miner).to_delegate());
server.add_delegate(EthFilterClient::new(&client, &miner).to_delegate()); server.add_delegate(EthFilterClient::new(&client, &miner).to_delegate());
server.add_delegate(PersonalClient::new(&secret_store).to_delegate()); server.add_delegate(PersonalClient::new(&secret_store).to_delegate());
server.add_delegate(EthcoreClient::new(&miner).to_delegate());
let start_result = match auth { let start_result = match auth {
None => { None => {
server.start_unsecure_http(url, ::num_cpus::get()) server.start_unsecure_http(url, ::num_cpus::get())

View File

@ -0,0 +1,47 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity 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.
// Parity 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 Parity. If not, see <http://www.gnu.org/licenses/>.
//! Ethcore-specific rpc implementation.
use std::sync::{Arc, Weak};
use jsonrpc_core::*;
use ethminer::{MinerService};
use v1::traits::Ethcore;
use v1::types::Bytes;
/// Ethcore implementation.
pub struct EthcoreClient<M>
where M: MinerService {
miner: Weak<M>,
}
impl<M> EthcoreClient<M> where M: MinerService {
/// Creates new `EthcoreClient`.
pub fn new(miner: &Arc<M>) -> Self {
EthcoreClient {
miner: Arc::downgrade(miner)
}
}
}
impl<M> Ethcore for EthcoreClient<M> where M: MinerService + 'static {
fn extra_data(&self, _: Params) -> Result<Value, Error> {
to_value(&Bytes::new(take_weak!(self.miner).extra_data()))
}
fn gas_floor_target(&self, _: Params) -> Result<Value, Error> {
to_value(&take_weak!(self.miner).gas_floor_target())
}
}

View File

@ -29,8 +29,11 @@ mod web3;
mod eth; mod eth;
mod net; mod net;
mod personal; mod personal;
mod ethcore;
pub use self::web3::Web3Client; pub use self::web3::Web3Client;
pub use self::eth::{EthClient, EthFilterClient}; pub use self::eth::{EthClient, EthFilterClient};
pub use self::net::NetClient; pub use self::net::NetClient;
pub use self::personal::PersonalClient; pub use self::personal::PersonalClient;
pub use self::ethcore::EthcoreClient;

View File

@ -25,5 +25,5 @@ mod helpers;
pub mod tests; pub mod tests;
pub use self::traits::{Web3, Eth, EthFilter, Personal, Net}; pub use self::traits::{Web3, Eth, EthFilter, Personal, Net, Ethcore};
pub use self::impls::*; pub use self::impls::*;

View File

@ -0,0 +1,54 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity 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.
// Parity 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 Parity. If not, see <http://www.gnu.org/licenses/>.
use std::sync::Arc;
use jsonrpc_core::IoHandler;
use v1::{Ethcore, EthcoreClient};
use v1::tests::helpers::{TestMinerService};
use util::numbers::*;
fn miner_service() -> Arc<TestMinerService> {
Arc::new(TestMinerService::default())
}
#[test]
fn rpc_ethcore_extra_data() {
let miner = miner_service();
let ethcore = EthcoreClient::new(&miner).to_delegate();
let io = IoHandler::new();
io.add_delegate(ethcore);
let request = r#"{"jsonrpc": "2.0", "method": "ethcore_extraData", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x01020304","id":1}"#;
assert_eq!(io.handle_request(request), Some(response.to_owned()));
}
#[test]
fn rpc_ethcore_gas_floor_target() {
let miner = miner_service();
let ethcore = EthcoreClient::new(&miner).to_delegate();
let io = IoHandler::new();
io.add_delegate(ethcore);
let request = r#"{"jsonrpc": "2.0", "method": "ethcore_gasFloorTarget", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x3039","id":1}"#;
assert_eq!(io.handle_request(request), Some(response.to_owned()));
}

View File

@ -111,4 +111,12 @@ impl MinerService for TestMinerService {
fn submit_seal(&self, _chain: &BlockChainClient, _pow_hash: H256, _seal: Vec<Bytes>) -> Result<(), Error> { fn submit_seal(&self, _chain: &BlockChainClient, _pow_hash: H256, _seal: Vec<Bytes>) -> Result<(), Error> {
unimplemented!(); unimplemented!();
} }
fn extra_data(&self) -> Bytes {
vec![1, 2, 3, 4]
}
fn gas_floor_target(&self) -> U256 {
U256::from(12345)
}
} }

View File

@ -25,3 +25,5 @@ mod net;
mod web3; mod web3;
#[cfg(test)] #[cfg(test)]
mod personal; mod personal;
#[cfg(test)]
mod ethcore;

View File

@ -0,0 +1,36 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity 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.
// Parity 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 Parity. If not, see <http://www.gnu.org/licenses/>.
//! Ethcore-specific rpc interface.
use std::sync::Arc;
use jsonrpc_core::*;
/// Ethcore-specific rpc interface.
pub trait Ethcore: Sized + Send + Sync + 'static {
/// Returns mining extra data.
fn extra_data(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns mining gas floor target.
fn gas_floor_target(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("ethcore_extraData", Ethcore::extra_data);
delegate.add_method("ethcore_gasFloorTarget", Ethcore::gas_floor_target);
delegate
}
}

View File

@ -24,8 +24,10 @@ pub mod web3;
pub mod eth; pub mod eth;
pub mod net; pub mod net;
pub mod personal; pub mod personal;
pub mod ethcore;
pub use self::web3::Web3; pub use self::web3::Web3;
pub use self::eth::{Eth, EthFilter}; pub use self::eth::{Eth, EthFilter};
pub use self::net::Net; pub use self::net::Net;
pub use self::personal::Personal; pub use self::personal::Personal;
pub use self::ethcore::Ethcore;