查看原文
其他

Libra 的 Move 语言初探,10 行代码实现你第一个智能合约

马超 CSDN 2020-10-29

作者 | 马超
责编 | 夕颜
出品 | CSDN(ID:CSDNnews)

Libra是Facebook新推出的虚拟加密货币,而Move是为Libra设计的全新编程语言,据称可以在Libra区块链中实现自定义交易逻辑和‘智能合约”。本文就为各位读者介绍一下有关使用move语言编写智能合约的方案。


Move语言最主要的特性


可编程的Move交易脚本


  • 每一个Libra区块链上交易都包含 Move交易脚本 用来对交易逻辑的编码,同时验证器据此验证客户端的行为(例如,将Libra币从Alice的帐户移动到Bob的帐户)。

  • 交易脚本通过调用一个或者多个Move模块的过程和Libra区块链全局存储中发布的 Move资源 进行交互。

  • 交易脚本不存储在区块链的全局状态中,其他的交易脚本也无法调用它,这是一次性程序。


可组合智能合约的Move模块


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


  • 模块声明两种结构类型(包括资源,这是一种特殊的结构)和过程。

  • Move模块的过程定义了创建,访问和销毁它声明的类型的规则。

  • 模块可重复使用。在一个模块中声明的结构类型可以使用在另一个模块中,并且在一个模块中声明的过程可以在另一个模块中声明的公共过程中调用。模块可以调用其他Move模块中声明的过程。交易脚本可以调用已发布模块的任何公共过程。

  • 最终,Libra用户将能够在自己的帐户下发布模块。


Move的一等资源


  • Move的关键功能是能自定义资源类型。资源类型通过编码具有丰富可编程性和安全性。

  • 资源是语言中的普通类型值。它们可以存储为数据结构,作为参数传递给过程,从过程返回,等等。

  • Move的系统为资源提供特殊的安全性保证。Move资源永远不会被复制,重用或丢弃。资源类型只能由定义类型的模块创建或销毁。这些保证由Move虚拟机 通过字节码静态验证,Move虚拟机将拒绝运行未通过字节码验证的程序代码。

  • Libra货币实现为名为 LibraCoin.T 的资源类型。LibraCoin.T 在语言中没有特殊的地位; 每个Move资源都享有相同的保护。


笔者对于Move语言对于move语言的解读


一、Move是一种静态类型语言,也就是强类型语言,在这方面Move与Java和C类似,将问题的出现更多放在编译时暴露,降低了在运行时出现crash的机率。


二、Move虚拟机通过字节码(我们一会儿可以看到)保证其资源保持first-class的地位,move拒绝执行未通过字节码验证的程序。也就是资源只能由定义的模块创建或者销毁。其它模块没有均无权限,这样也就避免了类似于以太坊solid多指针指向同一资源可能带来的安全漏洞和其它问题。


第一个智能合约


一、搭建Move的编译环境,通过以下命令


cd ~/libracargo build --bin compiler

并有如下返回,代码编译成功


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


好了,话不多说,我们直接来看我们的第一个合约长什么样子。注:以下代码直接转自libra的官网https://learnblockchain.cn


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


二、编译程序


新一个文件将上述代码拷到你的程序当中。笔者以test目录下的trans.m为例


mkdir testcd testgedit trans.m #贴粘上述代码到这个文件


1. 在Libra的client进行编译


~/libra/scripts/cli/start_cli_testnet.sh#进行到libra的命令行后执行以下命令dev c /opt/libra/test/trans.m /opt/libra/test/trans.ir


2. 也可以使用命令行进行编译


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


3. 查看生成的ir文件,我们看到其编译后的文件内容如下,这也就是我们刚刚所说的二进制码,其主要是应用为安全考虑,如果无权模块是不能改动资源的。


cat /opt/libra/test/trans.ir{"code":[76,73,66,82,65,86,77,10,1,0,8,1,83,0,0,0,6,0,0,0,2,89,0,0,0,4,0,0,0,3,93,0,0,0,9,0,0,0,12,102,0,0,0,22,0,0,0,13,124,0,0,0,9,0,0,0,5,133,0,0,0,66,0,0,0,4,199,0,0,0,32,0,0,0,7,231,0,0,0,21,0,0,0,0,0,0,1,0,2,2,4,1,0,0,3,0,1,5,1,1,6,2,2,0,2,4,2,0,2,1,7,0,0,1,2,0,2,0,2,4,7,0,0,0,3,0,3,3,4,2,7,0,0,6,60,83,69,76,70,62,12,76,105,98,114,97,65,99,99,111,117,110,116,9,76,105,98,114,97,67,111,105,110,4,109,97,105,110,1,84,20,119,105,116,104,100,114,97,119,95,102,114,111,109,95,115,101,110,100,101,114,7,100,101,112,111,115,105,116,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,1,7,0,12,1,17,1,0,13,2,12,0,12,2,17,2,0,2],"args":[],"modules":[]}


原文链接:

https://blog.csdn.net/BEYONDMA/article/details/98501624


【END】


更多精彩推荐

克隆一个 AI 替自己开会,爽吗?

☞为什么大厂都在用 GO 语言?读透 GO 语言的切片

☞饿了么交易系统 5 年演化史

北京四环堵车引发的智能交通大构想

从Ngin到Pandownload,程序员如何避免面向监狱编程?

☞从Web1.0到Web3.0:详析这些年互联网的发展及未来方向


你点的每个“在看”,我都认真当成了喜欢

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存