Skip to content

Commit 48403b6

Browse files
vid277cdxker
authored andcommitted
cleanup: overwrite prev config with cli options
1 parent 4161e63 commit 48403b6

9 files changed

Lines changed: 113 additions & 77 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ Cargo.lock
6161
.env
6262
visuals/nodes
6363
visuals/manim/__pycache__
64-
visuals/manim/media/
64+
visuals/manim/media/
65+
66+
tests/config.json
67+
tests/config.toml

bin/cli/src/main.rs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,19 @@ enum Commands {
138138
key_file_path: Option<String>,
139139
#[arg(short, long)]
140140
config_file_path: Option<String>,
141-
#[arg(short, long)]
141+
#[arg(short = 'p', long)]
142142
grpc_port: Option<u16>,
143143
#[arg(short = 'u', long)]
144144
libp2p_udp_port: Option<u16>,
145-
#[arg(short, long)]
145+
#[arg(short = 't', long)]
146146
libp2p_tcp_port: Option<u16>,
147147
#[arg(short, long)]
148148
database_directory: Option<String>,
149149
#[arg(short = 'o', long)]
150150
log_file: Option<String>,
151151
#[arg(short = 'n', long)]
152152
max_signers: Option<u16>,
153-
#[arg(short = 't', long)]
153+
#[arg(short = 'm', long)]
154154
min_signers: Option<u16>,
155155
},
156156
Spend {
@@ -201,7 +201,7 @@ async fn main() -> Result<(), CliError> {
201201
max_signers,
202202
min_signers,
203203
} => {
204-
start_node_cli(
204+
start_node_cli(StartNodeConfigParams {
205205
key_file_path,
206206
config_file_path,
207207
grpc_port,
@@ -211,7 +211,7 @@ async fn main() -> Result<(), CliError> {
211211
log_file,
212212
max_signers,
213213
min_signers,
214-
)
214+
})
215215
.await
216216
.map_err(|e| CliError::NodeError(e.to_string()))?;
217217
}
@@ -295,6 +295,10 @@ fn setup_config(output_dir: Option<String>, file_name: Option<String>) -> Result
295295
.map_err(|e| KeygenError::KeyFileNotFound(e.to_string()))?;
296296

297297
config.set_key_data(key_data);
298+
config.set_grpc_port(50051);
299+
config.set_libp2p_udp_port(0);
300+
config.set_libp2p_tcp_port(0);
301+
config.set_database_directory(PathBuf::from("nodedb.db"));
298302

299303
config
300304
.save_to_file()
@@ -310,7 +314,7 @@ fn setup_config(output_dir: Option<String>, file_name: Option<String>) -> Result
310314
Ok(())
311315
}
312316

313-
async fn start_node_cli(
317+
struct StartNodeConfigParams {
314318
key_file_path: Option<String>,
315319
config_file_path: Option<String>,
316320
grpc_port: Option<u16>,
@@ -320,23 +324,41 @@ async fn start_node_cli(
320324
log_file: Option<String>,
321325
max_signers: Option<u16>,
322326
min_signers: Option<u16>,
323-
) -> Result<(), NodeError> {
324-
let config = match get_config(key_file_path.clone(), config_file_path.clone()) {
327+
}
328+
329+
async fn start_node_cli(params: StartNodeConfigParams) -> Result<(), NodeError> {
330+
let mut config = match get_config(
331+
params.key_file_path.clone(),
332+
params.config_file_path.clone(),
333+
) {
325334
Ok(config) => config,
326335
Err(e) => {
327336
return Err(NodeError::Error(format!("Failed to get config: {}", e)));
328337
}
329338
};
330339

340+
if let Some(port) = params.grpc_port {
341+
config.set_grpc_port(port);
342+
}
343+
344+
if let Some(port) = params.libp2p_udp_port {
345+
config.set_libp2p_udp_port(port);
346+
}
347+
348+
if let Some(port) = params.libp2p_tcp_port {
349+
config.set_libp2p_tcp_port(port);
350+
}
351+
352+
if let Some(dir) = params.database_directory {
353+
config.set_database_directory(PathBuf::from(dir));
354+
}
355+
331356
start_node(
332-
max_signers,
333-
min_signers,
357+
params.max_signers,
358+
params.min_signers,
334359
config,
335-
grpc_port,
336-
log_file.map(PathBuf::from),
337-
libp2p_udp_port,
338-
libp2p_tcp_port,
339-
database_directory.map(PathBuf::from),
360+
params.grpc_port,
361+
params.log_file.map(PathBuf::from),
340362
)
341363
.await?;
342364
Ok(())

crates/node/src/handlers/dkg/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl DkgState {
140140

141141
node.db.insert_block(genesis_block.to_block())?;
142142

143-
node.config.save_to_file()?;
143+
node.config.save_to_keys_file()?;
144144
Ok(())
145145
}
146146
}

crates/node/src/lib.rs

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ pub struct NodeConfig {
6868
pub dkg_keys: Option<DkgKeys>,
6969
pub log_file_path: Option<PathBuf>,
7070
#[serde(skip)]
71-
key_file_path: PathBuf,
71+
pub key_file_path: PathBuf,
7272
#[serde(skip)]
73-
config_file_path: PathBuf,
74-
database_directory: Option<PathBuf>,
75-
grpc_port: Option<u16>,
76-
libp2p_udp_port: Option<u16>,
77-
libp2p_tcp_port: Option<u16>,
73+
pub config_file_path: PathBuf,
74+
pub database_directory: PathBuf,
75+
pub grpc_port: u16,
76+
pub libp2p_udp_port: u16,
77+
pub libp2p_tcp_port: u16,
7878
}
7979

8080
#[derive(Serialize, Deserialize)]
@@ -88,10 +88,10 @@ pub struct ConfigStore {
8888
allowed_peers: Vec<PeerData>,
8989
log_file_path: Option<PathBuf>,
9090
key_file_path: PathBuf,
91-
database_directory: Option<PathBuf>,
92-
grpc_port: Option<u16>,
93-
libp2p_udp_port: Option<u16>,
94-
libp2p_tcp_port: Option<u16>,
91+
database_directory: PathBuf,
92+
grpc_port: u16,
93+
libp2p_udp_port: u16,
94+
libp2p_tcp_port: u16,
9595
}
9696

9797
impl NodeConfig {
@@ -156,11 +156,26 @@ impl NodeConfig {
156156
log_file_path,
157157
key_file_path,
158158
config_file_path,
159-
database_directory: None,
160-
grpc_port: None,
161-
libp2p_udp_port: None,
162-
libp2p_tcp_port: None,
163-
})
159+
database_directory: PathBuf::from("nodedb.db"),
160+
grpc_port: 50051,
161+
libp2p_udp_port: 0,
162+
libp2p_tcp_port: 0,
163+
})
164+
}
165+
166+
pub fn save_to_keys_file(&self) -> Result<(), NodeError> {
167+
let key_store = KeyStore {
168+
key_data: self.key_data.clone(),
169+
dkg_keys: self.dkg_keys.clone(),
170+
};
171+
172+
let key_info_str = serde_json::to_string_pretty(&key_store)
173+
.map_err(|e| NodeError::Error(format!("Failed to serialize key data: {}", e)))?;
174+
175+
fs::write(&self.key_file_path, key_info_str)
176+
.map_err(|e| NodeError::Error(format!("Failed to write key data: {}", e)))?;
177+
178+
Ok(())
164179
}
165180

166181
pub fn save_to_file(&self) -> Result<(), NodeError> {
@@ -180,9 +195,9 @@ impl NodeConfig {
180195
log_file_path: self.log_file_path.clone(),
181196
key_file_path: self.key_file_path.clone(),
182197
database_directory: self.database_directory.clone(),
183-
grpc_port: self.grpc_port.clone(),
184-
libp2p_udp_port: self.libp2p_udp_port.clone(),
185-
libp2p_tcp_port: self.libp2p_tcp_port.clone(),
198+
grpc_port: self.grpc_port,
199+
libp2p_udp_port: self.libp2p_udp_port,
200+
libp2p_tcp_port: self.libp2p_tcp_port,
186201
};
187202

188203
let config_str: String = serde_yaml::to_string(&config_store).unwrap();
@@ -200,6 +215,22 @@ impl NodeConfig {
200215
pub fn set_key_data(&mut self, key_data: KeyData) {
201216
self.key_data = key_data;
202217
}
218+
219+
pub fn set_grpc_port(&mut self, port: u16) {
220+
self.grpc_port = port;
221+
}
222+
223+
pub fn set_libp2p_udp_port(&mut self, port: u16) {
224+
self.libp2p_udp_port = port;
225+
}
226+
227+
pub fn set_libp2p_tcp_port(&mut self, port: u16) {
228+
self.libp2p_tcp_port = port;
229+
}
230+
231+
pub fn set_database_directory(&mut self, dir: PathBuf) {
232+
self.database_directory = dir;
233+
}
203234
}
204235

205236
pub struct NodeState<N: Network, D: Db, O: Oracle> {

crates/node/src/start_node.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@ pub async fn start_node(
1919
config: NodeConfig,
2020
grpc_port: Option<u16>,
2121
log_file: Option<PathBuf>,
22-
libp2p_udp_port: Option<u16>,
23-
libp2p_tcp_port: Option<u16>,
24-
database_directory: Option<PathBuf>,
2522
) -> Result<(), NodeError> {
2623
// Initialize logging
2724
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
25+
2826
let config_database_path = config.database_directory.clone();
29-
let config_grpc_port = config.grpc_port.clone();
30-
let config_libp2p_udp_port = config.libp2p_tcp_port.clone();
31-
let config_libp2p_tcp_port = config.libp2p_tcp_port.clone();
27+
let config_grpc_port = config.grpc_port;
3228

3329
let registry = tracing_subscriber::registry().with(env_filter);
3430

@@ -91,8 +87,8 @@ pub async fn start_node(
9187

9288
let (network_handle, mut swarm) = build_swarm(
9389
keypair.clone(),
94-
libp2p_udp_port.or(config_libp2p_udp_port),
95-
libp2p_tcp_port.or(config_libp2p_tcp_port),
90+
config.libp2p_udp_port,
91+
config.libp2p_tcp_port,
9692
allowed_peers.clone(),
9793
)
9894
.expect("Failed to build swarm");
@@ -105,11 +101,7 @@ pub async fn start_node(
105101
min_signers,
106102
max_signers,
107103
config,
108-
RocksDb::new(
109-
database_directory.or(config_database_path).unwrap_or(PathBuf::from("nodedb.db"))
110-
.to_str()
111-
.unwrap(),
112-
),
104+
RocksDb::new(config_database_path.to_str().unwrap()),
113105
swarm.network_events.clone(),
114106
deposit_intent_tx,
115107
transaction_rx,
@@ -124,7 +116,7 @@ pub async fn start_node(
124116
});
125117

126118
let grpc_handle = tokio::spawn(async move {
127-
let addr = format!("0.0.0.0:{}", grpc_port.or(config_grpc_port).unwrap_or(50051))
119+
let addr = format!("0.0.0.0:{}", grpc_port.unwrap_or(config_grpc_port))
128120
.parse()
129121
.unwrap();
130122

crates/node/src/utils/swarm_manager.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ impl SwarmManager {
402402

403403
pub fn build_swarm(
404404
keypair: Keypair,
405-
libp2p_udp_port: Option<u16>,
406-
libp2p_tcp_port: Option<u16>,
405+
libp2p_udp_port: u16,
406+
libp2p_tcp_port: u16,
407407
peer_data: Vec<PeerData>,
408408
) -> Result<(NetworkHandle, SwarmManager), NodeError> {
409409
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(keypair)
@@ -464,15 +464,15 @@ pub fn build_swarm(
464464

465465
swarm
466466
.listen_on(
467-
format!("/ip4/0.0.0.0/udp/{}/quic-v1", libp2p_udp_port.unwrap_or(0))
467+
format!("/ip4/0.0.0.0/udp/{}/quic-v1", libp2p_udp_port)
468468
.parse()
469469
.expect("Failed to deserialize message"),
470470
)
471471
.map_err(|e| NodeError::Error(format!("Failed to listen on quic {}", e)))?;
472472

473473
swarm
474474
.listen_on(
475-
format!("/ip4/0.0.0.0/tcp/{}", libp2p_tcp_port.unwrap_or(0))
475+
format!("/ip4/0.0.0.0/tcp/{}", libp2p_tcp_port)
476476
.parse()
477477
.expect("Failed to deserialize message"),
478478
)

tests/config.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/config.toml

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/src/config.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ mod config_test {
1919
"salt_b64": "TnErEFlx9F1BeU8mJcFzKQ",
2020
"iv_b64": "hybTge0qoPaxNUhP"
2121
}
22-
}
22+
},
23+
"database_directory": "nodedb.db",
24+
"grpc_port": 50051,
25+
"libp2p_udp_port": 0,
26+
"libp2p_tcp_port": 0
2327
}"#;
2428

2529
let config: NodeConfig = serde_json::from_str(json_str).expect("Failed to deserialize");
@@ -29,5 +33,12 @@ mod config_test {
2933
"12D3KooWQDHzW448RmDoUz1KbMfuD4XqeojRJDsxqUZSEYo7FSUz"
3034
);
3135
assert!(config.dkg_keys.is_none());
36+
assert_eq!(
37+
config.database_directory,
38+
std::path::PathBuf::from("nodedb.db")
39+
);
40+
assert_eq!(config.grpc_port, 50051);
41+
assert_eq!(config.libp2p_udp_port, 0);
42+
assert_eq!(config.libp2p_tcp_port, 0);
3243
}
3344
}

0 commit comments

Comments
 (0)