Libra教程之:来了,你最爱的Move语言

简介: Libra教程之:来了,你最爱的Move语言

文章目录



Move语言


Move是一种新的编程语言,旨在为Libra区块链提供安全且可编程的基础。 Libra区块链中的帐户就是由任意数量的Move resources和Move modules组成的。 提交给Libra区块链的每个交易都使用Move编写的交易脚本来对其逻辑进行编码


交易脚本通过调用module声明的procedures来更新区块链的全局状态。


Move的核心概念


Move交易脚本


每个Libra交易都包含一个Move交易脚本,该脚本对验证程序代表客户执行的逻辑进行编码(例如,将Libra从A的帐户转移到B的帐户)。


通过调用一个或多个Move模块的procedures,事务脚本与发布在Libra区块链的全局存储中的Move resources进行交互。


事务脚本并不会在全局状态中存储,并且其他事务脚本也无法调用它。 它是一个一次性程序。


Move modules


Move modules定义了用来更新Libra区块链的全局状态的规则。 modules相当于其他区块链中的智能合约。 它声明了可以在用户帐户下发布的resources类型。


Libra区块链中的每个帐户都是一个容器,用于容纳任意数量的resources和modules。


module主要用来声明结构类型(包括资源,这是一种特殊的结构)和procedures。


Move module的procedures定义了用于创建,访问和销毁其声明的类型的规则。


modules是可重用的。 在一个module中声明的结构类型可以使用在另一个module中声明的结构类型,并且在一个module中声明的可以procedure调用在另一个module中声明的public procedures。


一个module可以调用在其他Move module中声明的procedures。 事务脚本可以调用已发布module的任何public procedures。


最后,Libra用户将能够使用自己的帐户发布modules。


Move resources


Move的主要功能是能够定义自定义资源类型。 资源类型主要对数字资产进行编码。

资源在Libra中随处可见。 它们可以存储为数据结构,可以作为参数传递给过程,可以从过程中返回,等等。


Move type system为资源提供了特殊的安全保证。 Move resources永远不能被复制,重用或丢弃。 资源类型只能由定义该类型的模块创建或销毁。这是由Move虚拟机通过字节码验证来强制进行保证的。 Move虚拟机将拒绝运行尚未通过字节码验证程序的代码。

Libra货币是通过LibraCoin.T的资源类型来实现的。 和其他的资源一样,LibraCoin.T也是一种资源。


写一个Move程序


本节我会介绍怎么使用Move IR来编写事务脚本和模块。IR是即将推出的Move源语言的预览版本(不稳定)。 Move IR是Move字节码上的一个薄语法层,用于测试字节码验证程序和虚拟机,它对开发人员并不特别友好。 它足够高,可以编写人类可读的代码,但是也足够低,可以直接编译为Move字节码。


编写交易脚本


用户通过交易脚本来请求对Libra区块链的全局存储进行更新。几乎所有事务脚本中都会出现两个重要资源:LibraAccount.T和LibraCoin.T资源类型。 LibraAccount是module的名称,而T是该module声明的资源的名称。这是Move中的通用命名约定。module声明的“主要”类型通常称为T。


当我们说用户“在Libra区块链上的地址0xff拥有一个帐户”时,我们的意思是地址0xff拥有LibraAccount.T资源的实例。每个非空地址都有一个LibraAccount.T资源。此资源存储帐户数据,例如序列号,身份验证密钥和余额。要与帐户进行交互的Libra系统的任何部分都必须通过从LibraAccount.T资源中读取数据或调用LibraAccount module的procedures来进行此操作。


帐户余额是LibraCoin.T类型的资源。这是Libra货币的类型。与任何其他Move资源一样,此类型在语言上是一等公民。


LibraCoin.T类型的资源可以存储在程序变量中,在过程之间传递,等等。


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


// 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: 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 `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;
}


此交易脚本有一个不幸的问题-如果收款人下没有帐户,它将失败。 我们将通过修改脚本为收款人创建帐户(如果尚不存在)来解决此问题。


// 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: 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: 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 `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;
}


好了,这就是简单的交易脚本,虽然我们不了解Move IR的语法,但是直接看内容应该就很容易明白这个脚本到底在做什么了。


编写自己的Modules


上面的交易脚本使用了现有的LibraAccount和LibraCoin modules,那么我们怎么编写自己的Move modules呢?


考虑这种情况:B将来会在地址a创建一个帐户。 A想为B“专款”一些资金,以便他一旦创建就可以将其存入他的帐户。 但是,如果B从未创建该帐户,她还希望能够自己收回资金。


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


  • 声明一个新的资源类型EarmarkedLibraCoin.T,该资源类型包装了Libra coin和收件人地址。
  • 允许A创建此类类型并将其发布到她的帐户下(创建过程)。
  • 允许B声明资源(claim_for_recipient过程)。
  • 允许拥有EarmarkedLibraCoin.T的任何人销毁它并获得相应的coin(拆包程序)。


// 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: 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: 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<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): 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<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(): 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<T>(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);
  }
}
相关文章
|
Web App开发 存储 Linux
Linux(33)Rockchip RK3568 Ubuntu22.04上通过SSH运行Qt程序和关闭Chrome的密钥提示
Linux(33)Rockchip RK3568 Ubuntu22.04上通过SSH运行Qt程序和关闭Chrome的密钥提示
2300 0
交叉证书
## 证书4要素 pubkey(公钥) ,有一个其一一对应的私钥,由证书拥有者保存。 subject(名字),可以理解为证书的名字,你可以理解为 网站的域名。 issuer(颁发者的名字),即上级证书的subject。 signature(签名) ,上级证书使用私钥对当前证书进行签名的值。 ## 证书签名 假设 有 3 级 证书 ![image.png](
6247 0
|
8月前
|
人工智能 程序员 C语言
Python 与 C++、C 语言的区别及选择指南
C 语言:语法相对复杂,变量需要显式声明数据类型,代码块用大括号表示。例如:
1113 3
|
11月前
|
自然语言处理 前端开发 Windows
推荐一款很好用的VSCode变量翻译插件
本文介绍VSCode插件“var-translate-en”,可一键将中文翻译为英文并转为小驼峰等命名格式,支持百度、腾讯、阿里等翻译服务。通过简单配置与快捷键设置,提升变量命名效率,解决命名难题。
2637 0
|
10月前
|
人工智能 算法 安全
AI应用软件的开发费用
2026年AI应用开发成本受功能复杂度、技术选型与团队构成影响。MVP版约15–40万元;自建团队月均10–15万;外包报价25–60万。大模型API成本下降,但ASR/TTS、合规备案(3–8万)、内容审核及持续运维成新支出重点。建议“核心自研+UI外包”,20万起跑内测。(239字)
|
9月前
|
人工智能 Linux API
今天早点下班教程!阿里云/本地部署OpenClaw、AI职场提效用法、大模型配置全手册
很多人安装 OpenClaw 后只停留在聊天、试玩功能,却忽略了它最核心的价值——**直接操作电脑、自动完成繁琐工作、7×24 小时充当隐形助理**。它与普通AI最大的区别在于:可以直接读写文件、执行命令、整理资料、生成文案、定时任务、远程控制,把职场人最耗时间的重复劳动全部接管。本文只讲真实可落地、每天都能用的职场效率用法,同时提供 2026 年阿里云、MacOS、Linux、Windows11 完整部署流程,以及阿里云百炼 Coding Plan 免费大模型 API 配置,所有命令可直接复制运行,无多余表述,适合所有职场人士快速上手。
812 0
|
12月前
|
算法 数据可视化
二叉树的递归/层序遍历 递归遍历(DFS)
本文详解二叉树的遍历框架,涵盖递归遍历的固定访问顺序及前、中、后序的本质区别——即代码在递归函数中的位置不同所致。同时深入讲解层序遍历(BFS)的三种实现方式,适用于不同场景,尤其适合求最短路径问题;而DFS则因结构天然适合搜索所有路径。通过实例对比,阐明BFS与DFS的应用偏好及原理依据。
二叉树的递归/层序遍历 递归遍历(DFS)
|
API 开发者 索引
Python中的省略号(Ellipsis)赋值方式
在Python中,省略号(`...`)是一种特殊对象,称为Ellipsis,虽不常用但在特定场景下非常实用,如函数占位、未实现方法示例及NumPy数组处理。本文通过示例介绍`a = ...`的用法。省略号类似于`None`,可用作代码结构的占位符,保持代码完整性和可读性,同时在API设计中标识待实现的方法。特别是在NumPy中,省略号用于表示多维数组的剩余维度,简化数组操作,提高代码灵活性和可读性。掌握这一技巧有助于提升Python编程能力。
810 1
|
容器
力扣206反转链表:代码实现+图文全解+方法总结(四种方法)
力扣206反转链表:代码实现+图文全解+方法总结(四种方法)
681 0

热门文章

最新文章