Libra客户端的第一个交易

操作系统:MAC OS 10.14.x,libra版本:testnet

搭建Libra测试网络

由于Libra网络还没有正式运转,我们要体验Libra体系架构,可以通过测试网络来进行。
根据Libra的文档,想要运行Libra测试网络,需要Linux或MacOS操作系统,本文以MacOS系统为例进行讲解。

官方的例子,My First Transaction

1、下载和构建Libra核心库

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

cd libra

//注意切换到测试分支
git checkout testnet 

//安装依赖,需要一些时间
./scripts/dev_setup.sh 

2、构建Libra cli客户端,连接Testnet

还是贴上第一手资料,直接开发者网站进去
https://developers.libra.org/docs/my-first-transaction

./scripts/cli/start_cli_testnet.sh 

连接测试网节点之后会出现Libra的控制台:

Building and running client in debug mode.
    Finished dev [unoptimized + debuginfo] target(s) in 0.93s
     Running `target/debug/client --host ac.testnet.libra.org --port 8000 -s ./scripts/cli/consensus_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 140000)] 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% 

可能的错误

一开始我没有切换到测试网分支,出现以下错误:

Not able to connect to validator at ac.testnet.libra.org:80, error RpcFailure(RpcStatus { status: DeadlineExceeded, details: Some("Deadline Exceeded") })

查看issue, https://github.com/libra/libra/issues/38
需要修改libra/client/src/grpc_client.rs 增大5000

    fn get_default_grpc_call_option() -> CallOption {
        CallOption::default()
            .wait_for_ready(true)
            .timeout(std::time::Duration::from_millis(5000))
    }

尝试后没有用,切换到testnet分支,无须改这个参数也可以运行。但是还是建议将5000增大,控制台交互的时候会报错。

3、创建账号

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

libra% account list
User account index: 0, address: dcaaf919d82f969f72ee873cb472c8bffd009f36bcfddce15c61acb8cff43fb8, sequence number: 5, status: Persisted
User account index: 1, address: 2fdc5c83f862983c1af2f4478a9c9e75a52b084ae18b7adf18c62aba92ea9f57, sequence number: 1, status: Persisted
给账号添加LBR,因为是测试网,可以任意给
1.给账号0增加libra货币,0是账号的序号,99999时增加的货币数量
libra% account mint 0 99999
>> Minting coins
Mint request submitted
2.账号1增加999990000 libra
libra% account mint 1 999990000
>> Minting coins
Mint request submitted
查询账户余额
libra% query balance 0
Balance is: 111758.000000
libra% query balance 1
Balance is: 999992428.000000

因为我已经多次测试了,每次产生的账号都是一样的,我每次新增libra,账号的的币一直增加的,所以查出来的值和新增的数量不一样。

4、发起交易

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

从账号0 给账号1转账,金额为199个LBR
这是一个异步的交易,可以按照提示查询交易的状态和结果

libra% query txn_acc_seq 0 5 true
>> Getting committed transaction by account and sequence number

Committed transaction: SignedTransaction {
        raw_txn: RawTransaction {
            sender: dcaaf919d82f969f72ee873cb472c8bffd009f36bcfddce15c61acb8cff43fb8,
            sequence_number: 5,
            payload: {,
                transaction: peer_to_peer_transaction,
                args: [{
                        ADDRESS: 2 fdc5c83f862983c1af2f4478a9c9e75a52b084ae18b7adf18c62aba92ea9f57
                    },
                    {
                        U64: 199000000
                    },
                ]
            },
            max_gas_amount: 140000,
            gas_unit_price: 0,
            expiration_time: 1574768307 s,
        },
        public_key: Ed25519PublicKey(
            PublicKey(CompressedEdwardsY: [132, 63, 87, 219, 131, 225, 137, 96, 167, 50, 234, 206, 254, 61, 126, 127, 125, 85, 133, 190, 0, 51, 37, 96, 113, 203, 176, 144, 227, 70, 142, 19]), EdwardsPoint {
                X: FieldElement51([1176422284674184, 2225165124562903, 675262596504187, 1992089984303741, 814523880810113]),
                Y: FieldElement51([529431413604228, 2209869227944977, 586408896887031, 1610441249357919, 344028729641740]),
                Z: FieldElement51([1, 0, 0, 0, 0]),
                T: FieldElement51([939970195170134, 1770098148800191, 1254516232889842, 768125926758313, 861953446577628])
            }),
    ),
    signature: Ed25519Signature(
        Signature(R: CompressedEdwardsY: [69, 224, 12, 118, 196, 144, 20, 71, 54, 203, 181, 195, 132, 4, 20, 44, 145, 163, 96, 227, 66, 158, 51, 106, 225, 80, 181, 255, 159, 152, 203, 145], s: Scalar {
            bytes: [122, 15, 164, 241, 3, 55, 37, 235, 119, 169, 163, 159, 129, 120, 6, 7, 209, 223, 65, 192, 234, 149, 158, 150, 14, 42, 202, 208, 127, 226, 26, 14],
        }),
    ),
}
Events:
    ContractEvent {
        key: 7943 a726db0d1376bf935dbb032a3840219de0b2f66f484686110358855dda62,
        index: 5,
        type: Struct(StructTag {
            address: 0000000000000000000000000000000000000000000000000000000000000000,
            module: Identifier("LibraAccount"),
            name: Identifier("SentPaymentEvent"),
            type_params: []
        }),
        event_data: AccountEvent {
            amount: 199000000,
            account: 2 fdc5c83f862983c1af2f4478a9c9e75a52b084ae18b7adf18c62aba92ea9f57
        }
    }
ContractEvent {
    key: e4a2f1506d395fdba536d8f6df31a3486b43295b6487a515bcdd0b4aef3f23a9,
    index: 9,
    type: Struct(StructTag {
        address: 0000000000000000000000000000000000000000000000000000000000000000,
        module: Identifier("LibraAccount"),
        name: Identifier("ReceivedPaymentEvent"),
        type_params: []
    }),
    event_data: AccountEvent {
        amount: 199000000,
        account: dcaaf919d82f969f72ee873cb472c8bffd009f36bcfddce15c61acb8cff43fb8
    }
}

显示的信息比较杂,不像eth、eos简洁,吐槽。。。发送方和接收方都有event。

查询账号的余额,发生变化199的变化,转账成功,可以根据序号和地址查询

libra% query balance 0
Balance is: 111559.000000
libra% query balance 1
Balance is: 999992627.000000


libra% query balance dcaaf919d82f969f72ee873cb472c8bffd009f36bcfddce15c61acb8cff43fb8
Balance is: 111559.000000
libra% query balance 2fdc5c83f862983c1af2f4478a9c9e75a52b084ae18b7adf18c62aba92ea9f57
Balance is: 999992627.000000

如果需要确认状态转账成功, 可以用阻塞转账命令,本地目测也是1s左右完成吧,b代表是blocking

libra% transferb 0 1 10
>> Transferring
waiting transaction is stored!
Finished transaction!
To query for transaction status, run: query txn_acc_seq 0 6 <fetch_events=true|false>

5、一些有用的命令:

libra% query account_state 0
>> Getting latest account state
Latest account state is: 
    Account: dcaaf919d82f969f72ee873cb472c8bffd009f36bcfddce15c61acb8cff43fb8
State: Some(
    AccountStateBlob {
        Raw: 0x010000002100000001a208df134fefed8442b1f01fab59071898f5a1af5164e12c594de55a7004a91c8e00000020000000dcaaf919d82f969f72ee873cb472c8bffd009f36bcfddce15c61acb8cff43fb840a9d6f81900000000000700000000000000200000002b6d9dc7398d37ef178df6c492722418638ad40664e5ff5c5410c540a84915c70700000000000000200000007943a726db0d1376bf935dbb032a3840219de0b2f66f484686110358855dda620700000000000000
        Decoded: AccountResource {
            authentication_key: 0xdcaaf919d82f969f72ee873cb472c8bffd009f36bcfddce15c61acb8cff43fb8,
            balance: 111549000000,
            delegated_key_rotation_capability: false,
            delegated_withdrawal_capability: false,
            received_events: EventHandle {
                count: 7,
                key: EventKey(
                    [
                        43,
                        109,
                        157,
                        199,
                        57,
                        141,
                        55,
                        239,
                        23,
                        141,
                        246,
                        196,
                        146,
                        114,
                        36,
                        24,
                        99,
                        138,
                        212,
                        6,
                        100,
                        229,
                        255,
                        92,
                        84,
                        16,
                        197,
                        64,
                        168,
                        73,
                        21,
                        199,
                    ],
                ),
            },
            sent_events: EventHandle {
                count: 7,
                key: EventKey(
                    [
                        121,
                        67,
                        167,
                        38,
                        219,
                        13,
                        19,
                        118,
                        191,
                        147,
                        93,
                        187,
                        3,
                        42,
                        56,
                        64,
                        33,
                        157,
                        224,
                        178,
                        246,
                        111,
                        72,
                        70,
                        134,
                        17,
                        3,
                        88,
                        133,
                        93,
                        218,
                        98,
                    ],
                ),
            },
            sequence_number: 7,
        }
    },
)
Blockchain Version: 98542

这个显示是不是bug,EventKey显示有问题。

忘记命令格式会有提示

libra% transfer 0 1 
Invalid number of arguments for transfer
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 140000)] Suffix 'b' is for blocking. 

6、Libra一手资料

Libra 官网
Libra 白皮书
Libra 技术白皮书
Libra 开发者文档: 这个写的非常详细,可以快速上手。
 
 

接下来一段时间,和大家一起学习Libra。
交流可加QQ群:693883930

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。