openethereum/parity/whisper.rs
Marek Kotewicz a257827f27 backports to beta (#7434)
* Merge pull request #7368 from paritytech/td-future-blocks

Wait for future blocks in AuRa

* Fix tracing failed calls.

* Problem: sending any Whisper message fails

The error is "PoW too low to compete with other messages"

This has been previously reported in #7144

Solution: prevent the move semantics

The source of the error is in PoolHandle.relay
implementation for NetPoolHandle.

Because of the move semantics, `res` variable is in fact
copied (as it implements Copy) into the closure and for
that reason, the returned result is always `false.

* Merge pull request #7433 from paritytech/td-strict-config

Strict config parsing

* Problem: AuRa's unsafeties around step duration (#7282)

Firstly, `Step.duration_remaining` casts it to u32, unnecesarily
limiting it to 2^32. While theoretically this is "good enough" (at 3
seconds steps it provides room for a little over 400 years), it is
still a lossy way to calculate the remaining time until the next step.

Secondly, step duration might be zero, triggering division by zero
in `Step.calibrate`

Solution: rework the code around the fact that duration is
typically in single digits and never grows, hence, it can be represented
by a much narrower range (u16) and this highlights the fact that
multiplying u64 by u16 will only result in an overflow in even further
future, at which point we should panic informatively (if anybody's
still around)

Similarly, panic when it is detected that incrementing the step
counter wrapped around on the overflow of usize.

As for the division by zero, prevent it by making zero an invalid
value for step duration. This will make AuRa log the constraint
mismatch and panic (after all, what purpose would zero step duration
serve? it makes no sense within the definition of the protocol,
as finality can only be achieved as per the specification
if messages are received within the step duration, which would violate
the speed of light and other physical laws in this case).

* Merge pull request #7437 from paritytech/a5-chains-expanse

Remove expanse chain

* Expanse Byzantium update w/ correct metropolis difficulty increment divisor (#7463)

* Byzantium Update for Expanse

Here the changes go. Hope I didnt miss anything.

* expip2 changes - update duration limit

* Fix missing EXPIP-2 fields

* Format numbers as hex

* Fix compilation errors

* Group expanse chain spec fields together

* Set metropolisDifficultyIncrementDivisor for Expanse

* Revert #7437

* Add Expanse block 900_000 hash checkpoint

* Advance AuRa step as far as we can and prevent invalid blocks. (#7451)

* Advance AuRa step as far as we can.

* Wait for future blocks.

* fixed panic when io is not available for export block, closes #7486 (#7495)

* Update Parity Mainnet Bootnodes (#7476)

* Update Parity Mainnet Bootnodes

* Replace the Azure HDD bootnodes with the new ones :)

* Use https connection (#7503)

Use https when connecting to etherscan.io API for price-info

* Expose default gas price percentile configuration in CLI (#7497)

* Expose gas price percentile.

* Fix light eth_call.

* fix gas_price in light client
2018-01-09 13:55:10 +01:00

117 lines
3.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/>.
use std::sync::Arc;
use std::io;
use ethsync::{AttachedProtocol, ManageNetwork};
use parity_rpc::Metadata;
use parity_whisper::message::Message;
use parity_whisper::net::{self as whisper_net, Network as WhisperNetwork};
use parity_whisper::rpc::{WhisperClient, PoolHandle, FilterManager};
/// Whisper config.
#[derive(Debug, PartialEq, Eq)]
pub struct Config {
pub enabled: bool,
pub target_message_pool_size: usize,
}
impl Default for Config {
fn default() -> Self {
Config {
enabled: false,
target_message_pool_size: 10 * 1024 * 1024,
}
}
}
/// Standard pool handle.
pub struct NetPoolHandle {
/// Pool handle.
handle: Arc<WhisperNetwork<Arc<FilterManager>>>,
/// Network manager.
net: Arc<ManageNetwork>,
}
impl PoolHandle for NetPoolHandle {
fn relay(&self, message: Message) -> bool {
let mut res = false;
let mut message = Some(message);
self.net.with_proto_context(whisper_net::PROTOCOL_ID, &mut |ctx| {
if let Some(message) = message.take() {
res = self.handle.post_message(message, ctx);
}
});
res
}
fn pool_status(&self) -> whisper_net::PoolStatus {
self.handle.pool_status()
}
}
/// Factory for standard whisper RPC.
pub struct RpcFactory {
net: Arc<WhisperNetwork<Arc<FilterManager>>>,
manager: Arc<FilterManager>,
}
impl RpcFactory {
pub fn make_handler(&self, net: Arc<ManageNetwork>) -> WhisperClient<NetPoolHandle, Metadata> {
let handle = NetPoolHandle { handle: self.net.clone(), net: net };
WhisperClient::new(handle, self.manager.clone())
}
}
/// Sets up whisper protocol and RPC handler.
///
/// Will target the given pool size.
#[cfg(not(feature = "ipc"))]
pub fn setup(target_pool_size: usize, protos: &mut Vec<AttachedProtocol>)
-> io::Result<Option<RpcFactory>>
{
let manager = Arc::new(FilterManager::new()?);
let net = Arc::new(WhisperNetwork::new(target_pool_size, manager.clone()));
protos.push(AttachedProtocol {
handler: net.clone() as Arc<_>,
packet_count: whisper_net::PACKET_COUNT,
versions: whisper_net::SUPPORTED_VERSIONS,
protocol_id: whisper_net::PROTOCOL_ID,
});
// parity-only extensions to whisper.
protos.push(AttachedProtocol {
handler: Arc::new(whisper_net::ParityExtensions),
packet_count: whisper_net::PACKET_COUNT,
versions: whisper_net::SUPPORTED_VERSIONS,
protocol_id: whisper_net::PARITY_PROTOCOL_ID,
});
let factory = RpcFactory { net: net, manager: manager };
Ok(Some(factory))
}
// TODO: make it possible to attach generic protocols in IPC.
#[cfg(feature = "ipc")]
pub fn setup(_target_pool_size: usize, _protos: &mut Vec<AttachedProtocol>)
-> io::Result<Option<RpcFactory>>
{
Ok(None)
}