https://github.com/sui-foundation/sui-move-intro-course/tree/main/unit-two/lessons
文档不太全,目前是个人理解,下方描述了一些需要理清的关键问题
有key ability的struct被称为Sui object,其第一个成员id: UID
是object的唯一标识符,链上存储与查找数据均使用UID。销毁struct并不会销毁UID(object本身)。创建object(UID)使用object::new
,销毁object使用object::delete(id)
。
wrapped:指的是一个object被存放在另一个object内部,这需要被存放的object有store ability。存放发生后,被存放object实际上会被隐藏(不再能用其UID检索到该object),只有wrapping object被销毁了才可以重新取出该object,该object的可见性就恢复了,且前后UID不会发生变化(即还是同一个object)
文档中描述了许多种object,包括shared、**Address-owned,是否shared只影响谁可以使用这个object(读/写均只有owner可以进行)。**无论权限如何,某种类型的struct只能由定义它的module创建(create/pack)、销毁(destroy/unpack)或修改。
没有key ability的类型是如何被处理的?相比于有key的有哪些限制?:不能被存储,只能在当次transaction中创建后也必须在本次transaction中被销毁
在flash loan中见到了key, store, drop均无的struct,获得它以后必须在本次transaction中调用创建它的module内的函数来销毁它,这种pattern被称为Hot Potato(烫手山芋)。
有drop ability的struct可在离开作用域后被自动销毁,不需要创建它的module显式unpack
UID和ID有何区别?
/// An object ID. This is used to reference Sui Objects.
/// This is *not* guaranteed to be globally unique--anyone can create an `ID` from a `UID` or
/// from an object, and ID's can be freely copied and dropped.
/// Here, the values are not globally unique because there can be multiple values of type `ID`
/// with the same underlying bytes. For example, `object::id(&obj)` can be called as many times
/// as you want for a given `obj`, and each `ID` value will be identical.
struct ID has copy, drop, store {
bytes: address
}
/// Globally unique IDs that define an object's ID in storage. Any Sui Object, that is a struct
/// with the `key` ability, must have `id: UID` as its first field.
/// These are globally unique in the sense that no two values of type `UID` are ever equal, in
/// other words for any two values `id1: UID` and `id2: UID`, `id1` != `id2`.
/// This is a privileged type that can only be derived from a `TxContext`.
/// `UID` doesn't have the `drop` ability, so deleting a `UID` requires a call to `delete`.
struct UID has store {
id: ID,
}
https://github.com/MystenLabs/sui/blob/testnet-v1.16.1/crates/sui-framework/packages/sui-framework/sources/object.move
//! Objects whose struct type has key ability represent Sui objects.
//! They have unique IDs that should never be reused. This verifier makes
//! sure that the id field of Sui objects never get leaked.
//! Unpack is the only bytecode that could extract the id field out of
//! a Sui object. From there, we track the flow of the value and make
//! sure it can never get leaked outside of the function. There are four
//! ways it can happen:
//! 1. Returned
//! 2. Written into a mutable reference
//! 3. Added to a vector
//! 4. Passed to a function cal::;
https://github.com/MystenLabs/sui/blob/testnet-v1.16.1/sui-execution/latest/sui-verifier/src/id_leak_verifier.rs
transfer:只有定义该object的module可以调用sui::transfer::transfer
;有store ability的object方可被其他module调用sui::transfer::public_transfer