* `duration_ns: u64 -> duration: Duration` (#8457) * duration_ns: u64 -> duration: Duration * format on millis {:.2} -> {} * Keep all enacted blocks notify in order (#8524) * Keep all enacted blocks notify in order * Collect is unnecessary * Update ChainNotify to use ChainRouteType * Fix all ethcore fn defs * Wrap the type within ChainRoute * Fix private-tx and sync api * Fix secret_store API * Fix updater API * Fix rpc api * Fix informant api * Eagerly cache enacted/retracted and remove contain_enacted/retracted * Fix indent * tests: should use full expr form for struct constructor * Use into_enacted_retracted to further avoid copy * typo: not a function * rpc/tests: ChainRoute -> ChainRoute::new * Handle removed logs in filter changes and add geth compatibility field (#8796) * Add removed geth compatibility field in log * Fix mocked tests * Add field block hash in PollFilter * Store last block hash info for log filters * Implement canon route * Use canon logs for fetching reorg logs Light client removed logs fetching is disabled. It looks expensive. * Make sure removed flag is set * Address grumbles * Fixed AuthorityRound deadlock on shutdown, closes #8088 (#8803) * CI: Fix docker tags (#8822) * scripts: enable docker builds for beta and stable * scripts: docker latest should be beta not master * scripts: docker latest is master * ethcore: fix ancient block error msg handling (#8832) * Disable parallel verification and skip verifiying already imported txs. (#8834) * Reject transactions that are already in pool without verifying them. * Avoid verifying already imported transactions. * Fix concurrent access to signer queue (#8854) * Fix concurrent access to signer queue * Put request back to the queue if confirmation failed * typo: fix docs and rename functions to be more specific `request_notify` does not need to be public, and it's renamed to `notify_result`. `notify` is renamed to `notify_message`. * Change trace info "Transaction" -> "Request" * Don't allocate in expect_valid_rlp unless necessary (#8867) * don't allocate via format! in case there's no error * fix test? * fixed ipc leak, closes #8774 (#8876) * Add new ovh bootnodes and fix port for foundation bootnode 3.2 (#8886) * Add new ovh bootnodes and fix port for foundation bootnode 3.2 * Remove old bootnodes. * Remove duplicate 1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082 * Block 0 is valid in queries (#8891) Early exit for block nr 0 leads to spurious error about pruning: `…your node is running with state pruning…`. Fixes #7547, #8762 * Add ETC Cooperative-run load balanced parity node (#8892) * Minor fix in chain supplier and light provider (#8906) * fix chain supplier increment * fix light provider block_headers * Check whether we need resealing in miner and unwrap has_account in account_provider (#8853) * Remove unused Result wrap in has_account * Check whether we need to reseal for external transactions * Fix reference to has_account interface * typo: missing ) * Refactor duplicates to prepare_and_update_sealing * Fix build * Allow disabling local-by-default for transactions with new config entry (#8882) * Add tx_queue_allow_unknown_local config option - Previous commit messages: dispatcher checks if we have the sender account Add `tx_queue_allow_unknown_local` to MinerOptions Add `tx_queue_allow_unknown_local` to config fix order in MinerOptions to match Configuration add cli flag for tx_queue_allow_unknown_local Update refs to `tx_queue_allow_unknown_local` Add tx_queue_allow_unknown_local to config test revert changes to dispatcher Move tx_queue_allow_unknown_local to `import_own_transaction` Fix var name if statement should return the values derp de derp derp derp semicolons Reset dispatch file to how it was before fix compile issues + change from FLAG to ARG add test and use `into` import MinerOptions, clone the secret Fix tests? Compiler/linter issues fixed Fix linter msg - case of constants IT LIVES refactor to omit yucky explict return update comments Fix based on diff AccountProvider.has_account method * Refactor flag name + don't change import_own_tx behaviour fix arg name Note: force commit to try and get gitlab tests working again 😠 * Add fn to TestMinerService * Avoid race condition from trusted sources - refactor the miner tests a bit to cut down on code reuse - add `trusted` param to dispatch_transaction and import_claimed_local_transaction Add param to `import_claimed_local_transaction` Fix fn sig in tests
315 lines
9.3 KiB
Rust
315 lines
9.3 KiB
Rust
// 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/>.
|
|
|
|
//! Eth PUB-SUB rpc implementation.
|
|
|
|
use std::sync::{Arc, Weak};
|
|
use std::collections::BTreeMap;
|
|
use std::time::Duration;
|
|
|
|
use jsonrpc_core::{BoxFuture, Result, Error};
|
|
use jsonrpc_core::futures::{self, Future, IntoFuture};
|
|
use jsonrpc_macros::Trailing;
|
|
use jsonrpc_macros::pubsub::{Sink, Subscriber};
|
|
use jsonrpc_pubsub::SubscriptionId;
|
|
|
|
use v1::helpers::{errors, limit_logs, Subscribers};
|
|
use v1::helpers::light_fetch::LightFetch;
|
|
use v1::metadata::Metadata;
|
|
use v1::traits::EthPubSub;
|
|
use v1::types::{pubsub, RichHeader, Log};
|
|
|
|
use ethcore::encoded;
|
|
use ethcore::filter::Filter as EthFilter;
|
|
use ethcore::client::{BlockChainClient, ChainNotify, ChainRoute, ChainRouteType, BlockId};
|
|
use sync::LightSync;
|
|
use light::cache::Cache;
|
|
use light::on_demand::OnDemand;
|
|
use light::client::{LightChainClient, LightChainNotify};
|
|
use parity_reactor::Remote;
|
|
use ethereum_types::H256;
|
|
use bytes::Bytes;
|
|
use parking_lot::{RwLock, Mutex};
|
|
|
|
type Client = Sink<pubsub::Result>;
|
|
|
|
/// Eth PubSub implementation.
|
|
pub struct EthPubSubClient<C> {
|
|
handler: Arc<ChainNotificationHandler<C>>,
|
|
heads_subscribers: Arc<RwLock<Subscribers<Client>>>,
|
|
logs_subscribers: Arc<RwLock<Subscribers<(Client, EthFilter)>>>,
|
|
transactions_subscribers: Arc<RwLock<Subscribers<Client>>>,
|
|
}
|
|
|
|
impl<C> EthPubSubClient<C> {
|
|
/// Creates new `EthPubSubClient`.
|
|
pub fn new(client: Arc<C>, remote: Remote) -> Self {
|
|
let heads_subscribers = Arc::new(RwLock::new(Subscribers::default()));
|
|
let logs_subscribers = Arc::new(RwLock::new(Subscribers::default()));
|
|
let transactions_subscribers = Arc::new(RwLock::new(Subscribers::default()));
|
|
|
|
EthPubSubClient {
|
|
handler: Arc::new(ChainNotificationHandler {
|
|
client,
|
|
remote,
|
|
heads_subscribers: heads_subscribers.clone(),
|
|
logs_subscribers: logs_subscribers.clone(),
|
|
transactions_subscribers: transactions_subscribers.clone(),
|
|
}),
|
|
heads_subscribers,
|
|
logs_subscribers,
|
|
transactions_subscribers,
|
|
}
|
|
}
|
|
|
|
/// Creates new `EthPubSubCient` with deterministic subscription ids.
|
|
#[cfg(test)]
|
|
pub fn new_test(client: Arc<C>, remote: Remote) -> Self {
|
|
let client = Self::new(client, remote);
|
|
*client.heads_subscribers.write() = Subscribers::new_test();
|
|
*client.logs_subscribers.write() = Subscribers::new_test();
|
|
*client.transactions_subscribers.write() = Subscribers::new_test();
|
|
client
|
|
}
|
|
|
|
/// Returns a chain notification handler.
|
|
pub fn handler(&self) -> Weak<ChainNotificationHandler<C>> {
|
|
Arc::downgrade(&self.handler)
|
|
}
|
|
}
|
|
|
|
impl EthPubSubClient<LightFetch> {
|
|
/// Creates a new `EthPubSubClient` for `LightClient`.
|
|
pub fn light(
|
|
client: Arc<LightChainClient>,
|
|
on_demand: Arc<OnDemand>,
|
|
sync: Arc<LightSync>,
|
|
cache: Arc<Mutex<Cache>>,
|
|
remote: Remote,
|
|
gas_price_percentile: usize,
|
|
) -> Self {
|
|
let fetch = LightFetch {
|
|
client,
|
|
on_demand,
|
|
sync,
|
|
cache,
|
|
gas_price_percentile,
|
|
};
|
|
EthPubSubClient::new(Arc::new(fetch), remote)
|
|
}
|
|
}
|
|
|
|
/// PubSub Notification handler.
|
|
pub struct ChainNotificationHandler<C> {
|
|
client: Arc<C>,
|
|
remote: Remote,
|
|
heads_subscribers: Arc<RwLock<Subscribers<Client>>>,
|
|
logs_subscribers: Arc<RwLock<Subscribers<(Client, EthFilter)>>>,
|
|
transactions_subscribers: Arc<RwLock<Subscribers<Client>>>,
|
|
}
|
|
|
|
impl<C> ChainNotificationHandler<C> {
|
|
fn notify(remote: &Remote, subscriber: &Client, result: pubsub::Result) {
|
|
remote.spawn(subscriber
|
|
.notify(Ok(result))
|
|
.map(|_| ())
|
|
.map_err(|e| warn!(target: "rpc", "Unable to send notification: {}", e))
|
|
);
|
|
}
|
|
|
|
fn notify_heads(&self, headers: &[(encoded::Header, BTreeMap<String, String>)]) {
|
|
for subscriber in self.heads_subscribers.read().values() {
|
|
for &(ref header, ref extra_info) in headers {
|
|
Self::notify(&self.remote, subscriber, pubsub::Result::Header(RichHeader {
|
|
inner: header.into(),
|
|
extra_info: extra_info.clone(),
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
|
|
fn notify_logs<F, T, Ex>(&self, enacted: &[(H256, Ex)], logs: F) where
|
|
F: Fn(EthFilter, &Ex) -> T,
|
|
Ex: Send,
|
|
T: IntoFuture<Item = Vec<Log>, Error = Error>,
|
|
T::Future: Send + 'static,
|
|
{
|
|
for &(ref subscriber, ref filter) in self.logs_subscribers.read().values() {
|
|
let logs = futures::future::join_all(enacted
|
|
.iter()
|
|
.map(|&(hash, ref ex)| {
|
|
let mut filter = filter.clone();
|
|
filter.from_block = BlockId::Hash(hash);
|
|
filter.to_block = filter.from_block.clone();
|
|
logs(filter, ex).into_future()
|
|
})
|
|
.collect::<Vec<_>>()
|
|
);
|
|
let limit = filter.limit;
|
|
let remote = self.remote.clone();
|
|
let subscriber = subscriber.clone();
|
|
self.remote.spawn(logs
|
|
.map(move |logs| {
|
|
let logs = logs.into_iter().flat_map(|log| log).collect();
|
|
|
|
for log in limit_logs(logs, limit) {
|
|
Self::notify(&remote, &subscriber, pubsub::Result::Log(log))
|
|
}
|
|
})
|
|
.map_err(|e| warn!("Unable to fetch latest logs: {:?}", e))
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Notify all subscribers about new transaction hashes.
|
|
pub fn new_transactions(&self, hashes: &[H256]) {
|
|
for subscriber in self.transactions_subscribers.read().values() {
|
|
for hash in hashes {
|
|
Self::notify(&self.remote, subscriber, pubsub::Result::TransactionHash((*hash).into()));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A light client wrapper struct.
|
|
pub trait LightClient: Send + Sync {
|
|
/// Get a recent block header.
|
|
fn block_header(&self, id: BlockId) -> Option<encoded::Header>;
|
|
|
|
/// Fetch logs.
|
|
fn logs(&self, filter: EthFilter) -> BoxFuture<Vec<Log>>;
|
|
}
|
|
|
|
impl LightClient for LightFetch {
|
|
fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
|
self.client.block_header(id)
|
|
}
|
|
|
|
fn logs(&self, filter: EthFilter) -> BoxFuture<Vec<Log>> {
|
|
LightFetch::logs(self, filter)
|
|
}
|
|
}
|
|
|
|
impl<C: LightClient> LightChainNotify for ChainNotificationHandler<C> {
|
|
fn new_headers(
|
|
&self,
|
|
enacted: &[H256],
|
|
) {
|
|
let headers = enacted
|
|
.iter()
|
|
.filter_map(|hash| self.client.block_header(BlockId::Hash(*hash)))
|
|
.map(|header| (header, Default::default()))
|
|
.collect::<Vec<_>>();
|
|
|
|
self.notify_heads(&headers);
|
|
self.notify_logs(&enacted.iter().map(|h| (*h, ())).collect::<Vec<_>>(), |filter, _| self.client.logs(filter))
|
|
}
|
|
}
|
|
|
|
impl<C: BlockChainClient> ChainNotify for ChainNotificationHandler<C> {
|
|
fn new_blocks(
|
|
&self,
|
|
_imported: Vec<H256>,
|
|
_invalid: Vec<H256>,
|
|
route: ChainRoute,
|
|
_sealed: Vec<H256>,
|
|
// Block bytes.
|
|
_proposed: Vec<Bytes>,
|
|
_duration: Duration,
|
|
) {
|
|
const EXTRA_INFO_PROOF: &'static str = "Object exists in in blockchain (fetched earlier), extra_info is always available if object exists; qed";
|
|
let headers = route.route()
|
|
.iter()
|
|
.filter_map(|&(hash, ref typ)| {
|
|
match typ {
|
|
&ChainRouteType::Retracted => None,
|
|
&ChainRouteType::Enacted => self.client.block_header(BlockId::Hash(hash))
|
|
}
|
|
})
|
|
.map(|header| {
|
|
let hash = header.hash();
|
|
(header, self.client.block_extra_info(BlockId::Hash(hash)).expect(EXTRA_INFO_PROOF))
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
// Headers
|
|
self.notify_heads(&headers);
|
|
|
|
// We notify logs enacting and retracting as the order in route.
|
|
self.notify_logs(route.route(), |filter, ex| {
|
|
match ex {
|
|
&ChainRouteType::Enacted =>
|
|
Ok(self.client.logs(filter).into_iter().map(Into::into).collect()),
|
|
&ChainRouteType::Retracted =>
|
|
Ok(self.client.logs(filter).into_iter().map(Into::into).map(|mut log: Log| {
|
|
log.log_type = "removed".into();
|
|
log.removed = true;
|
|
log
|
|
}).collect()),
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
impl<C: Send + Sync + 'static> EthPubSub for EthPubSubClient<C> {
|
|
type Metadata = Metadata;
|
|
|
|
fn subscribe(
|
|
&self,
|
|
_meta: Metadata,
|
|
subscriber: Subscriber<pubsub::Result>,
|
|
kind: pubsub::Kind,
|
|
params: Trailing<pubsub::Params>,
|
|
) {
|
|
let error = match (kind, params.into()) {
|
|
(pubsub::Kind::NewHeads, None) => {
|
|
self.heads_subscribers.write().push(subscriber);
|
|
return;
|
|
},
|
|
(pubsub::Kind::NewHeads, _) => {
|
|
errors::invalid_params("newHeads", "Expected no parameters.")
|
|
},
|
|
(pubsub::Kind::Logs, Some(pubsub::Params::Logs(filter))) => {
|
|
self.logs_subscribers.write().push(subscriber, filter.into());
|
|
return;
|
|
},
|
|
(pubsub::Kind::Logs, _) => {
|
|
errors::invalid_params("logs", "Expected a filter object.")
|
|
},
|
|
(pubsub::Kind::NewPendingTransactions, None) => {
|
|
self.transactions_subscribers.write().push(subscriber);
|
|
return;
|
|
},
|
|
(pubsub::Kind::NewPendingTransactions, _) => {
|
|
errors::invalid_params("newPendingTransactions", "Expected no parameters.")
|
|
},
|
|
_ => {
|
|
errors::unimplemented(None)
|
|
},
|
|
};
|
|
|
|
let _ = subscriber.reject(error);
|
|
}
|
|
|
|
fn unsubscribe(&self, id: SubscriptionId) -> Result<bool> {
|
|
let res = self.heads_subscribers.write().remove(&id).is_some();
|
|
let res2 = self.logs_subscribers.write().remove(&id).is_some();
|
|
let res3 = self.transactions_subscribers.write().remove(&id).is_some();
|
|
|
|
Ok(res || res2 || res3)
|
|
}
|
|
}
|