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

Test ID and Hashlock contracts from LigoIDE #27

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions crates/vm_library/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub fn populate_predef(sender: String, self_: String, source: String) {
map.insert("empty_set".to_owned(), Value::Set(ordset![]));
map.insert("empty_map".to_owned(), Value::Map(ordmap! {}));
map.insert("zero".to_owned(), Value::Int(0.into()));
map.insert("amount".to_owned(), Value::Int(0.into()));
map.insert("now".to_owned(), Value::Int(0.into()));
}
pub fn push_constants(vec: Vec<(i32, Value)>) {
let map = unsafe { &mut CONSTANTS };
Expand Down
26 changes: 19 additions & 7 deletions crates/vm_library/src/managed/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,10 @@ pub fn neq(env: &Context, value: Value) -> VMResult<i64> {

pub fn eq(env: &Context, value: Value) -> VMResult<i64> {
env.update_gas(300)?;
let one: rug::Integer = rug::Integer::from(1);

let res: VMResult<Value> = match value {
Value::Int(n) if n == Integer::ZERO => Ok(false),
Value::Int(n) if n == one => Ok(true),
Value::Int(n) if n == Integer::ZERO => Ok(true),
Value::Int(_) => Ok(false),

_ => Err(FFIError::ExternError {
value: (value).clone(),
Expand Down Expand Up @@ -151,7 +150,7 @@ pub fn unpair(env: &Context, value: Value) -> VMResult<()> {
}
_ => Err(FFIError::ExternError {
value: (value),
msg: "type mismatch, expected Pair".to_owned(),
msg: "unpair, type mismatch, expected Pair".to_owned(),
}
.into()),
}
Expand All @@ -162,7 +161,7 @@ pub fn car(env: &Context, value: Value) -> VMResult<i64> {
Value::Pair { fst, snd: _ } => conversions::to_i64(fst.data().as_ffi()),
_ => Err(FFIError::ExternError {
value: (value),
msg: "type mismatch, expected Pair".to_owned(),
msg: "car, type mismatch, expected Pair".to_owned(),
}
.into()),
}
Expand All @@ -173,7 +172,7 @@ pub fn cdr(env: &Context, value: Value) -> VMResult<i64> {
Value::Pair { fst: _, snd } => conversions::to_i64(snd.data().as_ffi()),
_ => Err(FFIError::ExternError {
value: (value),
msg: "type mismatch, expected Pair".to_owned(),
msg: "cdr, type mismatch, expected Pair".to_owned(),
}
.into()),
}
Expand Down Expand Up @@ -780,7 +779,7 @@ pub fn get_n(env: &Context, idx: u32, value: Value) -> VMResult<i64> {
(_, value) => {
return Err(FFIError::ExternError {
value: (value),
msg: "type mismatch, expected Pair".to_owned(),
msg: "get_n, type mismatch, expected Pair".to_owned(),
}
.into())
}
Expand Down Expand Up @@ -893,6 +892,7 @@ pub fn get_and_update(env: &Context, key: Value, value: Value, map: Value) -> VM
.into()),
}
}

pub const fn call1<A, F>(f: F) -> impl Fn(&Context, i64) -> VMResult<A>
where
F: Fn(&Context, Value) -> VMResult<A>,
Expand Down Expand Up @@ -1183,6 +1183,10 @@ pub fn make_imports(env: &Context, store: &Store) -> ImportObject {
"transfer_tokens",
Function::new_native_with_env(store, env.clone(), call3(transfer_tokens)),
);
exports.insert(
"now",
Function::new_native_with_env(store, env.clone(), now),
);
exports.insert(
"nil",
Function::new_native_with_env(store, env.clone(), nil),
Expand Down Expand Up @@ -1422,6 +1426,14 @@ fn read_ticket(env: &Context, payload: Value) -> VMResult<()> {
)),
}
}
fn now(c: &Context) -> VMResult<i64> {
let predef = unsafe { &PREDEF };
let now = predef
.get("now")
.map_or_else(|| Err(VmError::RuntimeErr("cant happen".to_owned())), Ok)?;
let bumped = c.bump(now.clone());
conversions::to_i64(bumped)
}
fn nil(c: &Context) -> VMResult<i64> {
let predef = unsafe { &PREDEF };
let nil = predef
Expand Down
6 changes: 4 additions & 2 deletions crates/vm_library/tests/fa12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ fn approve() {
let arg = Value::Union(Union::Left(bump));
let (deser, module) = common::deser(payload);
let tickets: Vec<Ticket> = vec![];
let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage, &None);
let init =
common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone(), &None);
let ExecutionResult { new_storage, .. } = invoke_managed(init).unwrap();
assert_eq!(
serde_json::to_string(&new_storage).unwrap(),
Expand Down Expand Up @@ -165,7 +166,8 @@ fn transfer() {
let arg = Value::Union(Union::Right(bump));
let (deser, module) = common::deser(payload);
let tickets: Vec<Ticket> = vec![];
let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage, &None);
let init =
common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone(), &None);
let ExecutionResult { new_storage, .. } = invoke_managed(init).unwrap();
assert_eq!(
serde_json::to_string(&new_storage).unwrap(),
Expand Down
Loading