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)]
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 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)]
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),
}

View File

@ -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),
}

View File

@ -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),
}

View File

@ -19,19 +19,19 @@ pub fn verify_local_data_commitment(
) -> Result<bool, LocalDataVerificationError> {
// 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<u8> = dpc_record_values.commitment;
let record_serial_number: Vec<u8> = 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<u8> = 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<Vec<u8>> = state_leaf_values.path;
let memo: Vec<u8> = 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)
}

View File

@ -20,24 +20,20 @@ impl TryFrom<&TypedStateLeaf> for StateLeafValues {
fn try_from(state_leaf: &TypedStateLeaf) -> Result<Self, Self::Error> {
// 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::<u8>()
.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::<u8>()?;
// 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,

View File

@ -16,15 +16,12 @@ impl TryFrom<&TypedState> for StateValues {
fn try_from(state: &TypedState) -> Result<Self, Self::Error> {
// 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::<u32>()
.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::<u32>()?;
// 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 })
}

View File

@ -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<Self, Self::Error> {
// 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 matched_parameter = parameters
.iter()
@ -98,8 +98,8 @@ fn get_parameter_value(name: String, record: &TypedRecord) -> Result<InputValue,
match matched_parameter {
Some((_parameter, value_option)) => 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)),
}
}