LOADING...
LOADING...
LOADING...
当前位置: 玩币族首页 > 新闻观点 > 在Facebook Libra上的实现第一笔交易

在Facebook Libra上的实现第一笔交易

2019-07-10 不详 来源:网络
Libra是Facebook 在 6/18 发表的区块链解决方案。提出 LibraCoin 加密货币来为金融基础建设赋能。以 Facebook 与这次参与的合作伙伴,我相信在未来能让支付、跨国跨境金融交易都能更加地便利。

本篇文章将带着大家一起进行 Libra 的教学,如何在 Libra 上面发送自己的第一笔交易吧!

Agenda

· 环境设置
· 下载与编译 Libra
· 连上 Libra testnet
· 建立自己的 Libra account 与透过水龙头 (Faucet) 取得 LibraCoin
· 查询 Libra account 的余额
· 发送 LibraCoin
· 查询发送的 transaction

环境设置


目前 Libra 只支持 Linux 与MacOS 两种环境,以下将以 Linux Ubuntu 18.04 作为示范。
Environment: Ubuntu 18.04 on AWS EC2t3.xlarge

# 更新一下 apt 与安装 git,其他的环境 Libra 有提供他的 script 来安装。
sudo apt update
sudo apt upgrade
sudo apt install git

下载与编译Libra

第一步:下载最新的Libra存储库

$ git clone https://github.com/libra/libra.git && cd libra
Cloning into 'libra'...
remote: Enumerating objects: 133, done.
remote: Counting objects: 100% (133/133), done.
remote: Compressing objects: 100% (105/105), done.
remote: Total 1717 (delta 40), reused 55 (delta 25), pack-reused 1584
Receiving objects: 100% (1717/1717), 1.89 MiB | 1.88 MiB/s, done.
Resolving deltas: 100% (340/340), done.

第二步: 安装 Libra Core 所需的 dependencies

$ ./scripts/dev_setup.sh
Welcome to Libra!
This script will download and install the necessary dependencies
needed to
build Libra Core. This includes:
* Rust (and the necessary components, e.g. rust-fmt, clippy)
* CMake, protobuf, go (for building protobuf)
If you'd prefer to install these dependencies yourself, please
exit this script
now with Ctrl-C.
Proceed with installing necessary dependencies (y) >
...
...
Installing CMake......
CMake is already installed
Installing Go......
Go is already installed
Installing Protobuf......
Protobuf is already installed

Step2-optional. 在安装 dependencies 的过程间我有遇到 protoc 安装失败,原因是 18.04 的 apt 上面的 protobuf 版本是 3.0.0,而 Libra 所需的版本为 >= 3.6.0 ,所以导致他安装失败。这时我们就需要手动安装最新版本的 protobuf 来解决这个问题。

sudo apt install unzip
PROTOC_ZIP=protoc-3.8.0-linux-x86_64.zip
curl -OL https://github.com/google/protobuf/releases/download/v3.8.0/$PROTOC_ZIP
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local include/*
rm -f $PROTOC_ZIP

第三步:编译并执行 Libra Cli 来连上 testnet

$ ./scripts/cli/start_cli_testnet.sh
Building and running client in debug mode.
Updating crates.io index
Updating git repository `https://github.com/pingcap/rust-rocksdb.git`
Updating git repository `https://github.com/alexcrichton/bzip2-rs.git`
Updating git repository `https://github.com/busyjay/lz4-rs.git`
Updating git repository `https://github.com/busyjay/rust-snappy.git`
Updating git repository `https://github.com/gyscos/zstd-rs.git`
Compiling proc-macro2 v0.4.30
Compiling unicode-xid v0.1.0
Compiling syn v0.15.36
...
...
...
Finished dev [unoptimized + debuginfo] target(s) in ...s
Running `target/debug/client --host ac.testnet.libra.org --port 80 -s ./scripts/cli/trusted_peers.config.toml`
Connected to validator at: ac.testnet.libra.org:80
usage: <command> <args>
Use the following commands:
account | a
Account operations
query | q
Query operations
transfer | transferb | t | tb
<sender_account_address>|<sender_account_ref_id> <receiver_account_address>|<receiver_account_ref_id> <number_of_coins> [gas_unit_price (default=0)] [max_gas_amount (default 10000)] Suffix 'b' is for blocking.
Transfer coins from account to another.
help | h
Prints this help
quit | q!
Exit this client
Please, input commands:
libra%

看到 libra% 出现就代表我们已经大功告成连上 testnet 了!

连接testnet

其实跟编译 libra cli 是同一个 script ,除非原始码有更动,他就不会浪费时间在重复编译上了。

$ ./scripts/cli/start_cli_testnet.sh
Building and running client in debug mode.
...
...
...
...
Finished dev [unoptimized + debuginfo] target(s) in ...s
Running `target/debug/client --host ac.testnet.libra.org --port 80 -s ./scripts/cli/trusted_peers.config.toml`
Connected to validator at: ac.testnet.libra.org:80
usage: <command> <args>
Use the following commands:
account | a
Account operations
query | q
Query operations
transfer | transferb | t | tb
<sender_account_address>|<sender_account_ref_id> <receiver_account_address>|<receiver_account_ref_id> <number_of_coins> [gas_unit_price (default=0)] [max_gas_amount (default 10000)] Suffix 'b' is for blocking.
Transfer coins from account to another.
help | h
Prints this help
quit | q!
Exit this client
Please, input commands:
libra%

建立自己的Libra account

所有跟 account 相关的功能都在 account 下面,我们可以先下 account 这个指令来看看有哪些功能可以使用吧!

libra% account
usage: account <arg>
Use the following args for this command:
create | c
Create an account. Returns reference ID to use in other operations
list | la
Print all accounts that were created or loaded
recover | r <file_path>
Recover Libra wallet from the file path
write | w <file_path>
Save Libra wallet mnemonic recovery seed to disk
mint | mintb | m | mb <receiver_account_ref_id>|<receiver_account_address> <number_of_coins>
Mint coins to the account. Suffix 'b' is for blocking

它主要有五大功能:

· Create: 建立一个账号,每个账号在 local 会给一个 reference ID,举例来说,第一个被建立的账号就是 ID=0。

libra% account create
>> Creating/retrieving next account from wallet
Created/retrieved account #0 address
fafbbc8aeed4b019ee8d1be854aa5279b26a07d5957b3a5a04e9aaa351bea2f0
# 可以看到我建立的第一个账号,他的 reference ID #0
# 以及我的 address 就是下面那一大串 hex string
# 如果是在同一个 libra cli 操作,我们都可以使用 reference ID 作为账号的
alias。如果是要请别人从他的 libra cli 发 transaction 给你的账号,
那就需要直接给对方你的 address 了。

· List: 印出所有被建立或者被加载的账号。

libra% account list
User account index: 0, address:
fafbbc8aeed4b019ee8d1be854aa5279b26a07d5957b3a5a04e9aaa351bea2f0,
sequence number: 0, status: Persisted
# 列出现有的账号。Sequence number 则是代表了这个 account
发起过几个 transaction。

· Recover: 从一个文件还原一个钱包账号。像是 import wallet。
· Write: 将钱包账号储存到一个档案。像是 export wallet。
· Mint: 请水龙头 (Faucet) 发钱给一个指定的账号。

# 请水龙头发钱的指令是
# account mint <account address/ref_id> <number_of_coin>
# 以下示范发 320 颗 Libra Token 给第一个账号。
libra% account mint 0 320
>> Minting coins
Mint request submitted

· Hint: 跟 transaction 相關的操作(mint/transfer)都有提供 blocking 與 non-blocking 的版本。想要使用 blocking 的版本,比如说要等水龙头真的发钱给你,那我们可以使用下面这种语法:

libra% account mintb 1 1205
>> Minting coins
[waiting
Transaction completed, found sequence number 9357
Finished minting!

他就会直接卡住并印出 waiting 直到这个 Transaction 被完成为止。

查询Libra account的余额

语法是 query balance <account address/ref_id>

libra% query balance 0
Balance is: 320

发送Libra币

转账的语法如下:

transfer <sender address/ref_id> <receiver address/ref_id>
<number_of_coins> [gas_unit_price (default=0)]
[max_gas_amount (default 10000)]

tranfer 一共有五个参数:

1. Sender Account 的 address 或 reference id
2. Receiver Account 的 address 或 reference id
3. 要发送多少数量的 libra
4. 这次 transaction 的 gas 手续费,预设是 0。这个参数可以不给。
5. 这次 transaction 的最高 gas 限制,预设是 10000。这个参数可以不给。

举例而言,如果我想从Account #0 转 100Libra 给Account #1,我可以这样写 transfer 0 1 100 。

查询发送的transaction

查询交易的指令为:

query txn_acc_seq <account address/ref_id> <sequence_number>
<fetch_events=true|false>

一共有三个参数:

1. Account 的 address 或 reference ID。

2. Sequence Number: 在 Libra 的设计中,sequence number 代表了该 account 发起了几个交易,每发起一次交易且被收进 Libra blockchain 中这个数字就会 +1 。这个设计是为了避免 replay 攻击,每个发送出去的transaction 带有当前 account 的 sequence number ,而发送出去的 transaction 会先被放入 mempool 中等待被执行。当 Libra blockchain 从 mempool 中拿 transaction 出来执行的时候会去比对当前的 account sequence number 是否跟 transaction 的 sequence number 相同。若相同才能执行,且马上会把 account 的 sequence number +1 ,这样即使在 mempool 中有多个重复的 transaction 也只会有一个合法的能被执行,其他的将会被丢弃。

3. 是否需要抓取 events。

完整的情景案例
在发送 LibraCoin 之前,我们需要有两个账号来进行转账。以下将示范从建立两个账号、各自从水龙头领到 LibraCoin 以及转账的过程吧!

# 建立第一个账号 alice
libra% account create
>> Creating/retrieving next account from wallet
Created/retrieved account #0 address
7faff73ba54fc82503ecd6438455e8a4ab0a26dc35d399e6593bb1058338e140
# 建立第二个账号 bob
libra% account create
>> Creating/retrieving next account from wallet
Created/retrieved account #1 address
bf2df78de21ea3727e0d48ae7142718f8c33f7eca854ac81b8866361fa2bd15d
# 帮 alice 要 320 个 libra
libra% account mint 0 320
>> Minting coins
Mint request submitted
# 查询 alice 的 balance
libra% query balance 0
Balance is: 320
# 帮 bob 要 1205 个 libra
libra% account mint 1 1205
>> Minting coins
Mint request submitted
# 查询 bob 的 balance
libra% query balance 1
Balance is: 1205
# 从 alice 转 20 个 libra 给 bob
libra% transfer 0 1 20
>> Transferring
Transaction submitted to validator
To query for transaction status, run: query txn_acc_seq 0 0
<fetch_events=true|false>
# 查询 transaction
libra% query txn_acc_seq 0 0 true
>> Getting committed transaction by account and sequence number
Committed transaction: SignedTransaction {
raw_txn: RawTransaction {
sender:
7faff73ba54fc82503ecd6438455e8a4ab0a26dc35d399e6593bb1058338e140,
sequence_number: 0,
payload: {,
transaction: peer_to_peer_transaction,
args: [
{ADDRESS:
bf2df78de21ea3727e0d48ae7142718f8c33f7eca854ac81b8866361fa2bd15d},
{U64: 20000000},
]
},
max_gas_amount: 10000,
gas_unit_price: 0,
expiration_time: 1560912163s,
},
public_key:
a2b9f68d1abbc887a39789a7f428df68adabfff395fff71bd0d0fe5674bdfbad,
signature: Signature( R: CompressedEdwardsY:
[74, 249, 58, 185, 235, 16, 175, 193, 158, 86, 31, 249, 215, 97,
138, 153, 65, 183, 161, 229, 162, 198, 30, 107, 246, 191, 44, 250,
12, 96, 20, 55], s: Scalar{
bytes: [170, 99, 70, 224, 9, 167, 230, 162, 110, 38, 87, 53, 164,
36, 91, 34, 116, 52, 73, 91, 169, 141, 159, 194, 40, 206, 16, 151,
5, 101, 91, 1],
} ),
}
Events:
ContractEvent { access_path: AccessPath { address:
7faff73ba54fc82503ecd6438455e8a4ab0a26dc35d399e6593bb1058338e140,
type: Resource, hash:
"217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc97",
suffix: "/sent_events_count/" } , index: 0, event_data: AccountEvent {
account: bf2df78de21ea3727e0d48ae7142718f8c33f7eca854ac81b8866361fa2b
d15d, amount: 20000000 } }
ContractEvent { access_path: AccessPath { address:
bf2df78de21ea3727e0d48ae7142718f8c33f7eca854ac81b8866361fa2bd15d,
type: Resource, hash: "217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647
c78df00b371b25cc97", suffix: "/received_events_count/" } ,
index: 0, event_data: AccountEvent {
account: 7faff73ba54fc82503ecd6438455e8a4ab0a26dc35d399e6593bb1058338
e140, amount: 20000000 } }

常见错误

Libra testnet 有挡一个区间内的爆量 request,如果你在短时间内送一堆 transaction (mint or transfer) 有机会拿到下面这个 error:

[ERROR] Error minting coins: Failed to query remote faucet server
[status=429 Too Many Requests]: "<html><body><h1>429 Too Many Requests
</h1>\nYou have sent too many requests in a given amount of time.\n
</body></html>\n"

这时候只要稍等一下再重新送那笔 transaction 就能解除了。

—-

编译者/作者:不详

玩币族申明:玩币族作为开放的资讯翻译/分享平台,所提供的所有资讯仅代表作者个人观点,与玩币族平台立场无关,且不构成任何投资理财建议。文章版权归原作者所有。

LOADING...
LOADING...