Skip to content

Commit

Permalink
[MoveosStd] Refactor ObjectRef, allow ObjectRef as transaction argume…
Browse files Browse the repository at this point in the history
…nt (#1026)

* [MoveosStd] Refactor ObjectRef, allow ObjectRef as transaction argument

* [CLI] verify modules when publish at client side.

* [Example] refactor simple_blog example via new ObjectRef API

* [MoveosStd] Add native function as_ref_inner and as_mut_ref_inner

* [MoveosStd] Simpler the Object&ObjectRef logic

* fix example/simple_blog

* update basic_object

* [Object] define to_permanent to_shared to_frozen

* [MoveosStd] set create_object default owner to system

* [Example] Refactor NFT example

* [Examples] Add entry function to nft example

* [examples] complex_struct and entry_function_arguments example

* [examples] blog example

* [examples] blog example

* [examples] Rename simple blog module name

* [Doc] Update moveos stdlib docs

* fix panic message
  • Loading branch information
jolestar authored Oct 28, 2023
1 parent c6276db commit 0c151ef
Show file tree
Hide file tree
Showing 71 changed files with 1,421 additions and 1,580 deletions.
28 changes: 6 additions & 22 deletions crates/rooch-framework-tests/tests/cases/object/basic.exp
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
processed 10 tasks
processed 6 tasks

task 1 'publish'. lines 3-40:
task 1 'publish'. lines 3-43:
status EXECUTED

task 2 'run'. lines 42-42:
task 2 'run'. lines 45-45:
status EXECUTED

task 3 'view_object'. lines 44-46:
task 3 'view_object'. lines 47-49:
Object { id: ObjectID(ae43e34e51db9c833ab50dd9aa8b27106519e5bbfd533737306e7b69ef253647), owner: 0000000000000000000000000000000000000000000000000000000000000043, value: AnnotatedMoveStruct { abilities: [Store, Key, ], type_: StructTag { address: 0000000000000000000000000000000000000000000000000000000000000042, module: Identifier("m"), name: Identifier("S"), type_params: [] }, value: [(Identifier("v"), U8(1))] } }

task 4 'run'. lines 48-48:
task 4 'run'. lines 51-51:
status EXECUTED

task 5 'view_object'. lines 50-53:
task 5 'view_object'. lines 53-53:
Object { id: ObjectID(0bbaf311ae6768a532b1f9dee65b1758a7bb1114fd57df8fa94cb2d1cb5f6896), owner: 0000000000000000000000000000000000000000000000000000000000000043, value: AnnotatedMoveStruct { abilities: [Store, Key, ], type_: StructTag { address: 0000000000000000000000000000000000000000000000000000000000000042, module: Identifier("m"), name: Identifier("Cup"), type_params: [Struct(StructTag { address: 0000000000000000000000000000000000000000000000000000000000000042, module: Identifier("m"), name: Identifier("S"), type_params: [] })] }, value: [(Identifier("v"), U8(2))] } }

task 6 'run'. lines 55-55:
status EXECUTED

task 7 'view'. lines 57-59:
store key 0x42::m::S {
v: 1u8
}

task 8 'run'. lines 61-61:
status EXECUTED

task 9 'view'. lines 63-63:
store key 0x42::m::Cup<0x42::m::S> {
v: 2u8
}
38 changes: 14 additions & 24 deletions crates/rooch-framework-tests/tests/cases/object/basic.move
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

module test::m {
use moveos_std::context::{Self, Context};
use moveos_std::object::ObjectID;
use moveos_std::account_storage;
use moveos_std::object_ref;
use std::debug;

struct S has store, key { v: u8 }
Expand All @@ -18,23 +17,27 @@ module test::m {
assert!(x"7852c5dcbd87e82102dba0db36d44b5a9fb0006b3e828c0b5f0832f70a8ff6ee" == tx_hash, 1000);
let obj_ref = context::new_object(ctx, S { v: 1});
debug::print(&obj_ref);
object_ref::to_permanent(obj_ref);
}

public entry fun move_s_to_global(ctx: &mut Context, sender: signer, object_id: ObjectID) {
debug::print(&object_id);
let (_id, _owner, value) = context::remove_object<S>(ctx, object_id);
account_storage::global_move_to(ctx, &sender, value);
}
//We can not use `ObjectRef<S>` as transaction argument now.
// public entry fun move_s_to_global(ctx: &mut Context, sender: signer, object_s: ObjectRef<S>) {
// let object_id = object_ref::id(&object_s);
// debug::print(&object_id);
// let value = object_ref::remove(object_s);
// account_storage::global_move_to(ctx, &sender, value);
// }

public entry fun mint_cup<T: store>(ctx: &mut Context) {
let obj_ref = context::new_object(ctx, Cup<T> { v: 2 });
debug::print(&obj_ref);
object_ref::to_permanent(obj_ref);
}

public entry fun move_cup_to_global<T:store>(ctx: &mut Context, sender: signer, object_id: ObjectID) {
let (_id,_owner,value) = context::remove_object<Cup<S>>(ctx, object_id);
account_storage::global_move_to(ctx, &sender, value);
}
// public entry fun move_cup_to_global<T:store>(ctx: &mut Context, sender: signer, object_s: ObjectRef<Cup<S>>) {
// let value = object_ref::remove(object_s);
// account_storage::global_move_to(ctx, &sender, value);
// }
}

// Mint S to A.
Expand All @@ -48,16 +51,3 @@ module test::m {
//# run test::m::mint_cup --type-args test::m::S --signers A

//# view_object --object-id 0x0bbaf311ae6768a532b1f9dee65b1758a7bb1114fd57df8fa94cb2d1cb5f6896

// Move S to global.
//Currently, we use @address to pass object argument to the transaction, define a new way to pass object argument to the transaction.

//# run test::m::move_s_to_global --signers A --args @0xae43e34e51db9c833ab50dd9aa8b27106519e5bbfd533737306e7b69ef253647

//# view --address A --resource test::m::S

// Move Cup<S> to global.

//# run test::m::move_cup_to_global --signers A --type-args test::m::S --args @0x0bbaf311ae6768a532b1f9dee65b1758a7bb1114fd57df8fa94cb2d1cb5f6896

//# view --address A --resource test::m::Cup<test::m::S>
12 changes: 6 additions & 6 deletions crates/rooch-framework-tests/tests/cases/object/object_cap.exp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ task 1 'publish'. lines 3-21:
status EXECUTED

task 2 'run'. lines 22-34:
Error: error: resource type "TestObject" in function "0x2::context::new_object" not defined in current module or not allowed
Error: error: resource type "TestStruct" in function "0x2::context::new_object" not defined in current module or not allowed
┌─ /tmp/tempfile:29:23
29 │ let obj_ref = context::new_object<TestObject>(ctx, object);
29 │ let obj_ref = context::new_object<TestStruct>(ctx, object);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: resource type "TestObject" in function "0x2::context::remove_object" not defined in current module or not allowed
┌─ /tmp/tempfile:30:42
error: resource type "TestStruct" in function "0x2::object_ref::remove" not defined in current module or not allowed
┌─ /tmp/tempfile:30:27
30 │ let (_id, _owner, test_object) = context::remove_object<TestObject>(ctx, object_ref::id(&obj_ref));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30 │ let test_struct = object_ref::remove(obj_ref);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^


20 changes: 10 additions & 10 deletions crates/rooch-framework-tests/tests/cases/object/object_cap.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
//# publish

module test::m {
struct TestObject has key{
struct TestStruct has key{
f: u8
}

public fun new_test_object(f: u8): TestObject {
TestObject{
public fun new_test_struct(f: u8): TestStruct {
TestStruct{
f,
}
}

public fun destroy_test_object(test_object: TestObject) {
let TestObject{f : _f} = test_object;
public fun destroy_test_struct(test_struct: TestStruct) {
let TestStruct{f : _f} = test_struct;
}
}

Expand All @@ -23,13 +23,13 @@ module test::m {
script {
use moveos_std::context::{Self, Context};
use moveos_std::object_ref;
use test::m::{Self, TestObject};
use test::m::{Self, TestStruct};

fun main(ctx: &mut Context) {
let object = m::new_test_object(12);
let obj_ref = context::new_object<TestObject>(ctx, object);
let (_id, _owner, test_object) = context::remove_object<TestObject>(ctx, object_ref::id(&obj_ref));
m::destroy_test_object(test_object);
let object = m::new_test_struct(12);
let obj_ref = context::new_object<TestStruct>(ctx, object);
let test_struct = object_ref::remove(obj_ref);
m::destroy_test_struct(test_struct);
}
}

6 changes: 3 additions & 3 deletions crates/rooch-framework-tests/tests/cases/table/basic.exp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
processed 4 tasks

task 1 'publish'. lines 3-42:
task 1 'publish'. lines 3-39:
status EXECUTED

task 2 'run'. lines 44-56:
task 2 'run'. lines 41-53:
status EXECUTED

task 3 'run'. lines 58-71:
task 3 'run'. lines 55-67:
status EXECUTED
30 changes: 13 additions & 17 deletions crates/rooch-framework-tests/tests/cases/table/basic.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ module test::m {
use std::string::String;
use moveos_std::table::{Self, Table};
use moveos_std::context::{Self, Context};
use moveos_std::object;
use moveos_std::object::ObjectID;
use moveos_std::object_ref::{ObjectRef};
use moveos_std::object_ref;

struct KVStore has store, key {
table: Table<String,vector<u8>>,
Expand All @@ -31,13 +30,11 @@ module test::m {
table::borrow(&store.table, key)
}

public fun save_to_object_storage(ctx: &mut Context, kv: KVStore) : ObjectRef<KVStore> {
context::new_object(ctx, kv)
}

public fun borrow_from_object_storage(ctx: &mut Context, object_id: ObjectID): &KVStore {
let object = context::borrow_object(ctx, object_id);
object::borrow<KVStore>(object)
public fun save_to_object_storage(ctx: &mut Context, kv: KVStore) : ObjectID {
let object_ref = context::new_object(ctx, kv);
let object_id = object_ref::id(&object_ref);
object_ref::to_permanent(object_ref);
object_id
}
}

Expand All @@ -50,20 +47,19 @@ script {
fun main(ctx: &mut Context) {
let kv = m::make_kv_store(ctx);
m::add(&mut kv, string::utf8(b"test"), b"value");
let object_ref = m::save_to_object_storage(ctx, kv);
std::debug::print(&object_ref);
let object_id = m::save_to_object_storage(ctx, kv);
std::debug::print(&object_id);
}
}

//# run --signers test --args @0xcc48c91b1a0f15813bed988390a2794660ae5dadcd86fdb1b55d4a28d0f74c4d
//# run --signers test --args @0x1a2c876ea44c751aedab69ef139181114c79abf4fb8bca363b66969218e7d815
script {
use std::string;
use moveos_std::context::{Context};
use moveos_std::object::ObjectID;
use test::m;
use moveos_std::object_ref::{Self, ObjectRef};
use test::m::{Self, KVStore};

fun main(ctx: &mut Context, object_id: ObjectID) {
let kv = m::borrow_from_object_storage(ctx, object_id);
fun main(kv_object: &ObjectRef<KVStore>) {
let kv = object_ref::borrow(kv_object);
assert!(m::contains(kv, string::utf8(b"test")), 1000);
let v = m::borrow(kv, string::utf8(b"test"));
assert!(v == &b"value", 1001);
Expand Down
12 changes: 6 additions & 6 deletions crates/rooch-framework-tests/tests/cases/table/test_destroy.exp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
processed 7 tasks

task 1 'publish'. lines 3-76:
task 1 'publish'. lines 3-64:
status EXECUTED

task 2 'run'. lines 78-92:
task 2 'run'. lines 66-80:
status EXECUTED

task 3 'run'. lines 93-108:
task 3 'run'. lines 81-96:
status EXECUTED

task 4 'run'. lines 109-124:
task 4 'run'. lines 97-112:
status EXECUTED

task 5 'run'. lines 125-137:
task 5 'run'. lines 113-125:
status ABORTED with code 4 in 0000000000000000000000000000000000000000000000000000000000000002::raw_table

task 6 'run'. lines 138-151:
task 6 'run'. lines 126-139:
status EXECUTED
16 changes: 2 additions & 14 deletions crates/rooch-framework-tests/tests/cases/table/test_destroy.move
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
module test::m {
use std::string::String;
use moveos_std::table::{Self, Table};
use moveos_std::context::{Self, Context};
use moveos_std::object;
use moveos_std::object_ref::{ObjectRef};
use moveos_std::object::ObjectID;
use moveos_std::context::{Context};
use moveos_std::account_storage;

struct KVStore has store, key {
Expand Down Expand Up @@ -35,16 +32,7 @@ module test::m {
public fun borrow(store: &KVStore, key: String): &vector<u8> {
table::borrow(&store.table, key)
}

public fun save_to_object_storage(ctx: &mut Context, kv: KVStore) : ObjectRef<KVStore> {
context::new_object(ctx, kv)
}

public fun borrow_from_object_storage(ctx: &mut Context, object_id: ObjectID): &KVStore {
let object = context::borrow_object(ctx, object_id);
object::borrow<KVStore>(object)
}


public fun save_to_account_storage(ctx: &mut Context, account: &signer, store: KVStore){
account_storage::global_move_to(ctx, account, store);
}
Expand Down
7 changes: 3 additions & 4 deletions crates/rooch-framework/doc/account_coin_store.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,9 @@ Returns the balance of <code>addr</code> for provided <code>CoinType</code>.


<pre><code><b>public</b> <b>fun</b> <a href="account_coin_store.md#0x3_account_coin_store_balance">balance</a>&lt;CoinType: key&gt;(ctx: &Context, addr: <b>address</b>): u256 {
<b>let</b> coin_store_id_option = <a href="account_coin_store.md#0x3_account_coin_store_coin_store_id">coin_store_id</a>&lt;CoinType&gt;(ctx, addr);
<b>if</b> (<a href="_is_some">option::is_some</a>(&coin_store_id_option)) {
<b>let</b> coin_store_id = <a href="_extract">option::extract</a>(&<b>mut</b> coin_store_id_option);
<a href="coin_store.md#0x3_coin_store_get_balance_with_id">coin_store::get_balance_with_id</a>(ctx, coin_store_id)
<b>if</b>(<a href="account_coin_store.md#0x3_account_coin_store_exist_account_coin_store">exist_account_coin_store</a>&lt;CoinType&gt;(ctx, addr)) {
<b>let</b> <a href="coin_store.md#0x3_coin_store">coin_store</a> = <a href="account_coin_store.md#0x3_account_coin_store_borrow_account_coin_store">borrow_account_coin_store</a>&lt;CoinType&gt;(ctx, addr);
<a href="coin_store.md#0x3_coin_store_balance">coin_store::balance</a>(<a href="coin_store.md#0x3_coin_store">coin_store</a>)
} <b>else</b> {
0u256
}
Expand Down
Loading

0 comments on commit 0c151ef

Please sign in to comment.