EOS跨鏈http通訊

操作系統(tǒng):MAC OS 10.13.x荔燎,EOSIO版本:1.1.3

在EOS側(cè)鏈的開發(fā)過程中捕犬,必然需要側(cè)鏈與主鏈之間的通訊米死。請先看我們的系列文章:EOS跨鏈轉(zhuǎn)賬 - 簡書具垫。

1.過程

在“EOS跨鏈轉(zhuǎn)賬”中欺冀,主鏈的同步節(jié)點(同步鏈)在確認(rèn)賬戶A到合約賬號M的轉(zhuǎn)賬不可撤銷后,通過http向測鏈節(jié)點發(fā)送一個action萨脑。

            void send_transaction(const block_state_ptr& irb) {
                auto& chain = chain_plug->chain();
                auto& db = chain.db();

                const auto& trmi = db.get_index<transaction_reversible_multi_index, by_block_num>();
                auto itr = trmi.begin();
                while( itr != trmi.end()) {
                    if (itr->block_num <= irb->block_num) {
                        auto data = fc::raw::unpack<cactus_transfer>(itr->data);
                        // need to validate ?

                        // send propose or approve
                        string datastr = DATA_FORMAT(_peer_chain_account, string(itr->trx_id), string(data.to), data.quantity.to_string());
                        vector<string> permissions = {_peer_chain_account};
                        try {
                            app().find_plugin<client_plugin>()->get_client_apis().push_action(_peer_chain_address, _peer_chain_constract,"msigtrans", datastr, permissions);
                        } catch (...) {
                            wlog("send transaction failed");
                        }

                        // remove or move to other table ?
                        db.remove(*itr);
                    }
                    ++ itr;
                }
            }

上述send_transaction中隐轩,app().find_plugin()->get_client_apis().push_action(_peer_chain_address, _peer_chain_constract, "msigtrans", datastr, permissions);即為client_plugin提供的push_action方法。入?yún)⑴cEOSIO的命令行cleos push action account action data類似渤早,增加了一個url职车,此url為側(cè)鏈上的一個節(jié)點地址如:http://192.168.31.91:8888。

2.實現(xiàn)

為了實現(xiàn)主鏈同步節(jié)點在同步過程中向側(cè)鏈查詢或者發(fā)送交易的功能鹊杖,我們寫了一個client_plugin悴灵,專門負(fù)責(zé)處理發(fā)送http請求。
client_plugin中push_action與EOSIO中的program/cleos/main 的push_action類似骂蓖,這為后續(xù)擴展其他查詢/http請求提供方便积瞒。具體代碼如下

 void client_cactus::push_action(const std::string& url, string contract_account, string action, string data, const vector<string>& tx_permission ){
          fc::variant action_args_var;
          if( !data.empty() ) {
              try {
                  action_args_var = json_from_file_or_string(data, fc::json::relaxed_parser);
              } EOS_RETHROW_EXCEPTIONS(action_type_exception, "Fail to parse action JSON data='${data}'", ("data", data))
          }
          auto arg= fc::mutable_variant_object
                    ("code", contract_account)
                    ("action", action)
                    ("args", action_args_var);
          auto result = call(url, json_to_bin_func, arg);
          auto accountPermissions = get_account_permissions(tx_permission); 
          send_actions(url, {eosio::chain::action{accountPermissions, contract_account, action, result.get_object()["binargs"].as<bytes>()}});
    }
    void client_cactus::send_actions(const std::string& url, std::vector<chain::action>&& actions, int32_t extra_kcpu, packed_transaction::compression_type compression) {
          auto result = push_actions( url, move(actions), extra_kcpu, compression);
          std::cout << fc::json::to_pretty_string( result ) << std::endl;
    }

    fc::variant client_cactus::push_actions(const std::string& url, std::vector<chain::action>&& actions, int32_t extra_kcpu, packed_transaction::compression_type compression) {
        signed_transaction trx;
        trx.actions = std::forward<decltype(actions)>(actions);
        return push_transaction(url, trx, extra_kcpu, compression);
    }

    fc::variant client_cactus::push_transaction( const std::string& url, signed_transaction& trx, int32_t extra_kcpu, packed_transaction::compression_type compression) {
        auto info = get_info(url);
        trx.expiration = info.head_block_time + tx_expiration;

        // Set tapos, default to last irreversible block
        trx.set_reference_block(info.last_irreversible_block_id);

        trx.max_cpu_usage_ms = tx_max_net_usage;
        trx.max_net_usage_words = (tx_max_net_usage + 7)/8;

        sign_transaction_local(trx, client_prikey, info.chain_id);//本地對交易簽名,沒有使用錢包登下!后續(xù)可以使用錢包來對交易簽名
        return call(url, push_txn_func, packed_transaction(trx, compression));
    }

不使用錢包茫孔,本地簽名實現(xiàn):

chain_id_type& chain_id) {
         optional<signature_type> sig = private_key.sign(trx.sig_digest(chain_id, trx.context_free_data));
         if (sig) {
            trx.signatures.push_back(*sig);
         }
    }

使用EOSIO的call實現(xiàn)do_http_call( )請求

    template<typename T>
    fc::variant client_cactus::call( const std::string& url,
                      const std::string& path,
                      const T& v ) {
        static http_context context = create_http_context();
        auto urlpath = parse_url(url) + path;
        connection_param *cp = new connection_param(context, urlpath, false);
        return do_http_call( *cp, fc::variant(v), false, false );
    }

3. 啟動client_plugin

需要配置如下參數(shù):
client-private-key 見證人的私鑰
--client-private-key 5K8MzDTmBKfGWE5wDpTcpmMimHH2SzFADjmSkvJe47RWHv3nbke
具體演示結(jié)果見EOS跨鏈轉(zhuǎn)賬 - 簡書

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市被芳,隨后出現(xiàn)的幾起案子缰贝,更是在濱河造成了極大的恐慌,老刑警劉巖畔濒,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件剩晴,死亡現(xiàn)場離奇詭異,居然都是意外死亡侵状,警方通過查閱死者的電腦和手機赞弥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來壹将,“玉大人嗤攻,你說我怎么就攤上這事毛嫉》谈” “怎么了?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵承粤,是天一觀的道長暴区。 經(jīng)常有香客問我,道長辛臊,這世上最難降的妖魔是什么仙粱? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任翰铡,我火速辦了婚禮俊扭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘笙隙。我一直安慰自己,他們只是感情好隔心,可當(dāng)我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布白群。 她就那樣靜靜地躺著,像睡著了一般硬霍。 火紅的嫁衣襯著肌膚如雪帜慢。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天唯卖,我揣著相機與錄音粱玲,去河邊找鬼。 笑死拜轨,一個胖子當(dāng)著我的面吹牛抽减,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播撩轰,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼胯甩,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了堪嫂?” 一聲冷哼從身側(cè)響起偎箫,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎皆串,沒想到半個月后淹办,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡恶复,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年怜森,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谤牡。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡副硅,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出翅萤,到底是詐尸還是另有隱情恐疲,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布套么,位于F島的核電站培己,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏胚泌。R本人自食惡果不足惜省咨,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望玷室。 院中可真熱鬧零蓉,春花似錦笤受、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至紊册,卻和暖如春比肄,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背囊陡。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工芳绩, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人撞反。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓妥色,卻偏偏與公主長得像,于是被迫代替她去往敵國和親遏片。 傳聞我的和親對象是個殘疾皇子嘹害,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內(nèi)容