Merge branch 'develop' into feature/son-for-ethereum

This commit is contained in:
serkixenos 2022-07-26 17:11:16 +02:00
commit 867f38796a
4 changed files with 241 additions and 1018 deletions

View file

@ -1,14 +1,20 @@
#include <graphene/peerplays_sidechain/common/rpc_client.hpp>
#include <regex>
#include <sstream>
#include <boost/asio/buffers_iterator.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/xpressive/xpressive.hpp>
#include <boost/asio/buffers_iterator.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/beast/http.hpp>
#include <curl/curl.h>
#include <fc/log/logger.hpp>
namespace graphene { namespace peerplays_sidechain {
@ -18,7 +24,8 @@ rpc_client::rpc_client(std::string _url, std::string _user, std::string _passwor
user(_user),
password(_password),
debug_rpc_calls(_debug_rpc_calls),
request_id(0) {
request_id(0),
resolver(ioc) {
std::string reg_expr = "^((?P<Protocol>https|http):\\/\\/)?(?P<Host>[a-zA-Z0-9\\-\\.]+)(:(?P<Port>\\d{1,5}))?(?P<Target>\\/.+)?";
boost::xpressive::sregex sr = boost::xpressive::sregex::compile(reg_expr);
@ -45,6 +52,10 @@ rpc_client::rpc_client(std::string _url, std::string _user, std::string _passwor
if (target.empty()) {
target = "/";
}
authorization = "Basic " + fc::base64_encode(user + ":" + password);
results = resolver.resolve(host, port);
} else {
elog("Invalid URL: ${url}", ("url", url));
}
@ -118,42 +129,58 @@ std::string rpc_client::send_post_request(std::string method, std::string params
boost::property_tree::ptree json;
boost::property_tree::read_json(ss, json);
if (json.count("error") && !json.get_child("error").empty()) {
wlog("RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body.str())("msg", ss.str()));
}
if (reply.status == 200) {
return ss.str();
}
if (json.count("error") && !json.get_child("error").empty()) {
wlog("RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body.str())("msg", ss.str()));
}
return "";
}
rpc_reply rpc_client::send_post_request(std::string body, bool show_log) {
// The io_context is required for all I/O
boost::beast::net::io_context ioc;
// These object is used as a context for ssl connection
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12_client);
// These objects perform our I/O
boost::beast::net::ip::tcp::resolver resolver(ioc);
boost::beast::tcp_stream stream(ioc);
boost::beast::net::ssl::stream<boost::beast::tcp_stream> ssl_tcp_stream(ioc, ctx);
boost::beast::tcp_stream tcp_stream(ioc);
// Look up the domain name
auto const results = resolver.resolve(host, port);
// Set SNI Hostname (many hosts need this to handshake successfully)
if (protocol == "https") {
if (!SSL_set_tlsext_host_name(ssl_tcp_stream.native_handle(), host.c_str())) {
boost::beast::error_code ec{static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category()};
throw boost::beast::system_error{ec};
}
ctx.set_default_verify_paths();
ctx.set_verify_mode(boost::asio::ssl::verify_peer);
}
// Make the connection on the IP address we get from a lookup
stream.connect(results);
if (protocol == "https") {
boost::beast::get_lowest_layer(ssl_tcp_stream).connect(results);
ssl_tcp_stream.handshake(boost::beast::net::ssl::stream_base::client);
} else {
tcp_stream.connect(results);
}
// Set up an HTTP GET request message
boost::beast::http::request<boost::beast::http::string_body> req{boost::beast::http::verb::post, target, 11};
req.set(boost::beast::http::field::host, host + ":" + port);
req.set(boost::beast::http::field::accept, "application/json");
req.set(boost::beast::http::field::authorization, authorization);
req.set(boost::beast::http::field::content_type, "application/json");
req.set(boost::beast::http::field::content_encoding, "utf-8");
req.set(boost::beast::http::field::content_length, body.length());
req.body() = body;
// Send the HTTP request to the remote host
boost::beast::http::write(stream, req);
if (protocol == "https")
boost::beast::http::write(ssl_tcp_stream, req);
else
boost::beast::http::write(tcp_stream, req);
// This buffer is used for reading and must be persisted
boost::beast::flat_buffer buffer;
@ -162,19 +189,23 @@ rpc_reply rpc_client::send_post_request(std::string body, bool show_log) {
boost::beast::http::response<boost::beast::http::dynamic_body> res;
// Receive the HTTP response
boost::beast::http::read(stream, buffer, res);
//// Write the message to standard out
//std::cout << res << std::endl;
if (protocol == "https")
boost::beast::http::read(ssl_tcp_stream, buffer, res);
else
boost::beast::http::read(tcp_stream, buffer, res);
// Gracefully close the socket
boost::beast::error_code ec;
stream.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
if (protocol == "https") {
boost::beast::get_lowest_layer(ssl_tcp_stream).close();
} else {
tcp_stream.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
}
// not_connected happens sometimes
// not_connected happens sometimes. Also on ssl level some servers are managing
// connecntion close, so closing here will sometimes end up with error stream truncated
// so don't bother reporting it.
//
if (ec && ec != boost::beast::errc::not_connected)
if (ec && ec != boost::beast::errc::not_connected && ec != boost::asio::ssl::error::stream_truncated)
throw boost::beast::system_error{ec};
std::string rbody{boost::asio::buffers_begin(res.body().data()),

View file

@ -3,6 +3,9 @@
#include <cstdint>
#include <string>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/core.hpp>
namespace graphene { namespace peerplays_sidechain {
struct rpc_reply {
@ -24,6 +27,7 @@ protected:
std::string host;
std::string port;
std::string target;
std::string authorization;
std::string user;
std::string password;
@ -33,6 +37,10 @@ protected:
private:
rpc_reply send_post_request(std::string body, bool show_log);
boost::beast::net::io_context ioc;
boost::beast::net::ip::tcp::resolver resolver;
boost::asio::ip::basic_resolver_results<boost::asio::ip::tcp> results;
};
}} // namespace graphene::peerplays_sidechain

View file

@ -1,5 +1,6 @@
#pragma once
#include <graphene/peerplays_sidechain/common/rpc_client.hpp>
#include <graphene/peerplays_sidechain/sidechain_net_handler.hpp>
#include <string>
@ -22,7 +23,7 @@ public:
uint64_t amount_;
};
class bitcoin_rpc_client {
class bitcoin_rpc_client : public rpc_client {
public:
enum class multi_type {
script,
@ -41,49 +42,29 @@ public:
};
public:
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);
bitcoin_rpc_client(std::string _url, std::string _user, std::string _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);
std::string createmultisig(const uint32_t nrequired, const std::vector<std::string> public_keys);
std::string createpsbt(const std::vector<btc_txout> &ins, const fc::flat_map<std::string, double> outs);
std::string createrawtransaction(const std::vector<btc_txout> &ins, const fc::flat_map<std::string, double> outs);
std::string createwallet(const std::string &wallet_name);
std::string decodepsbt(std::string const &tx_psbt);
std::string decoderawtransaction(std::string const &tx_hex);
std::string encryptwallet(const std::string &passphrase);
uint64_t estimatesmartfee(uint16_t conf_target = 128);
std::string finalizepsbt(std::string const &tx_psbt);
std::string getaddressinfo(const std::string &address);
std::string getblock(const std::string &block_hash, int32_t verbosity = 2);
std::string getrawtransaction(const std::string &txid, const bool verbose = false);
std::string getnetworkinfo();
std::string gettransaction(const std::string &txid, const bool include_watch_only = false);
std::string getblockchaininfo();
void importaddress(const std::string &address_or_script, const std::string &label = "", const bool rescan = true, const bool p2sh = false);
void importmulti(const std::vector<multi_params> &address_or_script_array, const bool rescan = true);
std::vector<btc_txout> listunspent(const uint32_t minconf = 1, const uint32_t maxconf = 9999999);
std::vector<btc_txout> listunspent_by_address_and_amount(const std::string &address, double transfer_amount, const uint32_t minconf = 1, const uint32_t maxconf = 9999999);
std::string loadwallet(const std::string &filename);
std::string sendrawtransaction(const std::string &tx_hex);
std::string signrawtransactionwithwallet(const std::string &tx_hash);
std::string unloadwallet(const std::string &filename);
std::string walletlock();
std::string walletprocesspsbt(std::string const &tx_psbt);
bool walletpassphrase(const std::string &passphrase, uint32_t timeout = 60);
private:
fc::http::reply send_post_request(std::string body, bool show_log);
std::string ip;
uint32_t rpc_port;
std::string user;
std::string password;
std::string wallet_name;
std::string wallet_password;
bool debug_rpc_calls;
fc::http::header authorization;
};
// =============================================================================