handle errors

This commit is contained in:
collin 2020-08-12 13:16:29 -07:00
parent 251783b9bc
commit 7ff3fb1b96
10 changed files with 108 additions and 53 deletions

View File

@ -0,0 +1,23 @@
use crate::InputValueError;
use snarkos_errors::objects::account::AccountError;
use std::{num::ParseIntError, str::ParseBoolError};
#[derive(Debug, Error)]
pub enum DPCRecordValuesError {
#[error("{}", _0)]
AccountError(#[from] AccountError),
#[error("{}", _0)]
InputValueError(#[from] InputValueError),
#[error("record parameter `{}` not found in state file", _0)]
MissingParameter(String),
#[error("{}", _0)]
ParseBoolError(#[from] ParseBoolError),
#[error("{}", _0)]
ParseIntError(#[from] ParseIntError),
}

View File

@ -1,2 +1,26 @@
use crate::{RecordVerificationError, StateLeafValuesError, StateValuesError};
use snarkos_errors::algorithms::{CommitmentError, MerkleError};
use std::io::Error as IOError;
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum LocalDataVerificationError {} pub enum LocalDataVerificationError {
#[error("{}", _0)]
CommitmentError(#[from] CommitmentError),
#[error("{}", _0)]
MerkleError(#[from] MerkleError),
#[error("{}", _0)]
IOError(#[from] IOError),
#[error("{}", _0)]
RecordVerificationError(#[from] RecordVerificationError),
#[error("{}", _0)]
StateLeafValuesError(#[from] StateLeafValuesError),
#[error("{}", _0)]
StateValuesError(#[from] StateValuesError),
}

View File

@ -1,3 +1,6 @@
pub mod dpc_record_values;
pub use self::dpc_record_values::*;
pub mod input_value; pub mod input_value;
pub use self::input_value::*; pub use self::input_value::*;

View File

@ -1,14 +1,11 @@
use crate::InputValueError; use crate::DPCRecordValuesError;
use snarkos_errors::{algorithms::CommitmentError, objects::account::AccountError}; use snarkos_errors::algorithms::CommitmentError;
use std::{io::Error as IOError, num::ParseIntError, str::ParseBoolError}; use std::io::Error as IOError;
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum RecordVerificationError { pub enum RecordVerificationError {
#[error("{}", _0)]
AccountError(#[from] AccountError),
#[error("record commitment does not match record data")] #[error("record commitment does not match record data")]
CommitmentsDoNotMatch, CommitmentsDoNotMatch,
@ -16,17 +13,8 @@ pub enum RecordVerificationError {
CommitmentError(#[from] CommitmentError), CommitmentError(#[from] CommitmentError),
#[error("{}", _0)] #[error("{}", _0)]
InputValueError(#[from] InputValueError), DPCRecordValuesError(#[from] DPCRecordValuesError),
#[error("{}", _0)] #[error("{}", _0)]
IOError(#[from] IOError), IOError(#[from] IOError),
#[error("record parameter `{}` not found in state file", _0)]
MissingParameter(String),
#[error("{}", _0)]
ParseBoolError(#[from] ParseBoolError),
#[error("{}", _0)]
ParseIntError(#[from] ParseIntError),
} }

View File

@ -1,5 +1,18 @@
use crate::InputValueError;
use std::{num::ParseIntError, str::ParseBoolError};
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum StateLeafValuesError { pub enum StateLeafValuesError {
#[error("state parameter `{}` not found in state file", _0)] #[error("{}", _0)]
InputValueError(#[from] InputValueError),
#[error("state leaf parameter `{}` not found in state file", _0)]
MissingParameter(String), MissingParameter(String),
#[error("{}", _0)]
ParseBoolError(#[from] ParseBoolError),
#[error("{}", _0)]
ParseIntError(#[from] ParseIntError),
} }

View File

@ -1,5 +1,18 @@
use crate::InputValueError;
use std::{num::ParseIntError, str::ParseBoolError};
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum StateValuesError { pub enum StateValuesError {
#[error("{}", _0)]
InputValueError(#[from] InputValueError),
#[error("state parameter `{}` not found in state file", _0)] #[error("state parameter `{}` not found in state file", _0)]
MissingParameter(String), MissingParameter(String),
#[error("{}", _0)]
ParseBoolError(#[from] ParseBoolError),
#[error("{}", _0)]
ParseIntError(#[from] ParseIntError),
} }

View File

@ -19,19 +19,19 @@ pub fn verify_local_data_commitment(
) -> Result<bool, LocalDataVerificationError> { ) -> Result<bool, LocalDataVerificationError> {
// verify record commitment // verify record commitment
let typed_record = typed_input.get_record(); let typed_record = typed_input.get_record();
let dpc_record_values = verify_record_commitment(typed_record, record_commitment_params).unwrap(); let dpc_record_values = verify_record_commitment(typed_record, record_commitment_params)?;
let record_commitment: Vec<u8> = dpc_record_values.commitment; let record_commitment: Vec<u8> = dpc_record_values.commitment;
let record_serial_number: Vec<u8> = dpc_record_values.serial_number; let record_serial_number: Vec<u8> = dpc_record_values.serial_number;
// parse typed state values // parse typed state values
let typed_state = typed_input.get_state(); let typed_state = typed_input.get_state();
let state_values = StateValues::try_from(typed_state).unwrap(); let state_values = StateValues::try_from(typed_state)?;
let leaf_index: u32 = state_values.leaf_index; let leaf_index: u32 = state_values.leaf_index;
let root: Vec<u8> = state_values.root; let root: Vec<u8> = state_values.root;
// parse typed state leaf values // parse typed state leaf values
let typed_state_leaf = typed_input.get_state_leaf(); let typed_state_leaf = typed_input.get_state_leaf();
let state_leaf_values = StateLeafValues::try_from(typed_state_leaf).unwrap(); let state_leaf_values = StateLeafValues::try_from(typed_state_leaf)?;
let _path: Vec<Vec<u8>> = state_leaf_values.path; let _path: Vec<Vec<u8>> = state_leaf_values.path;
let memo: Vec<u8> = state_leaf_values.memo; let memo: Vec<u8> = state_leaf_values.memo;
let network_id: u8 = state_leaf_values.network_id; let network_id: u8 = state_leaf_values.network_id;
@ -41,16 +41,16 @@ pub fn verify_local_data_commitment(
let is_death = leaf_index < (Components::NUM_INPUT_RECORDS as u32); let is_death = leaf_index < (Components::NUM_INPUT_RECORDS as u32);
let input_bytes = if is_death { let input_bytes = if is_death {
to_bytes![record_serial_number, record_commitment, memo, network_id].unwrap() to_bytes![record_serial_number, record_commitment, memo, network_id]?
} else { } else {
to_bytes![record_commitment, memo, network_id].unwrap() to_bytes![record_commitment, memo, network_id]?
}; };
// Construct local data commitment leaf // Construct local data commitment leaf
let local_data_leaf_randomness = Fp256::read(&leaf_randomness[..]).unwrap(); let local_data_leaf_randomness = Fp256::read(&leaf_randomness[..])?;
let local_data_commitment_leaf = let local_data_commitment_leaf =
LocalDataCommitment::commit(&local_data_commitment_params, &input_bytes, &local_data_leaf_randomness).unwrap(); LocalDataCommitment::commit(&local_data_commitment_params, &input_bytes, &local_data_leaf_randomness)?;
// Construct record commitment merkle path // Construct record commitment merkle path
@ -74,11 +74,9 @@ pub fn verify_local_data_commitment(
}; };
// Check record commitment merkle path is valid for the given local data commitment root // Check record commitment merkle path is valid for the given local data commitment root
let local_data_commitment_root = Fp256::read(&root[..]).unwrap(); let local_data_commitment_root = Fp256::read(&root[..])?;
let result = local_data_merkle_path let result = local_data_merkle_path.verify(&local_data_commitment_root, &local_data_commitment_leaf)?;
.verify(&local_data_commitment_root, &local_data_commitment_leaf)
.unwrap();
Ok(result) Ok(result)
} }

View File

@ -20,24 +20,20 @@ impl TryFrom<&TypedStateLeaf> for StateLeafValues {
fn try_from(state_leaf: &TypedStateLeaf) -> Result<Self, Self::Error> { fn try_from(state_leaf: &TypedStateLeaf) -> Result<Self, Self::Error> {
// Lookup path // Lookup path
let path_value = get_parameter_value(PATH_PARAMETER_STRING.to_owned(), state_leaf).unwrap(); let path_value = get_parameter_value(PATH_PARAMETER_STRING.to_owned(), state_leaf)?;
let path = input_to_nested_u8_vec(path_value).unwrap(); let path = input_to_nested_u8_vec(path_value)?;
// Lookup memo // Lookup memo
let memo_value = get_parameter_value(MEMO_PARAMETER_STRING.to_owned(), state_leaf).unwrap(); let memo_value = get_parameter_value(MEMO_PARAMETER_STRING.to_owned(), state_leaf)?;
let memo = input_to_u8_vec(memo_value).unwrap(); let memo = input_to_u8_vec(memo_value)?;
// Lookup network id // Lookup network id
let network_id_value = get_parameter_value(NETWORK_ID_PARAMETER_STRING.to_owned(), state_leaf).unwrap(); let network_id_value = get_parameter_value(NETWORK_ID_PARAMETER_STRING.to_owned(), state_leaf)?;
let network_id = input_to_integer_string(network_id_value) let network_id = input_to_integer_string(network_id_value)?.parse::<u8>()?;
.unwrap()
.parse::<u8>()
.unwrap();
// Lookup leaf randomness // Lookup leaf randomness
let leaf_randomness_value = let leaf_randomness_value = get_parameter_value(LEAF_RANDOMNESS_PARAMETER_STRING.to_owned(), state_leaf)?;
get_parameter_value(LEAF_RANDOMNESS_PARAMETER_STRING.to_owned(), state_leaf).unwrap(); let leaf_randomness = input_to_u8_vec(leaf_randomness_value)?;
let leaf_randomness = input_to_u8_vec(leaf_randomness_value).unwrap();
Ok(Self { Ok(Self {
path, path,

View File

@ -16,15 +16,12 @@ impl TryFrom<&TypedState> for StateValues {
fn try_from(state: &TypedState) -> Result<Self, Self::Error> { fn try_from(state: &TypedState) -> Result<Self, Self::Error> {
// Lookup leaf index // Lookup leaf index
let leaf_index_value = get_parameter_value(LEAF_INDEX_PARAMETER_STRING.to_owned(), state).unwrap(); let leaf_index_value = get_parameter_value(LEAF_INDEX_PARAMETER_STRING.to_owned(), state)?;
let leaf_index = input_to_integer_string(leaf_index_value) let leaf_index = input_to_integer_string(leaf_index_value)?.parse::<u32>()?;
.unwrap()
.parse::<u32>()
.unwrap();
// Lookup root // Lookup root
let root_value = get_parameter_value(ROOT_PARAMETER_STRING.to_owned(), state).unwrap(); let root_value = get_parameter_value(ROOT_PARAMETER_STRING.to_owned(), state)?;
let root = input_to_u8_vec(root_value).unwrap(); let root = input_to_u8_vec(root_value)?;
Ok(Self { leaf_index, root }) Ok(Self { leaf_index, root })
} }

View File

@ -1,4 +1,4 @@
use crate::{utilities::*, RecordVerificationError}; use crate::{utilities::*, DPCRecordValuesError};
use leo_typed::{InputValue, Record as TypedRecord}; use leo_typed::{InputValue, Record as TypedRecord};
use snarkos_dpc::base_dpc::instantiated::Components; use snarkos_dpc::base_dpc::instantiated::Components;
@ -30,7 +30,7 @@ pub struct DPCRecordValues {
} }
impl TryFrom<&TypedRecord> for DPCRecordValues { impl TryFrom<&TypedRecord> for DPCRecordValues {
type Error = RecordVerificationError; type Error = DPCRecordValuesError;
fn try_from(record: &TypedRecord) -> Result<Self, Self::Error> { fn try_from(record: &TypedRecord) -> Result<Self, Self::Error> {
// Lookup serial number // Lookup serial number
@ -89,7 +89,7 @@ impl TryFrom<&TypedRecord> for DPCRecordValues {
} }
} }
fn get_parameter_value(name: String, record: &TypedRecord) -> Result<InputValue, RecordVerificationError> { fn get_parameter_value(name: String, record: &TypedRecord) -> Result<InputValue, DPCRecordValuesError> {
let parameters = record.values(); let parameters = record.values();
let matched_parameter = parameters let matched_parameter = parameters
.iter() .iter()
@ -98,8 +98,8 @@ fn get_parameter_value(name: String, record: &TypedRecord) -> Result<InputValue,
match matched_parameter { match matched_parameter {
Some((_parameter, value_option)) => match value_option { Some((_parameter, value_option)) => match value_option {
Some(value) => Ok(value.clone()), Some(value) => Ok(value.clone()),
None => Err(RecordVerificationError::MissingParameter(name)), None => Err(DPCRecordValuesError::MissingParameter(name)),
}, },
None => Err(RecordVerificationError::MissingParameter(name)), None => Err(DPCRecordValuesError::MissingParameter(name)),
} }
} }