rooch-network / rooch

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

[Object] How to determine whether an ObjectID is a certain type of Object #962

Closed WGB5445 closed 4 days ago

WGB5445 commented 7 months ago

When we get an Object ID, there seems to be no way to get the relationship between the ID and the Object type.

Currently it can only be determined through borrow:

public fun assert_collection_exist_of_id(collectionID: ObjectID, ctx: &mut Context){
        assert!( context::exist_object(ctx, collectionID), ECollectionNotExist);
        context::borrow_object<Collection>(ctx,collectionID);
    }

We may need a function to determine whether the ID is of a certain type:

function signature:
public fun check<T>(ctx: &mut Context, objectid: ObjectID): bool 

use: 
   context::check<T>(ctx, ObjectID);
jolestar commented 7 months ago

exist_object only cares about the ID, and does not care about the Type.

We can make the exist_object care the Type, but

If object_id exists but not the type T, should exist_object<T>(object_id) return false?

baichuan3 commented 7 months ago

exist_object only cares about the ID, and does not care about the Type.

We can make the exist_object care the Type, but

If object_id exists but not the type T, should exist_object<T>(object_id) return false?

Would it be better to provide two functions that can be combined by user?

jolestar commented 7 months ago

Would it be better to provide two functions that can be combined by user?

How to name it? It is too complex for the user. We need to decide by the scenario.

  1. In the Table case, the user only cares about if the key exists and does not care about the value Type. Because the user will check if the key exists and then add the key-value pair. If the raw_table::contains(key) care about the value type, will cause an error.

  2. In the Object case, the ObjectID is generated via the system. The user can not construct the ObjectID, so the exist_object<T>(object_id) can care about the value type. But the raw_table implemention needs to refactor.

How about private a native function raw_table::borrow_option<T>(key):&Option<T>? and implement the contains function in Move.

pause125 commented 7 months ago

In raw_table native implementation, why we only use key layout to representing a Table, instead of both key and value layout?

The current table defination:

/// A structure representing a single table.
pub struct Table {
    handle: ObjectID,
    key_layout: MoveTypeLayout,
    content: BTreeMap<Vec<u8>, TableRuntimeValue>,
    size_increment: i64,
}

How about define table like this:

/// A structure representing a single table.
pub struct Table {
    handle: ObjectID,
    key_layout: MoveTypeLayout,
    value_layout: MoveTypeLayout,
    content: BTreeMap<Vec<u8>, TableRuntimeValue>,
    size_increment: i64,
}
jolestar commented 7 months ago

The key_layout is for serializing the Key to vec<u8>, and putting it in content: BTreeMap<Vec<u8>, TableRuntimeValue>. The value_layout in TableRuntimeValue.

jolestar commented 4 days ago

use exists_object_with_type<T: key>(object_id: ObjectID): bool

https://github.com/rooch-network/rooch/blob/85b9502e307cee0d7ecc3f51e2b7d07ea7da2e6b/frameworks/moveos-stdlib/sources/object.move#L235-L237