Skip to content

Commit 78b2599

Browse files
committed
Fix versioning for upcasting from bigint to bigint
Previously this worked unconditionally, even on v4.0 and v5.0
1 parent 2063a5d commit 78b2599

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

ergotree-interpreter/src/eval/upcast.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use ergotree_ir::bigint256::BigInt256;
2+
use ergotree_ir::ergo_tree::ErgoTreeVersion;
23
use ergotree_ir::mir::upcast::Upcast;
34
use ergotree_ir::mir::value::Value;
45
use ergotree_ir::types::stype::SType;
@@ -8,13 +9,17 @@ use crate::eval::Context;
89
use crate::eval::EvalError;
910
use crate::eval::Evaluable;
1011

11-
fn upcast_to_bigint(in_v: Value) -> Result<Value, EvalError> {
12+
fn upcast_to_bigint<'a>(in_v: Value<'a>, ctx: &Context) -> Result<Value<'a>, EvalError> {
1213
match in_v {
1314
Value::Byte(v) => Ok(BigInt256::from(v).into()),
1415
Value::Short(v) => Ok(BigInt256::from(v).into()),
1516
Value::Int(v) => Ok(BigInt256::from(v).into()),
1617
Value::Long(v) => Ok(BigInt256::from(v).into()),
17-
Value::BigInt(_) => Ok(in_v),
18+
Value::BigInt(_)
19+
if ctx.activated_script_version() >= ErgoTreeVersion::V6_SOFT_FORK_VERSION =>
20+
{
21+
Ok(in_v)
22+
}
1823
_ => Err(EvalError::UnexpectedValue(format!(
1924
"Upcast: cannot upcast {0:?} to BigInt",
2025
in_v
@@ -76,7 +81,7 @@ impl Evaluable for Upcast {
7681
) -> Result<Value<'ctx>, EvalError> {
7782
let input_v = self.input.eval(env, ctx)?;
7883
match self.tpe {
79-
SType::SBigInt => upcast_to_bigint(input_v),
84+
SType::SBigInt => upcast_to_bigint(input_v, ctx),
8085
SType::SLong => upcast_to_long(input_v),
8186
SType::SInt => upcast_to_int(input_v),
8287
SType::SShort => upcast_to_short(input_v),
@@ -94,8 +99,9 @@ impl Evaluable for Upcast {
9499
#[cfg(test)]
95100
mod tests {
96101
use ergotree_ir::mir::constant::Constant;
102+
use sigma_test_util::force_any_val;
97103

98-
use crate::eval::tests::eval_out_wo_ctx;
104+
use crate::eval::tests::{eval_out_wo_ctx, try_eval_out_with_version};
99105

100106
use super::*;
101107
use proptest::prelude::*;
@@ -177,13 +183,15 @@ mod tests {
177183
}
178184

179185
#[test]
180-
fn from_bigint(v in any::<i64>()) {
181-
let v: BigInt256 = v.into();
186+
fn from_bigint(v in any::<BigInt256>()) {
182187
let c: Constant = v.clone().into();
183-
assert_eq!(
184-
eval_out_wo_ctx::<BigInt256>(&Upcast::new(c.into(), SType::SBigInt).unwrap().into()),
185-
v
186-
);
188+
let ctx = force_any_val::<Context>();
189+
(0..ErgoTreeVersion::V6_SOFT_FORK_VERSION).for_each(|version| {
190+
assert!(try_eval_out_with_version::<BigInt256>(&Upcast::new(c.clone().into(), SType::SBigInt).unwrap().into(), &ctx, version).is_err());
191+
});
192+
(ErgoTreeVersion::V6_SOFT_FORK_VERSION..=ErgoTreeVersion::MAX_SCRIPT_VERSION).for_each(|version| {
193+
assert_eq!(try_eval_out_with_version::<BigInt256>(&Upcast::new(c.clone().into(), SType::SBigInt).unwrap().into(), &ctx, version).unwrap(), v.clone());
194+
});
187195
}
188196
}
189197
}

0 commit comments

Comments
 (0)