11use crate :: behaviour:: AnchorBehaviour ;
2+ use crate :: behaviour:: AnchorBehaviourEvent ;
23use crate :: keypair_utils:: load_private_key;
34use crate :: transport:: build_transport;
45use crate :: Config ;
56use futures:: StreamExt ;
67use libp2p:: core:: muxing:: StreamMuxerBox ;
78use libp2p:: core:: transport:: Boxed ;
9+ use libp2p:: gossipsub:: { MessageAuthenticity , ValidationMode } ;
810use libp2p:: identity:: Keypair ;
911use 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 } ;
1115use std:: num:: { NonZeroU8 , NonZeroUsize } ;
1216use std:: pin:: Pin ;
17+ use std:: time:: Duration ;
1318use task_executor:: TaskExecutor ;
14- use tracing:: info;
19+ use tracing:: { info, log } ;
1520
1621pub 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
86105fn 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