lighthouse-pulse/lighthouse/sync/network.rs

67 lines
1.7 KiB
Rust
Raw Normal View History

2018-09-09 14:36:00 +00:00
use std::sync::{ RwLock, Arc };
use super::db::DB;
use slog::Logger;
use super::network_libp2p::message::{
NetworkEvent,
OutgoingMessage,
NetworkEventType,
};
2018-09-09 16:28:36 +00:00
use super::wire_protocol::{ WireMessageType, message_type };
2018-09-09 14:36:00 +00:00
use super::futures::sync::mpsc::{
UnboundedSender,
};
2018-09-09 16:28:36 +00:00
/// Accept a network event and perform all required processing.
///
/// This function should be called whenever an underlying network
/// (e.g., libp2p) has an event to push up to the sync process.
2018-09-09 14:36:00 +00:00
pub fn handle_network_event(
event: NetworkEvent,
db: Arc<RwLock<DB>>,
network_tx: UnboundedSender<OutgoingMessage>,
log: Logger)
-> Result<(), ()>
{
2018-09-09 16:28:36 +00:00
debug!(&log, "";
"network_event" => format!("{:?}", &event));
2018-09-09 14:36:00 +00:00
match event.event {
NetworkEventType::PeerConnect => Ok(()),
NetworkEventType::PeerDrop => Ok(()),
2018-09-09 16:28:36 +00:00
NetworkEventType::Message => {
if let Some(data) = event.data {
handle_network_message(
data,
db,
network_tx,
log)
} else {
Ok(())
}
}
2018-09-09 14:36:00 +00:00
}
}
2018-09-09 16:28:36 +00:00
/// Accept a message from the network and perform all required
/// processing.
///
/// This function should be called whenever a peer from a network
/// (e.g., libp2p) has sent a message to us.
2018-09-09 14:36:00 +00:00
fn handle_network_message(
2018-09-09 16:28:36 +00:00
message: Vec<u8>,
2018-09-09 14:36:00 +00:00
_db: Arc<RwLock<DB>>,
_network_tx: UnboundedSender<OutgoingMessage>,
2018-09-09 16:28:36 +00:00
_log: Logger)
2018-09-09 14:36:00 +00:00
-> Result<(), ()>
{
2018-09-09 16:28:36 +00:00
match message_type(&message) {
Some(WireMessageType::Blocks) => {
// Do something with inbound blocks.
Ok(())
}
_ => Ok(())
}
2018-09-09 14:36:00 +00:00
}