From 5880c0c4a424d9205e008be7467b2615df30a22a Mon Sep 17 00:00:00 2001 From: john xu Date: Sat, 25 May 2024 10:13:50 +0800 Subject: [PATCH] fix: can't share setup between pods --- Cargo.lock | 11 ------- provers/sgx/prover/src/sgx_register_utils.rs | 21 +++++++++++-- provers/sgx/setup/Cargo.toml | 1 - provers/sgx/setup/src/setup_bootstrap.rs | 31 ++++++-------------- 4 files changed, 27 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2e6981708..05a72a7e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2562,16 +2562,6 @@ version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" -[[package]] -name = "file-lock" -version = "2.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "040b48f80a749da50292d0f47a1e2d5bf1d772f52836c07f64bfccc62ba6e664" -dependencies = [ - "cc", - "libc", -] - [[package]] name = "find_cuda_helper" version = "0.2.0" @@ -5020,7 +5010,6 @@ dependencies = [ "dirs", "env_logger", "ethers-core", - "file-lock", "flate2", "hyper 0.14.28", "lazy_static", diff --git a/provers/sgx/prover/src/sgx_register_utils.rs b/provers/sgx/prover/src/sgx_register_utils.rs index b08f41a72..efc285e69 100644 --- a/provers/sgx/prover/src/sgx_register_utils.rs +++ b/provers/sgx/prover/src/sgx_register_utils.rs @@ -15,9 +15,18 @@ use url::Url; const REGISTERED_FILE: &str = "registered"; -pub fn get_instance_id(dir: &Path) -> Result { +pub fn get_instance_id(dir: &Path) -> Result> { let file = dir.join(REGISTERED_FILE); - let id = fs::read_to_string(file)?.parse()?; + let id = match fs::read_to_string(file) { + Ok(t) => Some(t.parse()?), + Err(e) => { + if e.kind() == io::ErrorKind::NotFound { + None + } else { + return Err(e.into()); + } + } + }; Ok(id) } @@ -29,7 +38,13 @@ pub fn set_instance_id(dir: &Path, id: u64) -> io::Result<()> { pub fn remove_instance_id(dir: &Path) -> io::Result<()> { let file = dir.join(REGISTERED_FILE); - fs::remove_file(file)?; + fs::remove_file(file).or_else(|e| { + if e.kind() == io::ErrorKind::NotFound { + Ok(()) + } else { + Err(e) + } + })?; Ok(()) } diff --git a/provers/sgx/setup/Cargo.toml b/provers/sgx/setup/Cargo.toml index 9639c6e1d..2800fc972 100644 --- a/provers/sgx/setup/Cargo.toml +++ b/provers/sgx/setup/Cargo.toml @@ -57,7 +57,6 @@ url = { workspace = true } cfg-if = { workspace = true } cap = { workspace = true } dirs = { workspace = true } -file-lock = "2.1.11" [dev-dependencies] assert_cmd = { workspace = true } diff --git a/provers/sgx/setup/src/setup_bootstrap.rs b/provers/sgx/setup/src/setup_bootstrap.rs index 361ceccbe..1388a6f15 100644 --- a/provers/sgx/setup/src/setup_bootstrap.rs +++ b/provers/sgx/setup/src/setup_bootstrap.rs @@ -7,7 +7,6 @@ use std::{ use crate::app_args::BootstrapArgs; use anyhow::{anyhow, Context, Result}; -use file_lock::{FileLock, FileOptions}; use raiko_lib::consts::SupportedChainSpecs; use serde_json::{Number, Value}; use sgx_prover::{ @@ -21,15 +20,6 @@ pub(crate) async fn setup_bootstrap( config_dir: PathBuf, bootstrap_args: &BootstrapArgs, ) -> Result<()> { - // Lock the bootstrap process to prevent multiple instances from running concurrently. - // Block until the lock is acquired. - // Create the lock file if it does not exist. - // Drop the lock file when the lock goes out of scope by drop guard. - let _filelock = FileLock::lock( - config_dir.join("bootstrap.lock"), - true, - FileOptions::new().create(true).write(true), - )?; let chain_specs = SupportedChainSpecs::merge_from_file(bootstrap_args.chain_spec_path.clone())?; let l1_chain_spec = chain_specs .get_chain_spec(&bootstrap_args.l1_network) @@ -52,25 +42,22 @@ pub(crate) async fn setup_bootstrap( cmd }; - let mut instance_id = get_instance_id(&config_dir).ok(); + let mut instance_id = get_instance_id(&config_dir)?; let need_init = check_bootstrap(secret_dir.clone(), gramine_cmd()) .await + .map_err(|e| { + println!("Error checking bootstrap: {:?}", e); + e + }) .is_err() || instance_id.is_none(); + println!("Instance ID: {:?}", instance_id); + if need_init { - let bootstrap_proof = bootstrap(secret_dir, gramine_cmd()).await?; // clean check file - match remove_instance_id(&config_dir) { - Ok(_) => Ok(()), - Err(e) => { - if e.kind() == std::io::ErrorKind::NotFound { - Ok(()) - } else { - Err(e) - } - } - }?; + remove_instance_id(&config_dir)?; + let bootstrap_proof = bootstrap(secret_dir, gramine_cmd()).await?; let register_id = register_sgx_instance( &bootstrap_proof.quote, &l1_chain_spec.rpc,