mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-24 16:08:55 +03:00
add find input function to utilities
This commit is contained in:
parent
7ff3fb1b96
commit
a50dfb07bf
@ -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),
|
||||
|
||||
|
@ -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),
|
||||
}
|
||||
|
@ -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),
|
||||
|
||||
|
@ -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),
|
||||
|
||||
|
@ -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<Self, Self::Error> {
|
||||
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::<u8>()?;
|
||||
|
||||
// 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<InputValue, StateLeafValuesError> {
|
||||
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)),
|
||||
}
|
||||
}
|
||||
|
@ -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<Self, Self::Error> {
|
||||
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::<u32>()?;
|
||||
|
||||
// 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<InputValue, StateValuesError> {
|
||||
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)),
|
||||
}
|
||||
}
|
||||
|
@ -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<Self, Self::Error> {
|
||||
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::<Components>::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::<bool>()?;
|
||||
|
||||
// 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::<u64>()?;
|
||||
|
||||
// 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<InputValue, DPCRecordValuesError> {
|
||||
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)),
|
||||
}
|
||||
}
|
||||
|
@ -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<Parameter, Option<InputValue>>,
|
||||
) -> Result<InputValue, InputValueError> {
|
||||
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<String, InputValueError> {
|
||||
match input {
|
||||
|
Loading…
Reference in New Issue
Block a user