Frontend work (#109)

This commit is contained in:
Ahmad Afuni 2025-03-05 21:02:28 +10:00 committed by GitHub
parent 7eeb595dc2
commit 9d60b0ec3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 611 additions and 262 deletions

View file

@ -63,39 +63,39 @@ where
}
}
struct StatementTmplBuilder {
pub struct StatementTmplBuilder {
predicate: Predicate,
args: Vec<BuilderArg>,
}
impl StatementTmplBuilder {
fn new(p: impl Into<Predicate>) -> StatementTmplBuilder {
pub fn new(p: impl Into<Predicate>) -> StatementTmplBuilder {
StatementTmplBuilder {
predicate: p.into(),
args: Vec::new(),
}
}
fn arg(mut self, a: impl Into<BuilderArg>) -> Self {
pub fn arg(mut self, a: impl Into<BuilderArg>) -> Self {
self.args.push(a.into());
self
}
}
struct CustomPredicateBatchBuilder {
name: String,
predicates: Vec<CustomPredicate>,
pub struct CustomPredicateBatchBuilder {
pub name: String,
pub predicates: Vec<CustomPredicate>,
}
impl CustomPredicateBatchBuilder {
fn new(name: String) -> Self {
pub fn new(name: String) -> Self {
Self {
name,
predicates: Vec::new(),
}
}
fn predicate_and(
pub fn predicate_and(
&mut self,
params: &Params,
args: &[&str],
@ -105,7 +105,7 @@ impl CustomPredicateBatchBuilder {
self.predicate(params, true, args, priv_args, sts)
}
fn predicate_or(
pub fn predicate_or(
&mut self,
params: &Params,
args: &[&str],
@ -147,7 +147,7 @@ impl CustomPredicateBatchBuilder {
Ok(Predicate::BatchSelf(self.predicates.len() - 1))
}
fn finish(self) -> Arc<CustomPredicateBatch> {
pub fn finish(self) -> Arc<CustomPredicateBatch> {
Arc::new(CustomPredicateBatch {
name: self.name,
predicates: self.predicates,
@ -171,7 +171,10 @@ fn resolve_wildcard(args: &[&str], priv_args: &[&str], v: &HashOrWildcardStr) ->
#[cfg(test)]
mod tests {
use super::*;
use crate::middleware::{CustomPredicateRef, Params, PodType};
use crate::{
examples::custom::{eth_dos_batch, eth_friend_batch},
middleware::{CustomPredicateRef, Params, PodType},
};
#[test]
fn test_custom_pred() -> Result<()> {
@ -181,146 +184,14 @@ mod tests {
let params = Params::default();
params.print_serialized_sizes();
let mut builder = CustomPredicateBatchBuilder::new("eth_friend".into());
let _eth_friend = builder.predicate_and(
&params,
// arguments:
&["src_ori", "src_key", "dst_ori", "dst_key"],
// private arguments:
&["attestation_pod"],
// statement templates:
&[
// there is an attestation pod that's a SignedPod
STB::new(NP::ValueOf)
.arg(("attestation_pod", literal("type")))
.arg(PodType::Signed),
// the attestation pod is signed by (src_or, src_key)
STB::new(NP::Equal)
.arg(("attestation_pod", literal("signer")))
.arg(("src_ori", "src_key")),
// that same attestation pod has an "attestation"
STB::new(NP::Equal)
.arg(("attestation_pod", literal("attestation")))
.arg(("dst_ori", "dst_key")),
],
)?;
// ETH friend custom predicate batch
let eth_friend = eth_friend_batch(&params)?;
println!("a.0. eth_friend = {}", builder.predicates.last().unwrap());
let eth_friend = builder.finish();
// This batch only has 1 predicate, so we pick it already for convenience
let eth_friend = Predicate::Custom(CustomPredicateRef(eth_friend, 0));
// next chunk builds:
// eth_dos_distance_base(src_or, src_key, dst_or, dst_key, distance_or, distance_key) = and<
// eq(src_or, src_key, dst_or, dst_key),
// ValueOf(distance_or, distance_key, 0)
// >
let mut builder = CustomPredicateBatchBuilder::new("eth_dos_distance_base".into());
let eth_dos_distance_base = builder.predicate_and(
&params,
&[
// arguments:
"src_ori",
"src_key",
"dst_ori",
"dst_key",
"distance_ori",
"distance_key",
],
&[ // private arguments:
],
&[
// statement templates:
STB::new(NP::Equal)
.arg(("src_ori", "src_key"))
.arg(("dst_ori", "dst_key")),
STB::new(NP::ValueOf)
.arg(("distance_ori", "distance_key"))
.arg(0),
],
)?;
println!(
"b.0. eth_dos_distance_base = {}",
builder.predicates.last().unwrap()
);
let eth_dos_distance = Predicate::BatchSelf(2);
// next chunk builds:
let eth_dos_distance_ind = builder.predicate_and(
&params,
&[
// arguments:
"src_ori",
"src_key",
"dst_ori",
"dst_key",
"distance_ori",
"distance_key",
],
&[
// private arguments:
"one_ori",
"one_key",
"shorter_distance_ori",
"shorter_distance_key",
"intermed_ori",
"intermed_key",
],
&[
// statement templates:
STB::new(eth_dos_distance)
.arg(("src_ori", "src_key"))
.arg(("intermed_ori", "intermed_key"))
.arg(("shorter_distance_ori", "shorter_distance_key")),
// distance == shorter_distance + 1
STB::new(NP::ValueOf).arg(("one_ori", "one_key")).arg(1),
STB::new(NP::SumOf)
.arg(("distance_ori", "distance_key"))
.arg(("shorter_distance_ori", "shorter_distance_key"))
.arg(("one_ori", "one_key")),
// intermed is a friend of dst
STB::new(eth_friend)
.arg(("intermed_ori", "intermed_key"))
.arg(("dst_ori", "dst_key")),
],
)?;
println!(
"b.1. eth_dos_distance_ind = {}",
builder.predicates.last().unwrap()
);
let _eth_dos_distance = builder.predicate_or(
&params,
&[
"src_ori",
"src_key",
"dst_ori",
"dst_key",
"distance_ori",
"distance_key",
],
&[],
&[
STB::new(eth_dos_distance_base)
.arg(("src_ori", "src_key"))
.arg(("dst_ori", "dst_key"))
.arg(("distance_ori", "distance_key")),
STB::new(eth_dos_distance_ind)
.arg(("src_ori", "src_key"))
.arg(("dst_ori", "dst_key"))
.arg(("distance_ori", "distance_key")),
],
)?;
println!(
"b.2. eth_dos_distance = {}",
builder.predicates.last().unwrap()
);
let eth_dos_batch_b = builder.finish();
let fields = eth_dos_batch_b.to_fields(&params);
let eth_dos_batch = eth_dos_batch(&params)?;
let fields = eth_dos_batch.to_fields(&params);
println!("Batch b, serialized: {:?}", fields);
Ok(())