move-language / move

Apache License 2.0
2.24k stars 677 forks source link

[Feature Request] Introduce verifier annotation, such as private_generics #1043

Open jolestar opened 1 year ago

jolestar commented 1 year ago

πŸš€ Feature Request

Motivation

In Move, the verifier ensures that the generic parameters of the global storage instructions must be defined in the caller's module.

Such as

move_to<T>|move_from<T>|borrow_global<T>|borrow_global_mut<T>

The type T must be defined in the caller's current module.

The move developer also needs this feature to restrict the function's generic type. Still, this feature is hardcode in the verifier, and the developer can not use this feature for their function.

So, if we introduce some verifier annotation, we can provide this feature to the developer.

Pitch

Describe the solution you'd like

  1. Introduce a verifier annotation, such as #[private_generics(T)], indicating the function's generic parameter T is private, which requires the same restriction to T as global storage instructions.
  2. Implement a verifier extension for the annotation, like sui-verifier's private_generics.

UseCase

  1. TypeTable

140 The TypeTable also requires this feature to ensure Type Safety.

  1. Provide bcs::from_bytes
module std::bcs{
  #[private_generics(T)]
  public fun from_bytes<T>(bytes: vector<u8>): T
}

As described in the issue(https://github.com/rooch-network/rooch/issues/145) mentioned, relying on private_generics alone cannot guarantee type safety. We need two other annotations,#[self_struct] and #[require_self_struct(T)]. The name for the annotations is yet to be determined.

module std::bcs{

  #[private_generics(T), require_self_struct(T)]
  public fun from_bytes<T>(bytes: vector<u8>): T
}
module example::my_module{
   #[self_struct]
   struct MyStruct {
      f_number: u64,
      f_struct: StructB,
   }

   struct StructB {
      f: u64,
   }

   public fun decode(bytes: vector<u8>): MyStruct{
      std::bcs::from_bytes<MyStruct>(bytes)
   }
}

Are you willing to open a pull request? (See CONTRIBUTING)

Yes

Additional context

jolestar commented 1 year ago

Similar features have also been implemented on Aptos, [#view]. We can make this feature like a verifier extension.

https://github.com/aptos-labs/aptos-core/pull/5875