Move是一种新的编程语言,旨在为Libra Blockchain提供安全可编程的基础。Libra Blockchain中的帐户是任意数量的Move资源和Move模块的容器。提交给Libra Blockchain的每个事务都使用Move中编写的事务脚本来编码其逻辑。事务脚本可以调用模块声明的过程来更新区块链的全局状态。

在本指南的第一部分中,我们将对Move语言的主要功能进行高级介绍:

  1. 移动事务脚本启用可编程事务
  2. 移动模块允许可组合智能合约
  3. 移动有一流的资源

对于好奇的读者,Move技术论文包含有关该语言的更多细节。

在本指南的第二部分中,我们将“深入了解”并向您展示如何在Move中间表示中编写您自己的Move程序初始testnet版本不支持自定义移动程序,但这些功能可供您在本地试用。

移动的主要特点

移动事务脚本启用可编程事务

  • 每个Libra事务都包含一个Move事务脚本,该脚本对验证器应代表客户端执行的逻辑进行编码(例如,将Libra从Alice的帐户移动到Bob的帐户)。
  • 事务脚本通过调用一个或多个Move模块的过程与Libra Blockchain的全局存储中发布的Move资源进行交互
  • 事务脚本不存储在全局状态中,并且其他事务脚本无法调用它。这是一次性使用的程序。
  • 我们在编写事务脚本中提供了几个事务脚本示例

移动模块允许可组合智能合约

移动模块定义了更新Libra Blockchain全局状态的规则。模块在其他区块链系统中与智能合约相同。模块声明可以在用户帐户下发布的资源类型。Libra Blockchain中的每个帐户都是一个容器,可以容纳任意数量的资源和模块。

  • 模块声明两种结构类型(包括资源,这是一种特殊的结构)和过程。
  • Move模块的过程定义了创建,访问和销毁它声明的类型的规则。
  • 模块可重复使用。在一个模块中声明的结构类型可以使用在另一个模块中声明的结构类型,并且在一个模块中声明的过程可以调用在另一个模块中声明的公共过程。模块可以调用其他Move模块中声明的过程。事务脚本可以调用已发布模块的任何公共过程。
  • 最终,Libra用户将能够在自己的帐户下发布模块。

移动有一流的资源

  • Move的关键功能是定义自定义资源类型的能力。资源类型用于编码具有丰富可编程性的安全数字资产。
  • 资源是语言中的普通值。它们可以存储为数据结构,作为参数传递给过程,从过程返回,等等。
  • Move类型系统为资源提供特殊的安全保证。移动资源永远不会被复制,重用或丢弃。资源类型只能由定义类型的模块创建或销毁。这些保证由Move虚拟机通过字节码验证静态实施Move虚拟机将拒绝运行未通过字节码验证程序的代码。
  • Libra货币实现为名为的资源类型LibraCoin.TLibraCoin.T在该语言中没有特殊的地位; 每个Move资源都享有相同的保护。

移动:在引擎盖下

移动中间表示

本节介绍如何在Move中间表示(IR)中编写事务脚本模块我们提醒读者,IR是即将推出的Move源语言的早期(且不稳定)前体(有关更多详细信息,请参阅Future Developer Experience)。移动IR是移动字节码上的一个薄语法层,用于测试字节码验证器和虚拟机,它不是特别适合开发人员。它的高级别足以编写人类可读的代码,但低级别足以直接编译为移动字节码。尽管如此,我们对Move语言感到很兴奋,并希望开发人员尽可能地尝试IR,尽管边缘粗糙。

我们将继续介绍重要评论的Move IR的片段。我们鼓励读者通过在本地编译,运行和修改示例来跟随这些示例。README文件libra/language/README.mdlibra/language/compiler/README.md解释如何执行此操作。

编写交易脚本

正如我们在Move Transaction Scripts Enable Programmable Transactions中所述,用户编写事务脚本以请求更新Libra Blockchain的全局存储。几乎所有事务脚本中都会出现两个重要的构建块:资源类型LibraAccount.TLibraCoin.T资源类型。LibraAccount是模块T的名称,是该模块声明的资源的名称。这是Move中常见的命名约定; 模块声明的“main”类型通常是命名的T

When we say that a user "has an account at address 0xff on the Libra Blockchain", what we mean is that the address 0xff holds an instance of the LibraAccount.T resource. Every nonempty address has a LibraAccount.T resource. This resource stores account data, such as the sequence number, authentication key, and balance. Any part of the Libra system that wants to interact with an account must do so by reading data from the LibraAccount.T resource or invoking procedures of the LibraAccount module.

帐户余额是类型的资源LibraCoin.T正如我们在Move Has First Class Resources中所解释的那样,这是天秤座硬币的类型。与任何其他Move资源一样,此类型是语言中的“一等公民”。类型的资源LibraCoin.T可以存储在程序变量中,在程序之间传递,等等。

我们鼓励感兴趣的读者检查目录LibraAccountLibraCoin模块中这两个关键资源的Move IR定义libra/language/stdlib/modules/

现在让我们看看程序员如何在事务脚本中与这些模块和资源进行交互。

// 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.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.
  return;
}

此事务脚本有一个不幸的问题 - 如果地址下没有帐户,它将失败payee我们将通过修改脚本来修复此问题,以便为payee尚不存在的帐户创建帐户

// 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.LibraCoin;
main(payee: address, amount: u64) {
  let coin: R#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;
}

让我们看一个更复杂的例子。在此示例中,我们将使用事务脚本来支付多个收件人而不只是一个。

// 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 `LibraAccount.withdraw_from_sender`.

import 0x0.LibraAccount;
import 0x0.LibraCoin;
main(payee1: address, amount1: u64, payee2: address, amount2: u64) {
  let coin1: R#LibraCoin.T;
  let coin2: R#LibraCoin.T;
  let total: u64;

  total = move(amount1) + copy(amount2);
  coin1 = LibraAccount.withdraw_from_sender(move(total));
  // This mutates `coin1`, which now has value `amount1`.
  // `coin2` has value `amount2`.
  coin2 = LibraCoin.withdraw(&mut coin1, move(amount2));

  // Perform the payments
  LibraAccount.deposit(move(payee1), move(coin1));
  LibraAccount.deposit(move(payee2), move(coin2));
  return;
}

这就结束了我们对事务脚本的“浏览”。有关更多示例,包括初始testnet中支持的事务脚本,请参阅libra/language/stdlib/transaction_scripts

编写模块

我们现在将注意力转向编写自己的Move模块,而不是仅重用现有模块LibraAccountLibraCoin模块。考虑这种情况:Bob将在未来的某个时刻在地址a创建一个帐户Alice希望为Bob“收取”一些资金,以便他们可以在创建后将其提取到他的帐户中。但如果鲍勃从未创建帐户,她也希望能够为自己收回资金。

为了解决Alice的这个问题,我们将编写一个模块EarmarkedLibraCoin

  • 声明一个新的资源类型EarmarkedLibraCoin.T,用于包装Libra硬币和收件人地址。
  • 允许Alice创建此类型并在其帐户下发布(create程序)。
  • 允许Bob声明资源(claim_for_recipient过程)。
  • 允许任何人EarmarkedLibraCoin.T摧毁它并获得底层硬币(unwrap程序)。
// A module for earmarking a coin for a specific recipient
module EarmarkedLibraCoin {
  import 0x0.LibraCoin;

  // A wrapper containing a Libra coin and the address of the recipient the
  // coin is earmarked for.
  resource T {
    coin: R#LibraCoin.T,
    recipient: address
  }

  // Create a new earmarked coin with the given `recipient`.
  // Publish the coin under the transaction sender's account address.
  public create(coin: R#LibraCoin.T, recipient: address) {
    let t: R#Self.T;

    // Construct or "pack" a new resource of type T. Only procedures of the
    // `EarmarkedCoin` module can create an `EarmarkedCoin.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<T>(move(t));
    return;
  }

  // Allow the transaction sender to claim a coin that was earmarked for her.
  public claim_for_recipient(earmarked_coin_address: address): R#Self.T {
    let t: R#Self.T;
    let t_ref: &R#Self.T;
    let sender: address;

    // Remove the earmarked coin resource published under `earmarked_coin_address`.
    // If there is resource of type T published under the address, this will fail.
    t = move_from<T>(move(earmarked_coin_address));

    t_ref = &t;
    // 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.,
    // 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(): R#Self.T {
    let t: R#Self.T;
    let coin: R#LibraCoin.T;
    let recipient: address;
    let sender: address;

    sender = get_txn_sender();
    // This will fail if no resource of type T under the sender's address.
    t = move_from<T>(move(sender));
    return move(t);
  }

  // Extract the Libra coin from its wrapper and return it to the caller.
  public unwrap(t: R#Self.T): R#LibraCoin.T {
    let coin: R#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);
  }

}

Alice可以通过创建一个调用createBob的地址aLibraCoin.T她拥有的事务脚本为Bob创建一个专用硬币一旦被创建,鲍勃可以通过从发送交易权利要求的硬币一个这会调用claim_for_recipient,将结果传递给unwrap,并将返回的内容存储LibraCoin在他希望的任何地方。如果Bob花费太长时间在a下创建一个帐户并且Alice想要收回她的资金,那么她可以通过使用claim_for_creator后续来完成unwrap

细心的读者可能已经注意到,该模块中的代码与内部结构无关LibraCoin.T它可以像使用通用编程一样容易地编写(例如resource T<AnyResource: R> { coin: AnyResource, ... })。我们目前正致力于为Move添加对这种参数多态的支持。

未来的开发经验

在不久的将来,IR将稳定下来,编译和验证程序将变得更加用户友好。此外,将跟踪来自IR源的位置信息并将其传递给验证程序,以使错误消息更易于调试。但是,IR将继续作为测试Move字节码的工具。它意味着是底层字节码的语义透明表示。为了进行有效的测试,IR编译器必须生成错误的代码,这些代码将被字节码验证程序拒绝或在运行时在编译器中失败。用户友好的源语言会做出不同的选择; 它应该拒绝编译将在管道的后续步骤中失败的代码。

将来,我们将拥有更高级别的Move源语言。该源语言旨在安全轻松地表达常见的Move习语和编程模式。由于Move字节码是一种新语言,而Libra Blockchain是一种新的编程环境,我们对应该支持的习语和模式的理解仍在不断发展。源语言还处于开发的早期阶段,我们还没有发布它的时间表。

 

Libra国内开发者微信交流群:

不能入群请加管理微信,拉你进群=>

posted on 2019-07-20 01:29  王庆东mas  阅读(1099)  评论(0编辑  收藏  举报