New network IO API
This commit is contained in:
parent
fc25330804
commit
0de2a031d1
@ -1,6 +1,7 @@
|
|||||||
use client::BlockChainClient;
|
use client::BlockChainClient;
|
||||||
use util::network::{HandlerIo, PeerId, PacketId,};
|
use util::{NetworkContext, PeerId, PacketId,};
|
||||||
use util::error::UtilError;
|
use util::error::UtilError;
|
||||||
|
use sync::SyncMessage;
|
||||||
|
|
||||||
/// IO interface for the syning handler.
|
/// IO interface for the syning handler.
|
||||||
/// Provides peer connection management and an interface to the blockchain client.
|
/// Provides peer connection management and an interface to the blockchain client.
|
||||||
@ -16,15 +17,15 @@ pub trait SyncIo {
|
|||||||
fn chain<'s>(&'s mut self) -> &'s mut BlockChainClient;
|
fn chain<'s>(&'s mut self) -> &'s mut BlockChainClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `HandlerIo` and the blockchain client
|
/// Wraps `NetworkContext` and the blockchain client
|
||||||
pub struct NetSyncIo<'s, 'h> where 'h:'s {
|
pub struct NetSyncIo<'s, 'h, 'io> where 'h: 's, 'io: 'h {
|
||||||
network: &'s mut HandlerIo<'h>,
|
network: &'s mut NetworkContext<'h, 'io, SyncMessage>,
|
||||||
chain: &'s mut BlockChainClient
|
chain: &'s mut BlockChainClient
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'h> NetSyncIo<'s, 'h> {
|
impl<'s, 'h, 'io> NetSyncIo<'s, 'h, 'io> {
|
||||||
/// Creates a new instance from the `HandlerIo` and the blockchain client reference.
|
/// Creates a new instance from the `NetworkContext` and the blockchain client reference.
|
||||||
pub fn new(network: &'s mut HandlerIo<'h>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h> {
|
pub fn new(network: &'s mut NetworkContext<'h, 'io, SyncMessage>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h,'io> {
|
||||||
NetSyncIo {
|
NetSyncIo {
|
||||||
network: network,
|
network: network,
|
||||||
chain: chain,
|
chain: chain,
|
||||||
@ -32,7 +33,7 @@ impl<'s, 'h> NetSyncIo<'s, 'h> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> {
|
impl<'s, 'h, 'op> SyncIo for NetSyncIo<'s, 'h, 'op> {
|
||||||
fn disable_peer(&mut self, peer_id: &PeerId) {
|
fn disable_peer(&mut self, peer_id: &PeerId) {
|
||||||
self.network.disable_peer(*peer_id);
|
self.network.disable_peer(*peer_id);
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,9 @@
|
|||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use client::BlockChainClient;
|
use client::BlockChainClient;
|
||||||
use util::network::{ProtocolHandler, NetworkService, HandlerIo, TimerToken, PeerId, Message};
|
use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId};
|
||||||
|
use util::TimerToken;
|
||||||
|
use util::Bytes;
|
||||||
use sync::chain::ChainSync;
|
use sync::chain::ChainSync;
|
||||||
use sync::io::NetSyncIo;
|
use sync::io::NetSyncIo;
|
||||||
|
|
||||||
@ -35,6 +37,13 @@ mod range_collection;
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
|
/// Message type for external events
|
||||||
|
pub enum SyncMessage {
|
||||||
|
/// New block has been imported into the blockchain
|
||||||
|
NewBlock(Bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Ethereum network protocol handler
|
/// Ethereum network protocol handler
|
||||||
pub struct EthSync {
|
pub struct EthSync {
|
||||||
/// Shared blockchain client. TODO: this should evetually become an IPC endpoint
|
/// Shared blockchain client. TODO: this should evetually become an IPC endpoint
|
||||||
@ -47,7 +56,7 @@ pub use self::chain::SyncStatus;
|
|||||||
|
|
||||||
impl EthSync {
|
impl EthSync {
|
||||||
/// Creates and register protocol with the network service
|
/// Creates and register protocol with the network service
|
||||||
pub fn register(service: &mut NetworkService, chain: Arc<BlockChainClient + Send + Sized>) {
|
pub fn register(service: &mut NetworkService<SyncMessage>, chain: Arc<BlockChainClient + Send + Sized>) {
|
||||||
let sync = Box::new(EthSync {
|
let sync = Box::new(EthSync {
|
||||||
chain: chain,
|
chain: chain,
|
||||||
sync: ChainSync::new(),
|
sync: ChainSync::new(),
|
||||||
@ -61,39 +70,39 @@ impl EthSync {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Stop sync
|
/// Stop sync
|
||||||
pub fn stop(&mut self, io: &mut HandlerIo) {
|
pub fn stop(&mut self, io: &mut NetworkContext<SyncMessage>) {
|
||||||
self.sync.abort(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
self.sync.abort(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Restart sync
|
/// Restart sync
|
||||||
pub fn restart(&mut self, io: &mut HandlerIo) {
|
pub fn restart(&mut self, io: &mut NetworkContext<SyncMessage>) {
|
||||||
self.sync.restart(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
self.sync.restart(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProtocolHandler for EthSync {
|
impl NetworkProtocolHandler<SyncMessage> for EthSync {
|
||||||
fn initialize(&mut self, io: &mut HandlerIo) {
|
fn initialize(&mut self, io: &mut NetworkContext<SyncMessage>) {
|
||||||
self.sync.restart(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
self.sync.restart(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
||||||
io.register_timer(1000).unwrap();
|
io.register_timer(1000).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read(&mut self, io: &mut HandlerIo, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
fn read(&mut self, io: &mut NetworkContext<SyncMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||||
self.sync.on_packet(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer, packet_id, data);
|
self.sync.on_packet(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer, packet_id, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn connected(&mut self, io: &mut HandlerIo, peer: &PeerId) {
|
fn connected(&mut self, io: &mut NetworkContext<SyncMessage>, peer: &PeerId) {
|
||||||
self.sync.on_peer_connected(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer);
|
self.sync.on_peer_connected(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn disconnected(&mut self, io: &mut HandlerIo, peer: &PeerId) {
|
fn disconnected(&mut self, io: &mut NetworkContext<SyncMessage>, peer: &PeerId) {
|
||||||
self.sync.on_peer_aborting(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer);
|
self.sync.on_peer_aborting(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn timeout(&mut self, io: &mut HandlerIo, _timer: TimerToken) {
|
fn timeout(&mut self, io: &mut NetworkContext<SyncMessage>, _timer: TimerToken) {
|
||||||
self.sync.maintain_sync(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
self.sync.maintain_sync(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn message(&mut self, _io: &mut HandlerIo, _message: &Message) {
|
fn message(&mut self, _io: &mut NetworkContext<SyncMessage>, _message: &SyncMessage) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user