add limits to json_mempool

master
moneroexamples 8 years ago
parent fa168613ae
commit eac4fa8460

@ -386,10 +386,16 @@ int main(int ac, const char* av[]) {
return r; return r;
}); });
CROW_ROUTE(app, "/api/mempool") CROW_ROUTE(app, "/api/mempool").methods("GET"_method)
([&](const crow::request &req) { ([&](const crow::request &req) {
myxmr::jsonresponse r{xmrblocks.json_mempool()}; string page = regex_search(req.raw_url, regex {"page=\\d+"}) ?
req.url_params.get("page") : "0";
string limit = regex_search(req.raw_url, regex {"limit=\\d+"}) ?
req.url_params.get("limit") : "25";
myxmr::jsonresponse r{xmrblocks.json_mempool(page, limit)};
return r; return r;
}); });

@ -4092,7 +4092,7 @@ namespace xmreg
j_data["page"] = page; j_data["page"] = page;
j_data["limit"] = limit; j_data["limit"] = limit;
j_data["current_height"] = height; j_data["current_height"] = height;
j_data["total_page_no"] = (height / limit); j_data["total_page_no"] = limit > 0 ? (height / limit) : 0;
j_response["status"] = "success"; j_response["status"] = "success";
@ -4106,7 +4106,7 @@ namespace xmreg
* https://labs.omniti.com/labs/jsend * https://labs.omniti.com/labs/jsend
*/ */
json json
json_mempool() json_mempool(string _page, string _limit)
{ {
json j_response { json j_response {
{"status", "fail"}, {"status", "fail"},
@ -4115,6 +4115,23 @@ namespace xmreg
json& j_data = j_response["data"]; json& j_data = j_response["data"];
// parse page and limit into numbers
uint64_t page {0};
uint64_t limit {0};
try
{
page = boost::lexical_cast<uint64_t>(_page);
limit = boost::lexical_cast<uint64_t>(_limit);
}
catch (const boost::bad_lexical_cast& e)
{
j_data["title"] = fmt::format(
"Cant parse page and/or limit numbers: {:s}, {:s}", _page, _limit);
return j_response;
}
//get current server timestamp //get current server timestamp
server_timestamp = std::time(nullptr); server_timestamp = std::time(nullptr);
@ -4136,21 +4153,57 @@ namespace xmreg
(void) tx_hash_dummy; (void) tx_hash_dummy;
// for each transaction in the memory pool uint64_t no_mempool_txs = mempool_data.size();
for (const auto& a_pair: mempool_data)
// calculate starting and ending block numbers to show
int64_t start_height = no_mempool_txs - limit * (page + 1);
// check if start height is not below range
start_height = start_height < 0 ? 0 : start_height;
int64_t end_height = start_height + limit;
end_height = end_height > no_mempool_txs ? no_mempool_txs : end_height;
// loop index
int64_t i = end_height;
json j_txs = json::array();
// for each transaction in the memory pool in current page
while (i > start_height)
{ {
const tx_details& txd = get_tx_details(a_pair.second, false, 1, height); // 1 is dummy here const pair<tx_info, transaction>* a_pair {nullptr};
try
{
a_pair = &(mempool_data.at(i - 1));
}
catch (const std::out_of_range& e)
{
break;
}
const tx_details& txd = get_tx_details(a_pair->second, false, 1, height); // 1 is dummy here
// get basic tx info // get basic tx info
json j_tx = get_tx_json(a_pair.second, txd); json j_tx = get_tx_json(a_pair->second, txd);
// we add some extra data, for mempool txs, such as recieve timestamp // we add some extra data, for mempool txs, such as recieve timestamp
j_tx["timestamp"] = a_pair.first.receive_time; j_tx["timestamp"] = a_pair->first.receive_time;
j_tx["timestamp_utc"] = xmreg::timestamp_to_str_gm(a_pair.first.receive_time); j_tx["timestamp_utc"] = xmreg::timestamp_to_str_gm(a_pair->first.receive_time);
j_data.push_back(j_tx); j_txs.push_back(j_tx);
--i;
} }
j_data["txs"] = j_txs;
j_data["page"] = page;
j_data["limit"] = limit;
j_data["txs_no"] = no_mempool_txs;
j_data["total_page_no"] = limit > 0 ? (no_mempool_txs / limit) : 0;
j_response["status"] = "success"; j_response["status"] = "success";
return j_response; return j_response;

Loading…
Cancel
Save