Skip to content

Commit f26f6a5

Browse files
committed
Clean-up & fixes
1 parent 2b7c5ac commit f26f6a5

File tree

5 files changed

+239
-337
lines changed

5 files changed

+239
-337
lines changed

src/backends/mock_main.rs

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::middleware::{
22
self, hash_str, AnchoredKey, Hash, MainPodInputs, NativeOperation, NativeStatement, NonePod,
3-
Params, Pod, PodId, PodProver, Statement, StatementArg, ToFields, KEY_TYPE, SELF,
3+
Params, Pod, PodId, PodProver, StatementArg, ToFields, KEY_TYPE, SELF,
44
};
55
use anyhow::{anyhow, Result};
66
use itertools::Itertools;
@@ -96,26 +96,26 @@ impl ToFields for Statement {
9696
}
9797
}
9898

99-
impl TryInto<middleware::Statement> for Statement {
99+
impl TryFrom<Statement> for middleware::Statement {
100100
type Error = anyhow::Error;
101-
fn try_into(self) -> Result<middleware::Statement> {
101+
fn try_from(s: Statement) -> Result<Self> {
102102
type S = middleware::Statement;
103103
type NS = NativeStatement;
104104
type SA = StatementArg;
105105
let args = (
106-
self.1.get(0).cloned(),
107-
self.1.get(1).cloned(),
108-
self.1.get(2).cloned(),
106+
s.1.get(0).cloned(),
107+
s.1.get(1).cloned(),
108+
s.1.get(2).cloned(),
109109
);
110-
Ok(match (self.0, args) {
110+
Ok(match (s.0, args) {
111111
(NS::None, _) => S::None,
112-
(NS::ValueOf, (Some(SA::Key(ak)), Some(SA::Literal(v)), None)) => S::ValueOf(ak, v),
113-
(NS::Equal, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => S::Equal(ak1, ak2),
114-
(NS::NotEqual, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => S::NotEqual(ak1, ak2),
115-
(NS::Gt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => S::Gt(ak1, ak2),
116-
(NS::Lt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => S::Lt(ak1, ak2),
117-
(NS::Contains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => S::Contains(ak1, ak2),
118-
(NS::NotContains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => {
112+
(NS::ValueOf, (Some(SA::Key(ak)), Some(SA::Literal(v)), _)) => S::ValueOf(ak, v),
113+
(NS::Equal, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), _)) => S::Equal(ak1, ak2),
114+
(NS::NotEqual, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), _)) => S::NotEqual(ak1, ak2),
115+
(NS::Gt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), _)) => S::Gt(ak1, ak2),
116+
(NS::Lt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), _)) => S::Lt(ak1, ak2),
117+
(NS::Contains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), _)) => S::Contains(ak1, ak2),
118+
(NS::NotContains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), _)) => {
119119
S::NotContains(ak1, ak2)
120120
}
121121
(NS::SumOf, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3)))) => {
@@ -127,7 +127,7 @@ impl TryInto<middleware::Statement> for Statement {
127127
(NS::MaxOf, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3)))) => {
128128
S::MaxOf(ak1, ak2, ak3)
129129
}
130-
_ => Err(anyhow!("Malformed statement expression {}", self))?,
130+
_ => Err(anyhow!("Ill-formed statement expression {:?}", s))?,
131131
})
132132
}
133133
}
@@ -361,13 +361,22 @@ impl MockMainPod {
361361
statements
362362
}
363363

364-
fn find_op_arg(statements: &[Statement], op_arg: &middleware::Statement) -> Result<OperationArg, OperationArgError> {
365-
statements
366-
.iter()
367-
.enumerate()
368-
.find_map(|(i, s)| (s == &op_arg.clone().into()))
369-
.map(OperationArg::Index)
370-
.ok_or(OperationArgError::StatementNotFound)
364+
fn find_op_arg(
365+
statements: &[Statement],
366+
op_arg: &middleware::Statement,
367+
) -> Result<OperationArg, OperationArgError> {
368+
match op_arg {
369+
middleware::Statement::None => Ok(OperationArg::None),
370+
_ => statements
371+
.iter()
372+
.enumerate()
373+
// TODO
374+
.find_map(|(i, s)| {
375+
(&middleware::Statement::try_from(s.clone()).unwrap() == op_arg).then_some(i)
376+
})
377+
.map(OperationArg::Index)
378+
.ok_or(OperationArgError::StatementNotFound),
379+
}
371380
}
372381

373382
fn process_private_statements_operations(
@@ -413,7 +422,7 @@ impl MockMainPod {
413422
Operation(
414423
NativeOperation::CopyStatement,
415424
// TODO
416-
vec![Self::find_op_arg(statements, &mid_arg.try_into().unwrap())],
425+
vec![Self::find_op_arg(statements, &mid_arg.try_into().unwrap())?],
417426
)
418427
};
419428
fill_pad(&mut op.1, OperationArg::None, params.max_operation_args);
@@ -596,7 +605,10 @@ impl Pod for MockMainPod {
596605
pub mod tests {
597606
use super::*;
598607
use crate::backends::mock_signed::MockSigner;
599-
use crate::examples::{great_boy_pod_full_flow, tickets_pod_full_flow, zu_kyc_pod_builder, zu_kyc_sign_pod_builders};
608+
use crate::examples::{
609+
great_boy_pod_full_flow, tickets_pod_full_flow, zu_kyc_pod_builder,
610+
zu_kyc_sign_pod_builders,
611+
};
600612
use crate::middleware;
601613

602614
#[test]
@@ -650,6 +662,6 @@ pub mod tests {
650662
let pod = proof_pod.pod.into_any().downcast::<MockMainPod>().unwrap();
651663

652664
println!("{}", pod);
653-
assert_eq!(pod.verify(), true);
665+
assert_eq!(pod.verify(), true);
654666
}
655667
}

src/backends/mock_signed.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,7 @@ impl Pod for MockSignedPod {
8181
let id = self.id();
8282
self.dict
8383
.iter()
84-
.map(|(k, v)| {
85-
Statement(
86-
NativeStatement::ValueOf,
87-
vec![
88-
StatementArg::Key(AnchoredKey(id, Hash(k.0))),
89-
StatementArg::Literal(*v),
90-
],
91-
)
92-
})
84+
.map(|(k, v)| Statement::ValueOf(AnchoredKey(id, Hash(k.0)), *v))
9385
.collect()
9486
}
9587

src/examples.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,36 @@ pub fn tickets_sign_pod_builder(params: &Params) -> SignedPodBuilder {
208208
builder
209209
}
210210

211-
pub fn tickets_pod_builder(params: &Params, signed_pod: &SignedPod, expected_event_id: i64, expect_consumed: bool, blacklisted_emails: &Value) -> MainPodBuilder {
211+
pub fn tickets_pod_builder(
212+
params: &Params,
213+
signed_pod: &SignedPod,
214+
expected_event_id: i64,
215+
expect_consumed: bool,
216+
blacklisted_emails: &Value,
217+
) -> MainPodBuilder {
212218
// Create a main pod referencing this signed pod with some statements
213219
let mut builder = MainPodBuilder::new(params);
214220
builder.add_signed_pod(signed_pod);
215221
builder.pub_op(op!(eq, (signed_pod, "eventId"), expected_event_id));
216222
builder.pub_op(op!(eq, (signed_pod, "isConsumed"), expect_consumed));
217223
builder.pub_op(op!(eq, (signed_pod, "isRevoked"), false));
218-
builder.pub_op(op!(not_contains, blacklisted_emails, (signed_pod, "attendeeEmail")));
224+
builder.pub_op(op!(
225+
not_contains,
226+
blacklisted_emails,
227+
(signed_pod, "attendeeEmail")
228+
));
219229
builder
220230
}
221231

222232
pub fn tickets_pod_full_flow() -> MainPodBuilder {
223233
let params = Params::default();
224234
let builder = tickets_sign_pod_builder(&params);
225235
let signed_pod = builder.sign(&mut MockSigner { pk: "test".into() }).unwrap();
226-
tickets_pod_builder(&params, &signed_pod, 123, true, &Value::Dictionary(Dictionary::new(&HashMap::new())))
236+
tickets_pod_builder(
237+
&params,
238+
&signed_pod,
239+
123,
240+
true,
241+
&Value::Dictionary(Dictionary::new(&HashMap::new())),
242+
)
227243
}

0 commit comments

Comments
 (0)