fuel_core_p2p/gossipsub/
topics.rs

1use libp2p::gossipsub::{
2    Sha256Topic,
3    Topic,
4    TopicHash,
5};
6
7use super::messages::{
8    GossipTopicTag,
9    GossipsubBroadcastRequest,
10};
11
12pub const NEW_TX_GOSSIP_TOPIC: &str = "new_tx";
13pub const TX_PRECONFIRMATIONS_GOSSIP_TOPIC: &str = "tx_preconfirmations";
14
15/// Holds used Gossipsub Topics
16/// Each field contains TopicHash of existing topics
17/// in order to avoid converting topics to TopicHash on each received message
18#[derive(Debug)]
19pub struct GossipsubTopics {
20    new_tx_topic: TopicHash,
21    tx_preconfirmations_topic: TopicHash,
22}
23
24impl GossipsubTopics {
25    pub fn new(network_name: &str) -> Self {
26        let new_tx_topic: Sha256Topic =
27            Topic::new(format!("{NEW_TX_GOSSIP_TOPIC}/{network_name}"));
28        let tx_preconfirmations_topic: Sha256Topic =
29            Topic::new(format!("{TX_PRECONFIRMATIONS_GOSSIP_TOPIC}/{network_name}"));
30        Self {
31            new_tx_topic: new_tx_topic.hash(),
32            tx_preconfirmations_topic: tx_preconfirmations_topic.hash(),
33        }
34    }
35
36    /// Given a TopicHash it will return a matching GossipTopicTag
37    pub fn get_gossipsub_tag(
38        &self,
39        incoming_topic: &TopicHash,
40    ) -> Option<GossipTopicTag> {
41        match incoming_topic {
42            hash if hash == &self.new_tx_topic => Some(GossipTopicTag::NewTx),
43            hash if hash == &self.tx_preconfirmations_topic => {
44                Some(GossipTopicTag::TxPreconfirmations)
45            }
46            _ => None,
47        }
48    }
49
50    /// Given a `GossipsubBroadcastRequest` returns a `TopicHash`
51    /// which is broadcast over the network with the serialized inner value of `GossipsubBroadcastRequest`
52    pub fn get_gossipsub_topic_hash(
53        &self,
54        outgoing_request: &GossipsubBroadcastRequest,
55    ) -> TopicHash {
56        match outgoing_request {
57            GossipsubBroadcastRequest::NewTx(_) => self.new_tx_topic.clone(),
58            GossipsubBroadcastRequest::TxPreConfirmations(_) => {
59                self.tx_preconfirmations_topic.clone()
60            }
61        }
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use fuel_core_types::fuel_tx::Transaction;
69    use libp2p::gossipsub::Topic;
70    use std::sync::Arc;
71
72    #[test]
73    fn test_gossipsub_topics() {
74        let network_name = "fuel_test_network";
75        let new_tx_topic: Sha256Topic =
76            Topic::new(format!("{NEW_TX_GOSSIP_TOPIC}/{network_name}"));
77
78        let gossipsub_topics = GossipsubTopics::new(network_name);
79
80        // Test matching Topic Hashes
81        assert_eq!(gossipsub_topics.new_tx_topic, new_tx_topic.hash());
82
83        // Test given a TopicHash that `get_gossipsub_tag()` returns matching `GossipTopicTag`
84        assert_eq!(
85            gossipsub_topics.get_gossipsub_tag(&new_tx_topic.hash()),
86            Some(GossipTopicTag::NewTx)
87        );
88
89        // Test given a `GossipsubBroadcastRequest` that `get_gossipsub_topic_hash()` returns matching `TopicHash`
90        let broadcast_req =
91            GossipsubBroadcastRequest::NewTx(Arc::new(Transaction::default_test_tx()));
92        assert_eq!(
93            gossipsub_topics.get_gossipsub_topic_hash(&broadcast_req),
94            new_tx_topic.hash()
95        );
96    }
97}