Skip to content

Commit 821ca22

Browse files
committed
resolve #273: remove global constant MAX_DEPTH, instead use the respective value from Params
1 parent 115c3c1 commit 821ca22

File tree

6 files changed

+59
-27
lines changed

6 files changed

+59
-27
lines changed

src/backends/plonky2/mock/signedpod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::{
88
error::{Error, Result},
99
primitives::merkletree::MerkleTree,
1010
},
11-
constants::MAX_DEPTH,
1211
middleware::{
1312
containers::Dictionary, hash_str, serialization::ordered_map, AnchoredKey, Key, Params,
1413
Pod, PodId, PodSigner, PodType, RawValue, Statement, Value, KEY_SIGNER, KEY_TYPE, SELF,
@@ -35,7 +34,12 @@ impl MockSigner {
3534
let dict = Dictionary::new(params.max_depth_mt_containers, kvs.clone())?;
3635
let id = PodId(dict.commitment());
3736
let signature = format!("{}_signed_by_{}", id, pubkey);
38-
Ok(MockSignedPod { id, signature, kvs })
37+
Ok(MockSignedPod {
38+
mt_max_depth: params.max_depth_mt_containers,
39+
id,
40+
signature,
41+
kvs,
42+
})
3943
}
4044
}
4145

@@ -47,13 +51,15 @@ impl PodSigner for MockSigner {
4751

4852
#[derive(Clone, Debug, PartialEq, Eq)]
4953
pub struct MockSignedPod {
54+
mt_max_depth: usize,
5055
id: PodId,
5156
signature: String,
5257
kvs: HashMap<Key, Value>,
5358
}
5459

5560
#[derive(Serialize, Deserialize)]
5661
struct Data {
62+
mt_max_depth: usize,
5763
signature: String,
5864
#[serde(serialize_with = "ordered_map")]
5965
kvs: HashMap<Key, Value>,
@@ -67,6 +73,7 @@ impl MockSignedPod {
6773
pub(crate) fn deserialize(id: PodId, data: serde_json::Value) -> Result<Box<dyn Pod>> {
6874
let data: Data = serde_json::from_value(data)?;
6975
Ok(Box::new(Self {
76+
mt_max_depth: data.mt_max_depth,
7077
id,
7178
signature: data.signature,
7279
kvs: data.kvs,
@@ -90,7 +97,7 @@ impl Pod for MockSignedPod {
9097
fn verify(&self) -> Result<()> {
9198
// 1. Verify id
9299
let mt = MerkleTree::new(
93-
MAX_DEPTH,
100+
self.mt_max_depth,
94101
&self
95102
.kvs
96103
.iter()
@@ -153,6 +160,7 @@ impl Pod for MockSignedPod {
153160

154161
fn serialize_data(&self) -> serde_json::Value {
155162
serde_json::to_value(Data {
163+
mt_max_depth: self.mt_max_depth,
156164
signature: self.signature.clone(),
157165
kvs: self.kvs.clone(),
158166
})

src/backends/plonky2/signedpod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use crate::{
1818
},
1919
serialize_bytes,
2020
},
21-
constants::MAX_DEPTH,
2221
middleware::{
2322
containers::Dictionary, AnchoredKey, Hash, Key, Params, Pod, PodId, PodSigner, PodType,
2423
RawValue, Statement, Value, KEY_SIGNER, KEY_TYPE, SELF,
@@ -142,7 +141,7 @@ impl Pod for SignedPod {
142141

143142
// 2. Verify id
144143
let mt = MerkleTree::new(
145-
MAX_DEPTH,
144+
self.dict.max_depth(),
146145
&self
147146
.dict
148147
.kvs()

src/constants.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/lang/processor.rs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub fn process_pest_tree(
109109
available_batches,
110110
)?;
111111

112-
second_pass(&mut processing_ctx)
112+
second_pass(&mut processing_ctx, params)
113113
}
114114

115115
/// Pass 1: Iterates through top-level definitions, records custom predicate
@@ -266,18 +266,21 @@ enum StatementContext<'a> {
266266
},
267267
}
268268

269-
fn second_pass(ctx: &mut ProcessingContext) -> Result<PodlangOutput, ProcessorError> {
269+
fn second_pass(
270+
ctx: &mut ProcessingContext,
271+
params: &Params,
272+
) -> Result<PodlangOutput, ProcessorError> {
270273
let mut cpb_builder =
271274
CustomPredicateBatchBuilder::new(ctx.params.clone(), "PodlangBatch".to_string());
272275

273276
for pred_pair in &ctx.custom_predicate_pairs {
274-
process_and_add_custom_predicate_to_batch(pred_pair, ctx, &mut cpb_builder)?;
277+
process_and_add_custom_predicate_to_batch(params, pred_pair, ctx, &mut cpb_builder)?;
275278
}
276279

277280
let custom_batch = cpb_builder.finish();
278281

279282
let request_templates = if let Some(req_pair) = &ctx.request_pair {
280-
process_request_def(req_pair, ctx, &custom_batch)?
283+
process_request_def(params, req_pair, ctx, &custom_batch)?
281284
} else {
282285
Vec::new()
283286
};
@@ -288,10 +291,13 @@ fn second_pass(ctx: &mut ProcessingContext) -> Result<PodlangOutput, ProcessorEr
288291
})
289292
}
290293

291-
fn pest_pair_to_builder_arg(arg_content_pair: &Pair<Rule>) -> Result<BuilderArg, ProcessorError> {
294+
fn pest_pair_to_builder_arg(
295+
params: &Params,
296+
arg_content_pair: &Pair<Rule>,
297+
) -> Result<BuilderArg, ProcessorError> {
292298
match arg_content_pair.as_rule() {
293299
Rule::literal_value => {
294-
let value = process_literal_value(arg_content_pair)?;
300+
let value = process_literal_value(params, arg_content_pair)?;
295301
Ok(BuilderArg::Literal(value))
296302
}
297303
Rule::wildcard => {
@@ -421,6 +427,7 @@ fn validate_and_build_statement_template(
421427
}
422428

423429
fn process_and_add_custom_predicate_to_batch(
430+
params: &Params,
424431
pred_def_pair: &Pair<Rule>,
425432
processing_ctx: &ProcessingContext,
426433
cpb_builder: &mut CustomPredicateBatchBuilder,
@@ -505,6 +512,7 @@ fn process_and_add_custom_predicate_to_batch(
505512
.filter(|p| p.as_rule() == Rule::statement)
506513
{
507514
let stb = process_statement_template(
515+
params,
508516
&stmt_pair,
509517
processing_ctx,
510518
StatementContext::CustomPredicate,
@@ -526,6 +534,7 @@ fn process_and_add_custom_predicate_to_batch(
526534
}
527535

528536
fn process_request_def(
537+
params: &Params,
529538
req_def_pair: &Pair<Rule>,
530539
processing_ctx: &ProcessingContext,
531540
custom_batch: &Arc<CustomPredicateBatch>,
@@ -545,6 +554,7 @@ fn process_request_def(
545554
.filter(|p| p.as_rule() == Rule::statement)
546555
{
547556
let built_stb = process_statement_template(
557+
params,
548558
&stmt_pair,
549559
processing_ctx,
550560
StatementContext::Request {
@@ -569,6 +579,7 @@ fn process_request_def(
569579
}
570580

571581
fn process_statement_template(
582+
params: &Params,
572583
stmt_pair: &Pair<Rule>,
573584
processing_ctx: &ProcessingContext,
574585
mut context: StatementContext,
@@ -579,7 +590,7 @@ fn process_statement_template(
579590
.unwrap();
580591
let stmt_name_str = name_pair.as_str();
581592

582-
let builder_args = parse_statement_args(stmt_pair)?;
593+
let builder_args = parse_statement_args(params, stmt_pair)?;
583594

584595
if let StatementContext::Request {
585596
wildcard_names,
@@ -640,7 +651,10 @@ fn process_statement_template(
640651
Ok(stb.desugar())
641652
}
642653

643-
fn process_literal_value(lit_val_pair: &Pair<Rule>) -> Result<Value, ProcessorError> {
654+
fn process_literal_value(
655+
params: &Params,
656+
lit_val_pair: &Pair<Rule>,
657+
) -> Result<Value, ProcessorError> {
644658
let inner_lit = lit_val_pair.clone().into_inner().next().unwrap();
645659

646660
match inner_lit.as_rule() {
@@ -684,10 +698,10 @@ fn process_literal_value(lit_val_pair: &Pair<Rule>) -> Result<Value, ProcessorEr
684698
Rule::literal_array => {
685699
let elements: Result<Vec<Value>, ProcessorError> = inner_lit
686700
.into_inner()
687-
.map(|elem_pair| process_literal_value(&elem_pair))
701+
.map(|elem_pair| process_literal_value(params, &elem_pair))
688702
.collect();
689703
let middleware_array =
690-
middleware::containers::Array::new(crate::constants::MAX_DEPTH, elements?)
704+
middleware::containers::Array::new(params.max_depth_mt_containers, elements?)
691705
.map_err(|e| {
692706
ProcessorError::Internal(format!("Failed to create Array: {}", e))
693707
})?;
@@ -696,12 +710,13 @@ fn process_literal_value(lit_val_pair: &Pair<Rule>) -> Result<Value, ProcessorEr
696710
Rule::literal_set => {
697711
let elements: Result<HashSet<Value>, ProcessorError> = inner_lit
698712
.into_inner()
699-
.map(|elem_pair| process_literal_value(&elem_pair))
713+
.map(|elem_pair| process_literal_value(params, &elem_pair))
700714
.collect();
701715
let middleware_set =
702-
middleware::containers::Set::new(crate::constants::MAX_DEPTH, elements?).map_err(
703-
|e| ProcessorError::Internal(format!("Failed to create Set: {}", e)),
704-
)?;
716+
middleware::containers::Set::new(params.max_depth_mt_containers, elements?)
717+
.map_err(|e| {
718+
ProcessorError::Internal(format!("Failed to create Set: {}", e))
719+
})?;
705720
Ok(Value::from(middleware_set))
706721
}
707722
Rule::literal_dict => {
@@ -712,12 +727,12 @@ fn process_literal_value(lit_val_pair: &Pair<Rule>) -> Result<Value, ProcessorEr
712727
let key_pair = entry_inner.next().unwrap();
713728
let val_pair = entry_inner.next().unwrap();
714729
let key_str = parse_pest_string_literal(&key_pair)?;
715-
let val = process_literal_value(&val_pair)?;
730+
let val = process_literal_value(params, &val_pair)?;
716731
Ok((Key::new(key_str), val))
717732
})
718733
.collect();
719734
let middleware_dict =
720-
middleware::containers::Dictionary::new(crate::constants::MAX_DEPTH, pairs?)
735+
middleware::containers::Dictionary::new(params.max_depth_mt_containers, pairs?)
721736
.map_err(|e| {
722737
ProcessorError::Internal(format!("Failed to create Dictionary: {}", e))
723738
})?;
@@ -862,7 +877,10 @@ fn resolve_request_statement_builder(
862877
})
863878
}
864879

865-
fn parse_statement_args(stmt_pair: &Pair<Rule>) -> Result<Vec<BuilderArg>, ProcessorError> {
880+
fn parse_statement_args(
881+
params: &Params,
882+
stmt_pair: &Pair<Rule>,
883+
) -> Result<Vec<BuilderArg>, ProcessorError> {
866884
let mut builder_args = Vec::new();
867885
let mut inner_stmt_pairs = stmt_pair.clone().into_inner();
868886

@@ -873,7 +891,7 @@ fn parse_statement_args(stmt_pair: &Pair<Rule>) -> Result<Vec<BuilderArg>, Proce
873891
.filter(|p| p.as_rule() == Rule::statement_arg)
874892
{
875893
let arg_content_pair = arg_pair.into_inner().next().unwrap();
876-
let builder_arg = pest_pair_to_builder_arg(&arg_content_pair)?;
894+
let builder_arg = pest_pair_to_builder_arg(params, &arg_content_pair)?;
877895
builder_args.push(builder_arg);
878896
}
879897
}
@@ -1103,7 +1121,7 @@ mod processor_tests {
11031121
let params = Params::default();
11041122
let mut ctx = ProcessingContext::new(&params);
11051123
first_pass(pairs, &mut ctx, &[])?;
1106-
let result = second_pass(&mut ctx);
1124+
let result = second_pass(&mut ctx, &params);
11071125
assert!(result.is_err());
11081126
match result.err().unwrap() {
11091127
ProcessorError::UndefinedIdentifier { name, span: _ } => {
@@ -1122,7 +1140,7 @@ mod processor_tests {
11221140
let params = Params::default();
11231141
let mut ctx = ProcessingContext::new(&params);
11241142
first_pass(pairs, &mut ctx, &[])?;
1125-
let result = second_pass(&mut ctx);
1143+
let result = second_pass(&mut ctx, &params);
11261144
assert!(result.is_err());
11271145
match result.err().unwrap() {
11281146
ProcessorError::UndefinedIdentifier { name, span: _ } => {

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#![feature(mapped_lock_guards)]
44

55
pub mod backends;
6-
pub mod constants;
76
pub mod frontend;
87
pub mod lang;
98
pub mod middleware;

src/middleware/containers.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ impl Dictionary {
8383
pub fn kvs(&self) -> &HashMap<Key, Value> {
8484
&self.kvs
8585
}
86+
pub fn max_depth(&self) -> usize {
87+
self.max_depth
88+
}
8689
}
8790

8891
impl PartialEq for Dictionary {
@@ -171,6 +174,9 @@ impl Set {
171174
pub fn set(&self) -> &HashSet<Value> {
172175
&self.set
173176
}
177+
pub fn max_depth(&self) -> usize {
178+
self.max_depth
179+
}
174180
}
175181

176182
impl PartialEq for Set {
@@ -256,6 +262,9 @@ impl Array {
256262
pub fn array(&self) -> &[Value] {
257263
&self.array
258264
}
265+
pub fn max_depth(&self) -> usize {
266+
self.max_depth
267+
}
259268
}
260269

261270
impl PartialEq for Array {

0 commit comments

Comments
 (0)