Skip to content

Commit 22a309b

Browse files
committed
simplify custom predicate's frontend interfaces, making it less verbose to define Statement Template arguments
The main idea is that when defining the arguments at a statement template, it can be done from 3 different inputs: i. `(&str, literal)`: this is to set a POD and a field, ie. `(POD, literal("field")`) ii. `(&str, &str)`: this is to define a origin-key wildcard pair, ie. `(src_origin, src_dest)` iii. `Value`: this is to define a literal value, ie. `0`
1 parent 466ddb9 commit 22a309b

File tree

1 file changed

+63
-63
lines changed

1 file changed

+63
-63
lines changed

src/frontend/custom.rs

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,52 @@ use crate::middleware::{
77
};
88

99
/// Argument to an statement template
10-
enum HashOrWildcardStr {
11-
Hash(Hash), // represents a literal
10+
pub enum HashOrWildcardStr {
11+
Hash(Hash), // represents a literal key
1212
Wildcard(String),
1313
}
1414

1515
/// helper to build a literal HashOrWildcardStr::Hash from the given str
16-
fn literal(s: &str) -> HashOrWildcardStr {
16+
pub fn literal(s: &str) -> HashOrWildcardStr {
1717
HashOrWildcardStr::Hash(hash_str(s))
1818
}
1919

20-
/// helper to build a HashOrWildcardStr::Wildcard from the given str
20+
/// helper to build a HashOrWildcardStr::Wildcard from the given str. For the
21+
/// moment this method does not need to be public.
2122
fn wildcard(s: &str) -> HashOrWildcardStr {
2223
HashOrWildcardStr::Wildcard(s.to_string())
2324
}
2425

2526
/// Builder Argument for the StatementTmplBuilder
26-
enum BuilderArg {
27+
pub enum BuilderArg {
2728
Literal(Value),
2829
/// Key: (origin, key), where origin & key can be both Hash or Wildcard
2930
Key(HashOrWildcardStr, HashOrWildcardStr),
3031
}
3132

32-
impl From<(HashOrWildcardStr, HashOrWildcardStr)> for BuilderArg {
33-
fn from((pod_id, key): (HashOrWildcardStr, HashOrWildcardStr)) -> Self {
34-
Self::Key(pod_id, key)
33+
/// When defining a `BuilderArg`, it can be done from 3 different inputs:
34+
/// i. (&str, literal): this is to set a POD and a field, ie. (POD, literal("field"))
35+
/// ii. (&str, &str): this is to define a origin-key wildcard pair, ie. (src_origin, src_dest)
36+
/// iii. Value: this is to define a literal value, ie. 0
37+
///
38+
/// case i.
39+
impl From<(&str, HashOrWildcardStr)> for BuilderArg {
40+
fn from((origin, lit): (&str, HashOrWildcardStr)) -> Self {
41+
// ensure that `lit` is of HashOrWildcardStr::Hash type
42+
match lit {
43+
HashOrWildcardStr::Hash(_) => (),
44+
_ => panic!("not supported"),
45+
};
46+
Self::Key(wildcard(&origin), lit)
3547
}
3648
}
37-
49+
/// case ii.
50+
impl From<(&str, &str)> for BuilderArg {
51+
fn from((origin, field): (&str, &str)) -> Self {
52+
Self::Key(wildcard(&origin), wildcard(&field))
53+
}
54+
}
55+
/// case iii.
3856
impl<V> From<V> for BuilderArg
3957
where
4058
V: Into<Value>,
@@ -163,23 +181,23 @@ mod tests {
163181
let mut builder = CustomPredicateBatchBuilder::new("eth_friend".into());
164182
let _eth_friend = builder.predicate_and(
165183
// arguments:
166-
&["src_or", "src_key", "dst_or", "dst_key"],
184+
&["src_ori", "src_key", "dst_ori", "dst_key"],
167185
// private arguments:
168186
&["attestation_pod"],
169187
// statement templates:
170188
&[
171189
// there is an attestation pod that's a SignedPod
172190
STB::new(NP::ValueOf)
173-
.arg((wildcard("attestation_pod"), literal("type")))
191+
.arg(("attestation_pod", literal("type")))
174192
.arg(PodType::Signed),
175193
// the attestation pod is signed by (src_or, src_key)
176194
STB::new(NP::Equal)
177-
.arg((wildcard("attestation_pod"), literal("signer")))
178-
.arg((wildcard("src_or"), wildcard("src_key"))),
195+
.arg(("attestation_pod", literal("signer")))
196+
.arg(("src_ori", "src_key")),
179197
// that same attestation pod has an "attestation"
180198
STB::new(NP::Equal)
181-
.arg((wildcard("attestation_pod"), literal("attestation")))
182-
.arg((wildcard("dst_or"), wildcard("dst_key"))),
199+
.arg(("attestation_pod", literal("attestation")))
200+
.arg(("dst_ori", "dst_key")),
183201
],
184202
);
185203

@@ -197,22 +215,22 @@ mod tests {
197215
let eth_dos_distance_base = builder.predicate_and(
198216
&[
199217
// arguments:
200-
"src_or",
218+
"src_ori",
201219
"src_key",
202-
"dst_or",
220+
"dst_ori",
203221
"dst_key",
204-
"distance_or",
222+
"distance_ori",
205223
"distance_key",
206224
],
207225
&[ // private arguments:
208226
],
209227
&[
210228
// statement templates:
211229
STB::new(NP::Equal)
212-
.arg((wildcard("src_or"), literal("src_key")))
213-
.arg((wildcard("dst_or"), wildcard("dst_key"))),
230+
.arg(("src_ori", "src_key"))
231+
.arg(("dst_ori", "dst_key")),
214232
STB::new(NP::ValueOf)
215-
.arg((wildcard("distance_or"), wildcard("distance_key")))
233+
.arg(("distance_ori", "distance_key"))
216234
.arg(0),
217235
],
218236
);
@@ -224,59 +242,41 @@ mod tests {
224242
let eth_dos_distance = Predicate::BatchSelf(3);
225243

226244
// next chunk builds:
227-
// eth_dos_distance_ind_0(src_or, src_key, dst_or, dst_key, distance_or, distance_key) = and<
228-
// eth_dos_distance(src_or, src_key, intermed_or, intermed_key, shorter_distance_or, shorter_distance_key)
229-
230-
// // distance == shorter_distance + 1
231-
// ValueOf(one_or, one_key, 1)
232-
// SumOf(distance_or, distance_key, shorter_distance_or, shorter_distance_key, one_or, one_key)
233-
234-
// // intermed is a friend of dst
235-
// eth_friend(intermed_or, intermed_key, dst_or, dst_key)
236-
// >
237245
let eth_dos_distance_ind = builder.predicate_and(
238246
&[
239247
// arguments:
240-
"src_or",
248+
"src_ori",
241249
"src_key",
242-
"dst_or",
250+
"dst_ori",
243251
"dst_key",
244-
"distance_or",
252+
"distance_ori",
245253
"distance_key",
246254
],
247255
&[
248256
// private arguments:
249-
"one_or",
257+
"one_ori",
250258
"one_key",
251-
"shorter_distance_or",
259+
"shorter_distance_ori",
252260
"shorter_distance_key",
253-
"intermed_or",
261+
"intermed_ori",
254262
"intermed_key",
255263
],
256264
&[
257265
// statement templates:
258266
STB::new(eth_dos_distance)
259-
.arg((wildcard("src_or"), wildcard("src_key")))
260-
.arg((wildcard("intermed_or"), wildcard("intermed_key")))
261-
.arg((
262-
wildcard("shorter_distance_or"),
263-
wildcard("shorter_distance_key"),
264-
)),
267+
.arg(("src_ori", "src_key"))
268+
.arg(("intermed_ori", "intermed_key"))
269+
.arg(("shorter_distance_ori", "shorter_distance_key")),
265270
// distance == shorter_distance + 1
266-
STB::new(NP::ValueOf)
267-
.arg((wildcard("one_or"), wildcard("one_key")))
268-
.arg(1),
271+
STB::new(NP::ValueOf).arg(("one_ori", "one_key")).arg(1),
269272
STB::new(NP::SumOf)
270-
.arg((wildcard("distance_or"), wildcard("distance_key")))
271-
.arg((
272-
wildcard("shorter_distance_or"),
273-
wildcard("shorter_distance_key"),
274-
))
275-
.arg((wildcard("one_or"), wildcard("one_key"))),
273+
.arg(("distance_ori", "distance_key"))
274+
.arg(("shorter_distance_ori", "shorter_distance_key"))
275+
.arg(("one_ori", "one_key")),
276276
// intermed is a friend of dst
277277
STB::new(eth_friend)
278-
.arg((wildcard("intermed_or"), wildcard("intermed_key")))
279-
.arg((wildcard("dst_or"), wildcard("dst_key"))),
278+
.arg(("intermed_ori", "intermed_key"))
279+
.arg(("dst_ori", "dst_key")),
280280
],
281281
);
282282

@@ -287,23 +287,23 @@ mod tests {
287287

288288
let _eth_dos_distance = builder.predicate_or(
289289
&[
290-
"src_or",
290+
"src_ori",
291291
"src_key",
292-
"dst_or",
292+
"dst_ori",
293293
"dst_key",
294-
"distance_or",
294+
"distance_ori",
295295
"distance_key",
296296
],
297297
&[],
298298
&[
299299
STB::new(eth_dos_distance_base)
300-
.arg((wildcard("src_or"), wildcard("src_key")))
301-
.arg((wildcard("dst_or"), wildcard("dst_key")))
302-
.arg((wildcard("distance_or"), wildcard("distance_key"))),
300+
.arg(("src_ori", "src_key"))
301+
.arg(("dst_ori", "dst_key"))
302+
.arg(("distance_ori", "distance_key")),
303303
STB::new(eth_dos_distance_ind)
304-
.arg((wildcard("src_or"), wildcard("src_key")))
305-
.arg((wildcard("dst_or"), wildcard("dst_key")))
306-
.arg((wildcard("distance_or"), wildcard("distance_key"))),
304+
.arg(("src_ori", "src_key"))
305+
.arg(("dst_ori", "dst_key"))
306+
.arg(("distance_ori", "distance_key")),
307307
],
308308
);
309309

0 commit comments

Comments
 (0)