testing monero emission thread

This commit is contained in:
moneroexamples 2017-05-16 13:29:24 +08:00
parent f131bcfc9b
commit d6bcf94a3d
6 changed files with 378 additions and 8 deletions

View File

@ -128,6 +128,19 @@ int main(int ac, const char* av[]) {
cout << blockchain_path << endl;
if (!xmreg::CurrentBlockchainStatus::init_monero_blockchain())
{
cerr << "Error accessing blockchain." << endl;
return EXIT_FAILURE;
}
// launch the status monitoring thread so that it keeps track of blockchain
// info, e.g., current height. Information from this thread is used
// by tx searching threads that are launched for each user independently,
// when they log back or create new account.
xmreg::CurrentBlockchainStatus::start_monitor_blockchain_thread();
// create instance of our MicroCore
// and make pointer to the Blockchain
xmreg::MicroCore mcore;

View File

@ -6,7 +6,7 @@ set(SOURCE_HEADERS
MicroCore.h
tools.h
monero_headers.h
)
CurrentBlockchainStatus.h)
set(SOURCE_FILES
MicroCore.cpp
@ -14,7 +14,7 @@ set(SOURCE_FILES
CmdLineOptions.cpp
page.h
rpccalls.cpp rpccalls.h
version.h.in)
version.h.in CurrentBlockchainStatus.cpp)
# make static library called libmyxrm
# that we are going to link to

View File

@ -0,0 +1,256 @@
//
// Created by mwo on 16/05/17.
//
#include "CurrentBlockchainStatus.h"
namespace xmreg
{
using namespace std;
bool
CurrentBlockchainStatus::init_monero_blockchain()
{
// set monero log output level
uint32_t log_level = 0;
mlog_configure(mlog_get_default_log_path(""), true);
mcore = unique_ptr<xmreg::MicroCore>(new xmreg::MicroCore{});
// initialize the core using the blockchain path
if (!mcore->init(blockchain_path))
{
cerr << "Error accessing blockchain." << endl;
return false;
}
// get the high level Blockchain object to interact
// with the blockchain lmdb database
core_storage = &(mcore->get_core());
return true;
}
void
CurrentBlockchainStatus::start_monitor_blockchain_thread()
{
string emmision_saved_file = blockchain_path + output_file;
if (boost::filesystem::exists(emmision_saved_file))
{
if (!load_current_emission_amount())
{
cerr << "Cant read or has incorrect format: " << emmision_saved_file << endl;
return;
}
}
if (!is_running)
{
m_thread = std::thread{[]()
{
while (true)
{
current_height = core_storage->get_current_blockchain_height();
update_current_emission_amount();
save_current_emission_amount();
if (searched_blk_no < current_height - 1000)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
else
{
std::this_thread::sleep_for(std::chrono::seconds(60));
}
}
}};
is_running = true;
}
}
void
CurrentBlockchainStatus::update_current_emission_amount()
{
cout << "updating emission rate: " << current_height << endl;
uint64_t no_of_blocks_to_search_at_once {1000};
uint64_t blk_no = searched_blk_no;
uint64_t end_block = blk_no + no_of_blocks_to_search_at_once;
uint64_t current_blockchain_height = current_height;
end_block = end_block > current_blockchain_height ? current_blockchain_height : end_block;
uint64_t current_emission_amount {0};
uint64_t current_fee_amount {0};
while (blk_no < end_block)
{
block blk;
mcore->get_block_by_height(blk_no, blk);
uint64_t coinbase_amount = get_outs_money_amount(blk.miner_tx);
std::list<transaction> txs;
std::list<crypto::hash> missed_txs;
uint64_t tx_fee_amount = 0;
core_storage->get_transactions(blk.tx_hashes, txs, missed_txs);
for(const auto& tx: txs)
{
tx_fee_amount += get_tx_fee(tx);
}
current_emission_amount += coinbase_amount - tx_fee_amount;
current_fee_amount += tx_fee_amount;
++blk_no;
}
searched_blk_no = blk_no;
total_emission_amount += current_emission_amount;
total_fee_amount += current_fee_amount;
cout << "blk: " << blk_no
<< " total_emission_amount: " << xmr_amount_to_str(uint64_t(total_emission_amount))
<< " total_fee_amount: " << xmr_amount_to_str(uint64_t(total_fee_amount))
<< endl;
}
bool
CurrentBlockchainStatus::save_current_emission_amount()
{
string emmision_saved_file = blockchain_path + output_file;
ofstream out(emmision_saved_file);
if( !out )
{
cerr << "Couldn't open file." << endl;
return false;
}
uint64_t check_sum = uint64_t(searched_blk_no)
+ uint64_t(total_emission_amount)
+ uint64_t(total_fee_amount);
string out_line = to_string(searched_blk_no)
+ "," + to_string(total_emission_amount)
+ "," + to_string(total_fee_amount)
+ "," + to_string(check_sum);
out << out_line << flush;
return true;
}
bool
CurrentBlockchainStatus::load_current_emission_amount()
{
string emmision_saved_file = blockchain_path + output_file;
string last_saved_emmision = xmreg::read(emmision_saved_file);
if (last_saved_emmision.empty()) {
cerr << "Couldn't open file." << endl;
return false;
}
vector<string> strs;
boost::split(strs, last_saved_emmision, boost::is_any_of(","));
if (strs.empty()) {
cerr << "Problem spliting string values form emission_amount." << endl;
return false;
}
uint64_t read_check_sum{0};
try {
searched_blk_no = boost::lexical_cast<uint64_t>(strs.at(0));
total_emission_amount = boost::lexical_cast<uint64_t>(strs.at(1));
total_fee_amount = boost::lexical_cast<uint64_t>(strs.at(2));
read_check_sum = boost::lexical_cast<uint64_t>(strs.at(3));
}
catch (boost::bad_lexical_cast &e) {
cerr << "Cant parse to number: " << last_saved_emmision << endl;
return false;
}
uint64_t check_sum = uint64_t(searched_blk_no)
+ uint64_t(total_emission_amount)
+ uint64_t(total_fee_amount);
if (read_check_sum != check_sum) {
cerr << "read_check_sum != check_sum: "
<< read_check_sum << " != " << check_sum
<< endl;
return false;
}
return true;
}
vector<uint64_t>
CurrentBlockchainStatus::get_emission_amount()
{
uint64_t searched_block = searched_blk_no;
uint64_t emission_amount = total_emission_amount;
uint64_t fee_amount = total_fee_amount;
return {searched_block, emission_amount, fee_amount};
}
bool
CurrentBlockchainStatus::is_thread_running()
{
return is_running;
}
string CurrentBlockchainStatus::blockchain_path{"/home/mwo/.bitmonero/lmdb"};
string CurrentBlockchainStatus::output_file {"/emission_amount.txt"};
string CurrentBlockchainStatus::deamon_url{"http:://127.0.0.1:18081"};
atomic<uint64_t> CurrentBlockchainStatus::current_height {0};
atomic<uint64_t> CurrentBlockchainStatus::total_emission_amount {0} ;
atomic<uint64_t> CurrentBlockchainStatus::total_fee_amount {0} ;
atomic<uint64_t> CurrentBlockchainStatus::searched_blk_no {0};
std::thread CurrentBlockchainStatus::m_thread;
atomic<bool> CurrentBlockchainStatus::is_running {false};
cryptonote::Blockchain* CurrentBlockchainStatus::core_storage;
unique_ptr<xmreg::MicroCore> CurrentBlockchainStatus::mcore;
}

View File

@ -0,0 +1,73 @@
//
// Created by mwo on 16/05/17.
//
#ifndef XMRBLOCKS_CURRENTBLOCKCHAINSTATUS_H
#define XMRBLOCKS_CURRENTBLOCKCHAINSTATUS_H
#include "MicroCore.h"
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <memory>
#include <thread>
#include <mutex>
#include <atomic>
namespace xmreg
{
using namespace std;
class CurrentBlockchainStatus
{
static string blockchain_path;
static string output_file;
static string deamon_url;
static atomic<uint64_t> current_height;
static atomic<uint64_t> total_emission_amount;
static atomic<uint64_t> total_fee_amount;
static atomic<uint64_t> searched_blk_no;
static std::thread m_thread;
static atomic<bool> is_running;
// make object for accessing the blockchain here
static unique_ptr<xmreg::MicroCore> mcore;
static cryptonote::Blockchain *core_storage;
public:
static void
start_monitor_blockchain_thread();
static bool
init_monero_blockchain();
static void
update_current_emission_amount();
static bool
save_current_emission_amount();
static bool
load_current_emission_amount();
static vector<uint64_t>
get_emission_amount();
static bool
is_thread_running();
};
}
#endif //XMRBLOCKS_CURRENTBLOCKCHAINSTATUS_H

View File

@ -17,6 +17,8 @@
#include "tools.h"
#include "rpccalls.h"
#include "CurrentBlockchainStatus.h"
#include "../ext/crow/http_request.h"
#include "../ext/vpetrigocaches/cache.hpp"
@ -746,9 +748,9 @@ namespace xmreg
}
context["network_info"] = mstch::map {
{"difficulty", j_network_info["difficulty"].get<uint64_t>()},
{"hash_rate" , difficulty},
{"fee_per_kb", xmreg::xmr_amount_to_str(j_network_info["fee_per_kb"], "{:0.12f}")}
{"difficulty" , j_network_info["difficulty"].get<uint64_t>()},
{"hash_rate" , difficulty},
{"fee_per_kb" , print_money(j_network_info["fee_per_kb"])}
};
}
}
@ -757,6 +759,27 @@ namespace xmreg
cerr << "network_info future not ready yet, skipping." << endl;
}
if (CurrentBlockchainStatus::is_thread_running())
{
vector<uint64_t> emission_values
= CurrentBlockchainStatus::get_emission_amount();
string emission_blk_no = std::to_string(emission_values.at(0) - 1);
string emission_amount = xmr_amount_to_str(emission_values.at(1), "{:0.3f}");
string emission_fee_amount = xmr_amount_to_str(emission_values.at(2), "{:0.3f}");
context["emission"] = mstch::map {
{"blk_no" , emission_blk_no},
{"amount" , emission_amount},
{"fee_amount", emission_fee_amount}
};
}
else
{
cerr << "emission thread not running, skipping." << endl;
}
// get memory pool rendered template
string mempool_html = mempool(false, no_of_mempool_tx_of_frontpage);

View File

@ -35,13 +35,18 @@
{{#network_info}}
<h3 style="font-size: 12px; margin-top: 10px">
<h3 style="font-size: 12px; margin-top: 5px; margin-bottom: 3">
Network difficulty: {{difficulty}}
| Hash rate: {{hash_rate}}
| Fee per kb: {{fee_per_kb}}
</h3>
{{/network_info}}
{{/network_info}}
{{#emission}}
<h3 style="font-size: 12px; margin-top: 2px">
Monero emission (fees) is {{amount}} ({{fee_amount}}) as of {{blk_no}} block
</h3>
{{/emission}}
</div>