From b7751fa2224a68dd784d4733ad3b1080e45fd4b1 Mon Sep 17 00:00:00 2001 From: Jarry Xiao Date: Thu, 22 Jun 2023 23:08:15 -0400 Subject: [PATCH 1/5] Update args to make it easier to work with Anchor programs --- src/main.rs | 62 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9688a96..4b3c998 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,10 +54,10 @@ enum SubCommand { base_image: Option, /// If the program requires cargo build-bpf (instead of cargo build-sbf), as for anchor program, set this flag #[clap(long, default_value = "false")] - bpf_flag: bool, + bpf: bool, /// Docker workdir - #[clap(long, default_value = "build")] - workdir: String, + #[clap(long)] + workdir: Option, /// Arguments to pass to the underlying `cargo build-bpf` command #[clap(required = false, last = true)] cargo_args: Vec, @@ -119,10 +119,10 @@ enum SubCommand { library_name: Option, /// If the program requires cargo build-bpf (instead of cargo build-sbf), as for an Anchor program, set this flag #[clap(long, default_value = "false")] - bpf_flag: bool, + bpf: bool, /// Docker workdir - #[clap(long, default_value = "build")] - workdir: String, + #[clap(long)] + workdir: Option, /// Verify in current directory #[clap(long, default_value = "false")] current_dir: bool, @@ -155,7 +155,7 @@ fn main() -> anyhow::Result<()> { mount_directory, library_name, base_image, - bpf_flag, + bpf: bpf_flag, workdir, cargo_args, } => build( @@ -205,7 +205,7 @@ fn main() -> anyhow::Result<()> { program_id, base_image, library_name, - bpf_flag, + bpf: bpf_flag, workdir, cargo_args, current_dir, @@ -311,7 +311,7 @@ pub fn build( library_name: Option, base_image: Option, bpf_flag: bool, - workdir: String, + workdir_opt: Option, cargo_args: Vec, container_id_opt: &mut Option, ) -> anyhow::Result<()> { @@ -330,8 +330,6 @@ pub fn build( return Err(anyhow!(format!("No lockfile found at {}", lockfile))); } - let image = base_image.unwrap_or_else(|| "ellipsislabs/solana:latest".to_string()); - let is_anchor = std::path::Path::new(&format!("{}/Anchor.toml", mount_path)).exists(); let build_command = if bpf_flag || is_anchor { "build-bpf" @@ -339,6 +337,15 @@ pub fn build( "build-sbf" }; + let image = base_image.unwrap_or_else(|| { + if bpf_flag || is_anchor { + "projectserum/build:v0.26.0" + } else { + "ellipsislabs/solana:latest" + } + .to_string() + }); + let mut package_name = None; let relative_build_path = std::process::Command::new("find") @@ -371,6 +378,15 @@ pub fn build( }) .unwrap_or_else(|_| "".to_string()); + let workdir = workdir_opt.unwrap_or_else(|| { + if bpf_flag || is_anchor { + "workdir" + } else { + "build" + } + .to_string() + }); + let build_path = format!("/{}/{}", workdir, relative_build_path); println!("Building program at {}", build_path); @@ -433,7 +449,7 @@ pub fn build( .map_err(|e| anyhow!("Failed to find program: {}", e.to_string())) .and_then(|output| parse_output(output.stdout))?; let executable_hash = get_file_hash(&executable_path)?; - println!("Executable hash: {}", executable_hash); + println!("{}", executable_hash); } std::process::Command::new("docker") .args(&["kill", &container_id]) @@ -541,7 +557,7 @@ pub fn verify_from_repo( base_image: Option, library_name_opt: Option, bpf_flag: bool, - workdir: String, + workdir: Option, cargo_args: Vec, current_dir: bool, container_id_opt: &mut Option, @@ -573,16 +589,17 @@ pub fn verify_from_repo( temp_dir_opt.replace(verify_dir.clone()); - let verify_tmp_file_path = format!("{}/{}", verify_dir, base_name); + let verify_tmp_root_path = format!("{}/{}", verify_dir, base_name); + println!("Cloning repo into: {}", verify_tmp_root_path); std::process::Command::new("git") - .args(["clone", &repo_url, &verify_tmp_file_path]) + .args(["clone", &repo_url, &verify_tmp_root_path]) .output()?; // Checkout a specific commit hash, if provided if let Some(commit_hash) = commit_hash { let result = std::process::Command::new("cd") - .arg(&verify_tmp_file_path) + .arg(&verify_tmp_root_path) .output() .and_then(|_| { std::process::Command::new("git") @@ -600,7 +617,7 @@ pub fn verify_from_repo( } // Get the absolute build path to the solana program directory to build inside docker - let mount_path = PathBuf::from(verify_tmp_file_path.clone()).join(relative_mount_path); + let mount_path = PathBuf::from(verify_tmp_root_path.clone()).join(relative_mount_path); println!("Build path: {:?}", mount_path); let library_name = match library_name_opt { @@ -671,9 +688,9 @@ pub fn verify_from_repo( println!("On-chain Program Hash: {}", program_hash); if build_hash == program_hash { - println!("Program hash matches"); + println!("Program hash matches ✅"); } else { - println!("Program hash does not match"); + println!("Program hashes do not match ❌"); } Ok(()) @@ -689,7 +706,7 @@ pub fn build_and_verify_repo( library_name: String, connection_url: Option, program_id: Pubkey, - workdir: String, + workdir: Option, cargo_args: Vec, container_id_opt: &mut Option, ) -> anyhow::Result<(String, String)> { @@ -706,10 +723,6 @@ pub fn build_and_verify_repo( )?; // Get the hash of the build - println!( - "Looking for executable name {} at path: {}/target/deploy", - executable_filename, mount_path - ); let executable_path = std::process::Command::new("find") .args([ &format!("{}/target/deploy", mount_path), @@ -721,7 +734,6 @@ pub fn build_and_verify_repo( .and_then(|output| parse_output(output.stdout))?; println!("Executable file found at path: {:?}", executable_path); let build_hash = get_file_hash(&executable_path)?; - println!("Build hash: {}", build_hash); // Get the hash of the deployed program println!( From a4eb638e58e300034acab296584599027d7a454d Mon Sep 17 00:00:00 2001 From: Jarry Xiao Date: Thu, 22 Jun 2023 23:16:05 -0400 Subject: [PATCH 2/5] better logs for debugging --- src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 4b3c998..87494e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -601,10 +601,12 @@ pub fn verify_from_repo( let result = std::process::Command::new("cd") .arg(&verify_tmp_root_path) .output() + .map_err(|e| anyhow!("Failed to cd into verify_tmp_root_path: {:?}", e)) .and_then(|_| { std::process::Command::new("git") .args(["checkout", &commit_hash]) .output() + .map_err(|e| anyhow!("Failed to checkout commit hash: {:?}", e)) }); if result.is_ok() { println!("Checked out commit hash: {}", commit_hash); @@ -612,7 +614,7 @@ pub fn verify_from_repo( std::process::Command::new("rm") .args(["-rf", verify_dir.as_str()]) .output()?; - Err(anyhow!("Failed to checkout commit hash: {:?}", result))?; + Err(anyhow!("Encountered error in git setup: {:?}", result))?; } } From fde2b30e0d224deaea5dd56759e9b0ac8c187e38 Mon Sep 17 00:00:00 2001 From: Jarry Xiao Date: Thu, 22 Jun 2023 23:19:44 -0400 Subject: [PATCH 3/5] more logs --- src/main.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 87494e9..50e4db0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -594,6 +594,17 @@ pub fn verify_from_repo( std::process::Command::new("git") .args(["clone", &repo_url, &verify_tmp_root_path]) + .stdout(Stdio::inherit()) + .output()?; + + std::process::Command::new("ls") + .args(["-la"]) + .stdout(Stdio::inherit()) + .output()?; + + std::process::Command::new("ls") + .args(["-la", &verify_dir]) + .stdout(Stdio::inherit()) .output()?; // Checkout a specific commit hash, if provided @@ -601,7 +612,7 @@ pub fn verify_from_repo( let result = std::process::Command::new("cd") .arg(&verify_tmp_root_path) .output() - .map_err(|e| anyhow!("Failed to cd into verify_tmp_root_path: {:?}", e)) + .map_err(|e| anyhow!("Failed to cd into {}: {:?}", verify_tmp_root_path, e)) .and_then(|_| { std::process::Command::new("git") .args(["checkout", &commit_hash]) From 069fda3ef81c99aa54740bd7569d037000985cc9 Mon Sep 17 00:00:00 2001 From: Jarry Xiao Date: Thu, 22 Jun 2023 23:23:55 -0400 Subject: [PATCH 4/5] more logs --- src/main.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 50e4db0..33b26f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -602,8 +602,13 @@ pub fn verify_from_repo( .stdout(Stdio::inherit()) .output()?; + std::process::Command::new("cd") + .args([&verify_dir]) + .stdout(Stdio::inherit()) + .output()?; + std::process::Command::new("ls") - .args(["-la", &verify_dir]) + .args(["-la"]) .stdout(Stdio::inherit()) .output()?; From b5a6ae6668c933920184e4f547f17c4e6c3df733 Mon Sep 17 00:00:00 2001 From: Jarry Xiao Date: Thu, 22 Jun 2023 23:26:50 -0400 Subject: [PATCH 5/5] use git -C --- src/main.rs | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index 33b26f8..37c98a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -597,33 +597,13 @@ pub fn verify_from_repo( .stdout(Stdio::inherit()) .output()?; - std::process::Command::new("ls") - .args(["-la"]) - .stdout(Stdio::inherit()) - .output()?; - - std::process::Command::new("cd") - .args([&verify_dir]) - .stdout(Stdio::inherit()) - .output()?; - - std::process::Command::new("ls") - .args(["-la"]) - .stdout(Stdio::inherit()) - .output()?; - // Checkout a specific commit hash, if provided if let Some(commit_hash) = commit_hash { - let result = std::process::Command::new("cd") - .arg(&verify_tmp_root_path) + let result = std::process::Command::new("git") + .args(["-C", &verify_tmp_root_path]) + .args(["checkout", &commit_hash]) .output() - .map_err(|e| anyhow!("Failed to cd into {}: {:?}", verify_tmp_root_path, e)) - .and_then(|_| { - std::process::Command::new("git") - .args(["checkout", &commit_hash]) - .output() - .map_err(|e| anyhow!("Failed to checkout commit hash: {:?}", e)) - }); + .map_err(|e| anyhow!("Failed to checkout commit hash: {:?}", e)); if result.is_ok() { println!("Checked out commit hash: {}", commit_hash); } else {