Preliminary Exploration of Libra's Move Language, 10 Lines of Code to Realize Your First Intelligent Contract

In the blog of the last few days, the author introduced the relevant issues. libra environment construction as well as The Method of Transaction through client . Today, I will continue to introduce to you the scheme of writing intelligent contracts in mobile language.

The main features of mobile language are:

Programmable Move Transaction Script

  • Each Libra block chain transaction contains a Move transaction script to encode the transaction logic, and the verifier verifies the client's behavior accordingly (for example, moving Libra currency from Alice's account to Bob's account).
  • Transaction scripts call one or more Move module Process and published in global storage of Libra block chain Move resources Interaction.
  • Transaction scripts are not stored in the global state of block chains, and other transaction scripts cannot call it. This is a one-time program.

Move Module for Combinable Intelligent Contracts

The Move module defines rules for updating the global state of the Libra block chain. These Modules are the same as intelligent contracts in other block chain systems. Module declarations can be published under user accounts Resources Type. Each account in the Libra block chain is a container that can accommodate any number of resources and modules.

  • Modules declare two structural types (including resources, which are special structures) and processes.
  • The process of a Move module defines the rules for creating, accessing, and destroying the types it declares.
  • Modules can be reused. Structural types declared in one module can be used in another module, and processes declared in one module can be invoked in public processes declared in another module. Modules can call processes declared in other Move modules. Transaction scripts can call any common process of a published module.
  • Ultimately, Libra users will be able to publish modules under their own accounts.

Move's first-class resources

  • The key function of Move is to customize resource types. Resource types are programmable and secure by encoding.
  • Resources are common type values in languages. They can be stored as data structures, passed as parameters to processes, returned from processes, and so on.
  • Move's system provides special security assurance for resources. Move resources will never be replicated, reused or discarded. Resource types can only be created or destroyed by modules that define them. These guarantees are guaranteed by Move Virtual Machine Through bytecode static validation, the Move virtual machine will refuse to run program code that has not passed bytecode validation.
  • Libra currency is implemented as a resource type called LibraCoin.T. LibraCoin.T has no special place in the language; every Move resource enjoys the same protection.

The Author's Interpretation of Move Language to Move Language

First, Move is a static typed language, that is, a strongly typed language. In this respect, Move, like Java and C, exposes more problems at compile time and reduces the chance of crash at run time.

Second, the Move virtual machine guarantees that its resources remain first-class through bytecode (as we will see in a moment), and the move refuses to execute programs that fail bytecode validation. That is, resources can only be created or destroyed by defined modules. Other modules have no privileges, which avoids security vulnerabilities and other problems similar to that caused by multiple pointers pointing to the same resource in Ethernet solid.

First smart contract

First, build the compilation environment of Move by following commands

cd ~/libra
cargo build --bin compiler

And there are the following returns, the code compiled successfully

   Compiling compiler v0.1.0 (/home/machao/libra/language/compiler)
    Finished dev [unoptimized + debuginfo] target(s) in 57.23s

Well, let's just go ahead and see what our first contract looks like. Note: The following code is directly transferred from libra's official website

// Simple peer-peer payment example.

// 0x0...0 (with 64 zeroes). 0x0 is shorthand that the IR pads out to
// 256 bits (64 digits) by adding leading zeroes.
import 0x0.LibraAccount;
import 0x0.LibraCoin;
main(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: R#LibraCoin.T;
  // The R# part of the type above is one of two *kind annotation* R# and V#
  // (shorthand for "Resource" and "unrestricted Value"). These annotations
  // must match the kind of the type declaration (e.g., does the LibraCoin
  // module declare `resource T` or `struct 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 `return`. The IR compiler is very literal:
  // it directly translates the source it is given. It will not do fancy
  // things like inserting missing `return`s.


A new file copies the above code into your program. The author takes trans.m in the test directory as an example

mkdir test
cd test
gedit trans.m #Paste the above code to this file

1 Compiles in Libra's client

#After going to the command line of libra, execute the following commands
dev c /opt/libra/test/trans.m /opt/libra/test/

2 can also be compiled using the command line

cd ~/libra/target/debug
./compiler /opt/libra/test/trans.m -o /opt/libra/test/

3 Looking at the generated ir file, we can see that the compiled file is as follows, which is what we just said about binary code. Its main application is for security considerations, if the module is not authorized, it can not change the resources.

cat /opt/libra/test/

Well, today's time is limited. Next time, I'll introduce the programming skills of Move Intelligent Contracts.

Tags: Mobile encoding Java less

Posted on Mon, 05 Aug 2019 03:38:43 -0700 by StateGuy