In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
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 introduces "how to write Libra's Move transaction script". In daily operation, I believe many people have doubts about how to write Libra's Move transaction script. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to write Libra's Move transaction script". Next, please follow the editor to study!
Move language
Move is a new programming language designed to provide a secure and programmable foundation for Libra block chains. The accounts in the Libra block chain are made up of any number of Move resources and Move modules. Each transaction submitted to the Libra blockchain uses a transaction script written by Move to encode its logic.
The transaction script updates the global status of the blockchain by calling the procedures declared by module.
The core concept of Move Move transaction script
Each Libra transaction contains an Move transaction script that encodes the logic that the validator executes on behalf of the customer (for example, transferring Libra from A's account to B's account).
By calling the procedures of one or more Move modules, the transaction script interacts with the Move resources published in the global storage of the Libra blockchain.
The transaction script is not stored in the global state, and other transaction scripts cannot call it. It's an one-time program.
Move modules
Move modules defines rules for updating the global status of the Libra blockchain. Modules is the equivalent of a smart contract in other blockchains. It declares the type of resources that can be published under a user account. Each account in the Libra blockchain is a container for any number of resources and modules.
Module is mainly used to declare structure types (including resources, which is a special structure) and procedures.
Move module's procedures defines rules for creating, accessing, and destroying its declared types.
Modules is reusable. A structure type declared in one module can use a structure type declared in another module, and a structure declared in one module can procedure call a public procedures declared in another module. A module can call a procedures declared in another Move module. The transaction script can call any public procedures of the published module.
Finally, Libra users will be able to publish modules using their own accounts.
Move resources
The main function of Move is the ability to define custom resource types. Resource types mainly encode digital assets.
Resources can be seen everywhere in Libra. They can be stored as data structures, passed as parameters to the procedure, returned from the procedure, and so on.
Move type system provides a special security guarantee for resources. Move resources can never be copied, reused, or discarded. A resource type can only be created or destroyed by the module that defines it. This is enforced by the Move virtual machine through bytecode verification. The Move virtual machine will refuse to run code that has not passed the bytecode validator.
The Libra currency is implemented through the resource type of LibraCoin.T. Like other resources, LibraCoin.T is a resource.
Write a Move program
In this section, I'll show you how to use Move IR to write transaction scripts and modules. IR is the (unstable) preview version of the upcoming Move source language. Move IR is a thin grammar layer on Move bytecode that is used to test bytecode validators and virtual machines and is not particularly friendly to developers. It is high enough to write human-readable code, but also low enough to compile directly into Move bytecode.
Write a transaction script
The user requests an update to the global storage of the Libra blockchain through the transaction script. Two important resources appear in almost all transaction scripts: the LibraAccount.T and LibraCoin.T resource types. LibraAccount is the name of the module, and T is the name of the resource declared by the module. This is the universal naming convention in Move. The "primary" type declared by module is often referred to as T.
When we say that the user "has an account at the address 0xff on the Libra blockchain", we mean that the address 0xff owns an instance of the LibraAccount.T resource. Each non-empty address has a LibraAccount.T resource. This resource stores account data, such as serial numbers, authentication keys, and balances. Any part of the Libra system with which you want to interact with the account must do this by reading data from the LibraAccount.T resource or by calling LibraAccount module's procedures.
The account balance is a resource of type LibraCoin.T. This is the type of Libra currency. Like any other Move resource, this type is a first-class citizen in language.
Resources of type LibraCoin.T can be stored in program variables, passed between procedures, and so on.
Now let's see how programmers interact with these modules and resources in transaction scripts.
/ / Simple peer-peer payment example.// Use LibraAccount module published on the blockchain at account address// 0x0...0 (with 64 zeroes). 0x0 is shorthand that the IR pads out to// 256 bits (64 digits) by adding leading zeroes.import 0x0.LibraAccountTinct import 0x0.LibraAccountMain (payee: address, amount: U64) {/ / The bytecode (and consequently, the IR) has typed locals. The scope of / / each local is the entire procedure. All local variable declarations must / / be at the beginning of the procedure. Declaration and initialization of / / variables are separate operations, but the bytecode verifier will prevent / / any attempt to use an uninitialized variable. Let coin: LibraCoin.T; / / Acquire a LibraCoin.T resource with value `amount from the sender's / / account. This will fail if the sender's balance is less than `amount'. Coin = LibraAccount.withdraw_from_sender (move (amount)); / / Move the LibraCoin.T resource into the account of `payee. If there is no / / account at the address `payee`, this step will fail LibraAccount.deposit (move (payee), move (coin)); / / Every procedure must end in a `payn`. The IR compiler is very literal: / / it directly translates the source it is given. It will not do fancy / / things like inserting missing `roomn`s. Return;}
This transaction script has an unfortunate problem-if the payee does not have an account, it will fail. We will resolve this issue by modifying the script to create an account for the payee (if it does not already exist).
/ / A small variant of the peer-peer payment example that creates a fresh// account if one does not already exist.import 0x0.LibraAccount` import 0x0.LibraCoinscape main (payee: address, amount: U64) {let coin: LibraCoin.T; let account_exists: bool; / / Acquire a LibraCoin.T resource with value `amount from the sender's / / account. This will fail if the sender's balance is less than `amount'. Coin = LibraAccount.withdraw_from_sender (move (amount)); account_exists = LibraAccount.exists (copy (payee)); if (! move (account_exists)) {/ / Creates a fresh account at the address `payee` by publishing a / LibraAccount.T resource under this address. If theres is already a _ LibraAccount.T resource under the address, this will fail.create_account (copy (payee));} LibraAccount.deposit (move (payee), move (coin)); return;}
Let's look at a more complex example. In this example, we will use a transaction script to pay multiple recipients, not just one.
/ / Multiple payee example. This is written in a slightly verbose way to// emphasize the ability to split a `LibraCoin.T` resource. The more concise// way would be to use multiple calls to `let coin1: address, amount1: U64, payee2: address, amount2: U64) {let coin1: LibraCoin.T; let coin2: LibraCoin.T; let total: U64; total = move (amount1) + copy (amount2); coin1 = LibraAccount.withdraw_from_sender (move (total)); / / This mutates `coin1`, which now has value `amount 1`. / / `coin2` has value `amount 2`. Coin2 = LibraCoin.withdraw (& mut coin1, move (amount2)); / / Perform the payments LibraAccount.deposit (move (payee1), move (coin1)); LibraAccount.deposit (move (payee2), move (coin2)); return;}
Well, this is a simple transaction script, although we do not understand the syntax of Move IR, but looking directly at the content should be easy to understand what the script is doing.
Write your own Modules
The above trading script uses the existing LibraAccount and LibraCoin modules, so how do we write our own Move modules?
Consider this situation: B will create an account at address an in the future. A wants to "earmark" some funds for B so that he can deposit it into his account once he has created it. However, if B has never created the account, she also wants to be able to recover the funds herself.
To solve this problem of A, we will write a module EarmarkedLibraCoin:
Declare a new resource type EarmarkedLibraCoin.T that wraps the Libra coin and recipient address.
Allow A to create this type and publish it to her account (creation process).
Allow B to declare resources (claim_for_recipient procedures).
Allow anyone who owns the EarmarkedLibraCoin.T to destroy it and get the appropriate coin (unpacking program).
/ / A module for earmarking a coin for a specific recipientmodule EarmarkedLibraCoin {import 0x0.LibraCoin; / / A wrapper containing a Libra coin and the address of the recipient the / / coin is earmarked for. Resource T {coin: LibraCoin.T,recipient: address} / / Create a new earmarked coin with the given `principient`. / / Publish the coin under the transaction sender's account address. Public create (coin: LibraCoin.T, recipient: address) {let t: Self.T;// Construct or "pack" a new resource of type T. Only procedures of the// `EarmarkedLibraCoin` module can create an `EarmarkedLibraCoin.T`.t = T {coin: move (coin), recipient: move (recipient),}; / / Publish the earmarked coin under the transaction sender's account// address. Each account can contain at most one resource of a given type;// this call will fail if the sender already has a resource of this type.move_to_sender (move (t)); return;} / / Allow the transaction sender to claim a coin that was earmarked for her. Public claim_for_recipient (earmarked_coin_address: address): Self.T acquires T {let t: Self.T;let t_ref: & Self.T;let sender: address;// Remove the earmarked coin resource published under `earmarked_coin_ address`. / / If there is no resource of type T published under the address, this will fail.t = move_from (move (earmarked_coin_address)); t_ref = & t Tipple / This is a builtin that returns the address of the transaction sender.sender = get_txn_sender () / / Ensure that the transaction sender is the recipient. If this assertion// fails, the transaction will fail and none of its effects (e.g.Grample / removing the earmarked coin) will be committed. 99 is an error code// that will be emitted in the transaction output if the assertion fails.assert (* (& move (t_ref) .recipient) = = move (sender), 99); return move (t);} / / Allow the creator of the earmarked coin to reclaim it. Public claim_for_creator (): Self.T acquires T {let t: Self.T;let sender: address;sender = get_txn_sender (); / / This will fail if no resource of type T under the sender's address.t = move_from (move (sender)); return move (t);} / / Extract the Libra coin from its wrapper and return it to the caller. Public unwrap (t: Self.T): LibraCoin.T {let coin: LibraCoin.T;let recipient: address;// This "unpacks" a resource type by destroying the outer resource, but// returning its contents. Only the module that declares a resource type// can unpack it.T {coin, recipient} = move (t); return move (coin);}}
A can create a dedicated coin for B by creating a transaction script that invokes create at address B an and the LibraCoin.T she owns. After creating a, B can claim coin by sending a transaction from a. This calls claim_for_recipient, passes the result to unwrap, and stores the returned LibraCoin wherever he wants.
If B takes too long to create an account under account an and A wants to recover its funds, you can do this by using Claim_for_creator and then canceling unwrap.
At this point, the study on "how to write the Move transaction script for Libra" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.