kona_node_service/actors/network/
config.rs

1//! Configuration for the `Network`.
2
3use alloy_primitives::Address;
4use discv5::Enr;
5use kona_genesis::RollupConfig;
6use kona_p2p::{GaterConfig, LocalNode};
7use kona_peers::{PeerMonitoring, PeerScoreLevel};
8use kona_sources::BlockSigner;
9use libp2p::{Multiaddr, identity::Keypair};
10use std::path::PathBuf;
11use tokio::time::Duration;
12
13/// Configuration for kona's P2P stack.
14#[derive(Debug, Clone)]
15pub struct NetworkConfig {
16    /// Discovery Config.
17    pub discovery_config: discv5::Config,
18    /// The local node's advertised address to external peers.
19    /// Note: This may be different from the node's discovery listen address.
20    pub discovery_address: LocalNode,
21    /// The interval to find peers.
22    pub discovery_interval: Duration,
23    /// The interval to remove peers from the discovery service.
24    pub discovery_randomize: Option<Duration>,
25    /// The gossip address.
26    pub gossip_address: libp2p::Multiaddr,
27    /// The unsafe block signer.
28    pub unsafe_block_signer: Address,
29    /// The keypair.
30    pub keypair: Keypair,
31    /// The gossip config.
32    pub gossip_config: libp2p::gossipsub::Config,
33    /// The peer score level.
34    pub scoring: PeerScoreLevel,
35    /// Whether to enable topic scoring.
36    pub topic_scoring: bool,
37    /// Peer score monitoring config.
38    pub monitor_peers: Option<PeerMonitoring>,
39    /// An optional path to the bootstore.
40    pub bootstore: Option<PathBuf>,
41    /// The configuration for the connection gater.
42    pub gater_config: GaterConfig,
43    /// An optional list of bootnode ENRs to start the node with.
44    pub bootnodes: Vec<Enr>,
45    /// The [`RollupConfig`].
46    pub rollup_config: RollupConfig,
47    /// A signer for gossip payloads.
48    pub gossip_signer: Option<BlockSigner>,
49}
50
51impl NetworkConfig {
52    const DEFAULT_DISCOVERY_INTERVAL: Duration = Duration::from_secs(5);
53    const DEFAULT_DISCOVERY_RANDOMIZE: Option<Duration> = None;
54
55    /// Creates a new [`NetworkConfig`] with the given [`RollupConfig`] with the minimum required
56    /// fields. Generates a random keypair for the node.
57    pub fn new(
58        rollup_config: RollupConfig,
59        discovery_listen: LocalNode,
60        gossip_address: Multiaddr,
61        unsafe_block_signer: Address,
62    ) -> Self {
63        Self {
64            rollup_config,
65            discovery_config: discv5::ConfigBuilder::new((&discovery_listen).into()).build(),
66            discovery_address: discovery_listen,
67            discovery_interval: Self::DEFAULT_DISCOVERY_INTERVAL,
68            discovery_randomize: Self::DEFAULT_DISCOVERY_RANDOMIZE,
69            gossip_address,
70            unsafe_block_signer,
71            keypair: Keypair::generate_secp256k1(),
72            bootnodes: Default::default(),
73            bootstore: Default::default(),
74            gater_config: Default::default(),
75            gossip_config: Default::default(),
76            scoring: Default::default(),
77            topic_scoring: Default::default(),
78            monitor_peers: Default::default(),
79            gossip_signer: Default::default(),
80        }
81    }
82}