rooch-network / rooch

VApp Container with Move Language
https://rooch.network
Apache License 2.0
128 stars 54 forks source link

[object] A draft implementation of box style object #48

Closed jolestar closed 1 year ago

jolestar commented 1 year ago

Box Style Move Object

This PR is part of #21

In the Sui Move, Object is a Move struct which has some additional requirements:

  1. The struct must have the key ability.
  2. The struct's first field must be UID type.

In this PR, I try to introduce a Box-style Move Object. The Object is like a Box and has an ID field. We can put a Move resource to the Box and save it to the Object Store.

struct Object<T: store> {
        id: UID,
        value: T,
}

public fun new<T: store>(ctx: &mut TxContext, value: T): Object<T>{
    let id = UID {
        id: ID { bytes: tx_context::new_object(ctx) },
    };
    Object<T>{id, value}
}

public fun borrow_value<T: store>(this: &Object<T>): &T{
    &this.value
}

public fun borrow_value_mut<T: store>(this: &mut Object<T>): &mut T{
    &mut this.value
}

public fun transfer<T: store>(obj: Object<T>, recipient: address);

The ObjectRef:

struct ObjectRef< phantom T: store> has store, drop{
    id:  ID,
}

public fun as_object_ref<T: store>(this: &Object<T>): ObjectRef<T>{
    ObjectRef<T>{
        id: this.id.id,
    }
}

public fun borrow<T: store>(this: &ObjectRef<T>): &Object<T>{
        &borrow_object(this.id)
}

native fun borrow_object<T: store>(id: ID): &Object<T>;

Pros

  1. Box-style Move Object can be an extension to the Move VM like the Table extension, which does not require modifying the bytecode verifier.

Cons

  1. Developer can not directly define an Object type. (Article vs Object<Article>).

TODO

  1. Implement the ObjectRef(this will implement in the next PR)
jolestar commented 1 year ago

I remove the ObjectRef, please help review @baichuan3 @pause125

baichuan3 commented 1 year ago

I remove the ObjectRef, please help review @baichuan3 @pause125

Is object ref implemented later, or does this feature no longer exist?

jolestar commented 1 year ago

I remove the ObjectRef, please help review @baichuan3 @pause125

Is object ref implemented later, or does this feature no longer exist?

The ObjectRef design #51

baichuan3 commented 1 year ago

wait for further implementation of object and then continuing the review

jolestar commented 1 year ago
  1. implements raw_table (formerly any_table)
  2. implements Table, TypeTable(#19), ObjectStorage based on raw_table
  3. Implemented AccountStorage based on TypeTable, Table, and ObjectStorage, and implemented alternative methods for instructions such as move_to|move_from|borrow_global in AccountStorage. #20
  4. Unify the global storage command and table with raw_table.
baichuan3 commented 1 year ago

LGTM

jolestar commented 1 year ago

TODO:

  1. Do some cleanup
  2. Disable move_to|move_from|borrow_global or use same GlobalValue with table.
  3. Fix https://github.com/rooch-network/rooch/blob/bbc1b71d8b04253d2cd0a765c787b03849da721d/moveos/moveos-stdlib/tests/cases/account/basic.move#L12