Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global.serialize, tree-based versioning for methods #797

Merged
merged 4 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bindings/ergo-lib-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ console_error_panic_hook = { version = "0.1.6", optional = true }
derive_more = { workspace = true }
num-traits = { workspace = true }
serde_with = { workspace = true }
bounded-vec = { workspace = true, features=["serde"] }
bounded-vec = { workspace = true, features = ["serde"] }
wasm-bindgen = { version = "0.2.87", features = [] }
wasm-bindgen-futures = { version = "0.4.37" }

Expand Down
1 change: 1 addition & 0 deletions ergo-lib/src/wallet/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub fn make_context<'ctx, T: ErgoTransaction>(
pre_header: state_ctx.pre_header.clone(),
extension,
headers: state_ctx.headers.clone(),
tree_version: Default::default(),
})
}
// Updates a Context, changing its self box and context extension to transaction.inputs[i]
Expand Down
13 changes: 11 additions & 2 deletions ergotree-interpreter/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ fn smethod_eval_fn(method: &SMethod) -> Result<EvalFn, EvalError> {
sglobal::FROM_BIGENDIAN_BYTES_METHOD_ID => {
self::sglobal::SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN
}
sglobal::SERIALIZE_METHOD_ID => self::sglobal::SERIALIZE_EVAL_FN,
method_id => {
return Err(EvalError::NotFound(format!(
"Eval fn: method {:?} with method id {:?} not found in SGlobal",
Expand Down Expand Up @@ -378,6 +379,9 @@ pub(crate) mod tests {
use ergotree_ir::mir::constant::TryExtractInto;
use ergotree_ir::mir::val_def::ValDef;
use ergotree_ir::mir::val_use::ValUse;
use ergotree_ir::serialization::sigma_byte_reader::from_bytes;
use ergotree_ir::serialization::sigma_byte_reader::SigmaByteRead;
use ergotree_ir::serialization::SigmaSerializable;
use ergotree_ir::types::stype::SType;
use expect_test::expect;
use sigma_test_util::force_any_val;
Expand Down Expand Up @@ -424,10 +428,15 @@ pub(crate) mod tests {
pub fn try_eval_out_with_version<'ctx, T: TryExtractFrom<Value<'static>> + 'static>(
expr: &Expr,
ctx: &'ctx Context<'ctx>,
version: u8,
tree_version: u8,
activated_version: u8,
) -> Result<T, EvalError> {
let mut ctx = ctx.clone();
ctx.pre_header.version = version + 1;
ctx.pre_header.version = activated_version + 1;
ctx.tree_version.set(tree_version.into());
// roundtrip expr to test methodcall versioning
from_bytes(&expr.sigma_serialize_bytes()?)
.with_tree_version(ctx.tree_version(), Expr::sigma_parse)?;
let mut env = Env::empty();
expr.eval(&mut env, &ctx).and_then(|v| {
v.to_static()
Expand Down
57 changes: 25 additions & 32 deletions ergotree-interpreter/src/eval/downcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ fn downcast_to_bigint<'a>(in_v: Value<'a>, ctx: &Context<'_>) -> Result<Value<'a
Value::Short(v) => Ok(BigInt256::from(v).into()),
Value::Int(v) => Ok(BigInt256::from(v).into()),
Value::Long(v) => Ok(BigInt256::from(v).into()),
Value::BigInt(_)
if ctx.activated_script_version() >= ErgoTreeVersion::V6_SOFT_FORK_VERSION =>
{
Ok(in_v)
}
Value::BigInt(_) if ctx.tree_version() >= ErgoTreeVersion::V3 => Ok(in_v),
_ => Err(EvalError::UnexpectedValue(format!(
"Downcast: cannot downcast {0:?} to BigInt",
in_v
Expand All @@ -36,9 +32,7 @@ fn downcast_to_long<'a>(in_v: Value<'a>, ctx: &Context<'_>) -> Result<Value<'a>,
Value::Short(v) => Ok((v as i64).into()),
Value::Int(v) => Ok((v as i64).into()),
Value::Long(_) => Ok(in_v),
Value::BigInt(v)
if ctx.activated_script_version() >= ErgoTreeVersion::V6_SOFT_FORK_VERSION =>
{
Value::BigInt(v) if ctx.tree_version() >= ErgoTreeVersion::V3 => {
v.to_i64().map(Value::from).ok_or_else(|| {
EvalError::UnexpectedValue(
"Downcast: overflow converting BigInt to Long".to_string(),
Expand All @@ -63,9 +57,7 @@ fn downcast_to_int<'a>(in_v: Value<'a>, ctx: &Context<'_>) -> Result<Value<'a>,
"Downcast: Int overflow".to_string(),
)),
},
Value::BigInt(v)
if ctx.activated_script_version() >= ErgoTreeVersion::V6_SOFT_FORK_VERSION =>
{
Value::BigInt(v) if ctx.tree_version() >= ErgoTreeVersion::V3 => {
v.to_i32().map(Value::from).ok_or_else(|| {
EvalError::UnexpectedValue(
"Downcast: overflow converting BigInt to Int".to_string(),
Expand Down Expand Up @@ -94,9 +86,7 @@ fn downcast_to_short<'a>(in_v: Value<'a>, ctx: &Context<'_>) -> Result<Value<'a>
"Downcast: Short overflow".to_string(),
)),
},
Value::BigInt(v)
if ctx.activated_script_version() >= ErgoTreeVersion::V6_SOFT_FORK_VERSION =>
{
Value::BigInt(v) if ctx.tree_version() >= ErgoTreeVersion::V3 => {
v.to_i16().map(Value::from).ok_or_else(|| {
EvalError::UnexpectedValue(
"Downcast: overflow converting BigInt to Short".to_string(),
Expand Down Expand Up @@ -131,9 +121,7 @@ fn downcast_to_byte<'a>(in_v: Value<'a>, ctx: &Context<'_>) -> Result<Value<'a>,
"Downcast: Byte overflow".to_string(),
)),
},
Value::BigInt(v)
if ctx.activated_script_version() >= ErgoTreeVersion::V6_SOFT_FORK_VERSION =>
{
Value::BigInt(v) if ctx.tree_version() >= ErgoTreeVersion::V3 => {
v.to_i8().map(Value::from).ok_or_else(|| {
EvalError::UnexpectedValue(
"Downcast: overflow converting BigInt to Byte".to_string(),
Expand Down Expand Up @@ -214,14 +202,15 @@ mod tests {
v_long.into()
);
let ctx = force_any_val::<Context>();
(0..ErgoTreeVersion::V6_SOFT_FORK_VERSION)
.for_each(|version| assert!(try_eval_out_with_version::<BigInt256>(&downcast(v_bigint, SType::SBigInt), &ctx, version).is_err()));
(ErgoTreeVersion::V6_SOFT_FORK_VERSION..=ErgoTreeVersion::MAX_SCRIPT_VERSION).for_each(
(0..ErgoTreeVersion::V3.into())
.for_each(|version| assert!(try_eval_out_with_version::<BigInt256>(&downcast(v_bigint, SType::SBigInt), &ctx, version, 1).is_err()));
(ErgoTreeVersion::V3.into()..=ErgoTreeVersion::MAX_SCRIPT_VERSION.into()).for_each(
|version| {
assert_eq!(
try_eval_out_with_version::<BigInt256>(
&downcast(v_bigint, SType::SBigInt),
&ctx,
version,
version
).unwrap(),
v_bigint.clone()
Expand Down Expand Up @@ -254,13 +243,14 @@ mod tests {
v_long
);
let ctx = force_any_val::<Context>();
(0..ErgoTreeVersion::V6_SOFT_FORK_VERSION)
.for_each(|version| assert!(try_eval_out_with_version::<i64>(&downcast(c_bigint.clone(), SType::SLong), &ctx, version).is_err()));
(ErgoTreeVersion::V6_SOFT_FORK_VERSION..=ErgoTreeVersion::MAX_SCRIPT_VERSION).for_each(
(0..ErgoTreeVersion::V3.into())
.for_each(|version| assert!(try_eval_out_with_version::<i64>(&downcast(c_bigint.clone(), SType::SLong), &ctx, version, 1).is_err()));
(ErgoTreeVersion::V3.into()..=ErgoTreeVersion::MAX_SCRIPT_VERSION.into()).for_each(
|version| {
let res = try_eval_out_with_version::<i64>(
&downcast(c_bigint.clone(), SType::SLong),
&ctx,
version,
version
);
if v_bigint < BigInt256::from(i64::MIN) || v_bigint > BigInt256::from(i64::MAX) {
Expand Down Expand Up @@ -307,13 +297,14 @@ mod tests {
)
.is_err());
let ctx = force_any_val::<Context>();
(0..ErgoTreeVersion::V6_SOFT_FORK_VERSION)
.for_each(|version| assert!(try_eval_out_with_version::<i32>(&downcast(v_bigint, SType::SInt), &ctx, version).is_err()));
(ErgoTreeVersion::V6_SOFT_FORK_VERSION..=ErgoTreeVersion::MAX_SCRIPT_VERSION).for_each(
(0..ErgoTreeVersion::V3.into())
.for_each(|version| assert!(try_eval_out_with_version::<i32>(&downcast(v_bigint, SType::SInt), &ctx, version, 1).is_err()));
(ErgoTreeVersion::V3.into()..=ErgoTreeVersion::MAX_SCRIPT_VERSION.into()).for_each(
|version| {
let res = try_eval_out_with_version::<i32>(
&downcast(v_bigint, SType::SInt),
&ctx,
version,
version
);
if v_bigint < BigInt256::from(i32::MIN) || v_bigint > BigInt256::from(i32::MAX) {
Expand Down Expand Up @@ -361,13 +352,14 @@ mod tests {
);
assert!(try_eval_out_wo_ctx::<i16>(&downcast(c_long_oob, SType::SShort)).is_err());
let ctx = force_any_val::<Context>();
(0..ErgoTreeVersion::V6_SOFT_FORK_VERSION)
.for_each(|version| assert!(try_eval_out_with_version::<i16>(&downcast(v_bigint, SType::SShort), &ctx, version).is_err()));
(ErgoTreeVersion::V6_SOFT_FORK_VERSION..=ErgoTreeVersion::MAX_SCRIPT_VERSION).for_each(
(0..ErgoTreeVersion::V3.into())
.for_each(|version| assert!(try_eval_out_with_version::<i16>(&downcast(v_bigint, SType::SShort), &ctx, version, 1).is_err()));
(ErgoTreeVersion::V3.into()..=ErgoTreeVersion::MAX_SCRIPT_VERSION.into()).for_each(
|version| {
let res = try_eval_out_with_version::<i16>(
&downcast(v_bigint, SType::SShort),
&ctx,
version,
version
);
if v_bigint < BigInt256::from(i16::MIN) || v_bigint > BigInt256::from(i16::MAX) {
Expand Down Expand Up @@ -426,13 +418,14 @@ mod tests {
);
assert!(try_eval_out_wo_ctx::<i8>(&downcast(c_long_oob, SType::SByte)).is_err());
let ctx = force_any_val::<Context>();
(0..ErgoTreeVersion::V6_SOFT_FORK_VERSION)
.for_each(|version| assert!(try_eval_out_with_version::<i8>(&downcast(v_bigint, SType::SByte), &ctx, version).is_err()));
(ErgoTreeVersion::V6_SOFT_FORK_VERSION..=ErgoTreeVersion::MAX_SCRIPT_VERSION).for_each(
(0..ErgoTreeVersion::V3.into())
.for_each(|version| assert!(try_eval_out_with_version::<i8>(&downcast(v_bigint, SType::SByte), &ctx, version, 1).is_err()));
(ErgoTreeVersion::V3.into()..=ErgoTreeVersion::MAX_SCRIPT_VERSION.into()).for_each(
|version| {
let res = try_eval_out_with_version::<i8>(
&downcast(v_bigint, SType::SByte),
&ctx,
version,
version
);
if v_bigint < BigInt256::from(i16::MIN) || v_bigint > BigInt256::from(i16::MAX) {
Expand Down
7 changes: 4 additions & 3 deletions ergotree-interpreter/src/eval/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use alloc::boxed::Box;
use alloc::string::String;
use core::fmt::Debug;
use core::fmt::Display;
use ergotree_ir::ergo_tree::ErgoTreeVersion;
use ergotree_ir::mir::expr::SubstDeserializeError;

use bounded_vec::BoundedVecOutOfBounds;
Expand Down Expand Up @@ -76,12 +77,12 @@ pub enum EvalError {
#[error("eval error: {0:?}")]
Spanned(SpannedEvalError),
/// Script version error
#[error("Method requires at least version {required_version}, but activated version is {activated_version}")]
#[error("Method requires at least version {required_version:?}, but activated version is {activated_version:?}")]
ScriptVersionError {
/// Opcode/method call requires this version
required_version: u8,
required_version: ErgoTreeVersion,
/// Currently activated script version on network
activated_version: u8,
activated_version: ErgoTreeVersion,
},
/// Deserialize substitution error, see [`ergotree_ir::mir::expr::Expr::substitute_deserialize`]
#[error("DeserializeRegister/DeserializeContext error: {0}")]
Expand Down
27 changes: 15 additions & 12 deletions ergotree-interpreter/src/eval/sbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ pub(crate) static VALUE_EVAL_FN: EvalFn = |_mc, _env, _ctx, obj, _args| {
};

pub(crate) static GET_REG_EVAL_FN: EvalFn = |mc, _env, ctx, obj, args| {
if ctx.activated_script_version() < ErgoTreeVersion::V6_SOFT_FORK_VERSION {
if ctx.tree_version() < ErgoTreeVersion::V3 {
return Err(EvalError::ScriptVersionError {
required_version: ErgoTreeVersion::V6_SOFT_FORK_VERSION,
activated_version: ctx.activated_script_version(),
required_version: ErgoTreeVersion::V3,
activated_version: ctx.tree_version(),
});
}
#[allow(clippy::unwrap_used)]
Expand Down Expand Up @@ -128,13 +128,13 @@ mod tests {
.unwrap()
.into();
let ctx = force_any_val::<Context>();
(0..ErgoTreeVersion::V6_SOFT_FORK_VERSION).for_each(|version| {
assert!(try_eval_out_with_version::<i64>(&expr, &ctx, version).is_err())
(0..ErgoTreeVersion::V3.into()).for_each(|version| {
assert!(try_eval_out_with_version::<i64>(&expr, &ctx, version, version).is_err())
});
(ErgoTreeVersion::V6_SOFT_FORK_VERSION..=ErgoTreeVersion::MAX_SCRIPT_VERSION).for_each(
(ErgoTreeVersion::V3.into()..=ErgoTreeVersion::MAX_SCRIPT_VERSION.into()).for_each(
|version| {
assert_eq!(
try_eval_out_with_version::<Option<i64>>(&expr, &ctx, version)
try_eval_out_with_version::<Option<i64>>(&expr, &ctx, version, version)
.unwrap()
.unwrap(),
ctx.self_box.value.as_i64()
Expand All @@ -156,23 +156,26 @@ mod tests {
.unwrap()
.into();
let ctx = force_any_val::<Context>();
(0..ErgoTreeVersion::V6_SOFT_FORK_VERSION).for_each(|version| {
let res = try_eval_out_with_version::<Option<i64>>(&expr, &ctx, version);
(0..ErgoTreeVersion::V3.into()).for_each(|version| {
let res = try_eval_out_with_version::<Option<i64>>(&expr, &ctx, version, version);
match res {
Err(EvalError::Spanned(err))
if matches!(
*err.error,
EvalError::ScriptVersionError {
required_version: ErgoTreeVersion::V6_SOFT_FORK_VERSION,
required_version: ErgoTreeVersion::V3,
activated_version: _
}
) => {}
_ => panic!("Expected script version error"),
}
});
(ErgoTreeVersion::V6_SOFT_FORK_VERSION..=ErgoTreeVersion::MAX_SCRIPT_VERSION).for_each(
(ErgoTreeVersion::V3.into()..=ErgoTreeVersion::MAX_SCRIPT_VERSION.into()).for_each(
|version| {
assert!(try_eval_out_with_version::<Option<i64>>(&expr, &ctx, version).is_err())
assert!(
try_eval_out_with_version::<Option<i64>>(&expr, &ctx, version, version)
.is_err()
)
},
);
}
Expand Down
Loading
Loading