Remove HostTrait altogether (#8681)
This commit is contained in:
parent
2060ea5de3
commit
3d76417353
@ -21,7 +21,7 @@
|
|||||||
use transaction::UnverifiedTransaction;
|
use transaction::UnverifiedTransaction;
|
||||||
|
|
||||||
use io::TimerToken;
|
use io::TimerToken;
|
||||||
use network::{HostInfo, NetworkProtocolHandler, NetworkContext, PeerId};
|
use network::{NetworkProtocolHandler, NetworkContext, PeerId};
|
||||||
use rlp::{RlpStream, Rlp};
|
use rlp::{RlpStream, Rlp};
|
||||||
use ethereum_types::{H256, U256};
|
use ethereum_types::{H256, U256};
|
||||||
use kvdb::DBValue;
|
use kvdb::DBValue;
|
||||||
@ -1082,7 +1082,7 @@ fn punish(peer: PeerId, io: &IoContext, e: Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl NetworkProtocolHandler for LightProtocol {
|
impl NetworkProtocolHandler for LightProtocol {
|
||||||
fn initialize(&self, io: &NetworkContext, _host_info: &HostInfo) {
|
fn initialize(&self, io: &NetworkContext) {
|
||||||
io.register_timer(TIMEOUT, TIMEOUT_INTERVAL)
|
io.register_timer(TIMEOUT, TIMEOUT_INTERVAL)
|
||||||
.expect("Error registering sync timer.");
|
.expect("Error registering sync timer.");
|
||||||
io.register_timer(TICK_TIMEOUT, TICK_TIMEOUT_INTERVAL)
|
io.register_timer(TICK_TIMEOUT, TICK_TIMEOUT_INTERVAL)
|
||||||
|
@ -21,7 +21,7 @@ use std::ops::Range;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use devp2p::NetworkService;
|
use devp2p::NetworkService;
|
||||||
use network::{NetworkProtocolHandler, NetworkContext, HostInfo, PeerId, ProtocolId,
|
use network::{NetworkProtocolHandler, NetworkContext, PeerId, ProtocolId,
|
||||||
NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, Error, ErrorKind,
|
NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, Error, ErrorKind,
|
||||||
ConnectionFilter};
|
ConnectionFilter};
|
||||||
use ethereum_types::{H256, H512, U256};
|
use ethereum_types::{H256, H512, U256};
|
||||||
@ -371,7 +371,7 @@ struct SyncProtocolHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl NetworkProtocolHandler for SyncProtocolHandler {
|
impl NetworkProtocolHandler for SyncProtocolHandler {
|
||||||
fn initialize(&self, io: &NetworkContext, _host_info: &HostInfo) {
|
fn initialize(&self, io: &NetworkContext) {
|
||||||
if io.subprotocol_name() != WARP_SYNC_PROTOCOL_ID {
|
if io.subprotocol_name() != WARP_SYNC_PROTOCOL_ID {
|
||||||
io.register_timer(0, Duration::from_secs(1)).expect("Error registering sync timer");
|
io.register_timer(0, Duration::from_secs(1)).expect("Error registering sync timer");
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ use node_table::NodeId;
|
|||||||
use io::{IoContext, StreamToken};
|
use io::{IoContext, StreamToken};
|
||||||
use ethkey::{KeyPair, Public, Secret, recover, sign, Generator, Random};
|
use ethkey::{KeyPair, Public, Secret, recover, sign, Generator, Random};
|
||||||
use ethkey::crypto::{ecdh, ecies};
|
use ethkey::crypto::{ecdh, ecies};
|
||||||
use network::{Error, ErrorKind, HostInfo as HostInfoTrait};
|
use network::{Error, ErrorKind};
|
||||||
use host::HostInfo;
|
use host::HostInfo;
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug)]
|
#[derive(PartialEq, Eq, Debug)]
|
||||||
|
@ -39,7 +39,6 @@ use PROTOCOL_VERSION;
|
|||||||
use node_table::*;
|
use node_table::*;
|
||||||
use network::{NetworkConfiguration, NetworkIoMessage, ProtocolId, PeerId, PacketId};
|
use network::{NetworkConfiguration, NetworkIoMessage, ProtocolId, PeerId, PacketId};
|
||||||
use network::{NonReservedPeerMode, NetworkContext as NetworkContextTrait};
|
use network::{NonReservedPeerMode, NetworkContext as NetworkContextTrait};
|
||||||
use network::HostInfo as HostInfoTrait;
|
|
||||||
use network::{SessionInfo, Error, ErrorKind, DisconnectReason, NetworkProtocolHandler};
|
use network::{SessionInfo, Error, ErrorKind, DisconnectReason, NetworkProtocolHandler};
|
||||||
use discovery::{Discovery, TableUpdates, NodeEntry};
|
use discovery::{Discovery, TableUpdates, NodeEntry};
|
||||||
use ip_utils::{map_external_address, select_public_address};
|
use ip_utils::{map_external_address, select_public_address};
|
||||||
@ -223,10 +222,8 @@ impl HostInfo {
|
|||||||
pub(crate) fn secret(&self) -> &Secret {
|
pub(crate) fn secret(&self) -> &Secret {
|
||||||
self.keys.secret()
|
self.keys.secret()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl HostInfoTrait for HostInfo {
|
pub(crate) fn id(&self) -> &NodeId {
|
||||||
fn id(&self) -> &NodeId {
|
|
||||||
self.keys.public()
|
self.keys.public()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -997,7 +994,6 @@ impl IoHandler<NetworkIoMessage> for Host {
|
|||||||
let reserved = self.reserved_nodes.read();
|
let reserved = self.reserved_nodes.read();
|
||||||
h.initialize(
|
h.initialize(
|
||||||
&NetworkContext::new(io, *protocol, None, self.sessions.clone(), &reserved),
|
&NetworkContext::new(io, *protocol, None, self.sessions.clone(), &reserved),
|
||||||
&*self.info.read(),
|
|
||||||
);
|
);
|
||||||
self.handlers.write().insert(*protocol, h);
|
self.handlers.write().insert(*protocol, h);
|
||||||
let mut info = self.info.write();
|
let mut info = self.info.write();
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
//! struct MyHandler;
|
//! struct MyHandler;
|
||||||
//!
|
//!
|
||||||
//! impl NetworkProtocolHandler for MyHandler {
|
//! impl NetworkProtocolHandler for MyHandler {
|
||||||
//! fn initialize(&self, io: &NetworkContext, _host_info: &HostInfo) {
|
//! fn initialize(&self, io: &NetworkContext) {
|
||||||
//! io.register_timer(0, Duration::from_secs(1));
|
//! io.register_timer(0, Duration::from_secs(1));
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
|
@ -28,7 +28,7 @@ use connection::{EncryptedConnection, Packet, Connection, MAX_PAYLOAD_SIZE};
|
|||||||
use handshake::Handshake;
|
use handshake::Handshake;
|
||||||
use io::{IoContext, StreamToken};
|
use io::{IoContext, StreamToken};
|
||||||
use network::{Error, ErrorKind, DisconnectReason, SessionInfo, ProtocolId, PeerCapabilityInfo};
|
use network::{Error, ErrorKind, DisconnectReason, SessionInfo, ProtocolId, PeerCapabilityInfo};
|
||||||
use network::{SessionCapabilityInfo, HostInfo as HostInfoTrait};
|
use network::SessionCapabilityInfo;
|
||||||
use host::*;
|
use host::*;
|
||||||
use node_table::NodeId;
|
use node_table::NodeId;
|
||||||
use snappy;
|
use snappy;
|
||||||
|
@ -70,7 +70,7 @@ impl TestProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl NetworkProtocolHandler for TestProtocol {
|
impl NetworkProtocolHandler for TestProtocol {
|
||||||
fn initialize(&self, io: &NetworkContext, _host_info: &HostInfo) {
|
fn initialize(&self, io: &NetworkContext) {
|
||||||
io.register_timer(0, Duration::from_millis(10)).unwrap();
|
io.register_timer(0, Duration::from_millis(10)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,17 +333,12 @@ impl<'a, T> NetworkContext for &'a T where T: ?Sized + NetworkContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait HostInfo {
|
|
||||||
/// Returns public key
|
|
||||||
fn id(&self) -> &NodeId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Network IO protocol handler. This needs to be implemented for each new subprotocol.
|
/// Network IO protocol handler. This needs to be implemented for each new subprotocol.
|
||||||
/// All the handler function are called from within IO event loop.
|
/// All the handler function are called from within IO event loop.
|
||||||
/// `Message` is the type for message data.
|
/// `Message` is the type for message data.
|
||||||
pub trait NetworkProtocolHandler: Sync + Send {
|
pub trait NetworkProtocolHandler: Sync + Send {
|
||||||
/// Initialize the handler
|
/// Initialize the handler
|
||||||
fn initialize(&self, _io: &NetworkContext, _host_info: &HostInfo) {}
|
fn initialize(&self, _io: &NetworkContext) {}
|
||||||
/// Called when new network packet received.
|
/// Called when new network packet received.
|
||||||
fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]);
|
fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]);
|
||||||
/// Called when new peer is connected. Only called when peer supports the same protocol.
|
/// Called when new peer is connected. Only called when peer supports the same protocol.
|
||||||
|
@ -23,7 +23,7 @@ use std::time::{Duration, SystemTime};
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use ethereum_types::{H256, H512};
|
use ethereum_types::{H256, H512};
|
||||||
use network::{self, HostInfo, NetworkContext, NodeId, PeerId, ProtocolId, TimerToken};
|
use network::{self, NetworkContext, NodeId, PeerId, ProtocolId, TimerToken};
|
||||||
use ordered_float::OrderedFloat;
|
use ordered_float::OrderedFloat;
|
||||||
use parking_lot::{Mutex, RwLock};
|
use parking_lot::{Mutex, RwLock};
|
||||||
use rlp::{DecoderError, RlpStream, Rlp};
|
use rlp::{DecoderError, RlpStream, Rlp};
|
||||||
@ -423,7 +423,6 @@ pub struct Network<T> {
|
|||||||
messages: Arc<RwLock<Messages>>,
|
messages: Arc<RwLock<Messages>>,
|
||||||
handler: T,
|
handler: T,
|
||||||
peers: RwLock<HashMap<PeerId, Mutex<Peer>>>,
|
peers: RwLock<HashMap<PeerId, Mutex<Peer>>>,
|
||||||
node_key: RwLock<NodeId>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// public API.
|
// public API.
|
||||||
@ -434,7 +433,6 @@ impl<T> Network<T> {
|
|||||||
messages: Arc::new(RwLock::new(Messages::new(messages_size_bytes))),
|
messages: Arc::new(RwLock::new(Messages::new(messages_size_bytes))),
|
||||||
handler: handler,
|
handler: handler,
|
||||||
peers: RwLock::new(HashMap::new()),
|
peers: RwLock::new(HashMap::new()),
|
||||||
node_key: RwLock::new(Default::default()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -685,12 +683,10 @@ impl<T: MessageHandler> Network<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: MessageHandler> ::network::NetworkProtocolHandler for Network<T> {
|
impl<T: MessageHandler> ::network::NetworkProtocolHandler for Network<T> {
|
||||||
fn initialize(&self, io: &NetworkContext, host_info: &HostInfo) {
|
fn initialize(&self, io: &NetworkContext) {
|
||||||
// set up broadcast timer (< 1s)
|
// set up broadcast timer (< 1s)
|
||||||
io.register_timer(RALLY_TOKEN, RALLY_TIMEOUT)
|
io.register_timer(RALLY_TOKEN, RALLY_TIMEOUT)
|
||||||
.expect("Failed to initialize message rally timer");
|
.expect("Failed to initialize message rally timer");
|
||||||
|
|
||||||
*self.node_key.write() = host_info.id().clone();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||||
@ -720,7 +716,7 @@ impl<T: MessageHandler> ::network::NetworkProtocolHandler for Network<T> {
|
|||||||
pub struct ParityExtensions;
|
pub struct ParityExtensions;
|
||||||
|
|
||||||
impl ::network::NetworkProtocolHandler for ParityExtensions {
|
impl ::network::NetworkProtocolHandler for ParityExtensions {
|
||||||
fn initialize(&self, _io: &NetworkContext, _host_info: &HostInfo) { }
|
fn initialize(&self, _io: &NetworkContext) { }
|
||||||
|
|
||||||
fn read(&self, _io: &NetworkContext, _peer: &PeerId, _id: u8, _msg: &[u8]) { }
|
fn read(&self, _io: &NetworkContext, _peer: &PeerId, _id: u8, _msg: &[u8]) { }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user