From 7ff3fb1b96ac2a96398b223441fc3ad8613964df Mon Sep 17 00:00:00 2001 From: collin Date: Wed, 12 Aug 2020 13:16:29 -0700 Subject: [PATCH] handle errors --- state/src/errors/dpc_record_values.rs | 23 ++++++++++++++++ state/src/errors/local_data_commitment.rs | 26 ++++++++++++++++++- state/src/errors/mod.rs | 3 +++ state/src/errors/record_commitment.rs | 20 +++----------- state/src/errors/state_leaf_values.rs | 15 ++++++++++- state/src/errors/state_values.rs | 13 ++++++++++ .../local_data_commitment.rs | 20 +++++++------- .../state_leaf_values.rs | 20 ++++++-------- .../src/local_data_commitment/state_values.rs | 11 +++----- .../record_commitment/dpc_record_values.rs | 10 +++---- 10 files changed, 108 insertions(+), 53 deletions(-) create mode 100644 state/src/errors/dpc_record_values.rs diff --git a/state/src/errors/dpc_record_values.rs b/state/src/errors/dpc_record_values.rs new file mode 100644 index 0000000000..d063ac8824 --- /dev/null +++ b/state/src/errors/dpc_record_values.rs @@ -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), +} diff --git a/state/src/errors/local_data_commitment.rs b/state/src/errors/local_data_commitment.rs index e3e116eb9b..ecdc2b8c18 100644 --- a/state/src/errors/local_data_commitment.rs +++ b/state/src/errors/local_data_commitment.rs @@ -1,2 +1,26 @@ +use crate::{RecordVerificationError, StateLeafValuesError, StateValuesError}; + +use snarkos_errors::algorithms::{CommitmentError, MerkleError}; + +use std::io::Error as IOError; + #[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), +} diff --git a/state/src/errors/mod.rs b/state/src/errors/mod.rs index 38a61a49ea..fd5b2ade5a 100644 --- a/state/src/errors/mod.rs +++ b/state/src/errors/mod.rs @@ -1,3 +1,6 @@ +pub mod dpc_record_values; +pub use self::dpc_record_values::*; + pub mod input_value; pub use self::input_value::*; diff --git a/state/src/errors/record_commitment.rs b/state/src/errors/record_commitment.rs index 441eb25f1f..a00bb8eead 100644 --- a/state/src/errors/record_commitment.rs +++ b/state/src/errors/record_commitment.rs @@ -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)] pub enum RecordVerificationError { - #[error("{}", _0)] - AccountError(#[from] AccountError), - #[error("record commitment does not match record data")] CommitmentsDoNotMatch, @@ -16,17 +13,8 @@ pub enum RecordVerificationError { CommitmentError(#[from] CommitmentError), #[error("{}", _0)] - InputValueError(#[from] InputValueError), + DPCRecordValuesError(#[from] DPCRecordValuesError), #[error("{}", _0)] IOError(#[from] IOError), - - #[error("record parameter `{}` not found in state file", _0)] - MissingParameter(String), - - #[error("{}", _0)] - ParseBoolError(#[from] ParseBoolError), - - #[error("{}", _0)] - ParseIntError(#[from] ParseIntError), } diff --git a/state/src/errors/state_leaf_values.rs b/state/src/errors/state_leaf_values.rs index e8f49411b3..60237452e9 100644 --- a/state/src/errors/state_leaf_values.rs +++ b/state/src/errors/state_leaf_values.rs @@ -1,5 +1,18 @@ +use crate::InputValueError; + +use std::{num::ParseIntError, str::ParseBoolError}; + #[derive(Debug, Error)] 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), + + #[error("{}", _0)] + ParseBoolError(#[from] ParseBoolError), + + #[error("{}", _0)] + ParseIntError(#[from] ParseIntError), } diff --git a/state/src/errors/state_values.rs b/state/src/errors/state_values.rs index c4ae7e45c6..7d079e6b25 100644 --- a/state/src/errors/state_values.rs +++ b/state/src/errors/state_values.rs @@ -1,5 +1,18 @@ +use crate::InputValueError; + +use std::{num::ParseIntError, str::ParseBoolError}; + #[derive(Debug, Error)] pub enum StateValuesError { + #[error("{}", _0)] + InputValueError(#[from] InputValueError), + #[error("state parameter `{}` not found in state file", _0)] MissingParameter(String), + + #[error("{}", _0)] + ParseBoolError(#[from] ParseBoolError), + + #[error("{}", _0)] + ParseIntError(#[from] ParseIntError), } diff --git a/state/src/local_data_commitment/local_data_commitment.rs b/state/src/local_data_commitment/local_data_commitment.rs index 09bceda873..a2cb6567bf 100644 --- a/state/src/local_data_commitment/local_data_commitment.rs +++ b/state/src/local_data_commitment/local_data_commitment.rs @@ -19,19 +19,19 @@ pub fn verify_local_data_commitment( ) -> Result { // verify record commitment 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 = dpc_record_values.commitment; let record_serial_number: Vec = dpc_record_values.serial_number; // parse typed state values 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 root: Vec = state_values.root; // parse typed state leaf values 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> = state_leaf_values.path; let memo: Vec = state_leaf_values.memo; 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 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 { - to_bytes![record_commitment, memo, network_id].unwrap() + to_bytes![record_commitment, memo, network_id]? }; // 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 = - 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 @@ -74,11 +74,9 @@ pub fn verify_local_data_commitment( }; // 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 - .verify(&local_data_commitment_root, &local_data_commitment_leaf) - .unwrap(); + let result = local_data_merkle_path.verify(&local_data_commitment_root, &local_data_commitment_leaf)?; Ok(result) } diff --git a/state/src/local_data_commitment/state_leaf_values.rs b/state/src/local_data_commitment/state_leaf_values.rs index 29803450f3..3b60e5037a 100644 --- a/state/src/local_data_commitment/state_leaf_values.rs +++ b/state/src/local_data_commitment/state_leaf_values.rs @@ -20,24 +20,20 @@ impl TryFrom<&TypedStateLeaf> for StateLeafValues { fn try_from(state_leaf: &TypedStateLeaf) -> Result { // Lookup path - let path_value = get_parameter_value(PATH_PARAMETER_STRING.to_owned(), state_leaf).unwrap(); - let path = input_to_nested_u8_vec(path_value).unwrap(); + let path_value = get_parameter_value(PATH_PARAMETER_STRING.to_owned(), state_leaf)?; + let path = input_to_nested_u8_vec(path_value)?; // Lookup memo - let memo_value = get_parameter_value(MEMO_PARAMETER_STRING.to_owned(), state_leaf).unwrap(); - let memo = input_to_u8_vec(memo_value).unwrap(); + let memo_value = get_parameter_value(MEMO_PARAMETER_STRING.to_owned(), state_leaf)?; + let memo = input_to_u8_vec(memo_value)?; // Lookup network id - let network_id_value = get_parameter_value(NETWORK_ID_PARAMETER_STRING.to_owned(), state_leaf).unwrap(); - let network_id = input_to_integer_string(network_id_value) - .unwrap() - .parse::() - .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)?.parse::()?; // Lookup leaf randomness - let leaf_randomness_value = - get_parameter_value(LEAF_RANDOMNESS_PARAMETER_STRING.to_owned(), state_leaf).unwrap(); - let leaf_randomness = input_to_u8_vec(leaf_randomness_value).unwrap(); + let leaf_randomness_value = get_parameter_value(LEAF_RANDOMNESS_PARAMETER_STRING.to_owned(), state_leaf)?; + let leaf_randomness = input_to_u8_vec(leaf_randomness_value)?; Ok(Self { path, diff --git a/state/src/local_data_commitment/state_values.rs b/state/src/local_data_commitment/state_values.rs index d8dc715457..bb848a79d0 100644 --- a/state/src/local_data_commitment/state_values.rs +++ b/state/src/local_data_commitment/state_values.rs @@ -16,15 +16,12 @@ impl TryFrom<&TypedState> for StateValues { fn try_from(state: &TypedState) -> Result { // Lookup leaf index - let leaf_index_value = get_parameter_value(LEAF_INDEX_PARAMETER_STRING.to_owned(), state).unwrap(); - let leaf_index = input_to_integer_string(leaf_index_value) - .unwrap() - .parse::() - .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)?.parse::()?; // Lookup root - let root_value = get_parameter_value(ROOT_PARAMETER_STRING.to_owned(), state).unwrap(); - let root = input_to_u8_vec(root_value).unwrap(); + let root_value = get_parameter_value(ROOT_PARAMETER_STRING.to_owned(), state)?; + let root = input_to_u8_vec(root_value)?; Ok(Self { leaf_index, root }) } diff --git a/state/src/record_commitment/dpc_record_values.rs b/state/src/record_commitment/dpc_record_values.rs index 443934b049..4559383a55 100644 --- a/state/src/record_commitment/dpc_record_values.rs +++ b/state/src/record_commitment/dpc_record_values.rs @@ -1,4 +1,4 @@ -use crate::{utilities::*, RecordVerificationError}; +use crate::{utilities::*, DPCRecordValuesError}; use leo_typed::{InputValue, Record as TypedRecord}; use snarkos_dpc::base_dpc::instantiated::Components; @@ -30,7 +30,7 @@ pub struct DPCRecordValues { } impl TryFrom<&TypedRecord> for DPCRecordValues { - type Error = RecordVerificationError; + type Error = DPCRecordValuesError; fn try_from(record: &TypedRecord) -> Result { // Lookup serial number @@ -89,7 +89,7 @@ impl TryFrom<&TypedRecord> for DPCRecordValues { } } -fn get_parameter_value(name: String, record: &TypedRecord) -> Result { +fn get_parameter_value(name: String, record: &TypedRecord) -> Result { let parameters = record.values(); let matched_parameter = parameters .iter() @@ -98,8 +98,8 @@ fn get_parameter_value(name: String, record: &TypedRecord) -> Result match value_option { 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)), } }