Boost Beast based RPC client
This commit is contained in:
parent
662139ca22
commit
5bfd685684
4 changed files with 346 additions and 2041 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -3,134 +3,43 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio/ip/tcp.hpp>
|
||||||
#include <boost/asio/ssl.hpp>
|
#include <boost/beast/core.hpp>
|
||||||
|
|
||||||
//#include <fc/network/http/connection.hpp>
|
|
||||||
|
|
||||||
namespace graphene { namespace peerplays_sidechain {
|
namespace graphene { namespace peerplays_sidechain {
|
||||||
|
|
||||||
enum class url_schema_type { unknown,
|
struct rpc_reply {
|
||||||
http,
|
uint16_t status;
|
||||||
https,
|
|
||||||
};
|
|
||||||
|
|
||||||
// utl
|
|
||||||
|
|
||||||
url_schema_type identify_url_schema_type(const std::string &schema_name);
|
|
||||||
|
|
||||||
struct url_data {
|
|
||||||
|
|
||||||
url_schema_type schema_type;
|
|
||||||
std::string schema;
|
|
||||||
std::string host;
|
|
||||||
uint16_t port;
|
|
||||||
std::string path;
|
|
||||||
|
|
||||||
url_data() :
|
|
||||||
schema_type(url_schema_type::unknown),
|
|
||||||
port(0) {
|
|
||||||
}
|
|
||||||
|
|
||||||
url_data(const std::string &url);
|
|
||||||
|
|
||||||
void clear();
|
|
||||||
|
|
||||||
bool parse(const std::string &url);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct http_request {
|
|
||||||
|
|
||||||
std::string body;
|
std::string body;
|
||||||
std::string content_type;
|
|
||||||
|
|
||||||
http_request(const std::string &body_, const std::string &content_type_) :
|
|
||||||
body(body_),
|
|
||||||
content_type(content_type_) {
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct http_response {
|
|
||||||
|
|
||||||
uint16_t status_code;
|
|
||||||
std::string body;
|
|
||||||
|
|
||||||
void clear() {
|
|
||||||
status_code = 0;
|
|
||||||
body = decltype(body)();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
template <class>
|
|
||||||
class http_call_impl;
|
|
||||||
class tcp_socket;
|
|
||||||
class ssl_socket;
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
class http_call {
|
|
||||||
public:
|
|
||||||
http_call(const url_data &url, const std::string &method = std::string(), const std::string &headers = std::string());
|
|
||||||
~http_call();
|
|
||||||
|
|
||||||
bool is_ssl() const;
|
|
||||||
|
|
||||||
const std::string &path() const;
|
|
||||||
void set_path(const std::string &path);
|
|
||||||
void set_method(const std::string &method);
|
|
||||||
void set_headers(const std::string &headers);
|
|
||||||
const std::string &host() const;
|
|
||||||
void set_host(const std::string &host);
|
|
||||||
|
|
||||||
uint16_t port() const;
|
|
||||||
void set_port(uint16_t port);
|
|
||||||
|
|
||||||
bool exec(const http_request &request, http_response *response);
|
|
||||||
|
|
||||||
const std::string &error_what() const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
template <class>
|
|
||||||
friend class detail::http_call_impl;
|
|
||||||
friend detail::tcp_socket;
|
|
||||||
friend detail::ssl_socket;
|
|
||||||
static constexpr auto response_size_limit_bytes = 16 * 1024 * 1024;
|
|
||||||
static constexpr auto response_first_alloc_bytes = 32 * 1024;
|
|
||||||
static constexpr auto response_next_alloc_bytes = 256 * 1024;
|
|
||||||
std::string m_host;
|
|
||||||
uint16_t m_port_default;
|
|
||||||
uint16_t m_port;
|
|
||||||
std::string m_path;
|
|
||||||
std::string m_method;
|
|
||||||
std::string m_headers;
|
|
||||||
std::string m_error_what;
|
|
||||||
|
|
||||||
boost::asio::io_service m_service;
|
|
||||||
boost::asio::ssl::context *m_context;
|
|
||||||
boost::asio::ip::tcp::endpoint m_endpoint;
|
|
||||||
|
|
||||||
void ctor_priv();
|
|
||||||
};
|
|
||||||
|
|
||||||
}} // namespace graphene::peerplays_sidechain
|
|
||||||
|
|
||||||
namespace graphene { namespace peerplays_sidechain {
|
|
||||||
|
|
||||||
class rpc_client {
|
class rpc_client {
|
||||||
public:
|
public:
|
||||||
rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug);
|
rpc_client(std::string _url, std::string _user, std::string _password, bool _debug_rpc_calls);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string retrieve_array_value_from_reply(std::string reply_str, std::string array_path, uint32_t idx);
|
std::string retrieve_array_value_from_reply(std::string reply_str, std::string array_path, uint32_t idx);
|
||||||
std::string retrieve_value_from_reply(std::string reply_str, std::string value_path);
|
std::string retrieve_value_from_reply(std::string reply_str, std::string value_path);
|
||||||
std::string send_post_request(std::string method, std::string params, bool show_log);
|
std::string send_post_request(std::string method, std::string params, bool show_log);
|
||||||
|
|
||||||
|
std::string url;
|
||||||
|
std::string protocol;
|
||||||
|
std::string host;
|
||||||
|
std::string port;
|
||||||
|
std::string target;
|
||||||
|
std::string authorization;
|
||||||
|
|
||||||
|
std::string user;
|
||||||
|
std::string password;
|
||||||
bool debug_rpc_calls;
|
bool debug_rpc_calls;
|
||||||
uint32_t request_id;
|
uint32_t request_id;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
http_call client;
|
rpc_reply send_post_request(std::string body, bool show_log);
|
||||||
http_response send_post_request(const 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
|
}} // namespace graphene::peerplays_sidechain
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <graphene/peerplays_sidechain/sidechain_net_handler.hpp>
|
#include <graphene/peerplays_sidechain/sidechain_net_handler.hpp>
|
||||||
|
#include <graphene/peerplays_sidechain/common/rpc_client.hpp>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
@ -22,7 +23,7 @@ public:
|
||||||
uint64_t amount_;
|
uint64_t amount_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class bitcoin_rpc_client {
|
class bitcoin_rpc_client: public rpc_client {
|
||||||
public:
|
public:
|
||||||
enum class multi_type {
|
enum class multi_type {
|
||||||
script,
|
script,
|
||||||
|
|
@ -41,49 +42,30 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
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 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);
|
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 getblock(const std::string &block_hash, int32_t verbosity = 2);
|
||||||
std::string getrawtransaction(const std::string &txid, const bool verbose = false);
|
std::string getrawtransaction(const std::string &txid, const bool verbose = false);
|
||||||
std::string getnetworkinfo();
|
std::string getnetworkinfo();
|
||||||
std::string gettransaction(const std::string &txid, const bool include_watch_only = false);
|
|
||||||
std::string getblockchaininfo();
|
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);
|
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(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::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 loadwallet(const std::string &filename);
|
||||||
std::string sendrawtransaction(const std::string &tx_hex);
|
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 walletlock();
|
||||||
std::string walletprocesspsbt(std::string const &tx_psbt);
|
|
||||||
bool walletpassphrase(const std::string &passphrase, uint32_t timeout = 60);
|
bool walletpassphrase(const std::string &passphrase, uint32_t timeout = 60);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
fc::http::reply send_post_request(std::string body, bool show_log);
|
|
||||||
|
|
||||||
std::string ip;
|
std::string ip;
|
||||||
uint32_t rpc_port;
|
uint32_t rpc_port;
|
||||||
std::string user;
|
std::string user;
|
||||||
std::string password;
|
std::string password;
|
||||||
std::string wallet;
|
std::string wallet;
|
||||||
std::string wallet_password;
|
std::string wallet_password;
|
||||||
bool debug_rpc_calls;
|
|
||||||
|
|
||||||
fc::http::header authorization;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue