Skip to content

Commit

Permalink
Add ChildRef::is_child (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind committed Mar 14, 2022
1 parent 3a40bfc commit 90201f4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion sui_programmability/framework/sources/Collection.move
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ module Sui::Collection {
let len = size(c);
while (i < len) {
let child_ref = Vector::borrow(&c.objects, i);
if (Transfer::child_id(child_ref) == id) {
if (Transfer::is_child_unsafe(child_ref, id)) {
return Option::some(i)
};
i = i + 1;
Expand Down
20 changes: 13 additions & 7 deletions sui_programmability/framework/sources/Geniteam.move
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Sui::Geniteam {
use Sui::Bag::{Self, Bag};
use Sui::Collection::{Self, Collection};
use Sui::ID::{Self, VersionedID};
use Sui::ID::VersionedID;
use Sui::TxContext::{Self, TxContext};
use Std::Option::{Self, Option};
use Sui::Transfer::{Self, ChildRef};
Expand Down Expand Up @@ -135,8 +135,10 @@ module Sui::Geniteam {
);

// Check if this is the right collection
assert!(Transfer::child_id(&farm.pet_monsters) == ID::id(pet_monsters),
EMONSTER_COLLECTION_NOT_OWNED_BY_FARM);
assert!(
Transfer::is_child(&farm.pet_monsters, pet_monsters),
EMONSTER_COLLECTION_NOT_OWNED_BY_FARM,
);

// TODO: Decouple adding monster to farm from creating a monster.
// Add it to the collection
Expand All @@ -149,8 +151,10 @@ module Sui::Geniteam {
ctx: &mut TxContext
) {
// Check if this is the right collection
assert!(Transfer::child_id(&player.inventory) == ID::id(inventory),
EINVENTORY_NOT_OWNED_BY_PLAYER);
assert!(
Transfer::is_child(&player.inventory, inventory),
EINVENTORY_NOT_OWNED_BY_PLAYER,
);

// Create the farm cosmetic object
let farm_cosmetic = FarmCosmetic {
Expand All @@ -168,8 +172,10 @@ module Sui::Geniteam {
ctx: &mut TxContext
) {
// Check if this is the right collection
assert!(Transfer::child_id(&player.inventory) == ID::id(inventory),
EINVENTORY_NOT_OWNED_BY_PLAYER);
assert!(
Transfer::is_child(&player.inventory, inventory),
EINVENTORY_NOT_OWNED_BY_PLAYER,
);

// Create the farm cosmetic object
let monster_cosmetic = MonsterCosmetic {
Expand Down
19 changes: 16 additions & 3 deletions sui_programmability/framework/sources/Transfer.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Sui::Transfer {

// To allow access to transfer_to_object_unsafe.
friend Sui::Bag;
// To allow access to is_child_unsafe.
friend Sui::Collection;

// When transferring a child object, this error is thrown if the child object
// doesn't match the ChildRef that represents the onwershp.
Expand All @@ -20,9 +22,20 @@ module Sui::Transfer {
child_id: ID,
}

/// Getter for ChildRefs child id.
public fun child_id<T: key>(child_ref: &ChildRef<T>): &ID {
&child_ref.child_id
/// Check whether the `child` object is actually the child object
/// owned by the parent through the given `child_ref`.
public fun is_child<T: key>(child_ref: &ChildRef<T>, child: &T): bool {
&child_ref.child_id == ID::id(child)
}

/// Check whether the `child_ref`'s child_id is `id`.
/// This is less safe compared to `is_child` because we won't be able to check
/// whether the type of the child object is the same as what `ChildRef` represents.
/// We should always call `is_child` whenever we can.
/// This is currently only exposed to friend classes. If there turns out to be
/// general needs, we can open it up.
public(friend) fun is_child_unsafe<T: key>(child_ref: &ChildRef<T>, id: &ID): bool {
&child_ref.child_id == id
}

/// Transfers are implemented by emitting a
Expand Down

1 comment on commit 90201f4

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bench results

�[0m�[0m�[1m�[32m Finished�[0m release [optimized] target(s) in 0.27s
�[0m�[0m�[1m�[32m Running�[0m target/release/bench
�[2m2022-03-14T01:27:28.982148Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Starting benchmark: TransactionsAndCerts
�[2m2022-03-14T01:27:28.982179Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Preparing accounts.
�[2m2022-03-14T01:27:28.982669Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Open database on path: "/tmp/DB_853F8022E4F6D430CCEBA59514698F3F5133200C"
�[2m2022-03-14T01:27:34.146444Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Preparing transactions.
�[2m2022-03-14T01:27:42.737276Z�[0m �[32m INFO�[0m �[2msui_network::transport�[0m�[2m:�[0m Listening to TCP traffic on 127.0.0.1:9555
�[2m2022-03-14T01:27:43.739170Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Number of TCP connections: 2
�[2m2022-03-14T01:27:43.739202Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Set max_in_flight to 500
�[2m2022-03-14T01:27:43.739206Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Sending requests.
�[2m2022-03-14T01:27:43.745139Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m Sending TCP requests to 127.0.0.1:9555
�[2m2022-03-14T01:27:43.751201Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m Sending TCP requests to 127.0.0.1:9555
�[2m2022-03-14T01:27:44.635413Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 5000 packets
�[2m2022-03-14T01:27:45.415401Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 35000
�[2m2022-03-14T01:27:45.475696Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 35000
�[2m2022-03-14T01:27:45.514114Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 10000 packets
�[2m2022-03-14T01:27:46.395450Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 15000 packets
�[2m2022-03-14T01:27:47.164250Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 30000
�[2m2022-03-14T01:27:47.232403Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 30000
�[2m2022-03-14T01:27:47.288255Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 20000 packets
�[2m2022-03-14T01:27:48.172683Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 25000 packets
�[2m2022-03-14T01:27:48.908935Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 25000
�[2m2022-03-14T01:27:49.037258Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 25000
�[2m2022-03-14T01:27:49.057232Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 30000 packets
�[2m2022-03-14T01:27:49.943634Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 35000 packets
�[2m2022-03-14T01:27:50.716462Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 20000
�[2m2022-03-14T01:27:50.796129Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 20000
�[2m2022-03-14T01:27:50.834670Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 40000 packets
�[2m2022-03-14T01:27:51.741322Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 45000 packets
�[2m2022-03-14T01:27:52.507984Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 15000
�[2m2022-03-14T01:27:52.630907Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 15000
�[2m2022-03-14T01:27:52.642401Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 50000 packets
�[2m2022-03-14T01:27:53.534755Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 55000 packets
�[2m2022-03-14T01:27:54.319350Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 10000
�[2m2022-03-14T01:27:54.406905Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 10000
�[2m2022-03-14T01:27:54.430167Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 60000 packets
�[2m2022-03-14T01:27:55.325474Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 65000 packets
�[2m2022-03-14T01:27:56.089539Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 5000
�[2m2022-03-14T01:27:56.180548Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 5000
�[2m2022-03-14T01:27:56.220843Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 70000 packets
�[2m2022-03-14T01:27:57.114665Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 75000 packets
�[2m2022-03-14T01:27:58.001979Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m Done sending TCP requests to 127.0.0.1:9555
�[2m2022-03-14T01:27:58.064989Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 80000 packets
�[2m2022-03-14T01:27:58.114147Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m Done sending TCP requests to 127.0.0.1:9555
�[2m2022-03-14T01:27:58.114470Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Received 80000 responses.
�[2m2022-03-14T01:27:58.302608Z�[0m �[33m WARN�[0m �[2mbench�[0m�[2m:�[0m Completed benchmark for TransactionsAndCerts
Total time: 14375251us, items: 40000, tx/sec: 2782.5601097330405

Please sign in to comment.