Merge branch 'ethminer_crate'
This commit is contained in:
@@ -19,6 +19,8 @@
|
||||
#![cfg_attr(feature="nightly", feature(custom_derive, custom_attribute, plugin))]
|
||||
#![cfg_attr(feature="nightly", plugin(serde_macros, clippy))]
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate rustc_serialize;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
@@ -27,6 +29,7 @@ extern crate jsonrpc_http_server;
|
||||
extern crate ethcore_util as util;
|
||||
extern crate ethcore;
|
||||
extern crate ethsync;
|
||||
extern crate ethminer;
|
||||
extern crate transient_hashmap;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
//! Helper type with all filter possibilities.
|
||||
|
||||
use util::hash::H256;
|
||||
use ethcore::filter::Filter;
|
||||
|
||||
pub type BlockNumber = u64;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum PollFilter {
|
||||
Block,
|
||||
PendingTransaction,
|
||||
Logs(Filter)
|
||||
Block(BlockNumber),
|
||||
PendingTransaction(Vec<H256>),
|
||||
Logs(BlockNumber, Filter)
|
||||
}
|
||||
|
||||
@@ -22,28 +22,13 @@ use transient_hashmap::{TransientHashMap, Timer, StandardTimer};
|
||||
const POLL_LIFETIME: u64 = 60;
|
||||
|
||||
pub type PollId = usize;
|
||||
pub type BlockNumber = u64;
|
||||
|
||||
pub struct PollInfo<F> {
|
||||
pub filter: F,
|
||||
pub block_number: BlockNumber
|
||||
}
|
||||
|
||||
impl<F> Clone for PollInfo<F> where F: Clone {
|
||||
fn clone(&self) -> Self {
|
||||
PollInfo {
|
||||
filter: self.filter.clone(),
|
||||
block_number: self.block_number.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Indexes all poll requests.
|
||||
///
|
||||
/// Lazily garbage collects unused polls info.
|
||||
pub struct PollManager<F, T = StandardTimer> where T: Timer {
|
||||
polls: TransientHashMap<PollId, PollInfo<F>, T>,
|
||||
next_available_id: PollId
|
||||
polls: TransientHashMap<PollId, F, T>,
|
||||
next_available_id: PollId,
|
||||
}
|
||||
|
||||
impl<F> PollManager<F, StandardTimer> {
|
||||
@@ -54,6 +39,7 @@ impl<F> PollManager<F, StandardTimer> {
|
||||
}
|
||||
|
||||
impl<F, T> PollManager<F, T> where T: Timer {
|
||||
|
||||
pub fn new_with_timer(timer: T) -> Self {
|
||||
PollManager {
|
||||
polls: TransientHashMap::new_with_timer(POLL_LIFETIME, timer),
|
||||
@@ -64,31 +50,30 @@ impl<F, T> PollManager<F, T> where T: Timer {
|
||||
/// Returns id which can be used for new poll.
|
||||
///
|
||||
/// Stores information when last poll happend.
|
||||
pub fn create_poll(&mut self, filter: F, block: BlockNumber) -> PollId {
|
||||
pub fn create_poll(&mut self, filter: F) -> PollId {
|
||||
self.polls.prune();
|
||||
|
||||
let id = self.next_available_id;
|
||||
self.polls.insert(id, filter);
|
||||
|
||||
self.next_available_id += 1;
|
||||
self.polls.insert(id, PollInfo {
|
||||
filter: filter,
|
||||
block_number: block,
|
||||
});
|
||||
id
|
||||
}
|
||||
|
||||
/// Updates information when last poll happend.
|
||||
pub fn update_poll(&mut self, id: &PollId, block: BlockNumber) {
|
||||
self.polls.prune();
|
||||
if let Some(info) = self.polls.get_mut(id) {
|
||||
info.block_number = block;
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns number of block when last poll happend.
|
||||
pub fn poll_info(&mut self, id: &PollId) -> Option<&PollInfo<F>> {
|
||||
// Implementation is always using `poll_mut`
|
||||
#[cfg(test)]
|
||||
/// Get a reference to stored poll filter
|
||||
pub fn poll(&mut self, id: &PollId) -> Option<&F> {
|
||||
self.polls.prune();
|
||||
self.polls.get(id)
|
||||
}
|
||||
|
||||
/// Get a mutable reference to stored poll filter
|
||||
pub fn poll_mut(&mut self, id: &PollId) -> Option<&mut F> {
|
||||
self.polls.prune();
|
||||
self.polls.get_mut(id)
|
||||
}
|
||||
|
||||
/// Removes poll info.
|
||||
pub fn remove_poll(&mut self, id: &PollId) {
|
||||
self.polls.remove(id);
|
||||
@@ -97,48 +82,46 @@ impl<F, T> PollManager<F, T> where T: Timer {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::cell::RefCell;
|
||||
use std::cell::Cell;
|
||||
use transient_hashmap::Timer;
|
||||
use v1::helpers::PollManager;
|
||||
|
||||
struct TestTimer<'a> {
|
||||
time: &'a RefCell<i64>,
|
||||
time: &'a Cell<i64>,
|
||||
}
|
||||
|
||||
impl<'a> Timer for TestTimer<'a> {
|
||||
fn get_time(&self) -> i64 {
|
||||
*self.time.borrow()
|
||||
self.time.get()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_poll_indexer() {
|
||||
let time = RefCell::new(0);
|
||||
let time = Cell::new(0);
|
||||
let timer = TestTimer {
|
||||
time: &time,
|
||||
};
|
||||
|
||||
let mut indexer = PollManager::new_with_timer(timer);
|
||||
assert_eq!(indexer.create_poll(false, 20), 0);
|
||||
assert_eq!(indexer.create_poll(true, 20), 1);
|
||||
assert_eq!(indexer.create_poll(20), 0);
|
||||
assert_eq!(indexer.create_poll(20), 1);
|
||||
|
||||
*time.borrow_mut() = 10;
|
||||
indexer.update_poll(&0, 21);
|
||||
assert_eq!(indexer.poll_info(&0).unwrap().filter, false);
|
||||
assert_eq!(indexer.poll_info(&0).unwrap().block_number, 21);
|
||||
time.set(10);
|
||||
*indexer.poll_mut(&0).unwrap() = 21;
|
||||
assert_eq!(*indexer.poll(&0).unwrap(), 21);
|
||||
assert_eq!(*indexer.poll(&1).unwrap(), 20);
|
||||
|
||||
*time.borrow_mut() = 30;
|
||||
indexer.update_poll(&1, 23);
|
||||
assert_eq!(indexer.poll_info(&1).unwrap().filter, true);
|
||||
assert_eq!(indexer.poll_info(&1).unwrap().block_number, 23);
|
||||
time.set(30);
|
||||
*indexer.poll_mut(&1).unwrap() = 23;
|
||||
assert_eq!(*indexer.poll(&1).unwrap(), 23);
|
||||
|
||||
*time.borrow_mut() = 75;
|
||||
indexer.update_poll(&0, 30);
|
||||
assert!(indexer.poll_info(&0).is_none());
|
||||
assert_eq!(indexer.poll_info(&1).unwrap().filter, true);
|
||||
assert_eq!(indexer.poll_info(&1).unwrap().block_number, 23);
|
||||
time.set(75);
|
||||
assert!(indexer.poll(&0).is_none());
|
||||
assert_eq!(*indexer.poll(&1).unwrap(), 23);
|
||||
|
||||
indexer.remove_poll(&1);
|
||||
assert!(indexer.poll_info(&1).is_none());
|
||||
assert!(indexer.poll(&1).is_none());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,9 +15,11 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Eth rpc implementation.
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::{Arc, Weak, Mutex, RwLock};
|
||||
use std::ops::Deref;
|
||||
use ethsync::{SyncProvider, SyncState};
|
||||
use ethminer::{MinerService};
|
||||
use jsonrpc_core::*;
|
||||
use util::numbers::*;
|
||||
use util::sha3::*;
|
||||
@@ -34,19 +36,29 @@ use v1::helpers::{PollFilter, PollManager};
|
||||
use util::keys::store::AccountProvider;
|
||||
|
||||
/// Eth rpc implementation.
|
||||
pub struct EthClient<C, S, A> where C: BlockChainClient, S: SyncProvider, A: AccountProvider {
|
||||
pub struct EthClient<C, S, A, M>
|
||||
where C: BlockChainClient,
|
||||
S: SyncProvider,
|
||||
A: AccountProvider,
|
||||
M: MinerService {
|
||||
client: Weak<C>,
|
||||
sync: Weak<S>,
|
||||
accounts: Weak<A>,
|
||||
miner: Weak<M>,
|
||||
hashrates: RwLock<HashMap<H256, u64>>,
|
||||
}
|
||||
|
||||
impl<C, S, A> EthClient<C, S, A> where C: BlockChainClient, S: SyncProvider, A: AccountProvider {
|
||||
impl<C, S, A, M> EthClient<C, S, A, M>
|
||||
where C: BlockChainClient,
|
||||
S: SyncProvider,
|
||||
A: AccountProvider,
|
||||
M: MinerService {
|
||||
/// Creates new EthClient.
|
||||
pub fn new(client: &Arc<C>, sync: &Arc<S>, accounts: &Arc<A>) -> Self {
|
||||
pub fn new(client: &Arc<C>, sync: &Arc<S>, accounts: &Arc<A>, miner: &Arc<M>) -> Self {
|
||||
EthClient {
|
||||
client: Arc::downgrade(client),
|
||||
sync: Arc::downgrade(sync),
|
||||
miner: Arc::downgrade(miner),
|
||||
accounts: Arc::downgrade(accounts),
|
||||
hashrates: RwLock::new(HashMap::new()),
|
||||
}
|
||||
@@ -98,7 +110,12 @@ impl<C, S, A> EthClient<C, S, A> where C: BlockChainClient, S: SyncProvider, A:
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, S, A> Eth for EthClient<C, S, A> where C: BlockChainClient + 'static, S: SyncProvider + 'static, A: AccountProvider + 'static {
|
||||
impl<C, S, A, M> Eth for EthClient<C, S, A, M>
|
||||
where C: BlockChainClient + 'static,
|
||||
S: SyncProvider + 'static,
|
||||
A: AccountProvider + 'static,
|
||||
M: MinerService + 'static {
|
||||
|
||||
fn protocol_version(&self, params: Params) -> Result<Value, Error> {
|
||||
match params {
|
||||
Params::None => Ok(Value::String(format!("{}", take_weak!(self.sync).status().protocol_version).to_owned())),
|
||||
@@ -192,7 +209,7 @@ impl<C, S, A> Eth for EthClient<C, S, A> where C: BlockChainClient + 'static, S:
|
||||
fn block_transaction_count_by_number(&self, params: Params) -> Result<Value, Error> {
|
||||
from_params::<(BlockNumber,)>(params)
|
||||
.and_then(|(block_number,)| match block_number {
|
||||
BlockNumber::Pending => to_value(&take_weak!(self.sync).status().transaction_queue_pending),
|
||||
BlockNumber::Pending => to_value(&take_weak!(self.miner).status().transaction_queue_pending),
|
||||
_ => match take_weak!(self.client).block(block_number.into()) {
|
||||
Some(bytes) => to_value(&BlockView::new(&bytes).transactions_count()),
|
||||
None => Ok(Value::Null)
|
||||
@@ -253,8 +270,9 @@ impl<C, S, A> Eth for EthClient<C, S, A> where C: BlockChainClient + 'static, S:
|
||||
fn work(&self, params: Params) -> Result<Value, Error> {
|
||||
match params {
|
||||
Params::None => {
|
||||
let c = take_weak!(self.client);
|
||||
let u = c.sealing_block().lock().unwrap();
|
||||
let miner = take_weak!(self.miner);
|
||||
let client = take_weak!(self.client);
|
||||
let u = miner.sealing_block(client.deref()).lock().unwrap();
|
||||
match *u {
|
||||
Some(ref b) => {
|
||||
let pow_hash = b.hash();
|
||||
@@ -272,9 +290,10 @@ impl<C, S, A> Eth for EthClient<C, S, A> where C: BlockChainClient + 'static, S:
|
||||
fn submit_work(&self, params: Params) -> Result<Value, Error> {
|
||||
from_params::<(H64, H256, H256)>(params).and_then(|(nonce, pow_hash, mix_hash)| {
|
||||
// trace!("Decoded: nonce={}, pow_hash={}, mix_hash={}", nonce, pow_hash, mix_hash);
|
||||
let c = take_weak!(self.client);
|
||||
let miner = take_weak!(self.miner);
|
||||
let client = take_weak!(self.client);
|
||||
let seal = vec![encode(&mix_hash).to_vec(), encode(&nonce).to_vec()];
|
||||
let r = c.submit_seal(pow_hash, seal);
|
||||
let r = miner.submit_seal(client.deref(), pow_hash, seal);
|
||||
to_value(&r.is_ok())
|
||||
})
|
||||
}
|
||||
@@ -293,12 +312,21 @@ impl<C, S, A> Eth for EthClient<C, S, A> where C: BlockChainClient + 'static, S:
|
||||
let accounts = take_weak!(self.accounts);
|
||||
match accounts.account_secret(&transaction_request.from) {
|
||||
Ok(secret) => {
|
||||
let sync = take_weak!(self.sync);
|
||||
let miner = take_weak!(self.miner);
|
||||
let client = take_weak!(self.client);
|
||||
|
||||
let transaction: EthTransaction = transaction_request.into();
|
||||
let signed_transaction = transaction.sign(&secret);
|
||||
let hash = signed_transaction.hash();
|
||||
sync.insert_transaction(signed_transaction);
|
||||
to_value(&hash)
|
||||
|
||||
let import = miner.import_transactions(vec![signed_transaction], |a: &Address| client.nonce(a));
|
||||
match import {
|
||||
Ok(_) => to_value(&hash),
|
||||
Err(e) => {
|
||||
warn!("Error sending transaction: {:?}", e);
|
||||
to_value(&U256::zero())
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(_) => { to_value(&U256::zero()) }
|
||||
}
|
||||
@@ -307,27 +335,39 @@ impl<C, S, A> Eth for EthClient<C, S, A> where C: BlockChainClient + 'static, S:
|
||||
}
|
||||
|
||||
/// Eth filter rpc implementation.
|
||||
pub struct EthFilterClient<C> where C: BlockChainClient {
|
||||
pub struct EthFilterClient<C, M>
|
||||
where C: BlockChainClient,
|
||||
M: MinerService {
|
||||
|
||||
client: Weak<C>,
|
||||
miner: Weak<M>,
|
||||
polls: Mutex<PollManager<PollFilter>>,
|
||||
}
|
||||
|
||||
impl<C> EthFilterClient<C> where C: BlockChainClient {
|
||||
impl<C, M> EthFilterClient<C, M>
|
||||
where C: BlockChainClient,
|
||||
M: MinerService {
|
||||
|
||||
/// Creates new Eth filter client.
|
||||
pub fn new(client: &Arc<C>) -> Self {
|
||||
pub fn new(client: &Arc<C>, miner: &Arc<M>) -> Self {
|
||||
EthFilterClient {
|
||||
client: Arc::downgrade(client),
|
||||
polls: Mutex::new(PollManager::new())
|
||||
miner: Arc::downgrade(miner),
|
||||
polls: Mutex::new(PollManager::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C> EthFilter for EthFilterClient<C> where C: BlockChainClient + 'static {
|
||||
impl<C, M> EthFilter for EthFilterClient<C, M>
|
||||
where C: BlockChainClient + 'static,
|
||||
M: MinerService + 'static {
|
||||
|
||||
fn new_filter(&self, params: Params) -> Result<Value, Error> {
|
||||
from_params::<(Filter,)>(params)
|
||||
.and_then(|(filter,)| {
|
||||
let mut polls = self.polls.lock().unwrap();
|
||||
let id = polls.create_poll(PollFilter::Logs(filter.into()), take_weak!(self.client).chain_info().best_block_number);
|
||||
let block_number = take_weak!(self.client).chain_info().best_block_number;
|
||||
let id = polls.create_poll(PollFilter::Logs(block_number, filter.into()));
|
||||
to_value(&U256::from(id))
|
||||
})
|
||||
}
|
||||
@@ -336,7 +376,7 @@ impl<C> EthFilter for EthFilterClient<C> where C: BlockChainClient + 'static {
|
||||
match params {
|
||||
Params::None => {
|
||||
let mut polls = self.polls.lock().unwrap();
|
||||
let id = polls.create_poll(PollFilter::Block, take_weak!(self.client).chain_info().best_block_number);
|
||||
let id = polls.create_poll(PollFilter::Block(take_weak!(self.client).chain_info().best_block_number));
|
||||
to_value(&U256::from(id))
|
||||
},
|
||||
_ => Err(Error::invalid_params())
|
||||
@@ -347,7 +387,9 @@ impl<C> EthFilter for EthFilterClient<C> where C: BlockChainClient + 'static {
|
||||
match params {
|
||||
Params::None => {
|
||||
let mut polls = self.polls.lock().unwrap();
|
||||
let id = polls.create_poll(PollFilter::PendingTransaction, take_weak!(self.client).chain_info().best_block_number);
|
||||
let pending_transactions = take_weak!(self.miner).pending_transactions_hashes();
|
||||
let id = polls.create_poll(PollFilter::PendingTransaction(pending_transactions));
|
||||
|
||||
to_value(&U256::from(id))
|
||||
},
|
||||
_ => Err(Error::invalid_params())
|
||||
@@ -358,37 +400,47 @@ impl<C> EthFilter for EthFilterClient<C> where C: BlockChainClient + 'static {
|
||||
let client = take_weak!(self.client);
|
||||
from_params::<(Index,)>(params)
|
||||
.and_then(|(index,)| {
|
||||
let info = self.polls.lock().unwrap().poll_info(&index.value()).cloned();
|
||||
match info {
|
||||
let mut polls = self.polls.lock().unwrap();
|
||||
match polls.poll_mut(&index.value()) {
|
||||
None => Ok(Value::Array(vec![] as Vec<Value>)),
|
||||
Some(info) => match info.filter {
|
||||
PollFilter::Block => {
|
||||
Some(filter) => match *filter {
|
||||
PollFilter::Block(ref mut block_number) => {
|
||||
// + 1, cause we want to return hashes including current block hash.
|
||||
let current_number = client.chain_info().best_block_number + 1;
|
||||
let hashes = (info.block_number..current_number).into_iter()
|
||||
let hashes = (*block_number..current_number).into_iter()
|
||||
.map(BlockId::Number)
|
||||
.filter_map(|id| client.block_hash(id))
|
||||
.collect::<Vec<H256>>();
|
||||
|
||||
self.polls.lock().unwrap().update_poll(&index.value(), current_number);
|
||||
*block_number = current_number;
|
||||
|
||||
to_value(&hashes)
|
||||
},
|
||||
PollFilter::PendingTransaction => {
|
||||
// TODO: fix implementation once TransactionQueue is merged
|
||||
to_value(&vec![] as &Vec<H256>)
|
||||
PollFilter::PendingTransaction(ref mut previous_hashes) => {
|
||||
let current_hashes = take_weak!(self.miner).pending_transactions_hashes();
|
||||
// calculate diff
|
||||
let previous_hashes_set = previous_hashes.into_iter().map(|h| h.clone()).collect::<HashSet<H256>>();
|
||||
let diff = current_hashes
|
||||
.iter()
|
||||
.filter(|hash| previous_hashes_set.contains(&hash))
|
||||
.cloned()
|
||||
.collect::<Vec<H256>>();
|
||||
|
||||
*previous_hashes = current_hashes;
|
||||
|
||||
to_value(&diff)
|
||||
},
|
||||
PollFilter::Logs(mut filter) => {
|
||||
filter.from_block = BlockId::Number(info.block_number);
|
||||
PollFilter::Logs(ref mut block_number, ref mut filter) => {
|
||||
filter.from_block = BlockId::Number(*block_number);
|
||||
filter.to_block = BlockId::Latest;
|
||||
let logs = client.logs(filter)
|
||||
let logs = client.logs(filter.clone())
|
||||
.into_iter()
|
||||
.map(From::from)
|
||||
.collect::<Vec<Log>>();
|
||||
|
||||
let current_number = client.chain_info().best_block_number;
|
||||
self.polls.lock().unwrap().update_poll(&index.value(), current_number);
|
||||
|
||||
*block_number = current_number;
|
||||
to_value(&logs)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use ethcore::transaction::SignedTransaction;
|
||||
use ethsync::{SyncProvider, SyncStatus, SyncState};
|
||||
|
||||
pub struct Config {
|
||||
@@ -40,7 +39,6 @@ impl TestSyncProvider {
|
||||
num_peers: config.num_peers,
|
||||
num_active_peers: 0,
|
||||
mem_used: 0,
|
||||
transaction_queue_pending: 0,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -50,9 +48,5 @@ impl SyncProvider for TestSyncProvider {
|
||||
fn status(&self) -> SyncStatus {
|
||||
self.status.clone()
|
||||
}
|
||||
|
||||
fn insert_transaction(&self, _transaction: SignedTransaction) {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user