Skip to content

Commit

Permalink
fix: can't share setup between pods
Browse files Browse the repository at this point in the history
  • Loading branch information
johntaiko committed May 25, 2024
1 parent 9bcb44a commit 5880c0c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 37 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions provers/sgx/prover/src/sgx_register_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ use url::Url;

const REGISTERED_FILE: &str = "registered";

pub fn get_instance_id(dir: &Path) -> Result<u64> {
pub fn get_instance_id(dir: &Path) -> Result<Option<u64>> {
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)
}

Expand All @@ -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(())
}

Expand Down
1 change: 0 additions & 1 deletion provers/sgx/setup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
31 changes: 9 additions & 22 deletions provers/sgx/setup/src/setup_bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit 5880c0c

Please sign in to comment.