fuel_p2p/gossipsub/
config.rs

1use crate::config::P2PConfig;
2use fuel_metrics::p2p_metrics::P2P_METRICS;
3use libp2p::gossipsub::{
4    metrics::Config as MetricsConfig,
5    FastMessageId,
6    Gossipsub,
7    GossipsubConfig,
8    GossipsubConfigBuilder,
9    GossipsubMessage,
10    MessageAuthenticity,
11    MessageId,
12    PeerScoreParams,
13    PeerScoreThresholds,
14    RawGossipsubMessage,
15};
16use prometheus_client::registry::Registry;
17use sha2::{
18    Digest,
19    Sha256,
20};
21
22/// Creates `GossipsubConfigBuilder` with few of the Gossipsub values already defined
23pub fn default_gossipsub_builder() -> GossipsubConfigBuilder {
24    let gossip_message_id = move |message: &GossipsubMessage| {
25        MessageId::from(&Sha256::digest(&message.data)[..])
26    };
27
28    let fast_gossip_message_id = move |message: &RawGossipsubMessage| {
29        FastMessageId::from(&Sha256::digest(&message.data)[..])
30    };
31
32    let mut builder = GossipsubConfigBuilder::default();
33
34    builder
35        .protocol_id_prefix("/meshsub/1.0.0")
36        .message_id_fn(gossip_message_id)
37        .fast_message_id_fn(fast_gossip_message_id)
38        .validate_messages();
39
40    builder
41}
42
43/// Builds a default `GossipsubConfig`.
44/// Used in testing.
45pub(crate) fn default_gossipsub_config() -> GossipsubConfig {
46    default_gossipsub_builder()
47        .mesh_n(6)
48        .mesh_n_low(4)
49        .mesh_n_high(12)
50        .build()
51        .expect("valid gossipsub configuration")
52}
53
54/// Given a `P2pConfig` containing `GossipsubConfig` creates a Gossipsub Behaviour
55pub(crate) fn build_gossipsub_behaviour(p2p_config: &P2PConfig) -> Gossipsub {
56    if p2p_config.metrics {
57        // Move to Metrics related feature flag
58        let mut p2p_registry = Registry::default();
59
60        let metrics_config = MetricsConfig::default();
61
62        let mut gossipsub = Gossipsub::new_with_metrics(
63            MessageAuthenticity::Signed(p2p_config.keypair.clone()),
64            p2p_config.gossipsub_config.clone(),
65            &mut p2p_registry,
66            metrics_config,
67        )
68        .expect("gossipsub initialized");
69
70        // This couldn't be set unless multiple p2p services are running? So it's ok to unwrap
71        P2P_METRICS
72            .gossip_sub_registry
73            .set(Box::new(p2p_registry))
74            .unwrap_or(());
75
76        gossipsub
77            .with_peer_score(PeerScoreParams::default(), PeerScoreThresholds::default())
78            .expect("gossipsub initialized with peer score");
79
80        gossipsub
81    } else {
82        let mut gossipsub = Gossipsub::new(
83            MessageAuthenticity::Signed(p2p_config.keypair.clone()),
84            p2p_config.gossipsub_config.clone(),
85        )
86        .expect("gossipsub initialized");
87
88        gossipsub
89            .with_peer_score(PeerScoreParams::default(), PeerScoreThresholds::default())
90            .expect("gossipsub initialized with peer score");
91
92        gossipsub
93    }
94}