diff --git a/docs/website/pages/build/rooch-framework/_meta.en-US.json b/docs/website/pages/build/rooch-framework/_meta.en-US.json index 03338ce6e5..b70eaf3072 100644 --- a/docs/website/pages/build/rooch-framework/_meta.en-US.json +++ b/docs/website/pages/build/rooch-framework/_meta.en-US.json @@ -6,5 +6,6 @@ "cryptographic-primitives": "Cryptographic primitives", "timestamp": "Timestamp", "private-generics": "Private generics", + "data-struct": "Data Struct", "unit-test": "Unit test" } diff --git a/docs/website/pages/build/rooch-framework/_meta.zh-CN.json b/docs/website/pages/build/rooch-framework/_meta.zh-CN.json index 067ae7be78..7c88ab815b 100644 --- a/docs/website/pages/build/rooch-framework/_meta.zh-CN.json +++ b/docs/website/pages/build/rooch-framework/_meta.zh-CN.json @@ -6,5 +6,6 @@ "cryptographic-primitives": "密码原语", "timestamp": "时间戳", "private-generics": "私有泛型", + "data-struct": "数据结构体", "unit-test": "单元测试" } diff --git a/docs/website/pages/build/rooch-framework/data-struct.en-US.mdx b/docs/website/pages/build/rooch-framework/data-struct.en-US.mdx new file mode 100644 index 0000000000..a37d5f69a4 --- /dev/null +++ b/docs/website/pages/build/rooch-framework/data-struct.en-US.mdx @@ -0,0 +1,44 @@ +# Data Struct + +import { Callout, FileTree } from 'nextra/components' + +`#[data_struct]` is a struct annotation used to mark a struct as a pure data struct, allowing it to be directly deserialized within a contract. This feature is designed to facilitate developers in retrieving data from external data sources. + +## Example + +```move +module my_project::my_module { + #[data_struct] + struct MyData has copy, drop { + value: u64, + name: vector, + } +} +``` + +With this annotation, developers can directly deserialize the `MyData` struct within a contract: + +```move +let data: MyData = moveos_std::bcs::from_bytes(bytes); +``` + +The `moveos_std::bcs::from_bytes` function also uses the `#[data_struct(T)]` annotation to ensure that `T` must be a `#[data_struct]` type. + +```move +module moveos_std::bcs { + #[data_struct(T)] + /// Function to deserialize a type T. + /// The `data_struct` annotation ensures that `T` must be a `#[data_struct]` type. + public fun from_bytes(bytes: vector): T; +} +``` + +## How It Works + +The `#[data_struct]` annotation is implemented based on the Move verifier, which checks the struct definition at compile time to ensure it meets the `data_struct` requirements. Additionally, the contract is re-verified during deployment. + +## Data Struct Specification + + +TODO: This part of the document needs improvement. + \ No newline at end of file diff --git a/docs/website/pages/build/rooch-framework/data-struct.zh-CN.mdx b/docs/website/pages/build/rooch-framework/data-struct.zh-CN.mdx new file mode 100644 index 0000000000..b10b84a6e7 --- /dev/null +++ b/docs/website/pages/build/rooch-framework/data-struct.zh-CN.mdx @@ -0,0 +1,44 @@ +# 数据结构体 + +import { Callout, FileTree } from 'nextra/components' + +`#[data_struct]` 是一个结构体注解,用于标记结构体为纯数据结构体,使其可以在合约中直接反序列化。这个特性旨在方便开发者从外部数据源获取数据。 + +## 示例 + +```move +module my_project::my_module { + #[data_struct] + struct MyData has copy, drop { + value: u64, + name: vector, + } +} +``` + +这样,开发者就可以在合约中直接反序列化 `MyData` 结构体: + +```move +let data: MyData = moveos_std::bcs::from_bytes(bytes); +``` + +`moveos_std::bcs::from_bytes` 函数也使用了 `#[data_struct(T)]` 注解,确保 `T` 必须是一个 `#[data_struct]` 类型。 + +```move +module moveos_std::bcs { + #[data_struct(T)] + /// 反序列化类型 T 的函数。 + /// `data_struct` 注解确保 `T` 必须是一个 `#[data_struct]` 类型。 + public fun from_bytes(bytes: vector): T; +} +``` + +## 工作原理 + +`#[data_struct]` 基于 Move 的验证器实现,在编译时检查结构体的定义是否符合 `data_struct` 的要求。同时,在合约部署时也会进行再次验证。 + +## 数据结构体规范 + + + TODO 这部分文档需要改进 + \ No newline at end of file diff --git a/docs/website/pages/build/rooch-framework/private-generics.en-US.mdx b/docs/website/pages/build/rooch-framework/private-generics.en-US.mdx index 0f64d01b0f..d414bedd72 100644 --- a/docs/website/pages/build/rooch-framework/private-generics.en-US.mdx +++ b/docs/website/pages/build/rooch-framework/private-generics.en-US.mdx @@ -6,18 +6,20 @@ The `#[private_generics(T)]` function annotation ensures that functions annotate This annotation is particularly useful for developing foundational contract libraries. It ensures that functions with this annotation cannot be called arbitrarily by users and can only be called through higher-level contract functions. -Let's take `AccountStorage` as an example. We want to store a user's resources within their storage space, but at the same time, we want to restrict a contract to only operate on the resource types it defines and prevent it from operating on resource types defined by other contracts. This is where the `#[private_generics(T)]` annotation comes into play. +Let's take `Account` as an example. We want to store a user's resources within their storage space, but at the same time, we want to restrict a contract to only operate on the resource types it defines and prevent it from operating on resource types defined by other contracts. This is where the `#[private_generics(T)]` annotation comes into play. ```move -module moveos_std::account_storage{ +module moveos_std::account{ #[private_generics(T)] - public fun borrow_resource(ctx: &Context, account: address): &T; + /// Borrow a mut resource from the account's storage + /// This function equates to `borrow_global_mut(address)` instruction in Move + public fun borrow_mut_resource(account: address): &mut T; } ``` -In the above example, we define a `borrow_resource` function with a private generic `T` which can only be used within the module where `T` is defined. Other modules cannot use this function. +In the above example, we define a `borrow_mut_resource` function with a private generic `T` which can only be used within the module where `T` is defined. Other modules cannot use this function. -### How it works +## How it works `#[private_generics(T)]` is implemented based on Move's verifier, which checks the validity of function calls at compile-time. If an incorrect call is detected, it will result in an error. Additionally, the validity of function calls is checked during contract deployment. @@ -28,8 +30,7 @@ TODO: This part of the document needs improvement. ### References 1. The `#[private_generics(T)]` annotation is used in the following modules, which can serve as references: - - [moveos_std::account_storage](https://github.com/rooch-network/rooch/blob/main/frameworks/moveos-stdlib/sources/account_storage.move) - - [moveos_std::storage_context](https://github.com/rooch-network/rooch/blob/main/frameworks/moveos-stdlib/sources/storage_context.move) + - [moveos_std::account](https://github.com/rooch-network/rooch/blob/main/frameworks/moveos-stdlib/sources/account.move) - [moveos_std::signer](https://github.com/rooch-network/rooch/blob/main/frameworks/moveos-stdlib/sources/signer.move) - [rooch_framework::coin](https://github.com/rooch-network/rooch/blob/main/frameworks/rooch-framework/sources/coin.move) 2. [Understanding Private Generics Functions in one article](../../blog/read-private-generics-in-one-article) diff --git a/docs/website/pages/build/rooch-framework/private-generics.zh-CN.mdx b/docs/website/pages/build/rooch-framework/private-generics.zh-CN.mdx index cb3194c56a..41a5bf74b2 100644 --- a/docs/website/pages/build/rooch-framework/private-generics.zh-CN.mdx +++ b/docs/website/pages/build/rooch-framework/private-generics.zh-CN.mdx @@ -6,18 +6,20 @@ import { Callout, FileTree } from 'nextra/components' 这个注解对开发基础合约库非常有用,因为它可以保证添加了该注解的函数不能被用户随意调用,只能通过上层合约封装的函数进行调用。 -以 `AccountStorage` 为例,我们希望将用户的`资源(Resource)`都保存在用户的存储空间内,但又要求某个合约只能操作自己定义的资源类型,不能操作其他合约定义的资源类型,这时候就可以使用 `#[private_generics(T)]` 注解来实现。 +以 `Account` 为例,我们希望将用户的`资源(Resource)`都保存在用户的存储空间内,但又要求某个合约只能操作自己定义的资源类型,不能操作其他合约定义的资源类型,这时候就可以使用 `#[private_generics(T)]` 注解来实现。 ```move -module moveos_std::account_storage{ +module moveos_std::account{ #[private_generics(T)] - public fun borrow_resource(ctx: &StorageContext, account: address): &T; + /// Borrow a mut resource from the account's storage + /// This function equates to `borrow_global_mut(address)` instruction in Move + public fun borrow_mut_resource(account: address): &mut T; } ``` -在上面的例子中,我们定义了一个 `borrow_resource` 函数,它的泛型 `T` 是私有的,只能在 `T` 所在的模块内使用,其他模块无法使用该函数。 +在上面的例子中,我们定义了一个 `borrow_mut_resource` 函数,它的泛型 `T` 是私有的,只能在 `T` 所在的模块内使用,其他模块无法使用该函数。 -### 它是如何工作的 +## 工作原理 `#[private_generics(T)]` 是基于 Move 的 verifier 实现的,它会在编译时检查函数调用的合法性,如果发现错误的调用,就会报错。同时,合约部署的时候也会检查函数调用的合法性。 @@ -28,8 +30,7 @@ module moveos_std::account_storage{ ### 参考链接 1. 以下模块中使用了 `#[private_generics(T)]` 注解,可以作为参考: - - [moveos_std::account_storage](https://github.com/rooch-network/rooch/blob/main/frameworks/moveos-stdlib/sources/account_storage.move) - - [moveos_std::storage_context](https://github.com/rooch-network/rooch/blob/main/frameworks/moveos-stdlib/sources/storage_context.move) + - [moveos_std::account](https://github.com/rooch-network/rooch/blob/main/frameworks/moveos-stdlib/sources/account.move) - [moveos_std::signer](https://github.com/rooch-network/rooch/blob/main/frameworks/moveos-stdlib/sources/signer.move) - [rooch_framework::coin](https://github.com/rooch-network/rooch/blob/main/frameworks/rooch-framework/sources/coin.move) 2. [一文读懂私有泛型函数](../../blog/read-private-generics-in-one-article) diff --git a/docs/website/pages/learn/core-concepts/move-contracts/built-in-library.en-US.mdx b/docs/website/pages/learn/core-concepts/move-contracts/built-in-library.en-US.mdx index a594f842df..fbe65e4a78 100644 --- a/docs/website/pages/learn/core-concepts/move-contracts/built-in-library.en-US.mdx +++ b/docs/website/pages/learn/core-concepts/move-contracts/built-in-library.en-US.mdx @@ -1,15 +1,17 @@ # Rooch's built-in library -Rooch currently has three built-in standard libraries, namely `MoveStdlib`, `MoveosStdlib` and `RoochFramework`. +Rooch currently has four built-in standard libraries, namely `MoveStdlib`, `MoveosStdlib`, `RoochFramework` and `BitcoinMove`. -The addresses of the three libraries in Move are: +The addresses of the four libraries in Move are: - `MoveStdlib`: `0x1` - `MoveosStdlib`: `0x2` - `RoochFramework`: `0x3` +- `BitcoinMove`:`0x4` ## Documentation link - [MoveStdlib](https://github.com/rooch-network/rooch/tree/main/frameworks/move-stdlib/doc) - [MoveosStdlib](https://github.com/rooch-network/rooch/tree/main/frameworks/moveos-stdlib/doc) - [RoochFramework](https://github.com/rooch-network/rooch/tree/main/frameworks/rooch-framework/doc) +- [BitcoinMove](https://github.com/rooch-network/rooch/tree/main/frameworks/bitcoin-move/doc) \ No newline at end of file diff --git a/docs/website/pages/learn/core-concepts/move-contracts/built-in-library.zh-CN.mdx b/docs/website/pages/learn/core-concepts/move-contracts/built-in-library.zh-CN.mdx index 896a4786c1..9582852c0a 100644 --- a/docs/website/pages/learn/core-concepts/move-contracts/built-in-library.zh-CN.mdx +++ b/docs/website/pages/learn/core-concepts/move-contracts/built-in-library.zh-CN.mdx @@ -1,15 +1,17 @@ # Rooch 的内置库 -Rooch 当前内置了三个标准库,分别是 `MoveStdlib`、`MoveosStdlib` 和 `RoochFramework`。 +Rooch 当前内置了四个标准库,分别是 `MoveStdlib`、`MoveosStdlib`、`RoochFramework` 和 `BitcoinMove`。 -三个库在 Move 中的地址分别是: +四个库在 Move 中的地址分别是: - `MoveStdlib`:`0x1` - `MoveosStdlib`:`0x2` - `RoochFramework`:`0x3` +- `BitcoinMove`:`0x4` ## 文档链接 - [MoveStdlib](https://github.com/rooch-network/rooch/tree/main/frameworks/move-stdlib/doc) - [MoveosStdlib](https://github.com/rooch-network/rooch/tree/main/frameworks/moveos-stdlib/doc) - [RoochFramework](https://github.com/rooch-network/rooch/tree/main/frameworks/rooch-framework/doc) +- [BitcoinMove](https://github.com/rooch-network/rooch/tree/main/frameworks/bitcoin-move/doc) diff --git a/docs/website/pages/learn/core-concepts/move-contracts/move-on-rooch.en-US.mdx b/docs/website/pages/learn/core-concepts/move-contracts/move-on-rooch.en-US.mdx index c9df28d8c0..3336edf179 100644 --- a/docs/website/pages/learn/core-concepts/move-contracts/move-on-rooch.en-US.mdx +++ b/docs/website/pages/learn/core-concepts/move-contracts/move-on-rooch.en-US.mdx @@ -1,77 +1,58 @@ # Move on Rooch -Rooch's goal is to enable developers to quickly build and deploy Fully on-chain Applications. The most important thing for developing applications is to have an efficient development language. Rooch chose Move language as the development language. We believe that its following characteristics make it the most suitable smart contract language for building full-chain applications. +Rooch aims to enable developers to quickly build and deploy [VApps](../vapp) while ensuring that the application logic is verifiable through the smart contract language. Rooch has chosen the Move language for development, as we believe its features make it the most suitable smart contract language for building applications. -- **Platform independence**: Move and its virtual machine are not coupled to a specific blockchain platform. In order to adapt to different application scenarios, different blockchain platforms can innovate based on Move. -- **Security**: Move’s built-in security features and support for resource scarcity are more suitable for application scenarios like blockchain where assets and application logic are deeply bound. -- **Scalability**: Move's dependency management mechanism allows the platform to provide powerful built-in libraries, and developers can also easily introduce third-party libraries, making it more suitable for building complex applications. +1. **Platform Agnostic**: Move and its virtual machine are not tied to any specific blockchain platform. Different blockchain platforms can innovate based on Move to suit various application scenarios. +2. **Security**: Move's built-in security features and support for resource scarcity make it ideal for blockchain applications where assets and application logic are closely linked. +3. **Scalability**: Move's dependency management mechanism allows the platform to provide powerful built-in libraries while enabling developers to easily incorporate third-party libraries, making it suitable for building complex applications. -## Rooch Move Features +## Features of Rooch Move -The features of the Move language can be found in the reference materials at the end of the article. Here we mainly introduce the new features that Rooch brings to Move. +For a detailed overview of the Move language features, refer to the resources at the end. Here, we will focus on the new features that Rooch brings to Move. -## Built-in standard library +### Built-in Standard Libraries -Rooch currently has three built-in standard libraries, namely `MoveStdlib`, `MoveosStdlib` and `RoochFramework`. For details, see the [built-in libraries](./built-in-library). +Rooch currently includes four standard libraries: `MoveStdlib`, `MoveosStdlib`, `RoochFramework`, and `BitcoinMove`. For detailed information, see [Built-in Libraries](./built-in-library). -## Private generics +### Private Generics -`#[private_generics(T)]` is a function annotation that ensures that the function with this annotation can only be called within the module where `T` is defined. This annotation provides a similar constraint to Move's storage instructions, but it opens up this capability to developers. This feature is the basic condition for the following features. For details, see [Private Generics](../../../build/rooch-framework/private-generics). +`#[private_generics(T)]` is a function annotation that ensures functions with this annotation can only be called within the module where `T` is defined. This follows the same security model constraints as Move's storage instructions but opens this capability to developers. This feature is foundational for several other features. For more details, see [Private Generics](../../../build/rooch-framework/private-generics). -### Storage abstraction +### Data Structures -The goal of storage abstraction is to allow developers to more flexibly define their own state storage structures in smart contracts without being limited to the standardized solutions provided by the platform. So Rooch implemented the original storage instruction of Move in the contract. The following is a comparison table: +`#[data_struct(T)]` is a struct and function annotation that marks the struct as a pure data structure, which can be directly deserialized in a contract. This feature is designed to facilitate developers in obtaining data from external data sources. For more details, see [Data Structures](../../../build/rooch-framework/data-struct). -| Move store instructions | Functions in Rooch | Explanation | -|--------------------------------------------|----------------------------------------------------------------------------------|------------------------------------------------------------------| -| `move_to(&signer,T)` | `context::move_resource_to(&mut Context,&signer,T)` | Store resources of type `T` in the user state space of `signer`. | -| `move_from(address):T` | `context::move_resource_from(&mut Context,address): T` | Take resources of type `T` out of user state space. | -| `borrow_global(address):&T` | `context::borrow_resource(&Context,address): &T` | Read an immutable reference of type `T` from user space. | -| `borrow_global_mut(address):&mut T` | `context::borrow_mut_resource(&mut Context,address): &mut T` | Read a mutable reference of type `T` from user space. | -| `exists(address):bool` | `context::exists_resource(&Context,address): bool` | Determine whether there is a resource of type `T` in user space. | +### Storage Abstraction -The above methods provided by `account_storage` are all constrained by the `private_generics(T)` annotation, ensuring that the security is consistent with the Move storage instruction. +The goal of storage abstraction is to allow developers to define their own state storage structures in smart contracts more flexibly, without being limited to the standardized solutions provided by the platform. Rooch implements Move's original storage instructions within contracts, as shown in the table below: -In addition to `Account Resource,` Rooch also provides Object Storage, For detailed information about Rooch Object, see [Rooch Object](../objects/object). +| Move Storage Instruction | Corresponding Function in Rooch | Description | +|--------------------------------------------|-------------------------------------------------------------------------------|-----------------------------------------------------| +| `move_to(&signer,T)` | `account::move_resource_to(&signer,T)` | Stores a resource of type `T` in the user's state space of the `signer`. | +| `move_from(address):T` | `account::move_resource_from(address): T` | Retrieves a resource of type `T` from the user's state space. | +| `borrow_global(address):&T` | `account::borrow_resource(address): &T` | Reads an immutable reference of type `T` from the user space. | +| `borrow_global_mut(address):&mut T` | `account::borrow_mut_resource(address): &mut T` | Reads a mutable reference of type `T` from the user space. | +| `exists(address):bool` | `account::exists_resource(address): bool` | Checks if a resource of type `T` exists in the user space. | -Regarding the design of storage abstraction, you can refer to [Storage Abstraction](../objects/storage-abstraction). +All the methods provided by `account` are constrained by the `private_generics(T)` annotation, ensuring the same level of security as Move's storage instructions. -### Context +In addition to Account Resource, Rooch also provides Object storage. For more details on Rooch Object, see [Rooch Object](../objects/object). -Context contains two fields, one is `TxContext`, which contains information related to the current transaction, and the other is `StorageContext`, which is a global `Object` storage. +For more information on the design of storage abstraction, see [Storage Abstraction](../objects/storage-abstraction). -```move -module moveos_std::context{ - struct Context { - tx_context: TxContext, - storage_context: StorageContext, - } -} -``` +### Getting the Current Module's `signer` -Developers need to define the `Context` parameter in the `entry` function, and MoveVM will automatically fill in the parameter. +The function `moveos_std::signer::module_signer():signer` can be used to obtain the `signer` of the current module, allowing functions requiring `signer`, such as `context::move_resource_to`, to be called with the module's account identity. -```move -module example::my_module{ - public entry fun my_entry_fun(ctx: &mut Context){ - //function logic - } -} -``` +Here, `T` is constrained by `private_generics(T)`, ensuring safe invocation. -### Get the `signer` of the current module - -The function `moveos_std::signer::module_signer():signer` can be used to obtain the `signer` of the current module, and call `context::move_resource_to` and other functions that require a `signer` as the account of the current module. - -`T` here is subject to `private_generics(T)`, ensuring the safety of the call. - -### Crypto algorithm support +### Crypto Algorithm Support 1. [ed25519](https://github.com/rooch-network/rooch/blob/main/frameworks/rooch-framework/doc/ed25519.md) 2. [ecdsa_k1](https://github.com/rooch-network/rooch/blob/main/frameworks/rooch-framework/doc/ecdsa_k1.md) -### More references +## Additional Resources 1. [MoveBook](https://move-language.github.io/move/): A basic tutorial on the Move language -2. [Move on Aptos](https://aptos.dev/move/move-on-aptos/): Contains an introduction to the Move language and the features of Move on Aptos -3. [Move on Sui](https://docs.sui.io/learn/why-move): Contains an introduction to the Move language and the features of Move on Sui +2. [Move on Aptos](https://aptos.dev/move/move-on-aptos/): Introduction to the Move language and its features on Aptos +3. [Move on Sui](https://docs.sui.io/learn/why-move): Introduction to the Move language and its features on Sui \ No newline at end of file diff --git a/docs/website/pages/learn/core-concepts/move-contracts/move-on-rooch.zh-CN.mdx b/docs/website/pages/learn/core-concepts/move-contracts/move-on-rooch.zh-CN.mdx index b079e31d60..0ebfbece75 100644 --- a/docs/website/pages/learn/core-concepts/move-contracts/move-on-rooch.zh-CN.mdx +++ b/docs/website/pages/learn/core-concepts/move-contracts/move-on-rooch.zh-CN.mdx @@ -1,6 +1,6 @@ # Move on Rooch -Rooch 的目标是让开发者可以快速构建并部署全链应用(Fully on-chain Application),而开发应用最重要的是要有一个高效的开发语言。Rooch 选择了 Move 语言作为开发语言,我们认为它的以下特性,让它成为最适合构建全链应用的智能合约语言。 +Rooch 的目标是让开发者可以快速构建并部署 [VApp](../vapp),而智能合约语言可以保证应用逻辑的可验证性。Rooch 选择了 Move 语言作为开发语言,我们认为它的以下特性,让它成为最适合构建应用的智能合约语言。 1. **平台无关性**:Move 以及其虚拟机没有和具体的区块链平台实现耦合,不同的区块链平台为了适应不同的应用场景,可以基于 Move 做创新。 2. **安全性**:Move 自带的安全特性以及对资源的稀缺性的支持,更适合区块链这种资产和应用逻辑深度绑定的应用场景。 @@ -12,52 +12,34 @@ Move 语言的特性可以查阅文末的参考资料,这里主要介绍 Rooch ### 内置标准库 -Rooch 当前内置了三个标准库,分别是 `MoveStdlib`、`MoveosStdlib` 和 `RoochFramework`,详细介绍参看[内置库](./built-in-library)。 +Rooch 当前内置了四个标准库,分别是 `MoveStdlib`、`MoveosStdlib` 和 `RoochFramework`,`BitcoinMove`,详细介绍参看[内置库](./built-in-library)。 ### 私有泛型 `#[private_generics(T)]` 是一个函数注解,它保证了添加该注解的函数,只能在定义 `T` 的模块内调用,安全模型上遵循和 Move 存储指令类似约束,但把这个能力开放给了开发者。这个特性是后面几个特性的基础条件,详细介绍参看[私有泛型](../../../build/rooch-framework/private-generics)。 +### 数据结构体 + +`#[data_struct]` 是一个结构体注解,它标志该结构体是一个纯数据结构体,可以直接在合约中反序列化。这个特性是为了方便开发者从外部数据源中获取数据,详细介绍参看[数据结构体](../../../build/rooch-framework/data-struct)。 + ### 存储抽象 存储抽象的目标是让开发者可以在智能合约中更灵活地定义自己的状态存储结构,而不局限于平台提供的标准化方案。所以 Rooch 在合约中实现了 Move 原来的存储指令,以下是对照表: | Move 存储指令 | Rooch 中的函数 | 说明 | |--------------------------------------------|----------------------------------------------------------------------------------|-----------------------------------------------------| -| `move_to(&signer,T)` | `context::move_resource_to(&mut Context,&signer,T)` | 将 `T` 类型的资源存储在 `signer` 的用户状态空间内。 | -| `move_from(address):T` | `context::move_resource_from(&mut Context,address): T` | 将 `T` 类型的资源从用户状态空间中取出来。 | -| `borrow_global(address):&T` | `context::borrow_resource(&Context,address): &T` | 从用户空间中读取 `T` 类型的的不可变引用。 | -| `borrow_global_mut(address):&mut T` | `context::borrow_mut_resource(&mut Context,address): &mut T` | 从用户空间中读取 `T` 类型的的可变引用。 | -| `exists(address):bool` | `context::exists_resource(&Context,address): bool` | 判断用户空间中是否存在 `T` 类型的资源。 | +| `move_to(&signer,T)` | `account::move_resource_to(&signer,T)` | 将 `T` 类型的资源存储在 `signer` 的用户状态空间内。 | +| `move_from(address):T` | `account::move_resource_from(address): T` | 将 `T` 类型的资源从用户状态空间中取出来。 | +| `borrow_global(address):&T` | `account::borrow_resource(address): &T` | 从用户空间中读取 `T` 类型的的不可变引用。 | +| `borrow_global_mut(address):&mut T` | `account::borrow_mut_resource(address): &mut T` | 从用户空间中读取 `T` 类型的的可变引用。 | +| `exists(address):bool` | `account::exists_resource(address): bool` | 判断用户空间中是否存在 `T` 类型的资源。 | -`context` 提供的以上方法,都有 `private_generics(T)` 注解的约束,保证安全性上和 Move 存储指令一致。 +`account` 提供的以上方法,都有 `private_generics(T)` 注解的约束,保证安全性上和 Move 存储指令一致。 除了 Account Resource, Rooch 还同时提供 Object 存储。关于 Rooch Object 的详细信息参看 [Rooch Object](../objects/object)。 关于存储抽象的设计,可以参考[存储抽象](../objects/storage-abstraction)。 -### Context - -Context 包含两个字段,一个是 `TxContext` 包含当前交易相关的信息,另外一个是 `StorageContext`,是一个全局的 `Object` 存储。 - -```move -module moveos_std::context{ - struct Context { - tx_context: TxContext, - storage_context: StorageContext, - } -} -``` - -开发者需要在 `entry` 函数中定义 `Context` 参数,MoveVM 会自动填充该参数。 - -```move -module example::my_module{ - public entry fun my_entry_fun(ctx: &mut Context){ - //function logic - } -} -``` ### 获取当前模块的 `signer` diff --git a/docs/website/pages/learn/core-concepts/objects/object.en-US.mdx b/docs/website/pages/learn/core-concepts/objects/object.en-US.mdx index cb9bda59cd..f037222bb2 100644 --- a/docs/website/pages/learn/core-concepts/objects/object.en-US.mdx +++ b/docs/website/pages/learn/core-concepts/objects/object.en-US.mdx @@ -107,7 +107,7 @@ module moveos_std::object { ``` * Note, all Objects in Rooch are open to read, everyone can get any `&Object` through `ObjectID`. -* The owner of the Object can get `&mut Object` reference through `context::borrow_mut_object`. +* The owner of the Object can get `&mut Object` reference through `object::borrow_mut_object`. Method extension for developers: diff --git a/docs/website/pages/learn/core-concepts/objects/object.zh-CN.mdx b/docs/website/pages/learn/core-concepts/objects/object.zh-CN.mdx index 05df6d3c1a..c1a89acb2d 100644 --- a/docs/website/pages/learn/core-concepts/objects/object.zh-CN.mdx +++ b/docs/website/pages/learn/core-concepts/objects/object.zh-CN.mdx @@ -108,7 +108,7 @@ module moveos_std::object { ``` * 注意,Rooch 中的所有 Object 都是读公开的,任何人都可以通过 `ObjectID` 获取到任意的 `&Object`。 -* Object 的所有者可以通过 `context::borrow_mut_object` 获取 `&mut Object` 引用。 +* Object 的所有者可以通过 `object::borrow_mut_object` 获取 `&mut Object` 引用。 给开发者的扩展方法: @@ -244,7 +244,7 @@ https://dev-seed.rooch.network ### Object 相关的方法列表 -`context` 和 `object` 模块提供以下函数,可以对 `Object` 进行操作: +`object` 模块提供以下函数,可以对 `Object` 进行操作: | Object 函数 | `#[private_generics]` | 说明 | | -------------------------------------------------------------------------------------- | -------------------------- | ---------------------------------------------- | diff --git a/frameworks/moveos-stdlib/doc/bcs.md b/frameworks/moveos-stdlib/doc/bcs.md index ee6dd254e7..c904cffb07 100644 --- a/frameworks/moveos-stdlib/doc/bcs.md +++ b/frameworks/moveos-stdlib/doc/bcs.md @@ -3,11 +3,10 @@ # Module `0x2::bcs` -Source from https://github.com/aptos-labs/aptos-core/blob/main/aptos-move/framework/aptos-stdlib/sources/from_bcs.move +Part source from https://github.com/aptos-labs/aptos-core/blob/main/aptos-move/framework/aptos-stdlib/sources/from_bcs.move This module provides a number of functions to convert _primitive_ types from their representation in std::bcs -to values. This is the opposite of bcs::to_bytes. Note that it is not safe to define a generic public from_bytes -function because this can violate implicit struct invariants, therefore only primitive types are offerred. If -a general conversion back-and-force is needed, consider the moveos_std::Any type which preserves invariants. +to values. This is the opposite of bcs::to_bytes. +Note we provie a generic public from_bytes function and protected it with #[data_struct(T)]. - [Constants](#@Constants_0) @@ -123,11 +122,11 @@ The request Move type is not match with input Move type. ## Function `from_bytes` Function to deserialize a type T. -Note the private_generics ensure only the MoveValue's owner module can call this function +Note the data_struct ensure the T must be a #[data_struct] type -
#[data_struct(#[MoveValue])]
-public fun from_bytes<MoveValue>(bytes: vector<u8>): MoveValue
+
#[data_struct(#[T])]
+public fun from_bytes<T>(bytes: vector<u8>): T
 
@@ -137,12 +136,12 @@ Note the private_generics ensure only the MoveValue's ## Function `from_bytes_option` Function to deserialize a type T. -Note the private_generics ensure only the MoveValue's owner module can call this function +Note the data_struct ensure the T must be a #[data_struct] type If the bytes are invalid, it will return None. -
#[data_struct(#[MoveValue])]
-public fun from_bytes_option<MoveValue>(bytes: vector<u8>): option::Option<MoveValue>
+
#[data_struct(#[T])]
+public fun from_bytes_option<T>(bytes: vector<u8>): option::Option<T>
 
@@ -153,5 +152,5 @@ If the bytes are invalid, it will return None. -
public(friend) fun native_from_bytes<MoveValue>(bytes: vector<u8>): option::Option<MoveValue>
+
public(friend) fun native_from_bytes<T>(bytes: vector<u8>): option::Option<T>
 
diff --git a/frameworks/moveos-stdlib/sources/bcs.move b/frameworks/moveos-stdlib/sources/bcs.move index 154ff838a2..df64c10c3f 100644 --- a/frameworks/moveos-stdlib/sources/bcs.move +++ b/frameworks/moveos-stdlib/sources/bcs.move @@ -1,15 +1,17 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -/// Source from https://github.com/aptos-labs/aptos-core/blob/main/aptos-move/framework/aptos-stdlib/sources/from_bcs.move +/// Part source from https://github.com/aptos-labs/aptos-core/blob/main/aptos-move/framework/aptos-stdlib/sources/from_bcs.move /// This module provides a number of functions to convert _primitive_ types from their representation in `std::bcs` -/// to values. This is the opposite of `bcs::to_bytes`. Note that it is not safe to define a generic public `from_bytes` -/// function because this can violate implicit struct invariants, therefore only primitive types are offerred. If -/// a general conversion back-and-force is needed, consider the `moveos_std::Any` type which preserves invariants. +/// to values. This is the opposite of `bcs::to_bytes`. +/// Note we provie a generic public `from_bytes` function and protected it with `#[data_struct(T)]`. module moveos_std::bcs{ use std::option::{Self, Option}; + + friend moveos_std::any; + friend moveos_std::copyable_any; /// The request Move type is not match with input Move type. const ErrorTypeNotMatch: u64 = 1; @@ -39,26 +41,24 @@ module moveos_std::bcs{ from_bytes
(v) } - #[data_struct(MoveValue)] + #[data_struct(T)] /// Function to deserialize a type T. - /// Note the `private_generics` ensure only the `MoveValue`'s owner module can call this function - public fun from_bytes(bytes: vector): MoveValue { + /// Note the `data_struct` ensure the `T` must be a `#[data_struct]` type + public fun from_bytes(bytes: vector): T { let opt_result = native_from_bytes(bytes); assert!(option::is_some(&opt_result), ErrorInvalidBytes); option::destroy_some(opt_result) } - #[data_struct(MoveValue)] + #[data_struct(T)] /// Function to deserialize a type T. - /// Note the `private_generics` ensure only the `MoveValue`'s owner module can call this function + /// Note the `data_struct` ensure the `T` must be a `#[data_struct]` type /// If the bytes are invalid, it will return None. - public fun from_bytes_option(bytes: vector): Option { + public fun from_bytes_option(bytes: vector): Option { native_from_bytes(bytes) } - native public(friend) fun native_from_bytes(bytes: vector): Option; - friend moveos_std::any; - friend moveos_std::copyable_any; + native public(friend) fun native_from_bytes(bytes: vector): Option; // TODO: add test cases for this module. }