libra测试环境搭建

1、搭建前准备

1、所需系统:linux(Red Hat 或Debian) 或者 macOs系统
2、git环境
3、linux系统需要:yum或者 apt-get; macOs系统需要Homebrew

2、基础环境搭建

以下 基于参考 https://developers.libra.org/docs/my-first-transaction 来搭建 Libra 环境并连接到测试网络。

1、下载 Libra

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

2、Libra依赖软件安装

cd libra
./scripts/dev_setup.sh

这里主要涉及到Rust、CMake、Golang、Protobuf的依赖环境的安装。

安装正确显示如下:

Installing CMake......
CMake is already installed
Installing Go......
Go is already installed
Installing Protobuf......
Protobuf is already installed
 
Finished installing all dependencies.
 
You should now be able to build the project by running:
    source /root/.cargo/env
    cargo build

若上述环境安装存在某一步的失败,可自行下载安装,以免报错。

  • 安装 golang环境:
下载golang:
wget https://studygolang.com/dl/golang/go1.12.5.linux-amd64.tar.gz
tar -xvf go1.12.5.linux-amd64.tar.gz
 
 
修改环境变量:
vim /etc/profile
#添加
export GOROOT=/usr/go
export GOPATH=/usr/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
 
 
查看安装结果
go version
  • 安装 Rust 环境
curl https://sh.rustup.rs -sSf | sh
source $HOME/.cargo/env
rustc --version 
  • 安装 cmake
在官网下载 cmake :https://cmake.org/download/
tar -xvzf cmake-3.15.0-rc2.tar.gz
cd cmake-3.15.0-rc2/
./bootstrap
gmake
gmake install
  • 安装 protocol
wget https://github.com/protocolbuffers/protobuf/releases/tag/v3.6.1
tar -xvf protobuf-all-3.8.0.tar.gz
cd protobuf-3.8.0/
./configure
make
make check
sudo make install
protoc --version

3、依赖包下载,并编译

./scripts/cli/start_cli_testnet.sh

这里会安装 libra的依赖包,主要是从github下载的依赖包,所以下载会比较慢,且持续几个小时,故最好在半夜让它自行下载。

下载且启动成功显示如下,即链接到测试网:

Finished dev [unoptimized + debuginfo] target(s) in 178m 42s
     Running `target/debug/client --host ac.testnet.libra.org --port 8000 -s ./scripts/cli/trusted_peers.config.toml`
Connected to validator at: ac.testnet.libra.org:8000
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_in_micro_libras (default=0)] [max_gas_amount_in_micro_libras (default 10000)] Suffix 'b' is for blocking.
    Transfer coins (in libra) from account to another.
help | h
    Prints this help
quit | q!
    Exit this client
 
 
Please, input commands:
 
libra%
libra% exit
Unknown command: "exit"
libra% q!

实际运行指令:

target/debug/client --host ac.testnet.libra.org --port 8000 -s ./scripts/cli/trusted_peers.config.toml

3、测试运行

1、创建账户及账号状态

创建账户 Alice(account #0)、Bob(account #1)

libra% account create
>> Creating/retrieving next account from wallet
Created/retrieved account #0 address 68eaf88db7a25d533066b363ba82af9edda19765bb2072051fd8c09fef01ccdb
libra%  account create
>> Creating/retrieving next account from wallet
Created/retrieved account #1 address 6dcc4079a3164975ae408608b7a72d5a382c1a7a857dcace6d7995f36cf4c5ae

通过 account list 查看内容:

User account index: 0, address: 68eaf88db7a25d533066b363ba82af9edda19765bb2072051fd8c09fef01ccdb, sequence number: 1, status: Persisted
User account index: 1, address: 6dcc4079a3164975ae408608b7a72d5a382c1a7a857dcace6d7995f36cf4c5ae, sequence number: 0, status: Persisted

给Alice(account #0)、Bob(account #1)发放币:

libra% account mint 0 110
>> Minting coins
Mint request submitted
libra% account mint 1 52
>> Minting coins
Mint request submitted

检查下 account 0、1 的余额:

libra% query balance 0
Balance is: 110.000000
libra% query balance 1
Balance is: 52.000000

查看账户序列:

libra% query sequence 0
>> Getting current sequence number
Sequence number is: 1
libra% query sequence 1
>> Getting current sequence number
Sequence number is: 0

2、账号交易

我们转移 10 个 Libra coin 从 Alice(account #0)、Bob(account #1):

transfer 0 1 10

  • 0 是 Alice (account #0)的帐户的索引。
  • 1 是 Bob(account #1) 的帐户索引。
  • 10 是从 Alice 的账户转移到 Bob 的账户的 Libra 的数量。
libra% transfer 0 1 10
 
>> Transferring
Transaction submitted to validator
To query for transaction status, run: query txn_acc_seq 0 1 <fetch_events=true|false>

3、查询交易信息

我们可以查询每个账号下每个序列下每笔交易
query txn_acc_seq 0 0 true

  • 0 是 Alice (account #0)的帐户的索引。
  • 0 是 Alice的第一个序列的交易。
libra% query txn_acc_seq 0 0 true
>> Getting committed transaction by account and sequence number
Committed transaction: SignedTransaction {
 raw_txn: RawTransaction {
    sender: 68eaf88db7a25d533066b363ba82af9edda19765bb2072051fd8c09fef01ccdb,
    sequence_number: 0,
    payload: {,
        transaction: peer_to_peer_transaction,
        args: [
            {ADDRESS: 6dcc4079a3164975ae408608b7a72d5a382c1a7a857dcace6d7995f36cf4c5ae},
            {U64: 10000000},
        ]
    },
    max_gas_amount: 10000,
    gas_unit_price: 0,
    expiration_time: 1561688884s,
},
 public_key: 12f96f97d95fc05cb4a247af17f30ed014feb6906ef37638bef70a71c447ef08,
 signature: Signature( R: CompressedEdwardsY: [37, 189, 180, 86, 82, 230, 138, 19, 226, 181, 249, 64, 121, 36, 195, 246, 184, 100, 162, 81, 169, 146, 140, 107, 231, 48, 140, 148, 57, 39, 1, 169], s: Scalar{
    bytes: [151, 173, 147, 159, 210, 41, 38, 158, 171, 73, 231, 223, 95, 211, 253, 121, 45, 230, 159, 144, 80, 54, 216, 238, 69, 238, 121, 40, 254, 203, 255, 14],
} ),
 }
Events:
ContractEvent { access_path: AccessPath { address: 68eaf88db7a25d533066b363ba82af9edda19765bb2072051fd8c09fef01ccdb, type: Resource, hash: "217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc97", suffix: "/sent_events_count/" } , index: 0, event_data: AccountEvent { account: 6dcc4079a3164975ae408608b7a72d5a382c1a7a857dcace6d7995f36cf4c5ae, amount: 10000000 } }
ContractEvent { access_path: AccessPath { address: 6dcc4079a3164975ae408608b7a72d5a382c1a7a857dcace6d7995f36cf4c5ae, type: Resource, hash: "217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc97", suffix: "/received_events_count/" } , index: 0, event_data: AccountEvent { account: 68eaf88db7a25d533066b363ba82af9edda19765bb2072051fd8c09fef01ccdb, amount: 10000000 } }

4、查询账号状态

query account_state 0

  • 0 是 Alice (account #0)的帐户的索引。
>> Getting latest account state
Latest account state is:
 Account: 68eaf88db7a25d533066b363ba82af9edda19765bb2072051fd8c09fef01ccdb
 State: Some(
    AccountStateBlob {
     Raw: 0x010000002100000001217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc97440000002000000068eaf88db7a25d533066b363ba82af9edda19765bb2072051fd8c09fef01ccdb00e1f50500000000010000000000000002000000000000000200000000000000
     Decoded: Ok(
        AccountResource {
            balance: 100000000,
            sequence_number: 2,
            authentication_key: 0x68eaf88db7a25d533066b363ba82af9edda19765bb2072051fd8c09fef01ccdb,
            sent_events_count: 2,
            received_events_count: 1,
        },
    )
     },
)
 Blockchain Version: 266988

4、运行本地验证节点

以上我们是加入了libra的测试网络,我们也可以自行搭建自己的本地区块链网络。

切换到Libra Core存储库的根目录,并运行libra_swarm如下所示:

以下首先是编译过程,会占用大量的机器资源,需要谨慎操作。

cd ~/libra
cargo run -p libra_swarm -- -s

-p libra_swarm 使cargo运行libra_swarm包,该包启动由一个节点组成的本地区块链。

-s 启动本地客户端以连接到本地区块链

编译成功及运行起来后,会提示CLI客户端菜单和libra%提示。

5、Libra资源整理

Libra 官网: https://libra.org/zh-CN/
Libra 白皮书: https://libra.org/zh-CN/white-paper/
Libra 技术白皮书: https://developers.libra.org/docs/assets/papers/the-libra-blockchain.pdf
Libra 开发者技术文档:https://developers.libra.org/
Libra Github: https://github.com/libra/libra
发布了72 篇原创文章 · 获赞 52 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/niyuelin1990/article/details/94205368