Libra环境搭建和第一笔交易

环境准备

  • Linux或者Mac OS系统,本次测试使用CentOS 7.5
  • 稳定的网络环境,并且连接到互联网
  • 提前安装Git
  • 如果是MacOS,需要提前安装Homebrew
  • yum 或者 apt-get命令

踩的坑:

  • 安装包:yum install -y git gcc gcc-c++ ncurses-devel bison
  • CMake版本低(2.8)导致编译错误,升级到3.14.4解决

安装Libra Core

克隆代码库

1
git clone https://github.com/libra/libra.git

运行安装脚本,安装依赖

1
2
cd libra
./scripts/dev_setup.sh

该初始化脚本执行了如下动作:

  • 安装rustup – rustup是Rust官方的跨平台安装工具器, Libra Core使用Rust来实现
  • 安装rust-toolchain的必要版本
  • 安装CMake – 管理编译和构建
  • 安装protoc – protocol buffers的编译器
  • 安装Go – 为了构建protocol buffers使用

构建Libra CLI客户端并连接到测试网络

执行客户端脚本来连接到测试网络上的节点,时间较长,需耐心等待。。。

1
./scripts/cli/start_cli_testnet.sh

一旦连接到网络会看到如下输出信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Connected to validator at: ac.testnet.libra.org:8000
usage: <command> <args>

Use the following commands:

account | a
trueAccount operations
query | q
trueQuery operations
transfer | transferb | t | tb
true<sender_account_address>|<sender_account_ref_id> <receiver_account_address>|<receiver_account_ref_id> <number_of_coins> [gas_unit_price_in_micro_libras (default=0)] [max_gas_amount_in_micro_libras (default 10000)] Suffix 'b' is for blocking.
trueTransfer coins (in libra) from account to another.
help | h
truePrints this help
quit | q!
trueExit this client


Please, input commands:

libra%

使用help可以看到命令列表,可以执行的命令如下:

  • account – 账户相关操作
    • create – 创建账户
    • list – 查看所有账户列表
    • mint – 增加libra币
    • write <file_path> – 保存libra钱包到磁盘
    • recover <file_path> – 从磁盘恢复libra钱包
  • query – 查询操作
    • balance <account_ref_id>|<account_address> – 查询账户余额
    • sequence – 获得账户当前sequence number
    • account_state – 获取账户最近状态
    • txn_acc_seq – 通过账号或者sequence number获取已提交的交易
    • txn_range – 通过version范围获取已提交的交易
    • event – 通过账号或者事件类型来获取事件列表
  • transfer – 转账操作
    • transfer – 从一个账户转libra币到另外一个账户
    • transferb – transfer的阻塞式操作
  • quit – 退出连接

创建账户

创建账户A

1
2
3
libra% account create
>> Creating/retrieving next account from wallet
Created/retrieved account #0 address 1b7f0e3123d52cd4b80d451da785a1db96a5ec6a9cdcd84d5d344341b8e77158

创建账户B

1
2
3
libra% account create
>> Creating/retrieving next account from wallet
Created/retrieved account #1 address fc5fecde4dee37102a49085725f5d4491d9e9cda00afba078988eba0696c4691

列出本地所有账户信息:

1
2
3
libra% account list
User account index: 0, address: 1b7f0e3123d52cd4b80d451da785a1db96a5ec6a9cdcd84d5d344341b8e77158, sequence number: 0, status: Local
User account index: 1, address: fc5fecde4dee37102a49085725f5d4491d9e9cda00afba078988eba0696c4691, sequence number: 0, status: Local

A和B只是为了方便书面称谓,Libra目前不支持给账户命名,但如上A对应编号为0的账户,B对应编号为1的账户。地址后面的 sequence 指的是这个账户主动发起转账的次数,目前两个账户都还没有发起过转账,所以都是 0。

给账户铸币

给A账户铸币1000

1
2
3
libra% account mint 0 1000
>> Minting coins
Mint request submitted

可能会由于网络原因失败

1
2
3
4
libra% account mint 0 1000
>> Minting coins

[ERROR] Error minting coins: error trying to connect: failed to lookup address information: Name or service not known

给B账户铸币50

1
2
3
libra% account mint 1 50
>> Minting coins
Mint request submitted

查询账户余额

1
2
3
4
5
libra% query balance 0
Balance is: 1000

libra% query balance 1
Balance is: 50

发起转账交易

查询账户的sequence,由于两个账户都没有交易记录,所以都是0

1
2
3
4
5
6
libra% query sequence 0
>> Getting current sequence number
Sequence number is: 0
libra% query sequence 1
>> Getting current sequence number
Sequence number is: 0

从A给B转账100

1
2
3
4
libra% transfer 0 1 100
>> Transferring
Transaction submitted to validator
To query for transaction status, run: query txn_acc_seq 0 0 <fetch_events=true|false>

查询账户余额:

1
2
3
4
libra% query balance 0
Balance is: 900
libra% query balance 1
Balance is: 150

再次从A给B转账100

1
2
3
4
5
libra% transferb 0 1 100
>> Transferring
[waiting Transaction completed, found sequence number 2]
Finished transaction!
To query for transaction status, run: query txn_acc_seq 0 1 <fetch_events=true|false>

再查询账户余额:

1
2
3
4
libra% query balance 1
Balance is: 250
libra% query balance 0
Balance is: 800

查询A和B账户的sequence, A的账户sequence number是2表示A发起了两个交易,B账户的sequence number是0表示B账户没有发起交易

1
2
3
4
5
6
libra% query sequence 0
>> Getting current sequence number
Sequence number is: 2
libra% query sequence 1
>> Getting current sequence number
Sequence number is: 0

可以使用 query txn_acc_seq 0 0 true查询详细的转账信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
libra% query txn_acc_seq 0 0 true
>> Getting committed transaction by account and sequence number
Committed transaction: SignedTransaction {
raw_txn: RawTransaction {
truesender: 1b7f0e3123d52cd4b80d451da785a1db96a5ec6a9cdcd84d5d344341b8e77158,
truesequence_number: 0,
truepayload: {,
truetruetransaction: peer_to_peer_transaction,
truetrueargs: [
truetruetrue{ADDRESS: fc5fecde4dee37102a49085725f5d4491d9e9cda00afba078988eba0696c4691},
truetruetrue{U64: 100000000},
truetrue]
true},
truemax_gas_amount: 10000,
truegas_unit_price: 0,
trueexpiration_time: 1561190218s,
},
public_key: 2a9c5aa44975912b7eda3bc9df6047d19dd5f4937404bade1f922acce9cab42c,
signature: Signature( R: CompressedEdwardsY: [203, 171, 64, 238, 247, 224, 54, 137, 221, 158, 125, 30, 230, 58, 66, 151, 147, 23, 112, 148, 192, 216, 201, 210, 123, 53, 94, 96, 129, 180, 182, 126], s: Scalar{
truebytes: [71, 53, 181, 160, 59, 152, 34, 26, 218, 59, 72, 202, 235, 202, 104, 248, 52, 52, 53, 191, 171, 105, 222, 117, 50, 57, 155, 35, 206, 52, 185, 14],
} ),
}
Events:
ContractEvent { access_path: AccessPath { address: 1b7f0e3123d52cd4b80d451da785a1db96a5ec6a9cdcd84d5d344341b8e77158, type: Resource, hash: "217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc97", suffix: "/sent_events_count/" } , index: 0, event_data: AccountEvent { account: fc5fecde4dee37102a49085725f5d4491d9e9cda00afba078988eba0696c4691, amount: 100000000 } }
ContractEvent { access_path: AccessPath { address: fc5fecde4dee37102a49085725f5d4491d9e9cda00afba078988eba0696c4691, type: Resource, hash: "217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc97", suffix: "/received_events_count/" } , index: 0, event_data: AccountEvent { account: 1b7f0e3123d52cd4b80d451da785a1db96a5ec6a9cdcd84d5d344341b8e77158, amount: 100000000 } }

Over!