Skip to content

Commit 3e023ac

Browse files
diegomrsantosjking-ausjxs
authored
Add gossipsub behaviour (#70)
Co-authored-by: jking-aus <[email protected]> Co-authored-by: João Oliveira <[email protected]>
1 parent 6ec4b6a commit 3e023ac

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

anchor/network/src/behaviour.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use libp2p::swarm::NetworkBehaviour;
2-
use libp2p::{identify, ping};
2+
use libp2p::{gossipsub, identify, ping};
33

44
#[derive(NetworkBehaviour)]
55
pub struct AnchorBehaviour {
66
/// Provides IP addresses and peer information.
77
pub identify: identify::Behaviour,
88
/// Used for connection health checks.
99
pub ping: ping::Behaviour,
10-
// /// The routing pub-sub mechanism for Anchor.
11-
// pub gossipsub: gossipsub::Behaviour,
10+
/// The routing pub-sub mechanism for Anchor.
11+
pub gossipsub: gossipsub::Behaviour,
1212
}

anchor/network/src/network.rs

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
use crate::behaviour::AnchorBehaviour;
2+
use crate::behaviour::AnchorBehaviourEvent;
23
use crate::keypair_utils::load_private_key;
34
use crate::transport::build_transport;
45
use crate::Config;
56
use futures::StreamExt;
67
use libp2p::core::muxing::StreamMuxerBox;
78
use libp2p::core::transport::Boxed;
9+
use libp2p::gossipsub::{MessageAuthenticity, ValidationMode};
810
use libp2p::identity::Keypair;
911
use libp2p::multiaddr::Protocol;
10-
use libp2p::{futures, identify, ping, PeerId, Swarm, SwarmBuilder};
12+
use libp2p::swarm::SwarmEvent;
13+
use libp2p::{futures, gossipsub, identify, ping, PeerId, Swarm, SwarmBuilder};
14+
use lighthouse_network::discv5::enr::k256::sha2::{Digest, Sha256};
1115
use std::num::{NonZeroU8, NonZeroUsize};
1216
use std::pin::Pin;
17+
use std::time::Duration;
1318
use task_executor::TaskExecutor;
14-
use tracing::info;
19+
use tracing::{info, log};
1520

1621
pub struct Network {
1722
swarm: Swarm<AnchorBehaviour>,
@@ -74,8 +79,22 @@ impl Network {
7479
pub async fn run(mut self) {
7580
loop {
7681
tokio::select! {
77-
_swarm_message = self.swarm.select_next_some() => {
78-
// TODO handle and match swarm messages
82+
swarm_message = self.swarm.select_next_some() => {
83+
match swarm_message {
84+
SwarmEvent::Behaviour(behaviour_event) => match behaviour_event {
85+
AnchorBehaviourEvent::Gossipsub(_ge) => {
86+
// TODO handle gossipsub events
87+
},
88+
// TODO handle other behaviour events
89+
_ => {
90+
log::debug!("Unhandled behaviour event: {:?}", behaviour_event);
91+
}
92+
},
93+
// TODO handle other swarm events
94+
_ => {
95+
log::debug!("Unhandled swarm event: {:?}", swarm_message);
96+
}
97+
}
7998
}
8099
// TODO match input channels
81100
}
@@ -84,8 +103,7 @@ impl Network {
84103
}
85104

86105
fn build_anchor_behaviour(local_keypair: Keypair) -> AnchorBehaviour {
87-
// setup gossipsub
88-
// discv5
106+
// TODO setup discv5
89107
let identify = {
90108
let local_public_key = local_keypair.public();
91109
let identify_config = identify::Config::new("anchor".into(), local_public_key)
@@ -94,10 +112,40 @@ fn build_anchor_behaviour(local_keypair: Keypair) -> AnchorBehaviour {
94112
identify::Behaviour::new(identify_config)
95113
};
96114

115+
// TODO those values might need to be parameterized based on the network
116+
let slots_per_epoch = 32;
117+
let seconds_per_slot = 12;
118+
let duplicate_cache_time = Duration::from_secs(slots_per_epoch * seconds_per_slot); // 6.4 min
119+
120+
let gossip_message_id = move |message: &gossipsub::Message| {
121+
gossipsub::MessageId::from(&Sha256::digest(&message.data)[..20])
122+
};
123+
124+
// TODO Add Topic Message Validator and Subscription Filter
125+
let config = gossipsub::ConfigBuilder::default()
126+
.duplicate_cache_time(duplicate_cache_time)
127+
.message_id_fn(gossip_message_id)
128+
.flood_publish(false)
129+
.validation_mode(ValidationMode::Strict)
130+
.mesh_n(8) //D
131+
.mesh_n_low(6) // Dlo
132+
.mesh_n_high(12) // Dhi
133+
.mesh_outbound_min(4) // Dout
134+
.heartbeat_interval(Duration::from_millis(700))
135+
.history_length(6)
136+
.history_gossip(4)
137+
.max_ihave_length(1500)
138+
.max_ihave_messages(32)
139+
.build()
140+
.unwrap();
141+
142+
let gossipsub =
143+
gossipsub::Behaviour::new(MessageAuthenticity::Signed(local_keypair), config).unwrap();
144+
97145
AnchorBehaviour {
98146
identify,
99147
ping: ping::Behaviour::default(),
100-
// gossipsub: gossipsub::Behaviour::default(),
148+
gossipsub,
101149
}
102150
}
103151

0 commit comments

Comments
 (0)