Skip to content

Commit 449b0de

Browse files
authored
Merge pull request #254 from blockfrost/chore/offline-compilation
Allow offline compilation and `--config <PATH>`
2 parents 6df4459 + 976793e commit 449b0de

File tree

10 files changed

+207
-27
lines changed

10 files changed

+207
-27
lines changed

Cargo.lock

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ caryatid_module_clock = "0.12"
3737
caryatid_module_spy = "0.12"
3838
anyhow = "1.0"
3939
chrono = "0.4"
40+
clap = { version = "4.5", features = ["derive"] }
4041
config = "0.15.11"
4142
dashmap = "6.1.0"
4243
hex = "0.4"

modules/genesis_bootstrapper/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ tracing = { workspace = true }
2626
[build-dependencies]
2727
anyhow = { workspace = true }
2828
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
29+
serde_json = { workspace = true }
2930
tokio = { workspace = true }
3031

3132
[lib]

modules/genesis_bootstrapper/build.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
// Build-time script to download generics
2+
use std::collections::HashMap;
3+
use std::env;
24
use std::fs;
5+
use std::fs::File;
36
use std::io::Write;
47
use std::path::Path;
58

69
use anyhow::{Context, Result};
10+
use serde_json::from_reader;
711

812
const OUTPUT_DIR: &str = "downloads";
913

14+
async fn fetch_bytes(client: &reqwest::Client, url: &str) -> Result<Vec<u8>> {
15+
if let Ok(path) = env::var("ACROPOLIS_OFFLINE_MIRROR") {
16+
if let Ok(file) = File::open(&path) {
17+
if let Ok(map) = from_reader::<_, HashMap<String, String>>(file) {
18+
if let Some(path) = map.get(url) {
19+
if let Ok(bytes) = fs::read(&Path::new(path).to_path_buf()) {
20+
return Ok(bytes);
21+
}
22+
}
23+
}
24+
}
25+
}
26+
let req = client.get(url).build().with_context(|| format!("Failed to request {url}"))?;
27+
let resp = client.execute(req).await.with_context(|| format!("Failed to fetch {url}"))?;
28+
Ok(resp.bytes().await.context("Failed to read response")?.to_vec())
29+
}
30+
1031
/// Download a URL to a file in OUTPUT_DIR
1132
async fn download(client: &reqwest::Client, url: &str, filename: &str) -> Result<()> {
12-
let request = client.get(url).build().with_context(|| format!("Failed to request {url}"))?;
13-
let response =
14-
client.execute(request).await.with_context(|| format!("Failed to fetch {url}"))?;
15-
let data = response.bytes().await.context("Failed to read response")?;
33+
let data = fetch_bytes(client, url).await?;
1634

1735
let output_path = Path::new(OUTPUT_DIR);
1836
if !output_path.exists() {

modules/parameters_state/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ tracing = { workspace = true }
2929

3030
[build-dependencies]
3131
reqwest = { version = "0.11", features = ["blocking"] }
32+
serde_json = { workspace = true }
3233

3334
[lib]
3435
path = "src/parameters_state.rs"

modules/parameters_state/build.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
// Build-time script to download generics
22
use reqwest::blocking::get;
3+
use serde_json::from_reader;
4+
use std::collections::HashMap;
5+
use std::env;
36
use std::fs;
7+
use std::fs::File;
48
use std::io::Write;
59
use std::path::Path;
610

711
const OUTPUT_DIR: &str = "downloads";
812

13+
fn fetch_text(url: &str) -> Result<String, Box<dyn std::error::Error>> {
14+
if let Ok(path) = env::var("ACROPOLIS_OFFLINE_MIRROR") {
15+
if !path.is_empty() {
16+
if let Ok(file) = File::open(path) {
17+
if let Ok(map) = from_reader::<_, HashMap<String, String>>(file) {
18+
if let Some(path_str) = map.get(url) {
19+
if let Ok(s) = fs::read_to_string(&Path::new(path_str).to_path_buf()) {
20+
return Ok(s);
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}
27+
Ok(get(url)?.error_for_status()?.text()?)
28+
}
29+
930
/// Download a URL to a file in OUTPUT_DIR
1031
fn download(url_base: &str, epoch: &str, filename: &str, rename: &Vec<(&str, &str)>) {
1132
let url = format!("{}/{}-genesis.json", url_base, epoch);
12-
let response = get(url).expect("Failed to fetch {url}");
13-
let mut data = response.text().expect("Failed to read response");
33+
let mut data = fetch_text(&url).expect("Failed to fetch {url}");
1434

1535
for (what, with) in rename.iter() {
1636
data = data.replace(&format!("\"{what}\""), &format!("\"{with}\""));

processes/omnibus/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ caryatid_module_rest_server = { workspace = true }
3535
caryatid_module_spy = { workspace = true }
3636

3737
anyhow = { workspace = true }
38+
clap = { workspace = true }
3839
config = { workspace = true }
3940
tracing = { workspace = true }
4041
tracing-subscriber = { version = "0.3.20", features = ["registry", "env-filter"] }

processes/omnibus/src/main.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,18 @@ use tikv_jemallocator::Jemalloc;
4545
#[global_allocator]
4646
static GLOBAL: Jemalloc = Jemalloc;
4747

48+
#[derive(Debug, clap::Parser)]
49+
#[command(name = "acropolis_process_omnibus")]
50+
struct Args {
51+
#[arg(long, value_name = "PATH", default_value_t = option_env!("ACROPOLIS_OMNIBUS_DEFAULT_CONFIG").unwrap_or("omnibus.toml").to_string())]
52+
config: String,
53+
}
54+
4855
/// Standard main
4956
#[tokio::main]
5057
pub async fn main() -> Result<()> {
58+
let args = <self::Args as clap::Parser>::parse();
59+
5160
// Standard logging using RUST_LOG for log levels default to INFO for events only
5261
let fmt_layer = fmt::layer()
5362
.with_filter(EnvFilter::from_default_env().add_directive(filter::LevelFilter::INFO.into()))
@@ -77,7 +86,7 @@ pub async fn main() -> Result<()> {
7786
// Read the config
7887
let config = Arc::new(
7988
Config::builder()
80-
.add_source(File::with_name("omnibus"))
89+
.add_source(File::with_name(&args.config))
8190
.add_source(Environment::with_prefix("ACROPOLIS"))
8291
.build()
8392
.unwrap(),

processes/replayer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ caryatid_module_rest_server = { workspace = true }
3030
caryatid_module_spy = { workspace = true }
3131

3232
anyhow = { workspace = true }
33+
clap = { workspace = true }
3334
config = { workspace = true }
3435
tracing = { workspace = true }
3536
tracing-subscriber = { version = "0.3.20", features = ["registry", "env-filter"] }

0 commit comments

Comments
 (0)