美文网首页
Eos源码阅读(cleos部分-创建账户)

Eos源码阅读(cleos部分-创建账户)

作者: 坠叶飘香 | 来源:发表于2019-02-14 17:05 被阅读0次

1.封装创建账户的actions

根据参数simple的值会封装相应的action:create action,buyram action,delegate action

eos/programs/cleos/main.cpp

create_account_subcommand{
    //create action
    auto create = create_newaccount(creator, account_name, owner_key, active_key); 
    if (!simple) {
        //创建 buyram的action
        action buyram = !buy_ram_eos.empty() ? create_buyram(creator, account_name, to_asset(buy_ram_eos))
                  : create_buyrambytes(creator, account_name, (buy_ram_bytes_in_kbytes) ? (buy_ram_bytes_in_kbytes * 1024) :       
                     buy_ram_bytes);
        if ( net.get_amount() != 0 || cpu.get_amount() != 0 ) {
                  //创建delegate的action
                  action delegate = create_delegate( creator, account_name, net, cpu, transfer);
                  send_actions( { create, buyram, delegate } );   //三个action
         } else {
                  send_actions( { create, buyram } );  //两个action
        }
    } else {
        send_actions( { create } ); //这种情况下只构建一个create action
     }
}

2. 调用push_actions

void send_actions(std::vector<chain::action>&& actions, int32_t extra_kcpu = 1000, packed_transaction::compression_type compression = packed_transaction::none ) {
   auto result = push_actions( move(actions), extra_kcpu, compression);

   if( tx_print_json ) {
      cout << fc::json::to_pretty_string( result ) << endl;
   } else {
      print_result( result );
   }
}

3.调用push_transaction

构建signed_transaction,将actions赋值到signed_transaction的成员actions
fc::variant push_actions(std::vector<chain::action>&& actions, int32_t extra_kcpu, packed_transaction::compression_type compression = packed_transaction::none ) {
   //构建signed_transaction,赋值它的actions
   signed_transaction trx;
   trx.actions = std::forward<decltype(actions)>(actions);

   return push_transaction(trx, extra_kcpu, compression);
}

4. 调用push_txn_func

signed_transaction -> packed_transaction
push_txn_func:"/v1/chain/push_transaction"
fc::variant push_transaction( signed_transaction& trx, int32_t extra_kcpu = 1000, packed_transaction::compression_type compression = packed_transaction::none ) {
    //对交易进行签名
    if (!tx_skip_sign) {
      auto required_keys = determine_required_keys(trx);
      sign_transaction(trx, required_keys, info.chain_id);
   }
    return call(push_txn_func, packed_transaction(trx, compression)); //signed_transaction -> packed_transaction
}

5.发起网络请求,请求nodeos处理

参数:
url:nodeos
path:push_txn_func
v:packed_transaction
template<typename T>
fc::variant call( const std::string& url,
                  const std::string& path,
                  const T& v ) {
   try {
      auto sp = std::make_unique<eosio::client::http::connection_param>(context, parse_url(url) + path, no_verify ? false : true, 
                                     headers);
      return eosio::client::http::do_http_call(*sp, fc::variant(v), print_request, print_response );  //发起网络请求
   } catch(boost::system::system_error& e) {
      if(url == ::url)
         std::cerr << localized("Failed to connect to nodeos at ${u}; is nodeos running?", ("u", url)) << std::endl;
      else if(url == ::wallet_url)
         std::cerr << localized("Failed to connect to keosd at ${u}; is keosd running?", ("u", url)) << std::endl;
      throw connection_exception(fc::log_messages{FC_LOG_MESSAGE(error, e.what())});
   }
}

相关文章

网友评论

      本文标题:Eos源码阅读(cleos部分-创建账户)

      本文链接:https://www.haomeiwen.com/subject/tgpeeqtx.html