Merge branch 'master' into remote-tx-exec
This commit is contained in:
@@ -44,6 +44,7 @@ extern crate futures;
|
||||
extern crate order_stat;
|
||||
extern crate parity_updater as updater;
|
||||
extern crate parity_reactor;
|
||||
extern crate stats;
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
@@ -20,16 +20,19 @@ use std::fmt::Debug;
|
||||
use std::ops::Deref;
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
use futures::{future, Future, BoxFuture};
|
||||
use futures::{future, stream, Future, Stream, BoxFuture};
|
||||
use light::cache::Cache as LightDataCache;
|
||||
use light::client::LightChainClient;
|
||||
use light::on_demand::{request, OnDemand};
|
||||
use light::TransactionQueue as LightTransactionQueue;
|
||||
use rlp::{self, Stream};
|
||||
use util::{Address, H520, H256, U256, Uint, Bytes, RwLock};
|
||||
use rlp::{self, Stream as StreamRlp};
|
||||
use util::{Address, H520, H256, U256, Uint, Bytes, Mutex, RwLock};
|
||||
use util::sha3::Hashable;
|
||||
use stats::Corpus;
|
||||
|
||||
use ethkey::Signature;
|
||||
use ethsync::LightSync;
|
||||
use ethcore::ids::BlockId;
|
||||
use ethcore::miner::MinerService;
|
||||
use ethcore::client::MiningBlockChainClient;
|
||||
use ethcore::transaction::{Action, SignedTransaction, PendingTransaction, Transaction};
|
||||
@@ -159,10 +162,16 @@ impl<C: MiningBlockChainClient, M: MinerService> Dispatcher for FullDispatcher<C
|
||||
/// Light client `ETH` RPC.
|
||||
#[derive(Clone)]
|
||||
pub struct LightDispatcher {
|
||||
sync: Arc<LightSync>,
|
||||
client: Arc<LightChainClient>,
|
||||
on_demand: Arc<OnDemand>,
|
||||
transaction_queue: Arc<RwLock<LightTransactionQueue>>,
|
||||
/// Sync service.
|
||||
pub sync: Arc<LightSync>,
|
||||
/// Header chain client.
|
||||
pub client: Arc<LightChainClient>,
|
||||
/// 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 {
|
||||
@@ -173,43 +182,121 @@ impl LightDispatcher {
|
||||
sync: Arc<LightSync>,
|
||||
client: Arc<LightChainClient>,
|
||||
on_demand: Arc<OnDemand>,
|
||||
cache: Arc<Mutex<LightDataCache>>,
|
||||
transaction_queue: Arc<RwLock<LightTransactionQueue>>,
|
||||
) -> Self {
|
||||
LightDispatcher {
|
||||
sync: sync,
|
||||
client: client,
|
||||
on_demand: on_demand,
|
||||
cache: cache,
|
||||
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 {
|
||||
fn fill_optional_fields(&self, request: TransactionRequest, default_sender: Address)
|
||||
-> BoxFuture<FilledTransactionRequest, Error>
|
||||
{
|
||||
let request = request;
|
||||
let gas_limit = self.client.best_block_header().gas_limit();
|
||||
const DEFAULT_GAS_PRICE: U256 = U256([0, 0, 0, 21_000_000]);
|
||||
|
||||
future::ok(FilledTransactionRequest {
|
||||
from: request.from.unwrap_or(default_sender),
|
||||
used_default_from: request.from.is_none(),
|
||||
to: request.to,
|
||||
nonce: request.nonce,
|
||||
gas_price: request.gas_price.unwrap_or_else(|| 21_000_000.into()), // TODO: fetch corpus from network.
|
||||
gas: request.gas.unwrap_or_else(|| gas_limit / 3.into()),
|
||||
value: request.value.unwrap_or_else(|| 0.into()),
|
||||
data: request.data.unwrap_or_else(Vec::new),
|
||||
condition: request.condition,
|
||||
}).boxed()
|
||||
let gas_limit = self.client.best_block_header().gas_limit();
|
||||
let request_gas_price = request.gas_price.clone();
|
||||
|
||||
let with_gas_price = move |gas_price| {
|
||||
let request = request;
|
||||
FilledTransactionRequest {
|
||||
from: request.from.unwrap_or(default_sender),
|
||||
used_default_from: request.from.is_none(),
|
||||
to: request.to,
|
||||
nonce: request.nonce,
|
||||
gas_price: gas_price,
|
||||
gas: request.gas.unwrap_or_else(|| gas_limit / 3.into()),
|
||||
value: request.value.unwrap_or_else(|| 0.into()),
|
||||
data: request.data.unwrap_or_else(Vec::new),
|
||||
condition: request.condition,
|
||||
}
|
||||
};
|
||||
|
||||
// fast path for known gas price.
|
||||
match request_gas_price {
|
||||
Some(gas_price) => future::ok(with_gas_price(gas_price)).boxed(),
|
||||
None => self.gas_price_corpus().and_then(|corp| match corp.median() {
|
||||
Some(median) => future::ok(*median),
|
||||
None => future::ok(DEFAULT_GAS_PRICE), // fall back to default on error.
|
||||
}).map(with_gas_price).boxed()
|
||||
}
|
||||
}
|
||||
|
||||
fn sign(&self, accounts: Arc<AccountProvider>, filled: FilledTransactionRequest, password: SignWith)
|
||||
-> BoxFuture<WithToken<SignedTransaction>, Error>
|
||||
{
|
||||
let network_id = None; // TODO: fetch from client.
|
||||
let network_id = self.client.signing_network_id();
|
||||
let address = filled.from;
|
||||
let best_header = self.client.best_block_header();
|
||||
|
||||
let with_nonce = move |filled: FilledTransactionRequest, nonce| {
|
||||
let t = Transaction {
|
||||
@@ -234,25 +321,14 @@ impl Dispatcher for LightDispatcher {
|
||||
}))
|
||||
};
|
||||
|
||||
// fast path where we don't go to network; nonce provided or can be gotten from queue.
|
||||
let maybe_nonce = filled.nonce.or_else(|| self.transaction_queue.read().next_nonce(&address));
|
||||
if let Some(nonce) = maybe_nonce {
|
||||
// fast path for pre-filled nonce.
|
||||
if let Some(nonce) = filled.nonce {
|
||||
return future::done(with_nonce(filled, nonce)).boxed()
|
||||
}
|
||||
|
||||
let nonce_future = self.sync.with_context(|ctx| self.on_demand.account(ctx, request::Account {
|
||||
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
|
||||
self.next_nonce(address)
|
||||
.map_err(|_| errors::no_light_peers())
|
||||
.and_then(move |acc| with_nonce(filled, acc.nonce))
|
||||
.and_then(move |nonce| with_nonce(filled, nonce))
|
||||
.boxed()
|
||||
}
|
||||
|
||||
@@ -453,7 +529,7 @@ fn decrypt(accounts: &AccountProvider, address: Address, msg: Bytes, password: S
|
||||
pub fn default_gas_price<C, M>(client: &C, miner: &M) -> U256
|
||||
where C: MiningBlockChainClient, M: MinerService
|
||||
{
|
||||
client.gas_price_median(100).unwrap_or_else(|| miner.sensible_gas_price())
|
||||
client.gas_price_corpus(100).median().cloned().unwrap_or_else(|| miner.sensible_gas_price())
|
||||
}
|
||||
|
||||
/// Convert RPC confirmation payload to signer confirmation payload.
|
||||
|
||||
@@ -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 {
|
||||
Error {
|
||||
code: ErrorCode::ServerError(codes::REQUEST_NOT_FOUND),
|
||||
|
||||
@@ -62,11 +62,6 @@ pub struct EthClient {
|
||||
accounts: Arc<AccountProvider>,
|
||||
}
|
||||
|
||||
// helper for internal error: no network context.
|
||||
fn err_no_context() -> Error {
|
||||
errors::internal("network service detached", "")
|
||||
}
|
||||
|
||||
// helper for internal error: on demand sender cancelled.
|
||||
fn err_premature_cancel(_cancel: oneshot::Canceled) -> Error {
|
||||
errors::internal("on-demand sender prematurely cancelled", "")
|
||||
@@ -108,7 +103,7 @@ impl EthClient {
|
||||
|
||||
self.sync.with_context(|ctx|
|
||||
self.on_demand.header_by_number(ctx, req)
|
||||
.map(|(h, _)| Some(h))
|
||||
.map(Some)
|
||||
.map_err(err_premature_cancel)
|
||||
.boxed()
|
||||
)
|
||||
@@ -128,10 +123,9 @@ impl EthClient {
|
||||
_ => None, // latest, earliest, and pending will have all already returned.
|
||||
};
|
||||
|
||||
// todo: cache returned values (header, TD)
|
||||
match maybe_future {
|
||||
Some(recv) => recv,
|
||||
None => future::err(err_no_context()).boxed()
|
||||
None => future::err(errors::network_disabled()).boxed()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +144,7 @@ impl EthClient {
|
||||
address: address,
|
||||
}).map(Some))
|
||||
.map(|x| x.map_err(err_premature_cancel).boxed())
|
||||
.unwrap_or_else(|| future::err(err_no_context()).boxed())
|
||||
.unwrap_or_else(|| future::err(errors::network_disabled()).boxed())
|
||||
}).boxed()
|
||||
}
|
||||
|
||||
@@ -256,7 +250,7 @@ impl Eth for EthClient {
|
||||
sync.with_context(|ctx| on_demand.block(ctx, request::Body::new(hdr)))
|
||||
.map(|x| x.map(|b| Some(U256::from(b.transactions_count()).into())))
|
||||
.map(|x| x.map_err(err_premature_cancel).boxed())
|
||||
.unwrap_or_else(|| future::err(err_no_context()).boxed())
|
||||
.unwrap_or_else(|| future::err(errors::network_disabled()).boxed())
|
||||
}
|
||||
}).boxed()
|
||||
}
|
||||
@@ -276,7 +270,7 @@ impl Eth for EthClient {
|
||||
sync.with_context(|ctx| on_demand.block(ctx, request::Body::new(hdr)))
|
||||
.map(|x| x.map(|b| Some(U256::from(b.transactions_count()).into())))
|
||||
.map(|x| x.map_err(err_premature_cancel).boxed())
|
||||
.unwrap_or_else(|| future::err(err_no_context()).boxed())
|
||||
.unwrap_or_else(|| future::err(errors::network_disabled()).boxed())
|
||||
}
|
||||
}).boxed()
|
||||
}
|
||||
@@ -296,7 +290,7 @@ impl Eth for EthClient {
|
||||
sync.with_context(|ctx| on_demand.block(ctx, request::Body::new(hdr)))
|
||||
.map(|x| x.map(|b| Some(U256::from(b.uncles_count()).into())))
|
||||
.map(|x| x.map_err(err_premature_cancel).boxed())
|
||||
.unwrap_or_else(|| future::err(err_no_context()).boxed())
|
||||
.unwrap_or_else(|| future::err(errors::network_disabled()).boxed())
|
||||
}
|
||||
}).boxed()
|
||||
}
|
||||
@@ -316,7 +310,7 @@ impl Eth for EthClient {
|
||||
sync.with_context(|ctx| on_demand.block(ctx, request::Body::new(hdr)))
|
||||
.map(|x| x.map(|b| Some(U256::from(b.uncles_count()).into())))
|
||||
.map(|x| x.map_err(err_premature_cancel).boxed())
|
||||
.unwrap_or_else(|| future::err(err_no_context()).boxed())
|
||||
.unwrap_or_else(|| future::err(errors::network_disabled()).boxed())
|
||||
}
|
||||
}).boxed()
|
||||
}
|
||||
|
||||
@@ -15,7 +15,15 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! 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 parity;
|
||||
pub mod parity_set;
|
||||
pub mod trace;
|
||||
|
||||
pub use self::eth::EthClient;
|
||||
pub use self::parity::ParityClient;
|
||||
pub use self::parity_set::ParitySetClient;
|
||||
|
||||
332
rpc/src/v1/impls/light/parity.rs
Normal file
332
rpc/src/v1/impls/light/parity.rs
Normal file
@@ -0,0 +1,332 @@
|
||||
// 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::{future, 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.dapp_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();
|
||||
future::ok(self.accounts
|
||||
.dapp_addresses(dapp_id.into())
|
||||
.ok()
|
||||
.and_then(|accounts| accounts.get(0).cloned())
|
||||
.map(|acc| acc.into())
|
||||
.unwrap_or_default()
|
||||
).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())),
|
||||
})
|
||||
}
|
||||
}
|
||||
138
rpc/src/v1/impls/light/parity_set.rs
Normal file
138
rpc/src/v1/impls/light/parity_set.rs
Normal file
@@ -0,0 +1,138 @@
|
||||
// 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 interface for operations altering the settings.
|
||||
//! Implementation for light client.
|
||||
|
||||
use std::io;
|
||||
use std::sync::Arc;
|
||||
|
||||
use ethsync::ManageNetwork;
|
||||
use fetch::Fetch;
|
||||
use futures::{BoxFuture, Future};
|
||||
use util::sha3;
|
||||
|
||||
use jsonrpc_core::Error;
|
||||
use v1::helpers::errors;
|
||||
use v1::traits::ParitySet;
|
||||
use v1::types::{Bytes, H160, H256, U256, ReleaseInfo};
|
||||
|
||||
/// Parity-specific rpc interface for operations altering the settings.
|
||||
pub struct ParitySetClient<F> {
|
||||
net: Arc<ManageNetwork>,
|
||||
fetch: F,
|
||||
}
|
||||
|
||||
impl<F: Fetch> ParitySetClient<F> {
|
||||
/// Creates new `ParitySetClient` with given `Fetch`.
|
||||
pub fn new(net: Arc<ManageNetwork>, fetch: F) -> Self {
|
||||
ParitySetClient {
|
||||
net: net,
|
||||
fetch: fetch,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Fetch> ParitySet for ParitySetClient<F> {
|
||||
fn set_min_gas_price(&self, _gas_price: U256) -> Result<bool, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn set_gas_floor_target(&self, _target: U256) -> Result<bool, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn set_gas_ceil_target(&self, _target: U256) -> Result<bool, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn set_extra_data(&self, _extra_data: Bytes) -> Result<bool, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn set_author(&self, _author: H160) -> Result<bool, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn set_engine_signer(&self, _address: H160, _password: String) -> Result<bool, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn set_transactions_limit(&self, _limit: usize) -> Result<bool, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn set_tx_gas_limit(&self, _limit: U256) -> Result<bool, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn add_reserved_peer(&self, peer: String) -> Result<bool, Error> {
|
||||
match self.net.add_reserved_peer(peer) {
|
||||
Ok(()) => Ok(true),
|
||||
Err(e) => Err(errors::invalid_params("Peer address", e)),
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_reserved_peer(&self, peer: String) -> Result<bool, Error> {
|
||||
match self.net.remove_reserved_peer(peer) {
|
||||
Ok(()) => Ok(true),
|
||||
Err(e) => Err(errors::invalid_params("Peer address", e)),
|
||||
}
|
||||
}
|
||||
|
||||
fn drop_non_reserved_peers(&self) -> Result<bool, Error> {
|
||||
self.net.deny_unreserved_peers();
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn accept_non_reserved_peers(&self) -> Result<bool, Error> {
|
||||
self.net.accept_unreserved_peers();
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn start_network(&self) -> Result<bool, Error> {
|
||||
self.net.start_network();
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn stop_network(&self) -> Result<bool, Error> {
|
||||
self.net.stop_network();
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn set_mode(&self, _mode: String) -> Result<bool, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn hash_content(&self, url: String) -> BoxFuture<H256, Error> {
|
||||
self.fetch.process(self.fetch.fetch(&url).then(move |result| {
|
||||
result
|
||||
.map_err(errors::from_fetch_error)
|
||||
.and_then(|response| {
|
||||
sha3(&mut io::BufReader::new(response)).map_err(errors::from_fetch_error)
|
||||
})
|
||||
.map(Into::into)
|
||||
}))
|
||||
}
|
||||
|
||||
fn upgrade_ready(&self) -> Result<Option<ReleaseInfo>, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn execute_upgrade(&self) -> Result<bool, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
}
|
||||
57
rpc/src/v1/impls/light/trace.rs
Normal file
57
rpc/src/v1/impls/light/trace.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
// 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/>.
|
||||
|
||||
//! Traces api implementation.
|
||||
|
||||
use jsonrpc_core::Error;
|
||||
use jsonrpc_macros::Trailing;
|
||||
use v1::traits::Traces;
|
||||
use v1::helpers::errors;
|
||||
use v1::types::{TraceFilter, LocalizedTrace, BlockNumber, Index, CallRequest, Bytes, TraceResults, H256};
|
||||
|
||||
/// Traces api implementation.
|
||||
// TODO: all calling APIs should be possible w. proved remote TX execution.
|
||||
pub struct TracesClient;
|
||||
|
||||
impl Traces for TracesClient {
|
||||
fn filter(&self, _filter: TraceFilter) -> Result<Vec<LocalizedTrace>, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn block_traces(&self, _block_number: BlockNumber) -> Result<Vec<LocalizedTrace>, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn transaction_traces(&self, _transaction_hash: H256) -> Result<Vec<LocalizedTrace>, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn trace(&self, _transaction_hash: H256, _address: Vec<Index>) -> Result<Option<LocalizedTrace>, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn call(&self, _request: CallRequest, _flags: Vec<String>, _block: Trailing<BlockNumber>) -> Result<Option<TraceResults>, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn raw_transaction(&self, _raw_transaction: Bytes, _flags: Vec<String>, _block: Trailing<BlockNumber>) -> Result<Option<TraceResults>, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
|
||||
fn replay_transaction(&self, _transaction_hash: H256, _flags: Vec<String>) -> Result<Option<TraceResults>, Error> {
|
||||
Err(errors::light_unimplemented(None))
|
||||
}
|
||||
}
|
||||
@@ -232,8 +232,13 @@ impl<C, M, S: ?Sized, U> Parity for ParityClient<C, M, S, U> where
|
||||
Ok(Bytes::new(version_data()))
|
||||
}
|
||||
|
||||
fn gas_price_histogram(&self) -> Result<Histogram, Error> {
|
||||
take_weak!(self.client).gas_price_histogram(100, 10).ok_or_else(errors::not_enough_data).map(Into::into)
|
||||
fn gas_price_histogram(&self) -> BoxFuture<Histogram, Error> {
|
||||
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> {
|
||||
@@ -312,16 +317,16 @@ impl<C, M, S: ?Sized, U> Parity for ParityClient<C, M, S, U> where
|
||||
.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 miner = take_weak!(self.miner);
|
||||
let client = take_weak!(self.client);
|
||||
let miner = take_weakf!(self.miner);
|
||||
let client = take_weakf!(self.client);
|
||||
|
||||
Ok(miner.last_nonce(&address)
|
||||
future::ok(miner.last_nonce(&address)
|
||||
.map(|n| n + 1.into())
|
||||
.unwrap_or_else(|| client.latest_nonce(&address))
|
||||
.into()
|
||||
)
|
||||
).boxed()
|
||||
}
|
||||
|
||||
fn mode(&self) -> Result<String, Error> {
|
||||
|
||||
@@ -101,8 +101,8 @@ build_rpc_trait! {
|
||||
fn default_extra_data(&self) -> Result<Bytes, Error>;
|
||||
|
||||
/// Returns distribution of gas price in latest blocks.
|
||||
#[rpc(name = "parity_gasPriceHistogram")]
|
||||
fn gas_price_histogram(&self) -> Result<Histogram, Error>;
|
||||
#[rpc(async, name = "parity_gasPriceHistogram")]
|
||||
fn gas_price_histogram(&self) -> BoxFuture<Histogram, Error>;
|
||||
|
||||
/// Returns number of unsigned transactions waiting in the signer queue (if signer enabled)
|
||||
/// Returns error when signer is disabled
|
||||
@@ -164,8 +164,8 @@ build_rpc_trait! {
|
||||
fn dapps_interface(&self) -> Result<String, Error>;
|
||||
|
||||
/// Returns next nonce for particular sender. Should include all transactions in the queue.
|
||||
#[rpc(name = "parity_nextNonce")]
|
||||
fn next_nonce(&self, H160) -> Result<U256, Error>;
|
||||
#[rpc(async, name = "parity_nextNonce")]
|
||||
fn next_nonce(&self, H160) -> BoxFuture<U256, Error>;
|
||||
|
||||
/// Get the mode. Results one of: "active", "passive", "dark", "offline".
|
||||
#[rpc(name = "parity_mode")]
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
//! Gas prices histogram.
|
||||
|
||||
use v1::types::U256;
|
||||
use util::stats;
|
||||
|
||||
/// Values of RPC settings.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@@ -27,11 +26,11 @@ pub struct Histogram {
|
||||
#[serde(rename="bucketBounds")]
|
||||
pub bucket_bounds: Vec<U256>,
|
||||
/// Transacion counts for each bucket.
|
||||
pub counts: Vec<u64>,
|
||||
pub counts: Vec<usize>,
|
||||
}
|
||||
|
||||
impl From<stats::Histogram> for Histogram {
|
||||
fn from(h: stats::Histogram) -> Self {
|
||||
impl From<::stats::Histogram<::util::U256>> for Histogram {
|
||||
fn from(h: ::stats::Histogram<::util::U256>) -> Self {
|
||||
Histogram {
|
||||
bucket_bounds: h.bucket_bounds.into_iter().map(Into::into).collect(),
|
||||
counts: h.counts
|
||||
|
||||
Reference in New Issue
Block a user