From 04d31975f5417d3cb4357213174dbad3c178f81a Mon Sep 17 00:00:00 2001 From: johntaiko Date: Thu, 23 May 2024 09:10:53 +0800 Subject: [PATCH] feat: lock the setup for sharing instance (#244) --- Cargo.lock | 11 +++++++++++ provers/sgx/setup/Cargo.toml | 6 ++---- provers/sgx/setup/src/setup_bootstrap.rs | 10 ++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 05a72a7e2..2e6981708 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2562,6 +2562,16 @@ 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" @@ -5010,6 +5020,7 @@ dependencies = [ "dirs", "env_logger", "ethers-core", + "file-lock", "flate2", "hyper 0.14.28", "lazy_static", diff --git a/provers/sgx/setup/Cargo.toml b/provers/sgx/setup/Cargo.toml index 792ee6bc1..9639c6e1d 100644 --- a/provers/sgx/setup/Cargo.toml +++ b/provers/sgx/setup/Cargo.toml @@ -57,6 +57,7 @@ url = { workspace = true } cfg-if = { workspace = true } cap = { workspace = true } dirs = { workspace = true } +file-lock = "2.1.11" [dev-dependencies] assert_cmd = { workspace = true } @@ -76,7 +77,4 @@ default = ["sgx"] # "dep:risc0-driver", # "risc0-driver/enable", # ] -sgx = [ - "dep:sgx-prover", - "sgx-prover/enable", -] +sgx = ["dep:sgx-prover", "sgx-prover/enable"] diff --git a/provers/sgx/setup/src/setup_bootstrap.rs b/provers/sgx/setup/src/setup_bootstrap.rs index 15c134be8..06066ddaa 100644 --- a/provers/sgx/setup/src/setup_bootstrap.rs +++ b/provers/sgx/setup/src/setup_bootstrap.rs @@ -7,6 +7,7 @@ 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::{ @@ -20,6 +21,15 @@ 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), + )?; 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)