Introduce option to enable/disable printing RPC calls to console

This commit is contained in:
serkixenos 2021-08-18 00:07:03 +02:00
parent acb1432509
commit 73e4a0cc15
8 changed files with 63 additions and 45 deletions

View file

@ -11,11 +11,12 @@
namespace graphene { namespace peerplays_sidechain {
rpc_client::rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password) :
rpc_client::rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password, bool _debug_rpc_calls) :
ip(_ip),
port(_port),
user(_user),
password(_password),
debug_rpc_calls(_debug_rpc_calls),
request_id(0) {
authorization.key = "Authorization";
authorization.val = "Basic " + fc::base64_encode(user + ":" + password);

View file

@ -9,7 +9,7 @@ namespace graphene { namespace peerplays_sidechain {
class rpc_client {
public:
rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password);
rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password, bool _debug_rpc_calls);
protected:
std::string retrieve_array_value_from_reply(std::string reply_str, std::string array_path, uint32_t idx);
@ -20,6 +20,7 @@ protected:
uint32_t port;
std::string user;
std::string password;
bool debug_rpc_calls;
uint32_t request_id;

View file

@ -52,6 +52,8 @@ protected:
graphene::chain::database &database;
sidechain_type sidechain;
bool debug_rpc_calls;
std::map<std::string, std::string> private_keys;
void on_applied_block(const signed_block &b);

View file

@ -21,7 +21,7 @@ public:
class bitcoin_rpc_client {
public:
bitcoin_rpc_client(std::string _ip, uint32_t _rpc, std::string _user, std::string _password, std::string _wallet, std::string _wallet_password);
bitcoin_rpc_client(std::string _ip, uint32_t _rpc, std::string _user, std::string _password, std::string _wallet, std::string _wallet_password, bool _debug_rpc_calls);
std::string addmultisigaddress(const uint32_t nrequired, const std::vector<std::string> public_keys);
std::string combinepsbt(const vector<std::string> &psbts);
@ -51,7 +51,7 @@ public:
//bool walletpassphrase(const std::string &passphrase, uint32_t timeout = 60);
private:
fc::http::reply send_post_request(std::string body, bool show_log = false);
fc::http::reply send_post_request(std::string body, bool show_log);
std::string ip;
uint32_t rpc_port;
@ -59,6 +59,7 @@ private:
std::string password;
std::string wallet;
std::string wallet_password;
bool debug_rpc_calls;
fc::http::header authorization;
};

View file

@ -14,7 +14,7 @@ namespace graphene { namespace peerplays_sidechain {
class hive_node_rpc_client : public rpc_client {
public:
hive_node_rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password);
hive_node_rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password, bool _debug_rpc_calls);
std::string account_history_api_get_transaction(std::string transaction_id);
std::string block_api_get_block(uint32_t block_number);

View file

@ -142,6 +142,8 @@ void peerplays_sidechain_plugin_impl::plugin_set_program_options(
cli.add_options()("sidechain-retry-threshold", bpo::value<uint16_t>()->default_value(150), "Sidechain retry throttling threshold");
cli.add_options()("debug-rpc-calls", bpo::value<bool>()->default_value(false), "Outputs RPC calls to console");
cli.add_options()("bitcoin-sidechain-enabled", bpo::value<bool>()->default_value(false), "Bitcoin sidechain handler enabled");
cli.add_options()("bitcoin-node-ip", bpo::value<string>()->default_value("127.0.0.1"), "IP address of Bitcoin node");
cli.add_options()("bitcoin-node-zmq-port", bpo::value<uint32_t>()->default_value(11111), "ZMQ port of Bitcoin node");
@ -200,7 +202,9 @@ void peerplays_sidechain_plugin_impl::plugin_initialize(const boost::program_opt
}
config_ready_son = config_ready_son && !private_keys.empty();
}
retries_threshold = options.at("sidechain-retry-threshold").as<uint16_t>();
if (options.count("sidechain-retry-threshold")) {
retries_threshold = options.at("sidechain-retry-threshold").as<uint16_t>();
}
ilog("sidechain-retry-threshold: ${sidechain-retry-threshold}", ("sidechain-retry-threshold", retries_threshold));
}
if (!config_ready_son) {

View file

@ -26,13 +26,14 @@ namespace graphene { namespace peerplays_sidechain {
// =============================================================================
bitcoin_rpc_client::bitcoin_rpc_client(std::string _ip, uint32_t _rpc, std::string _user, std::string _password, std::string _wallet, std::string _wallet_password) :
bitcoin_rpc_client::bitcoin_rpc_client(std::string _ip, uint32_t _rpc, std::string _user, std::string _password, std::string _wallet, std::string _wallet_password, bool _debug_rpc_calls) :
ip(_ip),
rpc_port(_rpc),
user(_user),
password(_password),
wallet(_wallet),
wallet_password(_wallet_password) {
wallet_password(_wallet_password),
debug_rpc_calls(_debug_rpc_calls) {
authorization.key = "Authorization";
authorization.val = "Basic " + fc::base64_encode(user + ":" + password);
}
@ -51,7 +52,7 @@ std::string bitcoin_rpc_client::addmultisigaddress(const uint32_t nrequired, con
params = params + pubkeys + std::string("]");
body = body + params + std::string(", null, \"bech32\"] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -83,7 +84,7 @@ std::string bitcoin_rpc_client::combinepsbt(const vector<std::string> &psbts) {
params = params + std::string("\"") + psbt + std::string("\"");
}
body = body + params + std::string("]] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -120,7 +121,7 @@ std::string bitcoin_rpc_client::createmultisig(const uint32_t nrequired, const s
params = params + pubkeys + std::string("]");
body = body + params + std::string(", \"p2sh-segwit\" ] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -162,7 +163,7 @@ std::string bitcoin_rpc_client::createpsbt(const std::vector<btc_txout> &ins, co
}
body += std::string("]] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -206,7 +207,7 @@ std::string bitcoin_rpc_client::createrawtransaction(const std::vector<btc_txout
}
body += std::string("]] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -234,7 +235,7 @@ std::string bitcoin_rpc_client::createwallet(const std::string &wallet_name) {
"\"createwallet\", \"params\": [\"" +
wallet_name + "\"] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -262,7 +263,7 @@ std::string bitcoin_rpc_client::decodepsbt(std::string const &tx_psbt) {
"\"decodepsbt\", \"params\": [\"" +
tx_psbt + "\"] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -290,7 +291,7 @@ std::string bitcoin_rpc_client::decoderawtransaction(std::string const &tx_hex)
"\"decoderawtransaction\", \"params\": [\"" +
tx_hex + "\"] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -318,7 +319,7 @@ std::string bitcoin_rpc_client::encryptwallet(const std::string &passphrase) {
"\"encryptwallet\", \"params\": [\"" +
passphrase + "\"] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -346,7 +347,7 @@ uint64_t bitcoin_rpc_client::estimatesmartfee(uint16_t conf_target) {
"\"method\": \"estimatesmartfee\", \"params\": [" +
std::to_string(conf_target) + std::string("] }"));
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -383,7 +384,7 @@ std::string bitcoin_rpc_client::finalizepsbt(std::string const &tx_psbt) {
"\"finalizepsbt\", \"params\": [\"" +
tx_psbt + "\"] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -409,7 +410,7 @@ std::string bitcoin_rpc_client::getaddressinfo(const std::string &address) {
"\"getaddressinfo\", \"params\": [\"" +
address + "\"] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -437,7 +438,7 @@ std::string bitcoin_rpc_client::getblock(const std::string &block_hash, int32_t
"\"getblock\", \"params\": [\"" +
block_hash + "\", " + std::to_string(verbosity) + "] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -467,7 +468,7 @@ std::string bitcoin_rpc_client::getrawtransaction(const std::string &txid, const
std::string params = "\"" + txid + "\", " + (verbose ? "true" : "false");
body = body + params + "] }";
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -495,7 +496,7 @@ std::string bitcoin_rpc_client::gettransaction(const std::string &txid, const bo
std::string params = "\"" + txid + "\", " + (include_watch_only ? "true" : "false");
body = body + params + "] }";
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -520,7 +521,7 @@ std::string bitcoin_rpc_client::getblockchaininfo() {
std::string body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"getblockchaininfo\", \"method\": "
"\"getblockchaininfo\", \"params\": [] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -553,7 +554,7 @@ void bitcoin_rpc_client::importaddress(const std::string &address_or_script, con
(p2sh ? "true" : "false");
body = body + params + "] }";
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -576,7 +577,7 @@ std::vector<btc_txout> bitcoin_rpc_client::listunspent(const uint32_t minconf, c
"\"listunspent\", \"params\": [" +
std::to_string(minconf) + "," + std::to_string(maxconf) + "] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
std::vector<btc_txout> result;
@ -617,7 +618,7 @@ std::vector<btc_txout> bitcoin_rpc_client::listunspent_by_address_and_amount(con
body += std::to_string(minimum_amount);
body += std::string("} ] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
std::vector<btc_txout> result;
if (reply.body.empty()) {
@ -652,7 +653,7 @@ std::string bitcoin_rpc_client::loadwallet(const std::string &filename) {
"\"loadwallet\", \"params\": [\"" +
filename + "\"] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -680,7 +681,7 @@ std::string bitcoin_rpc_client::sendrawtransaction(const std::string &tx_hex) {
"\"method\": \"sendrawtransaction\", \"params\": [") +
std::string("\"") + tx_hex + std::string("\"") + std::string("] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -707,7 +708,7 @@ std::string bitcoin_rpc_client::signrawtransactionwithwallet(const std::string &
std::string params = "\"" + tx_hash + "\"";
body = body + params + std::string("]}");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -733,7 +734,7 @@ std::string bitcoin_rpc_client::unloadwallet(const std::string &filename) {
"\"unloadwallet\", \"params\": [\"" +
filename + "\"] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -760,7 +761,7 @@ std::string bitcoin_rpc_client::unloadwallet(const std::string &filename) {
// std::string body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"walletlock\", \"method\": "
// "\"walletlock\", \"params\": [] }");
//
// const auto reply = send_post_request(body);
// const auto reply = send_post_request(body, debug_rpc_calls);
//
// if (reply.body.empty()) {
// wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -788,7 +789,7 @@ std::string bitcoin_rpc_client::walletprocesspsbt(std::string const &tx_psbt) {
"\"walletprocesspsbt\", \"params\": [\"" +
tx_psbt + "\"] }");
const auto reply = send_post_request(body);
const auto reply = send_post_request(body, debug_rpc_calls);
if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -814,7 +815,7 @@ std::string bitcoin_rpc_client::walletprocesspsbt(std::string const &tx_psbt) {
// "\"walletpassphrase\", \"params\": [\"" +
// passphrase + "\", " + std::to_string(timeout) + "] }");
//
// const auto reply = send_post_request(body);
// const auto reply = send_post_request(body, debug_rpc_calls);
//
// if (reply.body.empty()) {
// wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
@ -911,6 +912,10 @@ sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(peerplays_sidechain
sidechain_net_handler(_plugin, options) {
sidechain = sidechain_type::bitcoin;
if (options.count("debug-rpc-calls")) {
debug_rpc_calls = options.at("debug-rpc-calls").as<bool>();
}
ip = options.at("bitcoin-node-ip").as<std::string>();
zmq_port = options.at("bitcoin-node-zmq-port").as<uint32_t>();
rpc_port = options.at("bitcoin-node-rpc-port").as<uint32_t>();
@ -945,7 +950,7 @@ sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(peerplays_sidechain
FC_ASSERT(false);
}
bitcoin_client = std::unique_ptr<bitcoin_rpc_client>(new bitcoin_rpc_client(ip, rpc_port, rpc_user, rpc_password, wallet, wallet_password));
bitcoin_client = std::unique_ptr<bitcoin_rpc_client>(new bitcoin_rpc_client(ip, rpc_port, rpc_user, rpc_password, wallet, wallet_password, debug_rpc_calls));
if (!wallet.empty()) {
bitcoin_client->loadwallet(wallet);
}

View file

@ -29,18 +29,18 @@
namespace graphene { namespace peerplays_sidechain {
hive_node_rpc_client::hive_node_rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password) :
rpc_client(_ip, _port, _user, _password) {
hive_node_rpc_client::hive_node_rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password, bool _debug_rpc_calls) :
rpc_client(_ip, _port, _user, _password, _debug_rpc_calls) {
}
std::string hive_node_rpc_client::account_history_api_get_transaction(std::string transaction_id) {
std::string params = "{ \"id\": \"" + transaction_id + "\" }";
return send_post_request("account_history_api.get_transaction", params, false);
return send_post_request("account_history_api.get_transaction", params, debug_rpc_calls);
}
std::string hive_node_rpc_client::block_api_get_block(uint32_t block_number) {
std::string params = "{ \"block_num\": " + std::to_string(block_number) + " }";
return send_post_request("block_api.get_block", params, false);
return send_post_request("block_api.get_block", params, debug_rpc_calls);
}
std::string hive_node_rpc_client::condenser_api_get_accounts(std::vector<std::string> accounts) {
@ -52,25 +52,25 @@ std::string hive_node_rpc_client::condenser_api_get_accounts(std::vector<std::st
params = "\"" + account + "\"";
}
params = "[[" + params + "]]";
return send_post_request("condenser_api.get_accounts", params, false);
return send_post_request("condenser_api.get_accounts", params, debug_rpc_calls);
}
std::string hive_node_rpc_client::condenser_api_get_config() {
std::string params = "[]";
return send_post_request("condenser_api.get_config", params, false);
return send_post_request("condenser_api.get_config", params, debug_rpc_calls);
}
std::string hive_node_rpc_client::database_api_get_dynamic_global_properties() {
return send_post_request("database_api.get_dynamic_global_properties", "", false);
return send_post_request("database_api.get_dynamic_global_properties", "", debug_rpc_calls);
}
std::string hive_node_rpc_client::database_api_get_version() {
return send_post_request("database_api.get_version", "", false);
return send_post_request("database_api.get_version", "", debug_rpc_calls);
}
std::string hive_node_rpc_client::network_broadcast_api_broadcast_transaction(std::string htrx) {
std::string params = "{ \"trx\": " + htrx + ", \"max_block_age\": -1 }";
return send_post_request("network_broadcast_api.broadcast_transaction", params, false);
return send_post_request("network_broadcast_api.broadcast_transaction", params, debug_rpc_calls);
}
std::string hive_node_rpc_client::get_account(std::string account) {
@ -115,6 +115,10 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi
sidechain_net_handler(_plugin, options) {
sidechain = sidechain_type::hive;
if (options.count("debug-rpc-calls")) {
debug_rpc_calls = options.at("debug-rpc-calls").as<bool>();
}
node_ip = options.at("hive-node-ip").as<std::string>();
node_rpc_port = options.at("hive-node-rpc-port").as<uint32_t>();
if (options.count("hive-node-rpc-user")) {
@ -148,7 +152,7 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi
FC_ASSERT(false);
}
node_rpc_client = new hive_node_rpc_client(node_ip, node_rpc_port, node_rpc_user, node_rpc_password);
node_rpc_client = new hive_node_rpc_client(node_ip, node_rpc_port, node_rpc_user, node_rpc_password, debug_rpc_calls);
std::string chain_id_str = node_rpc_client->get_chain_id();
chain_id = chain_id_type(chain_id_str);