From 67b48ccc933d0a1d016598536db3978db9e2dab3 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Thu, 11 May 2017 11:38:29 +0800 Subject: [PATCH] api to get raw tx and block data added --- main.cpp | 15 ++++ src/page.h | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 205 insertions(+), 9 deletions(-) diff --git a/main.cpp b/main.cpp index cf60a23..683da41 100644 --- a/main.cpp +++ b/main.cpp @@ -363,6 +363,14 @@ int main(int ac, const char* av[]) { return r; }); + CROW_ROUTE(app, "/api/rawtransaction/") + ([&](const crow::request &req, string tx_hash) { + + myxmr::jsonresponse r{xmrblocks.json_rawtransaction(tx_hash)}; + + return r; + }); + CROW_ROUTE(app, "/api/block/") ([&](const crow::request &req, string block_no_or_hash) { @@ -371,6 +379,13 @@ int main(int ac, const char* av[]) { return r; }); + CROW_ROUTE(app, "/api/rawblock/") + ([&](const crow::request &req, string block_no_or_hash) { + + myxmr::jsonresponse r{xmrblocks.json_rawblock(block_no_or_hash)}; + + return r; + }); CROW_ROUTE(app, "/api/transactions").methods("GET"_method) ([&](const crow::request &req) { diff --git a/src/page.h b/src/page.h index 2c026f8..aaea01b 100644 --- a/src/page.h +++ b/src/page.h @@ -3677,9 +3677,9 @@ namespace xmreg /* - * Lets use this json api convention for success and error - * https://labs.omniti.com/labs/jsend - */ + * Lets use this json api convention for success and error + * https://labs.omniti.com/labs/jsend + */ json json_transaction(string tx_hash_str) { @@ -3834,10 +3834,95 @@ namespace xmreg return j_response; } + + /* - * Lets use this json api convention for success and error - * https://labs.omniti.com/labs/jsend - */ + * Lets use this json api convention for success and error + * https://labs.omniti.com/labs/jsend + */ + json + json_rawtransaction(string tx_hash_str) + { + json j_response { + {"status", "fail"}, + {"data" , json {}} + }; + + json& j_data = j_response["data"]; + + // parse tx hash string to hash object + crypto::hash tx_hash; + + if (!xmreg::parse_str_secret_key(tx_hash_str, tx_hash)) + { + j_data["title"] = fmt::format("Cant parse tx hash: {:s}", tx_hash_str); + return j_response; + } + + // get transaction + transaction tx; + + // flag to indicate if tx is in mempool + bool found_in_mempool {false}; + + // for tx in blocks we get block timestamp + // for tx in mempool we get recievive time + uint64_t tx_timestamp {0}; + + if (!find_tx(tx_hash, tx, found_in_mempool, tx_timestamp)) + { + j_data["title"] = fmt::format("Cant find tx hash: {:s}", tx_hash_str); + return j_response; + } + + if (found_in_mempool == false) + { + + block blk; + + try + { + // get block cointaining this tx + uint64_t block_height = core_storage->get_db().get_tx_block_height(tx_hash); + + if (!mcore->get_block_by_height(block_height, blk)) + { + j_data["title"] = fmt::format("Cant get block: {:d}", block_height); + return j_response; + } + } + catch (const exception& e) + { + j_response["status"] = "error"; + j_response["message"] = fmt::format("Tx does not exist in blockchain, " + "but was there before: {:s}", + tx_hash_str); + return j_response; + } + } + + // get raw tx json as in monero + + try + { + j_data = json::parse(obj_to_json_str(tx)); + } + catch (std::invalid_argument& e) + { + j_response["status"] = "error"; + j_response["message"] = "Faild parsing raw tx data into json"; + return j_response; + } + + j_response["status"] = "success"; + + return j_response; + } + + /* + * Lets use this json api convention for success and error + * https://labs.omniti.com/labs/jsend + */ json json_block(string block_no_or_hash) { @@ -3978,9 +4063,105 @@ namespace xmreg /* - * Lets use this json api convention for success and error - * https://labs.omniti.com/labs/jsend - */ + * Lets use this json api convention for success and error + * https://labs.omniti.com/labs/jsend + */ + json + json_rawblock(string block_no_or_hash) + { + json j_response { + {"status", "fail"}, + {"data" , json {}} + }; + + json& j_data = j_response["data"]; + + uint64_t current_blockchain_height + = core_storage->get_current_blockchain_height(); + + uint64_t block_height {0}; + + crypto::hash blk_hash; + + block blk; + + if (block_no_or_hash.length() <= 8) + { + // we have something that seems to be a block number + try + { + block_height = boost::lexical_cast(block_no_or_hash); + } + catch (const boost::bad_lexical_cast& e) + { + j_data["title"] = fmt::format( + "Cant parse block number: {:s}", block_no_or_hash); + return j_response; + } + + if (block_height > current_blockchain_height) + { + j_data["title"] = fmt::format( + "Requested block is higher than blockchain:" + " {:d}, {:d}", block_height,current_blockchain_height); + return j_response; + } + + if (!mcore->get_block_by_height(block_height, blk)) + { + j_data["title"] = fmt::format("Cant get block: {:d}", block_height); + return j_response; + } + + blk_hash = core_storage->get_block_id_by_height(block_height); + + } + else if (block_no_or_hash.length() == 64) + { + // this seems to be block hash + if (!xmreg::parse_str_secret_key(block_no_or_hash, blk_hash)) + { + j_data["title"] = fmt::format("Cant parse blk hash: {:s}", block_no_or_hash); + return j_response; + } + + if (!core_storage->get_block_by_hash(blk_hash, blk)) + { + j_data["title"] = fmt::format("Cant get block: {:s}", blk_hash); + return j_response; + } + + block_height = core_storage->get_db().get_block_height(blk_hash); + } + else + { + j_data["title"] = fmt::format("Cant find blk using search string: {:s}", block_no_or_hash); + return j_response; + } + + // get raw tx json as in monero + + try + { + j_data = json::parse(obj_to_json_str(blk)); + } + catch (std::invalid_argument& e) + { + j_response["status"] = "error"; + j_response["message"] = "Faild parsing raw blk data into json"; + return j_response; + } + + j_response["status"] = "success"; + + return j_response; + } + + + /* + * Lets use this json api convention for success and error + * https://labs.omniti.com/labs/jsend + */ json json_transactions(string _page, string _limit) {