(most of) parity RPC for light client

This commit is contained in:
Robert Habermeier 2017-02-17 21:38:43 +01:00
parent 3b023c82b7
commit 9316eb4ad3
11 changed files with 587 additions and 89 deletions

View File

@ -322,6 +322,16 @@ impl LightProtocol {
.map(|peer| peer.lock().status.clone()) .map(|peer| peer.lock().status.clone())
} }
/// Get number of (connected, active) peers.
pub fn peer_count(&self) -> (usize, usize) {
let num_pending = self.pending_peers.read().len();
let peers = self.peers.read();
(
num_pending + peers.len(),
peers.values().filter(|p| !p.lock().pending_requests.is_empty()).count(),
)
}
/// Check the maximum amount of requests of a specific type /// Check the maximum amount of requests of a specific type
/// which a peer would be able to serve. Returns zero if the /// which a peer would be able to serve. Returns zero if the
/// peer is unknown or has no buffer flow parameters. /// peer is unknown or has no buffer flow parameters.

View File

@ -110,6 +110,14 @@ impl RequestSet {
pub fn collect_ids<F>(&self) -> F where F: FromIterator<ReqId> { pub fn collect_ids<F>(&self) -> F where F: FromIterator<ReqId> {
self.ids.keys().cloned().collect() self.ids.keys().cloned().collect()
} }
/// Number of requests in the set.
pub fn len(&self) -> usize {
self.ids.len()
}
/// Whether the set is empty.
pub fn is_empty(&self) -> bool { self.len() == 0 }
} }
#[cfg(test)] #[cfg(test)]

View File

@ -245,6 +245,31 @@ impl TransactionQueue {
.collect() .collect()
} }
/// Get all transactions not ready to be propagated.
/// `best_block_number` and `best_block_timestamp` are used to filter out conditionally
/// propagated transactions.
///
/// Returned transactions are batched by sender, in order of ascending nonce.
pub fn future_transactions(&self, best_block_number: u64, best_block_timestamp: u64) -> Vec<PendingTransaction> {
self.by_account.values()
.flat_map(|acct_txs| {
acct_txs.current.iter().skip_while(|tx| match tx.condition {
None => true,
Some(Condition::Number(blk_num)) => blk_num <= best_block_number,
Some(Condition::Timestamp(time)) => time <= best_block_timestamp,
}).chain(acct_txs.future.values()).map(|info| info.hash)
})
.filter_map(|hash| match self.by_hash.get(&hash) {
Some(tx) => Some(tx.clone()),
None => {
warn!(target: "txqueue", "Inconsistency detected between `by_hash` and `by_account`: {} not stored.",
hash);
None
}
})
.collect()
}
/// Addresses for which we store transactions. /// Addresses for which we store transactions.
pub fn queued_senders(&self) -> Vec<Address> { pub fn queued_senders(&self) -> Vec<Address> {
self.by_account.keys().cloned().collect() self.by_account.keys().cloned().collect()
@ -471,4 +496,22 @@ mod tests {
assert!(txq.transaction(&hash).is_none()); assert!(txq.transaction(&hash).is_none());
} }
#[test]
fn future_transactions() {
let sender = Address::default();
let mut txq = TransactionQueue::default();
for i in (0..1).chain(3..10) {
let mut tx = Transaction::default();
tx.nonce = i.into();
let tx = tx.fake_sign(sender);
txq.import(tx.into()).unwrap();
}
assert_eq!(txq.future_transactions(0, 0).len(), 7);
assert_eq!(txq.next_nonce(&sender).unwrap(), 1.into());
}
} }

View File

@ -28,6 +28,7 @@ use light::TransactionQueue as LightTransactionQueue;
use rlp::{self, Stream as StreamRlp}; use rlp::{self, Stream as StreamRlp};
use util::{Address, H520, H256, U256, Uint, Bytes, Mutex, RwLock}; use util::{Address, H520, H256, U256, Uint, Bytes, Mutex, RwLock};
use util::sha3::Hashable; use util::sha3::Hashable;
use stats::Corpus;
use ethkey::Signature; use ethkey::Signature;
use ethsync::LightSync; use ethsync::LightSync;
@ -161,11 +162,16 @@ impl<C: MiningBlockChainClient, M: MinerService> Dispatcher for FullDispatcher<C
/// Light client `ETH` RPC. /// Light client `ETH` RPC.
#[derive(Clone)] #[derive(Clone)]
pub struct LightDispatcher { pub struct LightDispatcher {
sync: Arc<LightSync>, /// Sync service.
client: Arc<LightChainClient>, pub sync: Arc<LightSync>,
on_demand: Arc<OnDemand>, /// Header chain client.
cache: Arc<Mutex<LightDataCache>>, pub client: Arc<LightChainClient>,
transaction_queue: Arc<RwLock<LightTransactionQueue>>, /// On-demand request service.
pub on_demand: Arc<OnDemand>,
/// Data cache.
pub cache: Arc<Mutex<LightDataCache>>,
/// Transaction queue.
pub transaction_queue: Arc<RwLock<LightTransactionQueue>>,
} }
impl LightDispatcher { impl LightDispatcher {
@ -187,13 +193,75 @@ impl LightDispatcher {
transaction_queue: transaction_queue, transaction_queue: transaction_queue,
} }
} }
/// Get a recent gas price corpus.
// TODO: this could be `impl Trait`.
pub fn gas_price_corpus(&self) -> BoxFuture<Corpus<U256>, Error> {
const GAS_PRICE_SAMPLE_SIZE: usize = 100;
if let Some(cached) = self.cache.lock().gas_price_corpus() {
return future::ok(cached).boxed()
}
let cache = self.cache.clone();
let eventual_corpus = self.sync.with_context(|ctx| {
// get some recent headers with gas used,
// and request each of the blocks from the network.
let block_futures = self.client.ancestry_iter(BlockId::Latest)
.filter(|hdr| hdr.gas_used() != U256::default())
.take(GAS_PRICE_SAMPLE_SIZE)
.map(request::Body::new)
.map(|req| self.on_demand.block(ctx, req));
// as the blocks come in, collect gas prices into a vector
stream::futures_unordered(block_futures)
.fold(Vec::new(), |mut v, block| {
for t in block.transaction_views().iter() {
v.push(t.gas_price())
}
future::ok(v)
})
.map(move |v| {
// produce a corpus from the vector, cache it, and return
// the median as the intended gas price.
let corpus: ::stats::Corpus<_> = v.into();
cache.lock().set_gas_price_corpus(corpus.clone());
corpus
})
});
match eventual_corpus {
Some(corp) => corp.map_err(|_| errors::no_light_peers()).boxed(),
None => future::err(errors::network_disabled()).boxed(),
}
}
/// Get an account's next nonce.
pub fn next_nonce(&self, addr: Address) -> BoxFuture<U256, Error> {
// fast path where we don't go to network; nonce provided or can be gotten from queue.
let maybe_nonce = self.transaction_queue.read().next_nonce(&addr);
if let Some(nonce) = maybe_nonce {
return future::ok(nonce).boxed()
}
let best_header = self.client.best_block_header();
let nonce_future = self.sync.with_context(|ctx| self.on_demand.account(ctx, request::Account {
header: best_header,
address: addr,
}));
match nonce_future {
Some(x) => x.map(|acc| acc.nonce).map_err(|_| errors::no_light_peers()).boxed(),
None => future::err(errors::network_disabled()).boxed()
}
}
} }
impl Dispatcher for LightDispatcher { impl Dispatcher for LightDispatcher {
fn fill_optional_fields(&self, request: TransactionRequest, default_sender: Address) fn fill_optional_fields(&self, request: TransactionRequest, default_sender: Address)
-> BoxFuture<FilledTransactionRequest, Error> -> BoxFuture<FilledTransactionRequest, Error>
{ {
const GAS_PRICE_SAMPLE_SIZE: usize = 100;
const DEFAULT_GAS_PRICE: U256 = U256([0, 0, 0, 21_000_000]); const DEFAULT_GAS_PRICE: U256 = U256([0, 0, 0, 21_000_000]);
let gas_limit = self.client.best_block_header().gas_limit(); let gas_limit = self.client.best_block_header().gas_limit();
@ -214,53 +282,13 @@ impl Dispatcher for LightDispatcher {
} }
}; };
// fast path for gas price supplied or cached corpus. // fast path for known gas price.
let known_price = request_gas_price.or_else(|| match request_gas_price {
self.cache.lock().gas_price_corpus().and_then(|corp| corp.median().cloned())
);
match known_price {
Some(gas_price) => future::ok(with_gas_price(gas_price)).boxed(), Some(gas_price) => future::ok(with_gas_price(gas_price)).boxed(),
None => { None => self.gas_price_corpus().and_then(|corp| match corp.median() {
let cache = self.cache.clone(); Some(median) => future::ok(*median),
let gas_price_res = self.sync.with_context(|ctx| { None => future::ok(DEFAULT_GAS_PRICE), // fall back to default on error.
}).map(with_gas_price).boxed()
// get some recent headers with gas used,
// and request each of the blocks from the network.
let block_futures = self.client.ancestry_iter(BlockId::Latest)
.filter(|hdr| hdr.gas_used() != U256::default())
.take(GAS_PRICE_SAMPLE_SIZE)
.map(request::Body::new)
.map(|req| self.on_demand.block(ctx, req));
// as the blocks come in, collect gas prices into a vector
stream::futures_unordered(block_futures)
.fold(Vec::new(), |mut v, block| {
for t in block.transaction_views().iter() {
v.push(t.gas_price())
}
future::ok(v)
})
.map(move |v| {
// produce a corpus from the vector, cache it, and return
// the median as the intended gas price.
let corpus: ::stats::Corpus<_> = v.into();
cache.lock().set_gas_price_corpus(corpus.clone());
corpus.median().cloned().unwrap_or(DEFAULT_GAS_PRICE)
})
.map_err(|_| errors::no_light_peers())
});
// attempt to fetch the median, but fall back to a hardcoded
// value in case of weak corpus or disconnected network.
match gas_price_res {
Some(res) => res.map(with_gas_price).boxed(),
None => future::ok(with_gas_price(DEFAULT_GAS_PRICE)).boxed()
}
}
} }
} }
@ -269,7 +297,6 @@ impl Dispatcher for LightDispatcher {
{ {
let network_id = self.client.signing_network_id(); let network_id = self.client.signing_network_id();
let address = filled.from; let address = filled.from;
let best_header = self.client.best_block_header();
let with_nonce = move |filled: FilledTransactionRequest, nonce| { let with_nonce = move |filled: FilledTransactionRequest, nonce| {
let t = Transaction { let t = Transaction {
@ -294,25 +321,14 @@ impl Dispatcher for LightDispatcher {
})) }))
}; };
// fast path where we don't go to network; nonce provided or can be gotten from queue. // fast path for pre-filled nonce.
let maybe_nonce = filled.nonce.or_else(|| self.transaction_queue.read().next_nonce(&address)); if let Some(nonce) = filled.nonce {
if let Some(nonce) = maybe_nonce {
return future::done(with_nonce(filled, nonce)).boxed() return future::done(with_nonce(filled, nonce)).boxed()
} }
let nonce_future = self.sync.with_context(|ctx| self.on_demand.account(ctx, request::Account { self.next_nonce(address)
header: best_header,
address: address,
}));
let nonce_future = match nonce_future {
Some(x) => x,
None => return future::err(errors::no_light_peers()).boxed()
};
nonce_future
.map_err(|_| errors::no_light_peers()) .map_err(|_| errors::no_light_peers())
.and_then(move |acc| with_nonce(filled, acc.nonce)) .and_then(move |nonce| with_nonce(filled, nonce))
.boxed() .boxed()
} }

View File

@ -60,6 +60,14 @@ pub fn unimplemented(details: Option<String>) -> Error {
} }
} }
pub fn light_unimplemented(details: Option<String>) -> Error {
Error {
code: ErrorCode::ServerError(codes::UNSUPPORTED_REQUEST),
message: "This request is unsupported for light clients.".into(),
data: details.map(Value::String),
}
}
pub fn request_not_found() -> Error { pub fn request_not_found() -> Error {
Error { Error {
code: ErrorCode::ServerError(codes::REQUEST_NOT_FOUND), code: ErrorCode::ServerError(codes::REQUEST_NOT_FOUND),

View File

@ -15,7 +15,12 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! RPC implementations for the light client. //! RPC implementations for the light client.
//!
//! This doesn't re-implement all of the RPC APIs, just those which aren't
//! significantly generic to be reused.
pub mod eth; pub mod eth;
pub mod parity;
pub use self::eth::EthClient; pub use self::eth::EthClient;
pub use self::parity::ParityClient;

View File

@ -0,0 +1,335 @@
// Copyright 2015-2017 Parity Technologies (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/>.
//! Parity-specific rpc implementation.
use std::sync::Arc;
use std::collections::{BTreeMap, HashSet};
use futures::{self, Future, BoxFuture};
use util::RotatingLogger;
use util::misc::version_data;
use crypto::ecies;
use ethkey::{Brain, Generator};
use ethstore::random_phrase;
use ethsync::LightSyncProvider;
use ethcore::account_provider::AccountProvider;
use jsonrpc_core::Error;
use jsonrpc_macros::Trailing;
use v1::helpers::{errors, SigningQueue, SignerService, NetworkSettings};
use v1::helpers::dispatch::{LightDispatcher, DEFAULT_MAC};
use v1::metadata::Metadata;
use v1::traits::Parity;
use v1::types::{
Bytes, U256, H160, H256, H512,
Peers, Transaction, RpcSettings, Histogram,
TransactionStats, LocalTransactionStatus,
BlockNumber, ConsensusCapability, VersionInfo,
OperationsInfo, DappId, ChainStatus,
AccountInfo, HwAccountInfo
};
/// Parity implementation for light client.
pub struct ParityClient {
light_dispatch: Arc<LightDispatcher>,
accounts: Arc<AccountProvider>,
logger: Arc<RotatingLogger>,
settings: Arc<NetworkSettings>,
signer: Option<Arc<SignerService>>,
dapps_interface: Option<String>,
dapps_port: Option<u16>,
}
impl ParityClient {
/// Creates new `ParityClient`.
pub fn new(
light_dispatch: Arc<LightDispatcher>,
accounts: Arc<AccountProvider>,
logger: Arc<RotatingLogger>,
settings: Arc<NetworkSettings>,
signer: Option<Arc<SignerService>>,
dapps_interface: Option<String>,
dapps_port: Option<u16>,
) -> Self {
ParityClient {
light_dispatch: light_dispatch,
accounts: accounts,
logger: logger,
settings: settings,
signer: signer,
dapps_interface: dapps_interface,
dapps_port: dapps_port,
}
}
}
impl Parity for ParityClient {
type Metadata = Metadata;
fn accounts_info(&self, dapp: Trailing<DappId>) -> Result<BTreeMap<H160, AccountInfo>, Error> {
let dapp = dapp.0;
let store = &self.accounts;
let dapp_accounts = store
.note_dapp_used(dapp.clone().into())
.and_then(|_| store.dapps_addresses(dapp.into()))
.map_err(|e| errors::internal("Could not fetch accounts.", e))?
.into_iter().collect::<HashSet<_>>();
let info = store.accounts_info().map_err(|e| errors::account("Could not fetch account info.", e))?;
let other = store.addresses_info();
Ok(info
.into_iter()
.chain(other.into_iter())
.filter(|&(ref a, _)| dapp_accounts.contains(a))
.map(|(a, v)| (H160::from(a), AccountInfo { name: v.name }))
.collect()
)
}
fn hardware_accounts_info(&self) -> Result<BTreeMap<H160, HwAccountInfo>, Error> {
let store = &self.accounts;
let info = store.hardware_accounts_info().map_err(|e| errors::account("Could not fetch account info.", e))?;
Ok(info
.into_iter()
.map(|(a, v)| (H160::from(a), HwAccountInfo { name: v.name, manufacturer: v.meta }))
.collect()
)
}
fn default_account(&self, meta: Self::Metadata) -> BoxFuture<H160, Error> {
let dapp_id = meta.dapp_id();
let default_account = move || {
Ok(self.accounts
.dapps_addresses(dapp_id.into())
.ok()
.and_then(|accounts| accounts.get(0).cloned())
.map(|acc| acc.into())
.unwrap_or_default())
};
futures::done(default_account()).boxed()
}
fn transactions_limit(&self) -> Result<usize, Error> {
Ok(usize::max_value())
}
fn min_gas_price(&self) -> Result<U256, Error> {
Ok(U256::default())
}
fn extra_data(&self) -> Result<Bytes, Error> {
Ok(Bytes::default())
}
fn gas_floor_target(&self) -> Result<U256, Error> {
Ok(U256::default())
}
fn gas_ceil_target(&self) -> Result<U256, Error> {
Ok(U256::default())
}
fn dev_logs(&self) -> Result<Vec<String>, Error> {
let logs = self.logger.logs();
Ok(logs.as_slice().to_owned())
}
fn dev_logs_levels(&self) -> Result<String, Error> {
Ok(self.logger.levels().to_owned())
}
fn net_chain(&self) -> Result<String, Error> {
Ok(self.settings.chain.clone())
}
fn net_peers(&self) -> Result<Peers, Error> {
let peers = self.light_dispatch.sync.peers().into_iter().map(Into::into).collect();
let peer_numbers = self.light_dispatch.sync.peer_numbers();
Ok(Peers {
active: peer_numbers.active,
connected: peer_numbers.connected,
max: peer_numbers.max as u32,
peers: peers,
})
}
fn net_port(&self) -> Result<u16, Error> {
Ok(self.settings.network_port)
}
fn node_name(&self) -> Result<String, Error> {
Ok(self.settings.name.clone())
}
fn registry_address(&self) -> Result<Option<H160>, Error> {
Err(errors::light_unimplemented(None))
}
fn rpc_settings(&self) -> Result<RpcSettings, Error> {
Ok(RpcSettings {
enabled: self.settings.rpc_enabled,
interface: self.settings.rpc_interface.clone(),
port: self.settings.rpc_port as u64,
})
}
fn default_extra_data(&self) -> Result<Bytes, Error> {
Ok(Bytes::new(version_data()))
}
fn gas_price_histogram(&self) -> BoxFuture<Histogram, Error> {
self.light_dispatch.gas_price_corpus()
.and_then(|corpus| corpus.histogram(10).ok_or_else(errors::not_enough_data))
.map(Into::into)
.boxed()
}
fn unsigned_transactions_count(&self) -> Result<usize, Error> {
match self.signer {
None => Err(errors::signer_disabled()),
Some(ref signer) => Ok(signer.len()),
}
}
fn generate_secret_phrase(&self) -> Result<String, Error> {
Ok(random_phrase(12))
}
fn phrase_to_address(&self, phrase: String) -> Result<H160, Error> {
Ok(Brain::new(phrase).generate().unwrap().address().into())
}
fn list_accounts(&self, _: u64, _: Option<H160>, _: Trailing<BlockNumber>) -> Result<Option<Vec<H160>>, Error> {
Err(errors::light_unimplemented(None))
}
fn list_storage_keys(&self, _: H160, _: u64, _: Option<H256>, _: Trailing<BlockNumber>) -> Result<Option<Vec<H256>>, Error> {
Err(errors::light_unimplemented(None))
}
fn encrypt_message(&self, key: H512, phrase: Bytes) -> Result<Bytes, Error> {
ecies::encrypt(&key.into(), &DEFAULT_MAC, &phrase.0)
.map_err(errors::encryption_error)
.map(Into::into)
}
fn pending_transactions(&self) -> Result<Vec<Transaction>, Error> {
let txq = self.light_dispatch.transaction_queue.read();
let chain_info = self.light_dispatch.client.chain_info();
Ok(
txq.ready_transactions(chain_info.best_block_number, chain_info.best_block_timestamp)
.into_iter()
.map(Into::into)
.collect::<Vec<_>>()
)
}
fn future_transactions(&self) -> Result<Vec<Transaction>, Error> {
let txq = self.light_dispatch.transaction_queue.read();
let chain_info = self.light_dispatch.client.chain_info();
Ok(
txq.future_transactions(chain_info.best_block_number, chain_info.best_block_timestamp)
.into_iter()
.map(Into::into)
.collect::<Vec<_>>()
)
}
fn pending_transactions_stats(&self) -> Result<BTreeMap<H256, TransactionStats>, Error> {
let stats = self.light_dispatch.sync.transactions_stats();
Ok(stats.into_iter()
.map(|(hash, stats)| (hash.into(), stats.into()))
.collect()
)
}
fn local_transactions(&self) -> Result<BTreeMap<H256, LocalTransactionStatus>, Error> {
let mut map = BTreeMap::new();
let chain_info = self.light_dispatch.client.chain_info();
let (best_num, best_tm) = (chain_info.best_block_number, chain_info.best_block_timestamp);
let txq = self.light_dispatch.transaction_queue.read();
for pending in txq.ready_transactions(best_num, best_tm) {
map.insert(pending.hash().into(), LocalTransactionStatus::Pending);
}
for future in txq.future_transactions(best_num, best_tm) {
map.insert(future.hash().into(), LocalTransactionStatus::Future);
}
// TODO: other types?
Ok(map)
}
fn signer_port(&self) -> Result<u16, Error> {
self.signer
.clone()
.and_then(|signer| signer.address())
.map(|address| address.1)
.ok_or_else(|| errors::signer_disabled())
}
fn dapps_port(&self) -> Result<u16, Error> {
self.dapps_port
.ok_or_else(|| errors::dapps_disabled())
}
fn dapps_interface(&self) -> Result<String, Error> {
self.dapps_interface.clone()
.ok_or_else(|| errors::dapps_disabled())
}
fn next_nonce(&self, address: H160) -> BoxFuture<U256, Error> {
self.light_dispatch.next_nonce(address.into()).map(Into::into).boxed()
}
fn mode(&self) -> Result<String, Error> {
Err(errors::light_unimplemented(None))
}
fn enode(&self) -> Result<String, Error> {
self.light_dispatch.sync.enode().ok_or_else(errors::network_disabled)
}
fn consensus_capability(&self) -> Result<ConsensusCapability, Error> {
Err(errors::light_unimplemented(None))
}
fn version_info(&self) -> Result<VersionInfo, Error> {
Err(errors::light_unimplemented(None))
}
fn releases_info(&self) -> Result<Option<OperationsInfo>, Error> {
Err(errors::light_unimplemented(None))
}
fn chain_status(&self) -> Result<ChainStatus, Error> {
let chain_info = self.light_dispatch.client.chain_info();
let gap = chain_info.ancient_block_number.map(|x| U256::from(x + 1))
.and_then(|first| chain_info.first_block_number.map(|last| (first, U256::from(last))));
Ok(ChainStatus {
block_gap: gap.map(|(x, y)| (x.into(), y.into())),
})
}
}

View File

@ -18,7 +18,7 @@
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use std::str::FromStr; use std::str::FromStr;
use std::collections::{BTreeMap, HashSet}; use std::collections::{BTreeMap, HashSet};
use futures::{self, Future, BoxFuture}; use futures::{self, future, Future, BoxFuture};
use util::{RotatingLogger, Address}; use util::{RotatingLogger, Address};
use util::misc::version_data; use util::misc::version_data;
@ -235,8 +235,13 @@ impl<C, M, S: ?Sized, U> Parity for ParityClient<C, M, S, U> where
Ok(Bytes::new(version_data())) Ok(Bytes::new(version_data()))
} }
fn gas_price_histogram(&self) -> Result<Histogram, Error> { fn gas_price_histogram(&self) -> BoxFuture<Histogram, Error> {
take_weak!(self.client).gas_price_corpus(100).histogram(10).ok_or_else(errors::not_enough_data).map(Into::into) future::done(take_weakf!(self.client)
.gas_price_corpus(100)
.histogram(10)
.ok_or_else(errors::not_enough_data)
.map(Into::into)
).boxed()
} }
fn unsigned_transactions_count(&self) -> Result<usize, Error> { fn unsigned_transactions_count(&self) -> Result<usize, Error> {
@ -315,16 +320,16 @@ impl<C, M, S: ?Sized, U> Parity for ParityClient<C, M, S, U> where
.ok_or_else(|| errors::dapps_disabled()) .ok_or_else(|| errors::dapps_disabled())
} }
fn next_nonce(&self, address: H160) -> Result<U256, Error> { fn next_nonce(&self, address: H160) -> BoxFuture<U256, Error> {
let address: Address = address.into(); let address: Address = address.into();
let miner = take_weak!(self.miner); let miner = take_weakf!(self.miner);
let client = take_weak!(self.client); let client = take_weakf!(self.client);
Ok(miner.last_nonce(&address) future::ok(miner.last_nonce(&address)
.map(|n| n + 1.into()) .map(|n| n + 1.into())
.unwrap_or_else(|| client.latest_nonce(&address)) .unwrap_or_else(|| client.latest_nonce(&address))
.into() .into()
) ).boxed()
} }
fn mode(&self) -> Result<String, Error> { fn mode(&self) -> Result<String, Error> {

View File

@ -101,8 +101,8 @@ build_rpc_trait! {
fn default_extra_data(&self) -> Result<Bytes, Error>; fn default_extra_data(&self) -> Result<Bytes, Error>;
/// Returns distribution of gas price in latest blocks. /// Returns distribution of gas price in latest blocks.
#[rpc(name = "parity_gasPriceHistogram")] #[rpc(async, name = "parity_gasPriceHistogram")]
fn gas_price_histogram(&self) -> Result<Histogram, Error>; fn gas_price_histogram(&self) -> BoxFuture<Histogram, Error>;
/// Returns number of unsigned transactions waiting in the signer queue (if signer enabled) /// Returns number of unsigned transactions waiting in the signer queue (if signer enabled)
/// Returns error when signer is disabled /// Returns error when signer is disabled
@ -164,8 +164,8 @@ build_rpc_trait! {
fn dapps_interface(&self) -> Result<String, Error>; fn dapps_interface(&self) -> Result<String, Error>;
/// Returns next nonce for particular sender. Should include all transactions in the queue. /// Returns next nonce for particular sender. Should include all transactions in the queue.
#[rpc(name = "parity_nextNonce")] #[rpc(async, name = "parity_nextNonce")]
fn next_nonce(&self, H160) -> Result<U256, Error>; fn next_nonce(&self, H160) -> BoxFuture<U256, Error>;
/// Get the mode. Results one of: "active", "passive", "dark", "offline". /// Get the mode. Results one of: "active", "passive", "dark", "offline".
#[rpc(name = "parity_mode")] #[rpc(name = "parity_mode")]

View File

@ -28,7 +28,7 @@ use ethcore::client::{BlockChainClient, ChainNotify};
use ethcore::snapshot::SnapshotService; use ethcore::snapshot::SnapshotService;
use ethcore::header::BlockNumber; use ethcore::header::BlockNumber;
use sync_io::NetSyncIo; use sync_io::NetSyncIo;
use chain::{ChainSync, SyncStatus}; use chain::{ChainSync, SyncStatus as EthSyncStatus};
use std::net::{SocketAddr, AddrParseError}; use std::net::{SocketAddr, AddrParseError};
use ipc::{BinaryConvertable, BinaryConvertError, IpcConfig}; use ipc::{BinaryConvertable, BinaryConvertError, IpcConfig};
use std::str::FromStr; use std::str::FromStr;
@ -82,12 +82,12 @@ impl Default for SyncConfig {
} }
binary_fixed_size!(SyncConfig); binary_fixed_size!(SyncConfig);
binary_fixed_size!(SyncStatus); binary_fixed_size!(EthSyncStatus);
/// Current sync status /// Current sync status
pub trait SyncProvider: Send + Sync { pub trait SyncProvider: Send + Sync {
/// Get sync status /// Get sync status
fn status(&self) -> SyncStatus; fn status(&self) -> EthSyncStatus;
/// Get peers information /// Get peers information
fn peers(&self) -> Vec<PeerInfo>; fn peers(&self) -> Vec<PeerInfo>;
@ -240,7 +240,7 @@ impl EthSync {
#[cfg_attr(feature = "ipc", ipc(client_ident="SyncClient"))] #[cfg_attr(feature = "ipc", ipc(client_ident="SyncClient"))]
impl SyncProvider for EthSync { impl SyncProvider for EthSync {
/// Get sync status /// Get sync status
fn status(&self) -> SyncStatus { fn status(&self) -> EthSyncStatus {
self.eth_handler.sync.write().status() self.eth_handler.sync.write().status()
} }
@ -620,6 +620,35 @@ pub struct ServiceConfiguration {
pub io_path: String, pub io_path: String,
} }
/// Numbers of peers (max, min, active).
#[derive(Debug, Clone)]
#[cfg_attr(feature = "ipc", binary)]
pub struct PeerNumbers {
/// Number of connected peers.
pub connected: usize,
/// Number of active peers.
pub active: usize,
/// Max peers.
pub max: usize,
/// Min peers.
pub min: usize,
}
/// Light synchronization.
pub trait LightSyncProvider {
/// Get peer numbers.
fn peer_numbers(&self) -> PeerNumbers;
/// Get peers information
fn peers(&self) -> Vec<PeerInfo>;
/// Get the enode if available.
fn enode(&self) -> Option<String>;
/// Returns propagation count for pending transactions.
fn transactions_stats(&self) -> BTreeMap<H256, TransactionStats>;
}
/// Configuration for the light sync. /// Configuration for the light sync.
pub struct LightSyncParams<L> { pub struct LightSyncParams<L> {
/// Network configuration. /// Network configuration.
@ -728,3 +757,46 @@ impl ManageNetwork for LightSync {
} }
} }
impl LightSyncProvider for LightSync {
fn peer_numbers(&self) -> PeerNumbers {
let (connected, active) = self.proto.peer_count();
let config = self.network_config();
PeerNumbers {
connected: connected,
active: active,
max: config.max_peers as usize,
min: config.min_peers as usize,
}
}
fn peers(&self) -> Vec<PeerInfo> {
self.network.with_context_eval(self.subprotocol_name, |ctx| {
let peer_ids = self.network.connected_peers();
peer_ids.into_iter().filter_map(|peer_id| {
let session_info = match ctx.session_info(peer_id) {
None => return None,
Some(info) => info,
};
Some(PeerInfo {
id: session_info.id.map(|id| id.hex()),
client_version: session_info.client_version,
capabilities: session_info.peer_capabilities.into_iter().map(|c| c.to_string()).collect(),
remote_address: session_info.remote_address,
local_address: session_info.local_address,
eth_info: None,
les_info: self.proto.peer_status(&peer_id).map(Into::into),
})
}).collect()
}).unwrap_or_else(Vec::new)
}
fn enode(&self) -> Option<String> {
self.network.external_url()
}
fn transactions_stats(&self) -> BTreeMap<H256, TransactionStats> {
Default::default() // TODO
}
}

View File

@ -72,11 +72,7 @@ mod api {
#[cfg(not(feature = "ipc"))] #[cfg(not(feature = "ipc"))]
mod api; mod api;
pub use api::{ pub use api::*;
EthSync, Params, SyncProvider, ManageNetwork, SyncConfig,
ServiceConfiguration, NetworkConfiguration, PeerInfo, AllowIP, TransactionStats,
LightSync, LightSyncParams, LesProtocolInfo, EthProtocolInfo,
};
pub use chain::{SyncStatus, SyncState}; pub use chain::{SyncStatus, SyncState};
pub use network::{is_valid_node_url, NonReservedPeerMode, NetworkError}; pub use network::{is_valid_node_url, NonReservedPeerMode, NetworkError};