This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 688
[move-stdlib] add struct decomposition via new struct_tag
module
#971
Open
bytedeveloperr
wants to merge
3
commits into
move-language:main
Choose a base branch
from
cozy-creator:struct-tag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
|
||
<a name="0x1_struct_tag"></a> | ||
|
||
# Module `0x1::struct_tag` | ||
|
||
Module to decompose a move struct into it's components. | ||
|
||
|
||
- [Struct `StructTag`](#0x1_struct_tag_StructTag) | ||
- [Function `get`](#0x1_struct_tag_get) | ||
- [Function `into`](#0x1_struct_tag_into) | ||
- [Function `module_authority`](#0x1_struct_tag_module_authority) | ||
|
||
|
||
<pre><code><b>use</b> <a href="ascii.md#0x1_ascii">0x1::ascii</a>; | ||
</code></pre> | ||
|
||
|
||
|
||
<a name="0x1_struct_tag_StructTag"></a> | ||
|
||
## Struct `StructTag` | ||
|
||
|
||
|
||
<pre><code><b>struct</b> <a href="struct_tag.md#0x1_struct_tag_StructTag">StructTag</a> <b>has</b> <b>copy</b>, drop, store | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Fields</summary> | ||
|
||
|
||
<dl> | ||
<dt> | ||
<code>address_: <b>address</b></code> | ||
</dt> | ||
<dd> | ||
Address of the entity that the struct belongs to. | ||
taking <code>00000000000000000000000000000001::option::Option<u64></code> for example, | ||
the address will be <code>00000000000000000000000000000001</code> | ||
</dd> | ||
<dt> | ||
<code>module_name: <a href="ascii.md#0x1_ascii_String">ascii::String</a></code> | ||
</dt> | ||
<dd> | ||
the name of the module where the struct is defined. | ||
using the example struct above the module name should be <code><a href="option.md#0x1_option">option</a></code> | ||
</dd> | ||
<dt> | ||
<code>struct_name: <a href="ascii.md#0x1_ascii_String">ascii::String</a></code> | ||
</dt> | ||
<dd> | ||
the name of the struct itself. | ||
using the example struct above the module name should be <code>Option</code> | ||
</dd> | ||
<dt> | ||
<code>generics: <a href="vector.md#0x1_vector">vector</a><<a href="ascii.md#0x1_ascii_String">ascii::String</a>></code> | ||
</dt> | ||
<dd> | ||
the generics or tyepe params of the struct. | ||
using the example struct above the module name should be <code><a href="vector.md#0x1_vector">vector</a>["u64"]</code> | ||
</dd> | ||
</dl> | ||
|
||
|
||
</details> | ||
|
||
<a name="0x1_struct_tag_get"></a> | ||
|
||
## Function `get` | ||
|
||
Returns the tag of the struct of type <code>T</code> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="struct_tag.md#0x1_struct_tag_get">get</a><T>(): <a href="struct_tag.md#0x1_struct_tag_StructTag">struct_tag::StructTag</a> | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="struct_tag.md#0x1_struct_tag_get">get</a><T>(): <a href="struct_tag.md#0x1_struct_tag_StructTag">StructTag</a>; | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
<a name="0x1_struct_tag_into"></a> | ||
|
||
## Function `into` | ||
|
||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="struct_tag.md#0x1_struct_tag_into">into</a>(self: &<a href="struct_tag.md#0x1_struct_tag_StructTag">struct_tag::StructTag</a>): (<b>address</b>, <a href="ascii.md#0x1_ascii_String">ascii::String</a>, <a href="ascii.md#0x1_ascii_String">ascii::String</a>, <a href="vector.md#0x1_vector">vector</a><<a href="ascii.md#0x1_ascii_String">ascii::String</a>>) | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="struct_tag.md#0x1_struct_tag_into">into</a>(self: &<a href="struct_tag.md#0x1_struct_tag_StructTag">StructTag</a>): (<b>address</b>, String, String, <a href="vector.md#0x1_vector">vector</a><String>) { | ||
(self.address_, self.module_name, self.struct_name, self.generics) | ||
} | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
<a name="0x1_struct_tag_module_authority"></a> | ||
|
||
## Function `module_authority` | ||
|
||
Returns the module authority for the struct of type <code>T</code> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="struct_tag.md#0x1_struct_tag_module_authority">module_authority</a><T>(): <a href="struct_tag.md#0x1_struct_tag_StructTag">struct_tag::StructTag</a> | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="struct_tag.md#0x1_struct_tag_module_authority">module_authority</a><T>(): <a href="struct_tag.md#0x1_struct_tag_StructTag">StructTag</a> { | ||
<b>let</b> <a href="struct_tag.md#0x1_struct_tag_StructTag">StructTag</a> { | ||
address_, | ||
module_name, | ||
struct_name: _, | ||
generics: _ | ||
} = <a href="struct_tag.md#0x1_struct_tag_get">get</a><T>(); | ||
|
||
<a href="struct_tag.md#0x1_struct_tag_StructTag">StructTag</a> { | ||
address_, | ||
module_name, | ||
struct_name: <a href="ascii.md#0x1_ascii_string">ascii::string</a>(b"Witness"), | ||
generics: <a href="vector.md#0x1_vector">vector</a>[] | ||
} | ||
} | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
|
||
[//]: # ("File containing references which can be used from documentation") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/// Module to decompose a move struct into it's components. | ||
module std::struct_tag { | ||
use std::ascii::{Self, String}; | ||
|
||
struct StructTag has copy, store, drop { | ||
/// Address of the entity that the struct belongs to. | ||
/// taking `00000000000000000000000000000001::option::Option<u64>` for example, | ||
/// the address will be `00000000000000000000000000000001` | ||
address_: address, | ||
/// the name of the module where the struct is defined. | ||
/// using the example struct above the module name should be `option` | ||
module_name: String, | ||
/// the name of the struct itself. | ||
/// using the example struct above the module name should be `Option` | ||
struct_name: String, | ||
/// the generics or tyepe params of the struct. | ||
/// using the example struct above the module name should be `vector["u64"]` | ||
generics: vector<String> | ||
} | ||
|
||
/// Returns the tag of the struct of type `T` | ||
public native fun get<T>(): StructTag; | ||
|
||
// Converts `self` into a tuple of it's inner values | ||
public fun into(self: &StructTag): (address, String, String, vector<String>) { | ||
(self.address_, self.module_name, self.struct_name, self.generics) | ||
} | ||
|
||
/// Returns the module authority for the struct of type `T` | ||
public fun module_authority<T>(): StructTag { | ||
let StructTag { | ||
address_, | ||
module_name, | ||
struct_name: _, | ||
generics: _ | ||
} = get<T>(); | ||
|
||
StructTag { | ||
address_, | ||
module_name, | ||
struct_name: ascii::string(b"Witness"), | ||
generics: vector[] | ||
} | ||
} | ||
|
||
#[test_only] | ||
public fun new_for_testing(address_: address, module_name: String, struct_name: String, generics: vector<String>): StructTag { | ||
StructTag { | ||
address_, | ||
module_name, | ||
struct_name, | ||
generics | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (c) The Move Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use move_binary_format::errors::PartialVMResult; | ||
use move_core_types::{ | ||
gas_algebra::{InternalGas, InternalGasPerByte, NumBytes}, | ||
language_storage::TypeTag, | ||
}; | ||
use move_vm_runtime::native_functions::{NativeContext, NativeFunction}; | ||
use move_vm_types::{ | ||
loaded_data::runtime_types::Type, | ||
natives::function::NativeResult, | ||
values::{Struct, Value}, | ||
}; | ||
|
||
use smallvec::smallvec; | ||
use std::{collections::VecDeque, sync::Arc}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct GetGasParameters { | ||
pub base: InternalGas, | ||
pub per_byte: InternalGasPerByte, | ||
} | ||
|
||
fn native_get( | ||
gas_params: &GetGasParameters, | ||
context: &mut NativeContext, | ||
ty_args: Vec<Type>, | ||
arguments: VecDeque<Value>, | ||
) -> PartialVMResult<NativeResult> { | ||
debug_assert!(ty_args.len() == 1); | ||
debug_assert!(arguments.is_empty()); | ||
|
||
let type_tag = context.type_to_type_tag(&ty_args[0])?; | ||
let type_name = type_tag.to_canonical_string(); | ||
|
||
let mut cost = gas_params.base; | ||
|
||
if let TypeTag::Struct(tag) = type_tag { | ||
let address = Value::address(tag.address); | ||
|
||
// make std::ascii::String for the module name | ||
let module = Value::struct_(Struct::pack(vec![Value::vector_u8( | ||
tag.module.into_bytes(), | ||
)])); | ||
|
||
// make std::ascii::String for the struct name | ||
let name = Value::struct_(Struct::pack(vec![Value::vector_u8(tag.name.into_bytes())])); | ||
|
||
// make a vector of std::ascii::String for the generics | ||
let generics_vec = tag | ||
.type_params | ||
.iter() | ||
.map(|ty| { | ||
Value::struct_(Struct::pack(vec![Value::vector_u8( | ||
ty.to_canonical_string().into_bytes(), | ||
)])) | ||
}) | ||
.collect::<Vec<Value>>(); | ||
|
||
// convert the generics vector into move supported value. | ||
// using the `vector_for_testing_only` which can break as it's currently the easiest way to do this without altering the existing `Value` struct. | ||
// it should the replaced when the proper API is ready. | ||
let generics = Value::vector_for_testing_only(generics_vec); | ||
|
||
cost += gas_params.per_byte * NumBytes::new(type_name.len() as u64); | ||
|
||
Ok(NativeResult::ok( | ||
cost, | ||
smallvec![Value::struct_(Struct::pack(vec![ | ||
address, module, name, generics | ||
]))], | ||
)) | ||
} else { | ||
Ok(NativeResult::err(cost, 0)) | ||
} | ||
} | ||
|
||
pub fn make_native_get(gas_params: GetGasParameters) -> NativeFunction { | ||
Arc::new(move |context, ty_args, args| native_get(&gas_params, context, ty_args, args)) | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct GasParameters { | ||
pub get: GetGasParameters, | ||
} | ||
|
||
pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, NativeFunction)> { | ||
let natives = [("get", make_native_get(gas_params.get))]; | ||
|
||
crate::natives::helpers::make_module_natives(natives) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How to understand the module authority? I see the struct_name is hardcode
Witness
, so is it a programming pattern?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah exactly! My intention is that every module should be able to assert their ownership in this way. For example you could do something like:
This is stating that, in order for this function to continue, we need a witness, and that Witness must be the module-authority witness produced by the same module that produced the Object.
This way we can guarantee that this function-call originated from the same module that defined Object in the first place. So even if you obtain a reference to &Object, you won't be able to use it without the defining-module's permission (from a Witness).