chore: add statement and KV metadata to frontend PODs (#117)
* Add statement and KV metadata to frontend PODs * Code review
This commit is contained in:
parent
02ec7c311b
commit
6627b46819
17 changed files with 290 additions and 224 deletions
|
|
@ -21,7 +21,7 @@ pub struct Dictionary {
|
|||
|
||||
impl Dictionary {
|
||||
pub fn new(kvs: &HashMap<Hash, Value>) -> Result<Self> {
|
||||
let kvs: HashMap<Value, Value> = kvs.into_iter().map(|(&k, &v)| (Value(k.0), v)).collect();
|
||||
let kvs: HashMap<Value, Value> = kvs.iter().map(|(&k, &v)| (Value(k.0), v)).collect();
|
||||
Ok(Self {
|
||||
mt: MerkleTree::new(MAX_DEPTH, &kvs)?,
|
||||
})
|
||||
|
|
@ -75,7 +75,7 @@ pub struct Set {
|
|||
impl Set {
|
||||
pub fn new(set: &Vec<Value>) -> Result<Self> {
|
||||
let kvs: HashMap<Value, Value> = set
|
||||
.into_iter()
|
||||
.iter()
|
||||
.map(|e| {
|
||||
let h = hash_value(e);
|
||||
(Value::from(h), EMPTY)
|
||||
|
|
@ -128,7 +128,7 @@ pub struct Array {
|
|||
impl Array {
|
||||
pub fn new(array: &Vec<Value>) -> Result<Self> {
|
||||
let kvs: HashMap<Value, Value> = array
|
||||
.into_iter()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, &e)| (Value::from(i as i64), e))
|
||||
.collect();
|
||||
|
|
|
|||
|
|
@ -26,8 +26,8 @@ impl HashOrWildcard {
|
|||
/// match is possible.
|
||||
pub fn match_against(&self, v: &Value) -> Result<Option<(usize, Value)>> {
|
||||
match self {
|
||||
HashOrWildcard::Hash(h) if &Value::from(h.clone()) == v => Ok(None),
|
||||
HashOrWildcard::Wildcard(i) => Ok(Some((*i, v.clone()))),
|
||||
HashOrWildcard::Hash(h) if &Value::from(*h) == v => Ok(None),
|
||||
HashOrWildcard::Wildcard(i) => Ok(Some((*i, *v))),
|
||||
_ => Err(anyhow!(
|
||||
"Failed to match hash or wildcard {} against value {}.",
|
||||
self,
|
||||
|
|
@ -79,9 +79,9 @@ impl StatementTmplArg {
|
|||
(Self::None, StatementArg::None) => Ok(vec![]),
|
||||
(Self::Literal(v), StatementArg::Literal(w)) if v == w => Ok(vec![]),
|
||||
(Self::Key(tmpl_o, tmpl_k), StatementArg::Key(AnchoredKey(PodId(o), k))) => {
|
||||
let o_corr = tmpl_o.match_against(&o.clone().into())?;
|
||||
let k_corr = tmpl_k.match_against(&k.clone().into())?;
|
||||
Ok([o_corr, k_corr].into_iter().flat_map(|x| x).collect())
|
||||
let o_corr = tmpl_o.match_against(&(*o).into())?;
|
||||
let k_corr = tmpl_k.match_against(&(*k).into())?;
|
||||
Ok([o_corr, k_corr].into_iter().flatten().collect())
|
||||
}
|
||||
_ => Err(anyhow!(
|
||||
"Failed to match statement template argument {:?} against statement argument {:?}.",
|
||||
|
|
@ -110,15 +110,15 @@ impl ToFields for StatementTmplArg {
|
|||
}
|
||||
StatementTmplArg::Literal(v) => {
|
||||
let fields: Vec<F> = std::iter::once(F::from_canonical_u64(1))
|
||||
.chain(v.to_fields(_params).0.into_iter())
|
||||
.chain(v.to_fields(_params).0)
|
||||
.chain(std::iter::repeat_with(|| F::from_canonical_u64(0)).take(hash_size))
|
||||
.collect();
|
||||
(fields, statement_tmpl_arg_size)
|
||||
}
|
||||
StatementTmplArg::Key(hw1, hw2) => {
|
||||
let fields: Vec<F> = std::iter::once(F::from_canonical_u64(2))
|
||||
.chain(hw1.to_fields(_params).0.into_iter())
|
||||
.chain(hw2.to_fields(_params).0.into_iter())
|
||||
.chain(hw1.to_fields(_params).0)
|
||||
.chain(hw2.to_fields(_params).0)
|
||||
.collect();
|
||||
(fields, statement_tmpl_arg_size)
|
||||
}
|
||||
|
|
@ -325,8 +325,8 @@ impl ToFields for CustomPredicateBatch {
|
|||
impl CustomPredicateBatch {
|
||||
pub fn hash(&self, _params: &Params) -> Hash {
|
||||
let input = self.to_fields(_params).0;
|
||||
let h = hash_fields(&input);
|
||||
h
|
||||
|
||||
hash_fields(&input)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -335,7 +335,7 @@ pub struct CustomPredicateRef(pub Arc<CustomPredicateBatch>, pub usize);
|
|||
|
||||
impl CustomPredicateRef {
|
||||
pub fn arg_len(&self) -> usize {
|
||||
(*self.0).predicates[self.1].args_len
|
||||
self.0.predicates[self.1].args_len
|
||||
}
|
||||
pub fn match_against(&self, statements: &[Statement]) -> Result<HashMap<usize, Value>> {
|
||||
let mut bindings = HashMap::new();
|
||||
|
|
@ -414,7 +414,7 @@ impl ToFields for Predicate {
|
|||
// in every case: pad to (hash_size + 2) field elements
|
||||
let mut fields: Vec<F> = match self {
|
||||
Self::Native(p) => std::iter::once(F::from_canonical_u64(1))
|
||||
.chain(p.to_fields(_params).0.into_iter())
|
||||
.chain(p.to_fields(_params).0)
|
||||
.collect(),
|
||||
Self::BatchSelf(i) => std::iter::once(F::from_canonical_u64(2))
|
||||
.chain(std::iter::once(F::from_canonical_usize(*i)))
|
||||
|
|
|
|||
|
|
@ -133,7 +133,7 @@ impl Params {
|
|||
self.max_custom_batch_size * self.custom_predicate_size()
|
||||
}
|
||||
|
||||
pub fn print_serialized_sizes(&self) -> () {
|
||||
pub fn print_serialized_sizes(&self) {
|
||||
println!("Parameter sizes:");
|
||||
println!(
|
||||
" Statement template argument: {}",
|
||||
|
|
@ -146,7 +146,7 @@ impl Params {
|
|||
" Custom predicate batch: {}",
|
||||
self.custom_predicate_batch_size_field_elts()
|
||||
);
|
||||
println!("");
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,9 @@
|
|||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
use super::{CustomPredicateRef, Statement};
|
||||
use crate::{
|
||||
middleware::{
|
||||
AnchoredKey, CustomPredicate, Params, PodId, Predicate, StatementTmpl, Value, SELF,
|
||||
},
|
||||
util::hashmap_insert_no_dupe,
|
||||
};
|
||||
use crate::middleware::{AnchoredKey, Params, Value, SELF};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum OperationType {
|
||||
|
|
@ -109,7 +103,7 @@ impl Operation {
|
|||
pub fn op(op_code: OperationType, args: &[Statement]) -> Result<Self> {
|
||||
type NO = NativeOperation;
|
||||
let arg_tup = (
|
||||
args.get(0).cloned(),
|
||||
args.first().cloned(),
|
||||
args.get(1).cloned(),
|
||||
args.get(2).cloned(),
|
||||
);
|
||||
|
|
@ -148,7 +142,7 @@ impl Operation {
|
|||
})
|
||||
}
|
||||
/// Checks the given operation against a statement.
|
||||
pub fn check(&self, params: &Params, output_statement: &Statement) -> Result<bool> {
|
||||
pub fn check(&self, _params: &Params, output_statement: &Statement) -> Result<bool> {
|
||||
use Statement::*;
|
||||
match (self, output_statement) {
|
||||
(Self::None, None) => Ok(true),
|
||||
|
|
@ -189,9 +183,9 @@ impl Operation {
|
|||
Self::SumOf(ValueOf(ak1, v1), ValueOf(ak2, v2), ValueOf(ak3, v3)),
|
||||
SumOf(ak4, ak5, ak6),
|
||||
) => {
|
||||
let v1: i64 = v1.clone().try_into()?;
|
||||
let v2: i64 = v2.clone().try_into()?;
|
||||
let v3: i64 = v3.clone().try_into()?;
|
||||
let v1: i64 = (*v1).try_into()?;
|
||||
let v2: i64 = (*v2).try_into()?;
|
||||
let v3: i64 = (*v3).try_into()?;
|
||||
Ok((v1 == v2 + v3) && ak4 == ak1 && ak5 == ak2 && ak6 == ak3)
|
||||
}
|
||||
(Self::Custom(CustomPredicateRef(cpb, i), args), Custom(cpr, s_args))
|
||||
|
|
@ -214,8 +208,8 @@ impl Operation {
|
|||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
let s_args = s_args
|
||||
.into_iter()
|
||||
.flat_map(|AnchoredKey(o, k)| [Value::from(o.0.clone()), k.clone().into()])
|
||||
.iter()
|
||||
.flat_map(|AnchoredKey(o, k)| [Value::from(o.0), (*k).into()])
|
||||
.collect::<Vec<_>>();
|
||||
if bound_args != s_args {
|
||||
Err(anyhow!("Arguments to output statement {} do not match those implied by operation {:?}", output_statement,self))
|
||||
|
|
@ -237,7 +231,7 @@ impl fmt::Display for Operation {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
writeln!(f, "middleware::Operation:")?;
|
||||
writeln!(f, " {:?} ", self.code())?;
|
||||
for (i, arg) in self.args().iter().enumerate() {
|
||||
for (_, arg) in self.args().iter().enumerate() {
|
||||
writeln!(f, " {}", arg)?;
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ impl Statement {
|
|||
Self::SumOf(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)],
|
||||
Self::ProductOf(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)],
|
||||
Self::MaxOf(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)],
|
||||
Self::Custom(_, args) => Vec::from_iter(args.into_iter().map(|h| Key(h))),
|
||||
Self::Custom(_, args) => Vec::from_iter(args.into_iter().map(Key)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue