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

688 lines
24 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-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"
2016-04-08 06:54:04 +00:00
2016-04-12 03:31:26 +00:00
#include <algorithm>
#include<ctime>
2016-04-12 07:15:33 +00:00
#define TMPL_DIR "./templates"
#define TMPL_INDEX TMPL_DIR "/index.html"
#define TMPL_MEMPOOL TMPL_DIR "/mempool.html"
#define TMPL_HEADER TMPL_DIR "/header.html"
#define TMPL_FOOTER TMPL_DIR "/footer.html"
2016-04-15 23:07:47 +00:00
#define TMPL_BLOCK TMPL_DIR "/block.html"
2016-04-12 03:31:26 +00:00
2016-04-08 06:54:04 +00:00
namespace xmreg {
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-12 07:15:33 +00:00
using request = cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::request;
using response = cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::response;
using http_simple_client = epee::net_utils::http::http_simple_client;
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;
size_t version;
uint64_t unlock_time;
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));
return mstch::map {
{"hash" , tx_hash_str},
{"pub_key" , tx_pk_str},
{"tx_fee" , fmt::format("{:0.4f}", XMR_AMOUNT(fee))},
{"sum_inputs" , fmt::format("{:0.4f}", XMR_AMOUNT(xmr_inputs))},
{"sum_outputs" , fmt::format("{:0.4f}", XMR_AMOUNT(xmr_outputs))},
{"mixin" , std::to_string(mixin_no - 1)},
{"version" , std::to_string(version)},
{"unlock_time" , std::to_string(unlock_time)},
{"tx_size" , fmt::format("{:0.4f}", static_cast<double>(size)/1024.0)}
};
}
};
2016-04-08 06:54:04 +00:00
class page {
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-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-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-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-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-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-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-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-15 02:59:41 +00:00
//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-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-04-14 07:58:01 +00:00
uint64_t sum_inputs = sum_xmr_inputs(_tx_info.tx_json);
2016-04-12 22:36:27 +00:00
uint64_t sum_outputs = sum_xmr_outputs(_tx_info.tx_json);
// 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-13 07:00:07 +00:00
{"fee" , fmt::format("{:0.4f}", XMR_AMOUNT(_tx_info.fee))},
{"xmr_inputs" , fmt::format("{:0.2f}", XMR_AMOUNT(sum_inputs))},
{"xmr_outputs" , fmt::format("{:0.2f}", XMR_AMOUNT(sum_outputs))},
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;
return fmt::format("Block of height {:d} not found!", _blk_height);
}
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-16 04:11:17 +00:00
bool have_txs = !blk.tx_hashes.empty();
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},
{"blk_height" , fmt::format("{:d}", _blk_height)},
{"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},
{"blk_nonce" , std::to_string(blk.nonce)},
{"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 01:44:24 +00:00
static_cast<double>(blk_size) / 1024.0)},
{"blk_txs" , mstch::array()}
2016-04-15 23:07:47 +00:00
};
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"]);
// for each transaction in the memory pool
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);
// add tx details mstch map to context
txs.push_back(txd.get_mstch_map());
}
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);
}
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);
}
2016-04-08 06:54:04 +00:00
private:
2016-04-12 03:31:26 +00:00
2016-04-17 01:44:24 +00:00
tx_details
get_tx_details(const transaction& tx)
{
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);
// get tx fee
txd.fee = get_tx_fee(tx);
// get tx size in bytes
txd.size = get_object_blobsize(tx);
// get tx version
txd.version = tx.version;
// get unlock time
txd.unlock_time = tx.unlock_time;
return txd;
}
2016-04-16 03:44:58 +00:00
pair<string, string>
get_age(uint64_t timestamp1, uint64_t timestamp2)
{
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
if (delta_time[0] > 0)
{
age_str = fmt::format("{:02d}:{:02d}:{:02d}:{:02d}:{:02d}",
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-04-12 22:20:14 +00:00
uint64_t
sum_xmr_outputs(const string& json_str)
{
2016-04-13 00:10:21 +00:00
uint64_t sum_xmr {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;
return 0;
}
// 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
sum_xmr += vout[i]["amount"].GetUint64();
}
}
return sum_xmr;
}
2016-04-13 00:10:21 +00:00
uint64_t
sum_xmr_inputs(const string& json_str)
{
uint64_t sum_xmr {0};
rapidjson::Document json;
if (json.Parse(json_str.c_str()).HasParseError())
{
cerr << "Failed to parse JSON" << endl;
return 0;
}
// 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()));
sum_xmr += key_img["amount"].GetUint64();
}
}
}
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