mina_node_testing/node/rust/
config.rs

1use std::{fs::File, path::Path, sync::Arc};
2
3use node::{
4    account::AccountSecretKey, config::DEVNET_CONFIG, p2p::P2pTimeouts,
5    transition_frontier::genesis::GenesisConfig, BlockProducerConfig, SnarkerConfig,
6};
7use serde::{Deserialize, Serialize};
8
9use crate::scenario::ListenerNode;
10
11#[derive(Serialize, Deserialize, Debug, Default, Clone)]
12pub enum TestPeerId {
13    /// NOTE This option results a deterministic private key derived from the
14    /// node index in the cluster. Be aware that when interacting with OCaml
15    /// nodes or other nodes outside the cluster might be interfer by previous
16    /// runs (e.g. peer_id might be blacklisted).
17    #[default]
18    Derived,
19    Bytes([u8; 32]),
20}
21
22#[derive(Serialize, Deserialize, Debug, Clone)]
23pub struct RustNodeTestingConfig {
24    pub initial_time: redux::Timestamp,
25    pub genesis: Arc<GenesisConfig>,
26    pub max_peers: usize,
27    #[serde(default)]
28    pub initial_peers: Vec<ListenerNode>,
29    #[serde(default)]
30    pub peer_id: TestPeerId,
31    #[serde(default)]
32    pub snark_worker: Option<SnarkerConfig>,
33    #[serde(default)]
34    pub block_producer: Option<RustNodeBlockProducerTestingConfig>,
35    #[serde(default)]
36    pub timeouts: P2pTimeouts,
37    #[serde(default)]
38    pub libp2p_port: Option<u16>,
39    #[serde(default)]
40    pub recorder: Recorder,
41    pub peer_discovery: bool,
42}
43
44#[derive(Serialize, Deserialize, Debug, Default, Clone)]
45pub enum Recorder {
46    #[default]
47    None,
48    StateWithInputActions,
49}
50
51#[derive(Serialize, Deserialize, Debug, Clone)]
52pub struct RustNodeBlockProducerTestingConfig {
53    pub sec_key: AccountSecretKey,
54    pub config: BlockProducerConfig,
55}
56
57impl RustNodeTestingConfig {
58    pub fn devnet_default() -> Self {
59        Self {
60            initial_time: redux::Timestamp::ZERO,
61            genesis: DEVNET_CONFIG.clone(),
62            max_peers: 100,
63            initial_peers: Vec::new(),
64            peer_id: TestPeerId::default(),
65            block_producer: None,
66            snark_worker: None,
67            timeouts: P2pTimeouts::default(),
68            libp2p_port: None,
69            recorder: Default::default(),
70            peer_discovery: true,
71        }
72    }
73
74    pub fn devnet_default_no_rpc_timeouts() -> Self {
75        Self {
76            initial_time: redux::Timestamp::ZERO,
77            genesis: DEVNET_CONFIG.clone(),
78            max_peers: 100,
79            initial_peers: Vec::new(),
80            peer_id: TestPeerId::default(),
81            block_producer: None,
82            snark_worker: None,
83            timeouts: P2pTimeouts::without_rpc(),
84            libp2p_port: None,
85            recorder: Default::default(),
86            peer_discovery: true,
87        }
88    }
89
90    pub fn max_peers(mut self, n: usize) -> Self {
91        self.max_peers = n;
92        self
93    }
94
95    pub fn initial_peers(mut self, v: Vec<ListenerNode>) -> Self {
96        self.initial_peers = v;
97        self
98    }
99
100    pub fn initial_time(mut self, time: redux::Timestamp) -> Self {
101        self.initial_time = time;
102        self
103    }
104
105    pub fn with_peer_id(mut self, bytes: [u8; 32]) -> Self {
106        self.peer_id = TestPeerId::Bytes(bytes);
107        self
108    }
109
110    pub fn with_timeouts(mut self, timeouts: P2pTimeouts) -> Self {
111        self.timeouts = timeouts;
112        self
113    }
114
115    pub fn with_libp2p_port(mut self, libp2p_port: u16) -> Self {
116        self.libp2p_port = Some(libp2p_port);
117        self
118    }
119
120    pub fn with_daemon_json<P: AsRef<Path>>(mut self, daemon_json: P) -> Self {
121        self.genesis = Arc::new(GenesisConfig::DaemonJson(
122            serde_json::from_reader(&mut File::open(daemon_json).expect("daemon json file"))
123                .expect("daemon json"),
124        ));
125        self
126    }
127
128    pub fn with_no_peer_discovery(mut self) -> Self {
129        self.peer_discovery = false;
130        self
131    }
132}