Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use move language

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use move language". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the move language.

The characteristics of move language

Libra's goal is to create a global financial and monetary platform that empowers billions of people on the planet. Then the move language as Libra needs to provide strong support in terms of security and programmability.

To achieve this goal, move follows the following four design principles:

Resource priority

In the blockchain, we need to access digital assets through programs, which are obviously different from variables in ordinary programs (such as booleans,integers,strings). We need a special way to represent the digital assets in the blockchain.

Resource in the Move language is defined specifically for digital assets, and it has the following characteristics:

Resource cannot be copied or implicitly destroyed, it can only be moved between different users. This feature is already declared in the type definition of Move. In addition to this special limitation, resource, like other ordinary resources, can be stored in data structures, passed as parameters to procedures, and so on.

The concept of resource first is a great help for programmers to write secure and effective code.

Libra coin is a resource, and because it will correspond to real-world currencies, it can be created, modified, or destroyed. What we need to do is to control the permission to operate coin through modules.

Modules in move is a bit like an intelligent contract in ethernet, where module declares resource types and procedures (business logic: how to create, move, and destroy coin). The type and procedures defined in module can be called by other module.

Flexibility

The flexibility of move is reflected in that you can freely combine all kinds of transaction to achieve different functions through transaction scripts. A script can call multiple transaction.

The relationship of modules/resources/procedures in move is a bit like that of classes/objects/methods in an object-oriented programming language.

Security.

Move defines the security of resources, types, and memory, and any operation that violates these security will be rejected.

Generally speaking, there are two ways to achieve this function: 1. At a high level of language definition, these exceptions are checked by the compiler. two。 Check for these exceptions on the low-level virtual machine assembly language.

Move is one of these two ways: Move executes bytecode a little higher than assembly language, but a little lower than programming language. When the bytecode of move is submitted to the chain, it is verified by the bytecode verifier and then executed by the bytecode interpreter.

Verifiability

The best way to verify is to submit the bytecode to the chain for real verification, but this will obviously increase the burden on the chain and affect the speed of the transaction, so in move we do lightweight verification on the chain as much as possible and do offline static verification at the language level.

A preliminary study of Move sentence

In this section, we will explain the specific features of the Move language in detail through an example. The example in this section is written in move IR, an experimental version, and the official move language is still being written.

Peer-to-peer payment transaction script public main (payee: address, amount: U64) {let coin: 0x0.Currency.Coin = 0x0.Currency.withdraw_from_sender (copy (amount)); 0x0.Currency.deposit (copy (payee), move (coin));}

The above is a simple transaction script, and the main method has two inputs, one is the address, and the other is the amount to be paid.

The logic is simple: subtract the amount from the sender's account and transfer it to the receiver.

0x0 is the storage address of module, Currency is the name of module, and 0x0.Currency.Coin represents resources.

As we mentioned in the above chapter, coin is resource, only move can not copy, try to replace move (coin) with copy (coin) will report an error.

Other non-resource resources such as payee and amout can be move or copy.

If we add a line like this:

0x0.Currency.deposit (copy (some_other_payee), move (coin))

Then coin will be used twice, which is logically problematic, in the move language

The move (coin) method becomes unavailable after using it once, which leads to the failure of the second move, which effectively ensures the application logic.

Coin can only be moved once and must be moved once. If you delete move (coin), you will also get an error. The purpose of this is to effectively avoid application logic errors caused by programmers' negligence.

Currency Module

Above we define a module for Currency. Here we talk about how Currency is implemented.

There are two kinds of programs in Libra, one is transaction script, the other is module, the script will call the procedure in module to update the global state.

A transaction script is a script that can only be executed once and can no longer be used after execution, while module is long-standing code released in the global state.

The global status is a mapping of an account address to an account. As shown in the following figure, 0x0.0x1.0x2 is the address of the account.

Each address can contain any module and resources. For example, the account at the 0x0 address contains a module:0x0.Currency and a resource:0x0.Currency.Coin.

Now that we know the structure of the account, let's take a look at how module defines resources:

Module Currency {resource Coin {value: u64} / /...}

The above code defines a resource for Coin, which has a value field of type U64.

Let's take a look at how the storage operation is defined:

Public deposit (payee: address, to_deposit: Coin) {let to_deposit_value: U64 = Unpack (move (to_deposit)); let coin_ref: & mut Coin = BorrowGlobal (move (payee)); let coin_value_ref: & mut U64 = & mut move (coin_ref) .value; let coin_value: U64 = * move (coin_value_ref); * move (coin_value_ref) = move (coin_value) + move (to_deposit_value);}

This process has mainly done the following things:

Destroy the entered Coin and record its value.

Gets the reference that exists in the Coin in the payee.

Add the input Coin to the payee.

Unpack is the only way to destroy T, and unpack destroys T and returns the corresponding value of T.

BorrowGlobal takes an address as an argument and returns a reference to the T instance in that address.

Again, let's take a look at how withdraw_from_sender is implemented:

Public withdraw_from_sender (amount: U64): Coin {let transaction_sender_address: address = GetTxnSenderAddress (); let coin_ref: & mut Coin = BorrowGlobal (move (transaction_sender_address)); let coin_value_ref: & mut U64 = & mut move (coin_ref) .value; let coin_value: U64 = * move (coin_value_ref); RejectUnless (copy (coin_value) > = copy (amount)); * move (coin_value_ref) = move (coin_value)-copy (amount) Let new_coin: Coin = Pack (move (amount)); return move (new_coin);}

This process has done three things:

Get the only reference to coin in the sender's address.

Subtract the corresponding quantity.

Create and return a new coin.

Where Pack is the reverse operation of Unpack. Used to create T resources.

At this point, I believe you have a deeper understanding of "how to use the move language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report