1
1
use crate :: behaviour:: AnchorBehaviour ;
2
+ use crate :: behaviour:: AnchorBehaviourEvent ;
2
3
use crate :: keypair_utils:: load_private_key;
3
4
use crate :: transport:: build_transport;
4
5
use crate :: Config ;
5
6
use futures:: StreamExt ;
6
7
use libp2p:: core:: muxing:: StreamMuxerBox ;
7
8
use libp2p:: core:: transport:: Boxed ;
9
+ use libp2p:: gossipsub:: { MessageAuthenticity , ValidationMode } ;
8
10
use libp2p:: identity:: Keypair ;
9
11
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 } ;
11
15
use std:: num:: { NonZeroU8 , NonZeroUsize } ;
12
16
use std:: pin:: Pin ;
17
+ use std:: time:: Duration ;
13
18
use task_executor:: TaskExecutor ;
14
- use tracing:: info;
19
+ use tracing:: { info, log } ;
15
20
16
21
pub struct Network {
17
22
swarm : Swarm < AnchorBehaviour > ,
@@ -74,8 +79,22 @@ impl Network {
74
79
pub async fn run ( mut self ) {
75
80
loop {
76
81
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
+ }
79
98
}
80
99
// TODO match input channels
81
100
}
@@ -84,8 +103,7 @@ impl Network {
84
103
}
85
104
86
105
fn build_anchor_behaviour ( local_keypair : Keypair ) -> AnchorBehaviour {
87
- // setup gossipsub
88
- // discv5
106
+ // TODO setup discv5
89
107
let identify = {
90
108
let local_public_key = local_keypair. public ( ) ;
91
109
let identify_config = identify:: Config :: new ( "anchor" . into ( ) , local_public_key)
@@ -94,10 +112,40 @@ fn build_anchor_behaviour(local_keypair: Keypair) -> AnchorBehaviour {
94
112
identify:: Behaviour :: new ( identify_config)
95
113
} ;
96
114
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
+
97
145
AnchorBehaviour {
98
146
identify,
99
147
ping : ping:: Behaviour :: default ( ) ,
100
- // gossipsub: gossipsub::Behaviour::default() ,
148
+ gossipsub,
101
149
}
102
150
}
103
151
0 commit comments