hotshot_libp2p_networking/network/
def.rs1use hotshot_types::traits::signature_key::SignatureKey;
8use libp2p::{
9 autonat,
10 gossipsub::{Behaviour as GossipBehaviour, Event as GossipEvent, IdentTopic},
11 identify::{Behaviour as IdentifyBehaviour, Event as IdentifyEvent},
12 kad::store::MemoryStore,
13 request_response::{OutboundRequestId, ResponseChannel},
14 Multiaddr,
15};
16use libp2p_identity::PeerId;
17use libp2p_swarm_derive::NetworkBehaviour;
18use tracing::{debug, error};
19
20use super::{
21 behaviours::dht::store::{
22 persistent::{DhtPersistentStorage, PersistentStore},
23 validated::ValidatedStore,
24 },
25 cbor, NetworkEventInternal,
26};
27
28#[derive(NetworkBehaviour, derive_more::Debug)]
34#[behaviour(to_swarm = "NetworkEventInternal")]
35pub struct NetworkDef<K: SignatureKey + 'static, D: DhtPersistentStorage> {
36 #[debug(skip)]
41 gossipsub: GossipBehaviour,
42
43 #[debug(skip)]
46 pub dht: libp2p::kad::Behaviour<PersistentStore<ValidatedStore<MemoryStore, K>, D>>,
47
48 #[debug(skip)]
50 identify: IdentifyBehaviour,
51
52 #[debug(skip)]
54 pub direct_message: cbor::Behaviour<Vec<u8>, Vec<u8>>,
55
56 #[debug(skip)]
59 pub autonat: libp2p::autonat::Behaviour,
60}
61
62impl<K: SignatureKey + 'static, D: DhtPersistentStorage> NetworkDef<K, D> {
63 #[must_use]
65 pub fn new(
66 gossipsub: GossipBehaviour,
67 dht: libp2p::kad::Behaviour<PersistentStore<ValidatedStore<MemoryStore, K>, D>>,
68 identify: IdentifyBehaviour,
69 direct_message: super::cbor::Behaviour<Vec<u8>, Vec<u8>>,
70 autonat: autonat::Behaviour,
71 ) -> NetworkDef<K, D> {
72 Self {
73 gossipsub,
74 dht,
75 identify,
76 direct_message,
77 autonat,
78 }
79 }
80}
81
82impl<K: SignatureKey + 'static, D: DhtPersistentStorage> NetworkDef<K, D> {
84 pub fn add_address(&mut self, peer_id: &PeerId, address: Multiaddr) {
86 self.dht.add_address(peer_id, address);
93 }
94}
95
96impl<K: SignatureKey + 'static, D: DhtPersistentStorage> NetworkDef<K, D> {
98 pub fn publish_gossip(&mut self, topic: IdentTopic, contents: Vec<u8>) {
100 if let Err(e) = self.gossipsub.publish(topic, contents) {
101 tracing::warn!("Failed to publish gossip message. Error: {:?}", e);
102 }
103 }
104 pub fn subscribe_gossip(&mut self, t: &str) {
106 if let Err(e) = self.gossipsub.subscribe(&IdentTopic::new(t)) {
107 error!("Failed to subscribe to topic {:?}. Error: {:?}", t, e);
108 }
109 }
110
111 pub fn unsubscribe_gossip(&mut self, t: &str) {
113 if let Err(e) = self.gossipsub.unsubscribe(&IdentTopic::new(t)) {
114 error!("Failed to unsubscribe from topic {:?}. Error: {:?}", t, e);
115 }
116 }
117}
118
119impl<K: SignatureKey + 'static, D: DhtPersistentStorage> NetworkDef<K, D> {
121 pub fn add_direct_request(&mut self, peer_id: PeerId, data: Vec<u8>) -> OutboundRequestId {
123 self.direct_message.send_request(&peer_id, data)
124 }
125
126 pub fn add_direct_response(&mut self, chan: ResponseChannel<Vec<u8>>, msg: Vec<u8>) {
128 let _ = self.direct_message.send_response(chan, msg);
129 }
130}
131
132impl From<GossipEvent> for NetworkEventInternal {
133 fn from(event: GossipEvent) -> Self {
134 Self::GossipEvent(Box::new(event))
135 }
136}
137
138impl From<libp2p::kad::Event> for NetworkEventInternal {
139 fn from(event: libp2p::kad::Event) -> Self {
140 Self::DHTEvent(event)
141 }
142}
143
144impl From<IdentifyEvent> for NetworkEventInternal {
145 fn from(event: IdentifyEvent) -> Self {
146 Self::IdentifyEvent(Box::new(event))
147 }
148}
149impl From<libp2p::request_response::Event<Vec<u8>, Vec<u8>>> for NetworkEventInternal {
150 fn from(value: libp2p::request_response::Event<Vec<u8>, Vec<u8>>) -> Self {
151 Self::DMEvent(value)
152 }
153}
154
155impl From<libp2p::autonat::Event> for NetworkEventInternal {
156 fn from(event: libp2p::autonat::Event) -> Self {
157 Self::AutonatEvent(event)
158 }
159}