其他分享
首页 > 其他分享> > Facebook:使用Libra完成第一笔交易

Facebook:使用Libra完成第一笔交易

作者:互联网

Facebook:使用Libra完成第一笔交易

http://www.libradocs.com/docs/my-first-transaction/

本文档将指导你在Libra Blockchain上完成你的第一笔交易。在你按照执行第一笔交易的步骤之前,我们建议你阅读以下文档,来熟悉Libra生态系统和Libra协议:

我们提供了一个命令行界面(CLI)客户端来与区块链进行交互。

准备

运行文档中的命令,需要满足以下条件:

提交交易的步骤

在这个例子中,我们将下载必要的Libra组件并在两个用户之间执行一个交易:A(Alice)和B(Bob)。

执行以下步骤将交易提交到Libra testnet上的验证者节点:

  1. 克隆并编译 Libra Core.
  2. 编译客户端并连接到测试网络.
  3. 创建测试账户.
  4. 充Libra币到测试账户中.
  5. 发起转账.

克隆并编译 Libra Core

在终端里找一个合适的目录下执行:

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

启动 Libra Core

然后进入 libra 目录,并运行安装脚本:


 
  1. cd libra

  2. ./scripts/dev_setup.sh

安装脚本主要执行以下操作:

如果你安装失败,请查看 错误处理

编译客户端并连接到测试网络

要连接到Libra testnet上运行的验证者节点,请执行以下命令:

./scripts/cli/start_cli_testnet.sh

这条命令会编译客户端,并把客户端连接到 Libra 测试网络中的验证节点上。

连接成功后,会看到以下的输出信息。连接过程可以使用 quit 结束退出。


 
  1. usage: <command> <args>

  2.  
  3. Use the following commands:

  4.  
  5. account | a

  6. Account operations

  7. query | q

  8. Query operations

  9. transfer | transferb | t | tb

  10. <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.

  11. Transfer coins from account to another.

  12. help | h

  13. Prints this help

  14. quit | q!

  15. Exit this client

  16.  
  17.  
  18. Please, input commands:

  19.  
  20. libra%

如果看到上面的信息,说明你的客户端运行正常,并且已经成功连接到 Libra 的测试网络上了,接着开始创建测试账户。 如果你在编译客户端和连接到testnet时遇到问题,参考错误处理

Note: 如果要在本地运行验证者节点,请参考 运行本地验证者节点. 创建帐户,造币和执行交易的说明与testnet上的节点相同。

创建测试账户

将客户端连接到testnet后,可以运行CLI命令来创建新帐户。我们将引导你为两个用户创建帐户(让我们称他们为A和B)。

Step 1: 检查CLI客户端是否在你的系统上运行

libra%命令行提示符指示你的Libra CLI客户端正在运行。要查看帐户命令的帮助信息,请输入“account”,如下所示:


 
  1. account

  2. usage: account <arg>

  3.  
  4. Use the following args for this command:

  5.  
  6. create | c

  7. Create an account. Returns reference ID to use in other operations

  8. list | la

  9. Print all accounts that were created or loaded

  10. recover | r <file path>

  11. Recover Libra wallet from the file path

  12. write | w <file name>

  13. Save Libra wallet mnemonic recovery seed to disk

  14. mint | mintb | m | mb <receiver account> <number of coins>

  15. Mint coins to the account. Suffix 'b' is for blocking

Step 2: 创建测试账户A

请注意,使用CLI创建帐户不会更新区块链,只会创建本地密钥对。

要创建Alice的帐户,在 libra%提示符后面输入命令::

account create

创建成功后,你会看到以下信息:


 
  1. >> Creating/retrieving next account from wallet

  2. Created/retrieved account #0 address 3ed8e5fafae4147b2a105a0be2f81972883441cfaaadf93fc0868e7a0253c4a8

#0 是A的账户索引, 索引是引用A帐户的一种方式,在区块链上没有意义,只是帐户地址的包装,目的是为了方便用户引用他们创建的帐户。 address后面的长字符串是A的帐户地址,你可以在CLI命令中使用十六进制地址。

当前创建的账户只是在本地,只有以下两种情况发生之后,才会真正在区块链上创建这个账户:

Step 3: 创建测试账户B

在libra% 后再次输入创建账户的命令:

account create

创建成功后,你会看到以下信息:


 
  1. >> Creating/retrieving next account from wallet

  2. Created/retrieved account #1 address 8337aac709a41fe6be03cad8878a0d4209740b1608f8a81566c9a7d4b95a2ec7

#1 是B的账户索引,address后面的长字符串是B的账户地址。 有关索引的更多详细信息,请参考创建测试账户A

Step 4 (可选): 列出本地所有账户

使用下边的命令可以列出目前本地的所有账户:

account list

执行成功后,你会看到以下信息:


 
  1. User account index: 0, address: 3ed8e5fafae4147b2a105a0be2f81972883441cfaaadf93fc0868e7a0253c4a8, sequence number: 0

  2. User account index: 1, address: 8337aac709a41fe6be03cad8878a0d4209740b1608f8a81566c9a7d4b95a2ec7, sequence number: 0

sequence number表示从该帐户发送的交易数。每次从该帐户发送的事务被执行并存储在区块链中时,它会递增。要了解更多信息,请参考sequence-number

充Libra币到测试账户中

在testnet上创建和添加硬币是通过Faucet完成的。Faucet是一种与testnet一起运行的服务。此服务仅用于为testnet创建钱,并且不存在主网。它只是用来测试的,在现实中并没有任何使用价值。假设你已经创建了A和B的帐户,分别为索引0和索引1,你可以按照以下步骤将Libra添加到两个帐户。

Step 1: 向A账户充值110Libra币

输入以下命令,来完成充值:

account mint 0 110

A successful account mint command will also create Alice’s account on the blockchain.

执行成功后,你会看到以下信息:


 
  1. >> Minting coins

  2. Mint request submitted

请注意,提交请求时,这意味着它已成功添加到mempool(testnet上的验证者节点)。它并不一定意味着它将成功完成。稍后,我们将查询帐户余额以确认铸币是否成功。

如果你的帐户mint命令未成功提交你的请求,请参考错误处理

Step 2: 向B账户充值52Libra币

输入以下命令,来完成充值:

account mint 1 52

执行成功后,你会看到以下信息:


 
  1. >> Minting coins

  2. Mint request submitted

如果你的帐户Minting命令未成功,请参考错误处理

Step 3: 检查余额

输入以下命令,检查A的余额

query balance 0

执行成功后,你会看到以下信息:

Balance is: 110

输入以下命令,检查A的余额

query balance 1

执行成功后,你会看到以下信息:

Balance is: 52

发起转账

在我们发起转账之前,我们可以查询每个账户的sequence number ,这将有助于我们了解执行交易是如何改变每个帐户的。

查询账户的sequence number


 
  1. query sequence 0

  2. >> Getting current sequence number

  3. Sequence number is: 0

  4. query sequence 1

  5. >> Getting current sequence number

  6. Sequence number is: 0

在 query sequence 0中, 0 是A的账户索引. Sequence number等于0,意味着没有发生任何的交易。

发起转账

输入以下命令可以实现,从A账户向B账户转10个Libra币

transfer 0 1 10

执行成功后,你会看到以下信息:


 
  1. >> Transferring

  2. Transaction submitted to validator

  3. To query for transaction status, run: query txn_acc_seq 0 0 <fetch_events=true|false>

你可以使用命令 query txn_acc_seq 0 0 true 来查询转账状态。第一个参数0是要查询的本地账户索引, 第二个参数0是当前此笔转账的序号,要查看此命令的示例输出,请参考 输出.

你刚才交易已经提交到了testnet验证者节点上,它已经在验证者节点中的mempool中。但这并不一定意味着你的交易已被执行。理论上,如果系统运行缓慢或过载,则需要一些时间才能看到结果,你可能需要通过查询帐户多次检查。要查询索引为0的帐户,可以使用命令 query account_state 0. 输出结果请参考输出.

如果出现问题,请参考错误处理.

阻止转移命令(同步转账): 你可以使用 transferb 命令替换 transfer 命令. 在使用transferb命令提交后,直到提交到blockchain后,才会返回提示信息。一个例子:

transferb 0 1 10

有关从提交到执行和存储的交易生命周期的理解,请参阅交易的生命周期

查询交易次数


 
  1. query sequence 0

  2. >> Getting current sequence number

  3. Sequence number is: 1

  4. query sequence 1

  5. >> Getting current sequence number

  6. Sequence number is: 0

A的帐号(索引0)的sequence number为1表示到目前为止已经从A的帐户发送了一个交易。B的帐户(索引1)的sequence number为0表示到目前为止尚未从B的帐户发送任何交易。每次从帐户发送交易时,序列号都会增加1。

查询账户余额

要检查两个帐户中的最终余额,可以参考下面的命令。如果你的交易(转账)成功执行,你应该在A账户中看到100个Libra币,在B账户中看到62个Libra币。


 
  1. query balance 0

  2. Balance is: 100

  3. query balance 1

  4. Balance is: 62

大功告成

你已成功在Libra testnet上执行了你的交易,并将10个Libra币从A账户转移到了B账户!

错误处理

启动相关

客户端编译与运行相关

如果编译失败,请先删除锁定文件

如果客户端无法连接到testnet:

造币(Minting)与充值相关

转账相关

如果testnet验证者节点(你的客户端已连接到)不可用或你与testnet的连接已超时,你将看到此错误:


 
  1. transfer 0 1 10

  2. >> Transferring

  3. [ERROR] Failed to perform transaction: Server unavailable, please retry and/or check if host passed to the client is running

解决此错误的步骤:

查询命令的输出信息示例

根据账户索引与Sequence Number查询交易的输出信息示例


 
  1. query txn_acc_seq 0 0 true

  2. >> Getting committed transaction by account and sequence number

  3. Committed transaction: SignedTransaction {

  4. { raw_txn: RawTransaction {

  5. sender: 3ed8e5fafae4147b2a105a0be2f81972883441cfaaadf93fc0868e7a0253c4a8,

  6. sequence_number: 0,

  7. payload: {,

  8. transaction: peer_to_peer_transaction,

  9. args: [

  10. {ADDRESS: 8337aac709a41fe6be03cad8878a0d4209740b1608f8a81566c9a7d4b95a2ec7},

  11. {U64: 10000000},

  12. ]

  13. },

  14. max_gas_amount: 10000,

  15. gas_unit_price: 0,

  16. expiration_time: 1560466424s,

  17. },

  18. public_key: 55af3fe3f28550a2f1e5ebf073ef193feda44344d94c463b48be202aa0b3255d,

  19. signature: Signature( R: CompressedEdwardsY: [210, 23, 214, 62, 228, 179, 64, 147, 81, 159, 180, 138, 100, 211, 111, 139, 178, 148, 81, 1, 240, 135, 148, 145, 104, 234, 227, 239, 198, 153, 13, 199], s: Scalar{

  20. bytes: [203, 76, 105, 49, 64, 130, 162, 81, 22, 237, 159, 26, 80, 181, 111, 94, 84, 6, 152, 126, 181, 192, 62, 103, 130, 94, 246, 174, 139, 214, 3, 15],

  21. } ),

  22. }

  23. }

  24. Events:

  25. ContractEvent { access_path: AccessPath { address: 3ed8e5fafae4147b2a105a0be2f81972883441cfaaadf93fc0868e7a0253c4a8, type: Resource, hash: "217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc97", suffix: "/sent_events_count/" } , index: 0, event_data: AccountEvent { account: 8337aac709a41fe6be03cad8878a0d4209740b1608f8a81566c9a7d4b95a2ec7, amount: 10000000 } }

  26. ContractEvent { access_path: AccessPath { address: 8337aac709a41fe6be03cad8878a0d4209740b1608f8a81566c9a7d4b95a2ec7, type: Resource, hash: "217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc97", suffix: "/received_events_count/" } , index: 0, event_data: AccountEvent { account: 3ed8e5fafae4147b2a105a0be2f81972883441cfaaadf93fc0868e7a0253c4a8, amount: 10000000 } }

请注意,交易金额以microlibra显示。

查询结果的输出信息示例

在下面示例中,我们从索引0的帐户查询“已发送”结果。注意:因为只发送了一个交易,所以只有一个结果。结果中还有验证信息,可以用来验证是否有丢失的内容 - this is done when the query does not return “limit” events。


 
  1. query event 0 sent 0 true 10

  2. >> Getting events by account and event type.

  3. EventWithProof {

  4. transaction_version: 3,

  5. event_index: 0,

  6. event: ContractEvent { access_path: AccessPath { address: e7460e02058b36d28e8eef03f0834c605d3d6c57455b8ec9c3f0a3c8b89f248b, type: Resource, hash: "217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc97", suffix: "/sent_events_count/" } , index: 0, event_data: AccountEvent { account: 46efbad798a739c088e0e98dd9d592c27c7eb45ba1f8ccbdfc00bd4d7f2947f3, amount: 10000000 } },

  7. proof: EventProof { ledger_info_to_transaction_info_proof: AccumulatorProof { siblings: [HashValue(62570ae9a994bcb20c03c055667a4966fa50d0f17867dd5819465072fd2c58ba), HashValue(cce2cf325714511e7d04fa5b48babacd5af943198e6c1ac3bdd39c53c87cb84c)] }, transaction_info: TransactionInfo { signed_transaction_hash: HashValue(69bed01473e0a64140d96e46f594bc4b463e88e244b694e962b7e19fde17f30d), state_root_hash: HashValue(5809605d5eed94c73e57f615190c165b11c5e26873012285cc6b131e0817c430), event_root_hash: HashValue(645df3dee8f53a0d018449392b8e9da814d258da7346cf64cd96824f914e68f9), gas_used: 0 }, transaction_info_to_event_proof: AccumulatorProof { siblings: [HashValue(5d0e2ebf0952f0989cb5b38b2a9b52a09e8d804e893cb99bf9fa2c74ab304bb1)] } }

  8. }

  9. Last event state: Some(

  10. AccountStateWithProof {

  11. version: 3,

  12. blob: Some(

  13. AccountStateBlob {

  14. Raw: 0x010000002100000001217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc974400000020000000e7460e02058b36d28e8eef03f0834c605d3d6c57455b8ec9c3f0a3c8b89f248b00e1f50500000000000000000000000001000000000000000100000000000000

  15. Decoded: Ok(

  16. AccountResource {

  17. balance: 100000000,

  18. sequence_number: 1,

  19. authentication_key: 0xe7460e02058b36d28e8eef03f0834c605d3d6c57455b8ec9c3f0a3c8b89f248b,

  20. sent_events_count: 1,

  21. received_events_count: 0,

  22. },

  23. )

  24. },

  25. ),

  26. proof: AccountStateProof {

  27. ledger_info_to_transaction_info_proof: AccumulatorProof {

  28. siblings: [

  29. HashValue(62570ae9a994bcb20c03c055667a4966fa50d0f17867dd5819465072fd2c58ba),

  30. HashValue(cce2cf325714511e7d04fa5b48babacd5af943198e6c1ac3bdd39c53c87cb84c),

  31. ],

  32. },

  33. transaction_info: TransactionInfo {

  34. signed_transaction_hash: HashValue(69bed01473e0a64140d96e46f594bc4b463e88e244b694e962b7e19fde17f30d),

  35. state_root_hash: HashValue(5809605d5eed94c73e57f615190c165b11c5e26873012285cc6b131e0817c430),

  36. event_root_hash: HashValue(645df3dee8f53a0d018449392b8e9da814d258da7346cf64cd96824f914e68f9),

  37. gas_used: 0,

  38. },

  39. transaction_info_to_account_proof: SparseMerkleProof {

  40. leaf: Some(

  41. (

  42. HashValue(c0fbd63b0ae4abfe57c8f24f912f164ba0537741e948a65f00d3fae0f9373981),

  43. HashValue(fc45057fd64606c7ca40256b48fbe486660930bfef1a9e941cafcae380c25871),

  44. ),

  45. ),

  46. siblings: [

  47. HashValue(4136803b3ba779bb2c1daae7360f3f839e6fef16ae742590a6698b350a5fc376),

  48. HashValue(5350415253455f4d45524b4c455f504c414345484f4c4445525f484153480000),

  49. HashValue(a9a6bda22dd6ee78ddd3a42da152b9bd39797b7da738e9d6023f407741810378),

  50. ],

  51. },

  52. },

  53. },

  54. )

查询账户状态的输出信息示例

在下面示例中,我们可以看到单个帐户的状态。


 
  1. query account_state 0

  2. >> Getting latest account state

  3. Latest account state is:

  4. Account: 3ed8e5fafae4147b2a105a0be2f81972883441cfaaadf93fc0868e7a0253c4a8

  5. State: Some(

  6. AccountStateBlob {

  7. Raw: 0x010000002100000001217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc9744000000200000003ed8e5fafae4147b2a105a0be2f81972883441cfaaadf93fc0868e7a0253c4a800e1f50500000000000000000000000001000000000000000100000000000000

  8. Decoded: Ok(

  9. AccountResource {

  10. balance: 100000000,

  11. sequence_number: 1,

  12. authentication_key: 0x3ed8e5fafae4147b2a105a0be2f81972883441cfaaadf93fc0868e7a0253c4a8,

  13. sent_events_count: 1,

  14. received_events_count: 0,

  15. },

  16. )

  17. },

  18. )

  19. Blockchain Version: 3

运行本地验证者节点

要在你的计算机上本地启动验证者节点并创建你自己的本地区块链网络(未连接到Libra testnet),请确保已按照Setup Libra Core中的说明运行编译脚本,并切换到Libra Core存储库的根目录后运行libra_swarm,如下所示:


 
  1. $ cd ~/libra

  2. $ cargo run -p libra_swarm -- -s

-p libra_swarm - causes cargo to run the libra_swarm package, which starts a local blockchain consisting of one node.

-s option starts a local client to connect to the local blockchain.

要查看所有的参数,可增加-h命令:

$ cargo run -p libra_swarm -- -h

The cargo run command may take some time to run. If the execution of this command completes without errors, an instance of the Libra CLI client and a Libra validator node is running on your system. Upon successful execution, you should see an output containing the CLI client menu and the libra% prompt.

交易的生命周期

当执行了第一笔交易,可以参考交易的生命周期

标签:account,帐户,sequence,number,testnet,Libra,Facebook,第一笔
来源: https://blog.csdn.net/starzhou/article/details/96186243