From a92071890b59ec522a3aa097b61f4991b6954eff Mon Sep 17 00:00:00 2001 From: Srdjan Obucina Date: Sat, 29 Feb 2020 22:04:29 +0100 Subject: [PATCH] Bitcon RFC logs improved, proposal aprovement improved --- .../sidechain_net_handler_bitcoin.hpp | 4 +- .../peerplays_sidechain_plugin.cpp | 12 ++++ .../sidechain_net_handler_bitcoin.cpp | 64 +++++++++---------- 3 files changed, 46 insertions(+), 34 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp index 9c05c802..ec9689f6 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp @@ -39,10 +39,10 @@ public: std::string signrawtransactionwithwallet(const std::string &tx_hash); std::string unloadwallet(const std::string &filename); std::string walletlock(); - std::string walletpassphrase(const std::string &passphrase, uint32_t timeout = 5); + bool walletpassphrase(const std::string &passphrase, uint32_t timeout = 60); private: - fc::http::reply send_post_request(std::string body); + fc::http::reply send_post_request(std::string body, bool show_log = false); std::string ip; uint32_t rpc_port; diff --git a/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp b/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp index a20de6c1..23f7193d 100644 --- a/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp +++ b/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp @@ -372,12 +372,24 @@ void peerplays_sidechain_plugin_impl::approve_proposals() { }; const auto &idx = plugin.database().get_index_type().indices().get(); + vector proposals; for (const auto &proposal : idx) { + proposals.push_back(proposal.id); + } + + for (const auto proposal_id : proposals) { for (son_id_type son_id : sons) { if (!is_active_son(son_id)) { continue; } + const object *obj = plugin.database().find_object(proposal_id); + const chain::proposal_object *proposal_ptr = dynamic_cast(obj); + if (proposal_ptr == nullptr) { + continue; + } + const proposal_object proposal = *proposal_ptr; + if (proposal.available_active_approvals.find(get_son_object(son_id).son_account) != proposal.available_active_approvals.end()) { continue; } diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp index 90ab2b4a..38a907a1 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp @@ -66,7 +66,7 @@ std::string bitcoin_rpc_client::addmultisigaddress(const std::vector bitcoin_rpc_client::listunspent() { } } } else if (json.count("error") && !json.get_child("error").empty()) { - wlog("Failed to list unspent txo! Reply: ${msg}", ("msg", ss.str())); + wlog("Bitcoin RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body)("msg", ss.str())); } return result; } @@ -314,7 +312,7 @@ std::vector bitcoin_rpc_client::listunspent_by_address_and_amount(con } } } else if (json.count("error") && !json.get_child("error").empty()) { - wlog("Failed to list unspent txo! Reply: ${msg}", ("msg", ss.str())); + wlog("Bitcoin RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body)("msg", ss.str())); } return result; } @@ -342,7 +340,7 @@ std::string bitcoin_rpc_client::loadwallet(const std::string &filename) { } if (json.count("error") && !json.get_child("error").empty()) { - wlog("Bitcoin RPC call ${function} failed with reply '${msg}'", ("function", __FUNCTION__)("msg", ss.str())); + wlog("Bitcoin RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body)("msg", ss.str())); } return ""; } @@ -352,8 +350,6 @@ void bitcoin_rpc_client::sendrawtransaction(const std::string &tx_hex) { "\"method\": \"sendrawtransaction\", \"params\": [") + std::string("\"") + tx_hex + std::string("\"") + std::string("] }"); - ilog(body); - const auto reply = send_post_request(body); if (reply.body.empty()) { @@ -372,7 +368,7 @@ void bitcoin_rpc_client::sendrawtransaction(const std::string &tx_hex) { if (error_code == -27) // transaction already in block chain return; - wlog("BTC tx is not sent! Reply: ${msg}", ("msg", ss.str())); + wlog("Bitcoin RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body)("msg", ss.str())); } } @@ -386,8 +382,6 @@ std::string bitcoin_rpc_client::signrawtransactionwithwallet(const std::string & std::string params = "\"" + tx_hash + "\""; body = body + params + std::string("]}"); - ilog(body); - const auto reply = send_post_request(body); if (reply.body.empty()) { @@ -404,7 +398,7 @@ std::string bitcoin_rpc_client::signrawtransactionwithwallet(const std::string & } if (json.count("error") && !json.get_child("error").empty()) { - wlog("BTC sign_raw_transaction_with_wallet failed! Reply: ${msg}", ("msg", ss.str())); + wlog("Bitcoin RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body)("msg", ss.str())); } return ""; } @@ -432,7 +426,7 @@ std::string bitcoin_rpc_client::unloadwallet(const std::string &filename) { } if (json.count("error") && !json.get_child("error").empty()) { - wlog("Bitcoin RPC call ${function} failed with reply '${msg}'", ("function", __FUNCTION__)("msg", ss.str())); + wlog("Bitcoin RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body)("msg", ss.str())); } return ""; } @@ -459,12 +453,12 @@ std::string bitcoin_rpc_client::walletlock() { } if (json.count("error") && !json.get_child("error").empty()) { - wlog("Bitcoin RPC call ${function} failed with reply '${msg}'", ("function", __FUNCTION__)("msg", ss.str())); + wlog("Bitcoin RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body)("msg", ss.str())); } return ""; } -std::string bitcoin_rpc_client::walletpassphrase(const std::string &passphrase, uint32_t timeout) { +bool bitcoin_rpc_client::walletpassphrase(const std::string &passphrase, uint32_t timeout) { std::string body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"walletpassphrase\", \"method\": " "\"walletpassphrase\", \"params\": [\"" + passphrase + "\", " + std::to_string(timeout) + "] }"); @@ -473,7 +467,7 @@ std::string bitcoin_rpc_client::walletpassphrase(const std::string &passphrase, if (reply.body.empty()) { wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__)); - return std::string(); + return false; } std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); @@ -481,18 +475,16 @@ std::string bitcoin_rpc_client::walletpassphrase(const std::string &passphrase, boost::property_tree::read_json(ss, json); if (reply.status == 200) { - std::stringstream ss; - boost::property_tree::json_parser::write_json(ss, json.get_child("result")); - return ss.str(); + return true; } if (json.count("error") && !json.get_child("error").empty()) { - wlog("Bitcoin RPC call ${function} failed with reply '${msg}'", ("function", __FUNCTION__)("msg", ss.str())); + wlog("Bitcoin RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body)("msg", ss.str())); } - return ""; + return false; } -fc::http::reply bitcoin_rpc_client::send_post_request(std::string body) { +fc::http::reply bitcoin_rpc_client::send_post_request(std::string body, bool show_log) { fc::http::connection conn; conn.connect_to(fc::ip::endpoint(fc::ip::address(ip), rpc_port)); @@ -502,7 +494,16 @@ fc::http::reply bitcoin_rpc_client::send_post_request(std::string body) { url = url + "/wallet/" + wallet; } - return conn.request("POST", url, body, fc::http::headers{authorization}); + fc::http::reply reply = conn.request("POST", url, body, fc::http::headers{authorization}); + + if (show_log) { + ilog("Request URL: ${url}", ("url", url)); + ilog("Request: ${body}", ("body", body)); + std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); + ilog("Response: ${ss}", ("ss", ss.str())); + } + + return reply; } // ============================================================================= @@ -707,14 +708,13 @@ std::string sidechain_net_handler_bitcoin::sign_and_send_transaction_with_wallet return ""; } - std::string unsigned_tx_hex = pt.get("result"); - if (!wallet_password.empty()) { - bitcoin_client->walletpassphrase(wallet_password); + bitcoin_client->walletpassphrase(wallet_password, 60); } + std::string unsigned_tx_hex = pt.get("result"); + reply_str = bitcoin_client->signrawtransactionwithwallet(unsigned_tx_hex); - ilog(reply_str); std::stringstream ss_stx(reply_str); boost::property_tree::ptree stx_json; boost::property_tree::read_json(ss_stx, stx_json);