2016-04-12 22:20:14 +00:00
|
|
|
//
|
|
|
|
// Created by mwo on 13/04/16.
|
|
|
|
//
|
|
|
|
|
2016-04-15 21:48:37 +00:00
|
|
|
|
2016-04-12 22:20:14 +00:00
|
|
|
#ifndef CROWXMR_RPCCALLS_H
|
|
|
|
#define CROWXMR_RPCCALLS_H
|
|
|
|
|
|
|
|
#include "monero_headers.h"
|
|
|
|
|
2016-04-15 21:48:37 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
2016-04-12 22:20:14 +00:00
|
|
|
namespace xmreg
|
|
|
|
{
|
|
|
|
|
|
|
|
using namespace cryptonote;
|
|
|
|
using namespace crypto;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
class rpccalls
|
|
|
|
{
|
2016-04-15 21:48:37 +00:00
|
|
|
string deamon_url ;
|
|
|
|
uint64_t timeout_time;
|
2016-04-15 08:44:45 +00:00
|
|
|
|
2017-02-07 00:56:17 +00:00
|
|
|
std::chrono::milliseconds timeout_time_ms;
|
|
|
|
|
2016-04-15 08:44:45 +00:00
|
|
|
epee::net_utils::http::url_content url;
|
|
|
|
|
2016-04-15 21:48:37 +00:00
|
|
|
epee::net_utils::http::http_simple_client m_http_client;
|
|
|
|
std::mutex m_daemon_rpc_mutex;
|
2016-04-15 08:44:45 +00:00
|
|
|
|
|
|
|
string port;
|
2016-04-12 22:20:14 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2016-04-15 21:48:37 +00:00
|
|
|
rpccalls(string _deamon_url = "http:://127.0.0.1:18081",
|
|
|
|
uint64_t _timeout = 200000)
|
2017-02-07 00:56:17 +00:00
|
|
|
: deamon_url {_deamon_url},
|
|
|
|
timeout_time {_timeout}
|
2016-04-15 08:44:45 +00:00
|
|
|
{
|
|
|
|
epee::net_utils::parse_url(deamon_url, url);
|
|
|
|
|
|
|
|
port = std::to_string(url.port);
|
2017-02-07 00:56:17 +00:00
|
|
|
|
|
|
|
timeout_time_ms = std::chrono::milliseconds {timeout_time};
|
|
|
|
|
2017-02-12 01:48:56 +00:00
|
|
|
m_http_client.set_server(
|
|
|
|
deamon_url,
|
|
|
|
boost::optional<epee::net_utils::http::login>{});
|
2016-04-15 08:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-04-15 21:48:37 +00:00
|
|
|
connect_to_monero_deamon()
|
2016-04-15 08:44:45 +00:00
|
|
|
{
|
2017-02-07 00:56:17 +00:00
|
|
|
//std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
|
2016-04-15 21:48:37 +00:00
|
|
|
|
|
|
|
if(m_http_client.is_connected())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-15 08:44:45 +00:00
|
|
|
|
2017-02-07 00:56:17 +00:00
|
|
|
return m_http_client.connect(timeout_time_ms);
|
2016-04-15 08:44:45 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 22:20:14 +00:00
|
|
|
uint64_t
|
|
|
|
get_current_height()
|
|
|
|
{
|
|
|
|
COMMAND_RPC_GET_HEIGHT::request req;
|
|
|
|
COMMAND_RPC_GET_HEIGHT::response res;
|
|
|
|
|
2016-04-15 21:48:37 +00:00
|
|
|
std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
|
2016-04-12 22:20:14 +00:00
|
|
|
|
2017-02-07 00:56:17 +00:00
|
|
|
if (!connect_to_monero_deamon())
|
|
|
|
{
|
|
|
|
cerr << "get_current_height: not connected to deamon" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool r = epee::net_utils::invoke_http_json(
|
|
|
|
"/getheight",
|
|
|
|
req, res, m_http_client, timeout_time_ms);
|
2016-04-12 22:20:14 +00:00
|
|
|
|
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
cerr << "Error connecting to Monero deamon at "
|
|
|
|
<< deamon_url << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "rpc call /getheight OK: " << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.height;
|
|
|
|
}
|
|
|
|
|
2016-04-15 07:03:48 +00:00
|
|
|
bool
|
2017-02-07 00:56:17 +00:00
|
|
|
get_mempool(vector<tx_info>& mempool_txs)
|
|
|
|
{
|
2016-04-12 22:20:14 +00:00
|
|
|
|
2016-04-15 07:03:48 +00:00
|
|
|
COMMAND_RPC_GET_TRANSACTION_POOL::request req;
|
|
|
|
COMMAND_RPC_GET_TRANSACTION_POOL::response res;
|
2016-04-12 22:20:14 +00:00
|
|
|
|
2016-04-15 21:48:37 +00:00
|
|
|
std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
|
2016-04-12 22:20:14 +00:00
|
|
|
|
2017-02-07 00:56:17 +00:00
|
|
|
if (!connect_to_monero_deamon())
|
|
|
|
{
|
|
|
|
cerr << "get_mempool: not connected to deamon" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool r = epee::net_utils::invoke_http_json(
|
|
|
|
"/get_transaction_pool",
|
|
|
|
req, res, m_http_client, timeout_time_ms);
|
2016-04-12 22:20:14 +00:00
|
|
|
|
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
cerr << "Error connecting to Monero deamon at "
|
|
|
|
<< deamon_url << endl;
|
2016-04-15 07:03:48 +00:00
|
|
|
return false;
|
2016-04-12 22:20:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-02 02:22:49 +00:00
|
|
|
|
2016-04-15 07:03:48 +00:00
|
|
|
mempool_txs = res.transactions;
|
2016-04-12 22:20:14 +00:00
|
|
|
|
2016-04-15 07:03:48 +00:00
|
|
|
return true;
|
2016-04-12 22:20:14 +00:00
|
|
|
}
|
2016-10-11 05:38:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
commit_tx(tools::wallet2::pending_tx& ptx, string& error_msg)
|
|
|
|
{
|
|
|
|
COMMAND_RPC_SEND_RAW_TX::request req;
|
|
|
|
COMMAND_RPC_SEND_RAW_TX::response res;
|
|
|
|
|
|
|
|
req.tx_as_hex = epee::string_tools::buff_to_hex_nodelimer(
|
|
|
|
tx_to_blob(ptx.tx)
|
|
|
|
);
|
|
|
|
|
|
|
|
req.do_not_relay = false;
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
|
|
|
|
|
2017-02-07 00:56:17 +00:00
|
|
|
if (!connect_to_monero_deamon())
|
|
|
|
{
|
|
|
|
cerr << "commit_tx: not connected to deamon" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool r = epee::net_utils::invoke_http_json(
|
|
|
|
"/sendrawtransaction",
|
|
|
|
req, res, m_http_client, timeout_time_ms);
|
2016-10-11 05:38:27 +00:00
|
|
|
|
|
|
|
if (!r || res.status == "Failed")
|
|
|
|
{
|
|
|
|
error_msg = res.reason;
|
|
|
|
|
|
|
|
cerr << "Error sending tx: " << res.reason << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-12 22:20:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif //CROWXMR_RPCCALLS_H
|