Libra开发(一)Ubuntu下编译libra,并执行一笔交易

近期Facebook刚发布了区块链libra官网和白皮书,世界哗然,Libra构建的以美元为支撑挂钩一揽子货币将挑战各国主权货币,更可能在不久的将来,强势取代落后国家的主权货币。

Libra 的使命是建立一套简单的、无国界的货币和为数十亿人服务的金融基础设施。

从白皮书上看,Libra将创造一个新的去中心化区块链、一种低波动性加密货币和一个智能合约平台的计划,以期为负责任的金融服务创新开创新的机遇。

从技术上分析,Libra 区块链采用了基于 LibraBFT 共识协议的 BFT 机制来实现所有验证者节点就将要执行的交易及其执行顺序达成一致。这种方法可以在网络中建立信任,因为即使某些验证者节点(最多三分之一的网络)被破坏或发生故障,BFT共识协议的设计也能够确保网络正常运行。与其他一些区块链中使用的“工作量证明”机制相比,这类共识协议还可实现高交易处理量、低延迟和更高能效的共识方法。

我们今天就从编译一次libra,并执行一笔交易为开始。开启libra的探索之旅。
libra的源代码委托在github上,我们可以轻松获取他的源代码

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

编译Libra
要编译libra,我们可以先要切换到Libra目录并运行安装脚本以安装依赖项,如下所示:

cd libra
./scripts/dev_setup.sh

安装脚本执行以下操作:

  • 安装rustup - rustup是Rust编程语言的安装程序,实现了Libra Core。
  • 安装所需版本的rust-toolchain。
  • 安装CMake - 管理构建过程。
  • 安装protoc - 协议缓冲区的编译器。
  • 安装Go - 用于构建协议缓冲区。
    如果设置失败,请参阅故障排除。
    我的环境是ubuntu16.05,执行完成后,报这样的错误。
error: failed to run custom build command for `openssl-sys v0.9.48`

针对上面的错误,请安装最新版本的openssl

sudo apt install libssl-dev

然后再执行

source /home/cuijb/.cargo/env
cargo build

构建Libra CLI客户端并连接到Testnet

./scripts/cli/start_cli_testnet.sh

这们会连接到Libra Core的测试网络。其实我们可以在本地启动测试网络,并且直接连接到本地的测试网络,在libra目录下运行如下命令:

cargo run -p libra_swarm -- -s

显示如下:

    Finished dev [unoptimized + debuginfo] target(s) in 0.40s
     Running `/home/cuijb/libra/target/debug/libra_swarm -s`
Faucet account created in (loaded from) file "/tmp/keypair.o14NS4KREcvA/temp_faucet_keys"
Base directory containing logs and configs: Temporary(TempDir { path: "/tmp/.tmp3hm7Az" })
To run the Libra CLI client in a separate process and connect to the local cluster of nodes you just spawned, use this command:
        cargo run --bin client -- -a localhost -p 37637 -s "/tmp/.tmp3hm7Az/trusted_peers.config.toml" -m "/tmp/keypair.o14NS4KREcvA/temp_faucet_keys"
Loading client...
Connected to validator at: localhost:37637
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 100000)] Suffix 'b' is for blocking.
        Transfer coins (in libra) from account to another.
dev
        Local move development
help | h
        Prints this help
quit | q!
        Exit this client


Please, input commands:

libra%

如果你看到显示 libra%,说明客户端启动成功了

创建账户

libra% account c
>> Creating/retrieving next account from wallet
Created/retrieved account #0 address f2f94bfd087444db386cc69712f256beda6ecb0f4e30134efed772c80bd223ef

我们可以清楚看到,我们创建的第一个账户,#0,他的地址是:f2f94bfd087444db386cc69712f256beda6ecb0f4e30134efed772c80bd223ef

显示账户列表

libra% account la
User account index: 0, address: f2f94bfd087444db386cc69712f256beda6ecb0f4e30134efed772c80bd223ef, sequence number: 0, status: Local
Faucet account address: 0000000000000000000000000000000000000000000000000000000000000000, sequence_number: 0, status: Persisted

铸币
我们创建了两个账户,暂定为Alice和Bob,为两个账户铸币,不妨为Alice的账户铸币100LBR,为Bob铸币200LBR(LBR为Libra的原生货币符号)
给Alice创建100libra

libra% account mint 0 100
>> Minting coins 
Mint request submitted

给Bob创建200个 Libra

libra% account mint 1 200
>> Minting coins
Mint request submitted

查账户余额
查询Alice的余额

libra% q b 0
Balance is: 100.000000

查询Bob的余额

libra% q b 1
Balance is: 200.000000

查账户的序列号

查询Alice的序列号

libra% q s 0

>> Getting current sequence number
Sequence number is: 0

查询Bob的序列号

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

注意:Libra中的序列号和以太坊1.0中的nonce意思是一样的。

转帐
我们将Alice的50个libra转移给Bob。

libra% t 0 1 50
>> Transferring
Transaction submitted to validator
To query for transaction status, run: query txn_acc_seq 0 0 <fetch_events=true|false>

按账户index和序列号查询交易

libra% query txn_acc_seq 0 0 true
>> Getting committed transaction by account and sequence number
Committed transaction: SignedTransaction {
 raw_txn: RawTransaction {
        sender: 265b54817517c9a1efb872998817df7802eac3df3dd19e2e090338337ef74a78,
        sequence_number: 0,
        payload: {,
                transaction: peer_to_peer_transaction,
                args: [
                        {ADDRESS: 52593c9d2408d25b20e7a9693f7af1154c99800647f8b9e9ec8974c99b9efdcf},
                        {U64: 50000000},
                ]
        },
        max_gas_amount: 100000,
        gas_unit_price: 0,
        expiration_time: 1564650898s,
},
 public_key: 54d60a80458c06969481dbf22f689f80bcdad133a045b6b86b9ec4298bb139f1,
 signature: Signature( R: CompressedEdwardsY: [190, 42, 181, 13, 77, 96, 141, 83, 142, 224, 55, 11, 0, 3                                                           2, 202, 131, 136, 91, 64, 139, 152, 118, 225, 112, 15, 74, 32, 107, 208, 9, 6, 5], s: Scalar{
        bytes: [66, 154, 212, 67, 4, 188, 127, 21, 8, 61, 167, 93, 247, 94, 88, 146, 74, 196, 54, 229, 6                                                           7, 77, 109, 116, 181, 73, 117, 139, 160, 44, 99, 8],
} ),
 }
Events:
ContractEvent { access_path: AccessPath { address: 265b54817517c9a1efb872998817df7802eac3df3dd19e2e09033                                                           8337ef74a78, type: Resource, hash: "217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc97", s                                                           uffix: "/sent_events_count/" } , index: 0, event_data: AccountEvent { account: 52593c9d2408d25b20e7a9693                                                           f7af1154c99800647f8b9e9ec8974c99b9efdcf, amount: 50000000 } }
ContractEvent { access_path: AccessPath { address: 52593c9d2408d25b20e7a9693f7af1154c99800647f8b9e9ec897                                                           4c99b9efdcf, type: Resource, hash: "217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc97", s                                                           uffix: "/received_events_count/" } , index: 1, event_data: AccountEvent { account: 265b54817517c9a1efb87                                                           2998817df7802eac3df3dd19e2e090338337ef74a78, amount: 50000000 } }

我们可以清楚的从交易中,分析到这笔交易。

做了一笔交易后,我们查询一下交易序列.

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

查询余额

libra% q b 0
Balance is: 50.000000
libra% q b 1
Balance is: 250.000000

从余额可以清楚看到,从Alice账户将50个libra转移给我Bob。

2019年8月1日,写于深圳。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值