作者 | 马超
责编 | 夕颜
出品 | CSDN(ID:CSDNnews)
Libra是Facebook新推出的虚拟加密货币,而Move是为Libra设计的全新编程语言,据称可以在Libra区块链中实现自定义交易逻辑和‘智能合约”。本文就为各位读者介绍一下有关使用move语言编写智能合约的方案。
Move语言最主要的特性
可编程的Move交易脚本
可组合智能合约的Move模块
Move模块(Modules)定义了更新 Libra 区块链全局状态的规则。这些模块与其他区块链系统中与智能合约相同。模块声明可以在用户帐户下发布的 资源 类型。Libra 区块链中的每个帐户都是一个容器,可以容纳任意数量的资源和模块。
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
推荐阅读
老铁们求在看!👇