[关闭]
@Fangzheng1992 2018-11-27T02:29:27.000000Z 字数 9039 阅读 1070

EOS节点搭建与合约部署

EOS节点搭建

查看服务器操作系统版本

  1. cat /proc/version
  2. Linux version 4.4.0-102-generic (buildd@lgw01-amd64-055) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5) ) #125-Ubuntu SMP Tue Nov 21 15:15:11 UTC 2017

查看服务器磁盘使用

  1. df -hl
  2. Filesystem Size Used Avail Use% Mounted on
  3. udev 3.9G 0 3.9G 0% /dev
  4. tmpfs 799M 4.7M 794M 1% /run
  5. /dev/vda1 345G 243G 85G 75% /

看这行:/dev/vda1 345G 243G 85G 75% ,还剩下85G可用空间。

一个EOS节点大约需要20G。

执行下面命令安装EOS节点

  1. wget https://github.com/eosio/eos/releases/download/v1.4.4/eosio_1.4.4-1-ubuntu-16.04_amd64.deb
  2. $ sudo apt install ./eosio_1.4.4-1-ubuntu-16.04_amd64.deb

执行完毕后nodeos将安装到/usr/bin下。

任意找个位置创建一个测试节点启动脚本

  1. touch eos-test.sh
  2. chmod 744 eos-test.sh

输入如下内容

  1. nodeos -e -p eosio -d /eosdata --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --contracts-console --filter-on "*" --access-control-allow-origin "*"

解释下其中的几个参数:

-p eosio 区块产生者的账户,这里设定为系统账户eosio

-d /eosdata 区块链数据存放的目录

--plugin 开启指定插件

--contracts-console 开启控制台

启动节点

  1. ./eos-test.sh

出现下面的生产区块日志,就表示启动成功。

  1. info 2018-11-21T06:14:48.000 thread-0 producer_plugin.cpp:1490 produce_block ] Produced block 00000985e9a86774... #2437 @ 2018-11-21T06:14:48.000 signed by eosio [trxs: 0, lib: 2436, confirmed: 0]
  2. info 2018-11-21T06:14:48.500 thread-0 producer_plugin.cpp:1490 produce_block ] Produced block 0000098659626754... #2438 @ 2018-11-21T06:14:48.500 signed by eosio [trxs: 0, lib: 2437, confirmed: 0]
  3. info 2018-11-21T06:14:49.000 thread-0 producer_plugin.cpp:1490 produce_block ] Produced block 00000987ddfe113b... #2439 @ 2018-11-21T06:14:49.000 signed by eosio

重新打开一个窗口

通过cleos与EOS节点和钱包交互

  1. cleos get info
  2. {
  3. "server_version": "59626f1e",
  4. "chain_id": "cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f",
  5. "head_block_num": 2779,
  6. "last_irreversible_block_num": 2778,
  7. "last_irreversible_block_id": "00000ada125d953aba94e94b2aa8a15b1773345766bab1863c6f6e977e2343d6",
  8. "head_block_id": "00000adb93c1f0fda3e8e21a180f98e49cb0840e71416476b402dd4837358f2f",
  9. "head_block_time": "2018-11-21T06:17:39.000",
  10. "head_block_producer": "eosio",
  11. "virtual_block_cpu_limit": 3214242,
  12. "virtual_block_net_limit": 16884231,
  13. "block_cpu_limit": 199900,
  14. "block_net_limit": 1048576,
  15. "server_version_string": "v1.4.4"
  16. }

chain id唯一标识了一条EOS链,这里我启动的是条本地测试链。

可以通过指定主网peer节点来连接主网,比如我们连接主网节点http://mainnet.genereos.io:80

  1. cleos --url=http://mainnet.genereos.io:80 get info
  2. {
  3. "server_version": "99d94446",
  4. "chain_id": "aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906",
  5. "head_block_num": 28055941,
  6. "last_irreversible_block_num": 28055607,
  7. "last_irreversible_block_id": "01ac183760816ad2812e4c442ca53bb3e41040841b577d81e2d08ade18850f62",
  8. "head_block_id": "01ac19850756798d85bdb52ab48d7ef4432113f991e6d795cc99e5bbe713c8b7",
  9. "head_block_time": "2018-11-21T06:27:05.000",
  10. "head_block_producer": "eos42freedom",
  11. "virtual_block_cpu_limit": 4920210,
  12. "virtual_block_net_limit": 1048576000,
  13. "block_cpu_limit": 176543,
  14. "block_net_limit": 1045016,
  15. "server_version_string": "mainnet-1.4.1"
  16. }

主链的chain id是aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906

区块数28055941

可以和浏览器https://eospark.com/MainNet/数据对应起来。

可以通过添加--p2p-peer-address p2p.meet.one:9876 --genesis-json /genesis.json来启动nodeos,这样节点可以加入主网。

genesis.json文件从https://github.com/EOS-Mainnet/eos/blob/launch-rc-1.0.2/mainnet-genesis.json获取,放置在eosdata目录下。

全节点上网找或者https://docs.google.com/spreadsheets/u/1/d/1K_un5Vak3eDh_b4Wdh43sOersuhs0A76HMCfeQplDOY/htmlview?sle=true#gid=0里取一个。

钱包生成

  1. cleos wallet create --to-console -n test
  2. Creating wallet: test
  3. Save password to use in the future to unlock this wallet.
  4. Without password imported keys will not be retrievable.
  5. "PW5HwmHNRee71Hke6ADGCSYFWrjjbssrzuNY95hmyQAGdCS57uPDR"

记住这个钱包密码,后续用于解锁钱包。

还需要把eosio账户的私钥导入到钱包中才能控制该账户,eosio账户的私钥在config.ini文件中。

系统默认的配置文件:~/.local/share/eosio/nodeos/config/config.ini

其中signature-provider = EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

记录了默认区块生产者eosio的公私钥对,前面的是公钥,key后面的是私钥。

导入eosio账户的私钥到test钱包。

  1. cleos wallet import -n test --private-key 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
  2. imported private key for: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV

接下来再用钱包test创建一个个人账户test,下面的命令生成了一对新的公私钥对并导入到钱包里。

  1. cleos wallet create_key -n test
  2. Created new private key with a public key of: "EOS5mPPFpdAjMKWxYeYyWiyjNhghbUUyiqXw4yhhRnUApJQbnPACQ"

生成账户test,并关联上面生成的公钥,这里省略了一个active公钥,只填了一个owner公钥。

  1. # cleos wallet keys
  2. [
  3. "EOS5mPPFpdAjMKWxYeYyWiyjNhghbUUyiqXw4yhhRnUApJQbnPACQ",
  4. "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
  5. ]
  6. # cleos create account eosio test EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
  7. executed transaction: f6d69af4b265fae07a27842d88be7e145b95977461b2b1eb79aef95dbbe17b82 200 bytes 332 us
  8. # eosio <= eosio::newaccount {"creator":"eosio","name":"test","owner":{"threshold":1,"keys":[{"key":"EOS6MRyAjQq8ud7hVNYcfnVPJqcV...
  9. warning: transaction executed locally, but may not be confirmed by the network yet ]

生成合约

安装合约开发工具EOS.cdt

  1. $ wget https://github.com/eosio/eosio.cdt/releases/download/v1.4.1/eosio.cdt-1.4.1.x86_64.deb
  2. $ sudo apt install ./eosio.cdt-1.4.1.x86_64.deb

新建hello.cpp文件

  1. #include <eosiolib/eosio.hpp>
  2. #include <eosiolib/print.hpp>
  3. using namespace eosio;
  4. class hello : public contract {
  5. public:
  6. using contract::contract;
  7. [[eosio::action]]
  8. void hi( name user ) {
  9. print( "Hello, ", name{user});
  10. }
  11. };
  12. EOSIO_DISPATCH( hello, (hi))

编译

  1. eosio-cpp -o hello.wasm hello.cpp --abigen

生成了hello.abi和hello.wasm

  1. cat hello.abi
  2. {
  3. "____comment": "This file was generated with eosio-abigen. DO NOT EDIT Wed Nov 21 16:04:26 2018",
  4. "version": "eosio::abi/1.1",
  5. "structs": [
  6. {
  7. "name": "hi",
  8. "base": "",
  9. "fields": [
  10. {
  11. "name": "user",
  12. "type": "name"
  13. }
  14. ]
  15. }
  16. ],
  17. "types": [],
  18. "actions": [
  19. {
  20. "name": "hi",
  21. "type": "hi",
  22. "ricardian_contract": ""
  23. }
  24. ],
  25. "tables": [],
  26. "ricardian_clauses": [],
  27. "variants": [],
  28. "abi_extensions": []
  29. }

部署合约

记住,这时我们有两个账户,一个默认账户eosio,一个个人账户test

如果钱包锁住的话先解锁钱包

  1. # cleos wallet unlock -n test --password PW5HwmHNRee71Hke6ADGCSYFWrjjbssrzuNY95hmyQAGdCS57uPDR
  2. Unlocked: test

然后用账户eosio发布合约

  1. cleos set contract eosio /root/contract/ hello.wasm hello.abi -p eosio@active
  2. Reading WASM from /root/contract/hello.wasm...
  3. Publishing contract...
  4. executed transaction: 4f1f10536dc09095366f8d18ecd2d4ef6b3755cbcf38865f6a8103f25730500c 1432 bytes 747 us
  5. # eosio <= eosio::setcode "0000000000ea30550000bf110061736d0100000001390b60027f7e006000017f60027f7f017f60027f7f0060037f7f7f017...
  6. # eosio <= eosio::setabi "0000000000ea3055320e656f73696f3a3a6162692f312e31000102686900010475736572046e616d6501000000000000806...
  7. warning: transaction executed locally, but may not be confirmed by the network yet ]

这笔交易长这样

  1. # cleos get transaction 4f1f10536dc09095366f8d18ecd2d4ef6b3755cbcf38865f6a8103f25730500c
  2. {
  3. "id": "4f1f10536dc09095366f8d18ecd2d4ef6b3755cbcf38865f6a8103f25730500c",
  4. "trx": {
  5. "receipt": {
  6. "status": "executed",
  7. "cpu_usage_us": 747,
  8. "net_usage_words": 179,
  9. "trx": [
  10. 1,{
  11. "signatures": [
  12. "SIG_K1_K4WjRqmiMAArWxmVmXwjQgrwt3JeqUJFGdehsYj99ZG1tA25x5mejKNGopLYGDZdWkjQkQKm2xoCruiTJifqcgeFgbJM6A"
  13. ],
  14. "compression": "zlib",
  15. "packed_context_free_data": "",
  16. "packed_trx": "78da85564d8b1c45187eeba3a77ba767b29d8322bb1eaadb081b309ae4b0c178c854309b84200121a78093ded94e323ddf1f89595976d6e041047f8007ef1e3c09426eae924340bcfa0b3c048d472107617ddeaa99dd450467d9eeaab7df8fe77deaa9ea6ebdf2d7ed8fde5ffdfeeb6b849fe40bfd7ef616ae8d37bff8ee2771cc40dffc79fef

很长,不在这里显示全部了。

调用合约

  1. # cleos push action eosio hi '["wormhole"]' -p test@active
  2. executed transaction: aff91ac7a7d4f1467c610e4eb593071810f230ce8d53b5bd946fdc3753bcf59f 104 bytes 539 us
  3. # eosio <= eosio::hi {"user":"wormhole"}
  4. >> Hello, wormhole
  5. warning: transaction executed locally, but may not be confirmed by the network yet ]

这里的eosio为被调用合约代码的账户,hi为调用方法,'["wormhole"]'为参数,实际的参数用'[ ]'包起来,-p表示哪个账户发起调用,发起这个动作用的什么权限,权限分为active和owner。

可以看到合约已成功执行:>> Hello, wormhole

查看调用交易

  1. cleos get transaction aff91ac7a7d4f1467c610e4eb593071810f230ce8d53b5bd946fdc3753bcf59f
  2. {
  3. "id": "aff91ac7a7d4f1467c610e4eb593071810f230ce8d53b5bd946fdc3753bcf59f",
  4. "trx": {
  5. "receipt": {
  6. "status": "executed",
  7. "cpu_usage_us": 539,
  8. "net_usage_words": 13,
  9. "trx": [
  10. 1,{
  11. "signatures": [
  12. "SIG_K1_K7Q9MV1kY3j4s4haTCwwhEsK4PmjDoZjfstCHcADdGRdvUvhiTDCx8PYKZbZ9GVrtDUsDpDrCX99GSseXqBUVgNrRstNPn"
  13. ],
  14. "compression": "none",
  15. "packed_context_free_data": "",
  16. "packed_trx": "8517f55ba248e9e70b2400000000010000000000ea3055000000000000806b01000000000090b1ca00000000a8ed3232080000002ad2262fe500"
  17. }
  18. ]
  19. },
  20. "trx": {
  21. "expiration": "2018-11-21T08:29:57",
  22. "ref_block_num": 18594,
  23. "ref_block_prefix": 604760041,
  24. "max_net_usage_words": 0,
  25. "max_cpu_usage_ms": 0,
  26. "delay_sec": 0,
  27. "context_free_actions": [],
  28. "actions": [{
  29. "account": "eosio",
  30. "name": "hi",
  31. "authorization": [{
  32. "actor": "test",
  33. "permission": "active"
  34. }
  35. ],
  36. "data": {
  37. "user": "wormhole"
  38. },
  39. "hex_data": "0000002ad2262fe5"
  40. }
  41. ],
  42. "transaction_extensions": [],
  43. "signatures": [
  44. "SIG_K1_K7Q9MV1kY3j4s4haTCwwhEsK4PmjDoZjfstCHcADdGRdvUvhiTDCx8PYKZbZ9GVrtDUsDpDrCX99GSseXqBUVgNrRstNPn"
  45. ],
  46. "context_free_data": []
  47. }
  48. },
  49. "block_time": "2018-11-21T08:29:27.500",
  50. "block_num": 18596,
  51. "last_irreversible_block": 19189,
  52. "traces": [{
  53. "receipt": {
  54. "receiver": "eosio",
  55. "act_digest": "9f7f4bf7be81ecc70381ef8e202070900f94fae225e5cd72feba0d4e3e41a0e2",
  56. "global_sequence": 18599,
  57. "recv_sequence": 18599,
  58. "auth_sequence": [[
  59. "test",
  60. 1
  61. ]
  62. ],
  63. "code_sequence": 1,
  64. "abi_sequence": 1
  65. },
  66. "act": {
  67. "account": "eosio",
  68. "name": "hi",
  69. "authorization": [{
  70. "actor": "test",
  71. "permission": "active"
  72. }
  73. ],
  74. "data": {
  75. "user": "wormhole"
  76. },
  77. "hex_data": "0000002ad2262fe5"
  78. },
  79. "context_free": false,
  80. "elapsed": 120,
  81. "console": "Hello, wormhole",
  82. "trx_id": "aff91ac7a7d4f1467c610e4eb593071810f230ce8d53b5bd946fdc3753bcf59f",
  83. "block_num": 18596,
  84. "block_time": "2018-11-21T08:29:27.500",
  85. "producer_block_id": null,
  86. "account_ram_deltas": [],
  87. "except": null,
  88. "inline_traces": []
  89. }
  90. ]
  91. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注