2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-07-14 20:40:28 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-07-14 20:40:28 +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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-07-14 20:40:28 +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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-07-14 20:40:28 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
use std::{io, sync::Arc};
|
2017-07-14 20:40:28 +02:00
|
|
|
|
|
|
|
use parity_rpc::Metadata;
|
2020-08-05 06:08:03 +02:00
|
|
|
use parity_whisper::{
|
|
|
|
message::Message,
|
|
|
|
net::{self as whisper_net, Network as WhisperNetwork},
|
|
|
|
rpc::{FilterManager, PoolHandle, WhisperClient},
|
|
|
|
};
|
|
|
|
use sync::{AttachedProtocol, ManageNetwork};
|
2017-07-14 20:40:28 +02:00
|
|
|
|
|
|
|
/// Whisper config.
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub struct Config {
|
2020-08-05 06:08:03 +02:00
|
|
|
pub enabled: bool,
|
|
|
|
pub target_message_pool_size: usize,
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn default() -> Self {
|
|
|
|
Config {
|
|
|
|
enabled: false,
|
|
|
|
target_message_pool_size: 10 * 1024 * 1024,
|
|
|
|
}
|
|
|
|
}
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-10 18:02:14 +02:00
|
|
|
/// Standard pool handle.
|
|
|
|
pub struct NetPoolHandle {
|
2020-08-05 06:08:03 +02:00
|
|
|
/// Pool handle.
|
|
|
|
handle: Arc<WhisperNetwork<Arc<FilterManager>>>,
|
|
|
|
/// Network manager.
|
2020-07-29 10:36:15 +02:00
|
|
|
net: Arc<dyn ManageNetwork>,
|
2017-09-10 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PoolHandle for NetPoolHandle {
|
2020-08-05 06:08:03 +02:00
|
|
|
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()
|
|
|
|
}
|
2017-09-10 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2017-07-14 20:40:28 +02:00
|
|
|
/// Factory for standard whisper RPC.
|
|
|
|
pub struct RpcFactory {
|
2020-08-05 06:08:03 +02:00
|
|
|
net: Arc<WhisperNetwork<Arc<FilterManager>>>,
|
|
|
|
manager: Arc<FilterManager>,
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RpcFactory {
|
2020-07-29 10:36:15 +02:00
|
|
|
pub fn make_handler(
|
|
|
|
&self,
|
|
|
|
net: Arc<dyn ManageNetwork>,
|
|
|
|
) -> WhisperClient<NetPoolHandle, Metadata> {
|
2020-08-05 06:08:03 +02:00
|
|
|
let handle = NetPoolHandle {
|
|
|
|
handle: self.net.clone(),
|
|
|
|
net: net,
|
|
|
|
};
|
|
|
|
WhisperClient::new(handle, self.manager.clone())
|
|
|
|
}
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets up whisper protocol and RPC handler.
|
|
|
|
///
|
|
|
|
/// Will target the given pool size.
|
|
|
|
#[cfg(not(feature = "ipc"))]
|
2020-08-05 06:08:03 +02:00
|
|
|
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<_>,
|
|
|
|
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),
|
|
|
|
versions: whisper_net::SUPPORTED_VERSIONS,
|
|
|
|
protocol_id: whisper_net::PARITY_PROTOCOL_ID,
|
|
|
|
});
|
|
|
|
|
|
|
|
let factory = RpcFactory {
|
|
|
|
net: net,
|
|
|
|
manager: manager,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Some(factory))
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make it possible to attach generic protocols in IPC.
|
|
|
|
#[cfg(feature = "ipc")]
|
2020-08-05 06:08:03 +02:00
|
|
|
pub fn setup(
|
|
|
|
_target_pool_size: usize,
|
|
|
|
_protos: &mut Vec<AttachedProtocol>,
|
|
|
|
) -> io::Result<Option<RpcFactory>> {
|
|
|
|
Ok(None)
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|