Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

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

Open
jolestar opened this issue Apr 26, 2023 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@jolestar
Copy link
Collaborator

jolestar commented Apr 26, 2023

🚀 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(rooch-network/rooch#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.

  • #[self_struct]: A struct with this annotation indicates that all its fields are either of primitive type or defined in the current module.
  • #[require_self_struct(T)]: A function with annotation indicates that the generic type T must have #[self_struct] annotation.
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 jolestar added the enhancement New feature or request label Apr 26, 2023
@jolestar
Copy link
Collaborator Author

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

aptos-labs/aptos-core#5875

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant