small fixes for various todos (#62)
* todos for frontend * mock_main todos * have OperationArgError bubble up * revert frontend.rs
This commit is contained in:
parent
bb865a4fea
commit
452bda8087
2 changed files with 42 additions and 21 deletions
|
|
@ -8,6 +8,7 @@ use itertools::Itertools;
|
|||
use plonky2::hash::poseidon::PoseidonHash;
|
||||
use plonky2::plonk::config::Hasher;
|
||||
use std::any::Any;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
|
||||
pub struct MockProver {}
|
||||
|
|
@ -30,6 +31,23 @@ impl OperationArg {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
enum OperationArgError {
|
||||
KeyNotFound,
|
||||
StatementNotFound,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for OperationArgError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
OperationArgError::KeyNotFound => write!(f, "Key not found"),
|
||||
OperationArgError::StatementNotFound => write!(f, "Statement not found"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for OperationArgError {}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
struct Operation(pub NativeOperation, pub Vec<OperationArg>);
|
||||
|
||||
|
|
@ -236,11 +254,13 @@ impl MockMainPod {
|
|||
statements
|
||||
}
|
||||
|
||||
fn find_op_arg(statements: &[Statement], op_arg: &middleware::OperationArg) -> OperationArg {
|
||||
pub fn find_op_arg(
|
||||
statements: &[Statement],
|
||||
op_arg: &middleware::OperationArg,
|
||||
) -> Result<OperationArg, OperationArgError> {
|
||||
match op_arg {
|
||||
middleware::OperationArg::None => OperationArg::None,
|
||||
middleware::OperationArg::Key(k) => OperationArg::Index(
|
||||
// TODO: Error handling when the key is not found in any ValueOf statement
|
||||
middleware::OperationArg::None => Ok(OperationArg::None),
|
||||
middleware::OperationArg::Key(k) => {
|
||||
statements
|
||||
.iter()
|
||||
.enumerate()
|
||||
|
|
@ -251,16 +271,17 @@ impl MockMainPod {
|
|||
},
|
||||
_ => None,
|
||||
})
|
||||
.unwrap(),
|
||||
),
|
||||
middleware::OperationArg::Statement(st) => OperationArg::Index(
|
||||
// TODO: Error handling when the statement is not found
|
||||
.map(OperationArg::Index)
|
||||
.ok_or(OperationArgError::KeyNotFound)
|
||||
}
|
||||
middleware::OperationArg::Statement(st) => {
|
||||
statements
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find_map(|(i, s)| (s == st).then_some(i))
|
||||
.unwrap(),
|
||||
),
|
||||
.map(OperationArg::Index)
|
||||
.ok_or(OperationArgError::StatementNotFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -268,7 +289,7 @@ impl MockMainPod {
|
|||
params: &Params,
|
||||
statements: &[Statement],
|
||||
input_operations: &[middleware::Operation],
|
||||
) -> Vec<Operation> {
|
||||
) -> Result<Vec<Operation>, OperationArgError> {
|
||||
let op_none = Self::operation_none(params);
|
||||
|
||||
let mut operations = Vec::new();
|
||||
|
|
@ -278,11 +299,12 @@ impl MockMainPod {
|
|||
Self::pad_operation_args(params, &mut mid_args);
|
||||
let mut args = Vec::with_capacity(mid_args.len());
|
||||
for mid_arg in &mid_args {
|
||||
args.push(Self::find_op_arg(statements, mid_arg));
|
||||
let op_arg = Self::find_op_arg(statements, mid_arg)?;
|
||||
args.push(op_arg)
|
||||
}
|
||||
operations.push(Operation(op.0, args));
|
||||
}
|
||||
operations
|
||||
Ok(operations)
|
||||
}
|
||||
|
||||
// NOTE: In this implementation public statements are always copies from previous statements,
|
||||
|
|
@ -291,9 +313,7 @@ impl MockMainPod {
|
|||
params: &Params,
|
||||
statements: &[Statement],
|
||||
mut operations: Vec<Operation>,
|
||||
) -> Vec<Operation> {
|
||||
let op_none = Self::operation_none(params);
|
||||
|
||||
) -> Result<Vec<Operation>, OperationArgError> {
|
||||
let offset_public_statements = statements.len() - params.max_public_statements;
|
||||
operations.push(Operation(NativeOperation::NewEntry, vec![]));
|
||||
for i in 0..(params.max_public_statements - 1) {
|
||||
|
|
@ -302,15 +322,16 @@ impl MockMainPod {
|
|||
Operation(NativeOperation::None, vec![])
|
||||
} else {
|
||||
let mid_arg = middleware::OperationArg::Statement(st.clone());
|
||||
let op_arg = Self::find_op_arg(statements, &mid_arg)?;
|
||||
Operation(
|
||||
NativeOperation::CopyStatement,
|
||||
vec![Self::find_op_arg(statements, &mid_arg)],
|
||||
vec![op_arg],
|
||||
)
|
||||
};
|
||||
fill_pad(&mut op.1, OperationArg::None, params.max_operation_args);
|
||||
operations.push(op);
|
||||
}
|
||||
operations
|
||||
Ok(operations)
|
||||
}
|
||||
|
||||
pub fn new(params: &Params, inputs: MainPodInputs) -> Result<Self> {
|
||||
|
|
@ -321,9 +342,9 @@ impl MockMainPod {
|
|||
// value=PodType::MockMainPod`
|
||||
let statements = Self::layout_statements(params, &inputs);
|
||||
let operations =
|
||||
Self::process_private_statements_operations(params, &statements, inputs.operations);
|
||||
Self::process_private_statements_operations(params, &statements, inputs.operations)?;
|
||||
let operations =
|
||||
Self::process_public_statements_operations(params, &statements, operations);
|
||||
Self::process_public_statements_operations(params, &statements, operations)?;
|
||||
|
||||
let input_signed_pods = inputs
|
||||
.signed_pods
|
||||
|
|
|
|||
|
|
@ -613,4 +613,4 @@ pub mod tests {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue