-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support list fields for object.move (#3142)
* feat: support list fields for object.move * fix UNKNOWN_INVARIANT_VIOLATION_ERROR * rename list_fields to list_field_names * fix native return types * make code tidy * make code tidy * make code tidy * support cursor and limit for list_field_keys * update object list_field_keys gas handle and adjust fetch data from db * make code tidy * update native_list_field_keys default value to 0 * check is_empty that before add native_list_field_keys * update is_empty for ListFieldsGasParametersOption * fix review notes * fix lint * add list_field_keys for table and add rooch-framework-tests * update ut assert value * update list_field_keys of table notes * add length ut for list_field_keys * add more ut for list_field_keys * make ut tidy * add iterator for table * fix next/next_mut for table issue * update borrow_field_key_internal and borrow_mut_field_key_internal notes * add How to test move contract? notes * update borrow_field_key_internal and borrow_mut_field_key_internal notes * update review notes * update review notes --------- Co-authored-by: jolestar <jolestar@gmail.com>
- Loading branch information
Showing
16 changed files
with
625 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
crates/rooch-framework-tests/tests/cases/table/list_field_keys.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
processed 4 tasks | ||
|
||
task 1 'publish'. lines 3-67: | ||
status EXECUTED | ||
|
||
task 2 'run'. lines 69-123: | ||
status EXECUTED | ||
|
||
task 3 'run'. lines 125-145: | ||
status EXECUTED |
145 changes: 145 additions & 0 deletions
145
crates/rooch-framework-tests/tests/cases/table/list_field_keys.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
//# init --addresses test=0x42 | ||
|
||
//# publish | ||
module test::m { | ||
use std::string::String; | ||
use std::option; | ||
|
||
use moveos_std::table::{Self, Table, Iterator}; | ||
|
||
use moveos_std::object::ObjectID; | ||
use moveos_std::object; | ||
|
||
struct KVStore has store, key { | ||
table: Table<String,vector<u8>>, | ||
} | ||
|
||
public fun make_kv_store(): KVStore { | ||
KVStore{ | ||
table: table::new(), | ||
} | ||
} | ||
|
||
public fun add(store: &mut KVStore, key: String, value: vector<u8>) { | ||
table::add(&mut store.table, key, value); | ||
} | ||
|
||
public fun contains(store: &KVStore, key: String): bool { | ||
table::contains(&store.table, key) | ||
} | ||
|
||
public fun borrow(store: &KVStore, key: String): &vector<u8> { | ||
table::borrow(&store.table, key) | ||
} | ||
|
||
public fun save_to_object_storage(kv: KVStore) : ObjectID { | ||
let object = object::new(kv); | ||
let object_id = object::id(&object); | ||
object::to_shared(object); | ||
object_id | ||
} | ||
|
||
public fun list_field_keys(store: &KVStore): Iterator<String, vector<u8>> { | ||
table::list_field_keys(&store.table, option::none(), 10000) | ||
} | ||
|
||
public fun list_field_keys_with_name(store: &KVStore, key: String, limit: u64): Iterator<String, vector<u8>> { | ||
table::list_field_keys(&store.table, option::some(key), limit) | ||
} | ||
|
||
public fun field_keys_len(iterator: &Iterator<String, vector<u8>>): u64 { | ||
table::field_keys_len(iterator) | ||
} | ||
|
||
public fun next(iterator: &mut Iterator<String, vector<u8>>): (String, vector<u8>) { | ||
let (k, v) = table::next(iterator); | ||
(*k, *v) | ||
} | ||
|
||
public fun next_mut(iterator: &mut Iterator<String, vector<u8>>): (String, vector<u8>) { | ||
let (k, v) = table::next_mut(iterator); | ||
(*k, *v) | ||
} | ||
|
||
public fun length(store: &KVStore): u64 { | ||
table::length(&store.table) | ||
} | ||
} | ||
|
||
//# run --signers test | ||
script { | ||
use std::string; | ||
use test::m; | ||
|
||
fun main() { | ||
let kv = m::make_kv_store(); | ||
m::add(&mut kv, string::utf8(b"test"), b"value"); | ||
m::add(&mut kv, string::utf8(b"test2"), b"value2"); | ||
m::add(&mut kv, string::utf8(b"test3"), b"value3"); | ||
|
||
let iter = m::list_field_keys(&kv); | ||
std::debug::print(&iter); | ||
assert!(m::field_keys_len(&iter) == 3, 1001); | ||
|
||
m::add(&mut kv, string::utf8(b"test4"), b"value4"); | ||
let iter = m::list_field_keys(&kv); | ||
std::debug::print(&iter); | ||
assert!(m::field_keys_len(&iter) == 4, 1002); | ||
|
||
let (k, v) = m::next_mut(&mut iter); | ||
std::debug::print(&k); | ||
std::debug::print(&string::utf8(v)); | ||
v = b"new_value"; | ||
std::debug::print(&string::utf8(v)); | ||
|
||
let (k, v) = m::next(&mut iter); | ||
std::debug::print(&k); | ||
std::debug::print(&string::utf8(v)); | ||
|
||
let (k, v) = m::next(&mut iter); | ||
std::debug::print(&k); | ||
std::debug::print(&string::utf8(v)); | ||
|
||
let (k, v) = m::next(&mut iter); | ||
std::debug::print(&k); | ||
std::debug::print(&string::utf8(v)); | ||
|
||
let iter = m::list_field_keys_with_name(&kv, string::utf8(b"test"), 2); | ||
std::debug::print(&iter); | ||
assert!(m::field_keys_len(&iter) == 2, 1003); | ||
|
||
let (k, v) = m::next(&mut iter); | ||
std::debug::print(&k); | ||
std::debug::print(&string::utf8(v)); | ||
|
||
let (k, v) = m::next(&mut iter); | ||
std::debug::print(&k); | ||
std::debug::print(&string::utf8(v)); | ||
|
||
let object_id = m::save_to_object_storage(kv); | ||
std::debug::print(&110120); | ||
std::debug::print(&object_id); | ||
} | ||
} | ||
|
||
//# run --signers test --args object:0x5f3e8d46295d27dd3b6d60c6f629e96cfb83a0aa4d75ec61a331d6ac0d4d1358 | ||
|
||
script { | ||
use std::string; | ||
use moveos_std::object::{Self, Object}; | ||
use test::m::{Self, KVStore}; | ||
|
||
fun main(kv_object: &Object<KVStore>) { | ||
let kv = object::borrow(kv_object); | ||
assert!(m::contains(kv, string::utf8(b"test")), 1000); | ||
|
||
let iter = m::list_field_keys(kv); | ||
assert!(m::field_keys_len(&iter) == 4, 1001); | ||
|
||
let size = m::length(kv); | ||
assert!(size == 4, 1002); | ||
|
||
let v = m::borrow(kv, string::utf8(b"test")); | ||
assert!(v == &b"value", 1003); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.