diff --git a/README.md b/README.md index 5ef67c7..4b077cb 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# crow-monero-test +# Onion Monero Blockchain Explorer diff --git a/main.cpp b/main.cpp index a51ae9b..7699409 100644 --- a/main.cpp +++ b/main.cpp @@ -72,6 +72,16 @@ int main(int ac, const char* av[]) { return xmrblocks.index(page_no); }); + CROW_ROUTE(app, "/block/") + ([&](size_t block_height) { + return xmrblocks.show_block(block_height); + }); + + CROW_ROUTE(app, "/block/") + ([&](string block_hash) { + return xmrblocks.show_block(block_hash); + }); + CROW_ROUTE(app, "/autorefresh") ([&]() { uint64_t page_no {0}; diff --git a/src/page.h b/src/page.h index 9a3f90b..b6d2ada 100644 --- a/src/page.h +++ b/src/page.h @@ -29,6 +29,7 @@ #define TMPL_MEMPOOL TMPL_DIR "/mempool.html" #define TMPL_HEADER TMPL_DIR "/header.html" #define TMPL_FOOTER TMPL_DIR "/footer.html" +#define TMPL_BLOCK TMPL_DIR "/block.html" @@ -92,7 +93,7 @@ namespace xmreg { {"total_page_no" , fmt::format("{:d}", height / (no_of_last_blocks))}, {"is_page_zero" , !bool(page_no)}, {"next_page" , fmt::format("{:d}", page_no + 1)}, - {"prev_page" , fmt::format("{:d}", (page_no > 0 ? page_no - 1 : 0))}, + {"prev_page" , fmt::format("{:d}", (page_no > 0 ? page_no - 1 : 0))} }; @@ -216,7 +217,7 @@ namespace xmreg { {"blksize" , fmt::format("{:0.2f}", static_cast(blk_size) / 1024.0)} }); - } + } // 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 @@ -316,6 +317,68 @@ namespace xmreg { } + string + show_block(uint64_t _blk_height) + { + // get block at the given height i + block blk; + + if (!mcore->get_block_by_height(_blk_height, blk)) + { + cerr << "Cant get block: " << _blk_height << endl; + return fmt::format("Block of height {:d} not found!", _blk_height); + } + + // initalise page tempate map with basic info about blockchain + mstch::map context { + {"blk_height" , fmt::format("{:d}", _blk_height)} + }; + + // get block's hash + crypto::hash blk_hash = core_storage->get_block_id_by_height(_blk_height); + + // read block.html + string block_html = xmreg::read(TMPL_BLOCK); + + // add header and footer + string full_page = get_full_page(block_html); + + // render the page + return mstch::render(full_page, context); + } + + string + show_block(string _blk_hash) + { + crypto::hash blk_hash; + + if (!xmreg::parse_str_secret_key(_blk_hash, blk_hash)) + { + cerr << "Cant parse blk hash: " << blk_hash << endl; + return fmt::format("Cant parse block hash {:s}!", blk_hash); + } + + uint64_t blk_height; + + try + { + blk_height = core_storage->get_db().get_block_height(blk_hash); + } + catch (const BLOCK_DNE& e) + { + cerr << "Block does not exist: " << blk_hash << endl; + return fmt::format("Block of hash {:s} does not exist!", blk_hash); + } + catch (const std::exception& e) + { + cerr << "Cant get block: " << blk_hash << endl; + return fmt::format("Block of hash {:s} not found!", blk_hash); + } + + return show_block(blk_height); + } + + private: uint64_t diff --git a/src/templates/block.html b/src/templates/block.html new file mode 100644 index 0000000..ca26211 --- /dev/null +++ b/src/templates/block.html @@ -0,0 +1,4 @@ + +
+ Height: {{blk_height}} +
\ No newline at end of file diff --git a/src/templates/footer.html b/src/templates/footer.html index ca7ad58..0748056 100644 --- a/src/templates/footer.html +++ b/src/templates/footer.html @@ -1,6 +1,6 @@ diff --git a/src/templates/header.html b/src/templates/header.html index 950339c..fa08551 100644 --- a/src/templates/header.html +++ b/src/templates/header.html @@ -59,4 +59,9 @@ -
\ No newline at end of file +
+ +
+

Onion Monero Blockchain Explorer

+

(no javascript - no web analytics trackers - no images - open sourced)

+
\ No newline at end of file diff --git a/src/templates/index.html b/src/templates/index.html index 8455442..cf63c01 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -1,7 +1,5 @@
-

Onion Monero Blockchain Explorer

-

(no javascript - no web analytics trackers - no images - open sourced)

Server time: {{server_timestamp}} | @@ -39,9 +37,9 @@ {{#blocks}} - {{height}} + {{height}} {{age}} - {{hash}} + {{hash}} {{block_reward}} {{notx}} {{xmr_inputs}}/{{xmr_outputs}}