From 97477e80043ea5b860130123a1e509bccf51844b Mon Sep 17 00:00:00 2001 From: Artem Storozhuk Date: Tue, 18 Jun 2024 18:21:09 +0100 Subject: [PATCH] feat: Introduce downloading from our AWS private bucket --- aptos/Cargo.lock | 3 + aptos/solidity/contracts-generator/Cargo.toml | 3 + .../contracts-generator/src/bin/main.rs | 79 ++++++++++++++++++- 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/aptos/Cargo.lock b/aptos/Cargo.lock index 942ca058..b2150522 100644 --- a/aptos/Cargo.lock +++ b/aptos/Cargo.lock @@ -3577,8 +3577,11 @@ name = "contracts-generator" version = "0.1.0" dependencies = [ "anyhow", + "home", "log", + "sphinx-prover", "sphinx-sdk", + "tempfile", ] [[package]] diff --git a/aptos/solidity/contracts-generator/Cargo.toml b/aptos/solidity/contracts-generator/Cargo.toml index fa354dfb..55a05a64 100644 --- a/aptos/solidity/contracts-generator/Cargo.toml +++ b/aptos/solidity/contracts-generator/Cargo.toml @@ -10,4 +10,7 @@ path = "src/bin/main.rs" [dependencies] anyhow = "1.0.86" log = "0.4.21" +home = "0.5.9" +tempfile = "3.9.0" sphinx-sdk = { workspace = true } +sphinx-prover = { workspace = true } \ No newline at end of file diff --git a/aptos/solidity/contracts-generator/src/bin/main.rs b/aptos/solidity/contracts-generator/src/bin/main.rs index 6ac6298a..8ecec9c3 100644 --- a/aptos/solidity/contracts-generator/src/bin/main.rs +++ b/aptos/solidity/contracts-generator/src/bin/main.rs @@ -1,13 +1,20 @@ use anyhow::Result; +use home::home_dir; use log::info; -use sphinx_sdk::artifacts::try_install_plonk_bn254_artifacts; use sphinx_sdk::utils::setup_logger; +use std::fs::create_dir_all; use std::path::PathBuf; +use std::process::Command; fn main() -> Result<()> { setup_logger(); - let artifacts_dir = try_install_plonk_bn254_artifacts(); + // This should be replaced with + // + // `sphinx_sdk::artifacts::try_install_plonk_bn254_artifacts` + // + // once we can make our AWS bucket public + let artifacts_dir = download_artifacts_from_private_aws(); // Read all Solidity files from the artifacts_dir. let sol_files = std::fs::read_dir(artifacts_dir)? @@ -28,7 +35,73 @@ fn main() -> Result<()> { )?; } - info!("Contracts have been installed to: {:?}", contracts_src_dir); + info!( + "[contracts-generator] Contracts have been installed to: {:?}", + contracts_src_dir + ); Ok(()) } + +fn plonk_bn254_artifacts_dir() -> PathBuf { + home_dir() + .unwrap() + .join(".sp1") + .join("circuits") + .join("plonk_bn254") + .join(sphinx_prover::install::PLONK_BN254_ARTIFACTS_COMMIT) +} + +fn download_artifacts_from_private_aws() -> PathBuf { + let build_dir = plonk_bn254_artifacts_dir(); + if build_dir.exists() { + info!( + "[contracts-generator] plonk bn254 artifacts already seem to exist at {}. if you want to re-download them, delete the directory", + build_dir.display() + ); + } else { + info!( + "[contracts-generator] plonk bn254 artifacts for commit {} do not exist at {}. downloading...", + sphinx_prover::install::PLONK_BN254_ARTIFACTS_COMMIT, + build_dir.display() + ); + + create_dir_all(build_dir.clone()).unwrap(); + + let archive_path = format!("{}.tar.gz", build_dir.to_str().unwrap()); + let mut res = Command::new("aws") + .args([ + "s3", + "cp", + format!( + "s3://sphinx-plonk-params/{}.tar.gz", + sphinx_prover::install::PLONK_BN254_ARTIFACTS_COMMIT + ) + .as_str(), + archive_path.as_str(), + ]) + .spawn() + .expect("couldn't run `aws` command. Probably it is not installed"); + res.wait().unwrap(); + + // Extract the tarball to the build directory. + let mut res = Command::new("tar") + .args([ + "-Pxzf", + archive_path.as_str(), + "-C", + build_dir.to_str().unwrap(), + ]) + .spawn() + .expect("failed to extract tarball"); + res.wait().unwrap(); + + // Remove archive + let mut res = Command::new("rm") + .args(["-rf", archive_path.as_str()]) + .spawn() + .expect("failed to remove the archive"); + res.wait().unwrap(); + } + build_dir +}