-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.rs
More file actions
493 lines (438 loc) · 18.1 KB
/
config.rs
File metadata and controls
493 lines (438 loc) · 18.1 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
//! Configuration module for foc-devnet.
//!
//! This module defines the configuration structures used to manage the local
//! Filecoin on-chain cloud cluster. It includes settings for node counts,
//! port allocations, and executable locations for various components.
use serde::{Deserialize, Serialize};
/// Represents the location of an executable or source code for a component.
///
/// This enum allows specifying how to obtain and run different Filecoin-related
/// executables (lotus, lotus-miner, curio) in various deployment scenarios.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Location {
/// Use a local directory containing source code that needs to be built.
///
/// The `dir` field should point to a directory with the source code.
/// The application will handle building the executable from this source.
LocalSource { dir: String },
/// Fetch source code from a Git repository at a specific commit.
///
/// The `url` field is the Git repository URL, and `commit` is the specific
/// commit hash to check out. The application will clone the repo and
/// build the executable from this commit.
GitCommit { url: String, commit: String },
/// Fetch source code from a Git repository at a specific tag.
///
/// The `url` field is the Git repository URL, and `tag` is the specific
/// tag (e.g., "v1.2.3") to check out. This is useful for stable releases.
GitTag { url: String, tag: String },
/// Fetch source code from a Git repository at a specific branch.
///
/// The `url` field is the Git repository URL, and `branch` is the specific
/// branch (e.g., "main", "develop") to check out.
GitBranch { url: String, branch: String },
}
impl Location {
/// Given a url and a selector, finds the latest tag given that selector.
///
/// Runs `git ls-remote --tags --sort=-version:refname <url> <selector>` and
/// returns the first tag name from the output (i.e. the lexicographically newest
/// version-sorted tag that matches `selector`).
///
/// Example: `resolveLatestTag("https://github.com/foo/bar.git", "v*")` might
/// return `"v1.2.3"`.
fn resolve_latest_tag(url: &str, selector: &str) -> Result<String, String> {
let stdout = {
#[cfg(not(test))]
{
let output = std::process::Command::new("git")
.args([
"ls-remote",
"--tags",
"--sort=-version:refname",
url,
selector,
])
.output()
.map_err(|e| format!("Failed to run git ls-remote: {}", e))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(format!("git ls-remote failed: {}", stderr.trim()));
}
String::from_utf8_lossy(&output.stdout).into_owned()
}
#[cfg(test)]
{
String::from("0000000000000 refs/tags/v1.0.0")
}
};
let first_line = stdout
.lines()
.next()
.ok_or_else(|| format!("No tags found for selector '{}' at '{}'", selector, url))?;
// Output format: "<hash>\trefs/tags/<tag>"
let tag_ref = first_line
.split_whitespace()
.nth(1)
.ok_or_else(|| format!("Unexpected git ls-remote output: '{}'", first_line))?;
tag_ref
.strip_prefix("refs/tags/")
.map(|t| t.to_string())
.ok_or_else(|| format!("Unexpected tag ref format: '{}'", tag_ref))
}
/// Canonicalizes the location from a set of following variants:
///
/// - `latesttag` — newest tag matching `*` (uses default URL)
/// - `latesttag:<selector>` — newest tag matching selector (e.g. `latesttag:pdp/v*`)
/// - `latesttag:<url>:<selector>` — newest tag matching selector at a custom URL
/// - `gittag:<tag>` — (uses default URL)
/// - `gitcommit:<commit>` — (uses default URL)
/// - `gitbranch:<branch>` — (uses default URL)
/// - `local:<dir>`
/// - `gittag:<url>:<tag>`
/// - `gitcommit:<url>:<commit>`
/// - `gitbranch:<url>:<branch>`
///
/// to a smaller subset:
///
/// - `local:<dir>` -> `("local", "", "dir")`
/// - `gittag:<url>:<tag>` -> `("gittag", "url", "tag")`
/// - `gitcommit:<url>:<commit>` -> `("gitcommit", "url", "commit")`
/// - `gitbranch:<url>:<branch>` -> `("gitbranch", "url", "branch")`
fn canonicalize_location(
location: &str,
default_url: &str,
) -> Result<(String, String, String), String> {
// Special case: bare "latesttag" with no selector — implicitly matches all tags.
if location == "latesttag" {
let tag = Self::resolve_latest_tag(default_url, "*")?;
return Ok(("gittag".into(), default_url.into(), tag));
}
// We need to do this setup in two steps since otherwise
// "gittag:https://github.com/orgs/repo:v1.2.0" would not be parseable
// and split the <url> string itself in two parts
let (mut location_type, remaining) = location.split_once(':').ok_or_else(|| {
format!(
"Invalid location format: '{}'. Expected \
'latesttag:<selector>', or 'gittag/gitcommit/gitbranch/local:...'",
location
)
})?;
// If the remaining part contains another ':', the portion before the last ':'
// is the URL and everything after is the value (handles HTTPS URLs with colons).
let (url, mut selector) = if let Some(colon_pos) = remaining.rfind(':') {
(&remaining[..colon_pos], &remaining[colon_pos + 1..])
} else {
(default_url, remaining)
};
// Special case: latesttag resolution into GitTag
let resolved_tag; // must outlive the match below
if location_type == "latesttag" {
resolved_tag = Self::resolve_latest_tag(url, selector)?;
selector = resolved_tag.as_str();
location_type = "gittag";
}
Ok((location_type.into(), url.into(), selector.into()))
}
/// Parse a location string in the format "type:value" or "type:url:value".
/// May attempt to resolve `latesttag` if provided by reaching over the internet.
///
/// Supported formats:
/// - `latesttag` — newest tag (uses default URL, matches `*`)
/// - `latesttag:<selector>` — newest tag matching selector (e.g. `latesttag:pdp/v*`)
/// - `latesttag:<url>:<selector>` — newest tag matching selector at a custom URL
/// - `gittag:<tag>` — (uses default URL)
/// - `gitcommit:<commit>` — (uses default URL)
/// - `gitbranch:<branch>` — (uses default URL)
/// - `local:<dir>`
/// - `gittag:<url>:<tag>`
/// - `gitcommit:<url>:<commit>`
/// - `gitbranch:<url>:<branch>`
pub fn resolve_with_default(location: &str, default_url: &str) -> Result<Self, String> {
let canonical_location = Self::canonicalize_location(location, default_url)?;
let (_type, url, selector) = canonical_location;
match _type.as_ref() {
"local" => Ok(Location::LocalSource { dir: selector }),
"gittag" => Ok(Location::GitTag { url, tag: selector }),
"gitcommit" => Ok(Location::GitCommit {
url,
commit: selector,
}),
"gitbranch" => Ok(Location::GitBranch {
url,
branch: selector,
}),
_ => Err(format!(
"Unknown location type: {}. Supported types: local, gittag, gitcommit, gitbranch",
_type
)),
}
}
}
/// Main configuration structure for the foc-devnet application.
///
/// This struct contains all the settings needed to configure and run a local
/// Filecoin cluster for testing filecoin-onchain-cloud functionality. It includes
/// counts of different node types, port allocations, and locations for executables.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
/// Starting port number for the contiguous port range.
///
/// All ports used by the devnet will be dynamically allocated from a contiguous
/// range starting at this port. This ensures no port conflicts and allows
/// easy firewall configuration.
/// Default: 5700
pub port_range_start: u16,
/// Number of ports in the contiguous port range.
///
/// This defines the size of the port range available for allocation.
/// For example, with port_range_start=5700 and port_range_count=300,
/// ports 5700-5999 are reserved for the devnet.
/// Default: 100
pub port_range_count: u16,
/// Location specification for the lotus executable.
///
/// Defines how to obtain and run the lotus daemon executable.
/// See [`Location`] for available options.
pub lotus: Location,
/// Location specification for the curio executable.
///
/// Defines how to obtain and run the curio executable.
/// See [`Location`] for available options.
pub curio: Location,
/// Location specification for the filecoin-services repository.
///
/// Defines how to obtain the filecoin-services code, which contains
/// the FOC (Filecoin Onchain Contracts) deployment scripts needed by Curio.
/// See [`Location`] for available options.
pub filecoin_services: Location,
/// Location specification for the multicall3 repository.
///
/// Defines how to obtain the multicall3 code, which provides the
/// Multicall3 contract for batching multiple calls in a single transaction.
/// See [`Location`] for available options.
pub multicall3: Location,
/// URL to download Yugabyte database tarball.
///
/// This is the direct link to the Yugabyte tarball required for running curio.
/// The default URL is automatically selected based on system architecture:
/// - ARM64 (aarch64): yugabyte-2.25.1.0-b381-el8-aarch64.tar.gz
/// - x86_64: yugabyte-2.25.1.0-b381-linux-x86_64.tar.gz
pub yugabyte_download_url: String,
/// Number of approved PDP service providers.
///
/// This is the total number of Curio SPs that will be registered and approved
/// in the service provider registry. These SPs can accept storage deals.
/// Must satisfy: ENDORSED_PDP_SP_COUNT <= APPROVED_PDP_SP_COUNT <= ACTIVE_PDP_SP_COUNT <= MAX_PDP_SP_COUNT
/// Default: 1
pub approved_pdp_sp_count: usize,
/// Number of endorsed PDP service providers.
///
/// This is the number of approved SPs that will be endorsed in the endorsements contract.
/// Endorsed providers are a privileged subset of approved providers.
/// Must satisfy: ENDORSED_PDP_SP_COUNT <= APPROVED_PDP_SP_COUNT <= ACTIVE_PDP_SP_COUNT <= MAX_PDP_SP_COUNT
/// Default: 1
pub endorsed_pdp_sp_count: usize,
/// Number of active PDP service providers.
///
/// This is the total number of Curio SPs that will actually be started/running.
/// Some may be approved, some may not (for testing unapproved SP scenarios).
/// Total miners = 1 (lotus-miner) + ACTIVE_PDP_SP_COUNT (curio SPs)
/// Must satisfy: ENDORSED_PDP_SP_COUNT <= APPROVED_PDP_SP_COUNT <= ACTIVE_PDP_SP_COUNT <= MAX_PDP_SP_COUNT
/// Default: 1
pub active_pdp_sp_count: usize,
}
impl Default for Config {
/// Creates a default configuration with sensible defaults.
///
/// The default configuration sets up a minimal cluster with one of each
/// node type and assumes pre-built executables are available in standard
/// system locations (/usr/local/bin/).
///
/// The defaults should always use `GitCommit` or `GitTag` locations to ensure
/// reproducibility.
fn default() -> Self {
Self {
port_range_start: 5700,
port_range_count: 100,
lotus: Location::GitTag {
url: "https://github.com/filecoin-project/lotus.git".to_string(),
tag: "v1.34.4-rc1".to_string(),
},
curio: Location::GitCommit {
url: "https://github.com/filecoin-project/curio.git".to_string(),
commit: "4d53c8017ad345410adfd80794fd7518b49c9128".to_string(),
},
filecoin_services: Location::GitCommit {
url: "https://github.com/FilOzone/filecoin-services.git".to_string(),
commit: "2b247916ddd33e4112dc69fd3ea4fc88a3976f56".to_string(),
},
multicall3: Location::GitTag {
url: "https://github.com/mds1/multicall3.git".to_string(),
tag: "v3.1.0".to_string(),
},
yugabyte_download_url: Self::get_default_yugabyte_url(),
approved_pdp_sp_count: 2,
endorsed_pdp_sp_count: 1,
active_pdp_sp_count: 2,
}
}
}
impl Config {
/// Get the default YugabyteDB download URL based on system architecture.
///
/// Returns the appropriate YugabyteDB tarball URL for the current platform:
/// - el8-aarch64 for ARM64 systems (Apple Silicon, AWS Graviton, etc.)
/// - linux-x86_64 for x86_64 systems
fn get_default_yugabyte_url() -> String {
const YUGABYTE_VERSION: &str = "2.25.1.0";
const YUGABYTE_BUILD: &str = "b381";
let arch_suffix = if std::env::consts::ARCH == "aarch64" {
"el8-aarch64"
} else {
"linux-x86_64"
};
format!(
"https://software.yugabyte.com/releases/{}/yugabyte-{}-{}-{}.tar.gz",
YUGABYTE_VERSION, YUGABYTE_VERSION, YUGABYTE_BUILD, arch_suffix
)
}
/// Validate configuration values.
///
/// Ensures that:
/// - ENDORSED_PDP_SP_COUNT <= APPROVED_PDP_SP_COUNT <= ACTIVE_PDP_SP_COUNT <= MAX_PDP_SP_COUNT
pub fn validate(&self) -> Result<(), String> {
const MAX_PDP_SP_COUNT: usize = crate::constants::MAX_PDP_SP_COUNT;
if self.endorsed_pdp_sp_count > self.approved_pdp_sp_count {
return Err(format!(
"endorsed_pdp_sp_count ({}) cannot exceed approved_pdp_sp_count ({})",
self.endorsed_pdp_sp_count, self.approved_pdp_sp_count
));
}
if self.approved_pdp_sp_count > self.active_pdp_sp_count {
return Err(format!(
"approved_pdp_sp_count ({}) cannot exceed active_pdp_sp_count ({})",
self.approved_pdp_sp_count, self.active_pdp_sp_count
));
}
if self.active_pdp_sp_count > MAX_PDP_SP_COUNT {
return Err(format!(
"active_pdp_sp_count ({}) cannot exceed MAX_PDP_SP_COUNT ({})",
self.active_pdp_sp_count, MAX_PDP_SP_COUNT
));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::Location;
const DEFAULT_URL: &str = "https://github.com/default/repo.git";
fn canonicalize(s: &str) -> Result<(String, String, String), String> {
Location::canonicalize_location(s, DEFAULT_URL)
}
// --- happy-path tests ---
#[test]
fn gittag_short_uses_default_url() {
assert_eq!(
canonicalize("gittag:v1.2.3").unwrap(),
("gittag".into(), DEFAULT_URL.into(), "v1.2.3".into())
);
}
#[test]
fn gittag_explicit_https_url() {
assert_eq!(
canonicalize("gittag:https://github.com/foo/bar.git:v1.2.3").unwrap(),
(
"gittag".into(),
"https://github.com/foo/bar.git".into(),
"v1.2.3".into()
)
);
}
#[test]
fn gitcommit_short_uses_default_url() {
assert_eq!(
canonicalize("gitcommit:abc123def456").unwrap(),
(
"gitcommit".into(),
DEFAULT_URL.into(),
"abc123def456".into()
)
);
}
#[test]
fn gitcommit_explicit_https_url() {
assert_eq!(
canonicalize("gitcommit:https://github.com/foo/bar.git:abc123def456").unwrap(),
(
"gitcommit".into(),
"https://github.com/foo/bar.git".into(),
"abc123def456".into()
)
);
}
#[test]
fn gitbranch_short_uses_default_url() {
assert_eq!(
canonicalize("gitbranch:main").unwrap(),
("gitbranch".into(), DEFAULT_URL.into(), "main".into())
);
}
#[test]
fn gitbranch_explicit_https_url() {
assert_eq!(
canonicalize("gitbranch:https://github.com/foo/bar.git:feature/my-branch").unwrap(),
(
"gitbranch".into(),
"https://github.com/foo/bar.git".into(),
"feature/my-branch".into()
)
);
}
#[test]
fn local_dir() {
assert_eq!(
canonicalize("local:/home/user/my-project").unwrap(),
(
"local".into(),
DEFAULT_URL.into(),
"/home/user/my-project".into()
)
);
}
#[test]
fn latesttag_with_url() {
assert_eq!(
canonicalize("latesttag:https://github.com/randomorg/randomrepo:v*").unwrap(),
(
"gittag".into(),
"https://github.com/randomorg/randomrepo".into(),
"v1.0.0".into()
)
);
}
#[test]
fn latesttag_without_url() {
assert_eq!(
canonicalize("latesttag:v*").unwrap(),
("gittag".into(), DEFAULT_URL.into(), "v1.0.0".into())
);
}
#[test]
fn latesttag_without_url_without_selector() {
assert_eq!(
canonicalize("latesttag").unwrap(),
("gittag".into(), DEFAULT_URL.into(), "v1.0.0".into())
);
}
// --- error-path tests ---
#[test]
fn missing_colon_returns_error() {
assert!(canonicalize("gittag").is_err());
}
#[test]
fn empty_string_returns_error() {
assert!(canonicalize("").is_err());
}
}