-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmod.rs
More file actions
561 lines (499 loc) · 22 KB
/
mod.rs
File metadata and controls
561 lines (499 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
mod curio;
mod eth_acc_funding;
mod foc_deploy;
mod foc_deployer;
mod foc_metadata;
mod genesis;
mod lotus;
mod lotus_miner;
mod lotus_utils;
mod multicall3_deploy;
mod pdp_service_provider;
pub mod step;
mod synapse_test_e2e;
mod usdfc_deploy;
mod usdfc_funding;
mod yugabyte;
use curio::CurioStep;
use eth_acc_funding::ETHAccFundingStep;
use foc_deploy::FOCDeployStep;
pub use genesis::ensure_genesis_prerequisites;
use lotus::LotusStep;
use lotus_miner::LotusMinerStep;
use multicall3_deploy::MultiCall3DeployStep;
use pdp_service_provider::PdpSpRegistrationStep;
pub use step::{execute_steps, execute_steps_parallel, SetupContext, Step};
use synapse_test_e2e::SynapseTestE2EStep;
use usdfc_deploy::USDFCDeployStep;
use yugabyte::YugabyteStep;
use crate::commands::start::usdfc_funding::USDFCFundingStep;
use crate::config::Config;
use crate::docker::core::{container_is_running, remove_container, stop_container};
use crate::docker::{create_all_networks, start_portainer};
use crate::paths::{foc_devnet_config, foc_devnet_run_dir};
use crate::run_id::{create_latest_symlink, save_current_run_id};
use crate::version_info::write_version_file;
pub use eth_acc_funding::constants::FEVM_ACCOUNTS_PREFUNDED;
use std::net::ToSocketAddrs;
use std::path::{Path, PathBuf};
use tracing::{error, info, warn};
/// Check that host.docker.internal resolves to 127.0.0.1.
///
/// This is required for SP-to-SP fetch to work. The hostname must resolve to localhost
/// so that URLs registered in the SP registry work from both the host and inside containers.
///
/// On macOS with Docker Desktop, this works automatically.
/// On Linux, users must add `127.0.0.1 host.docker.internal` to /etc/hosts.
fn check_host_docker_internal() -> Result<(), Box<dyn std::error::Error>> {
info!("Checking host.docker.internal resolution...");
// Try to resolve host.docker.internal:80 (port doesn't matter, just need DNS resolution)
match "host.docker.internal:80".to_socket_addrs() {
Ok(mut addrs) => {
// Check if any resolved address is 127.0.0.1
let is_localhost = addrs.any(|addr| addr.ip().is_loopback());
if is_localhost {
info!("✓ host.docker.internal resolves to localhost");
Ok(())
} else {
error!("════════════════════════════════════════════════════════════════════");
error!("ERROR: host.docker.internal does not resolve to localhost (127.0.0.1)");
error!("════════════════════════════════════════════════════════════════════");
error!("");
error!("SP-to-SP fetch requires host.docker.internal to resolve to 127.0.0.1");
error!("so that registered SP URLs work from both host and containers.");
error!("");
error!("To fix this, add the following line to /etc/hosts:");
error!("");
error!(" 127.0.0.1 host.docker.internal");
error!("");
error!("You can do this with:");
error!(" echo '127.0.0.1 host.docker.internal' | sudo tee -a /etc/hosts");
error!("");
error!("════════════════════════════════════════════════════════════════════");
Err("host.docker.internal must resolve to 127.0.0.1".into())
}
}
Err(_) => {
error!("════════════════════════════════════════════════════════════════════");
error!("ERROR: host.docker.internal does not resolve");
error!("════════════════════════════════════════════════════════════════════");
error!("");
error!("SP-to-SP fetch requires host.docker.internal to resolve to 127.0.0.1");
error!("so that registered SP URLs work from both host and containers.");
error!("");
error!("Add the following line to /etc/hosts:");
error!("");
error!(" 127.0.0.1 host.docker.internal");
error!("");
error!("You can do this with:");
error!(" echo '127.0.0.1 host.docker.internal' | sudo tee -a /etc/hosts");
error!("");
error!("For GitHub Actions, add this step before running foc-devnet:");
error!(" - run: echo '127.0.0.1 host.docker.internal' | sudo tee -a /etc/hosts");
error!("");
error!("════════════════════════════════════════════════════════════════════");
Err("host.docker.internal must be resolvable".into())
}
}
}
/// Stop any existing cluster before starting a new one.
fn stop_existing_cluster() -> Result<(), Box<dyn std::error::Error>> {
info!("Ensuring clean state by stopping any existing cluster...");
if let Err(e) = crate::commands::stop::stop_cluster() {
warn!("Warning: Failed to stop existing cluster: {}", e);
info!("Continuing with startup...");
}
Ok(())
}
/// Setup directories, run ID, and version information for the cluster startup.
fn setup_directories_and_run_id(
volumes_dir: Option<String>,
_run_dir: Option<String>,
run_id: String,
) -> Result<(PathBuf, PathBuf, String), Box<dyn std::error::Error>> {
// Save run ID to persistent storage
save_current_run_id(&run_id)?;
// Determine volumes directory
let volumes_dir = if let Some(dir) = volumes_dir {
PathBuf::from(dir)
} else {
crate::paths::foc_devnet_docker_volumes_run_specific(&run_id)
};
// Determine run directory
let run_dir = foc_devnet_run_dir(&run_id);
// Create directories if they don't exist
std::fs::create_dir_all(&volumes_dir)?;
std::fs::create_dir_all(&run_dir)?;
// Write version information to the run directory
let version_info = crate::version_info::VersionInfo::from_env();
write_version_file(&run_dir, &version_info)?;
// Create symlink from state/latest to this run directory for easier access
create_latest_symlink(&run_id)?;
Ok((volumes_dir, run_dir, run_id))
}
/// Stop any running containers from previous runs.
///
/// Note: We do NOT delete old run volumes or directories since each run has
/// a unique run ID. Old runs are preserved for historical reference and debugging.
fn stop_running_containers() -> Result<(), Box<dyn std::error::Error>> {
info!("Stopping any running containers from previous runs...");
let containers = vec![
crate::constants::LOTUS_MINER_CONTAINER,
crate::constants::LOTUS_CONTAINER,
crate::constants::CURIO_CONTAINER,
crate::constants::YUGABYTE_CONTAINER,
];
for container in containers {
if container_is_running(container)? {
info!("Stopping container '{}'...", container);
stop_container(container)?;
remove_container(container)?;
}
}
info!("All running containers stopped.");
Ok(())
}
/// Perform legacy full regenesis (deletes ALL runs - deprecated).
///
/// This function is kept for backward compatibility but should not be used
/// since it defeats the purpose of run IDs.
#[allow(dead_code)]
fn perform_regenesis_legacy() -> Result<(), Box<dyn std::error::Error>> {
warn!("Legacy regenesis called - this deletes ALL previous runs!");
let run_specific_volumes_root = crate::paths::foc_devnet_docker_volumes_run_specific_root();
let runs_dir = crate::paths::foc_devnet_runs();
// Files and directories to delete (ALL runs)
let paths_to_delete = vec![run_specific_volumes_root, runs_dir];
for path in paths_to_delete {
if path.exists() {
let result = if path.is_dir() {
std::fs::remove_dir_all(&path)
} else {
std::fs::remove_file(&path)
};
if let Err(e) = result {
if e.kind() == std::io::ErrorKind::PermissionDenied {
warn!(
"Permission denied removing {}, trying with Docker...",
path.display()
);
// Fallback to Docker
let parent = path.parent().unwrap_or_else(|| std::path::Path::new("/"));
let file_name = path.file_name().unwrap().to_string_lossy();
let status = std::process::Command::new("docker")
.args([
"run",
"-u",
"root",
"-v",
&format!("{}:/work", parent.display()),
crate::constants::BUILDER_DOCKER_IMAGE,
"rm",
"-rf",
&format!("/work/{}", file_name),
])
.status()?;
if status.success() {
info!("Removed with Docker: {}", path.display());
} else {
return Err(format!(
"Failed to remove {} even with Docker",
path.display()
)
.into());
}
} else {
return Err(e.into());
}
} else {
info!("Removed: {}", path.display());
}
} else {
info!("Skipped (not found): {}", path.display());
}
}
info!("Regenesis complete.");
Ok(())
}
/// Load and validate the configuration file.
fn load_and_validate_config() -> Result<Config, Box<dyn std::error::Error>> {
// Load config to get port range settings
let config_path = foc_devnet_config();
let config_content = std::fs::read_to_string(&config_path).map_err(|e| {
format!(
"Failed to read config file at {:?}: {}. Run 'foc-devnet init' first.",
config_path, e
)
})?;
let config: Config = toml::from_str(&config_content)
.map_err(|e| format!("Failed to parse config file at {:?}: {}", config_path, e))?;
// Validate config
config.validate()?;
// Display PDP SP configuration
info!("PDP Service Provider Configuration:");
info!("• Active PDP SPs: {}", config.active_pdp_sp_count);
info!("• Approved PDP SPs: {}", config.approved_pdp_sp_count);
Ok(config)
}
/// Create all the step instances for the cluster startup sequence.
fn create_steps(
volumes_dir: &Path,
run_dir: &Path,
config: &Config,
notest: bool,
) -> Vec<Box<dyn Step>> {
let lotus_step = LotusStep::new(volumes_dir.to_path_buf(), run_dir.to_path_buf());
let lotus_miner_step = LotusMinerStep::new(volumes_dir.to_path_buf(), run_dir.to_path_buf());
let eth_acc_funding_step =
ETHAccFundingStep::new(run_dir.to_path_buf(), config.active_pdp_sp_count);
let usdfc_deploy_step = USDFCDeployStep::new(volumes_dir.to_path_buf(), run_dir.to_path_buf());
let usdfc_funding_step =
USDFCFundingStep::new(run_dir.to_path_buf(), config.active_pdp_sp_count);
let multicall3_deploy_step =
MultiCall3DeployStep::new(volumes_dir.to_path_buf(), run_dir.to_path_buf());
let foc_deploy_step = FOCDeployStep::new(volumes_dir.to_path_buf(), run_dir.to_path_buf());
let pdp_sp_reg_step = PdpSpRegistrationStep::new(
volumes_dir.to_path_buf(),
run_dir.to_path_buf(),
config.active_pdp_sp_count,
config.approved_pdp_sp_count,
);
let yugabyte_step = YugabyteStep::new(
volumes_dir.to_path_buf(),
run_dir.to_path_buf(),
config.active_pdp_sp_count,
);
let curio_step = CurioStep::new(
volumes_dir.to_path_buf(),
run_dir.to_path_buf(),
config.active_pdp_sp_count,
);
let synapse_test_step =
SynapseTestE2EStep::new(volumes_dir.to_path_buf(), run_dir.to_path_buf(), notest);
// Execute all steps
// Note: PDP SP registration MUST happen after Curio because it needs
// the dynamic ports allocated to each Curio instance's PDP endpoint
vec![
Box::new(lotus_step),
Box::new(lotus_miner_step),
Box::new(eth_acc_funding_step),
Box::new(usdfc_deploy_step),
Box::new(usdfc_funding_step),
Box::new(multicall3_deploy_step),
Box::new(foc_deploy_step),
Box::new(yugabyte_step),
Box::new(curio_step),
Box::new(pdp_sp_reg_step),
Box::new(synapse_test_step),
]
}
/// Create step epochs for parallel execution.
///
/// Each epoch contains steps that can be executed in parallel.
/// All steps in an epoch must complete before the next epoch begins.
///
/// # Parallelization Strategy
///
/// - Epoch 1: Lotus + Yugabyte (independent services)
/// - Epoch 2: Lotus Miner (depends on Lotus)
/// - Epoch 3: ETH Account Funding (needs blockchain running)
/// - Epoch 4: MockUSDFC Deploy + MultiCall3 Deploy + FOC Deploy (can be parallelized)
/// - Epoch 5: MockUSDFC Funding + Curio daemons (can be parallelized, needs FOC Deploy)
/// - Epoch 6: PDP SP Registration (needs Curio daemons started)
fn create_step_epochs(
volumes_dir: &Path,
run_dir: &Path,
config: &Config,
notest: bool,
) -> Vec<Vec<Box<dyn Step>>> {
let lotus_step = LotusStep::new(volumes_dir.to_path_buf(), run_dir.to_path_buf());
let yugabyte_step = YugabyteStep::new(
volumes_dir.to_path_buf(),
run_dir.to_path_buf(),
config.active_pdp_sp_count,
);
let lotus_miner_step = LotusMinerStep::new(volumes_dir.to_path_buf(), run_dir.to_path_buf());
let eth_acc_funding_step =
ETHAccFundingStep::new(run_dir.to_path_buf(), config.active_pdp_sp_count);
let usdfc_deploy_step = USDFCDeployStep::new(volumes_dir.to_path_buf(), run_dir.to_path_buf());
let multicall3_deploy_step =
MultiCall3DeployStep::new(volumes_dir.to_path_buf(), run_dir.to_path_buf());
let foc_deploy_step = FOCDeployStep::new(volumes_dir.to_path_buf(), run_dir.to_path_buf());
let usdfc_funding_step =
USDFCFundingStep::new(run_dir.to_path_buf(), config.active_pdp_sp_count);
let curio_step = CurioStep::new(
volumes_dir.to_path_buf(),
run_dir.to_path_buf(),
config.active_pdp_sp_count,
);
let pdp_sp_reg_step = PdpSpRegistrationStep::new(
volumes_dir.to_path_buf(),
run_dir.to_path_buf(),
config.active_pdp_sp_count,
config.approved_pdp_sp_count,
);
let synapse_test_step =
SynapseTestE2EStep::new(volumes_dir.to_path_buf(), run_dir.to_path_buf(), notest);
vec![
// Epoch 1: Start Lotus
vec![Box::new(lotus_step)],
// Epoch 2: Start Lotus Miner (depends on Lotus)
vec![Box::new(lotus_miner_step)],
// Epoch 3: ETH Account Funding (needs blockchain running)
vec![Box::new(eth_acc_funding_step)],
// Epoch 4: Deploy contracts (can be parallelized)
vec![
Box::new(usdfc_deploy_step),
Box::new(multicall3_deploy_step),
],
// Epoch 5: Fund accounts with USDFC, deploy foc (needs usdfc deployed), start yugabyte for curio later
vec![
Box::new(foc_deploy_step),
Box::new(usdfc_funding_step),
Box::new(yugabyte_step),
],
// Epoch 6: Start Curio daemons
vec![Box::new(curio_step)],
// Epoch 7: Register PDP SPs (needs Curio running, for port information)
vec![Box::new(pdp_sp_reg_step)],
// Epoch 8: Run Synapse E2E Test
vec![Box::new(synapse_test_step)],
]
}
/// Execute the cluster startup steps.
fn execute_cluster_steps(
volumes_dir: &Path,
run_dir: &Path,
run_id: &str,
config: &Config,
parallel: bool,
portainer_port: u16,
notest: bool,
) -> Result<(), Box<dyn std::error::Error>> {
// Ensure genesis prerequisites are ready (one-time setup, needs config for sector count)
ensure_genesis_prerequisites(config.active_pdp_sp_count, run_id)?;
// Validate configuration
config
.validate()
.map_err(|e| format!("Configuration validation failed: {}", e))?;
info!(
"Port allocation: {}-{} ({} ports total)",
config.port_range_start,
config.port_range_start + config.port_range_count - 1,
config.port_range_count
);
info!(
"PDP SPs: {} active ({} approved)",
config.active_pdp_sp_count, config.approved_pdp_sp_count
);
if parallel {
info!("Execution mode: PARALLEL (experimental)");
let step_epochs = create_step_epochs(volumes_dir, run_dir, config, notest);
// Convert Vec<Vec<Box<dyn Step>>> to Vec<Vec<&dyn Step>>
let epoch_refs: Vec<Vec<&dyn Step>> = step_epochs
.iter()
.map(|epoch| epoch.iter().map(|s| s.as_ref()).collect())
.collect();
execute_steps_parallel(
epoch_refs,
step::StepExecutionConfig {
run_id: run_id.to_string(),
run_dir: run_dir.to_path_buf(),
port_start: config.port_range_start,
port_count: config.port_range_count,
portainer_port: Some(portainer_port),
active_pdp_sp_count: config.active_pdp_sp_count,
approved_pdp_sp_count: config.approved_pdp_sp_count,
},
)?;
} else {
info!("Execution mode: SEQUENTIAL");
let steps = create_steps(volumes_dir, run_dir, config, notest);
execute_steps(
steps.iter().map(|s| s.as_ref()).collect::<Vec<_>>(),
step::StepExecutionConfig {
run_id: run_id.to_string(),
run_dir: run_dir.to_path_buf(),
port_start: config.port_range_start,
port_count: config.port_range_count,
portainer_port: Some(portainer_port),
active_pdp_sp_count: config.active_pdp_sp_count,
approved_pdp_sp_count: config.approved_pdp_sp_count,
},
)?;
}
Ok(())
}
/// Start the local Filecoin network cluster.
pub fn start_cluster(
volumes_dir: Option<String>,
run_dir: Option<String>,
parallel: bool,
run_id: String,
notest: bool,
) -> Result<(), Box<dyn std::error::Error>> {
// Check host.docker.internal resolution first (required for SP-to-SP fetch)
check_host_docker_internal()?;
stop_existing_cluster()?;
let (volumes_dir, run_dir, run_id) =
setup_directories_and_run_id(volumes_dir, run_dir, run_id)?;
// Stop any running containers (but preserve old run data)
stop_running_containers()?;
info!("Starting local cluster...");
info!("Run ID: {}", run_id);
info!("Volumes directory: {}", volumes_dir.display());
info!("Run directory: {}", run_dir.display());
// Log system information
crate::utils::system_info::log_system_info();
let config = load_and_validate_config()?;
// Allocate port for Portainer (first port in dynamic range)
let mut port_allocator = crate::port_allocator::PortAllocator::new(
config.port_range_start,
config.port_range_count,
)?;
let portainer_port = port_allocator.allocate()?;
// Start Portainer
start_portainer(&run_id, portainer_port)?;
// Create networks
create_all_networks(&run_id, config.active_pdp_sp_count)?;
// Execute steps
let exec_result = execute_cluster_steps(
&volumes_dir,
&run_dir,
&run_id,
&config,
parallel,
portainer_port,
notest,
);
// Always run post-start teardown: persist logs, cleanup dead containers, write status
if let Err(e) = finalize_start_teardown(&run_id) {
warn!("Post-start teardown encountered an error: {}", e);
}
// Propagate original execution result
exec_result?;
info!("Cluster started successfully!");
Ok(())
}
/// Finalize the start attempt by collecting logs, cleaning dead containers, and writing status.
fn finalize_start_teardown(run_id: &str) -> Result<(), Box<dyn std::error::Error>> {
use crate::docker::{
persist_foc_container_logs, remove_dead_foc_containers, write_post_start_status_log,
};
info!("═══════════════════════════════════════════════════════════");
info!("Running post-start teardown for run ID: {}", run_id);
info!("═══════════════════════════════════════════════════════════");
// Persist logs for all foc* image containers
info!("[1/3] Persisting logs for all foc* image containers...");
persist_foc_container_logs(run_id)?;
// Remove dead containers to keep environment tidy
info!("[2/3] Removing dead foc* containers...");
remove_dead_foc_containers()?;
// Write status snapshot to the run directory
info!("[3/3] Writing post-start status snapshot...");
write_post_start_status_log(run_id)?;
info!("═══════════════════════════════════════════════════════════");
info!("Post-start teardown completed successfully");
info!("═══════════════════════════════════════════════════════════");
Ok(())
}