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-02 13:59:03 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::{Arc, Weak, Mutex, RwLock};
|
2016-03-10 17:32:17 +01:00
|
|
|
use ethsync::{SyncProvider, SyncState};
|
2016-01-26 19:24:33 +01:00
|
|
|
use jsonrpc_core::*;
|
2016-02-29 22:21:15 +01:00
|
|
|
use util::numbers::*;
|
2016-01-27 12:31:54 +01:00
|
|
|
use util::sha3::*;
|
2016-03-01 00:02:48 +01:00
|
|
|
use util::rlp::encode;
|
2016-01-21 01:19:29 +01:00
|
|
|
use ethcore::client::*;
|
2016-02-29 19:30:13 +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-02-10 18:26:06 +01:00
|
|
|
use ethcore::ethereum::denominations::shannon;
|
2016-03-11 12:31:45 +01:00
|
|
|
use ethcore::transaction::Transaction as EthTransaction;
|
2016-01-27 17:14:41 +01:00
|
|
|
use v1::traits::{Eth, EthFilter};
|
2016-03-04 18:10:07 +01:00
|
|
|
use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, TransactionRequest, OptionalValue, Index, Filter, Log};
|
2016-03-02 05:46:38 +01:00
|
|
|
use v1::helpers::{PollFilter, PollManager};
|
2016-03-10 17:18:01 +01:00
|
|
|
use util::keys::store::AccountProvider;
|
2016-01-21 01:19:29 +01:00
|
|
|
|
2016-01-27 17:08:59 +01:00
|
|
|
/// Eth rpc implementation.
|
2016-03-10 18:56:02 +01:00
|
|
|
pub struct EthClient<C, S, A> where C: BlockChainClient, S: SyncProvider, A: AccountProvider {
|
2016-03-09 17:31:43 +01:00
|
|
|
client: Weak<C>,
|
|
|
|
sync: Weak<S>,
|
2016-03-10 17:18:01 +01:00
|
|
|
accounts: Weak<A>,
|
2016-03-01 01:52:22 +01:00
|
|
|
hashrates: RwLock<HashMap<H256, u64>>,
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 18:56:02 +01:00
|
|
|
impl<C, S, A> EthClient<C, S, A> where C: BlockChainClient, S: SyncProvider, A: AccountProvider {
|
2016-01-27 17:08:59 +01:00
|
|
|
/// Creates new EthClient.
|
2016-03-10 17:18:01 +01:00
|
|
|
pub fn new(client: &Arc<C>, sync: &Arc<S>, accounts: &Arc<A>) -> Self {
|
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-10 17:18:01 +01:00
|
|
|
accounts: Arc::downgrade(accounts),
|
2016-03-01 01:52:22 +01:00
|
|
|
hashrates: RwLock::new(HashMap::new()),
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-10 22:54:12 +01:00
|
|
|
|
|
|
|
fn block(&self, id: BlockId, include_txs: bool) -> Result<Value, 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 {
|
|
|
|
hash: OptionalValue::Value(view.sha3()),
|
|
|
|
parent_hash: view.parent_hash(),
|
|
|
|
uncles_hash: view.uncles_hash(),
|
|
|
|
author: view.author(),
|
|
|
|
miner: view.author(),
|
|
|
|
state_root: view.state_root(),
|
|
|
|
transactions_root: view.transactions_root(),
|
|
|
|
receipts_root: view.receipts_root(),
|
|
|
|
number: OptionalValue::Value(U256::from(view.number())),
|
|
|
|
gas_used: view.gas_used(),
|
|
|
|
gas_limit: view.gas_limit(),
|
|
|
|
logs_bloom: view.log_bloom(),
|
|
|
|
timestamp: U256::from(view.timestamp()),
|
|
|
|
difficulty: view.difficulty(),
|
|
|
|
total_difficulty: total_difficulty,
|
|
|
|
uncles: vec![],
|
|
|
|
transactions: {
|
|
|
|
if include_txs {
|
|
|
|
BlockTransactions::Full(block_view.localized_transactions().into_iter().map(From::from).collect())
|
|
|
|
} else {
|
|
|
|
BlockTransactions::Hashes(block_view.transaction_hashes())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
extra_data: Bytes::default()
|
|
|
|
};
|
|
|
|
to_value(&block)
|
|
|
|
},
|
|
|
|
_ => Ok(Value::Null)
|
|
|
|
}
|
|
|
|
}
|
2016-02-29 11:58:33 +01:00
|
|
|
|
2016-02-10 22:54:12 +01:00
|
|
|
fn transaction(&self, id: TransactionId) -> Result<Value, Error> {
|
2016-02-29 11:58:33 +01:00
|
|
|
match take_weak!(self.client).transaction(id) {
|
2016-02-10 22:54:12 +01:00
|
|
|
Some(t) => to_value(&Transaction::from(t)),
|
|
|
|
None => Ok(Value::Null)
|
|
|
|
}
|
|
|
|
}
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 18:56:02 +01:00
|
|
|
impl<C, S, A> Eth for EthClient<C, S, A> where C: BlockChainClient + 'static, S: SyncProvider + 'static, A: AccountProvider + 'static {
|
2016-01-21 01:19:29 +01:00
|
|
|
fn protocol_version(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
match params {
|
2016-03-13 12:09:30 +01:00
|
|
|
Params::None => Ok(Value::String(format!("{}", take_weak!(self.sync).status().protocol_version).to_owned())),
|
2016-01-21 01:19:29 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:21:34 +01:00
|
|
|
fn syncing(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
match params {
|
2016-02-10 16:28:59 +01:00
|
|
|
Params::None => {
|
2016-02-29 11:58:33 +01:00
|
|
|
let status = take_weak!(self.sync).status();
|
2016-02-10 16:28:59 +01:00
|
|
|
let res = match status.state {
|
|
|
|
SyncState::NotSynced | SyncState::Idle => SyncStatus::None,
|
|
|
|
SyncState::Waiting | SyncState::Blocks | SyncState::NewBlocks => SyncStatus::Info(SyncInfo {
|
|
|
|
starting_block: U256::from(status.start_block_number),
|
2016-02-29 11:58:33 +01:00
|
|
|
current_block: U256::from(take_weak!(self.client).chain_info().best_block_number),
|
2016-02-10 16:28:59 +01:00
|
|
|
highest_block: U256::from(status.highest_block_number.unwrap_or(status.start_block_number))
|
|
|
|
})
|
|
|
|
};
|
|
|
|
to_value(&res)
|
|
|
|
}
|
2016-02-05 13:21:34 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: do not hardcode author.
|
2016-01-21 01:19:29 +01:00
|
|
|
fn author(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
match params {
|
2016-01-27 14:25:12 +01:00
|
|
|
Params::None => to_value(&Address::new()),
|
2016-01-21 01:19:29 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:21:34 +01:00
|
|
|
// TODO: return real value of mining once it's implemented.
|
|
|
|
fn is_mining(&self, params: Params) -> Result<Value, Error> {
|
2016-01-21 11:25:39 +01:00
|
|
|
match params {
|
2016-03-01 02:33:41 +01:00
|
|
|
Params::None => to_value(&!self.hashrates.read().unwrap().is_empty()),
|
2016-01-21 11:25:39 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:21:34 +01:00
|
|
|
// TODO: return real hashrate once we have mining
|
|
|
|
fn hashrate(&self, params: Params) -> Result<Value, Error> {
|
2016-01-21 01:19:29 +01:00
|
|
|
match params {
|
2016-03-01 01:52:22 +01:00
|
|
|
Params::None => to_value(&self.hashrates.read().unwrap().iter().fold(0u64, |sum, (_, v)| sum + v)),
|
2016-01-21 01:19:29 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:21:34 +01:00
|
|
|
fn gas_price(&self, params: Params) -> Result<Value, Error> {
|
2016-01-21 01:19:29 +01:00
|
|
|
match params {
|
2016-02-10 18:26:06 +01:00
|
|
|
Params::None => to_value(&(shannon() * U256::from(50))),
|
2016-01-21 01:19:29 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
2016-01-21 11:25:39 +01:00
|
|
|
|
2016-03-12 19:21:08 +01:00
|
|
|
fn accounts(&self, _: Params) -> Result<Value, Error> {
|
|
|
|
let store = take_weak!(self.accounts);
|
|
|
|
match store.accounts() {
|
|
|
|
Ok(account_list) => to_value(&account_list),
|
|
|
|
Err(_) => Err(Error::internal_error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:21:34 +01:00
|
|
|
fn block_number(&self, params: Params) -> Result<Value, Error> {
|
2016-01-21 11:25:39 +01:00
|
|
|
match params {
|
2016-02-29 11:58:33 +01:00
|
|
|
Params::None => to_value(&U256::from(take_weak!(self.client).chain_info().best_block_number)),
|
2016-01-21 11:25:39 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 00:42:07 +01:00
|
|
|
|
2016-03-12 19:51:24 +01:00
|
|
|
fn balance(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(Address, BlockNumber)>(params)
|
|
|
|
.and_then(|(address, _block_number)| to_value(&take_weak!(self.client).balance(&address)))
|
|
|
|
}
|
|
|
|
|
2016-03-13 12:09:30 +01:00
|
|
|
fn storage_at(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(Address, U256, BlockNumber)>(params)
|
|
|
|
.and_then(|(address, position, _block_number)|
|
|
|
|
to_value(&U256::from(take_weak!(self.client).storage_at(&address, &H256::from(position)))))
|
|
|
|
}
|
|
|
|
|
2016-03-13 14:37:33 +01:00
|
|
|
fn transaction_count(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(Address, BlockNumber)>(params)
|
|
|
|
.and_then(|(address, _block_number)| to_value(&take_weak!(self.client).nonce(&address)))
|
|
|
|
}
|
|
|
|
|
2016-02-25 16:58:18 +01:00
|
|
|
fn block_transaction_count_by_hash(&self, params: Params) -> Result<Value, Error> {
|
2016-02-10 10:12:56 +01:00
|
|
|
from_params::<(H256,)>(params)
|
2016-03-13 14:37:33 +01:00
|
|
|
.and_then(|(hash,)| // match
|
|
|
|
to_value(&take_weak!(self.client).block(BlockId::Hash(hash))
|
|
|
|
.map_or_else(U256::zero, |bytes| U256::from(BlockView::new(&bytes).transactions_count()))))
|
2016-02-05 13:21:34 +01:00
|
|
|
}
|
|
|
|
|
2016-02-25 16:58:18 +01:00
|
|
|
fn block_transaction_count_by_number(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(BlockNumber,)>(params)
|
|
|
|
.and_then(|(block_number,)| match block_number {
|
2016-03-13 14:37:33 +01:00
|
|
|
BlockNumber::Pending => to_value(&U256::from(take_weak!(self.sync).status().transaction_queue_pending)),
|
|
|
|
_ => to_value(&take_weak!(self.client).block(block_number.into())
|
|
|
|
.map_or_else(U256::zero, |bytes| U256::from(BlockView::new(&bytes).transactions_count())))
|
2016-02-25 16:58:18 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-13 14:37:33 +01:00
|
|
|
fn block_uncles_count_by_hash(&self, params: Params) -> Result<Value, Error> {
|
2016-02-10 10:12:56 +01:00
|
|
|
from_params::<(H256,)>(params)
|
2016-03-13 14:37:33 +01:00
|
|
|
.and_then(|(hash,)|
|
|
|
|
to_value(&take_weak!(self.client).block(BlockId::Hash(hash))
|
|
|
|
.map_or_else(U256::zero, |bytes| U256::from(BlockView::new(&bytes).uncles_count()))))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_uncles_count_by_number(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(BlockNumber,)>(params)
|
|
|
|
.and_then(|(block_number,)| match block_number {
|
|
|
|
BlockNumber::Pending => to_value(&U256::from(0)),
|
|
|
|
_ => to_value(&take_weak!(self.client).block(block_number.into())
|
|
|
|
.map_or_else(U256::zero, |bytes| U256::from(BlockView::new(&bytes).uncles_count())))
|
2016-02-10 10:12:56 +01:00
|
|
|
})
|
2016-01-26 00:42:07 +01:00
|
|
|
}
|
2016-01-26 11:37:24 +01:00
|
|
|
|
2016-02-08 10:58:08 +01:00
|
|
|
// TODO: do not ignore block number param
|
|
|
|
fn code_at(&self, params: Params) -> Result<Value, Error> {
|
2016-02-10 10:12:56 +01:00
|
|
|
from_params::<(Address, BlockNumber)>(params)
|
2016-03-13 14:45:39 +01:00
|
|
|
.and_then(|(address, _block_number)|
|
|
|
|
to_value(&take_weak!(self.client).code(&address).map_or_else(Bytes::default, Bytes::new)))
|
2016-02-08 10:58:08 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 22:54:12 +01:00
|
|
|
fn block_by_hash(&self, params: Params) -> Result<Value, Error> {
|
2016-02-10 10:12:56 +01:00
|
|
|
from_params::<(H256, bool)>(params)
|
2016-02-10 22:54:12 +01:00
|
|
|
.and_then(|(hash, include_txs)| self.block(BlockId::Hash(hash), include_txs))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_by_number(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(BlockNumber, bool)>(params)
|
|
|
|
.and_then(|(number, include_txs)| self.block(number.into(), include_txs))
|
2016-01-26 11:37:24 +01:00
|
|
|
}
|
2016-02-09 13:17:44 +01:00
|
|
|
|
2016-02-10 10:12:56 +01:00
|
|
|
fn transaction_by_hash(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(H256,)>(params)
|
2016-02-10 22:54:12 +01:00
|
|
|
.and_then(|(hash,)| self.transaction(TransactionId::Hash(hash)))
|
2016-02-10 11:28:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn transaction_by_block_hash_and_index(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(H256, Index)>(params)
|
2016-02-10 22:54:12 +01:00
|
|
|
.and_then(|(hash, index)| self.transaction(TransactionId::Location(BlockId::Hash(hash), index.value())))
|
2016-02-10 11:28:40 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 22:36:59 +01:00
|
|
|
fn transaction_by_block_number_and_index(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(BlockNumber, Index)>(params)
|
2016-02-10 22:54:12 +01:00
|
|
|
.and_then(|(number, index)| self.transaction(TransactionId::Location(number.into(), index.value())))
|
2016-02-09 13:17:44 +01:00
|
|
|
}
|
2016-02-15 13:18:26 +01:00
|
|
|
|
|
|
|
fn logs(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(Filter,)>(params)
|
|
|
|
.and_then(|(filter,)| {
|
2016-02-29 11:58:33 +01:00
|
|
|
let logs = take_weak!(self.client).logs(filter.into())
|
2016-02-15 13:39:58 +01:00
|
|
|
.into_iter()
|
2016-02-17 14:13:51 +01:00
|
|
|
.map(From::from)
|
|
|
|
.collect::<Vec<Log>>();
|
|
|
|
to_value(&logs)
|
2016-02-15 13:18:26 +01:00
|
|
|
})
|
|
|
|
}
|
2016-02-29 19:30:13 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
match *u {
|
|
|
|
Some(ref b) => {
|
2016-03-01 00:02:48 +01:00
|
|
|
let pow_hash = b.hash();
|
2016-02-29 19:30:13 +01:00
|
|
|
let target = Ethash::difficulty_to_boundary(b.block().header().difficulty());
|
2016-03-01 01:15:00 +01:00
|
|
|
let seed_hash = Ethash::get_seedhash(b.block().header().number());
|
2016-03-01 00:02:48 +01:00
|
|
|
to_value(&(pow_hash, seed_hash, target))
|
2016-02-29 19:30:13 +01:00
|
|
|
}
|
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 00:02:48 +01:00
|
|
|
fn submit_work(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(H64, H256, H256)>(params).and_then(|(nonce, pow_hash, mix_hash)| {
|
2016-03-01 16:58:14 +01:00
|
|
|
// trace!("Decoded: nonce={}, pow_hash={}, mix_hash={}", nonce, pow_hash, mix_hash);
|
2016-03-01 00:02:48 +01:00
|
|
|
let c = take_weak!(self.client);
|
|
|
|
let seal = vec![encode(&mix_hash).to_vec(), encode(&nonce).to_vec()];
|
2016-03-01 16:58:14 +01:00
|
|
|
let r = c.submit_seal(pow_hash, seal);
|
|
|
|
to_value(&r.is_ok())
|
2016-03-01 00:02:48 +01:00
|
|
|
})
|
|
|
|
}
|
2016-02-29 19:30:13 +01:00
|
|
|
|
2016-03-01 01:15:00 +01:00
|
|
|
fn submit_hashrate(&self, params: Params) -> Result<Value, Error> {
|
2016-03-01 01:52:22 +01:00
|
|
|
// TODO: Index should be U256.
|
2016-03-01 01:15:00 +01:00
|
|
|
from_params::<(Index, H256)>(params).and_then(|(rate, id)| {
|
2016-03-01 01:52:22 +01:00
|
|
|
self.hashrates.write().unwrap().insert(id, rate.value() as u64);
|
2016-03-01 01:15:00 +01:00
|
|
|
to_value(&true)
|
|
|
|
})
|
|
|
|
}
|
2016-03-04 18:10:07 +01:00
|
|
|
|
|
|
|
fn send_transaction(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(TransactionRequest, )>(params)
|
|
|
|
.and_then(|(transaction_request, )| {
|
2016-03-10 17:18:01 +01:00
|
|
|
let accounts = take_weak!(self.accounts);
|
|
|
|
match accounts.account_secret(&transaction_request.from) {
|
2016-03-05 16:29:01 +01:00
|
|
|
Ok(secret) => {
|
|
|
|
let sync = take_weak!(self.sync);
|
2016-03-11 12:31:45 +01:00
|
|
|
let transaction: EthTransaction = transaction_request.into();
|
2016-03-05 16:29:01 +01:00
|
|
|
let signed_transaction = transaction.sign(&secret);
|
|
|
|
let hash = signed_transaction.hash();
|
|
|
|
sync.insert_transaction(signed_transaction);
|
|
|
|
to_value(&hash)
|
2016-03-04 18:10:07 +01:00
|
|
|
},
|
2016-03-05 16:29:01 +01:00
|
|
|
Err(_) => { to_value(&U256::zero()) }
|
2016-03-04 18:10:07 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-01-21 11:25:39 +01:00
|
|
|
}
|
|
|
|
|
2016-01-27 17:08:59 +01:00
|
|
|
/// Eth filter rpc implementation.
|
2016-03-09 17:31:43 +01:00
|
|
|
pub struct EthFilterClient<C> where C: BlockChainClient {
|
|
|
|
client: Weak<C>,
|
2016-03-02 05:46:38 +01:00
|
|
|
polls: Mutex<PollManager<PollFilter>>,
|
2016-01-21 11:25:39 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 17:31:43 +01:00
|
|
|
impl<C> EthFilterClient<C> where C: BlockChainClient {
|
2016-01-27 17:08:59 +01:00
|
|
|
/// Creates new Eth filter client.
|
2016-03-09 17:31:43 +01:00
|
|
|
pub fn new(client: &Arc<C>) -> Self {
|
2016-01-21 11:25:39 +01:00
|
|
|
EthFilterClient {
|
2016-03-02 05:15:21 +01:00
|
|
|
client: Arc::downgrade(client),
|
2016-03-02 05:46:38 +01:00
|
|
|
polls: Mutex::new(PollManager::new())
|
2016-01-21 11:25:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 17:31:43 +01:00
|
|
|
impl<C> EthFilter for EthFilterClient<C> where C: BlockChainClient + 'static {
|
2016-02-23 18:51:29 +01:00
|
|
|
fn new_filter(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(Filter,)>(params)
|
|
|
|
.and_then(|(filter,)| {
|
|
|
|
let mut polls = self.polls.lock().unwrap();
|
2016-03-02 05:15:21 +01:00
|
|
|
let id = polls.create_poll(PollFilter::Logs(filter.into()), take_weak!(self.client).chain_info().best_block_number);
|
2016-02-23 18:51:29 +01:00
|
|
|
to_value(&U256::from(id))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_block_filter(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
match params {
|
|
|
|
Params::None => {
|
|
|
|
let mut polls = self.polls.lock().unwrap();
|
2016-03-02 05:15:21 +01:00
|
|
|
let id = polls.create_poll(PollFilter::Block, take_weak!(self.client).chain_info().best_block_number);
|
2016-02-23 18:51:29 +01:00
|
|
|
to_value(&U256::from(id))
|
|
|
|
},
|
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
2016-01-21 11:25:39 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 18:51:29 +01:00
|
|
|
fn new_pending_transaction_filter(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
match params {
|
|
|
|
Params::None => {
|
|
|
|
let mut polls = self.polls.lock().unwrap();
|
2016-03-02 05:15:21 +01:00
|
|
|
let id = polls.create_poll(PollFilter::PendingTransaction, take_weak!(self.client).chain_info().best_block_number);
|
2016-02-23 18:51:29 +01:00
|
|
|
to_value(&U256::from(id))
|
|
|
|
},
|
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
2016-01-21 11:25:39 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 18:51:29 +01:00
|
|
|
fn filter_changes(&self, params: Params) -> Result<Value, Error> {
|
2016-03-02 05:15:21 +01:00
|
|
|
let client = take_weak!(self.client);
|
2016-02-23 18:51:29 +01:00
|
|
|
from_params::<(Index,)>(params)
|
|
|
|
.and_then(|(index,)| {
|
2016-03-10 14:24:33 +01:00
|
|
|
let info = self.polls.lock().unwrap().poll_info(&index.value()).cloned();
|
2016-02-23 18:51:29 +01:00
|
|
|
match info {
|
|
|
|
None => Ok(Value::Array(vec![] as Vec<Value>)),
|
|
|
|
Some(info) => match info.filter {
|
|
|
|
PollFilter::Block => {
|
2016-03-07 10:30:19 +01:00
|
|
|
// + 1, cause we want to return hashes including current block hash.
|
|
|
|
let current_number = client.chain_info().best_block_number + 1;
|
2016-02-24 14:16:05 +01:00
|
|
|
let hashes = (info.block_number..current_number).into_iter()
|
|
|
|
.map(BlockId::Number)
|
2016-03-02 05:15:21 +01:00
|
|
|
.filter_map(|id| client.block_hash(id))
|
2016-02-24 14:16:05 +01:00
|
|
|
.collect::<Vec<H256>>();
|
|
|
|
|
|
|
|
self.polls.lock().unwrap().update_poll(&index.value(), current_number);
|
|
|
|
|
|
|
|
to_value(&hashes)
|
2016-02-23 18:51:29 +01:00
|
|
|
},
|
|
|
|
PollFilter::PendingTransaction => {
|
2016-03-02 14:09:44 +01:00
|
|
|
// TODO: fix implementation once TransactionQueue is merged
|
|
|
|
to_value(&vec![] as &Vec<H256>)
|
2016-02-23 18:51:29 +01:00
|
|
|
},
|
|
|
|
PollFilter::Logs(mut filter) => {
|
|
|
|
filter.from_block = BlockId::Number(info.block_number);
|
|
|
|
filter.to_block = BlockId::Latest;
|
2016-03-02 05:15:21 +01:00
|
|
|
let logs = client.logs(filter)
|
2016-02-23 18:51:29 +01:00
|
|
|
.into_iter()
|
|
|
|
.map(From::from)
|
|
|
|
.collect::<Vec<Log>>();
|
2016-03-02 05:15:21 +01:00
|
|
|
|
|
|
|
let current_number = client.chain_info().best_block_number;
|
2016-02-23 18:51:29 +01:00
|
|
|
self.polls.lock().unwrap().update_poll(&index.value(), current_number);
|
|
|
|
|
|
|
|
to_value(&logs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2016-01-21 11:25:39 +01:00
|
|
|
}
|
2016-02-24 14:16:05 +01:00
|
|
|
|
|
|
|
fn uninstall_filter(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(Index,)>(params)
|
|
|
|
.and_then(|(index,)| {
|
|
|
|
self.polls.lock().unwrap().remove_poll(&index.value());
|
|
|
|
to_value(&true)
|
|
|
|
})
|
|
|
|
}
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|