rpccalls.h updated to match current monero rpc changes

This commit is contained in:
moneroexamples 2017-02-07 00:56:17 +00:00
parent 925f233c58
commit a0f85e68eb
2 changed files with 39 additions and 212 deletions

View file

@ -278,202 +278,6 @@ public:
no_of_mempool_tx_of_frontpage = 25; no_of_mempool_tx_of_frontpage = 25;
} }
/**
* @brief Show recent blocks and mempool
*
* Not used currently. index2 method is used instead
*
* @param page_no block page to show
* @param refresh_page enable autorefresh
* @return rendered index page
*/
string
index(uint64_t page_no = 0, bool refresh_page = false)
{
// connect to the deamon if not yet connected
bool is_connected = rpc.connect_to_monero_deamon();
if (!is_connected)
{
cerr << "Connection to the Monero demon does not exist or was lost!" << endl;
return "Connection to the Monero demon does not exist or was lost!";
}
//get current server timestamp
server_timestamp = std::time(nullptr);
// number of last blocks to show
uint64_t no_of_last_blocks {100 + 1};
uint64_t height = rpc.get_current_height() - 1;
// initalise page tempate map with basic info about blockchain
mstch::map context {
{"refresh" , refresh_page},
{"height" , std::to_string(height)},
{"server_timestamp", xmreg::timestamp_to_str(server_timestamp)},
{"age_format" , string("[h:m:d]")},
{"page_no" , std::to_string(page_no)},
{"total_page_no" , std::to_string(height / (no_of_last_blocks))},
{"is_page_zero" , !bool(page_no)},
{"next_page" , std::to_string(page_no + 1)},
{"prev_page" , std::to_string((page_no > 0 ? page_no - 1 : 0))}
};
context.emplace("blocks", mstch::array());
// get reference to blocks template map to be field below
mstch::array& blocks = boost::get<mstch::array>(context["blocks"]);
// calculate starting and ending block numbers to show
uint64_t start_height = height - no_of_last_blocks * (page_no + 1);
uint64_t end_height = height - no_of_last_blocks * (page_no);
// check few conditions to make sure we are whithin the avaliable range
//@TODO its too messed up. needs to find cleaner way.
start_height = start_height > 0 ? start_height : 0;
end_height = end_height < height ? end_height : height;
start_height = start_height > end_height ? 0 : start_height;
end_height = end_height - start_height > no_of_last_blocks
? no_of_last_blocks : end_height;
// previous blk timestamp, initalised to lowest possible value
double prev_blk_timestamp {std::numeric_limits<double>::lowest()};
// iterate over last no_of_last_blocks of blocks
for (uint64_t i = start_height; i <= end_height; ++i)
{
// get block at the given height i
block blk;
if (!mcore->get_block_by_height(i, blk))
{
cerr << "Cant get block: " << i << endl;
continue;
}
// get block's hash
crypto::hash blk_hash = core_storage->get_block_id_by_height(i);
// remove "<" and ">" from the hash string
string blk_hash_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", blk_hash));
// get block timestamp in user friendly format
string timestamp_str = xmreg::timestamp_to_str(blk.timestamp);
pair<string, string> age = get_age(server_timestamp, blk.timestamp);
context["age_format"] = age.second;
// get time difference [m] between previous and current blocks
string time_delta_str {};
if (prev_blk_timestamp > std::numeric_limits<double>::lowest())
{
time_delta_str = fmt::format("{:0.2f}",
(double(blk.timestamp) - double(prev_blk_timestamp))/60.0);
}
// get xmr in the block reward
array<uint64_t, 2> coinbase_tx = sum_money_in_tx(blk.miner_tx);
// get transactions in the block
const vector<cryptonote::transaction>& txs_in_blk =
core_storage->get_db().get_tx_list(blk.tx_hashes);
// sum xmr in the inputs and ouputs of all transactions
array<uint64_t, 2> sum_xmr_in_out = sum_money_in_txs(txs_in_blk);
// get sum of all transactions in the block
uint64_t sum_fees = sum_fees_in_txs(txs_in_blk);
// get mixin number in each transaction
vector<uint64_t> mixin_numbers = xmreg::get_mixin_no_in_txs(txs_in_blk);
// find minimum and maxium mixin numbers
int mixin_min {-1};
int mixin_max {-1};
if (!mixin_numbers.empty())
{
mixin_min = static_cast<int>(
*std::min_element(mixin_numbers.begin(), mixin_numbers.end()));
mixin_max = static_cast<int>(
*max_element(mixin_numbers.begin(), mixin_numbers.end()));
}
// mixing format for the templates
auto mixin_format = [=]() -> mstch::node
{
if (mixin_min < 0)
{
return string("N/A");
}
return fmt::format("{:d}-{:d}", mixin_min - 1, mixin_max - 1);
};
// get block size in bytes
uint64_t blk_size = get_object_blobsize(blk);
// set output page template map
blocks.push_back(mstch::map {
{"height" , to_string(i)},
{"timestamp" , timestamp_str},
{"time_delta" , time_delta_str},
{"age" , age.first},
{"hash" , blk_hash_str},
{"block_reward", fmt::format("{:0.4f}/{:0.4f}",
XMR_AMOUNT(coinbase_tx[1] - sum_fees),
XMR_AMOUNT(sum_fees))},
{"fees" , fmt::format("{:0.3f}", XMR_AMOUNT(sum_fees))},
{"notx" , fmt::format("{:d}", blk.tx_hashes.size())},
{"xmr_inputs" , fmt::format("{:0.2f}",
XMR_AMOUNT(sum_xmr_in_out[0]))},
{"xmr_outputs" , fmt::format("{:0.2f}",
XMR_AMOUNT(sum_xmr_in_out[1]))},
{"mixin_range" , mstch::lambda {mixin_format}},
{"blksize" , fmt::format("{:0.2f}",
static_cast<double>(blk_size) / 1024.0)}
});
// save current's block timestamp as reference for the next one
prev_blk_timestamp = static_cast<double>(blk.timestamp);
} // for (uint64_t i = start_height; i <= end_height; ++i)
// reverse blocks and remove last (i.e., oldest)
// block. This is done so that time delats
// are easier to calcualte in the above for loop
std::reverse(blocks.begin(), blocks.end());
// if we look at the genesis time, we should not remove
// the last block, i.e. genesis one.
if (!(start_height < 2))
{
blocks.pop_back();
}
// get memory pool rendered template
string mempool_html = mempool();
// append mempool_html to the index context map
context["mempool_info"] = mempool_html;
// read index.html
string index_html = xmreg::read(TMPL_INDEX);
// add header and footer
string full_page = get_full_page(index_html);
context["css_styles"] = this->css_styles;
// render the page
return mstch::render(full_page, context);
}
/** /**
* @brief show recent transactions and mempool * @brief show recent transactions and mempool
* @param page_no block page to show * @param page_no block page to show

View file

@ -23,6 +23,8 @@ namespace xmreg
string deamon_url ; string deamon_url ;
uint64_t timeout_time; uint64_t timeout_time;
std::chrono::milliseconds timeout_time_ms;
epee::net_utils::http::url_content url; epee::net_utils::http::url_content url;
epee::net_utils::http::http_simple_client m_http_client; epee::net_utils::http::http_simple_client m_http_client;
@ -34,26 +36,29 @@ namespace xmreg
rpccalls(string _deamon_url = "http:://127.0.0.1:18081", rpccalls(string _deamon_url = "http:://127.0.0.1:18081",
uint64_t _timeout = 200000) uint64_t _timeout = 200000)
: deamon_url {_deamon_url}, timeout_time {_timeout} : deamon_url {_deamon_url},
timeout_time {_timeout}
{ {
epee::net_utils::parse_url(deamon_url, url); epee::net_utils::parse_url(deamon_url, url);
port = std::to_string(url.port); port = std::to_string(url.port);
timeout_time_ms = std::chrono::milliseconds {timeout_time};
m_http_client.set_server(deamon_url);
} }
bool bool
connect_to_monero_deamon() connect_to_monero_deamon()
{ {
std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex); //std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
if(m_http_client.is_connected()) if(m_http_client.is_connected())
{ {
return true; return true;
} }
return m_http_client.connect(url.host, return m_http_client.connect(timeout_time_ms);
port,
timeout_time);
} }
uint64_t uint64_t
@ -64,9 +69,15 @@ namespace xmreg
std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex); std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
bool r = epee::net_utils::invoke_http_json_remote_command2( if (!connect_to_monero_deamon())
deamon_url + "/getheight", {
req, res, m_http_client, timeout_time); 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);
if (!r) if (!r)
{ {
@ -83,16 +94,23 @@ namespace xmreg
} }
bool bool
get_mempool(vector<tx_info>& mempool_txs) { get_mempool(vector<tx_info>& mempool_txs)
{
COMMAND_RPC_GET_TRANSACTION_POOL::request req; COMMAND_RPC_GET_TRANSACTION_POOL::request req;
COMMAND_RPC_GET_TRANSACTION_POOL::response res; COMMAND_RPC_GET_TRANSACTION_POOL::response res;
std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex); std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
bool r = epee::net_utils::invoke_http_json_remote_command2( if (!connect_to_monero_deamon())
deamon_url + "/get_transaction_pool", {
req, res, m_http_client, timeout_time); 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);
if (!r) if (!r)
{ {
@ -122,10 +140,15 @@ namespace xmreg
std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex); std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
bool r = epee::net_utils::invoke_http_json_remote_command2(deamon_url if (!connect_to_monero_deamon())
+ "/sendrawtransaction", {
req, res, cerr << "commit_tx: not connected to deamon" << endl;
m_http_client, 200000);; return false;
}
bool r = epee::net_utils::invoke_http_json(
"/sendrawtransaction",
req, res, m_http_client, timeout_time_ms);
if (!r || res.status == "Failed") if (!r || res.status == "Failed")
{ {