From a50dfb07bf0aef2e87459384ed78cc15233719b0 Mon Sep 17 00:00:00 2001 From: collin Date: Wed, 12 Aug 2020 13:29:02 -0700 Subject: [PATCH] add find input function to utilities --- state/src/errors/dpc_record_values.rs | 3 -- state/src/errors/input_value.rs | 3 ++ state/src/errors/state_leaf_values.rs | 3 -- state/src/errors/state_values.rs | 3 -- .../state_leaf_values.rs | 29 ++++---------- .../src/local_data_commitment/state_values.rs | 25 +++--------- .../record_commitment/dpc_record_values.rs | 40 ++++++------------- state/src/utilities/input_value.rs | 20 +++++++++- 8 files changed, 49 insertions(+), 77 deletions(-) diff --git a/state/src/errors/dpc_record_values.rs b/state/src/errors/dpc_record_values.rs index d063ac8824..f659b134b0 100644 --- a/state/src/errors/dpc_record_values.rs +++ b/state/src/errors/dpc_record_values.rs @@ -12,9 +12,6 @@ pub enum DPCRecordValuesError { #[error("{}", _0)] InputValueError(#[from] InputValueError), - #[error("record parameter `{}` not found in state file", _0)] - MissingParameter(String), - #[error("{}", _0)] ParseBoolError(#[from] ParseBoolError), diff --git a/state/src/errors/input_value.rs b/state/src/errors/input_value.rs index 0af7057d5d..1504d0ebef 100644 --- a/state/src/errors/input_value.rs +++ b/state/src/errors/input_value.rs @@ -8,6 +8,9 @@ pub enum InputValueError { #[error("expected integer parameter, found `{}`", _0)] ExpectedInteger(String), + #[error("input parameter `{}` not found in state file", _0)] + MissingParameter(String), + #[error("{}", _0)] ParseIntError(#[from] ParseIntError), } diff --git a/state/src/errors/state_leaf_values.rs b/state/src/errors/state_leaf_values.rs index 60237452e9..b14620eb89 100644 --- a/state/src/errors/state_leaf_values.rs +++ b/state/src/errors/state_leaf_values.rs @@ -7,9 +7,6 @@ pub enum StateLeafValuesError { #[error("{}", _0)] InputValueError(#[from] InputValueError), - #[error("state leaf parameter `{}` not found in state file", _0)] - MissingParameter(String), - #[error("{}", _0)] ParseBoolError(#[from] ParseBoolError), diff --git a/state/src/errors/state_values.rs b/state/src/errors/state_values.rs index 7d079e6b25..72b47c51bc 100644 --- a/state/src/errors/state_values.rs +++ b/state/src/errors/state_values.rs @@ -7,9 +7,6 @@ pub enum StateValuesError { #[error("{}", _0)] InputValueError(#[from] InputValueError), - #[error("state parameter `{}` not found in state file", _0)] - MissingParameter(String), - #[error("{}", _0)] ParseBoolError(#[from] ParseBoolError), diff --git a/state/src/local_data_commitment/state_leaf_values.rs b/state/src/local_data_commitment/state_leaf_values.rs index 3b60e5037a..b0a9ef34b7 100644 --- a/state/src/local_data_commitment/state_leaf_values.rs +++ b/state/src/local_data_commitment/state_leaf_values.rs @@ -1,6 +1,6 @@ -use crate::{input_to_integer_string, input_to_nested_u8_vec, input_to_u8_vec, StateLeafValuesError}; +use crate::{find_input, input_to_integer_string, input_to_nested_u8_vec, input_to_u8_vec, StateLeafValuesError}; -use leo_typed::{InputValue, StateLeaf as TypedStateLeaf}; +use leo_typed::StateLeaf as TypedStateLeaf; use std::convert::TryFrom; static PATH_PARAMETER_STRING: &str = "path"; @@ -19,20 +19,22 @@ impl TryFrom<&TypedStateLeaf> for StateLeafValues { type Error = StateLeafValuesError; fn try_from(state_leaf: &TypedStateLeaf) -> Result { + let parameters = state_leaf.values(); + // Lookup path - let path_value = get_parameter_value(PATH_PARAMETER_STRING.to_owned(), state_leaf)?; + let path_value = find_input(PATH_PARAMETER_STRING.to_owned(), ¶meters)?; let path = input_to_nested_u8_vec(path_value)?; // Lookup memo - let memo_value = get_parameter_value(MEMO_PARAMETER_STRING.to_owned(), state_leaf)?; + let memo_value = find_input(MEMO_PARAMETER_STRING.to_owned(), ¶meters)?; 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)?; + let network_id_value = find_input(NETWORK_ID_PARAMETER_STRING.to_owned(), ¶meters)?; 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)?; + let leaf_randomness_value = find_input(LEAF_RANDOMNESS_PARAMETER_STRING.to_owned(), ¶meters)?; let leaf_randomness = input_to_u8_vec(leaf_randomness_value)?; Ok(Self { @@ -43,18 +45,3 @@ impl TryFrom<&TypedStateLeaf> for StateLeafValues { }) } } - -fn get_parameter_value(name: String, state: &TypedStateLeaf) -> Result { - let parameters = state.values(); - let matched_parameter = parameters - .iter() - .find(|(parameter, _value)| parameter.variable.name == name); - - match matched_parameter { - Some((_parameter, value_option)) => match value_option { - Some(value) => Ok(value.clone()), - None => Err(StateLeafValuesError::MissingParameter(name)), - }, - None => Err(StateLeafValuesError::MissingParameter(name)), - } -} diff --git a/state/src/local_data_commitment/state_values.rs b/state/src/local_data_commitment/state_values.rs index bb848a79d0..cd017b2d4e 100644 --- a/state/src/local_data_commitment/state_values.rs +++ b/state/src/local_data_commitment/state_values.rs @@ -1,6 +1,6 @@ -use crate::{input_to_integer_string, input_to_u8_vec, StateValuesError}; +use crate::{find_input, input_to_integer_string, input_to_u8_vec, StateValuesError}; -use leo_typed::{InputValue, State as TypedState}; +use leo_typed::State as TypedState; use std::convert::TryFrom; static LEAF_INDEX_PARAMETER_STRING: &str = "leaf_index"; @@ -15,29 +15,16 @@ impl TryFrom<&TypedState> for StateValues { type Error = StateValuesError; fn try_from(state: &TypedState) -> Result { + let parameters = state.values(); + // Lookup leaf index - let leaf_index_value = get_parameter_value(LEAF_INDEX_PARAMETER_STRING.to_owned(), state)?; + let leaf_index_value = find_input(LEAF_INDEX_PARAMETER_STRING.to_owned(), ¶meters)?; 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)?; + let root_value = find_input(ROOT_PARAMETER_STRING.to_owned(), ¶meters)?; let root = input_to_u8_vec(root_value)?; Ok(Self { leaf_index, root }) } } - -fn get_parameter_value(name: String, state: &TypedState) -> Result { - let parameters = state.values(); - let matched_parameter = parameters - .iter() - .find(|(parameter, _value)| parameter.variable.name == name); - - match matched_parameter { - Some((_parameter, value_option)) => match value_option { - Some(value) => Ok(value.clone()), - None => Err(StateValuesError::MissingParameter(name)), - }, - None => Err(StateValuesError::MissingParameter(name)), - } -} diff --git a/state/src/record_commitment/dpc_record_values.rs b/state/src/record_commitment/dpc_record_values.rs index 4559383a55..5460d0bb46 100644 --- a/state/src/record_commitment/dpc_record_values.rs +++ b/state/src/record_commitment/dpc_record_values.rs @@ -1,5 +1,5 @@ use crate::{utilities::*, DPCRecordValuesError}; -use leo_typed::{InputValue, Record as TypedRecord}; +use leo_typed::Record as TypedRecord; use snarkos_dpc::base_dpc::instantiated::Components; use snarkos_objects::AccountAddress; @@ -33,45 +33,46 @@ impl TryFrom<&TypedRecord> for DPCRecordValues { type Error = DPCRecordValuesError; fn try_from(record: &TypedRecord) -> Result { + let parameters = record.values(); + // Lookup serial number - let serial_number_value = get_parameter_value(SERIAL_NUMBER_PARAMETER_STRING.to_owned(), record)?; + let serial_number_value = find_input(SERIAL_NUMBER_PARAMETER_STRING.to_owned(), ¶meters)?; let serial_number = input_to_u8_vec(serial_number_value)?; // Lookup record owner - let owner_value = get_parameter_value(OWNER_PARAMETER_STRING.to_owned(), record)?; + let owner_value = find_input(OWNER_PARAMETER_STRING.to_owned(), ¶meters)?; let owner = AccountAddress::::from_str(&format!("{}", owner_value))?; // Lookup record is_dummy - let is_dummy_value = get_parameter_value(IS_DUMMY_PARAMETER_STRING.to_owned(), record)?; + let is_dummy_value = find_input(IS_DUMMY_PARAMETER_STRING.to_owned(), ¶meters)?; let is_dummy = is_dummy_value.to_string().parse::()?; // Lookup record value - let value_value = get_parameter_value(VALUE_PARAMETER_STRING.to_owned(), record)?; + let value_value = find_input(VALUE_PARAMETER_STRING.to_owned(), ¶meters)?; let value = input_to_integer_string(value_value)?.parse::()?; // Lookup record payload - let payload_value = get_parameter_value(PAYLOAD_PARAMETER_STRING.to_owned(), record)?; + let payload_value = find_input(PAYLOAD_PARAMETER_STRING.to_owned(), ¶meters)?; let payload = input_to_u8_vec(payload_value)?; // Lookup record birth program id - let birth_program_id_value = get_parameter_value(BIRTH_PROGRAM_ID_PARAMETER_STRING.to_owned(), record)?; + let birth_program_id_value = find_input(BIRTH_PROGRAM_ID_PARAMETER_STRING.to_owned(), ¶meters)?; let birth_program_id = input_to_u8_vec(birth_program_id_value)?; // Lookup record death program id - let death_program_id_value = get_parameter_value(DEATH_PROGRAM_ID_PARAMETER_STRING.to_owned(), record)?; + let death_program_id_value = find_input(DEATH_PROGRAM_ID_PARAMETER_STRING.to_owned(), ¶meters)?; let death_program_id = input_to_u8_vec(death_program_id_value)?; // Lookup record serial number nonce - let serial_number_nonce_value = get_parameter_value(SERIAL_NUMBER_NONCE_PARAMETER_STRING.to_owned(), record)?; + let serial_number_nonce_value = find_input(SERIAL_NUMBER_NONCE_PARAMETER_STRING.to_owned(), ¶meters)?; let serial_number_nonce = input_to_u8_vec(serial_number_nonce_value)?; // Lookup record commitment - let commitment_value = get_parameter_value(COMMITMENT_PARAMETER_STRING.to_owned(), record)?; + let commitment_value = find_input(COMMITMENT_PARAMETER_STRING.to_owned(), ¶meters)?; let commitment = input_to_u8_vec(commitment_value)?; // Lookup record commitment randomness - let commitment_randomness_value = - get_parameter_value(COMMITMENT_RANDOMNESS_PARAMETER_STRING.to_owned(), record)?; + let commitment_randomness_value = find_input(COMMITMENT_RANDOMNESS_PARAMETER_STRING.to_owned(), ¶meters)?; let commitment_randomness = input_to_u8_vec(commitment_randomness_value)?; Ok(Self { @@ -88,18 +89,3 @@ impl TryFrom<&TypedRecord> for DPCRecordValues { }) } } - -fn get_parameter_value(name: String, record: &TypedRecord) -> Result { - let parameters = record.values(); - let matched_parameter = parameters - .iter() - .find(|(parameter, _value)| parameter.variable.name == name); - - match matched_parameter { - Some((_parameter, value_option)) => match value_option { - Some(value) => Ok(value.clone()), - None => Err(DPCRecordValuesError::MissingParameter(name)), - }, - None => Err(DPCRecordValuesError::MissingParameter(name)), - } -} diff --git a/state/src/utilities/input_value.rs b/state/src/utilities/input_value.rs index 9746011e80..0e427a3bc7 100644 --- a/state/src/utilities/input_value.rs +++ b/state/src/utilities/input_value.rs @@ -1,5 +1,23 @@ use crate::InputValueError; -use leo_typed::InputValue; +use leo_typed::{InputValue, Parameter}; +use std::collections::HashMap; + +pub fn find_input( + name: String, + parameters: &HashMap>, +) -> Result { + let matched_parameter = parameters + .iter() + .find(|(parameter, _value)| parameter.variable.name == name); + + match matched_parameter { + Some((_parameter, value_option)) => match value_option { + Some(value) => Ok(value.clone()), + None => Err(InputValueError::MissingParameter(name)), + }, + None => Err(InputValueError::MissingParameter(name)), + } +} pub fn input_to_integer_string(input: InputValue) -> Result { match input {