Skip to content

Commit

Permalink
fix(net): make gosipsub field public on behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Aug 26, 2024
1 parent d9d8fd3 commit 3b9f0e9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
31 changes: 29 additions & 2 deletions crates/net/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use crate::{event::Event, handler::Handler};
#[behaviour(out_event = "Event")]
pub struct Behaviour {
/// Responds to inbound pings and send outbound pings.
ping: libp2p::ping::Behaviour,
pub ping: libp2p::ping::Behaviour,
/// Enables gossipsub as the routing layer.
gossipsub: libp2p::gossipsub::Behaviour,
pub gossipsub: libp2p::gossipsub::Behaviour,
}

impl Behaviour {
Expand Down Expand Up @@ -44,3 +44,30 @@ impl Behaviour {
Ok(Self { ping, gossipsub })
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::handler::BlockHandler;
use alloy::primitives::Address;

#[test]
fn test_behaviour_no_handlers() {
let cfg = crate::config::default_config_builder()
.build()
.expect("Failed to build default config");
let handlers = vec![];
let _ = Behaviour::new(cfg, &handlers).unwrap();
}

#[test]
fn test_behaviour_with_handlers() {
let cfg = crate::config::default_config_builder()
.build()
.expect("Failed to build default config");
let (_, recv) = tokio::sync::watch::channel(Address::default());
let (block_handler, _) = BlockHandler::new(0, recv);
let handlers: Vec<Box<dyn Handler>> = vec![Box::new(block_handler)];
let _ = Behaviour::new(cfg, &handlers).unwrap();
}
}
16 changes: 8 additions & 8 deletions crates/net/src/driver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Driver for p2p services.
//! Driver for network services.
use alloy::primitives::Address;
use eyre::Result;
Expand Down Expand Up @@ -68,16 +68,16 @@ impl GossipDriver {
},
event = swarm.select_next_some() => {
if let SwarmEvent::Behaviour(Event::Gossipsub(libp2p::gossipsub::Event::Message {
propagation_source: _peer_id,
message_id: _id,
propagation_source: src,
message_id: id,
message,
})) = event {
if handler.topics().contains(&message.topic) {
let _status = handler.handle(message);
// TODO: report message validation result??
// _ = swarm
// .behaviour_mut()
// .report_message_validation_result(&message_id, &propagation_source, status);
let status = handler.handle(message);
_ = swarm
.behaviour_mut()
.gossipsub
.report_message_validation_result(&id, &src, status);
}
}
},
Expand Down
12 changes: 6 additions & 6 deletions crates/net/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ pub trait Handler: Send {
pub struct BlockHandler {
/// Chain ID of the L2 blockchain. Used to filter out gossip messages intended for other
/// blockchains.
chain_id: u64,
pub chain_id: u64,
/// A channel sender to forward new blocks to other modules
block_sender: Sender<ExecutionPayloadEnvelope>,
pub block_sender: Sender<ExecutionPayloadEnvelope>,
/// A [Receiver] to monitor changes to the unsafe block signer.
unsafe_signer_recv: Receiver<Address>,
pub unsafe_signer_recv: Receiver<Address>,
/// The libp2p topic for pre Canyon/Shangai blocks.
blocks_v1_topic: IdentTopic,
pub blocks_v1_topic: IdentTopic,
/// The libp2p topic for Canyon/Delta blocks.
blocks_v2_topic: IdentTopic,
pub blocks_v2_topic: IdentTopic,
/// The libp2p topic for Ecotone V3 blocks.
blocks_v3_topic: IdentTopic,
pub blocks_v3_topic: IdentTopic,
}

impl Handler for BlockHandler {
Expand Down

0 comments on commit 3b9f0e9

Please sign in to comment.