onion-wownero-blockchain-ex.../src/page.h

1751 lines
64 KiB
C
Raw Normal View History

2016-04-08 06:54:04 +00:00
//
// Created by mwo on 8/04/16.
//
#ifndef CROWXMR_PAGE_H
#define CROWXMR_PAGE_H
2016-04-08 09:32:12 +00:00
#include "mstch/mstch.hpp"
2016-04-12 22:20:14 +00:00
#include "rapidjson/document.h"
2016-04-08 09:32:12 +00:00
#include "../ext/format.h"
2016-04-24 01:11:08 +00:00
#include "../ext/member_checker.h"
2016-04-08 09:32:12 +00:00
2016-04-08 06:54:04 +00:00
#include "monero_headers.h"
2016-04-08 09:32:12 +00:00
#include "MicroCore.h"
2016-04-08 06:54:04 +00:00
#include "tools.h"
2016-04-12 22:20:14 +00:00
#include "rpccalls.h"
#include "mylmdb.h"
2016-04-08 06:54:04 +00:00
2016-04-12 03:31:26 +00:00
#include <algorithm>
2016-04-18 01:40:57 +00:00
#include <limits>
2016-04-18 05:57:38 +00:00
#include <ctime>
2016-04-12 03:31:26 +00:00
2016-04-28 07:03:16 +00:00
#define TMPL_DIR "./templates"
2016-04-30 01:35:48 +00:00
#define TMPL_PARIALS_DIR TMPL_DIR "/partials"
2016-04-28 07:03:16 +00:00
#define TMPL_INDEX TMPL_DIR "/index.html"
2016-05-01 02:02:37 +00:00
#define TMPL_INDEX2 TMPL_DIR "/index2.html"
2016-04-28 07:03:16 +00:00
#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"
#define TMPL_TX TMPL_DIR "/tx.html"
#define TMPL_SEARCH_RESULTS TMPL_DIR "/search_results.html"
2016-04-08 06:54:04 +00:00
namespace xmreg {
2016-04-24 01:11:08 +00:00
2016-04-08 09:32:12 +00:00
using namespace cryptonote;
using namespace crypto;
using namespace std;
2016-04-08 06:54:04 +00:00
2016-04-24 01:11:08 +00:00
// define a checker to test if a structure has "tx_blob"
// member variable. I use modified daemon with few extra
// bits and pieces here and there. One of them is
// tx_blob in cryptonote::tx_info structure
// thus I check if I run my version, or just
// generic one
DEFINE_MEMBER_CHECKER(tx_blob)
// define getter to get tx_blob, i.e., get_tx_blob function
// as string if exists. the getter return empty string if
// tx_blob does not exist
DEFINE_MEMBER_GETTER(tx_blob, string)
2016-04-17 01:44:24 +00:00
/**
* @brief The tx_details struct
*
* Basic information about tx
*
*/
struct tx_details
{
crypto::hash hash;
crypto::public_key pk;
uint64_t xmr_inputs;
uint64_t xmr_outputs;
uint64_t fee;
uint64_t mixin_no;
uint64_t size;
2016-04-30 12:16:44 +00:00
size_t version;
2016-04-17 01:44:24 +00:00
uint64_t unlock_time;
2016-05-02 06:09:41 +00:00
vector<uint8_t> extra;
2016-04-17 01:44:24 +00:00
2016-04-25 03:54:26 +00:00
crypto::hash payment_id = null_hash; // normal
crypto::hash8 payment_id8 = null_hash8; // encrypted
2016-04-23 06:14:22 +00:00
std::vector<std::vector<crypto::signature> > signatures;
2016-04-19 07:13:16 +00:00
// key images of inputs
vector<txin_to_key> input_key_imgs;
// public keys and xmr amount of outputs
vector<pair<txout_to_key, uint64_t>> output_pub_keys;
2016-04-17 01:44:24 +00:00
mstch::map
get_mstch_map()
{
// remove "<" and ">" from the hash string
string tx_hash_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", hash));
string tx_pk_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", pk));
2016-04-30 12:16:44 +00:00
//cout << "payment_id: " << payment_id << endl;
2016-04-25 03:54:26 +00:00
string pid_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", payment_id));
string pid8_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", payment_id8));
2016-05-01 03:31:57 +00:00
string mixin_str {"N/A"};
string fee_str {"N/A"};
string fee_short_str {"N/A"};
if (!input_key_imgs.empty())
{
mixin_str = std::to_string(mixin_no - 1);
fee_str = fmt::format("{:0.6f}", XMR_AMOUNT(fee));
fee_short_str = fmt::format("{:0.3f}", XMR_AMOUNT(fee));
}
2016-04-30 12:16:44 +00:00
//cout << "extra: " << extra_str << endl;
2016-04-23 06:14:22 +00:00
mstch::map txd_map {
2016-05-01 03:31:57 +00:00
{"hash" , tx_hash_str},
{"pub_key" , tx_pk_str},
{"tx_fee" , fee_str},
{"tx_fee_short" , fee_short_str},
{"sum_inputs" , fmt::format("{:0.6f}", XMR_AMOUNT(xmr_inputs))},
{"sum_outputs" , fmt::format("{:0.6f}", XMR_AMOUNT(xmr_outputs))},
{"sum_inputs_short" , fmt::format("{:0.3f}", XMR_AMOUNT(xmr_inputs))},
{"sum_outputs_short" , fmt::format("{:0.3f}", XMR_AMOUNT(xmr_outputs))},
{"no_inputs" , input_key_imgs.size()},
{"no_outputs" , output_pub_keys.size()},
{"mixin" , mixin_str},
{"version" , std::to_string(version)},
{"has_payment_id" , payment_id != null_hash},
{"has_payment_id8" , payment_id8 != null_hash8},
{"payment_id" , pid_str},
{"extra" , get_extra_str()},
{"payment_id8" , pid8_str},
{"unlock_time" , std::to_string(unlock_time)},
{"tx_size" , fmt::format("{:0.4f}",
static_cast<double>(size)/1024.0)},
{"tx_size_short" , fmt::format("{:0.2f}",
static_cast<double>(size)/1024.0)}
2016-04-17 01:44:24 +00:00
};
2016-04-23 06:14:22 +00:00
return txd_map;
2016-04-17 01:44:24 +00:00
}
2016-04-23 06:14:22 +00:00
2016-04-30 12:16:44 +00:00
string
get_extra_str()
{
string extra_str = epee::string_tools::buff_to_hex_nodelimer(
string{reinterpret_cast<const char*>(extra.data()), extra.size()});
return extra_str;
}
mstch::array
get_ring_sig_for_input(uint64_t in_i)
{
2016-04-23 06:14:22 +00:00
mstch::array ring_sigs {};
if (in_i >= signatures.size())
{
return ring_sigs;
}
for (const crypto::signature &sig: signatures.at(in_i))
2016-04-25 03:54:26 +00:00
{
2016-04-24 01:11:08 +00:00
ring_sigs.push_back(mstch::map{
{"ring_sig", print_signature(sig)}
});
2016-04-23 06:14:22 +00:00
}
return ring_sigs;
2016-04-30 12:16:44 +00:00
}
2016-04-23 06:14:22 +00:00
2016-04-30 12:16:44 +00:00
string
print_signature(const signature& sig)
{
2016-04-23 06:14:22 +00:00
stringstream ss;
ss << epee::string_tools::pod_to_hex(sig.c)
<< epee::string_tools::pod_to_hex(sig.r);
return ss.str();
2016-04-30 12:16:44 +00:00
}
2016-04-17 01:44:24 +00:00
};
2016-04-08 06:54:04 +00:00
class page {
2016-04-24 01:11:08 +00:00
// check if we have tx_blob member in tx_info structure
static const bool HAVE_TX_BLOB {
HAS_MEMBER(cryptonote::tx_info, tx_blob)
};
2016-04-22 02:10:30 +00:00
static const bool FULL_AGE_FORMAT {true};
2016-04-08 09:32:12 +00:00
MicroCore* mcore;
Blockchain* core_storage;
2016-04-12 22:20:14 +00:00
rpccalls rpc;
2016-04-13 07:00:07 +00:00
time_t server_timestamp;
2016-04-08 09:32:12 +00:00
2016-04-24 01:11:08 +00:00
2016-04-08 06:54:04 +00:00
public:
2016-04-16 09:58:21 +00:00
page(MicroCore* _mcore, Blockchain* _core_storage, string deamon_url)
2016-04-15 02:59:41 +00:00
: mcore {_mcore},
core_storage {_core_storage},
2016-04-16 09:58:21 +00:00
rpc {deamon_url},
2016-04-14 07:58:01 +00:00
server_timestamp {std::time(nullptr)}
2016-04-08 09:32:12 +00:00
{
2016-04-24 01:11:08 +00:00
2016-04-08 09:32:12 +00:00
}
2016-05-01 02:02:37 +00:00
/**
* @brief Show recent blocks and mempool
2016-05-07 10:33:56 +00:00
*
* Not used currently. index2 method is used instead
*
2016-05-01 02:02:37 +00:00
* @param page_no block page to show
* @param refresh_page enable autorefresh
* @return rendered index page
*/
2016-04-08 06:54:04 +00:00
string
2016-04-15 02:59:41 +00:00
index(uint64_t page_no = 0, bool refresh_page = false)
2016-04-08 06:54:04 +00:00
{
2016-04-15 21:48:37 +00:00
// connect to the deamon if not yet connected
bool is_connected = rpc.connect_to_monero_deamon();
2016-04-15 21:48:37 +00:00
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!";
}
2016-04-12 03:31:26 +00:00
//get current server timestamp
2016-04-13 07:00:07 +00:00
server_timestamp = std::time(nullptr);
2016-04-12 03:31:26 +00:00
2016-04-15 02:59:41 +00:00
// number of last blocks to show
uint64_t no_of_last_blocks {100 + 1};
2016-04-12 22:20:14 +00:00
uint64_t height = rpc.get_current_height() - 1;
2016-04-08 09:32:12 +00:00
2016-04-12 03:31:26 +00:00
// initalise page tempate map with basic info about blockchain
2016-04-08 09:32:12 +00:00
mstch::map context {
2016-04-15 02:59:41 +00:00
{"refresh" , refresh_page},
2016-04-17 01:44:24 +00:00
{"height" , std::to_string(height)},
2016-04-12 03:31:26 +00:00
{"server_timestamp", xmreg::timestamp_to_str(server_timestamp)},
2016-04-15 02:59:41 +00:00
{"blocks" , mstch::array()},
2016-04-15 03:27:57 +00:00
{"age_format" , string("[h:m:d]")},
2016-04-17 01:44:24 +00:00
{"page_no" , std::to_string(page_no)},
{"total_page_no" , std::to_string(height / (no_of_last_blocks))},
2016-04-15 04:41:15 +00:00
{"is_page_zero" , !bool(page_no)},
2016-04-17 01:44:24 +00:00
{"next_page" , std::to_string(page_no + 1)},
{"prev_page" , std::to_string((page_no > 0 ? page_no - 1 : 0))}
2016-04-08 09:32:12 +00:00
};
2016-04-12 03:31:26 +00:00
// get reference to blocks template map to be field below
2016-04-08 09:32:12 +00:00
mstch::array& blocks = boost::get<mstch::array>(context["blocks"]);
2016-04-15 04:41:15 +00:00
// calculate starting and ending block numbers to show
2016-04-15 02:59:41 +00:00
uint64_t start_height = height - no_of_last_blocks * (page_no + 1);
uint64_t end_height = height - no_of_last_blocks * (page_no);
2016-04-15 04:41:15 +00:00
// check few conditions to make sure we are whithin the avaliable range
2016-04-15 07:03:48 +00:00
//@TODO its too messed up. needs to find cleaner way.
2016-04-15 02:59:41 +00:00
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;
2016-04-15 03:27:57 +00:00
end_height = end_height - start_height > no_of_last_blocks
? no_of_last_blocks : end_height;
2016-04-15 02:59:41 +00:00
2016-04-18 01:40:57 +00:00
// previous blk timestamp, initalised to lowest possible value
double prev_blk_timestamp {std::numeric_limits<double>::lowest()};
2016-04-18 00:50:21 +00:00
2016-04-12 03:31:26 +00:00
// iterate over last no_of_last_blocks of blocks
2016-04-15 02:59:41 +00:00
for (uint64_t i = start_height; i <= end_height; ++i)
2016-04-08 09:32:12 +00:00
{
2016-04-12 03:31:26 +00:00
// get block at the given height i
2016-04-08 09:32:12 +00:00
block blk;
if (!mcore->get_block_by_height(i, blk))
{
cerr << "Cant get block: " << i << endl;
continue;
}
2016-04-08 09:32:12 +00:00
2016-04-12 03:31:26 +00:00
// get block's hash
2016-04-08 09:32:12 +00:00
crypto::hash blk_hash = core_storage->get_block_id_by_height(i);
2016-04-16 03:44:58 +00:00
// remove "<" and ">" from the hash string
2016-04-13 08:29:01 +00:00
string blk_hash_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", blk_hash));
2016-04-16 03:44:58 +00:00
// get block timestamp in user friendly format
2016-04-14 07:58:01 +00:00
string timestamp_str = xmreg::timestamp_to_str(blk.timestamp);
2016-04-12 04:14:15 +00:00
2016-04-16 03:44:58 +00:00
pair<string, string> age = get_age(server_timestamp, blk.timestamp);
2016-04-15 07:03:48 +00:00
2016-04-16 03:44:58 +00:00
context["age_format"] = age.second;
2016-04-15 02:59:41 +00:00
2016-04-18 00:50:21 +00:00
// get time difference [m] between previous and current blocks
string time_delta_str {};
2016-04-18 01:40:57 +00:00
if (prev_blk_timestamp > std::numeric_limits<double>::lowest())
2016-04-18 00:50:21 +00:00
{
time_delta_str = fmt::format("{:0.2f}",
(double(blk.timestamp) - double(prev_blk_timestamp))/60.0);
}
2016-04-12 03:31:26 +00:00
// 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);
2016-04-12 03:31:26 +00:00
// get mixin number in each transaction
2016-04-12 22:36:27 +00:00
vector<uint64_t> mixin_numbers = xmreg::get_mixin_no_in_txs(txs_in_blk);
2016-04-12 03:31:26 +00:00
// 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()));
}
2016-04-15 07:03:48 +00:00
// mixing format for the templates
2016-04-12 03:31:26 +00:00
auto mixin_format = [=]() -> mstch::node
{
if (mixin_min < 0)
{
return string("N/A");
}
2016-04-15 00:13:04 +00:00
return fmt::format("{:d}-{:d}", mixin_min - 1, mixin_max - 1);
2016-04-12 03:31:26 +00:00
};
2016-04-15 07:03:48 +00:00
// get block size in bytes
2016-04-13 01:49:09 +00:00
uint64_t blk_size = get_object_blobsize(blk);
2016-04-12 03:31:26 +00:00
// set output page template map
2016-04-08 09:32:12 +00:00
blocks.push_back(mstch::map {
2016-04-12 03:31:26 +00:00
{"height" , to_string(i)},
2016-04-12 04:14:15 +00:00
{"timestamp" , timestamp_str},
2016-04-18 00:50:21 +00:00
{"time_delta" , time_delta_str},
2016-04-16 03:44:58 +00:00
{"age" , age.first},
2016-04-13 08:29:01 +00:00
{"hash" , blk_hash_str},
2016-04-14 00:31:19 +00:00
{"block_reward", fmt::format("{:0.4f}/{:0.4f}",
XMR_AMOUNT(coinbase_tx[1] - sum_fees),
XMR_AMOUNT(sum_fees))},
2016-04-17 22:37:55 +00:00
{"fees" , fmt::format("{:0.3f}", XMR_AMOUNT(sum_fees))},
2016-04-12 03:31:26 +00:00
{"notx" , fmt::format("{:d}", blk.tx_hashes.size())},
2016-04-13 00:15:56 +00:00
{"xmr_inputs" , fmt::format("{:0.2f}",
2016-04-12 05:25:12 +00:00
XMR_AMOUNT(sum_xmr_in_out[0]))},
2016-04-13 00:15:56 +00:00
{"xmr_outputs" , fmt::format("{:0.2f}",
2016-04-12 05:25:12 +00:00
XMR_AMOUNT(sum_xmr_in_out[1]))},
2016-04-13 01:49:09 +00:00
{"mixin_range" , mstch::lambda {mixin_format}},
{"blksize" , fmt::format("{:0.2f}",
static_cast<double>(blk_size) / 1024.0)}
2016-04-08 09:32:12 +00:00
});
2016-04-18 00:50:21 +00:00
// save current's block timestamp as reference for the next one
2016-04-18 01:40:57 +00:00
prev_blk_timestamp = static_cast<double>(blk.timestamp);
2016-04-18 00:50:21 +00:00
2016-04-15 23:07:47 +00:00
} // for (uint64_t i = start_height; i <= end_height; ++i)
2016-04-08 09:32:12 +00:00
2016-04-12 05:25:12 +00:00
// 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());
2016-04-18 01:40:57 +00:00
// 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();
}
2016-04-12 05:25:12 +00:00
2016-04-15 02:59:41 +00:00
// get memory pool rendered template
2016-04-12 07:15:33 +00:00
string mempool_html = mempool();
2016-04-15 02:59:41 +00:00
// append mempool_html to the index context map
2016-04-12 07:15:33 +00:00
context["mempool_info"] = mempool_html;
2016-04-12 03:31:26 +00:00
// read index.html
2016-04-12 07:15:33 +00:00
string index_html = xmreg::read(TMPL_INDEX);
2016-04-08 09:32:12 +00:00
2016-04-12 03:31:26 +00:00
// add header and footer
string full_page = get_full_page(index_html);
2016-04-08 09:32:12 +00:00
2016-04-12 03:31:26 +00:00
// render the page
return mstch::render(full_page, context);
2016-04-08 06:54:04 +00:00
}
2016-05-01 02:02:37 +00:00
/**
* @brief show recent transactions and mempool
* @param page_no block page to show
* @param refresh_page enable autorefresh
* @return rendered index page
*/
string
index2(uint64_t page_no = 0, bool refresh_page = false)
{
//get current server timestamp
server_timestamp = std::time(nullptr);
// number of last blocks to show
2016-05-01 03:31:57 +00:00
uint64_t no_of_last_blocks {50 + 1};
2016-05-01 02:02:37 +00:00
// get the current blockchain height. Just to check
uint64_t height =
2016-05-01 03:31:57 +00:00
xmreg::MyLMDB::get_blockchain_height(mcore->get_blkchain_path()) - 1;
2016-05-01 02:02:37 +00:00
// 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))},
{"txs" , mstch::array()}, // will keep tx to show
};
// get reference to txs mstch map to be field below
mstch::array& txs = boost::get<mstch::array>(context["txs"]);
// 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);
2016-05-01 03:31:57 +00:00
// 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;
2016-05-01 02:02:37 +00:00
// 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;
}
2016-05-01 02:13:44 +00:00
// 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));
2016-05-01 02:02:37 +00:00
// get block age
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())
{
2016-05-01 03:31:57 +00:00
time_delta_str = fmt::format("({:06.2f})",
2016-05-01 02:02:37 +00:00
(double(blk.timestamp) - double(prev_blk_timestamp))/60.0);
}
// get all transactions in the block found
// initialize the first list with transaction for solving
// the block i.e. coinbase.
list<cryptonote::transaction> blk_txs {blk.miner_tx};
list<crypto::hash> missed_txs;
if (!core_storage->get_transactions(blk.tx_hashes, blk_txs, missed_txs))
{
cerr << "Cant get transactions in block: " << i << endl;
continue;
}
2016-05-01 03:31:57 +00:00
uint64_t tx_i {0};
2016-05-01 04:17:19 +00:00
for(list<cryptonote::transaction>::reverse_iterator rit = blk_txs.rbegin();
rit != blk_txs.rend(); ++rit)
2016-05-02 06:09:41 +00:00
{
2016-05-01 04:17:19 +00:00
const cryptonote::transaction& tx = *rit;
2016-05-01 02:02:37 +00:00
tx_details txd = get_tx_details(tx);
mstch::map txd_map = txd.get_mstch_map();
//add age to the txd mstch map
2016-05-01 02:13:44 +00:00
txd_map.insert({"height" , i});
txd_map.insert({"blk_hash" , blk_hash_str});
2016-05-01 02:02:37 +00:00
txd_map.insert({"time_delta", time_delta_str});
txd_map.insert({"age" , age.first});
2016-05-01 03:31:57 +00:00
// do not show block info for other than
2016-05-01 04:17:19 +00:00
// last (i.e., first after reverse below)
// tx in the block
if (tx_i < blk_txs.size() - 1)
2016-05-01 03:31:57 +00:00
{
txd_map["height"] = string("");
txd_map["age"] = string("");
txd_map["time_delta"] = string("");
}
2016-05-01 02:02:37 +00:00
txs.push_back(txd_map);
2016-05-01 03:31:57 +00:00
++tx_i;
2016-05-01 02:02:37 +00:00
}
2016-05-01 03:31:57 +00:00
// save current's block timestamp as reference for the next one
prev_blk_timestamp = static_cast<double>(blk.timestamp);
2016-05-01 02:02:37 +00:00
} // for (uint64_t i = start_height; i <= end_height; ++i)
// reverse txs and remove last (i.e., oldest)
// tx. This is done so that time delats
// are easier to calcualte in the above for loop
std::reverse(txs.begin(), txs.end());
// if we look at the genesis time, we should not remove
// the last block, i.e. genesis one.
if (!(start_height < 2))
{
2016-05-01 03:31:57 +00:00
//txs.pop_back();
2016-05-01 02:02:37 +00:00
}
// 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
2016-05-01 02:13:44 +00:00
string index2_html = xmreg::read(TMPL_INDEX2);
2016-05-01 02:02:37 +00:00
// add header and footer
2016-05-01 02:13:44 +00:00
string full_page = get_full_page(index2_html);
2016-05-01 02:02:37 +00:00
// render the page
return mstch::render(full_page, context);
}
2016-05-07 10:33:56 +00:00
/**
* Render mempool data
*/
2016-04-12 07:15:33 +00:00
string
mempool()
{
2016-04-15 07:03:48 +00:00
std::vector<tx_info> mempool_txs;
2016-04-12 07:15:33 +00:00
2016-04-15 07:03:48 +00:00
if (!rpc.get_mempool(mempool_txs))
2016-04-12 07:15:33 +00:00
{
2016-04-15 07:03:48 +00:00
return "Getting mempool failed";
2016-04-12 07:15:33 +00:00
}
2016-04-14 07:58:01 +00:00
// initalise page tempate map with basic info about mempool
2016-04-12 07:15:33 +00:00
mstch::map context {
2016-04-17 01:44:24 +00:00
{"mempool_size", std::to_string(mempool_txs.size())},
2016-04-12 07:15:33 +00:00
{"mempooltxs" , mstch::array()}
};
// get reference to blocks template map to be field below
mstch::array& txs = boost::get<mstch::array>(context["mempooltxs"]);
// for each transaction in the memory pool
2016-04-15 07:03:48 +00:00
for (size_t i = 0; i < mempool_txs.size(); ++i)
2016-04-12 07:15:33 +00:00
{
// get transaction info of the tx in the mempool
2016-04-15 07:03:48 +00:00
tx_info _tx_info = mempool_txs.at(i);
2016-04-12 07:15:33 +00:00
2016-04-15 02:59:41 +00:00
// calculate difference between tx in mempool and server timestamps
2016-04-13 07:00:07 +00:00
array<size_t, 5> delta_time = timestamp_difference(
2016-04-15 02:59:41 +00:00
server_timestamp,
2016-04-14 07:58:01 +00:00
_tx_info.receive_time);
2016-04-12 07:15:33 +00:00
2016-04-15 02:59:41 +00:00
// use only hours, so if we have days, add
2016-04-14 07:58:01 +00:00
// it to hours
2016-04-13 07:00:07 +00:00
uint64_t delta_hours {delta_time[1]*24 + delta_time[2]};
2016-04-12 07:15:33 +00:00
2016-04-13 07:00:07 +00:00
string age_str = fmt::format("{:02d}:{:02d}:{:02d}",
2016-04-13 08:29:01 +00:00
delta_hours,
2016-04-13 07:00:07 +00:00
delta_time[3], delta_time[4]);
2016-04-15 02:59:41 +00:00
// if more than 99 hourse, change formating
// for the template
2016-04-13 07:00:07 +00:00
if (delta_hours > 99)
{
age_str = fmt::format("{:03d}:{:02d}:{:02d}",
2016-04-13 08:29:01 +00:00
delta_hours,
2016-04-13 07:00:07 +00:00
delta_time[3], delta_time[4]);
}
2016-04-12 07:15:33 +00:00
2016-04-15 02:59:41 +00:00
// sum xmr in inputs and ouputs in the given tx
2016-05-01 03:43:48 +00:00
pair<uint64_t, uint64_t> sum_inputs = sum_xmr_inputs(_tx_info.tx_json);
pair<uint64_t, uint64_t> sum_outputs = sum_xmr_outputs(_tx_info.tx_json);
2016-04-12 22:36:27 +00:00
// get mixin number in each transaction
vector<uint64_t> mixin_numbers = get_mixin_no_in_txs(_tx_info.tx_json);
2016-04-12 22:20:14 +00:00
2016-04-12 07:15:33 +00:00
// set output page template map
txs.push_back(mstch::map {
2016-04-13 07:00:07 +00:00
{"timestamp" , xmreg::timestamp_to_str(_tx_info.receive_time)},
{"age" , age_str},
2016-04-13 08:29:01 +00:00
{"hash" , fmt::format("{:s}", _tx_info.id_hash)},
2016-04-17 22:37:55 +00:00
{"fee" , fmt::format("{:0.3f}", XMR_AMOUNT(_tx_info.fee))},
2016-05-01 03:43:48 +00:00
{"xmr_inputs" , fmt::format("{:0.2f}", XMR_AMOUNT(sum_inputs.first))},
{"xmr_outputs" , fmt::format("{:0.2f}", XMR_AMOUNT(sum_outputs.first))},
{"no_inputs" , sum_inputs.second},
{"no_outputs" , sum_outputs.second},
2016-04-15 00:13:04 +00:00
{"mixin" , fmt::format("{:d}", mixin_numbers.at(0) - 1)},
2016-04-13 07:00:07 +00:00
{"txsize" , fmt::format("{:0.2f}", static_cast<double>(_tx_info.blob_size)/1024.0)}
2016-04-12 07:15:33 +00:00
});
}
// read index.html
string mempool_html = xmreg::read(TMPL_MEMPOOL);
// render the page
return mstch::render(mempool_html, context);
}
2016-04-15 23:07:47 +00:00
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;
2016-04-24 03:42:49 +00:00
return fmt::format("Cant get block {:d}!", _blk_height);
2016-04-15 23:07:47 +00:00
}
2016-04-16 03:44:58 +00:00
// get block's hash
crypto::hash blk_hash = core_storage->get_block_id_by_height(_blk_height);
crypto::hash prev_hash = blk.prev_id;
crypto::hash next_hash = core_storage->get_block_id_by_height(_blk_height + 1);
bool have_next_hash = (next_hash == null_hash ? false : true);
bool have_prev_hash = (prev_hash == null_hash ? false : true);
// remove "<" and ">" from the hash string
string prev_hash_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", prev_hash));
string next_hash_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", next_hash));
// 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 blk_timestamp = xmreg::timestamp_to_str(blk.timestamp);
// get age of the block relative to the server time
pair<string, string> age = get_age(server_timestamp, blk.timestamp);
// get time from the last block
string delta_time {"N/A"};
if (have_prev_hash)
{
block prev_blk = core_storage->get_db().get_block(prev_hash);
pair<string, string> delta_diff = get_age(blk.timestamp, prev_blk.timestamp);
delta_time = delta_diff.first;
}
// get block size in bytes
uint64_t blk_size = get_object_blobsize(blk);
// miner reward tx
transaction coinbase_tx = blk.miner_tx;
// transcation in the block
vector<crypto::hash> tx_hashes = blk.tx_hashes;
2016-04-17 05:09:46 +00:00
2016-04-16 04:11:17 +00:00
bool have_txs = !blk.tx_hashes.empty();
2016-04-17 05:09:46 +00:00
// sum of all transactions in the block
uint64_t sum_fees = 0;
// get tx details for the coinbase tx, i.e., miners reward
tx_details txd_coinbase = get_tx_details(blk.miner_tx, true);
2016-04-15 23:07:47 +00:00
// initalise page tempate map with basic info about blockchain
mstch::map context {
2016-04-16 03:44:58 +00:00
{"blk_hash" , blk_hash_str},
2016-04-17 05:09:46 +00:00
{"blk_height" , _blk_height},
2016-04-16 03:44:58 +00:00
{"blk_timestamp" , blk_timestamp},
{"prev_hash" , prev_hash_str},
{"next_hash" , next_hash_str},
{"have_next_hash" , have_next_hash},
{"have_prev_hash" , have_prev_hash},
2016-04-17 01:44:24 +00:00
{"have_txs" , have_txs},
{"no_txs" , std::to_string(blk.tx_hashes.size())},
2016-04-16 03:44:58 +00:00
{"blk_age" , age.first},
{"delta_time" , delta_time},
2016-04-17 05:09:46 +00:00
{"blk_nonce" , blk.nonce},
2016-04-16 03:44:58 +00:00
{"age_format" , age.second},
{"major_ver" , std::to_string(blk.major_version)},
{"minor_ver" , std::to_string(blk.minor_version)},
{"blk_size" , fmt::format("{:0.4f}",
2016-04-17 22:37:55 +00:00
static_cast<double>(blk_size) / 1024.0)},
2016-04-17 05:09:46 +00:00
{"coinbase_txs" , mstch::array{{txd_coinbase.get_mstch_map()}}},
{"blk_txs" , mstch::array()}
2016-04-15 23:07:47 +00:00
};
2016-04-17 05:09:46 +00:00
// .push_back(txd_coinbase.get_mstch_map()
// boost::get<mstch::array>(context["blk_txs"]).push_back(txd_coinbase.get_mstch_map());
2016-04-17 01:44:24 +00:00
2016-04-17 05:09:46 +00:00
// now process nomral transactions
2016-04-17 01:44:24 +00:00
// get reference to blocks template map to be field below
mstch::array& txs = boost::get<mstch::array>(context["blk_txs"]);
2016-04-20 07:11:43 +00:00
// timescale representation for each tx in the block
vector<string> mixin_timescales_str;
2016-05-01 02:02:37 +00:00
// for each transaction in the block
2016-04-17 01:44:24 +00:00
for (size_t i = 0; i < blk.tx_hashes.size(); ++i)
{
// get transaction info of the tx in the mempool
const crypto::hash& tx_hash = blk.tx_hashes.at(i);
// remove "<" and ">" from the hash string
string tx_hash_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", tx_hash));
// get transaction
transaction tx;
if (!mcore->get_tx(tx_hash, tx))
{
cerr << "Cant get tx: " << tx_hash << endl;
continue;
}
tx_details txd = get_tx_details(tx);
2016-04-17 05:09:46 +00:00
// add fee to the rest
sum_fees += txd.fee;
2016-04-20 07:11:43 +00:00
// get mixins in time scale for visual representation
//string mixin_times_scale = xmreg::timestamps_time_scale(mixin_timestamps,
// server_timestamp);
2016-04-17 01:44:24 +00:00
// add tx details mstch map to context
txs.push_back(txd.get_mstch_map());
}
2016-04-17 05:09:46 +00:00
// add total fees in the block to the context
context["sum_fees"] = fmt::format("{:0.6f}",
XMR_AMOUNT(sum_fees));
// get xmr in the block reward
context["blk_reward"] = fmt::format("{:0.6f}",
XMR_AMOUNT(txd_coinbase.xmr_outputs - sum_fees));
2016-04-15 23:07:47 +00:00
// 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);
}
2016-04-17 05:09:46 +00:00
2016-04-15 23:07:47 +00:00
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;
2016-04-28 05:17:55 +00:00
return fmt::format("Cant get block {:s} due to block hash parse error!", blk_hash);
2016-04-15 23:07:47 +00:00
}
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;
2016-04-28 05:17:55 +00:00
return fmt::format("Cant get block {:s} because it does not exist!", blk_hash);
2016-04-15 23:07:47 +00:00
}
catch (const std::exception& e)
{
cerr << "Cant get block: " << blk_hash << endl;
2016-04-28 05:17:55 +00:00
return fmt::format("Cant get block {:s} for some uknown reason", blk_hash);
2016-04-15 23:07:47 +00:00
}
return show_block(blk_height);
}
2016-04-18 05:57:38 +00:00
string
show_tx(string tx_hash_str, uint with_ring_signatures = 0)
2016-04-18 05:57:38 +00:00
{
2016-04-20 07:11:43 +00:00
// parse tx hash string to hash object
crypto::hash tx_hash;
if (!xmreg::parse_str_secret_key(tx_hash_str, tx_hash))
{
cerr << "Cant parse tx hash: " << tx_hash_str << endl;
2016-04-24 03:42:49 +00:00
return string("Cant get tx hash due to parse error: " + tx_hash_str);
2016-04-20 07:11:43 +00:00
}
2016-04-24 01:11:08 +00:00
// tx age
pair<string, string> age;
string blk_timestamp {"N/A"};
2016-04-20 07:11:43 +00:00
// get transaction
transaction tx;
if (!mcore->get_tx(tx_hash, tx))
{
2016-04-24 03:42:49 +00:00
cerr << "Cant get tx in blockchain: " << tx_hash
<< ". \n Check mempool now" << endl;
2016-04-24 01:11:08 +00:00
vector<pair<tx_info, transaction>> found_txs
= search_mempool(tx_hash);
if (!found_txs.empty())
{
// there should be only one tx found
tx = found_txs.at(0).second;
// since its tx in mempool, it has no blk yet
// so use its recive_time as timestamp to show
uint64_t tx_recieve_timestamp
= found_txs.at(0).first.receive_time;
blk_timestamp = xmreg::timestamp_to_str(tx_recieve_timestamp);
age = get_age(server_timestamp, tx_recieve_timestamp,
FULL_AGE_FORMAT);
}
else
{
// tx is nowhere to be found :-(
2016-04-24 03:42:49 +00:00
return string("Cant get tx: " + tx_hash_str);
2016-04-24 01:11:08 +00:00
}
2016-04-20 07:11:43 +00:00
}
tx_details txd = get_tx_details(tx);
2016-04-22 06:15:06 +00:00
uint64_t tx_blk_height {0};
bool tx_blk_found {false};
try
{
tx_blk_height = core_storage->get_db().get_tx_block_height(tx_hash);
tx_blk_found = true;
}
2016-04-24 01:11:08 +00:00
catch (exception& e)
2016-04-22 06:15:06 +00:00
{
2016-04-24 01:11:08 +00:00
cerr << "Cant get block height: " << tx_hash
<< e.what() << endl;
2016-04-22 06:15:06 +00:00
}
// get block cointaining this tx
block blk;
2016-04-24 01:11:08 +00:00
if (tx_blk_found && !mcore->get_block_by_height(tx_blk_height, blk))
2016-04-22 06:15:06 +00:00
{
cerr << "Cant get block: " << tx_blk_height << endl;
}
2016-04-24 01:11:08 +00:00
string tx_blk_height_str {"N/A"};
if (tx_blk_found)
{
// calculate difference between tx and server timestamps
age = get_age(server_timestamp, blk.timestamp, FULL_AGE_FORMAT);
blk_timestamp = xmreg::timestamp_to_str(blk.timestamp);
2016-04-23 06:14:22 +00:00
2016-04-24 01:11:08 +00:00
tx_blk_height_str = std::to_string(tx_blk_height);
2016-04-25 03:54:26 +00:00
}
2016-04-23 06:14:22 +00:00
2016-04-28 05:17:55 +00:00
// payments id. both normal and encrypted (payment_id8)
2016-04-25 03:54:26 +00:00
string pid_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", txd.payment_id));
string pid8_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", txd.payment_id8));
2016-04-18 05:57:38 +00:00
// initalise page tempate map with basic info about blockchain
mstch::map context {
{"tx_hash" , tx_hash_str},
{"tx_pub_key" , REMOVE_HASH_BRAKETS(fmt::format("{:s}", txd.pk))},
{"blk_height" , tx_blk_height_str},
{"tx_size" , fmt::format("{:0.4f}",
static_cast<double>(txd.size) / 1024.0)},
{"tx_fee" , fmt::format("{:0.12f}", XMR_AMOUNT(txd.fee))},
{"blk_timestamp" , blk_timestamp},
{"delta_time" , age.first},
{"inputs_no" , txd.input_key_imgs.size()},
2016-05-08 08:45:51 +00:00
{"has_inputs" , !txd.input_key_imgs.empty()},
{"outputs_no" , txd.output_pub_keys.size()},
{"has_payment_id" , txd.payment_id != null_hash},
{"has_payment_id8" , txd.payment_id8 != null_hash8},
{"payment_id" , pid_str},
{"payment_id8" , pid8_str},
2016-04-30 12:16:44 +00:00
{"extra" , txd.get_extra_str()},
{"with_ring_signatures" , static_cast<bool>(with_ring_signatures)}
2016-04-18 05:57:38 +00:00
};
2016-04-24 01:11:08 +00:00
string server_time_str = xmreg::timestamp_to_str(server_timestamp, "%F");
2016-04-20 17:32:51 +00:00
mstch::array inputs = mstch::array{};
2016-04-20 07:11:43 +00:00
mstch::array mixins_timescales;
2016-04-24 01:11:08 +00:00
2016-04-20 23:34:05 +00:00
double timescale_scale {0.0}; // size of one '_' in days
2016-04-20 07:11:43 +00:00
2016-04-22 01:54:16 +00:00
uint64_t input_idx {0};
2016-04-20 07:11:43 +00:00
// make timescale maps for mixins in input
for (const txin_to_key& in_key: txd.input_key_imgs)
{
// get absolute offsets of mixins
std::vector<uint64_t> absolute_offsets
= cryptonote::relative_output_offsets_to_absolute(
in_key.key_offsets);
// get public keys of outputs used in the mixins that match to the offests
std::vector<cryptonote::output_data_t> outputs;
core_storage->get_db().get_output_key(in_key.amount,
absolute_offsets,
outputs);
vector<uint64_t> mixin_timestamps;
2016-04-20 17:32:51 +00:00
inputs.push_back(mstch::map {
2016-04-20 23:38:31 +00:00
{"in_key_img", REMOVE_HASH_BRAKETS(fmt::format("{:s}", in_key.k_image))},
2016-04-22 02:10:30 +00:00
{"amount" , fmt::format("{:0.12f}", XMR_AMOUNT(in_key.amount))},
2016-04-23 06:14:22 +00:00
{"input_idx" , fmt::format("{:02d}", input_idx)},
2016-04-22 01:54:16 +00:00
{"mixins" , mstch::array{}},
2016-04-23 06:14:22 +00:00
{"ring_sigs" , txd.get_ring_sig_for_input(input_idx)}
2016-04-20 17:32:51 +00:00
});
2016-04-23 06:14:22 +00:00
2016-04-20 17:32:51 +00:00
// get reference to mixins array created above
mstch::array& mixins = boost::get<mstch::array>(
boost::get<mstch::map>(inputs.back())["mixins"]);
2016-04-20 07:11:43 +00:00
2016-04-24 01:11:08 +00:00
// mixin counter
2016-04-22 01:54:16 +00:00
size_t count = 0;
2016-04-20 07:11:43 +00:00
// for each found output public key find its block to get timestamp
for (const uint64_t &i: absolute_offsets)
{
// get basic information about mixn's output
2016-04-20 07:11:43 +00:00
cryptonote::output_data_t output_data = outputs.at(count);
// get pair pair<crypto::hash, uint64_t> where first is tx hash
// and second is local index of the output i in that tx
tx_out_index tx_out_idx =
core_storage->get_db().get_output_tx_and_index(in_key.amount, i);
2016-04-20 07:11:43 +00:00
// get block of given height, as we want to get its timestamp
cryptonote::block blk;
if (!mcore->get_block_by_height(output_data.height, blk))
{
cerr << "- cant get block of height: " << output_data.height << endl;
2016-04-22 06:15:06 +00:00
return fmt::format("- cant get block of height: {}", output_data.height);
2016-04-20 07:11:43 +00:00
}
2016-04-22 06:15:06 +00:00
// get age of mixin relative to server time
2016-04-22 02:10:30 +00:00
pair<string, string> mixin_age = get_age(server_timestamp,
blk.timestamp,
FULL_AGE_FORMAT);
2016-04-24 01:11:08 +00:00
// get mixin transaction
transaction mixin_tx;
2016-04-22 06:15:06 +00:00
2016-04-24 01:11:08 +00:00
if (!mcore->get_tx(tx_out_idx.first, mixin_tx))
{
cerr << "Cant get tx: " << tx_out_idx.first << endl;
return fmt::format("Cant get tx: {:s}", tx_out_idx.first);
}
2016-04-22 06:15:06 +00:00
// mixin tx details
2016-04-24 01:11:08 +00:00
tx_details mixin_txd = get_tx_details(mixin_tx, true);
2016-04-20 17:32:51 +00:00
2016-04-24 01:11:08 +00:00
mixins.push_back(mstch::map {
2016-04-22 02:10:30 +00:00
{"mix_blk" , fmt::format("{:08d}", output_data.height)},
2016-04-22 06:15:06 +00:00
{"mix_pub_key" , REMOVE_HASH_BRAKETS(fmt::format("{:s}",
output_data.pubkey))},
{"mix_tx_hash" , REMOVE_HASH_BRAKETS(fmt::format("{:s}",
tx_out_idx.first))},
{"mix_out_indx" , fmt::format("{:d}", tx_out_idx.second)},
{"mix_timestamp" , xmreg::timestamp_to_str(blk.timestamp)},
2016-04-20 17:32:51 +00:00
{"mix_age" , mixin_age.first},
2016-04-22 06:15:06 +00:00
{"mix_mixin_no" , mixin_txd.mixin_no},
{"mix_inputs_no" , mixin_txd.input_key_imgs.size()},
{"mix_outputs_no" , mixin_txd.output_pub_keys.size()},
2016-04-22 01:54:16 +00:00
{"mix_age_format" , mixin_age.second},
2016-04-22 02:10:30 +00:00
{"mix_idx" , fmt::format("{:02d}", count)},
2016-04-24 01:11:08 +00:00
});
2016-04-20 17:32:51 +00:00
2016-04-24 01:11:08 +00:00
// get mixin timestamp from its orginal block
mixin_timestamps.push_back(blk.timestamp);
2016-04-20 07:11:43 +00:00
2016-04-24 01:11:08 +00:00
++count;
2016-04-23 06:14:22 +00:00
} // for (const uint64_t &i: absolute_offsets)
2016-04-20 07:11:43 +00:00
// get mixins in time scale for visual representation
2016-04-20 23:34:05 +00:00
pair<string, double> mixin_times_scale = xmreg::timestamps_time_scale(
mixin_timestamps,
2016-05-01 22:29:27 +00:00
server_timestamp, 170);
2016-04-20 23:34:05 +00:00
2016-04-21 08:38:50 +00:00
// save resolution of mixin timescales
timescale_scale = mixin_times_scale.second;
2016-04-20 23:34:05 +00:00
2016-04-20 07:11:43 +00:00
// save the string timescales for later to show
2016-04-21 05:13:24 +00:00
mixins_timescales.push_back(mstch::map {
{"timescale", mixin_times_scale.first}});
2016-04-23 06:14:22 +00:00
input_idx++;
} // for (const txin_to_key& in_key: txd.input_key_imgs)
2016-04-20 07:11:43 +00:00
2016-04-21 04:52:40 +00:00
context["server_time"] = server_time_str;
2016-04-20 23:34:05 +00:00
context["inputs"] = inputs;
context["timescales"] = mixins_timescales;
context["timescales_scale"] = fmt::format("{:0.2f}",
timescale_scale / 3600.0 / 24.0); // in days
2016-04-20 07:11:43 +00:00
2016-04-22 01:54:16 +00:00
uint64_t output_idx {0};
mstch::array outputs;
for (pair<txout_to_key, uint64_t>& outp: txd.output_pub_keys)
{
outputs.push_back(mstch::map {
2016-04-20 23:38:31 +00:00
{"out_pub_key" , REMOVE_HASH_BRAKETS(fmt::format("{:s}", outp.first.key))},
2016-04-22 02:10:30 +00:00
{"amount" , fmt::format("{:0.12f}", XMR_AMOUNT(outp.second))},
2016-04-22 01:54:16 +00:00
{"output_idx" , fmt::format("{:02d}", output_idx++)}
});
}
context["outputs"] = outputs;
2016-04-18 05:57:38 +00:00
// read tx.html
string tx_html = xmreg::read(TMPL_TX);
// add header and footer
string full_page = get_full_page(tx_html);
// render the page
return mstch::render(full_page, context);
}
2016-04-24 03:42:49 +00:00
string
2016-04-24 03:57:48 +00:00
search(string search_text)
2016-04-24 03:42:49 +00:00
{
2016-04-24 03:57:48 +00:00
// remove white characters
boost::trim(search_text);
2016-04-24 03:49:42 +00:00
2016-04-28 05:17:55 +00:00
string default_txt {"No such thing found: " + search_text};
string result_html {default_txt};
2016-04-24 03:49:42 +00:00
// first check if searching for block of given height
2016-04-25 03:54:26 +00:00
if (search_text.size() < 12)
2016-04-24 03:49:42 +00:00
{
2016-04-24 04:15:08 +00:00
uint64_t blk_height;
2016-04-24 03:42:49 +00:00
2016-04-24 04:15:08 +00:00
try
2016-04-24 03:57:48 +00:00
{
2016-04-24 04:15:08 +00:00
blk_height = boost::lexical_cast<uint64_t>(search_text);
result_html = show_block(blk_height);
// nasty check if output is "Cant get" as a sign of
// a not found tx. Later need to think of something better.
if (result_html.find("Cant get") == string::npos)
{
return result_html;
}
}
catch(boost::bad_lexical_cast &e)
2016-04-25 03:54:26 +00:00
{
2016-04-24 04:18:31 +00:00
return result_html;
2016-04-24 03:57:48 +00:00
}
2016-04-24 04:15:08 +00:00
2016-04-24 03:42:49 +00:00
}
2016-04-24 03:49:42 +00:00
// second let try searching for tx
result_html = show_tx(search_text);
// nasty check if output is "Cant get" as a sign of
// a not found tx. Later need to think of something better.
if (result_html.find("Cant get") == string::npos)
{
return result_html;
}
// if tx search not successful, check if we are looking
// for a block with given hash
2016-04-24 03:42:49 +00:00
result_html = show_block(search_text);
2016-04-24 03:49:42 +00:00
if (result_html.find("Cant get") == string::npos)
{
2016-04-24 03:42:49 +00:00
return result_html;
}
2016-04-28 05:17:55 +00:00
result_html = default_txt;
2016-05-02 06:09:41 +00:00
// get mempool transaction so that what we search,
// might be there. Note: show_tx above already searches it
// but only looks for tx hash. Now want to check
// for key_images, public_keys, payments_id, etc.
vector<transaction> mempool_txs = get_mempool_txs();
// key is string indicating where search_text was found.
map<string, vector<string>> tx_search_results
= search_txs(mempool_txs, search_text);
2016-04-28 07:03:16 +00:00
// now search my own custom lmdb database
// with key_images, public_keys, payments_id etc.
2016-04-28 07:03:16 +00:00
vector<pair<string, vector<string>>> all_possible_tx_hashes;
//@TODO make lmdb2 path to some option
xmreg::MyLMDB mylmdb {"/home/mwo/.bitmonero/lmdb2"};
// search the custum lmdb for key_images and append the result
// to those from the mempool search if found
mylmdb.search(search_text,
tx_search_results["key_images"],
"key_images");
cout << "size: " << tx_search_results["key_images"].size() << endl;
all_possible_tx_hashes.push_back(
make_pair("key_images",
tx_search_results["key_images"]));
// search the custum lmdb for tx_public_keys and append the result
// to those from the mempool search if found
mylmdb.search(search_text,
tx_search_results["tx_public_keys"],
"tx_public_keys");
all_possible_tx_hashes.push_back(
make_pair("tx_public_keys",
tx_search_results["tx_public_keys"]));
// search the custum lmdb for payments_id and append the result
// to those from the mempool search if found
mylmdb.search(search_text,
tx_search_results["payments_id"],
"payments_id");
all_possible_tx_hashes.push_back(
make_pair("payments_id",
tx_search_results["payments_id"]));
// search the custum lmdb for encrypted_payments_id and append the result
// to those from the mempool search if found
mylmdb.search(search_text,
tx_search_results["encrypted_payments_id"],
"encrypted_payments_id");
all_possible_tx_hashes.push_back(
make_pair("encrypted_payments_id",
tx_search_results["encrypted_payments_id"]));
// search the custum lmdb for output_public_keys and append the result
// to those from the mempool search if found
mylmdb.search(search_text,
tx_search_results["output_public_keys"],
"output_public_keys");
all_possible_tx_hashes.push_back(
make_pair("output_public_keys",
tx_search_results["output_public_keys"]));
result_html = show_search_results(search_text, all_possible_tx_hashes);
2016-04-28 07:03:16 +00:00
2016-04-24 03:49:42 +00:00
return result_html;
2016-04-24 03:42:49 +00:00
}
map<string, vector<string>>
search_txs(vector<transaction> txs, const string& search_text)
{
map<string, vector<string>> tx_hashes;
// initlizte the map with empty results
tx_hashes["key_images"] = {};
tx_hashes["tx_public_keys"] = {};
tx_hashes["payments_id"] = {};
tx_hashes["encrypted_payments_id"] = {};
tx_hashes["output_public_keys"] = {};
for (const transaction& tx: txs)
{
tx_details txd = get_tx_details(tx);
string tx_hash_str = pod_to_hex(txd.hash);
// check if any key_image matches the search_text
vector<txin_to_key>::iterator it1 =
find_if(begin(txd.input_key_imgs), end(txd.input_key_imgs),
[&](const txin_to_key& key_img)
{
return pod_to_hex(key_img.k_image) == search_text;
});
if (it1 != txd.input_key_imgs.end())
{
tx_hashes["key_images"].push_back(tx_hash_str);
}
// check if tx_public_key matches the search_text
if (pod_to_hex(txd.pk) == search_text)
{
tx_hashes["tx_public_keys"].push_back(tx_hash_str);
}
// check if payments_id matches the search_text
if (pod_to_hex(txd.payment_id) == search_text)
{
tx_hashes["payment_id"].push_back(tx_hash_str);
}
// check if encrypted_payments_id matches the search_text
if (pod_to_hex(txd.payment_id8) == search_text)
{
tx_hashes["encrypted_payments_id"].push_back(tx_hash_str);
}
// check if output_public_keys matche the search_text
vector<pair<txout_to_key, uint64_t>>::iterator it2 =
find_if(begin(txd.output_pub_keys), end(txd.output_pub_keys),
[&](const pair<txout_to_key, uint64_t>& tx_out_pk)
{
return pod_to_hex(tx_out_pk.first.key) == search_text;
});
if (it2 != txd.output_pub_keys.end())
{
tx_hashes["output_public_keys"].push_back(tx_hash_str);
}
}
return tx_hashes;
}
2016-05-02 06:09:41 +00:00
vector<transaction>
get_mempool_txs()
{
// get mempool data using rpc call
vector<pair<tx_info, transaction>> mempool_data = search_mempool();
// output only transactions
vector<transaction> mempool_txs;
mempool_txs.reserve(mempool_data.size());
for (const auto& a_pair: mempool_data)
{
mempool_txs.push_back(a_pair.second);
}
return mempool_txs;
}
2016-04-28 07:03:16 +00:00
string
show_search_results(const string& search_text,
2016-04-28 07:03:16 +00:00
const vector<pair<string, vector<string>>>& all_possible_tx_hashes)
{
// initalise page tempate map with basic info about blockchain
mstch::map context {
2016-05-02 06:09:41 +00:00
{"search_text" , search_text},
{"no_results" , true},
2016-05-01 05:28:09 +00:00
{"to_many_results", false}
2016-04-28 07:03:16 +00:00
};
for (const pair<string, vector<string>>& found_txs: all_possible_tx_hashes)
{
// define flag, e.g., has_key_images denoting that
// tx hashes for key_image searched were found
context.insert({"has_" + found_txs.first, !found_txs.second.empty()});
// insert new array based on what we found to context if not exist
pair< mstch::map::iterator, bool> res
= context.insert({found_txs.first, mstch::array{}});
if (!found_txs.second.empty())
2016-05-01 05:28:09 +00:00
{
uint64_t tx_i {0};
2016-04-30 01:35:48 +00:00
// for each found tx_hash, get the corresponding tx
// and its details, and put into mstch for rendering
for (const string& tx_hash: found_txs.second)
{
2016-05-07 09:32:28 +00:00
crypto::hash tx_hash_pod;
epee::string_tools::hex_to_pod(tx_hash, tx_hash_pod);
2016-04-30 01:35:48 +00:00
transaction tx;
2016-05-07 09:32:28 +00:00
uint64_t blk_height {0};
int64_t blk_timestamp;
// first check in the blockchain
if (mcore->get_tx(tx_hash, tx))
2016-04-30 01:35:48 +00:00
{
2016-05-07 09:32:28 +00:00
// get timestamp of the tx's block
blk_height = core_storage
->get_db().get_tx_block_height(tx_hash_pod);
blk_timestamp = core_storage
->get_db().get_block_timestamp(blk_height);
}
else
{
// check in mempool if tx_hash not found in the
// blockchain
vector<pair<tx_info, transaction>> found_txs
= search_mempool(tx_hash_pod);
if (!found_txs.empty())
{
// there should be only one tx found
tx = found_txs.at(0).second;
}
else
{
return string("Cant get tx of hash (show_search_results): " + tx_hash);
}
// tx in mempool have no blk_timestamp
// but can use their recive time
blk_timestamp = found_txs.at(0).first.receive_time;
2016-04-30 01:35:48 +00:00
}
tx_details txd = get_tx_details(tx);
2016-05-01 08:07:49 +00:00
mstch::map txd_map = txd.get_mstch_map();
// add the timestamp to tx mstch map
txd_map.insert({"timestamp", xmreg::timestamp_to_str(blk_timestamp)});
boost::get<mstch::array>((res.first)->second).push_back(txd_map);
2016-05-01 05:28:09 +00:00
// dont show more than 500 results
if (tx_i > 500)
{
context["to_many_results"] = true;
break;
}
++tx_i;
}
2016-04-30 01:35:48 +00:00
// if found something, set this flag to indicate this fact
context["no_results"] = false;
}
}
2016-04-28 07:03:16 +00:00
// read search_results.html
string search_results_html = xmreg::read(TMPL_SEARCH_RESULTS);
2016-04-28 07:03:16 +00:00
2016-05-02 06:09:41 +00:00
// add header and footer
string full_page = get_full_page(search_results_html);
2016-04-28 07:03:16 +00:00
2016-04-30 01:35:48 +00:00
// read partial for showing details of tx(s) found
map<string, string> partials {
{"tx_table_head", xmreg::read(string(TMPL_PARIALS_DIR) + "/tx_table_header.html")},
{"tx_table_row" , xmreg::read(string(TMPL_PARIALS_DIR) + "/tx_table_row.html")}
};
2016-04-28 07:03:16 +00:00
// render the page
2016-04-30 01:35:48 +00:00
return mstch::render(full_page, context, partials);
2016-04-28 07:03:16 +00:00
}
2016-04-15 23:07:47 +00:00
2016-04-08 06:54:04 +00:00
private:
2016-04-12 03:31:26 +00:00
2016-05-02 06:09:41 +00:00
map<string, vector<string>>
get_tx_keys(vector<tx_info> tx_infos)
{
map<string, vector<string>> tx_keys;
for (const tx_info& tx_info: tx_infos)
{
}
return tx_keys;
}
2016-04-17 01:44:24 +00:00
tx_details
2016-04-17 05:09:46 +00:00
get_tx_details(const transaction& tx, bool coinbase = false)
2016-04-17 01:44:24 +00:00
{
tx_details txd;
// get tx hash
txd.hash = get_transaction_hash(tx);
// get tx public key
txd.pk = get_tx_pub_key_from_extra(tx);
// sum xmr in inputs and ouputs in the given tx
txd.xmr_inputs = sum_money_in_inputs(tx);
txd.xmr_outputs = sum_money_in_outputs(tx);
// get mixin number
txd.mixin_no = get_mixin_no(tx);
2016-04-17 01:44:24 +00:00
2016-05-06 01:00:22 +00:00
if (!coinbase && tx.vin.size() > 1)
2016-04-17 05:09:46 +00:00
{
// get tx fee
txd.fee = get_tx_fee(tx);
}
2016-04-17 01:44:24 +00:00
2016-04-25 03:54:26 +00:00
get_payment_id(tx, txd.payment_id, txd.payment_id8);
2016-04-17 01:44:24 +00:00
// get tx size in bytes
txd.size = get_object_blobsize(tx);
2016-04-19 07:13:16 +00:00
txd.input_key_imgs = get_key_images(tx);
txd.output_pub_keys = get_ouputs(tx);
2016-04-30 12:16:44 +00:00
txd.extra = tx.extra;
2016-04-23 06:14:22 +00:00
// get tx signatures for each input
txd.signatures = tx.signatures;
2016-04-19 07:13:16 +00:00
2016-04-17 01:44:24 +00:00
// get tx version
txd.version = tx.version;
// get unlock time
txd.unlock_time = tx.unlock_time;
return txd;
}
2016-04-24 01:11:08 +00:00
vector<pair<tx_info, transaction>>
2016-05-02 06:09:41 +00:00
search_mempool(crypto::hash tx_hash = null_hash)
2016-04-24 01:11:08 +00:00
{
vector<pair<tx_info, transaction>> found_txs;
// get txs in the mempool
std::vector<tx_info> mempool_txs;
if (!rpc.get_mempool(mempool_txs))
{
cerr << "Getting mempool failed " << endl;
return found_txs;
}
// if we have tx blob disply more.
// this info can also be obtained from json that is
// normally returned by the RCP call (see below in detailed view)
if (HAVE_TX_BLOB)
{
// get tx_blob if exists
//string tx_blob = get_tx_blob(_tx_info);
for (size_t i = 0; i < mempool_txs.size(); ++i)
{
// get transaction info of the tx in the mempool
tx_info _tx_info = mempool_txs.at(i);
// get tx_blob if exists
string tx_blob = get_tx_blob(_tx_info);
if (tx_blob.empty())
{
cerr << "tx_blob is empty. Probably its not a custom deamon." << endl;
continue;
}
// pare tx_blob into tx class
transaction tx;
if (!parse_and_validate_tx_from_blob(
tx_blob, tx))
{
2016-04-24 03:42:49 +00:00
cerr << "Cant get tx from blob" << endl;
2016-04-24 01:11:08 +00:00
continue;
}
2016-05-02 06:09:41 +00:00
// if we dont provide tx_hash, just get all txs in
// the mempool
if (tx_hash != null_hash)
2016-04-24 01:11:08 +00:00
{
2016-05-02 06:09:41 +00:00
// check if tx hash matches, and if yes, save it for return
if (tx_hash == get_transaction_hash(tx))
{
found_txs.push_back(make_pair(_tx_info, tx));
break;
}
2016-04-24 01:11:08 +00:00
}
2016-05-02 06:09:41 +00:00
else
{
found_txs.push_back(make_pair(_tx_info, tx));
}
2016-04-24 01:11:08 +00:00
}
}
return found_txs;
}
2016-04-16 03:44:58 +00:00
pair<string, string>
2016-04-22 02:10:30 +00:00
get_age(uint64_t timestamp1, uint64_t timestamp2, bool full_format = 0)
2016-04-16 03:44:58 +00:00
{
pair<string, string> age_pair;
// calculate difference between server and block timestamps
array<size_t, 5> delta_time = timestamp_difference(
timestamp1, timestamp2);
// default format for age
string age_str = fmt::format("{:02d}:{:02d}:{:02d}",
delta_time[2], delta_time[3],
delta_time[4]);
string age_format {"[h:m:s]"};
// if have days or years, change age format
2016-04-22 02:10:30 +00:00
if (delta_time[0] > 0 || full_format == true)
2016-04-16 03:44:58 +00:00
{
2016-04-22 02:13:00 +00:00
age_str = fmt::format("{:02d}:{:03d}:{:02d}:{:02d}:{:02d}",
2016-04-16 03:44:58 +00:00
delta_time[0], delta_time[1], delta_time[2],
delta_time[3], delta_time[4]);
age_format = string("[y:d:h:m:s]");
}
else if (delta_time[1] > 0)
{
age_str = fmt::format("{:02d}:{:02d}:{:02d}:{:02d}",
delta_time[1], delta_time[2],
delta_time[3], delta_time[4]);
age_format = string("[d:h:m:s]");
}
age_pair.first = age_str;
age_pair.second = age_format;
return age_pair;
}
2016-05-01 03:43:48 +00:00
pair<uint64_t, uint64_t>
2016-04-12 22:20:14 +00:00
sum_xmr_outputs(const string& json_str)
{
2016-05-01 03:43:48 +00:00
pair<uint64_t, uint64_t> sum_xmr {0, 0};
2016-04-12 22:20:14 +00:00
rapidjson::Document json;
if (json.Parse(json_str.c_str()).HasParseError())
{
cerr << "Failed to parse JSON" << endl;
2016-05-01 03:43:48 +00:00
return sum_xmr;
2016-04-12 22:20:14 +00:00
}
// get information about outputs
const rapidjson::Value& vout = json["vout"];
if (vout.IsArray())
{
for (rapidjson::SizeType i = 0; i < vout.Size(); ++i)
{
//print(" - {:s}, {:0.8f} xmr\n",
2016-04-14 07:58:01 +00:00
// vout[i]["target"]["key"].GetString(),
// XMR_AMOUNT(vout[i]["amount"].GetUint64()));
2016-04-12 22:20:14 +00:00
2016-05-01 03:43:48 +00:00
sum_xmr.first += vout[i]["amount"].GetUint64();
2016-04-12 22:20:14 +00:00
}
2016-05-01 03:43:48 +00:00
sum_xmr.second = vout.Size();
2016-04-12 22:20:14 +00:00
}
return sum_xmr;
}
2016-05-01 03:43:48 +00:00
pair<uint64_t, uint64_t>
2016-04-13 00:10:21 +00:00
sum_xmr_inputs(const string& json_str)
{
2016-05-01 03:43:48 +00:00
pair<uint64_t, uint64_t> sum_xmr {0, 0};
2016-04-13 00:10:21 +00:00
rapidjson::Document json;
if (json.Parse(json_str.c_str()).HasParseError())
{
cerr << "Failed to parse JSON" << endl;
2016-05-01 03:43:48 +00:00
return sum_xmr;
2016-04-13 00:10:21 +00:00
}
// get information about inputs
const rapidjson::Value& vin = json["vin"];
if (vin.IsArray())
{
// print("Input key images:\n");
for (rapidjson::SizeType i = 0; i < vin.Size(); ++i)
{
if (vin[i].HasMember("key"))
{
const rapidjson::Value& key_img = vin[i]["key"];
// print(" - {:s}, {:0.8f} xmr\n",
// key_img["k_image"].GetString(),
// XMR_AMOUNT(key_img["amount"].GetUint64()));
2016-05-01 03:43:48 +00:00
sum_xmr.first += key_img["amount"].GetUint64();
2016-04-13 00:10:21 +00:00
}
}
2016-05-01 03:43:48 +00:00
sum_xmr.second = vin.Size();
2016-04-13 00:10:21 +00:00
}
return sum_xmr;
}
2016-04-14 07:58:01 +00:00
2016-04-12 22:36:27 +00:00
vector<uint64_t>
get_mixin_no_in_txs(const string& json_str)
{
vector<uint64_t> mixin_no;
rapidjson::Document json;
if (json.Parse(json_str.c_str()).HasParseError())
{
cerr << "Failed to parse JSON" << endl;
return mixin_no;
}
// get information about inputs
const rapidjson::Value& vin = json["vin"];
if (vin.IsArray())
{
// print("Input key images:\n");
for (rapidjson::SizeType i = 0; i < vin.Size(); ++i)
{
if (vin[i].HasMember("key"))
{
const rapidjson::Value& key_img = vin[i]["key"];
// print(" - {:s}, {:0.8f} xmr\n",
// key_img["k_image"].GetString(),
// XMR_AMOUNT(key_img["amount"].GetUint64()));
mixin_no.push_back(key_img["key_offsets"].Size());
}
}
}
return mixin_no;
}
2016-04-08 09:32:12 +00:00
string
get_full_page(string& middle)
{
2016-04-12 03:31:26 +00:00
return xmreg::read(TMPL_HEADER)
2016-04-08 09:32:12 +00:00
+ middle
2016-04-12 03:31:26 +00:00
+ xmreg::read(TMPL_FOOTER);
2016-04-08 09:32:12 +00:00
}
2016-04-08 06:54:04 +00:00
};
}
#endif //CROWXMR_PAGE_H