Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Download expected bb version if installed backend has version mismatch #3150

Merged
merged 9 commits into from
Oct 16, 2023
2 changes: 2 additions & 0 deletions tooling/backend_interface/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ mod gates;
mod info;
mod prove;
mod verify;
mod version;
mod write_vk;

pub(crate) use contract::ContractCommand;
pub(crate) use gates::GatesCommand;
pub(crate) use info::InfoCommand;
pub(crate) use prove::ProveCommand;
pub(crate) use verify::VerifyCommand;
pub(crate) use version::VersionCommand;
pub(crate) use write_vk::WriteVkCommand;

#[test]
Expand Down
29 changes: 29 additions & 0 deletions tooling/backend_interface/src/cli/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::path::Path;

use crate::BackendError;

use super::string_from_stderr;

/// VersionCommand will call the backend binary
/// to query installed version.
pub(crate) struct VersionCommand;

impl VersionCommand {
pub(crate) fn run(self, binary_path: &Path) -> Result<String, BackendError> {
let mut command = std::process::Command::new(binary_path);

command.arg("--version");

let output = command.output()?;
if output.status.success() {
match String::from_utf8(output.stdout) {
Ok(result) => Ok(result),
Err(_) => Err(BackendError::CommandFailed(
"Unexpected output from --version check.".to_owned(),
)),
}
} else {
Err(BackendError::CommandFailed(string_from_stderr(&output.stderr)))
}
}
}
29 changes: 29 additions & 0 deletions tooling/backend_interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use acvm::acir::circuit::Opcode;
use bb_abstraction_leaks::ACVM_BACKEND_BARRETENBERG;
use bb_abstraction_leaks::BB_VERSION;
use cli::VersionCommand;
pub use download::download_backend;

const BACKENDS_DIR: &str = ".nargo/backends";
Expand Down Expand Up @@ -104,6 +106,33 @@
fn crs_directory(&self) -> PathBuf {
self.backend_directory().join("crs")
}

fn assert_correct_version(&self) -> Result<&PathBuf, BackendError> {
let binary_path = self.binary_path();
if binary_path.to_string_lossy().contains(ACVM_BACKEND_BARRETENBERG) {
match VersionCommand.run(binary_path) {
// If version matches then do nothing.
Ok(version_string) if version_string == BB_VERSION => (),

// If version doesn't match then download the correct version.
Ok(version_string) => {
println!("`{ACVM_BACKEND_BARRETENBERG}` version `{version_string}` is different from expected `{BB_VERSION}`. Downloading expected version...");
let bb_url = std::env::var("BB_BINARY_URL")
.unwrap_or_else(|_| bb_abstraction_leaks::BB_DOWNLOAD_URL.to_owned());
download_backend(&bb_url, binary_path)?;
}

// If `bb` fails to report its version, then attempt to fix it by re-downloading the binary.
Err(_) => {
println!("Could not determine version of `{ACVM_BACKEND_BARRETENBERG}`. Downloading expected version...");
let bb_url = std::env::var("BB_BINARY_URL")
.unwrap_or_else(|_| bb_abstraction_leaks::BB_DOWNLOAD_URL.to_owned());
download_backend(&bb_url, binary_path)?;
}
}
}
Ok(binary_path)
}
}

pub struct BackendOpcodeSupport {
Expand Down Expand Up @@ -132,7 +161,7 @@

#[test]
fn raises_error_on_missing_binary() {
let bad_backend = Backend::new("i_dont_exist".to_string());

Check warning on line 164 in tooling/backend_interface/src/lib.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (dont)

let binary_path = bad_backend.assert_binary_exists();

Expand Down
4 changes: 4 additions & 0 deletions tooling/backend_interface/src/proof_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{Backend, BackendError, BackendOpcodeSupport};
impl Backend {
pub fn get_exact_circuit_size(&self, circuit: &Circuit) -> Result<u32, BackendError> {
let binary_path = self.assert_binary_exists()?;
self.assert_correct_version()?;

let temp_directory = tempdir().expect("could not create a temporary directory");
let temp_directory = temp_directory.path().to_path_buf();
Expand All @@ -28,6 +29,7 @@ impl Backend {

pub fn get_backend_info(&self) -> Result<(Language, BackendOpcodeSupport), BackendError> {
let binary_path = self.assert_binary_exists()?;
self.assert_correct_version()?;
InfoCommand { crs_path: self.crs_directory() }.run(binary_path)
}

Expand All @@ -38,6 +40,7 @@ impl Backend {
is_recursive: bool,
) -> Result<Vec<u8>, BackendError> {
let binary_path = self.assert_binary_exists()?;
self.assert_correct_version()?;

let temp_directory = tempdir().expect("could not create a temporary directory");
let temp_directory = temp_directory.path().to_path_buf();
Expand Down Expand Up @@ -78,6 +81,7 @@ impl Backend {
is_recursive: bool,
) -> Result<bool, BackendError> {
let binary_path = self.assert_binary_exists()?;
self.assert_correct_version()?;

let temp_directory = tempdir().expect("could not create a temporary directory");
let temp_directory = temp_directory.path().to_path_buf();
Expand Down
1 change: 1 addition & 0 deletions tooling/backend_interface/src/smart_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use tempfile::tempdir;
impl Backend {
pub fn eth_contract(&self, circuit: &Circuit) -> Result<String, BackendError> {
let binary_path = self.assert_binary_exists()?;
self.assert_correct_version()?;

let temp_directory = tempdir().expect("could not create a temporary directory");
let temp_directory_path = temp_directory.path().to_path_buf();
Expand Down
1 change: 1 addition & 0 deletions tooling/bb_abstraction_leaks/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn main() -> Result<(), String> {
};

println!("cargo:rustc-env=BB_BINARY_URL={}", get_bb_download_url(arch, os));
println!("cargo:rustc-env=BB_VERSION={}", VERSION);

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions tooling/bb_abstraction_leaks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use acvm::FieldElement;

pub const ACVM_BACKEND_BARRETENBERG: &str = "acvm-backend-barretenberg";
pub const BB_DOWNLOAD_URL: &str = env!("BB_BINARY_URL");
pub const BB_VERSION: &str = env!("BB_VERSION");

/// Embed the Solidity verifier file
const ULTRA_VERIFIER_CONTRACT: &str = include_str!("contract.sol");
Expand Down
Loading