Skip to content

Commit

Permalink
Implement get_id native function
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind committed Feb 1, 2022
1 parent 39c1f83 commit 3c20ed6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
6 changes: 1 addition & 5 deletions fastx_programmability/framework/sources/ID.move
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ module FastX::ID {
/// Get the ID for `obj`. Safe because fastX has an extra
/// bytecode verifier pass that forces every struct with
/// the `key` ability to have a distinguished `ID` field.
//public native fun get_id<T: key>(obj: &T): &ID;
public fun get_id<T: key>(_obj: &T): &ID {
// TODO: implement native function for this.
abort(0)
}
public native fun get_id<T: key>(obj: &T): &ID;

public native fun bytes_to_address(bytes: vector<u8>): address;
}
19 changes: 18 additions & 1 deletion fastx_programmability/framework/src/natives/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use move_vm_types::{
loaded_data::runtime_types::Type,
natives::function::{native_gas, NativeResult},
pop_arg,
values::Value,
values::{StructRef, Value},
};
use smallvec::smallvec;
use std::collections::VecDeque;
Expand All @@ -34,3 +34,20 @@ pub fn bytes_to_address(

Ok(NativeResult::ok(cost, smallvec![Value::address(addr)]))
}

pub fn get_id(
context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.len() == 1);
debug_assert!(args.len() == 1);

let obj = pop_arg!(args, StructRef);
let id_field = obj.borrow_field(0)?;

// TODO: what should the cost of this be?
let cost = native_gas(context.cost_table(), NativeCostIndex::SIGNER_BORROW, 0);

Ok(NativeResult::ok(cost, smallvec![id_field]))
}
1 change: 1 addition & 0 deletions fastx_programmability/framework/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn all_natives(
const FASTX_NATIVES: &[(&str, &str, NativeFunction)] = &[
("Event", "emit", event::emit),
("ID", "bytes_to_address", id::bytes_to_address),
("ID", "get_id", id::get_id),
("Transfer", "transfer_internal", transfer::transfer_internal),
(
"Transfer",
Expand Down
21 changes: 21 additions & 0 deletions fastx_programmability/framework/tests/IDTests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#[test_only]
module FastX::IDTests {
use FastX::ID;
use FastX::TxContext;

const ID_BYTES_MISMATCH: u64 = 0;

struct Object has key, drop {
id: ID::ID,
}

#[test]
fun test_get_id() {
let ctx = TxContext::dummy();
let id = TxContext::new_id(&mut ctx);
let id_bytes = *ID::get_inner(&id);
let obj = Object { id };

assert!(ID::get_id_bytes(&obj) == &id_bytes, ID_BYTES_MISMATCH);
}
}

0 comments on commit 3c20ed6

Please sign in to comment.