bitshares研究系列【私链运行】

in #bitshares6 years ago (edited)

一直在公链上玩肯定是不合适的,也不是见证人,也不是董事会成员,各种操作都要花钱,要想更多的了解bitshares,避免不了要自己搭建私链,这样就可以为所欲为了!

私链运行

bitshares私链运行:

./witness_node --rpc-endpoint "127.0.0.1:8090" --enable-stale-production --seed-nodes "[]"

在cli_wallet中也可以取一些数据,但是有一些操作却报时间相关异常:

Assert Exception: o.expiration_time <= d.head_block_time() + global_parameters.maximum_proposal_lifetime: Proposal expiration time is too far in the future.

好歹我的VS Code也已经跑起来了,可以调试还有什么不能解决的吗?

void_result proposal_create_evaluator::do_evaluate(const proposal_create_operation& o)
{ try {
   const database& d = db();
   const auto& global_parameters = d.get_global_properties().parameters;

   FC_ASSERT( o.expiration_time > d.head_block_time(), "Proposal has already expired on creation." );
   FC_ASSERT( o.expiration_time <= d.head_block_time() + global_parameters.maximum_proposal_lifetime,
              "Proposal expiration time is too far in the future.");
              ...
      }
}

找到提示错误的语句,加上断点,还没仔细看时间变量,取d.head_block_time中哪个变量晃了我一眼,blocknum=0,看起来根本没有生成新块啊!!!

记得在bitshares研究系列【bitshares-ui代码分析之api】这篇文章中说过,订阅消息后只要取了对象就会有订阅通知,再试一下:

Chaim:~ Chaim$ wscat -c ws://127.0.0.1:8090
connected (press CTRL+C to quit)
> {"method": "call", "params": [1, "login", ["", ""]], "id": 2}
< {"id":2,"jsonrpc":"2.0","result":true}
> {"method": "call", "params": [1, "database", []], "id": 3}
< {"id":3,"jsonrpc":"2.0","result":2}
> {"method": "call", "params": [2, "set_subscribe_callback", [5, false]], "id": 4}
< {"id":4,"jsonrpc":"2.0","result":null}
> {"method": "call", "params": [2, "get_objects", [["2.1.0"]]], "id": 5}
< {"id":5,"jsonrpc":"2.0","result":[{"id":"2.1.0","head_block_number":0,"head_block_id":"0000000000000000000000000000000000000000","time":"2018-04-27T11:02:35","current_witness":"1.6.0","next_maintenance_time":"1970-01-01T00:00:00","last_budget_time":"1970-01-01T00:00:00","witness_budget":0,"accounts_registered_this_interval":12,"recently_missed_count":0,"current_aslot":0,"recent_slots_filled":"340282366920938463463374607431768211455","dynamic_flags":0,"last_irreversible_block_num":0}]}

这次果真没有订阅通知了,因为根本就没有新块!

私链配置

建立genesis.json

Chaim:witness_node Chaim$ ./witness_node --create-genesis-json genesis.json
924289ms th_a       main.cpp:134                  create_new_config_fi ] Writing new config file at /Users/Chaim/Documents/workspace/bitshares-core/build/programs/witness_node/witness_node_data_dir/config.ini
Creating example genesis state in file genesis.json

这个genesis.json在bitshares研究系列【operation fee】这篇文章中已有所介绍,通过genesis.json还可以修改资产初始配置、链初始参数、见证人、董事会等参数。

链ID是gensis的哈希值,所以旦这个文件被修改链ID也会改变,而只有相同的链ID节点才有可能同步。

建立数据目录

Chaim:witness_node Chaim$ ./witness_node --genesis-json genesis.json --seed-nodes "[]"
1269852ms th_a       witness.cpp:87                plugin_initialize    ] witness plugin:  plugin_initialize() begin
1269854ms th_a       witness.cpp:97                plugin_initialize    ] Public Key: BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
1269855ms th_a       witness.cpp:115               plugin_initialize    ] witness plugin:  plugin_initialize() end
1269855ms th_a       db_management.cpp:153         open                 ] Wiping object_database due to missing or wrong version
1269855ms th_a       object_database.cpp:93        wipe                 ] Wiping object database...
1269855ms th_a       object_database.cpp:95        wipe                 ] Done wiping object databse.
1269855ms th_a       object_database.cpp:106       open                 ] Opening object database from /Users/Chaim/Documents/workspace/bitshares-core/build/programs/witness_node/witness_node_data_dir/blockchain ...
1269856ms th_a       object_database.cpp:111       open                 ] Done opening object database.
1269857ms th_a       application.cpp:330           operator()           ] Initializing database...
1269907ms th_a       db_debug.cpp:88               debug_dump           ] total_balances[asset_id_type()].value: 0 core_asset_data.current_supply.value: 1000000000000000 
1269915ms th_a       application.cpp:205           reset_p2p_node       ] Configured p2p node to listen on 0.0.0.0:53381
1269920ms th_a       witness.cpp:120               plugin_startup       ] witness plugin:  plugin_startup() begin
1269920ms th_a       witness.cpp:135               plugin_startup       ] No witnesses configured! Please add witness IDs and private keys to configuration.
1269920ms th_a       witness.cpp:136               plugin_startup       ] witness plugin:  plugin_startup() end
1269920ms th_a       main.cpp:266                  main                 ] Started BitShares node on a chain with 0 blocks.
1269920ms th_a       main.cpp:267                  main                 ] Chain ID is daa4773ef922acf907828b63c308eda9b0ee6d59133d890cac50e0d40f2b259d

这种情况下启动的链是没有新块产生的,提示里也有说明要进行节点配置。

节点配置

修改config.ini,如下:

p2p-endpoint = 127.0.0.1:11010
rpc-endpoint = 127.0.0.1:11011

genesis-json = genesis/my-genesis.json

private-key = ["GPH6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV","5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"]

witness-id = "1.6.1"
witness-id = "1.6.2"
witness-id = "1.6.3"
witness-id = "1.6.4"
witness-id = "1.6.5"
witness-id = "1.6.6"
witness-id = "1.6.7"
witness-id = "1.6.8"
witness-id = "1.6.9"
witness-id = "1.6.10"
witness-id = "1.6.11"

运行:

Chaim:witness_node Chaim$ ./witness_node --data-dir=witness_node_data_dir --enable-stale-production --seed-nodes "[]"
3175715ms th_a       main.cpp:120                  load_config_file     ] Error parsing logging config from config file /Users/Chaim/Documents/workspace/bitshares-core/build/programs/witness_node/witness_node_data_dir/config.ini, using default config
3175715ms th_a       witness.cpp:87                plugin_initialize    ] witness plugin:  plugin_initialize() begin
3175715ms th_a       witness.cpp:97                plugin_initialize    ] Public Key: BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
3175716ms th_a       witness.cpp:115               plugin_initialize    ] witness plugin:  plugin_initialize() end
3175716ms th_a       object_database.cpp:106       open                 ] Opening object database from /Users/Chaim/Documents/workspace/bitshares-core/build/programs/witness_node/witness_node_data_dir/blockchain ...
3175752ms th_a       object_database.cpp:111       open                 ] Done opening object database.
3175755ms th_a       application.cpp:205           reset_p2p_node       ] Configured p2p node to listen on 127.0.0.1:11010
3175755ms th_a       application.cpp:282           reset_websocket_serv ] Configured websocket rpc to listen on 127.0.0.1:11011
3175760ms th_a       witness.cpp:120               plugin_startup       ] witness plugin:  plugin_startup() begin
3175760ms th_a       witness.cpp:125               plugin_startup       ] Launching block production for 11 witnesses.

********************************
*                              *
*   ------- NEW CHAIN ------   *
*   - Welcome to Graphene! -   *
*   ------------------------   *
*                              *
********************************

Your genesis seems to have an old timestamp
Please consider using the --genesis-timestamp option to give your genesis a recent timestamp

3175760ms th_a       witness.cpp:136               plugin_startup       ] witness plugin:  plugin_startup() end
3175760ms th_a       main.cpp:266                  main                 ] Started BitShares node on a chain with 0 blocks.
3175761ms th_a       main.cpp:267                  main                 ] Chain ID is daa4773ef922acf907828b63c308eda9b0ee6d59133d890cac50e0d40f2b259d
3176002ms th_a       witness.cpp:197               block_production_loo ] Not producing block because node didn't wake up within 500ms of the slot time.
3177000ms th_a       witness.cpp:197               block_production_loo ] Not producing block because node didn't wake up within 500ms of the slot time.
3178005ms th_a       witness.cpp:197               block_production_loo ] Not producing block because node didn't wake up within 500ms of the slot time.
3179000ms th_a       witness.cpp:197               block_production_loo ] Not producing block because node didn't wake up within 500ms of the slot time.
3180012ms th_a       witness.cpp:181               block_production_loo ] Generated block #1 with timestamp 2018-04-28T07:53:00 at time 2018-04-28T07:53:00
3200006ms th_a       witness.cpp:181               block_production_loo ] Generated block #2 with timestamp 2018-04-28T07:53:20 at time 2018-04-28T07:53:20
3205001ms th_a       witness.cpp:181               block_production_loo ] Generated block #3 with timestamp 2018-04-28T07:53:25 at time 2018-04-28T07:53:25
3210006ms th_a       witness.cpp:181               block_production_loo ] Generated block #4 with timestamp 2018-04-28T07:53:30 at time 2018-04-28T07:53:30
3215002ms th_a       witness.cpp:181               block_production_loo ] Generated block #5 with timestamp 2018-04-28T07:53:35 at time 2018-04-28T07:53:35

成功看到“NEW CHAIN”标志了,也可以产生新块了!

至于出现的“Not producing block because node didn't wake up within 500ms of the slot time.”的错误,也可以在代码中找一下:

   if( llabs((scheduled_time - now).count()) > fc::milliseconds( 500 ).count() )
   {
      capture("scheduled_time", scheduled_time)("now", now);
      return block_production_condition::lag;
   }

看起来这个只是时间引起的一个错误,正常了应该就没问题了!

cli_wallet

bitshares研究系列【初识cli_wallet】
这篇文章已经说过cli_wallet基本使用了,连上去看看:

Chaim:cli_wallet Chaim$ ./cli_wallet --server-rpc-endpoint=ws://127.0.0.1:11011 --chain-id daa4773ef922acf907828b63c308eda9b0ee6d59133d890cac50e0d40f2b259d
Logging RPC to file: logs/rpc/rpc.log
654017ms th_a       main.cpp:136                  main                 ] key_to_wif( committee_private_key ): 5KCBDTcyDqzsqehcb52tW5nU6pXife6V2rX9Yf7c3saYSzbDZ5W 
654018ms th_a       main.cpp:140                  main                 ] nathan_pub_key: BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV 
654018ms th_a       main.cpp:141                  main                 ] key_to_wif( nathan_private_key ): 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3 
Chain ID in wallet file does not match specified chain ID

又报这个错误,轻车熟路了,把wallet.json删除,重新运行:

Chaim:cli_wallet Chaim$ rm wallet.json 
Chaim:cli_wallet Chaim$ ./cli_wallet --server-rpc-endpoint=ws://127.0.0.1:11011 --chain-id daa4773ef922acf907828b63c308eda9b0ee6d59133d890cac50e0d40f2b259d
Logging RPC to file: logs/rpc/rpc.log
960419ms th_a       main.cpp:136                  main                 ] key_to_wif( committee_private_key ): 5KCBDTcyDqzsqehcb52tW5nU6pXife6V2rX9Yf7c3saYSzbDZ5W 
960420ms th_a       main.cpp:140                  main                 ] nathan_pub_key: BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV 
960420ms th_a       main.cpp:141                  main                 ] key_to_wif( nathan_private_key ): 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3 
Starting a new wallet with chain ID daa4773ef922acf907828b63c308eda9b0ee6d59133d890cac50e0d40f2b259d (from CLI)
960420ms th_a       main.cpp:188                  main                 ] wdata.ws_server: ws://127.0.0.1:11011 
960422ms th_a       main.cpp:193                  main                 ] wdata.ws_user:  wdata.ws_password:  
Please use the set_password method to initialize a new wallet before continuing

可以在这个私链上随意工作了!

cli_wallet命令

以下测试了一些命令,全拷贝上来凑字数吗?真不是,这个结果可能某些时候需要看一下,例如看看operation的执行数据格式。反正我现在有Bandwidth,哈哈!

suggest_brain_key

unlocked >>> suggest_brain_key
suggest_brain_key
{
  "brain_priv_key": "CRIPPLY COTYPE FENSIVE DRONA REVOKER UNWHEEL PLEROME KANGA EXPOSER COTYPE CHALLIE ACIFORM SCOPATE BEVER GLASS UPBROOK",
  "wif_priv_key": "5JPiy3voZD1qSgZsBPZCczyEkmKQvZ4DE9MLxfxfEthbpe83ELX",
  "pub_key": "BTS5g2awh79XezJE7VaKvyjVPK5FVaFCxnDnJyGTFU186q36xG2Qq"
}

create_account_with_brain_key

unlocked >>> create_account_with_brain_key "CRIPPLY COTYPE FENSIVE DRONA REVOKER UNWHEEL PLEROME KANGA EXPOSER COTYPE CHALLIE ACIFORM SCOPATE BEVER GLASS UPBROOK" barnard nathan nathan true
create_account_with_brain_key "CRIPPLY COTYPE FENSIVE DRONA REVOKER UNWHEEL PLEROME KANGA EXPOSER COTYPE CHALLIE ACIFORM SCOPATE BEVER GLASS UPBROOK" barnard nathan nathan true
1754278ms th_a       wallet.cpp:779                save_wallet_file     ] saving wallet to file wallet.json
{
  "ref_block_num": 432,
  "ref_block_prefix": 4191092752,
  "expiration": "2018-04-28T08:29:40",
  "operations": [[
      5,{
        "fee": {
          "amount": 200014257,
          "asset_id": "1.3.0"
        },
        "registrar": "1.2.17",
        "referrer": "1.2.17",
        "referrer_percent": 0,
        "name": "barnard",
        "owner": {
          "weight_threshold": 1,
          "account_auths": [],
          "key_auths": [[
              "BTS5g2awh79XezJE7VaKvyjVPK5FVaFCxnDnJyGTFU186q36xG2Qq",
              1
            ]
          ],
          "address_auths": []
        },
        "active": {
          "weight_threshold": 1,
          "account_auths": [],
          "key_auths": [[
              "BTS6fD2trehbSEch3F15h5EyUi7m7KqKoqPdKuLuK5J6TMkqJ458b",
              1
            ]
          ],
          "address_auths": []
        },
        "options": {
          "memo_key": "BTS6mPfuvmZjShy5Bfco17ASkDkT5pfQ375WNy8YLCnsRfEnjDous",
          "voting_account": "1.2.5",
          "num_witness": 0,
          "num_committee": 0,
          "votes": [],
          "extensions": []
        },
        "extensions": {}
      }
    ]
  ],
  "extensions": [],
  "signatures": [
    "20142736d9b1e432671767dc27609f4a5ae82a02d59fd49645f1dc313a4dc9b8863fdac49d7578e3ae1b4d7e83e849b1505d48b3546e617622550e3300937d93e9"
  ]
}

transfer

unlocked >>> transfer nathan barnard 2000000000 BTS "here is some cash" true
transfer nathan barnard 2000000000 BTS "here is some cash" true
{
  "ref_block_num": 440,
  "ref_block_prefix": 3279184194,
  "expiration": "2018-04-28T08:30:20",
  "operations": [[
      0,{
        "fee": {
          "amount": 2105468,
          "asset_id": "1.3.0"
        },
        "from": "1.2.17",
        "to": "1.2.18",
        "amount": {
          "amount": "200000000000000",
          "asset_id": "1.3.0"
        },
        "memo": {
          "from": "BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
          "to": "BTS6mPfuvmZjShy5Bfco17ASkDkT5pfQ375WNy8YLCnsRfEnjDous",
          "nonce": "17223289879256757094",
          "message": "eda186d271f055543997859aa2b70463d9c2712dee9a3ed49f47a788e449463f"
        },
        "extensions": []
      }
    ]
  ],
  "extensions": [],
  "signatures": [
    "20176d7d2d2256dfd8ce273fe5bc94077355bee3f575e6f56a132cc74ade761ffa7d2955bdfcd27cb3c0b78e55811fea9adbc41067af33f6fda7710bb89e3be1a6"
  ]
}

list_account_balances

unlocked >>> list_account_balances barnard          
list_account_balances barnard
2000000000 BTS

upgrade_account

unlocked >>> upgrade_account barnard true
upgrade_account barnard true
{
  "ref_block_num": 593,
  "ref_block_prefix": 1425737919,
  "expiration": "2018-04-28T08:43:05",
  "operations": [[
      8,{
        "fee": {
          "amount": 1000000000,
          "asset_id": "1.3.0"
        },
        "account_to_upgrade": "1.2.18",
        "upgrade_to_lifetime_member": true,
        "extensions": []
      }
    ]
  ],
  "extensions": [],
  "signatures": [
    "1f38d3171583fab9bb2c60447c4ddc3fd5a87e93acd96b320f4b0d3eab0cc2334e5719be169931395a72ac9d16cc825bc3749b0e5c73987bdec2a7c19b39a7798a"
  ]
}

create_witness

unlocked >>> create_witness barnard "http://www.barnard" true
create_witness barnard "http://www.barnard" true
{
  "ref_block_num": 659,
  "ref_block_prefix": 1089995618,
  "expiration": "2018-04-28T08:48:35",
  "operations": [[
      20,{
        "fee": {
          "amount": 500000000,
          "asset_id": "1.3.0"
        },
        "witness_account": "1.2.18",
        "url": "http://www.barnard",
        "block_signing_key": "BTS57DnkAgDKRL8CirBjc9PN83HLXrjxKwPHKVeFEpSceK1YdkLy6"
      }
    ]
  ],
  "extensions": [],
  "signatures": [
    "1f1c5d26af98fe99359e36cb9aac5fa6f3e01d6758142df29f29d04963dd344c9e6339048f8c52477c9f22273b2d1236681e8c8f895245e3e640f4b4836008f73e"
  ]
}

create_account_with_brain_key

unlocked >>> create_account_with_brain_key com0 com0 nathan nathan true
create_account_with_brain_key com0 com0 nathan nathan true
2345645ms th_a       wallet.cpp:779                save_wallet_file     ] saving wallet to file wallet.json
{
  "ref_block_num": 551,
  "ref_block_prefix": 2502641622,
  "expiration": "2018-04-28T08:39:35",
  "operations": [[
      5,{
        "fee": {
          "amount": 513964,
          "asset_id": "1.3.0"
        },
        "registrar": "1.2.17",
        "referrer": "1.2.17",
        "referrer_percent": 0,
        "name": "com0",
        "owner": {
          "weight_threshold": 1,
          "account_auths": [],
          "key_auths": [[
              "BTS5ZYQtrYUPk5ELqJfygsJHnTJ9wDQmFZK4UXH8d6Txqsjg9MdBc",
              1
            ]
          ],
          "address_auths": []
        },
        "active": {
          "weight_threshold": 1,
          "account_auths": [],
          "key_auths": [[
              "BTS5arjFbgDWrqxhuqvvUGpLFCguse3ebKVC4XGcjUnDgdtNT22Dd",
              1
            ]
          ],
          "address_auths": []
        },
        "options": {
          "memo_key": "BTS67M2hU4tdqD53DdiUxsNaPrRehq3q8zvgVuwfVy1umu7EDqQLU",
          "voting_account": "1.2.5",
          "num_witness": 0,
          "num_committee": 0,
          "votes": [],
          "extensions": []
        },
        "extensions": {}
      }
    ]
  ],
  "extensions": [],
  "signatures": [
    "20497e2ad20dee2ec1fbb324f434640e290d787dd27ee9f1113e87235ed1920c4b1a7e2469185a11965dcc735a2f0154292d2adf652617a447fa867f93313d86e3"
  ]
}

create_committee_member

unlocked >>> create_committee_member barnard "http://www.barnard" true
create_committee_member barnard "http://www.barnard" true
{
  "ref_block_num": 707,
  "ref_block_prefix": 2667516363,
  "expiration": "2018-04-28T08:52:35",
  "operations": [[
      29,{
        "fee": {
          "amount": 500000000,
          "asset_id": "1.3.0"
        },
        "committee_member_account": "1.2.18",
        "url": "http://www.barnard"
      }
    ]
  ],
  "extensions": [],
  "signatures": [
    "20378c07b19451f0a035f1f6400de8c2cbec7d839d12213ae8c38580bdff62a71241d52e1b4a9a2c0fc3b83c10cb74e6323a431ca9219b62475cae7a3aa4e7ea05"
  ]
}

vote_for_committee_member

unlocked >>> vote_for_committee_member nathan barnard true true
vote_for_committee_member nathan barnard true true
{
  "ref_block_num": 716,
  "ref_block_prefix": 2382867450,
  "expiration": "2018-04-28T08:53:20",
  "operations": [[
      6,{
        "fee": {
          "amount": 2005664,
          "asset_id": "1.3.0"
        },
        "account": "1.2.17",
        "new_options": {
          "memo_key": "BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
          "voting_account": "1.2.5",
          "num_witness": 0,
          "num_committee": 0,
          "votes": [
            "0:23"
          ],
          "extensions": []
        },
        "extensions": {}
      }
    ]
  ],
  "extensions": [],
  "signatures": [
    "201db36fb4f2726df7f201d281961bac510e952adc0e63246431b9426d9eb0a522618e9cb56f3ce6a38103492808f1a8f30743fb4bc11e0aa2c5750c1c4ece849c"
  ]
}

结论

私链开始运行时,可能已经存在数据目录,导致一些问题。另外指定参数 --seed-nodes "[]" 禁止了连接其它节点,导致也无法更新块!

如果碰到一些莫明其妙的错误,建议把数据目录删掉重来!

同样的道理,我们编译一些代码时也有类似的问题,实在整不明白了,重新建个目录重新下代码重新走一遍流程也是个好方法!

参考

https://github.com/bitshares/bitshares-core/wiki/private-testnet

http://docs.bitshares.org/testnet/private-testnet.html

http://docs.bitshares.org/testnet/3-init-blockchain.html


感谢您阅读 @chaimyu 的帖子,期待您能留言交流!

Sort:  

你好!cn区点赞机器人 @cnbuddy 很开心你能成为cn区的一员。如果不想再收到我的留言,请回复“取消”。

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.032
BTC 59263.92
ETH 2580.97
USDT 1.00
SBD 2.46