-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathscontext.rs
More file actions
298 lines (278 loc) · 10.6 KB
/
Copy pathscontext.rs
File metadata and controls
298 lines (278 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use alloc::boxed::Box;
use alloc::string::ToString;
use alloc::sync::Arc;
use ergotree_ir::mir::avl_tree_data::AvlTreeData;
use ergotree_ir::mir::avl_tree_data::AvlTreeFlags;
use ergotree_ir::mir::constant::TryExtractInto;
use ergotree_ir::mir::value::CollKind;
use ergotree_ir::mir::value::Value;
use ergotree_ir::reference::Ref;
use ergotree_ir::serialization::SigmaSerializable;
use ergotree_ir::types::stype::SType;
use super::EvalError;
use super::EvalFn;
pub(crate) static DATA_INPUTS_EVAL_FN: EvalFn = |_mc, _env, ctx, obj, _args| {
if obj != Value::Context {
return Err(EvalError::UnexpectedValue(format!(
"Context.dataInputs: expected object of Value::Context, got {:?}",
obj
)));
}
Ok(Value::Coll(CollKind::WrappedColl {
items: ctx.data_inputs.clone().map_or(Arc::new([]), |d| {
d.iter().map(|&di| Ref::from(di)).map(Value::CBox).collect()
}),
elem_tpe: SType::SBox,
}))
};
pub(crate) static SELF_BOX_INDEX_EVAL_FN: EvalFn = |_mc, _env, ctx, obj, _args| {
if obj != Value::Context {
return Err(EvalError::UnexpectedValue(format!(
"Context.selfBoxIndex: expected object of Value::Context, got {:?}",
obj
)));
}
// JVM bug compatibility: selfBoxIndex always returned -1 before JIT
// activation (v5.0). The bug was `eq` (reference equality) instead of
// `==` (value equality) in CostingDataContext.scala — a global impl
// bug, not per-script semantics. Fixed in v5.x for ALL scripts.
// Gate: activated_script_version (block level), NOT tree_version.
// See: https://github.com/ScorexFoundation/sigmastate-interpreter/issues/603
if ctx.activated_script_version() < ergotree_ir::ergo_tree::ErgoTreeVersion::V2 {
return Ok(Value::Int(-1));
}
let box_index = ctx
.inputs
.iter()
.position(|it| *it == ctx.self_box)
.ok_or_else(|| EvalError::NotFound("Context.selfBoxIndex: box not found".to_string()))?;
Ok(Value::Int(box_index as i32))
};
pub(crate) static HEADERS_EVAL_FN: EvalFn = |_mc, _env, ctx, obj, _args| {
if obj != Value::Context {
return Err(EvalError::UnexpectedValue(format!(
"Context.headers: expected object of Value::Context, got {:?}",
obj
)));
}
Ok(Value::Coll(CollKind::WrappedColl {
items: Arc::new(ctx.headers.clone().map(Box::new).map(Value::Header)),
elem_tpe: SType::SHeader,
}))
};
pub(crate) static PRE_HEADER_EVAL_FN: EvalFn = |_mc, _env, ctx, obj, _args| {
if obj != Value::Context {
return Err(EvalError::UnexpectedValue(format!(
"Context.preHeader: expected object of Value::Context, got {:?}",
obj
)));
}
Ok(Box::from(ctx.pre_header.clone()).into())
};
pub(crate) static LAST_BLOCK_UTXO_ROOT_HASH_EVAL_FN: EvalFn = |_mc, _env, ctx, obj, _args| {
if obj != Value::Context {
return Err(EvalError::UnexpectedValue(format!(
"Context.LastBlockUtxoRootHash: expected object of Value::Context, got {:?}",
obj
)));
}
let digest = ctx.headers[0].state_root;
let tree_flags = AvlTreeFlags::new(true, true, true);
Ok(Value::AvlTree(Box::from(AvlTreeData {
digest,
tree_flags,
key_length: 32,
value_length_opt: None,
})))
};
pub(crate) static MINER_PUBKEY_EVAL_FN: EvalFn = |_mc, _env, ctx, obj, _args| {
if obj != Value::Context {
return Err(EvalError::UnexpectedValue(format!(
"Context.preHeader: expected object of Value::Context, got {:?}",
obj
)));
}
Ok(ctx
.pre_header
.miner_pk
.clone()
.sigma_serialize_bytes()?
.into())
};
pub(crate) static GET_VAR_FROM_INPUT_EVAL_FN: EvalFn = |mc, _env, ctx, _obj, args| {
#[allow(clippy::unreachable)] // getVarFromInput output type is always SOption[T]
let SType::SOption(output_tpe) = &*mc.tpe().t_range
else {
unreachable!()
};
let input_idx = args[0].clone().try_extract_into::<i16>()? as usize;
let var_id = args[1].clone().try_extract_into::<i8>()? as u8;
Ok(
match ctx
.extension_provider
.context_extension(input_idx)
.and_then(|extension| extension.values.get(&(var_id)))
.cloned()
{
Some(c) if c.tpe == **output_tpe => Value::Opt(Some(Box::new(c.v.into()))),
_ => Value::Opt(None),
},
)
};
#[cfg(test)]
#[cfg(feature = "arbitrary")]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use crate::eval::test_util::eval_out;
use ergo_chain_types::{Header, PreHeader};
use ergotree_ir::chain::context::Context;
use ergotree_ir::chain::ergo_box::ErgoBox;
use ergotree_ir::mir::avl_tree_data::{AvlTreeData, AvlTreeFlags};
use ergotree_ir::mir::constant::TryExtractFrom;
use ergotree_ir::mir::expr::Expr;
use ergotree_ir::mir::method_call::MethodCall;
use ergotree_ir::mir::property_call::PropertyCall;
use ergotree_ir::mir::value::Value;
use ergotree_ir::ergo_tree::ErgoTreeVersion;
use ergotree_ir::serialization::SigmaSerializable;
use ergotree_ir::types::scontext::{self, GET_VAR_FROM_INPUT_METHOD};
use ergotree_ir::types::stype::LiftIntoSType;
use ergotree_ir::types::stype_param::STypeVar;
use sigma_test_util::force_any_val;
use core::cell::Cell;
fn make_ctx_inputs_includes_self_box(
tree_version: ErgoTreeVersion,
pre_header_version: u8,
) -> Context<'static> {
let ctx = force_any_val::<Context>();
let self_box = &*Box::leak(Box::new(force_any_val::<ErgoBox>()));
let inputs = vec![&*Box::leak(Box::new(force_any_val::<ErgoBox>())), self_box]
.try_into()
.unwrap();
let pre_header = PreHeader {
version: pre_header_version,
..ctx.pre_header.clone()
};
Context {
height: 0u32,
self_box,
inputs,
pre_header,
tree_version: Cell::new(tree_version),
..ctx
}
}
#[test]
fn eval_self_box_index_v2_tree() {
let expr: Expr =
PropertyCall::new(Expr::Context, scontext::SELF_BOX_INDEX_PROPERTY.clone())
.unwrap()
.into();
// V2 tree in v5+ block (pre_header.version=3 → activated=V2): real index.
let context = make_ctx_inputs_includes_self_box(ErgoTreeVersion::V2, 3);
assert_eq!(eval_out::<i32>(&expr, &context), 1);
}
#[test]
fn eval_self_box_index_v0_tree_pre_v5() {
let expr: Expr =
PropertyCall::new(Expr::Context, scontext::SELF_BOX_INDEX_PROPERTY.clone())
.unwrap()
.into();
// V0 tree in pre-v5 block (pre_header.version=1 → activated=V0): -1.
let context = make_ctx_inputs_includes_self_box(ErgoTreeVersion::V0, 1);
assert_eq!(eval_out::<i32>(&expr, &context), -1);
}
#[test]
fn eval_self_box_index_v1_tree_pre_v5() {
let expr: Expr =
PropertyCall::new(Expr::Context, scontext::SELF_BOX_INDEX_PROPERTY.clone())
.unwrap()
.into();
// V1 tree in pre-v5 block (pre_header.version=1 → activated=V0): -1.
let context = make_ctx_inputs_includes_self_box(ErgoTreeVersion::V1, 1);
assert_eq!(eval_out::<i32>(&expr, &context), -1);
}
#[test]
fn eval_self_box_index_v0_tree_v5_context() {
let expr: Expr =
PropertyCall::new(Expr::Context, scontext::SELF_BOX_INDEX_PROPERTY.clone())
.unwrap()
.into();
// V0 tree in v5+ block (pre_header.version=3 → activated=V2): real index.
// JVM bug #603 was a global impl bug fixed in v5.x for ALL scripts.
let context = make_ctx_inputs_includes_self_box(ErgoTreeVersion::V0, 3);
assert_eq!(eval_out::<i32>(&expr, &context), 1);
}
#[test]
fn eval_headers() {
let expr: Expr = PropertyCall::new(Expr::Context, scontext::HEADERS_PROPERTY.clone())
.expect("internal error: `headers` method has parameters length != 1")
.into();
let ctx = force_any_val::<Context>();
assert_eq!(eval_out::<[Header; 10]>(&expr, &ctx), ctx.headers);
}
#[test]
fn eval_preheader() {
let expr: Expr = PropertyCall::new(Expr::Context, scontext::PRE_HEADER_PROPERTY.clone())
.unwrap()
.into();
let ctx = force_any_val::<Context>();
assert_eq!(eval_out::<PreHeader>(&expr, &ctx), ctx.pre_header);
}
#[test]
fn eval_miner_pubkey() {
let expr: Expr = PropertyCall::new(Expr::Context, scontext::MINER_PUBKEY_PROPERTY.clone())
.unwrap()
.into();
let ctx = force_any_val::<Context>();
assert_eq!(
eval_out::<Vec<u8>>(&expr, &ctx),
ctx.pre_header.miner_pk.sigma_serialize_bytes().unwrap()
);
}
#[test]
fn eval_last_block_utxo_root_hash() {
let expr: Expr = PropertyCall::new(
Expr::Context,
scontext::LAST_BLOCK_UTXO_ROOT_HASH_PROPERTY.clone(),
)
.unwrap()
.into();
let ctx = force_any_val::<Context>();
let digest = ctx.headers[0].state_root;
let tree_flags = AvlTreeFlags::new(true, true, true);
let avl_tree_data = AvlTreeData {
digest,
tree_flags,
key_length: 32,
value_length_opt: None,
};
assert_eq!(eval_out::<AvlTreeData>(&expr, &ctx), avl_tree_data);
}
#[test]
fn eval_get_var_from_input() {
fn get_var_from_input<T: LiftIntoSType + TryExtractFrom<Value<'static>> + 'static>(
ctx: &Context<'static>,
input_index: i16,
var_id: i8,
) -> Option<T> {
let mc = MethodCall::with_type_args(
Expr::Context,
GET_VAR_FROM_INPUT_METHOD.clone(),
vec![input_index.into(), var_id.into()],
[(STypeVar::t(), T::stype())].into_iter().collect(),
)
.unwrap()
.into();
eval_out(&mc, ctx)
}
let context = crate::eval::get_var::tests::prepare_context();
assert_eq!(get_var_from_input::<i32>(&context, 0, 3), Some(123));
assert_eq!(get_var_from_input::<i64>(&context, 0, 3), None); // wrong type
assert_eq!(get_var_from_input::<i32>(&context, 0, 4), None); // context extension var doesn't exist
assert_eq!(
get_var_from_input::<i32>(&context, context.inputs.len() as i16 + 1, 4),
None
); // input out of bounds
}
}