Simplify the floodsub handler (#868)

This commit is contained in:
Pierre Krieger
2019-01-22 14:45:03 +01:00
committed by GitHub
parent d59ec09a83
commit a2ab7ff4a9
6 changed files with 327 additions and 320 deletions

View File

@ -18,13 +18,12 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::handler::FloodsubHandler;
use crate::protocol::{FloodsubMessage, FloodsubRpc, FloodsubSubscription, FloodsubSubscriptionAction};
use crate::protocol::{FloodsubConfig, FloodsubMessage, FloodsubRpc, FloodsubSubscription, FloodsubSubscriptionAction};
use crate::topic::{Topic, TopicHash};
use cuckoofilter::CuckooFilter;
use futures::prelude::*;
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p_core::{protocols_handler::ProtocolsHandler, PeerId};
use libp2p_core::{protocols_handler::ProtocolsHandler, protocols_handler::OneShotHandler, PeerId};
use rand;
use smallvec::SmallVec;
use std::{collections::VecDeque, iter, marker::PhantomData};
@ -176,11 +175,11 @@ impl<TSubstream, TTopology> NetworkBehaviour<TTopology> for Floodsub<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite,
{
type ProtocolsHandler = FloodsubHandler<TSubstream>;
type ProtocolsHandler = OneShotHandler<TSubstream, FloodsubConfig, FloodsubRpc, InnerMessage>;
type OutEvent = FloodsubEvent;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
FloodsubHandler::new()
Default::default()
}
fn inject_connected(&mut self, id: PeerId, _: ConnectedPoint) {
@ -209,8 +208,14 @@ where
fn inject_node_event(
&mut self,
propagation_source: PeerId,
event: FloodsubRpc,
event: InnerMessage,
) {
// We ignore successful sends event.
let event = match event {
InnerMessage::Rx(event) => event,
InnerMessage::Sent => return,
};
// Update connected peers topics
for subscription in event.subscriptions {
let remote_peer_topics = self.connected_peers
@ -300,6 +305,28 @@ where
}
}
/// Transmission between the `OneShotHandler` and the `FloodsubHandler`.
pub enum InnerMessage {
/// We received an RPC from a remote.
Rx(FloodsubRpc),
/// We successfully sent an RPC request.
Sent,
}
impl From<FloodsubRpc> for InnerMessage {
#[inline]
fn from(rpc: FloodsubRpc) -> InnerMessage {
InnerMessage::Rx(rpc)
}
}
impl From<()> for InnerMessage {
#[inline]
fn from(_: ()) -> InnerMessage {
InnerMessage::Sent
}
}
/// Event that can happen on the floodsub behaviour.
#[derive(Debug)]
pub enum FloodsubEvent {