sui icon indicating copy to clipboard operation
sui copied to clipboard

[sui/move] support dynamic access of child objects

Open sblackshear opened this issue 3 years ago • 1 comments

This is a step we have discussed that will significantly improve the expressiveness of Sui Move by allowing the implementation of collections that require pointer-like structures. The basic idea is:

  • Remove the requirement to supply child objects as explicit transaction inputs
  • Expose operators for dynamically accessing child objects given a reference to the parent. Children might be accessed by a "key" value (e.g., remove_child_by_key<K: copy + drop, P: key, C: key>(parent: &mut P, key: &K): C), a type (e.g., remove_child_by_type<K, P: key, C: key(parent: &mut P): C), by id (e.g., remove_child_by_id<P: key, C: key>(parent: &mut P, id: ID): C, or all of the above.
  • The parent/child relationship now looks like a pointer, and design patterns/collection types that require this can be build accordingly

sblackshear avatar Aug 22 '22 02:08 sblackshear

  • gaining consensus around lamport timestamps
  • Todd to bring up remaining decision points in next weeks design meeting

bholc646 avatar Sep 08 '22 18:09 bholc646

How long will it take to complete this feature

xander988 avatar Sep 26 '22 02:09 xander988

If completed, can an object own infinitely many child objects, like Key-Value map? (What I mean by "Infinitely" is only in theoretical term.)

0xys avatar Sep 26 '22 15:09 0xys

If completed, can an object own infinitely many child objects, like Key-Value map? (What I mean by "Infinitely" is only in theoretical term.)

Yes! It can have an unbounded number

sblackshear avatar Sep 29 '22 16:09 sblackshear

How long will it take to complete this feature

This is a question for @tnowacki, but I think sometime in October

sblackshear avatar Sep 29 '22 16:09 sblackshear

How long will it take to complete this feature

This is a question for @tnowacki, but I think sometime in October

@tnowacki Can you speed it up

xander988 avatar Oct 02 '22 01:10 xander988

Removing child objects by type would be useful for the following use case:

// package1

struct Foo has key {
  id: UID,
  children: vector<ID>,
}

struct Bar<phantom FT> has key {
  id: UID,
  amount: Balance<FT>,
}

public fun pop_child_any_mint<FT>(parent: &mut Foo): Balance<FT> {
  let Bar { id, amount } = remove_child_by_type<FT>(parent);
  object::delete(id);
  amount
}

// package2 

// custom royalty collection in USDC and SUI
public fun custom_royalty_collection(foo: Foo) {
  let ft = package1::pop_child_any_mint<USDC>(&mut foo);
  let s = package1::pop_child_any_mint<SUI>(&mut foo);
  // ...
}

Ie. I know the type I want to get in a specific implementation, but want to create a generic interface for doing that for any type.

Not sure how multiple children of the same type should be handled though. Return a vec of all, or first one at random, or ...?

porkbrain avatar Oct 11 '22 17:10 porkbrain

It would be ideal to also have native APIs to check whether a parent object owns a child object, and other helper methods as well. e.g.contains_child_by_id<P: key>(parent: &P, id: &ID): bool Are there APIs to show all objects the parents owns? If not then a vector of IDs need to be kept to keep track of which objects the parent owns.

JohnChangUK avatar Oct 13 '22 04:10 JohnChangUK

It would be ideal to also have native APIs to check whether a parent object owns a child object, and other helper methods as well. e.g.contains_child_by_id<P: key>(parent: &P, id: &ID): bool Are there APIs to show all objects the parents owns? If not then a vector of IDs need to be kept to keep track of which objects the parent owns.

has_child_object is being included as a method:

https://github.com/MystenLabs/sui/pull/5169/files

PaulFidika avatar Oct 13 '22 04:10 PaulFidika