2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-04-11 21:06:32 +02:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2016-04-11 21:06:32 +02:00
|
|
|
// 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.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2016-04-11 21:06:32 +02:00
|
|
|
// 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
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-04-11 21:06:32 +02:00
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
//! Parity-specific rpc implementation.
|
2019-02-07 14:34:24 +01:00
|
|
|
use std::{collections::BTreeMap, str::FromStr, sync::Arc};
|
2016-08-11 17:19:20 +02:00
|
|
|
|
2018-05-05 11:02:33 +02:00
|
|
|
use crypto::DEFAULT_MAC;
|
2018-04-13 17:34:27 +02:00
|
|
|
use ethcore::{
|
|
|
|
client::{BlockChainClient, Call, StateClient},
|
|
|
|
miner::{self, MinerService},
|
2018-11-07 09:30:34 +01:00
|
|
|
snapshot::{RestorationStatus, SnapshotService},
|
2018-03-03 18:42:13 +01:00
|
|
|
state::StateInfo,
|
|
|
|
};
|
2017-08-28 14:11:55 +02:00
|
|
|
use ethcore_logger::RotatingLogger;
|
2019-02-25 14:27:28 +01:00
|
|
|
use ethereum_types::{Address, H160, H256, H512, H64, U256, U64};
|
2019-01-04 14:05:46 +01:00
|
|
|
use ethkey::{crypto::ecies, Brain, Generator};
|
|
|
|
use ethstore::random_phrase;
|
|
|
|
use jsonrpc_core::{futures::future, BoxFuture, Result};
|
2020-09-14 16:08:57 +02:00
|
|
|
use stats::PrometheusMetrics;
|
2019-01-04 14:05:46 +01:00
|
|
|
use sync::{ManageNetwork, SyncProvider};
|
|
|
|
use types::ids::BlockId;
|
2019-02-07 14:34:24 +01:00
|
|
|
use version::version_data;
|
2018-11-07 09:30:34 +01:00
|
|
|
|
|
|
|
use v1::{
|
|
|
|
helpers::{
|
2020-08-05 06:08:03 +02:00
|
|
|
self,
|
2018-11-07 09:30:34 +01:00
|
|
|
block_import::is_major_importing,
|
2019-02-07 14:34:24 +01:00
|
|
|
errors,
|
|
|
|
external_signer::{SignerService, SigningQueue},
|
2020-08-17 12:47:53 +02:00
|
|
|
fake_sign, verify_signature, NetworkSettings,
|
2019-02-07 14:34:24 +01:00
|
|
|
},
|
2017-01-30 11:10:58 +01:00
|
|
|
metadata::Metadata,
|
2016-11-06 12:51:53 +01:00
|
|
|
traits::Parity,
|
2016-11-16 17:54:54 +01:00
|
|
|
types::{
|
2020-08-14 12:24:16 +02:00
|
|
|
block_number_to_id, BlockNumber, Bytes, CallRequest, ChainStatus, Histogram,
|
|
|
|
LocalTransactionStatus, Peers, Receipt, RecoveredAccount, RichHeader, RpcSettings,
|
|
|
|
Transaction, TransactionStats,
|
2020-08-05 06:08:03 +02:00
|
|
|
},
|
2016-11-16 17:54:54 +01:00
|
|
|
};
|
2017-09-21 14:52:44 +02:00
|
|
|
use Host;
|
2016-04-11 21:06:32 +02:00
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
/// Parity implementation.
|
2020-09-14 16:08:57 +02:00
|
|
|
pub struct ParityClient<C, M>
|
|
|
|
where
|
|
|
|
C: PrometheusMetrics,
|
|
|
|
{
|
2017-05-28 14:40:36 +02:00
|
|
|
client: Arc<C>,
|
|
|
|
miner: Arc<M>,
|
2020-07-29 10:36:15 +02:00
|
|
|
sync: Arc<dyn SyncProvider>,
|
|
|
|
net: Arc<dyn ManageNetwork>,
|
2016-04-19 09:58:26 +02:00
|
|
|
logger: Arc<RotatingLogger>,
|
2016-04-21 19:19:42 +02:00
|
|
|
settings: Arc<NetworkSettings>,
|
2016-09-21 12:44:49 +02:00
|
|
|
signer: Option<Arc<SignerService>>,
|
2017-09-21 14:52:44 +02:00
|
|
|
ws_address: Option<Host>,
|
2020-07-29 10:36:15 +02:00
|
|
|
snapshot: Option<Arc<dyn SnapshotService>>,
|
2016-04-11 21:06:32 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 12:24:16 +02:00
|
|
|
impl<C, M> ParityClient<C, M>
|
2017-08-28 14:11:55 +02:00
|
|
|
where
|
2020-09-14 16:08:57 +02:00
|
|
|
C: BlockChainClient + PrometheusMetrics,
|
2016-11-06 12:51:53 +01:00
|
|
|
{
|
|
|
|
/// Creates new `ParityClient`.
|
2016-08-11 17:19:20 +02:00
|
|
|
pub fn new(
|
2017-08-28 14:11:55 +02:00
|
|
|
client: Arc<C>,
|
|
|
|
miner: Arc<M>,
|
2020-07-29 10:36:15 +02:00
|
|
|
sync: Arc<dyn SyncProvider>,
|
|
|
|
net: Arc<dyn ManageNetwork>,
|
2016-08-11 17:19:20 +02:00
|
|
|
logger: Arc<RotatingLogger>,
|
|
|
|
settings: Arc<NetworkSettings>,
|
2016-10-24 12:21:15 +02:00
|
|
|
signer: Option<Arc<SignerService>>,
|
2017-09-21 14:52:44 +02:00
|
|
|
ws_address: Option<Host>,
|
2020-07-29 10:36:15 +02:00
|
|
|
snapshot: Option<Arc<dyn SnapshotService>>,
|
2016-08-17 19:25:02 +02:00
|
|
|
) -> Self {
|
2016-11-06 12:51:53 +01:00
|
|
|
ParityClient {
|
2017-08-28 14:11:55 +02:00
|
|
|
client,
|
|
|
|
miner,
|
|
|
|
sync,
|
|
|
|
net,
|
|
|
|
logger,
|
|
|
|
settings,
|
|
|
|
signer,
|
|
|
|
ws_address,
|
2018-11-07 09:30:34 +01:00
|
|
|
snapshot,
|
2016-04-11 21:06:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-14 12:24:16 +02:00
|
|
|
impl<C, M, S> Parity for ParityClient<C, M>
|
2018-03-03 18:42:13 +01:00
|
|
|
where
|
|
|
|
S: StateInfo + 'static,
|
2018-04-13 17:34:27 +02:00
|
|
|
C: miner::BlockChainClient
|
|
|
|
+ BlockChainClient
|
2020-09-14 16:08:57 +02:00
|
|
|
+ PrometheusMetrics
|
2018-04-13 17:34:27 +02:00
|
|
|
+ StateClient<State = S>
|
|
|
|
+ Call<State = S>
|
|
|
|
+ 'static,
|
2018-03-03 18:42:13 +01:00
|
|
|
M: MinerService<State = S> + 'static,
|
2016-12-23 18:52:02 +01:00
|
|
|
{
|
2017-01-30 11:10:58 +01:00
|
|
|
type Metadata = Metadata;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn transactions_limit(&self) -> Result<usize> {
|
2018-04-13 17:34:27 +02:00
|
|
|
Ok(self.miner.queue_status().limits.max_count)
|
2016-04-18 23:13:38 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn min_gas_price(&self) -> Result<U256> {
|
2019-03-22 12:01:11 +01:00
|
|
|
Ok(self.miner.queue_status().options.minimal_gas_price)
|
2016-04-13 00:04:40 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn extra_data(&self) -> Result<Bytes> {
|
2018-04-13 17:34:27 +02:00
|
|
|
Ok(Bytes::new(self.miner.authoring_params().extra_data))
|
2016-04-11 21:06:32 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn gas_floor_target(&self) -> Result<U256> {
|
2019-03-22 12:01:11 +01:00
|
|
|
Ok(self.miner.authoring_params().gas_range_target.0)
|
2016-04-11 21:06:32 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn gas_ceil_target(&self) -> Result<U256> {
|
2019-03-22 12:01:11 +01:00
|
|
|
Ok(self.miner.authoring_params().gas_range_target.1)
|
2016-06-23 14:29:16 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn dev_logs(&self) -> Result<Vec<String>> {
|
2019-01-08 15:07:20 +01:00
|
|
|
warn!("This method is deprecated and will be removed in future. See PR #10102");
|
2016-04-19 09:58:26 +02:00
|
|
|
let logs = self.logger.logs();
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(logs.as_slice().to_owned())
|
2016-04-19 09:58:26 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn dev_logs_levels(&self) -> Result<String> {
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(self.logger.levels().to_owned())
|
2016-04-19 09:58:26 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn net_chain(&self) -> Result<String> {
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(self.settings.chain.clone())
|
2016-04-21 19:19:42 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn chain(&self) -> Result<String> {
|
2017-05-28 14:40:36 +02:00
|
|
|
Ok(self.client.spec_name())
|
2017-03-13 12:10:53 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn net_peers(&self) -> Result<Peers> {
|
2017-05-28 14:40:36 +02:00
|
|
|
let sync_status = self.sync.status();
|
2018-06-01 09:49:46 +02:00
|
|
|
let num_peers_range = self.net.num_peers_range();
|
2019-01-22 09:51:40 +01:00
|
|
|
debug_assert!(num_peers_range.end() >= num_peers_range.start());
|
2017-05-28 14:40:36 +02:00
|
|
|
let peers = self.sync.peers().into_iter().map(Into::into).collect();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(Peers {
|
2016-08-11 17:19:20 +02:00
|
|
|
active: sync_status.num_active_peers,
|
|
|
|
connected: sync_status.num_peers,
|
2019-01-22 09:51:40 +01:00
|
|
|
max: sync_status.current_max_peers(*num_peers_range.start(), *num_peers_range.end()),
|
2019-03-22 12:01:11 +01:00
|
|
|
peers,
|
2016-10-04 19:05:46 +02:00
|
|
|
})
|
2016-04-21 19:19:42 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn net_port(&self) -> Result<u16> {
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(self.settings.network_port)
|
2016-04-21 19:19:42 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn node_name(&self) -> Result<String> {
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(self.settings.name.clone())
|
2016-04-21 19:19:42 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn registry_address(&self) -> Result<Option<H160>> {
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(self
|
2017-05-28 14:40:36 +02:00
|
|
|
.client
|
2016-10-04 19:05:46 +02:00
|
|
|
.additional_params()
|
|
|
|
.get("registrar")
|
|
|
|
.and_then(|s| Address::from_str(s).ok()))
|
2016-08-17 19:25:02 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn rpc_settings(&self) -> Result<RpcSettings> {
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(RpcSettings {
|
|
|
|
enabled: self.settings.rpc_enabled,
|
|
|
|
interface: self.settings.rpc_interface.clone(),
|
|
|
|
port: self.settings.rpc_port as u64,
|
|
|
|
})
|
2016-04-21 19:19:42 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn default_extra_data(&self) -> Result<Bytes> {
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(Bytes::new(version_data()))
|
2016-06-16 12:44:08 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn gas_price_histogram(&self) -> BoxFuture<Histogram> {
|
2017-10-05 12:35:01 +02:00
|
|
|
Box::new(future::done(
|
|
|
|
self.client
|
2017-02-17 21:38:43 +01:00
|
|
|
.gas_price_corpus(100)
|
|
|
|
.histogram(10)
|
|
|
|
.ok_or_else(errors::not_enough_data)
|
|
|
|
.map(Into::into),
|
2017-10-05 12:35:01 +02:00
|
|
|
))
|
2016-05-02 16:12:01 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn unsigned_transactions_count(&self) -> Result<usize> {
|
2016-09-21 12:44:49 +02:00
|
|
|
match self.signer {
|
2016-08-08 17:25:15 +02:00
|
|
|
None => Err(errors::signer_disabled()),
|
2016-10-04 19:05:46 +02:00
|
|
|
Some(ref signer) => Ok(signer.len()),
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-04 19:05:46 +02:00
|
|
|
fn generate_secret_phrase(&self) -> Result<String> {
|
|
|
|
Ok(random_phrase(12))
|
2016-06-21 14:55:25 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn phrase_to_address(&self, phrase: String) -> Result<H160> {
|
2016-10-04 19:05:46 +02:00
|
|
|
Ok(Brain::new(phrase)
|
2017-11-14 11:38:17 +01:00
|
|
|
.generate()
|
2019-03-22 12:01:11 +01:00
|
|
|
.expect("Brain::generate always returns Ok; qed")
|
|
|
|
.address())
|
2016-08-10 21:23:17 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-05 14:31:19 +01:00
|
|
|
fn list_accounts(
|
|
|
|
&self,
|
|
|
|
count: u64,
|
|
|
|
after: Option<H160>,
|
|
|
|
block_number: Option<BlockNumber>,
|
|
|
|
) -> Result<Option<Vec<H160>>> {
|
2018-03-03 18:42:13 +01:00
|
|
|
let number = match block_number.unwrap_or_default() {
|
|
|
|
BlockNumber::Pending => {
|
|
|
|
warn!("BlockNumber::Pending is unsupported");
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
num => block_number_to_id(num),
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-05-28 14:40:36 +02:00
|
|
|
Ok(self
|
|
|
|
.client
|
2018-03-03 18:42:13 +01:00
|
|
|
.list_accounts(number, after.map(Into::into).as_ref(), count)
|
2016-10-04 19:05:46 +02:00
|
|
|
.map(|a| a.into_iter().map(Into::into).collect()))
|
2016-10-03 11:13:10 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-05 14:31:19 +01:00
|
|
|
fn list_storage_keys(
|
|
|
|
&self,
|
|
|
|
address: H160,
|
|
|
|
count: u64,
|
|
|
|
after: Option<H256>,
|
|
|
|
block_number: Option<BlockNumber>,
|
|
|
|
) -> Result<Option<Vec<H256>>> {
|
2018-03-03 18:42:13 +01:00
|
|
|
let number = match block_number.unwrap_or_default() {
|
|
|
|
BlockNumber::Pending => {
|
|
|
|
warn!("BlockNumber::Pending is unsupported");
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
num => block_number_to_id(num),
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-05-28 14:40:36 +02:00
|
|
|
Ok(self
|
|
|
|
.client
|
2019-03-22 12:01:11 +01:00
|
|
|
.list_storage(number, &address, after.map(Into::into).as_ref(), count)
|
2016-11-27 11:11:56 +01:00
|
|
|
.map(|a| a.into_iter().map(Into::into).collect()))
|
2016-10-03 11:13:10 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn encrypt_message(&self, key: H512, phrase: Bytes) -> Result<Bytes> {
|
2019-03-22 12:01:11 +01:00
|
|
|
ecies::encrypt(&key, &DEFAULT_MAC, &phrase.0)
|
2017-07-04 17:01:06 +02:00
|
|
|
.map_err(errors::encryption)
|
2016-10-04 19:05:46 +02:00
|
|
|
.map(Into::into)
|
2016-09-22 14:48:22 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-05 14:31:19 +01:00
|
|
|
fn pending_transactions(&self, limit: Option<usize>) -> Result<Vec<Transaction>> {
|
2018-06-12 08:22:54 +02:00
|
|
|
let ready_transactions = self.miner.ready_transactions(
|
|
|
|
&*self.client,
|
|
|
|
limit.unwrap_or_else(usize::max_value),
|
|
|
|
miner::PendingOrdering::Priority,
|
|
|
|
);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
Ok(ready_transactions
|
2018-06-26 09:03:38 +02:00
|
|
|
.into_iter()
|
2018-08-01 13:17:04 +02:00
|
|
|
.map(|t| Transaction::from_pending(t.pending().clone()))
|
2018-06-26 09:03:38 +02:00
|
|
|
.collect())
|
2016-09-23 18:13:03 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
fn all_transactions(&self) -> Result<Vec<Transaction>> {
|
|
|
|
let all_transactions = self.miner.queued_transactions();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
Ok(all_transactions
|
2018-06-26 09:03:38 +02:00
|
|
|
.into_iter()
|
2018-08-01 13:17:04 +02:00
|
|
|
.map(|t| Transaction::from_pending(t.pending().clone()))
|
2018-06-26 09:03:38 +02:00
|
|
|
.collect())
|
2018-04-13 17:34:27 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-10-27 09:38:35 +02:00
|
|
|
fn all_transaction_hashes(&self) -> Result<Vec<H256>> {
|
2019-03-22 12:01:11 +01:00
|
|
|
Ok(self.miner.queued_transaction_hashes())
|
2018-10-27 09:38:35 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
fn future_transactions(&self) -> Result<Vec<Transaction>> {
|
|
|
|
Err(errors::deprecated("Use `parity_allTransaction` instead."))
|
2016-12-15 18:19:19 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn pending_transactions_stats(&self) -> Result<BTreeMap<H256, TransactionStats>> {
|
2017-05-28 14:40:36 +02:00
|
|
|
let stats = self.sync.transactions_stats();
|
2016-11-16 13:37:21 +01:00
|
|
|
Ok(stats
|
|
|
|
.into_iter()
|
2019-03-22 12:01:11 +01:00
|
|
|
.map(|(hash, stats)| (hash, stats.into()))
|
2018-06-26 09:03:38 +02:00
|
|
|
.collect())
|
2016-11-16 13:37:21 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn local_transactions(&self) -> Result<BTreeMap<H256, LocalTransactionStatus>> {
|
2017-05-28 14:40:36 +02:00
|
|
|
let transactions = self.miner.local_transactions();
|
2016-11-16 17:54:54 +01:00
|
|
|
Ok(transactions
|
2018-06-26 09:03:38 +02:00
|
|
|
.into_iter()
|
2019-03-22 12:01:11 +01:00
|
|
|
.map(|(hash, status)| (hash, LocalTransactionStatus::from(status)))
|
2018-06-26 09:03:38 +02:00
|
|
|
.collect())
|
2016-11-16 17:54:54 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn ws_url(&self) -> Result<String> {
|
2017-05-24 12:24:07 +02:00
|
|
|
helpers::to_url(&self.ws_address).ok_or_else(errors::ws_disabled)
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:06:14 +02:00
|
|
|
fn next_nonce(&self, address: H160) -> BoxFuture<U256> {
|
2017-11-14 11:38:17 +01:00
|
|
|
Box::new(future::ok(self.miner.next_nonce(&*self.client, &address)))
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn mode(&self) -> Result<String> {
|
2019-03-22 12:01:11 +01:00
|
|
|
Ok(self.client.mode().to_string())
|
2016-10-27 19:29:55 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn enode(&self) -> Result<String> {
|
2018-06-06 14:14:45 +02:00
|
|
|
self.sync.enode().ok_or_else(errors::network_disabled)
|
2016-10-31 16:58:35 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn chain_status(&self) -> Result<ChainStatus> {
|
2017-05-28 14:40:36 +02:00
|
|
|
let chain_info = self.client.chain_info();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-19 15:27:17 +01:00
|
|
|
let gap = chain_info
|
2017-05-28 14:40:36 +02:00
|
|
|
.ancient_block_number
|
|
|
|
.map(|x| U256::from(x + 1))
|
2017-11-14 11:38:17 +01:00
|
|
|
.and_then(|first| {
|
2017-05-28 14:40:36 +02:00
|
|
|
chain_info
|
2016-12-19 15:27:17 +01:00
|
|
|
.first_block_number
|
|
|
|
.map(|last| (first, U256::from(last)))
|
|
|
|
});
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-19 15:27:17 +01:00
|
|
|
Ok(ChainStatus { block_gap: gap })
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn node_kind(&self) -> Result<::v1::types::NodeKind> {
|
2019-03-22 12:01:11 +01:00
|
|
|
use v1::types::{Availability, Capability, NodeKind};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
Ok(NodeKind {
|
2017-03-27 17:30:19 +02:00
|
|
|
availability: Availability::Personal,
|
|
|
|
capability: Capability::Full,
|
2020-08-05 06:08:03 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-27 17:30:19 +02:00
|
|
|
fn block_header(&self, number: Option<BlockNumber>) -> BoxFuture<RichHeader> {
|
2018-06-01 16:49:55 +02:00
|
|
|
const EXTRA_INFO_PROOF: &str = "Object exists in blockchain (fetched earlier), extra_info is always available if object exists; qed";
|
2017-03-27 17:30:19 +02:00
|
|
|
let number = number.unwrap_or_default();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-05 14:31:19 +01:00
|
|
|
let (header, extra) = if number == BlockNumber::Pending {
|
2018-03-03 18:42:13 +01:00
|
|
|
let info = self.client.chain_info();
|
|
|
|
let header = try_bf!(self
|
2020-08-05 06:08:03 +02:00
|
|
|
.miner
|
2018-03-03 18:42:13 +01:00
|
|
|
.pending_block_header(info.best_block_number)
|
|
|
|
.ok_or_else(errors::unknown_block));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
(header.encoded(), None)
|
|
|
|
} else {
|
|
|
|
let id = match number {
|
2018-09-25 19:06:14 +02:00
|
|
|
BlockNumber::Num(num) => BlockId::Number(num),
|
2018-03-03 18:42:13 +01:00
|
|
|
BlockNumber::Earliest => BlockId::Earliest,
|
|
|
|
BlockNumber::Latest => BlockId::Latest,
|
|
|
|
BlockNumber::Pending => unreachable!(), // Already covered
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-09-25 19:06:14 +02:00
|
|
|
let header = try_bf!(self
|
|
|
|
.client
|
|
|
|
.block_header(id)
|
|
|
|
.ok_or_else(errors::unknown_block));
|
2018-03-03 18:42:13 +01:00
|
|
|
let info = self.client.block_extra_info(id).expect(EXTRA_INFO_PROOF);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
(header, Some(info))
|
2017-04-03 11:37:07 +02:00
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-09-25 19:06:14 +02:00
|
|
|
Box::new(future::ok(RichHeader {
|
2017-10-05 12:35:01 +02:00
|
|
|
inner: header.into(),
|
2018-03-03 18:42:13 +01:00
|
|
|
extra_info: extra.unwrap_or_default(),
|
2020-08-05 06:08:03 +02:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
fn block_receipts(&self, number: Option<BlockNumber>) -> BoxFuture<Vec<Receipt>> {
|
2018-03-03 18:42:13 +01:00
|
|
|
let number = number.unwrap_or_default();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-05 14:31:19 +01:00
|
|
|
let id = match number {
|
|
|
|
BlockNumber::Pending => {
|
2018-09-25 19:06:14 +02:00
|
|
|
let info = self.client.chain_info();
|
2019-02-05 14:31:19 +01:00
|
|
|
let receipts = try_bf!(self
|
2018-09-25 19:06:14 +02:00
|
|
|
.miner
|
|
|
|
.pending_receipts(info.best_block_number)
|
2018-11-18 00:06:34 +01:00
|
|
|
.ok_or_else(errors::unknown_block));
|
2018-09-25 19:06:14 +02:00
|
|
|
return Box::new(future::ok(receipts.into_iter().map(Into::into).collect()));
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2018-03-03 18:42:13 +01:00
|
|
|
BlockNumber::Num(num) => BlockId::Number(num),
|
|
|
|
BlockNumber::Earliest => BlockId::Earliest,
|
|
|
|
BlockNumber::Latest => BlockId::Latest,
|
2020-08-05 06:08:03 +02:00
|
|
|
};
|
2018-09-25 19:06:14 +02:00
|
|
|
let receipts = try_bf!(self
|
2020-08-05 06:08:03 +02:00
|
|
|
.client
|
2018-11-18 00:06:34 +01:00
|
|
|
.localized_block_receipts(id)
|
2018-09-25 19:06:14 +02:00
|
|
|
.ok_or_else(errors::unknown_block));
|
|
|
|
Box::new(future::ok(receipts.into_iter().map(Into::into).collect()))
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn call(&self, requests: Vec<CallRequest>, num: Option<BlockNumber>) -> Result<Vec<Bytes>> {
|
2017-10-05 12:35:01 +02:00
|
|
|
let requests = requests
|
2017-11-14 11:38:17 +01:00
|
|
|
.into_iter()
|
2019-02-05 14:31:19 +01:00
|
|
|
.map(|request| Ok((fake_sign::sign_call(request.into())?, Default::default())))
|
|
|
|
.collect::<Result<Vec<_>>>()?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-09-25 19:06:14 +02:00
|
|
|
let num = num.unwrap_or_default();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-05 14:31:19 +01:00
|
|
|
let (mut state, header) = if num == BlockNumber::Pending {
|
|
|
|
let info = self.client.chain_info();
|
2017-10-05 12:35:01 +02:00
|
|
|
let state = self
|
2020-08-05 06:08:03 +02:00
|
|
|
.miner
|
2017-08-04 15:58:14 +02:00
|
|
|
.pending_state(info.best_block_number)
|
2018-08-07 14:52:23 +02:00
|
|
|
.ok_or_else(errors::state_pruned)?;
|
2018-03-03 18:42:13 +01:00
|
|
|
let header = self
|
2020-08-05 06:08:03 +02:00
|
|
|
.miner
|
2018-03-03 18:42:13 +01:00
|
|
|
.pending_block_header(info.best_block_number)
|
|
|
|
.ok_or_else(errors::state_pruned)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
(state, header)
|
2020-08-05 06:08:03 +02:00
|
|
|
} else {
|
2018-03-03 18:42:13 +01:00
|
|
|
let id = match num {
|
|
|
|
BlockNumber::Num(num) => BlockId::Number(num),
|
|
|
|
BlockNumber::Earliest => BlockId::Earliest,
|
|
|
|
BlockNumber::Latest => BlockId::Latest,
|
|
|
|
BlockNumber::Pending => unreachable!(), // Already covered
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-09-25 19:06:14 +02:00
|
|
|
let state = self.client.state_at(id).ok_or_else(errors::state_pruned)?;
|
|
|
|
let header = self
|
|
|
|
.client
|
|
|
|
.block_header(id)
|
|
|
|
.ok_or_else(errors::state_pruned)?
|
|
|
|
.decode()
|
|
|
|
.map_err(errors::decode)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-09 12:05:56 +02:00
|
|
|
(state, header)
|
2018-03-03 18:42:13 +01:00
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
self.client
|
|
|
|
.call_many(&requests, &mut state, &header)
|
2017-08-04 15:58:14 +02:00
|
|
|
.map(|res| res.into_iter().map(|res| res.output.into()).collect())
|
2017-10-05 12:35:01 +02:00
|
|
|
.map_err(errors::call)
|
2017-08-04 15:58:14 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-10-02 21:02:48 +02:00
|
|
|
fn submit_work_detail(&self, nonce: H64, pow_hash: H256, mix_hash: H256) -> Result<H256> {
|
|
|
|
helpers::submit_work_detail(&self.client, &self.miner, nonce, pow_hash, mix_hash)
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-11-07 09:30:34 +01:00
|
|
|
fn status(&self) -> Result<()> {
|
|
|
|
let has_peers = self.settings.is_dev_chain || self.sync.status().num_peers > 0;
|
2020-09-14 16:08:57 +02:00
|
|
|
let is_warping = match self.snapshot.as_ref().map(|s| s.restoration_status()) {
|
2018-11-07 09:30:34 +01:00
|
|
|
Some(RestorationStatus::Ongoing { .. }) => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
let is_not_syncing = !is_warping
|
|
|
|
&& !is_major_importing(Some(self.sync.status().state), self.client.queue_info());
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-11-07 09:30:34 +01:00
|
|
|
if has_peers && is_not_syncing {
|
|
|
|
Ok(())
|
2020-08-05 06:08:03 +02:00
|
|
|
} else {
|
2018-11-07 09:30:34 +01:00
|
|
|
Err(errors::status_error(has_peers))
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 20:40:25 +01:00
|
|
|
fn verify_signature(
|
|
|
|
&self,
|
2018-11-28 12:18:43 +01:00
|
|
|
is_prefixed: bool,
|
|
|
|
message: Bytes,
|
|
|
|
r: H256,
|
|
|
|
s: H256,
|
|
|
|
v: U64,
|
|
|
|
) -> Result<RecoveredAccount> {
|
|
|
|
verify_signature(
|
|
|
|
is_prefixed,
|
|
|
|
message,
|
|
|
|
r,
|
|
|
|
s,
|
|
|
|
v,
|
|
|
|
self.client.signing_chain_id(),
|
|
|
|
)
|
|
|
|
}
|
2016-04-11 21:06:32 +02:00
|
|
|
}
|