2016-02-05 13:40:41 +01:00
|
|
|
// 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/>.
|
|
|
|
|
2016-01-27 17:08:59 +01:00
|
|
|
//! Eth rpc implementation.
|
2016-03-26 18:51:27 +01:00
|
|
|
|
|
|
|
extern crate ethash;
|
|
|
|
|
2016-07-14 14:25:44 +02:00
|
|
|
use std::io::{Write};
|
|
|
|
use std::process::{Command, Stdio};
|
2016-06-28 18:46:09 +02:00
|
|
|
use std::thread;
|
|
|
|
use std::time::{Instant, Duration};
|
2016-07-13 19:59:59 +02:00
|
|
|
use std::sync::{Arc, Weak};
|
2016-08-23 17:07:00 +02:00
|
|
|
use time::get_time;
|
2016-03-10 17:32:17 +01:00
|
|
|
use ethsync::{SyncProvider, SyncState};
|
2016-05-31 21:31:42 +02:00
|
|
|
use ethcore::miner::{MinerService, ExternalMinerService};
|
2016-01-26 19:24:33 +01:00
|
|
|
use jsonrpc_core::*;
|
2016-08-03 18:05:17 +02:00
|
|
|
use util::{H256, Address, FixedHash, U256, H64, Uint};
|
2016-01-27 12:31:54 +01:00
|
|
|
use util::sha3::*;
|
2016-07-14 14:25:44 +02:00
|
|
|
use util::{FromHex, Mutex};
|
2016-09-01 14:49:12 +02:00
|
|
|
use rlp::{self, UntrustedRlp, View};
|
2016-06-20 00:10:34 +02:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
2016-05-31 21:31:42 +02:00
|
|
|
use ethcore::client::{MiningBlockChainClient, BlockID, TransactionID, UncleID};
|
2016-07-07 09:39:32 +02:00
|
|
|
use ethcore::header::Header as BlockHeader;
|
2016-03-14 14:18:29 +01:00
|
|
|
use ethcore::block::IsBlock;
|
2016-01-27 12:31:54 +01:00
|
|
|
use ethcore::views::*;
|
2016-02-29 19:30:13 +01:00
|
|
|
use ethcore::ethereum::Ethash;
|
2016-03-22 17:17:50 +01:00
|
|
|
use ethcore::transaction::{Transaction as EthTransaction, SignedTransaction, Action};
|
2016-05-24 21:56:32 +02:00
|
|
|
use ethcore::log_entry::LogEntry;
|
|
|
|
use ethcore::filter::Filter as EthcoreFilter;
|
2016-03-26 18:51:27 +01:00
|
|
|
use self::ethash::SeedHashCompute;
|
2016-06-01 19:37:34 +02:00
|
|
|
use v1::traits::Eth;
|
2016-09-23 19:42:33 +02:00
|
|
|
use v1::types::{
|
|
|
|
Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo,
|
|
|
|
Transaction, CallRequest, Index, Filter, Log, Receipt, Work,
|
|
|
|
H64 as RpcH64, H256 as RpcH256, H160 as RpcH160, U256 as RpcU256,
|
|
|
|
};
|
2016-09-21 12:51:10 +02:00
|
|
|
use v1::helpers::{CallRequest as CRequest, errors, limit_logs};
|
2016-08-08 17:25:15 +02:00
|
|
|
use v1::helpers::dispatch::{default_gas_price, dispatch_transaction};
|
2016-09-23 19:42:33 +02:00
|
|
|
use v1::helpers::auto_args::Trailing;
|
2016-01-21 01:19:29 +01:00
|
|
|
|
2016-08-03 15:31:00 +02:00
|
|
|
/// Eth RPC options
|
|
|
|
pub struct EthClientOptions {
|
|
|
|
/// Returns receipt from pending blocks
|
|
|
|
pub allow_pending_receipt_query: bool,
|
|
|
|
/// Send additional block number when asking for work
|
|
|
|
pub send_block_number_in_get_work: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for EthClientOptions {
|
|
|
|
fn default() -> Self {
|
|
|
|
EthClientOptions {
|
|
|
|
allow_pending_receipt_query: true,
|
|
|
|
send_block_number_in_get_work: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 17:08:59 +01:00
|
|
|
/// Eth rpc implementation.
|
2016-07-16 15:51:06 +02:00
|
|
|
pub struct EthClient<C, S: ?Sized, M, EM> where
|
2016-05-31 21:31:42 +02:00
|
|
|
C: MiningBlockChainClient,
|
2016-05-22 12:42:22 +02:00
|
|
|
S: SyncProvider,
|
|
|
|
M: MinerService,
|
|
|
|
EM: ExternalMinerService {
|
2016-05-23 11:30:11 +02:00
|
|
|
|
2016-03-09 17:31:43 +01:00
|
|
|
client: Weak<C>,
|
|
|
|
sync: Weak<S>,
|
2016-06-20 00:10:34 +02:00
|
|
|
accounts: Weak<AccountProvider>,
|
2016-03-10 16:18:33 +01:00
|
|
|
miner: Weak<M>,
|
2016-04-21 17:32:53 +02:00
|
|
|
external_miner: Arc<EM>,
|
2016-03-26 18:51:27 +01:00
|
|
|
seed_compute: Mutex<SeedHashCompute>,
|
2016-08-03 15:31:00 +02:00
|
|
|
options: EthClientOptions,
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|
|
|
|
|
2016-07-16 15:51:06 +02:00
|
|
|
impl<C, S: ?Sized, M, EM> EthClient<C, S, M, EM> where
|
2016-05-31 21:31:42 +02:00
|
|
|
C: MiningBlockChainClient,
|
2016-05-22 12:42:22 +02:00
|
|
|
S: SyncProvider,
|
|
|
|
M: MinerService,
|
|
|
|
EM: ExternalMinerService {
|
2016-03-14 14:18:29 +01:00
|
|
|
|
2016-04-21 17:32:53 +02:00
|
|
|
/// Creates new EthClient.
|
2016-08-03 15:31:00 +02:00
|
|
|
pub fn new(client: &Arc<C>, sync: &Arc<S>, accounts: &Arc<AccountProvider>, miner: &Arc<M>, em: &Arc<EM>, options: EthClientOptions)
|
2016-06-20 00:10:34 +02:00
|
|
|
-> EthClient<C, S, M, EM> {
|
2016-01-21 01:19:29 +01:00
|
|
|
EthClient {
|
2016-02-29 11:58:33 +01:00
|
|
|
client: Arc::downgrade(client),
|
2016-03-01 01:52:22 +01:00
|
|
|
sync: Arc::downgrade(sync),
|
2016-03-08 15:46:44 +01:00
|
|
|
miner: Arc::downgrade(miner),
|
2016-03-10 17:18:01 +01:00
|
|
|
accounts: Arc::downgrade(accounts),
|
2016-04-21 17:32:53 +02:00
|
|
|
external_miner: em.clone(),
|
2016-03-26 18:51:27 +01:00
|
|
|
seed_compute: Mutex::new(SeedHashCompute::new()),
|
2016-08-03 15:31:00 +02:00
|
|
|
options: options,
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-10 22:54:12 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn block(&self, id: BlockID, include_txs: bool) -> Result<Option<Block>, Error> {
|
2016-02-29 11:58:33 +01:00
|
|
|
let client = take_weak!(self.client);
|
|
|
|
match (client.block(id.clone()), client.block_total_difficulty(id)) {
|
2016-02-10 22:54:12 +01:00
|
|
|
(Some(bytes), Some(total_difficulty)) => {
|
|
|
|
let block_view = BlockView::new(&bytes);
|
|
|
|
let view = block_view.header_view();
|
|
|
|
let block = Block {
|
2016-07-06 11:23:29 +02:00
|
|
|
hash: Some(view.sha3().into()),
|
2016-08-17 19:25:02 +02:00
|
|
|
size: Some(bytes.len().into()),
|
2016-07-06 11:23:29 +02:00
|
|
|
parent_hash: view.parent_hash().into(),
|
|
|
|
uncles_hash: view.uncles_hash().into(),
|
|
|
|
author: view.author().into(),
|
|
|
|
miner: view.author().into(),
|
|
|
|
state_root: view.state_root().into(),
|
|
|
|
transactions_root: view.transactions_root().into(),
|
|
|
|
receipts_root: view.receipts_root().into(),
|
|
|
|
number: Some(view.number().into()),
|
|
|
|
gas_used: view.gas_used().into(),
|
|
|
|
gas_limit: view.gas_limit().into(),
|
|
|
|
logs_bloom: view.log_bloom().into(),
|
|
|
|
timestamp: view.timestamp().into(),
|
|
|
|
difficulty: view.difficulty().into(),
|
|
|
|
total_difficulty: total_difficulty.into(),
|
2016-09-01 14:49:12 +02:00
|
|
|
seal_fields: view.seal().into_iter().map(|f| rlp::decode(&f)).map(Bytes::new).collect(),
|
2016-07-06 11:23:29 +02:00
|
|
|
uncles: block_view.uncle_hashes().into_iter().map(Into::into).collect(),
|
2016-07-19 20:40:18 +02:00
|
|
|
transactions: match include_txs {
|
|
|
|
true => BlockTransactions::Full(block_view.localized_transactions().into_iter().map(Into::into).collect()),
|
|
|
|
false => BlockTransactions::Hashes(block_view.transaction_hashes().into_iter().map(Into::into).collect()),
|
2016-02-10 22:54:12 +01:00
|
|
|
},
|
2016-03-19 11:44:36 +01:00
|
|
|
extra_data: Bytes::new(view.extra_data())
|
2016-02-10 22:54:12 +01:00
|
|
|
};
|
2016-09-23 19:42:33 +02:00
|
|
|
Ok(Some(block))
|
2016-02-10 22:54:12 +01:00
|
|
|
},
|
2016-09-23 19:42:33 +02:00
|
|
|
_ => Ok(None)
|
2016-02-10 22:54:12 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-29 11:58:33 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn transaction(&self, id: TransactionID) -> Result<Option<Transaction>, Error> {
|
2016-02-29 11:58:33 +01:00
|
|
|
match take_weak!(self.client).transaction(id) {
|
2016-09-23 19:42:33 +02:00
|
|
|
Some(t) => Ok(Some(Transaction::from(t))),
|
|
|
|
None => Ok(None),
|
2016-02-10 22:54:12 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-14 17:01:10 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn uncle(&self, id: UncleID) -> Result<Option<Block>, Error> {
|
2016-03-22 16:07:42 +01:00
|
|
|
let client = take_weak!(self.client);
|
2016-07-01 20:37:17 +02:00
|
|
|
let uncle: BlockHeader = match client.uncle(id) {
|
2016-09-01 14:49:12 +02:00
|
|
|
Some(rlp) => rlp::decode(&rlp),
|
2016-09-23 19:42:33 +02:00
|
|
|
None => { return Ok(None); }
|
2016-07-01 20:37:17 +02:00
|
|
|
};
|
|
|
|
let parent_difficulty = match client.block_total_difficulty(BlockID::Hash(uncle.parent_hash().clone())) {
|
|
|
|
Some(difficulty) => difficulty,
|
2016-09-23 19:42:33 +02:00
|
|
|
None => { return Ok(None); }
|
2016-07-01 20:37:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let block = Block {
|
2016-07-06 11:23:29 +02:00
|
|
|
hash: Some(uncle.hash().into()),
|
2016-07-19 20:40:18 +02:00
|
|
|
size: None,
|
2016-08-29 11:35:24 +02:00
|
|
|
parent_hash: uncle.parent_hash().clone().into(),
|
|
|
|
uncles_hash: uncle.uncles_hash().clone().into(),
|
|
|
|
author: uncle.author().clone().into(),
|
|
|
|
miner: uncle.author().clone().into(),
|
|
|
|
state_root: uncle.state_root().clone().into(),
|
|
|
|
transactions_root: uncle.transactions_root().clone().into(),
|
|
|
|
number: Some(uncle.number().into()),
|
|
|
|
gas_used: uncle.gas_used().clone().into(),
|
|
|
|
gas_limit: uncle.gas_limit().clone().into(),
|
|
|
|
logs_bloom: uncle.log_bloom().clone().into(),
|
|
|
|
timestamp: uncle.timestamp().into(),
|
|
|
|
difficulty: uncle.difficulty().clone().into(),
|
|
|
|
total_difficulty: (uncle.difficulty().clone() + parent_difficulty).into(),
|
|
|
|
receipts_root: uncle.receipts_root().clone().into(),
|
|
|
|
extra_data: uncle.extra_data().clone().into(),
|
2016-09-01 14:49:12 +02:00
|
|
|
seal_fields: uncle.seal().clone().into_iter().map(|f| rlp::decode(&f)).map(Bytes::new).collect(),
|
2016-07-01 20:37:17 +02:00
|
|
|
uncles: vec![],
|
|
|
|
transactions: BlockTransactions::Hashes(vec![]),
|
|
|
|
};
|
2016-09-23 19:42:33 +02:00
|
|
|
Ok(Some(block))
|
2016-03-14 17:01:10 +01:00
|
|
|
}
|
2016-03-22 17:17:50 +01:00
|
|
|
|
2016-07-06 11:23:29 +02:00
|
|
|
fn sign_call(&self, request: CRequest) -> Result<SignedTransaction, Error> {
|
2016-06-22 15:55:07 +02:00
|
|
|
let (client, miner) = (take_weak!(self.client), take_weak!(self.miner));
|
2016-03-26 13:30:02 +01:00
|
|
|
let from = request.from.unwrap_or(Address::zero());
|
2016-03-28 18:53:33 +02:00
|
|
|
Ok(EthTransaction {
|
2016-05-26 22:17:55 +02:00
|
|
|
nonce: request.nonce.unwrap_or_else(|| client.latest_nonce(&from)),
|
2016-03-26 13:30:02 +01:00
|
|
|
action: request.to.map_or(Action::Create, Action::Call),
|
2016-04-14 21:01:12 +02:00
|
|
|
gas: request.gas.unwrap_or(U256::from(50_000_000)),
|
2016-06-22 15:55:07 +02:00
|
|
|
gas_price: request.gas_price.unwrap_or_else(|| default_gas_price(&*client, &*miner)),
|
2016-03-26 13:30:02 +01:00
|
|
|
value: request.value.unwrap_or_else(U256::zero),
|
|
|
|
data: request.data.map_or_else(Vec::new, |d| d.to_vec())
|
2016-03-28 18:53:33 +02:00
|
|
|
}.fake_sign(from))
|
2016-03-22 17:17:50 +01:00
|
|
|
}
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|
2016-03-22 17:17:50 +01:00
|
|
|
|
2016-06-01 19:37:34 +02:00
|
|
|
pub fn pending_logs<M>(miner: &M, filter: &EthcoreFilter) -> Vec<Log> where M: MinerService {
|
|
|
|
let receipts = miner.pending_receipts();
|
|
|
|
|
|
|
|
let pending_logs = receipts.into_iter()
|
|
|
|
.flat_map(|(hash, r)| r.logs.into_iter().map(|l| (hash.clone(), l)).collect::<Vec<(H256, LogEntry)>>())
|
|
|
|
.collect::<Vec<(H256, LogEntry)>>();
|
|
|
|
|
|
|
|
let result = pending_logs.into_iter()
|
|
|
|
.filter(|pair| filter.matches(&pair.1))
|
|
|
|
.map(|pair| {
|
|
|
|
let mut log = Log::from(pair.1);
|
2016-07-06 11:23:29 +02:00
|
|
|
log.transaction_hash = Some(pair.0.into());
|
2016-06-01 19:37:34 +02:00
|
|
|
log
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2016-03-24 15:13:43 +01:00
|
|
|
const MAX_QUEUE_SIZE_TO_MINE_ON: usize = 4; // because uncles go back 6.
|
2016-03-22 17:17:50 +01:00
|
|
|
|
2016-07-16 15:51:06 +02:00
|
|
|
impl<C, S: ?Sized, M, EM> EthClient<C, S, M, EM> where
|
2016-07-05 17:50:46 +02:00
|
|
|
C: MiningBlockChainClient + 'static,
|
|
|
|
S: SyncProvider + 'static,
|
|
|
|
M: MinerService + 'static,
|
|
|
|
EM: ExternalMinerService + 'static {
|
|
|
|
|
|
|
|
fn active(&self) -> Result<(), Error> {
|
|
|
|
// TODO: only call every 30s at most.
|
|
|
|
take_weak!(self.client).keep_alive();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 14:25:44 +02:00
|
|
|
#[cfg(windows)]
|
|
|
|
static SOLC: &'static str = "solc.exe";
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
static SOLC: &'static str = "solc";
|
|
|
|
|
2016-07-16 15:51:06 +02:00
|
|
|
impl<C, S: ?Sized, M, EM> Eth for EthClient<C, S, M, EM> where
|
2016-05-31 21:31:42 +02:00
|
|
|
C: MiningBlockChainClient + 'static,
|
2016-05-22 12:42:22 +02:00
|
|
|
S: SyncProvider + 'static,
|
|
|
|
M: MinerService + 'static,
|
|
|
|
EM: ExternalMinerService + 'static {
|
2016-03-11 14:26:23 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn protocol_version(&self) -> Result<String, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
let version = take_weak!(self.sync).status().protocol_version.to_owned();
|
|
|
|
Ok(format!("{}", version))
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn syncing(&self) -> Result<SyncStatus, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
|
|
|
|
let status = take_weak!(self.sync).status();
|
2016-09-23 19:42:33 +02:00
|
|
|
match status.state {
|
|
|
|
SyncState::Idle => Ok(SyncStatus::None),
|
2016-09-06 15:31:13 +02:00
|
|
|
SyncState::Waiting | SyncState::Blocks | SyncState::NewBlocks | SyncState::ChainHead
|
|
|
|
| SyncState::SnapshotManifest | SyncState::SnapshotData | SyncState::SnapshotWaiting => {
|
2016-08-08 17:25:15 +02:00
|
|
|
let current_block = U256::from(take_weak!(self.client).chain_info().best_block_number);
|
|
|
|
let highest_block = U256::from(status.highest_block_number.unwrap_or(status.start_block_number));
|
|
|
|
|
|
|
|
if highest_block > current_block + U256::from(6) {
|
|
|
|
let info = SyncInfo {
|
|
|
|
starting_block: status.start_block_number.into(),
|
|
|
|
current_block: current_block.into(),
|
|
|
|
highest_block: highest_block.into(),
|
|
|
|
};
|
2016-09-23 19:42:33 +02:00
|
|
|
Ok(SyncStatus::Info(info))
|
2016-08-08 17:25:15 +02:00
|
|
|
} else {
|
2016-09-23 19:42:33 +02:00
|
|
|
Ok(SyncStatus::None)
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
2016-02-10 16:28:59 +01:00
|
|
|
}
|
2016-09-23 19:42:33 +02:00
|
|
|
}
|
2016-02-05 13:21:34 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn author(&self) -> Result<RpcH160, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
Ok(RpcH160::from(take_weak!(self.miner).author()))
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn is_mining(&self) -> Result<bool, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
Ok(take_weak!(self.miner).is_sealing())
|
2016-01-21 11:25:39 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn hashrate(&self) -> Result<RpcU256, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
Ok(RpcU256::from(self.external_miner.hashrate()))
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn gas_price(&self) -> Result<RpcU256, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
|
|
|
|
let (client, miner) = (take_weak!(self.client), take_weak!(self.miner));
|
2016-09-23 19:42:33 +02:00
|
|
|
Ok(RpcU256::from(default_gas_price(&*client, &*miner)))
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|
2016-01-21 11:25:39 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn accounts(&self) -> Result<Vec<RpcH160>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
|
|
|
|
let store = take_weak!(self.accounts);
|
|
|
|
let accounts = try!(store.accounts().map_err(|e| errors::internal("Could not fetch accounts.", e)));
|
2016-09-23 19:42:33 +02:00
|
|
|
Ok(accounts.into_iter().map(Into::into).collect())
|
2016-03-12 19:21:08 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn block_number(&self) -> Result<RpcU256, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
Ok(RpcU256::from(take_weak!(self.client).chain_info().best_block_number))
|
2016-01-21 11:25:39 +01:00
|
|
|
}
|
2016-01-26 00:42:07 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn balance(&self, address: RpcH160, num: Trailing<BlockNumber>) -> Result<RpcU256, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
let address = address.into();
|
|
|
|
match num.0 {
|
|
|
|
BlockNumber::Pending => Ok(take_weak!(self.miner).balance(&*take_weak!(self.client), &address).into()),
|
|
|
|
id => match take_weak!(self.client).balance(&address, id.into()) {
|
|
|
|
Some(balance) => Ok(balance.into()),
|
|
|
|
None => Err(errors::state_pruned()),
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 19:51:24 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn storage_at(&self, address: RpcH160, pos: RpcU256, num: Trailing<BlockNumber>) -> Result<RpcH256, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
let address: Address = RpcH160::into(address);
|
|
|
|
let position: U256 = RpcU256::into(pos);
|
|
|
|
match num.0 {
|
|
|
|
BlockNumber::Pending => Ok(take_weak!(self.miner).storage_at(&*take_weak!(self.client), &address, &H256::from(position)).into()),
|
|
|
|
id => match take_weak!(self.client).storage_at(&address, &H256::from(position), id.into()) {
|
|
|
|
Some(s) => Ok(s.into()),
|
|
|
|
None => Err(errors::state_pruned()),
|
|
|
|
}
|
|
|
|
}
|
2016-03-13 12:09:30 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn transaction_count(&self, address: RpcH160, num: Trailing<BlockNumber>) -> Result<RpcU256, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
let address: Address = RpcH160::into(address);
|
|
|
|
match num.0 {
|
|
|
|
BlockNumber::Pending => Ok(take_weak!(self.miner).nonce(&*take_weak!(self.client), &address).into()),
|
|
|
|
id => match take_weak!(self.client).nonce(&address, id.into()) {
|
|
|
|
Some(nonce) => Ok(nonce.into()),
|
|
|
|
None => Err(errors::state_pruned()),
|
|
|
|
}
|
|
|
|
}
|
2016-03-13 14:37:33 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn block_transaction_count_by_hash(&self, hash: RpcH256) -> Result<Option<RpcU256>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
Ok(
|
|
|
|
take_weak!(self.client).block(BlockID::Hash(hash.into()))
|
|
|
|
.map(|bytes| BlockView::new(&bytes).transactions_count().into())
|
|
|
|
)
|
2016-02-05 13:21:34 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn block_transaction_count_by_number(&self, num: BlockNumber) -> Result<Option<RpcU256>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
match num {
|
|
|
|
BlockNumber::Pending => Ok(Some(
|
|
|
|
take_weak!(self.miner).status().transactions_in_pending_block.into()
|
|
|
|
)),
|
|
|
|
_ => Ok(
|
|
|
|
take_weak!(self.client).block(num.into())
|
|
|
|
.map(|bytes| BlockView::new(&bytes).transactions_count().into())
|
|
|
|
)
|
|
|
|
}
|
2016-02-25 16:58:18 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn block_uncles_count_by_hash(&self, hash: RpcH256) -> Result<Option<RpcU256>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
Ok(
|
|
|
|
take_weak!(self.client).block(BlockID::Hash(hash.into()))
|
|
|
|
.map(|bytes| BlockView::new(&bytes).uncles_count().into())
|
|
|
|
)
|
2016-03-13 14:37:33 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn block_uncles_count_by_number(&self, num: BlockNumber) -> Result<Option<RpcU256>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
match num {
|
|
|
|
BlockNumber::Pending => Ok(Some(0.into())),
|
|
|
|
_ => Ok(
|
|
|
|
take_weak!(self.client).block(num.into())
|
|
|
|
.map(|bytes| BlockView::new(&bytes).uncles_count().into())
|
|
|
|
),
|
|
|
|
}
|
2016-01-26 00:42:07 +01:00
|
|
|
}
|
2016-01-26 11:37:24 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn code_at(&self, address: RpcH160, num: Trailing<BlockNumber>) -> Result<Bytes, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
let address: Address = RpcH160::into(address);
|
|
|
|
match num.0 {
|
|
|
|
BlockNumber::Pending => Ok(take_weak!(self.miner).code(&*take_weak!(self.client), &address).map_or_else(Bytes::default, Bytes::new)),
|
|
|
|
_ => match take_weak!(self.client).code(&address, num.0.into()) {
|
|
|
|
Some(code) => Ok(code.map_or_else(Bytes::default, Bytes::new)),
|
|
|
|
None => Err(errors::state_pruned()),
|
|
|
|
},
|
|
|
|
}
|
2016-02-08 10:58:08 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn block_by_hash(&self, hash: RpcH256, include_txs: bool) -> Result<Option<Block>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
self.block(BlockID::Hash(hash.into()), include_txs)
|
2016-02-10 22:54:12 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn block_by_number(&self, num: BlockNumber, include_txs: bool) -> Result<Option<Block>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
self.block(num.into(), include_txs)
|
2016-01-26 11:37:24 +01:00
|
|
|
}
|
2016-02-09 13:17:44 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn transaction_by_hash(&self, hash: RpcH256) -> Result<Option<Transaction>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
let miner = take_weak!(self.miner);
|
|
|
|
let hash: H256 = hash.into();
|
|
|
|
match miner.transaction(&hash) {
|
|
|
|
Some(pending_tx) => Ok(Some(pending_tx.into())),
|
|
|
|
None => self.transaction(TransactionID::Hash(hash))
|
|
|
|
}
|
2016-02-10 11:28:40 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn transaction_by_block_hash_and_index(&self, hash: RpcH256, index: Index) -> Result<Option<Transaction>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
self.transaction(TransactionID::Location(BlockID::Hash(hash.into()), index.value()))
|
2016-02-10 11:28:40 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn transaction_by_block_number_and_index(&self, num: BlockNumber, index: Index) -> Result<Option<Transaction>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
self.transaction(TransactionID::Location(num.into(), index.value()))
|
2016-02-09 13:17:44 +01:00
|
|
|
}
|
2016-02-15 13:18:26 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn transaction_receipt(&self, hash: RpcH256) -> Result<Option<Receipt>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
let miner = take_weak!(self.miner);
|
|
|
|
let hash: H256 = hash.into();
|
|
|
|
match (miner.pending_receipt(&hash), self.options.allow_pending_receipt_query) {
|
|
|
|
(Some(receipt), true) => Ok(Some(receipt.into())),
|
|
|
|
_ => {
|
|
|
|
let client = take_weak!(self.client);
|
|
|
|
let receipt = client.transaction_receipt(TransactionID::Hash(hash));
|
|
|
|
Ok(receipt.map(Into::into))
|
|
|
|
}
|
|
|
|
}
|
2016-03-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn uncle_by_block_hash_and_index(&self, hash: RpcH256, index: Index) -> Result<Option<Block>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
self.uncle(UncleID { block: BlockID::Hash(hash.into()), position: index.value() })
|
2016-03-14 17:01:10 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn uncle_by_block_number_and_index(&self, num: BlockNumber, index: Index) -> Result<Option<Block>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
self.uncle(UncleID { block: num.into(), position: index.value() })
|
2016-03-14 17:01:10 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn compilers(&self) -> Result<Vec<String>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-08-08 17:25:15 +02:00
|
|
|
|
|
|
|
let mut compilers = vec![];
|
|
|
|
if Command::new(SOLC).output().is_ok() {
|
|
|
|
compilers.push("solidity".to_owned())
|
2016-03-13 15:02:46 +01:00
|
|
|
}
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
Ok(compilers)
|
2016-03-13 15:02:46 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn logs(&self, filter: Filter) -> Result<Vec<Log>, Error> {
|
|
|
|
let include_pending = filter.to_block == Some(BlockNumber::Pending);
|
|
|
|
let filter: EthcoreFilter = filter.into();
|
|
|
|
let mut logs = take_weak!(self.client).logs(filter.clone())
|
|
|
|
.into_iter()
|
|
|
|
.map(From::from)
|
|
|
|
.collect::<Vec<Log>>();
|
2016-09-14 12:02:30 +02:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
if include_pending {
|
|
|
|
let pending = pending_logs(&*take_weak!(self.miner), &filter);
|
|
|
|
logs.extend(pending);
|
|
|
|
}
|
2016-05-24 21:56:32 +02:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
let logs = limit_logs(logs, filter.limit);
|
|
|
|
|
|
|
|
Ok(logs)
|
2016-02-15 13:18:26 +01:00
|
|
|
}
|
2016-02-29 19:30:13 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn work(&self, no_new_work_timeout: Trailing<u64>) -> Result<Work, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
let no_new_work_timeout = no_new_work_timeout.0;
|
2016-06-28 18:46:09 +02:00
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
let client = take_weak!(self.client);
|
|
|
|
// check if we're still syncing and return empty strings in that case
|
|
|
|
{
|
|
|
|
//TODO: check if initial sync is complete here
|
|
|
|
//let sync = take_weak!(self.sync);
|
|
|
|
if /*sync.status().state != SyncState::Idle ||*/ client.queue_info().total_queue_size() > MAX_QUEUE_SIZE_TO_MINE_ON {
|
|
|
|
trace!(target: "miner", "Syncing. Cannot give any work.");
|
|
|
|
return Err(errors::no_work());
|
|
|
|
}
|
2016-03-15 17:13:44 +01:00
|
|
|
|
2016-08-08 17:25:15 +02:00
|
|
|
// Otherwise spin until our submitted block has been included.
|
|
|
|
let timeout = Instant::now() + Duration::from_millis(1000);
|
|
|
|
while Instant::now() < timeout && client.queue_info().total_queue_size() > 0 {
|
|
|
|
thread::sleep(Duration::from_millis(1));
|
|
|
|
}
|
2016-02-29 19:30:13 +01:00
|
|
|
}
|
2016-08-08 17:25:15 +02:00
|
|
|
|
|
|
|
let miner = take_weak!(self.miner);
|
|
|
|
if miner.author().is_zero() {
|
|
|
|
warn!(target: "miner", "Cannot give work package - no author is configured. Use --author to configure!");
|
|
|
|
return Err(errors::no_author())
|
|
|
|
}
|
2016-08-10 16:29:40 +02:00
|
|
|
miner.map_sealing_work(&*client, |b| {
|
2016-08-08 17:25:15 +02:00
|
|
|
let pow_hash = b.hash();
|
|
|
|
let target = Ethash::difficulty_to_boundary(b.block().header().difficulty());
|
|
|
|
let seed_hash = self.seed_compute.lock().get_seedhash(b.block().header().number());
|
|
|
|
|
2016-08-23 17:07:00 +02:00
|
|
|
if no_new_work_timeout > 0 && b.block().header().timestamp() + no_new_work_timeout < get_time().sec as u64 {
|
|
|
|
Err(errors::no_new_work())
|
|
|
|
} else if self.options.send_block_number_in_get_work {
|
2016-09-23 19:42:33 +02:00
|
|
|
let block_number = b.block().header().number();
|
|
|
|
Ok(Work {
|
|
|
|
pow_hash: pow_hash.into(),
|
|
|
|
seed_hash: seed_hash.into(),
|
|
|
|
target: target.into(),
|
|
|
|
number: Some(block_number),
|
|
|
|
})
|
2016-08-08 17:25:15 +02:00
|
|
|
} else {
|
2016-09-23 19:42:33 +02:00
|
|
|
Ok(Work {
|
|
|
|
pow_hash: pow_hash.into(),
|
|
|
|
seed_hash: seed_hash.into(),
|
|
|
|
target: target.into(),
|
|
|
|
number: None
|
|
|
|
})
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
}).unwrap_or(Err(Error::internal_error())) // no work found.
|
2016-02-29 19:30:13 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn submit_work(&self, nonce: RpcH64, pow_hash: RpcH256, mix_hash: RpcH256) -> Result<bool, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
let nonce: H64 = nonce.into();
|
|
|
|
let pow_hash: H256 = pow_hash.into();
|
|
|
|
let mix_hash: H256 = mix_hash.into();
|
|
|
|
trace!(target: "miner", "submit_work: Decoded: nonce={}, pow_hash={}, mix_hash={}", nonce, pow_hash, mix_hash);
|
|
|
|
|
|
|
|
let miner = take_weak!(self.miner);
|
|
|
|
let client = take_weak!(self.client);
|
|
|
|
let seal = vec![rlp::encode(&mix_hash).to_vec(), rlp::encode(&nonce).to_vec()];
|
|
|
|
Ok(miner.submit_seal(&*client, pow_hash, seal).is_ok())
|
2016-03-01 00:02:48 +01:00
|
|
|
}
|
2016-02-29 19:30:13 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn submit_hashrate(&self, rate: RpcU256, id: RpcH256) -> Result<bool, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
self.external_miner.submit_hashrate(rate.into(), id.into());
|
|
|
|
Ok(true)
|
2016-03-01 01:15:00 +01:00
|
|
|
}
|
2016-03-04 18:10:07 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn send_raw_transaction(&self, raw: Bytes) -> Result<RpcH256, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
let raw_transaction = raw.to_vec();
|
|
|
|
match UntrustedRlp::new(&raw_transaction).as_val() {
|
|
|
|
Ok(signed_transaction) => dispatch_transaction(&*take_weak!(self.client), &*take_weak!(self.miner), signed_transaction),
|
|
|
|
Err(_) => Ok(RpcH256::from(H256::from(0))),
|
|
|
|
}
|
2016-03-19 21:37:11 +01:00
|
|
|
}
|
2016-03-20 10:29:21 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn call(&self, request: CallRequest, num: Trailing<BlockNumber>) -> Result<Bytes, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
let request = CallRequest::into(request);
|
|
|
|
let signed = try!(self.sign_call(request));
|
|
|
|
|
|
|
|
let r = match num.0 {
|
|
|
|
BlockNumber::Pending => take_weak!(self.miner).call(&*take_weak!(self.client), &signed, Default::default()),
|
|
|
|
num => take_weak!(self.client).call(&signed, num.into(), Default::default()),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(r.map(|e| Bytes(e.output)).unwrap_or(Bytes::new(vec![])))
|
2016-03-19 21:37:11 +01:00
|
|
|
}
|
2016-03-20 10:29:21 +01:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn estimate_gas(&self, request: CallRequest, num: Trailing<BlockNumber>) -> Result<RpcU256, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
|
|
|
let request = CallRequest::into(request);
|
|
|
|
let signed = try!(self.sign_call(request));
|
|
|
|
let r = match num.0 {
|
|
|
|
BlockNumber::Pending => take_weak!(self.miner).call(&*take_weak!(self.client), &signed, Default::default()),
|
|
|
|
num => take_weak!(self.client).call(&signed, num.into(), Default::default()),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(RpcU256::from(r.map(|res| res.gas_used + res.refunded).unwrap_or(From::from(0))))
|
2016-03-20 10:29:21 +01:00
|
|
|
}
|
2016-05-28 15:25:37 +02:00
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn compile_lll(&self, _: String) -> Result<Bytes, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
2016-05-28 15:25:37 +02:00
|
|
|
rpc_unimplemented!()
|
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn compile_serpent(&self, _: String) -> Result<Bytes, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-23 19:42:33 +02:00
|
|
|
|
2016-05-28 15:25:37 +02:00
|
|
|
rpc_unimplemented!()
|
|
|
|
}
|
|
|
|
|
2016-09-23 19:42:33 +02:00
|
|
|
fn compile_solidity(&self, code: String) -> Result<Bytes, Error> {
|
|
|
|
try!(self.active());
|
|
|
|
let maybe_child = Command::new(SOLC)
|
|
|
|
.arg("--bin")
|
|
|
|
.arg("--optimize")
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.spawn();
|
|
|
|
|
|
|
|
maybe_child
|
|
|
|
.map_err(errors::compilation)
|
|
|
|
.and_then(|mut child| {
|
|
|
|
try!(child.stdin.as_mut()
|
|
|
|
.expect("we called child.stdin(Stdio::piped()) before spawn; qed")
|
|
|
|
.write_all(code.as_bytes())
|
|
|
|
.map_err(errors::compilation));
|
|
|
|
let output = try!(child.wait_with_output().map_err(errors::compilation));
|
|
|
|
|
|
|
|
let s = String::from_utf8_lossy(&output.stdout);
|
|
|
|
if let Some(hex) = s.lines().skip_while(|ref l| !l.contains("Binary")).skip(1).next() {
|
|
|
|
Ok(Bytes::new(hex.from_hex().unwrap_or(vec![])))
|
|
|
|
} else {
|
|
|
|
Err(errors::compilation("Unexpected output."))
|
|
|
|
}
|
2016-07-14 14:25:44 +02:00
|
|
|
})
|
2016-05-28 15:25:37 +02:00
|
|
|
}
|
2016-01-21 11:25:39 +01:00
|
|
|
}
|