/altblocks added

This commit is contained in:
moneroexamples 2017-06-22 10:00:38 +08:00
parent 160a1abe48
commit e4a4e2c9ee
5 changed files with 152 additions and 23 deletions

View File

@ -431,6 +431,11 @@ main(int ac, const char* av[])
return xmrblocks.mempool(true);
});
CROW_ROUTE(app, "/altblocks")
([&](const crow::request& req) {
return xmrblocks.altblocks();
});
CROW_ROUTE(app, "/robots.txt")
([&]() {
string text = "User-agent: *\n"

View File

@ -37,6 +37,7 @@
#define TMPL_INDEX TMPL_DIR "/index.html"
#define TMPL_INDEX2 TMPL_DIR "/index2.html"
#define TMPL_MEMPOOL TMPL_DIR "/mempool.html"
#define TMPL_ALTBLOCKS TMPL_DIR "/altblocks.html"
#define TMPL_MEMPOOL_ERROR TMPL_DIR "/mempool_error.html"
#define TMPL_HEADER TMPL_DIR "/header.html"
#define TMPL_FOOTER TMPL_DIR "/footer.html"
@ -342,6 +343,7 @@ namespace xmreg
template_file["footer"] = get_footer();
template_file["index2"] = get_full_page(xmreg::read(TMPL_INDEX2));
template_file["mempool"] = xmreg::read(TMPL_MEMPOOL);
template_file["altblocks"] = get_full_page(xmreg::read(TMPL_ALTBLOCKS));
template_file["mempool_error"] = xmreg::read(TMPL_MEMPOOL_ERROR);
template_file["mempool_full"] = get_full_page(template_file["mempool"]);
template_file["block"] = get_full_page(xmreg::read(TMPL_BLOCK));
@ -425,17 +427,6 @@ namespace xmreg
{"show_cache_times" , show_cache_times}
};
vector<string> atl_blks_hashes;
rpc.get_alt_blocks(atl_blks_hashes);
cout << "atl_blks_hashes.size(): " << atl_blks_hashes.size() << endl;
for (const string& alt_blk_hash: atl_blks_hashes)
{
cout << "alt_blk_hash: " << alt_blk_hash << endl;
}
context.emplace("txs", mstch::array()); // will keep tx to show
// get reference to txs mstch map to be field below
@ -918,6 +909,51 @@ namespace xmreg
}
string
altblocks()
{
// initalise page tempate map with basic info about blockchain
mstch::map context {
{"testnet" , testnet},
{"blocks" , mstch::array()}
};
// get reference to alt blocks template map to be field below
mstch::array& blocks = boost::get<mstch::array>(context["blocks"]);
vector<string> atl_blks_hashes;
rpc.get_alt_blocks(atl_blks_hashes);
cout << "atl_blks_hashes.size(): " << atl_blks_hashes.size() << endl;
context.emplace("no_alt_blocks", atl_blks_hashes.size());
for (const string& alt_blk_hash: atl_blks_hashes)
{
cout << "alt_blk_hash: " << alt_blk_hash << endl;
block alt_blk;
string error_msg;
blocks.push_back(mstch::map {
{"hash", alt_blk_hash}
});
if (rpc.get_block(alt_blk_hash, alt_blk, error_msg))
{
cout << " - alt block txs no: " << alt_blk.tx_hashes.size() << endl;
}
}
add_css_style(context);
// render the page
return mstch::render(template_file["altblocks"], context);
}
string
show_block(uint64_t _blk_height)
{

View File

@ -61,10 +61,6 @@ rpccalls::get_current_height()
<< deamon_url << endl;
return 0;
}
else
{
cout << "rpc call /getheight OK: " << endl;
}
return res.height;
}
@ -153,8 +149,10 @@ bool
rpccalls::get_network_info(COMMAND_RPC_GET_INFO::response& response)
{
epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_INFO::request> req_t = AUTO_VAL_INIT(req_t);
epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_INFO::response, std::string> resp_t = AUTO_VAL_INIT(resp_t);
epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_INFO::request>
req_t = AUTO_VAL_INIT(req_t);
epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_INFO::response, std::string>
resp_t = AUTO_VAL_INIT(resp_t);
bool r {false};
@ -167,7 +165,7 @@ rpccalls::get_network_info(COMMAND_RPC_GET_INFO::response& response)
if (!connect_to_monero_deamon())
{
cerr << "get_mempool: not connected to deamon" << endl;
cerr << "get_network_info: not connected to deamon" << endl;
return false;
}
@ -228,12 +226,12 @@ rpccalls::get_dynamic_per_kb_fee_estimate(
bool r {false};
std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
{
std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
if (!connect_to_monero_deamon())
{
cerr << "get_current_height: not connected to deamon" << endl;
cerr << "get_dynamic_per_kb_fee_estimate: not connected to deamon" << endl;
return false;
}
@ -273,6 +271,74 @@ rpccalls::get_dynamic_per_kb_fee_estimate(
fee = resp_t.result.fee;
return true;
}
bool
rpccalls::get_block(string const& blk_hash, block& blk, string& error_msg)
{
epee::json_rpc::request<COMMAND_RPC_GET_BLOCK::request> req_t;
epee::json_rpc::response<COMMAND_RPC_GET_BLOCK::response, std::string> resp_t;
req_t.jsonrpc = "2.0";
req_t.id = epee::serialization::storage_entry(0);
req_t.method = "getblock";
req_t.params.hash = blk_hash;
bool r {false};
{
std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
if (!connect_to_monero_deamon())
{
cerr << "get_block: not connected to deamon" << endl;
return false;
}
r = epee::net_utils::invoke_http_json("/json_rpc",
req_t, resp_t,
m_http_client);
}
string err;
if (r)
{
if (resp_t.result.status == CORE_RPC_STATUS_BUSY)
{
err = "daemon is busy. Please try again later.";
}
else if (resp_t.result.status != CORE_RPC_STATUS_OK)
{
err = resp_t.result.status;
}
if (!err.empty())
{
cerr << "Error connecting to Monero deamon due to "
<< err << endl;
return false;
}
}
else
{
cerr << "get_block: error connecting to Monero deamon at "
<< deamon_url << endl;
return false;
}
std::string block_bin_blob;
if(!epee::string_tools::parse_hexstr_to_binbuff(resp_t.result.blob, block_bin_blob))
return false;
return parse_and_validate_block_from_blob(block_bin_blob, blk);
}
}
}

View File

@ -128,7 +128,7 @@ public:
if (!connect_to_monero_deamon())
{
cerr << "get_mempool: not connected to deamon" << endl;
cerr << "get_alt_blocks: not connected to deamon" << endl;
return false;
}
@ -179,6 +179,9 @@ public:
return false;
}
bool
get_block(string const& blk_hash, block& blk, string& error_msg);
};

View File

@ -0,0 +1,19 @@
<h2 style="margin-bottom: 0px">
Alternative blocks
</h2>
<h4 style="font-size: 14px; margin-top: 0px">(no of alt blocks: {{no_alt_blocks}})</h4>
<div class="center">
<table class="center" style="width:80%">
<tr>
<td>hash</td>
</tr>
{{#blocks}}
<tr>
<td>{{hash}}</td>
</tr>
{{/blocks}}
</table>
</div>