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

@@ -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 net;
mod personal;
mod ethcore;
pub use self::web3::Web3Client;
pub use self::eth::{EthClient, EthFilterClient};
pub use self::net::NetClient;
pub use self::personal::PersonalClient;
pub use self::ethcore::EthcoreClient;

View File

@@ -25,5 +25,5 @@ mod helpers;
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::*;

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> {
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;
#[cfg(test)]
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 net;
pub mod personal;
pub mod ethcore;
pub use self::web3::Web3;
pub use self::eth::{Eth, EthFilter};
pub use self::net::Net;
pub use self::personal::Personal;
pub use self::ethcore::Ethcore;