Skip to content

Commit fb3a19e

Browse files
committed
ergotree-interpreter: fix semantics of contains, update, remove
1 parent 238d3a8 commit fb3a19e

File tree

1 file changed

+20
-41
lines changed

1 file changed

+20
-41
lines changed

ergotree-interpreter/src/eval/savltree.rs

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ pub(crate) static REMOVE_EVAL_FN: EvalFn =
322322
avl_tree_data.into(),
323323
)))))
324324
} else {
325-
Err(EvalError::AvlTree("Cannot update digest".into()))
325+
Ok(Value::Opt(None))
326326
}
327327
};
328328

@@ -361,16 +361,13 @@ pub(crate) static CONTAINS_EVAL_FN: EvalFn = |_mc, _env, _ctx, obj, args| {
361361
)
362362
.map_err(map_eval_err)?;
363363

364-
match bv.perform_one_operation(&Operation::Lookup(key)) {
364+
Ok(match bv.perform_one_operation(&Operation::Lookup(key)) {
365365
Ok(s) => match s {
366-
Some(_e) => Ok(Value::Boolean(true)),
367-
_ => Ok(Value::Boolean(false)),
366+
Some(_e) => Value::Boolean(true),
367+
_ => Value::Boolean(false),
368368
},
369-
Err(_) => Err(EvalError::AvlTree(format!(
370-
"Incorrect contains call for {:?}",
371-
avl_tree_data
372-
))),
373-
}
369+
Err(_) => Value::Boolean(false),
370+
})
374371
};
375372

376373
pub(crate) static UPDATE_EVAL_FN: EvalFn =
@@ -419,21 +416,16 @@ pub(crate) static UPDATE_EVAL_FN: EvalFn =
419416
}))
420417
.is_err()
421418
{
422-
return Err(EvalError::AvlTree(format!(
423-
"Incorrect update for {:?}",
424-
avl_tree_data
425-
)));
419+
break;
426420
}
427421
}
428-
if let Some(new_digest) = bv.digest() {
422+
Ok(if let Some(new_digest) = bv.digest() {
429423
let digest = ADDigest::scorex_parse_bytes(&new_digest)?;
430424
avl_tree_data.digest = digest;
431-
Ok(Value::Opt(Some(Box::new(Value::AvlTree(
432-
avl_tree_data.into(),
433-
)))))
425+
Value::Opt(Some(Value::AvlTree(avl_tree_data.into()).into()))
434426
} else {
435-
Err(EvalError::AvlTree("Cannot update digest".into()))
436-
}
427+
Value::Opt(None)
428+
})
437429
};
438430

439431
fn map_eval_err<T: core::fmt::Debug>(e: T) -> EvalError {
@@ -458,18 +450,17 @@ mod tests {
458450
types::{savltree, stuple::STuple, stype::SType},
459451
};
460452
use proptest::prelude::*;
453+
use sigma_test_util::force_any_val;
461454

462-
use crate::eval::test_util::eval_out_wo_ctx;
455+
use crate::eval::test_util::{eval_out_wo_ctx, try_eval_out_with_version};
463456

464457
use super::*;
465458
use sigma_util::{AsVecI8, AsVecU8};
466459

467460
#[test]
468461
fn eval_avl_get() {
469462
let mut prover = populate_tree(vec![(vec![1u8], 10u64.to_be_bytes().to_vec())]);
470-
let initial_digest =
471-
ADDigest::scorex_parse_bytes(&prover.digest().unwrap().into_iter().collect::<Vec<_>>())
472-
.unwrap();
463+
let initial_digest = ADDigest::scorex_parse_bytes(&prover.digest().unwrap()).unwrap();
473464

474465
let key1 = Bytes::from(vec![1u8]);
475466
let key2 = Bytes::from(vec![2u8]);
@@ -539,9 +530,7 @@ mod tests {
539530
(vec![2u8], 20u64.to_be_bytes().to_vec()),
540531
]);
541532

542-
let initial_digest =
543-
ADDigest::scorex_parse_bytes(&prover.digest().unwrap().into_iter().collect::<Vec<_>>())
544-
.unwrap();
533+
let initial_digest = ADDigest::scorex_parse_bytes(&prover.digest().unwrap()).unwrap();
545534

546535
let key1 = Bytes::from(vec![1u8]);
547536
let key2 = Bytes::from(vec![2u8]);
@@ -909,9 +898,7 @@ mod tests {
909898
(vec![2u8], 20u64.to_be_bytes().to_vec()),
910899
(vec![3u8], 30u64.to_be_bytes().to_vec()),
911900
]);
912-
let digest =
913-
ADDigest::scorex_parse_bytes(&prover.digest().unwrap().into_iter().collect::<Vec<_>>())
914-
.unwrap();
901+
let digest = ADDigest::scorex_parse_bytes(&prover.digest().unwrap()).unwrap();
915902

916903
let op = Operation::Lookup(Bytes::from(vec![2u8]));
917904
prover.perform_one_operation(&op).unwrap();
@@ -946,16 +933,12 @@ mod tests {
946933
#[test]
947934
fn eval_avl_remove() {
948935
let mut prover = populate_tree(vec![(vec![1u8], 10u64.to_be_bytes().to_vec())]);
949-
let initial_digest =
950-
ADDigest::scorex_parse_bytes(&prover.digest().unwrap().into_iter().collect::<Vec<_>>())
951-
.unwrap();
936+
let initial_digest = ADDigest::scorex_parse_bytes(&prover.digest().unwrap()).unwrap();
952937

953938
let key1 = Bytes::from(vec![1u8]);
954939
let op1 = Operation::Remove(key1);
955940
prover.perform_one_operation(&op1).unwrap();
956-
let final_digest =
957-
ADDigest::scorex_parse_bytes(&prover.digest().unwrap().into_iter().collect::<Vec<_>>())
958-
.unwrap();
941+
let final_digest = ADDigest::scorex_parse_bytes(&prover.digest().unwrap()).unwrap();
959942
let proof: Constant = prover
960943
.generate_proof()
961944
.into_iter()
@@ -1008,9 +991,7 @@ mod tests {
1008991
(vec![2u8], 20u64.to_be_bytes().to_vec()),
1009992
(vec![3u8], 30u64.to_be_bytes().to_vec()),
1010993
]);
1011-
let initial_digest =
1012-
ADDigest::scorex_parse_bytes(&prover.digest().unwrap().into_iter().collect::<Vec<_>>())
1013-
.unwrap();
994+
let initial_digest = ADDigest::scorex_parse_bytes(&prover.digest().unwrap()).unwrap();
1014995

1015996
let op1 = Operation::Update(KeyValue {
1016997
key: Bytes::from(vec![2u8]),
@@ -1023,9 +1004,7 @@ mod tests {
10231004
prover.perform_one_operation(&op1).unwrap();
10241005
prover.perform_one_operation(&op2).unwrap();
10251006

1026-
let final_digest =
1027-
ADDigest::scorex_parse_bytes(&prover.digest().unwrap().into_iter().collect::<Vec<_>>())
1028-
.unwrap();
1007+
let final_digest = ADDigest::scorex_parse_bytes(&prover.digest().unwrap()).unwrap();
10291008
let proof: Constant = prover
10301009
.generate_proof()
10311010
.into_iter()

0 commit comments

Comments
 (0)