-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathmixed_behaviour.rs
More file actions
130 lines (121 loc) · 5.06 KB
/
mixed_behaviour.rs
File metadata and controls
130 lines (121 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// TODO(shahak): Erase main_behaviour and make this a separate module.
use std::time::Duration;
use libp2p::identity::Keypair;
use libp2p::kad::store::MemoryStore;
use libp2p::swarm::behaviour::toggle::Toggle;
use libp2p::swarm::dial_opts::DialOpts;
use libp2p::swarm::NetworkBehaviour;
use libp2p::{gossipsub, identify, kad, Multiaddr, PeerId, StreamProtocol};
use starknet_api::core::ChainId;
use crate::discovery::identify_impl::{IdentifyToOtherBehaviourEvent, IDENTIFY_PROTOCOL_VERSION};
use crate::discovery::kad_impl::KadToOtherBehaviourEvent;
use crate::discovery::DiscoveryConfig;
use crate::peer_manager::PeerManagerConfig;
use crate::{discovery, gossipsub_impl, peer_manager, sqmr};
// TODO(Shahak): consider reducing the pulicity of all behaviour to pub(crate)
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "Event")]
pub struct MixedBehaviour {
pub peer_manager: peer_manager::PeerManager,
pub discovery: Toggle<discovery::Behaviour>,
pub identify: identify::Behaviour,
// TODO(shahak): Consider using a different store.
pub kademlia: kad::Behaviour<MemoryStore>,
pub sqmr: sqmr::Behaviour,
pub gossipsub: gossipsub::Behaviour,
}
#[derive(Debug)]
pub enum Event {
ExternalEvent(ExternalEvent),
ToOtherBehaviourEvent(ToOtherBehaviourEvent),
}
#[derive(Debug)]
pub enum ExternalEvent {
Sqmr(sqmr::behaviour::ExternalEvent),
GossipSub(gossipsub_impl::ExternalEvent),
}
#[derive(Debug)]
pub enum ToOtherBehaviourEvent {
NoOp,
Identify(IdentifyToOtherBehaviourEvent),
Kad(KadToOtherBehaviourEvent),
Discovery(discovery::ToOtherBehaviourEvent),
PeerManager(peer_manager::ToOtherBehaviourEvent),
Sqmr(sqmr::ToOtherBehaviourEvent),
}
pub trait BridgedBehaviour {
fn on_other_behaviour_event(&mut self, event: &ToOtherBehaviourEvent);
}
impl MixedBehaviour {
// TODO(Shahak): get config details from network manager config
/// Panics if bootstrap_peer_multiaddr doesn't have a peer id.
pub fn new(
keypair: Keypair,
// TODO(AndrewL): consider making this non optional
bootstrap_peers_multiaddrs: Option<Vec<Multiaddr>>,
streamed_bytes_config: sqmr::Config,
chain_id: ChainId,
node_version: Option<String>,
discovery_config: DiscoveryConfig,
peer_manager_config: PeerManagerConfig,
) -> Self {
let public_key = keypair.public();
let local_peer_id = PeerId::from_public_key(&public_key);
let protocol_name =
StreamProtocol::try_from_owned(format!("/starknet/kad/{chain_id}/1.0.0"))
.expect("Failed to create StreamProtocol from a string that starts with /");
let kademlia_config = kad::Config::new(protocol_name);
Self {
peer_manager: peer_manager::PeerManager::new(peer_manager_config),
discovery: bootstrap_peers_multiaddrs
.map(|bootstrap_peer_multiaddr| {
discovery::Behaviour::new(
local_peer_id,
discovery_config,
bootstrap_peer_multiaddr
.iter()
.map(|bootstrap_peer_multiaddr| {
(
DialOpts::from(bootstrap_peer_multiaddr.clone())
.get_peer_id()
.expect("bootstrap_peer_multiaddr doesn't have a peer id"),
bootstrap_peer_multiaddr.clone(),
)
})
.collect(),
)
})
.into(),
identify: match node_version {
Some(version) => identify::Behaviour::new(
identify::Config::new(IDENTIFY_PROTOCOL_VERSION.to_string(), public_key)
.with_agent_version(version),
),
None => identify::Behaviour::new(identify::Config::new(
IDENTIFY_PROTOCOL_VERSION.to_string(),
public_key,
)),
},
// TODO(Shahak): change kademlia protocol name
kademlia: kad::Behaviour::with_config(
local_peer_id,
MemoryStore::new(local_peer_id),
kademlia_config,
),
sqmr: sqmr::Behaviour::new(streamed_bytes_config),
gossipsub: gossipsub::Behaviour::new(
gossipsub::MessageAuthenticity::Signed(keypair),
gossipsub::ConfigBuilder::default()
// TODO(shahak): try to reduce this bound.
.max_transmit_size(1 << 34).flood_publish(false).gossip_factor(0.0).duplicate_cache_time(Duration::from_secs(5))
.build()
.expect("Failed to build gossipsub config"),
)
.unwrap_or_else(|err_string| {
panic!(
"Failed creating gossipsub behaviour due to the following error: {err_string}"
)
}),
}
}
}