1
1
use ergotree_ir:: bigint256:: BigInt256 ;
2
+ use ergotree_ir:: ergo_tree:: ErgoTreeVersion ;
2
3
use ergotree_ir:: mir:: upcast:: Upcast ;
3
4
use ergotree_ir:: mir:: value:: Value ;
4
5
use ergotree_ir:: types:: stype:: SType ;
@@ -8,13 +9,17 @@ use crate::eval::Context;
8
9
use crate :: eval:: EvalError ;
9
10
use crate :: eval:: Evaluable ;
10
11
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 > {
12
13
match in_v {
13
14
Value :: Byte ( v) => Ok ( BigInt256 :: from ( v) . into ( ) ) ,
14
15
Value :: Short ( v) => Ok ( BigInt256 :: from ( v) . into ( ) ) ,
15
16
Value :: Int ( v) => Ok ( BigInt256 :: from ( v) . into ( ) ) ,
16
17
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
+ }
18
23
_ => Err ( EvalError :: UnexpectedValue ( format ! (
19
24
"Upcast: cannot upcast {0:?} to BigInt" ,
20
25
in_v
@@ -76,7 +81,7 @@ impl Evaluable for Upcast {
76
81
) -> Result < Value < ' ctx > , EvalError > {
77
82
let input_v = self . input . eval ( env, ctx) ?;
78
83
match self . tpe {
79
- SType :: SBigInt => upcast_to_bigint ( input_v) ,
84
+ SType :: SBigInt => upcast_to_bigint ( input_v, ctx ) ,
80
85
SType :: SLong => upcast_to_long ( input_v) ,
81
86
SType :: SInt => upcast_to_int ( input_v) ,
82
87
SType :: SShort => upcast_to_short ( input_v) ,
@@ -94,8 +99,9 @@ impl Evaluable for Upcast {
94
99
#[ cfg( test) ]
95
100
mod tests {
96
101
use ergotree_ir:: mir:: constant:: Constant ;
102
+ use sigma_test_util:: force_any_val;
97
103
98
- use crate :: eval:: tests:: eval_out_wo_ctx;
104
+ use crate :: eval:: tests:: { eval_out_wo_ctx, try_eval_out_with_version } ;
99
105
100
106
use super :: * ;
101
107
use proptest:: prelude:: * ;
@@ -177,13 +183,15 @@ mod tests {
177
183
}
178
184
179
185
#[ test]
180
- fn from_bigint( v in any:: <i64 >( ) ) {
181
- let v: BigInt256 = v. into( ) ;
186
+ fn from_bigint( v in any:: <BigInt256 >( ) ) {
182
187
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
+ } ) ;
187
195
}
188
196
}
189
197
}
0 commit comments