Skip to content

Commit

Permalink
fix fromating
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianrof committed Dec 18, 2024
1 parent 038ec0d commit aa99af9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
4 changes: 3 additions & 1 deletion ergotree-interpreter/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ fn smethod_eval_fn(method: &SMethod) -> Result<EvalFn, EvalError> {
sglobal::TYPE_CODE => match method.method_id() {
sglobal::GROUP_GENERATOR_METHOD_ID => self::sglobal::GROUP_GENERATOR_EVAL_FN,
sglobal::XOR_METHOD_ID => self::sglobal::XOR_EVAL_FN,
sglobal::FROM_BIGENDIAN_BYTES_METHOD_ID => self::sglobal::SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN,
sglobal::FROM_BIGENDIAN_BYTES_METHOD_ID => {
self::sglobal::SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN
}
method_id => {
return Err(EvalError::NotFound(format!(
"Eval fn: method {:?} with method id {:?} not found in SGlobal",
Expand Down
77 changes: 46 additions & 31 deletions ergotree-interpreter/src/eval/sglobal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use crate::eval::EvalError;

use ergotree_ir::mir::value::{CollKind, NativeColl, Value};

use super::EvalFn;
use ergo_chain_types::ec_point::generator;
use ergotree_ir::bigint256::BigInt256;
use ergotree_ir::types::stype::SType;
use super::EvalFn;

fn helper_xor(x: &[i8], y: &[i8]) -> Arc<[i8]> {
x.iter().zip(y.iter()).map(|(x1, x2)| *x1 ^ *x2).collect()
Expand Down Expand Up @@ -70,10 +70,12 @@ pub(crate) static SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN: EvalFn = |mc, _env, _ctx

let bytes = match bytes_val {
Value::Coll(CollKind::NativeColl(NativeColl::CollByte(bytes))) => bytes,
_ => return Err(EvalError::UnexpectedValue(format!(
"fromBigEndianBytes: expected first argument to be byte array, got {:?}",
bytes_val
))),
_ => {
return Err(EvalError::UnexpectedValue(format!(
"fromBigEndianBytes: expected first argument to be byte array, got {:?}",
bytes_val
)))
}
};

match *type_val {
Expand Down Expand Up @@ -102,9 +104,9 @@ pub(crate) static SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN: EvalFn = |mc, _env, _ctx
));
}
let bytes_vec: Vec<u8> = bytes.iter().map(|&x| x as u8).collect();

Check failure on line 106 in ergotree-interpreter/src/eval/sglobal.rs

View workflow job for this annotation

GitHub Actions / Build without default features

cannot find type `Vec` in this scope
let int_bytes: [u8; 4] = bytes_vec.try_into().map_err(|_| EvalError::UnexpectedValue(
"Invalid byte array length for Int".to_string(),
))?;
let int_bytes: [u8; 4] = bytes_vec.try_into().map_err(|_| {
EvalError::UnexpectedValue("Invalid byte array length for Int".to_string())
})?;
Ok(Value::Int(i32::from_be_bytes(int_bytes)))
}
SType::SLong => {
Expand All @@ -114,9 +116,9 @@ pub(crate) static SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN: EvalFn = |mc, _env, _ctx
));
}
let bytes_vec: Vec<u8> = bytes.iter().map(|&x| x as u8).collect();

Check failure on line 118 in ergotree-interpreter/src/eval/sglobal.rs

View workflow job for this annotation

GitHub Actions / Build without default features

cannot find type `Vec` in this scope
let long_bytes: [u8; 8] = bytes_vec.try_into().map_err(|_| EvalError::UnexpectedValue(
"Invalid byte array length for Long".to_string(),
))?;
let long_bytes: [u8; 8] = bytes_vec.try_into().map_err(|_| {
EvalError::UnexpectedValue("Invalid byte array length for Long".to_string())
})?;
Ok(Value::Long(i64::from_be_bytes(long_bytes)))
}
SType::SBigInt => {
Expand All @@ -127,8 +129,8 @@ pub(crate) static SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN: EvalFn = |mc, _env, _ctx
}
let bytes_vec: Vec<u8> = bytes.iter().map(|&x| x as u8).collect();

Check failure on line 130 in ergotree-interpreter/src/eval/sglobal.rs

View workflow job for this annotation

GitHub Actions / Build without default features

cannot find type `Vec` in this scope
let big_int = num_bigint::BigInt::from_bytes_be(num_bigint::Sign::Plus, &bytes_vec);
Ok(Value::BigInt(BigInt256::try_from(big_int).map_err(|e|
EvalError::UnexpectedValue(format!("Failed to convert to BigInt256: {:?}", e))
Ok(Value::BigInt(BigInt256::try_from(big_int).map_err(
|e| EvalError::UnexpectedValue(format!("Failed to convert to BigInt256: {:?}", e)),
)?))
}
SType::SUnit => {
Expand All @@ -140,7 +142,8 @@ pub(crate) static SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN: EvalFn = |mc, _env, _ctx
Ok(Value::Unit)
}
_ => Err(EvalError::UnexpectedValue(format!(
"Unsupported type provided in fromBigEndianBytes: {:?}", type_val
"Unsupported type provided in fromBigEndianBytes: {:?}",
type_val
))),
}
};
Expand Down Expand Up @@ -201,11 +204,13 @@ mod tests {
let bytes = vec![0_i8, 0, 0, 1];
let expr: Expr = MethodCall::new(
Expr::Global,
sglobal::FROM_BIGENDIAN_BYTES_METHOD.clone().with_concrete_types(&type_args),
sglobal::FROM_BIGENDIAN_BYTES_METHOD
.clone()
.with_concrete_types(&type_args),
vec![bytes.into()],
)
.unwrap()
.into();
.unwrap()
.into();
let ctx = force_any_val::<Context>();
assert_eq!(eval_out::<i32>(&expr, &ctx), 1);
}
Expand All @@ -217,11 +222,13 @@ mod tests {
let bytes = vec![0_i8, 1];
let expr: Expr = MethodCall::new(
Expr::Global,
sglobal::FROM_BIGENDIAN_BYTES_METHOD.clone().with_concrete_types(&type_args),
sglobal::FROM_BIGENDIAN_BYTES_METHOD
.clone()
.with_concrete_types(&type_args),
vec![bytes.into()],
)
.unwrap()
.into();
.unwrap()
.into();
let ctx = force_any_val::<Context>();
assert_eq!(eval_out::<i16>(&expr, &ctx), 1);
}
Expand All @@ -233,11 +240,13 @@ mod tests {
let bytes = vec![0_i8, 0, 0, 0, 0, 0, 0, 1];
let expr: Expr = MethodCall::new(
Expr::Global,
sglobal::FROM_BIGENDIAN_BYTES_METHOD.clone().with_concrete_types(&type_args),
sglobal::FROM_BIGENDIAN_BYTES_METHOD
.clone()
.with_concrete_types(&type_args),
vec![bytes.into()],
)
.unwrap()
.into();
.unwrap()
.into();
let ctx = force_any_val::<Context>();
assert_eq!(eval_out::<i64>(&expr, &ctx), 1);
}
Expand All @@ -249,11 +258,13 @@ mod tests {
let bytes = vec![0_i8, 0, 0, 1];
let expr: Expr = MethodCall::new(
Expr::Global,
sglobal::FROM_BIGENDIAN_BYTES_METHOD.clone().with_concrete_types(&type_args),
sglobal::FROM_BIGENDIAN_BYTES_METHOD
.clone()
.with_concrete_types(&type_args),
vec![bytes.into()],
)
.unwrap()
.into();
.unwrap()
.into();
let ctx = force_any_val::<Context>();
assert_ne!(eval_out::<i32>(&expr, &ctx), 2);
}
Expand All @@ -265,14 +276,18 @@ mod tests {
let bytes = vec![0_i8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
let expr: Expr = MethodCall::new(
Expr::Global,
sglobal::FROM_BIGENDIAN_BYTES_METHOD.clone().with_concrete_types(&type_args),
sglobal::FROM_BIGENDIAN_BYTES_METHOD
.clone()
.with_concrete_types(&type_args),
vec![bytes.into()],
)
.unwrap()
.into();
.unwrap()
.into();
let ctx = force_any_val::<Context>();
let expected_bigint = num_bigint::BigInt::from(1);
assert_eq!(eval_out::<BigInt256>(&expr, &ctx), BigInt256::try_from(expected_bigint).unwrap());
assert_eq!(
eval_out::<BigInt256>(&expr, &ctx),
BigInt256::try_from(expected_bigint).unwrap()
);
}

}
4 changes: 2 additions & 2 deletions ergotree-ir/src/types/sglobal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use super::smethod::SMethodDesc;
use super::stype::SType;
use crate::types::smethod::SMethod;
use crate::types::stype_companion::STypeCompanion;
use crate::types::stype_param::{STypeParam, STypeVar};
use alloc::vec;
use alloc::vec::Vec;
use lazy_static::lazy_static;
use crate::types::stype_param::{STypeParam, STypeVar};

/// SGlobal type code
pub const TYPE_CODE: TypeCode = TypeCode::SGLOBAL;
Expand Down Expand Up @@ -65,7 +65,7 @@ lazy_static! {

}

lazy_static!{
lazy_static! {
static ref FROM_BIGENDIAN_BYTES_METHOD_DESC: SMethodDesc = SMethodDesc {
method_id: FROM_BIGENDIAN_BYTES_METHOD_ID,
name: "fromBigEndianBytes",
Expand Down

0 comments on commit aa99af9

Please sign in to comment.