Skip to content

Commit

Permalink
refactor: Store genesis.json next to executable
Browse files Browse the repository at this point in the history
  • Loading branch information
morgsmccauley committed Mar 27, 2023
1 parent 2976446 commit 6537660
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion database/src/adapters/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where
/// memory usage and minimize database queries
pub async fn store_genesis_records(
pool: Database<PgConnection>,
genesis_file_path: String,
genesis_file_path: std::path::PathBuf,
) -> anyhow::Result<()> {
tracing::info!(
target: crate::EXPLORER_DATABASE,
Expand Down
17 changes: 9 additions & 8 deletions indexer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,32 +141,33 @@ async fn handle_message(
Ok(())
}

async fn download_genesis_file(opts: &configs::Opts) -> anyhow::Result<String> {
async fn download_genesis_file(opts: &configs::Opts) -> anyhow::Result<std::path::PathBuf> {
let res = reqwest::get(opts.genesis_file_url()).await?;

let total_size = res.content_length().unwrap();

let file_path = "genesis.json";
let mut exe_path = std::env::current_exe()?;
exe_path.pop();
let genesis_path = exe_path.join("genesis.json");

match std::fs::File::open(file_path) {
match std::fs::File::open(genesis_path.clone()) {
Ok(_) => {
tracing::info!(
target: INDEXER_FOR_EXPLORER,
"Using existing genesis file: {}",
file_path
genesis_path.display()
);
}
Err(_) => {
let mut file = std::fs::File::create(file_path)?;
let mut file = std::fs::File::create(genesis_path.clone())?;
let mut downloaded = 0;
let mut stream = res.bytes_stream();
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
downloaded = std::cmp::min(downloaded + (chunk.len() as u64), total_size);
tracing::info!(
target: INDEXER_FOR_EXPLORER,
"Downloading {}: {}/{}",
file_path,
"Downloading genesis.json: {}/{}",
indicatif::HumanBytes(downloaded),
indicatif::HumanBytes(total_size)
);
Expand All @@ -177,7 +178,7 @@ async fn download_genesis_file(opts: &configs::Opts) -> anyhow::Result<String> {
}
}

Ok(file_path.to_string())
Ok(genesis_path)
}

#[actix::main]
Expand Down

0 comments on commit 6537660

Please sign in to comment.