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

[MoveosStd] Refactor ObjectRef, allow ObjectRef as transaction argument #1026

Merged
merged 18 commits into from
Oct 28, 2023
Merged
Changes from 1 commit
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
Next Next commit
[MoveosStd] Refactor ObjectRef, allow ObjectRef as transaction argument
jolestar committed Oct 25, 2023
commit 254ff4119c70d86f5a77f319ddbbee08eb779d77
18 changes: 9 additions & 9 deletions crates/rooch-framework-tests/tests/cases/object/basic.exp
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
processed 10 tasks

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

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

task 3 'view_object'. lines 44-46:
task 3 'view_object'. lines 49-51:
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 53-53:
status EXECUTED

task 5 'view_object'. lines 50-53:
task 5 'view_object'. lines 55-58:
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:
task 6 'run'. lines 60-60:
status EXECUTED

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

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

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

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

@@ -17,22 +17,27 @@ module test::m {
// if the tx hash change, need to figure out why.
assert!(x"7852c5dcbd87e82102dba0db36d44b5a9fb0006b3e828c0b5f0832f70a8ff6ee" == tx_hash, 1000);
let obj_ref = context::new_object(ctx, S { v: 1});
let sender = context::sender(ctx);
debug::print(&obj_ref);
object_ref::to_user_owner(obj_ref, sender);
}

public entry fun move_s_to_global(ctx: &mut Context, sender: signer, object_id: ObjectID) {
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 (_id, _owner, value) = context::remove_object<S>(ctx, 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);
let sender = context::sender(ctx);
object_ref::to_user_owner(obj_ref, sender);
}

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);
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);
}
}
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
@@ -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;
}
}

@@ -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
@@ -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>>,
@@ -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_user_owner(object_ref, context::sender(ctx));
object_id
}
}

@@ -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);
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
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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);
}
7 changes: 3 additions & 4 deletions crates/rooch-framework/doc/account_coin_store.md
Original file line number Diff line number Diff line change
@@ -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
}
81 changes: 9 additions & 72 deletions crates/rooch-framework/doc/coin_store.md
Original file line number Diff line number Diff line change
@@ -16,8 +16,6 @@
- [Function `is_frozen`](#0x3_coin_store_is_frozen)
- [Function `withdraw`](#0x3_coin_store_withdraw)
- [Function `deposit`](#0x3_coin_store_deposit)
- [Function `is_coin_store_frozen`](#0x3_coin_store_is_coin_store_frozen)
- [Function `get_balance_with_id`](#0x3_coin_store_get_balance_with_id)
- [Function `freeze_coin_store_extend`](#0x3_coin_store_freeze_coin_store_extend)
- [Function `create_coin_store_internal`](#0x3_coin_store_create_coin_store_internal)

@@ -69,7 +67,7 @@ A holder of a specific coin types.
These are kept in a single resource to ensure locality of data.


<pre><code><b>struct</b> <a href="coin_store.md#0x3_coin_store_CoinStore">CoinStore</a> <b>has</b> key
<pre><code><b>struct</b> <a href="coin_store.md#0x3_coin_store_CoinStore">CoinStore</a> <b>has</b> store, key
</code></pre>


@@ -349,68 +347,6 @@ Deposit <code>amount</code> Coin<CoinType> to the balance of the passed-in <code



</details>

<a name="0x3_coin_store_is_coin_store_frozen"></a>

## Function `is_coin_store_frozen`

Get if the CoinStore is frozen via the coin store id


<pre><code><b>public</b> <b>fun</b> <a href="coin_store.md#0x3_coin_store_is_coin_store_frozen">is_coin_store_frozen</a>(ctx: &<a href="_Context">context::Context</a>, coin_store_id: <a href="_ObjectID">object::ObjectID</a>): bool
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="coin_store.md#0x3_coin_store_is_coin_store_frozen">is_coin_store_frozen</a>(ctx: &Context, coin_store_id: ObjectID): bool {
<b>if</b> (<a href="_exist_object">context::exist_object</a>(ctx, coin_store_id)){
<b>let</b> coin_store_object = <a href="_borrow_object">context::borrow_object</a>&lt;<a href="coin_store.md#0x3_coin_store_CoinStore">CoinStore</a>&gt;(ctx, coin_store_id);
<a href="_borrow">object::borrow</a>(coin_store_object).frozen
}<b>else</b>{
//TODO <b>if</b> the <a href="coin.md#0x3_coin">coin</a> store is not exist, should we <b>return</b> <b>true</b> or <b>false</b>?
<b>false</b>
}
}
</code></pre>



</details>

<a name="0x3_coin_store_get_balance_with_id"></a>

## Function `get_balance_with_id`

Get the balance of the CoinStore with the coin store id
If the CoinStore is not exist, return 0


<pre><code><b>public</b> <b>fun</b> <a href="coin_store.md#0x3_coin_store_get_balance_with_id">get_balance_with_id</a>(ctx: &<a href="_Context">context::Context</a>, coin_store_id: <a href="_ObjectID">object::ObjectID</a>): u256
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="coin_store.md#0x3_coin_store_get_balance_with_id">get_balance_with_id</a>(ctx: &Context, coin_store_id: ObjectID): u256 {
<b>if</b> (<a href="_exist_object">context::exist_object</a>(ctx, coin_store_id)){
<b>let</b> coin_store_object = <a href="_borrow_object">context::borrow_object</a>&lt;<a href="coin_store.md#0x3_coin_store_CoinStore">CoinStore</a>&gt;(ctx, coin_store_id);
<a href="_borrow">object::borrow</a>(coin_store_object).balance.value
}<b>else</b>{
0u256
}
}
</code></pre>



</details>

<a name="0x3_coin_store_freeze_coin_store_extend"></a>
@@ -422,7 +358,7 @@ This function is for he <code>CoinType</code> module to extend,
Only the <code>CoinType</code> module can freeze or unfreeze a CoinStore by the coin store id


<pre><code><b>public</b> <b>fun</b> <a href="coin_store.md#0x3_coin_store_freeze_coin_store_extend">freeze_coin_store_extend</a>&lt;CoinType: key&gt;(ctx: &<b>mut</b> <a href="_Context">context::Context</a>, coin_store_id: <a href="_ObjectID">object::ObjectID</a>, frozen: bool)
<pre><code><b>public</b> <b>fun</b> <a href="coin_store.md#0x3_coin_store_freeze_coin_store_extend">freeze_coin_store_extend</a>&lt;CoinType: key&gt;(_ctx: &<b>mut</b> <a href="_Context">context::Context</a>, _coin_store_id: <a href="_ObjectID">object::ObjectID</a>, _frozen: bool)
</code></pre>


@@ -432,13 +368,14 @@ Only the <code>CoinType</code> module can freeze or unfreeze a CoinStore by the


<pre><code><b>public</b> <b>fun</b> <a href="coin_store.md#0x3_coin_store_freeze_coin_store_extend">freeze_coin_store_extend</a>&lt;CoinType: key&gt;(
ctx: &<b>mut</b> Context,
coin_store_id: ObjectID,
frozen: bool,
_ctx: &<b>mut</b> Context,
_coin_store_id: ObjectID,
_frozen: bool,
) {
<b>assert</b>!(<a href="_exist_object">context::exist_object</a>(ctx, coin_store_id), <a href="_invalid_argument">error::invalid_argument</a>(<a href="coin_store.md#0x3_coin_store_ErrorCoinStoreNotFound">ErrorCoinStoreNotFound</a>));
<b>let</b> coin_store_object = <a href="_borrow_object_mut">context::borrow_object_mut</a>&lt;<a href="coin_store.md#0x3_coin_store_CoinStore">CoinStore</a>&gt;(ctx, coin_store_id);
<a href="_borrow_mut">object::borrow_mut</a>(coin_store_object).frozen = frozen;
//TODO how <b>to</b> provide <b>freeze</b> <a href="coin.md#0x3_coin">coin</a> store via <a href="coin.md#0x3_coin">coin</a> store id
// <b>assert</b>!(<a href="_exist_object">context::exist_object</a>(ctx, coin_store_id), <a href="_invalid_argument">error::invalid_argument</a>(<a href="coin_store.md#0x3_coin_store_ErrorCoinStoreNotFound">ErrorCoinStoreNotFound</a>));
// <b>let</b> coin_store_object = <a href="_borrow_object_mut">context::borrow_object_mut</a>&lt;<a href="coin_store.md#0x3_coin_store_CoinStore">CoinStore</a>&gt;(ctx, coin_store_id);
// <a href="_borrow_mut">object::borrow_mut</a>(coin_store_object).frozen = frozen;
}
</code></pre>

7 changes: 3 additions & 4 deletions crates/rooch-framework/sources/account_coin_store.move
Original file line number Diff line number Diff line change
@@ -82,10 +82,9 @@ module rooch_framework::account_coin_store {

/// Returns the balance of `addr` for provided `CoinType`.
public fun balance<CoinType: key>(ctx: &Context, addr: address): u256 {
let coin_store_id_option = coin_store_id<CoinType>(ctx, addr);
if (option::is_some(&coin_store_id_option)) {
let coin_store_id = option::extract(&mut coin_store_id_option);
coin_store::get_balance_with_id(ctx, coin_store_id)
if(exist_account_coin_store<CoinType>(ctx, addr)) {
let coin_store = borrow_account_coin_store<CoinType>(ctx, addr);
coin_store::balance(coin_store)
} else {
0u256
}
Loading