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

chore: Add more information to backend errors #2791

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions tooling/backend_interface/src/cli/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::path::{Path, PathBuf};

use crate::BackendError;

use super::string_from_stderr;

/// VerifyCommand will call the barretenberg binary
/// to return a solidity library with the verification key
/// that can be used to verify proofs on-chain.
Expand Down Expand Up @@ -31,9 +33,9 @@ impl ContractCommand {

if output.status.success() {
String::from_utf8(output.stdout)
.map_err(|error| BackendError::MalformedResponse(error.into_bytes()))
.map_err(|error| BackendError::InvalidUTF8Vector(error.into_bytes()))
} else {
Err(BackendError::CommandFailed(output.stderr))
Err(BackendError::CommandFailed(string_from_stderr(&output.stderr)))
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions tooling/backend_interface/src/cli/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::path::{Path, PathBuf};

use crate::BackendError;

use super::string_from_stderr;

/// GatesCommand will call the barretenberg binary
/// to return the number of gates needed to create a proof
/// for the given bytecode.
Expand All @@ -21,13 +23,16 @@ impl GatesCommand {
.output()?;

if !output.status.success() {
return Err(BackendError::CommandFailed(output.stderr));
return Err(BackendError::CommandFailed(string_from_stderr(&output.stderr)));
}
// Note: barretenberg includes the newline, so that subsequent prints to stdout
// are not on the same line as the gates output.

let gates_bytes: [u8; 8] =
output.stdout.try_into().map_err(BackendError::MalformedResponse)?;
const EXPECTED_BYTES: usize = 8;
let gates_bytes: [u8; EXPECTED_BYTES] =
output.stdout.as_slice().try_into().map_err(|_| {
BackendError::UnexpectedNumberOfBytes(EXPECTED_BYTES, output.stdout.clone())
})?;

// Convert bytes to u64 in little-endian format
let value = u64::from_le_bytes(gates_bytes);
Expand Down
4 changes: 3 additions & 1 deletion tooling/backend_interface/src/cli/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::path::{Path, PathBuf};

use crate::{BackendError, BackendOpcodeSupport};

use super::string_from_stderr;

pub(crate) struct InfoCommand {
pub(crate) crs_path: PathBuf,
}
Expand Down Expand Up @@ -43,7 +45,7 @@ impl InfoCommand {
let output = command.output()?;

if !output.status.success() {
return Err(BackendError::CommandFailed(output.stderr));
return Err(BackendError::CommandFailed(string_from_stderr(&output.stderr)));
}

let backend_info: InfoResponse =
Expand Down
7 changes: 6 additions & 1 deletion tooling/backend_interface/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ fn no_command_provided_works() -> Result<(), crate::BackendError> {

let output = std::process::Command::new(backend.binary_path()).output()?;

let stderr = String::from_utf8_lossy(&output.stderr);
let stderr = string_from_stderr(&output.stderr);
// Assert help message is printed due to no command being provided.
assert!(stderr.contains("Usage: mock_backend <COMMAND>"));

Ok(())
}

// Converts a stderr byte array to a string (including invalid characters)
fn string_from_stderr(stderr: &[u8]) -> String {
String::from_utf8_lossy(stderr).to_string()
}
4 changes: 3 additions & 1 deletion tooling/backend_interface/src/cli/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::path::{Path, PathBuf};

use crate::BackendError;

use super::string_from_stderr;

/// ProveCommand will call the barretenberg binary
/// to create a proof, given the witness and the bytecode.
///
Expand Down Expand Up @@ -39,7 +41,7 @@ impl ProveCommand {
if output.status.success() {
Ok(output.stdout)
} else {
Err(BackendError::CommandFailed(output.stderr))
Err(BackendError::CommandFailed(string_from_stderr(&output.stderr)))
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion tooling/backend_interface/src/cli/write_vk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::{Path, PathBuf};

use super::string_from_stderr;
use crate::BackendError;

/// WriteCommand will call the barretenberg binary
Expand Down Expand Up @@ -32,7 +33,7 @@ impl WriteVkCommand {
if output.status.success() {
Ok(())
} else {
Err(BackendError::CommandFailed(output.stderr))
Err(BackendError::CommandFailed(string_from_stderr(&output.stderr)))
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions tooling/backend_interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ pub enum BackendError {
#[error("Backend binary does not exist")]
MissingBinary,

#[error("The backend responded with malformed data: {0:?}")]
MalformedResponse(Vec<u8>),
#[error("The backend responded with a malformed UTF8 byte vector: {0:?}")]
InvalidUTF8Vector(Vec<u8>),

#[error("The backend encountered an error")]
CommandFailed(Vec<u8>),
#[error(
"The backend responded with a unexpected number of bytes. Expected: {0} but got {} ({1:?})", .1.len()
)]
UnexpectedNumberOfBytes(usize, Vec<u8>),

#[error("The backend encountered an error: {0:?}")]
CommandFailed(String),
}

#[derive(Debug)]
Expand Down