Emission atomic structure added

This commit is contained in:
moneroexamples 2017-05-17 13:30:36 +08:00
parent 7a4a02b563
commit 9c57d8b3a3
6 changed files with 112 additions and 77 deletions

View File

@ -113,6 +113,7 @@ set(LIBRARIES
unbound unbound
curl curl
crypto crypto
atomic
ssl) ssl)
if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT WIN32) if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT WIN32)

View File

@ -26,7 +26,18 @@ struct jsonresponse: crow::response
}; };
} }
class ExitHandler
{
public:
void exit() { exitHandler(0); }
static void exitHandler(int) { s_shouldExit = true; }
bool shouldExit() const { return s_shouldExit; }
private:
static bool s_shouldExit;
};
bool ExitHandler::s_shouldExit = false;
int main(int ac, const char* av[]) { int main(int ac, const char* av[]) {
@ -60,6 +71,8 @@ int main(int ac, const char* av[]) {
auto enable_tx_cache_opt = opts.get_option<bool>("enable-tx-cache"); auto enable_tx_cache_opt = opts.get_option<bool>("enable-tx-cache");
auto enable_block_cache_opt = opts.get_option<bool>("enable-block-cache"); auto enable_block_cache_opt = opts.get_option<bool>("enable-block-cache");
auto show_cache_times_opt = opts.get_option<bool>("show-cache-times"); auto show_cache_times_opt = opts.get_option<bool>("show-cache-times");
auto enable_emission_monitor_opt = opts.get_option<bool>("enable-emission-monitor");
bool testnet {*testnet_opt}; bool testnet {*testnet_opt};
bool enable_pusher {*enable_pusher_opt}; bool enable_pusher {*enable_pusher_opt};
@ -71,6 +84,7 @@ int main(int ac, const char* av[]) {
bool enable_json_api {*enable_json_api_opt}; bool enable_json_api {*enable_json_api_opt};
bool enable_tx_cache {*enable_tx_cache_opt}; bool enable_tx_cache {*enable_tx_cache_opt};
bool enable_block_cache {*enable_block_cache_opt}; bool enable_block_cache {*enable_block_cache_opt};
bool enable_emission_monitor {*enable_emission_monitor_opt};
bool show_cache_times {*show_cache_times_opt}; bool show_cache_times {*show_cache_times_opt};
@ -149,24 +163,41 @@ int main(int ac, const char* av[]) {
deamon_url = "http:://127.0.0.1:28081"; deamon_url = "http:://127.0.0.1:28081";
} }
xmreg::CurrentBlockchainStatus::blockchain_path
= blockchain_path.string();
xmreg::CurrentBlockchainStatus::testnet
= testnet;
xmreg::CurrentBlockchainStatus::deamon_url
= deamon_url;
if (!xmreg::CurrentBlockchainStatus::init_monero_blockchain()) if (enable_emission_monitor == true)
{ {
cerr << "Error accessing blockchain from CurrentBlockchainStatus." << endl; // This starts new thread, which aim is
return EXIT_FAILURE; // to calculate, store and monitor
} // current total Monero emission amount.
// launch the status monitoring thread so that it keeps track of blockchain // This thread stores the current emission
// info, e.g., current height. Information from this thread is used // which it has caluclated in
// by tx searching threads that are launched for each user independently, // <blockchain_path>/emission_amount.txt file,
// when they log back or create new account. // e.g., ~/.bitmonero/lmdb/emission_amount.txt.
xmreg::CurrentBlockchainStatus::start_monitor_blockchain_thread(); // So instead of calcualting the emission
// from scrach whenever the explorer is started,
// the thread is initalized with the values
// found in emission_amount.txt file.
xmreg::CurrentBlockchainStatus::blockchain_path
= blockchain_path.string();
xmreg::CurrentBlockchainStatus::testnet
= testnet;
xmreg::CurrentBlockchainStatus::deamon_url
= deamon_url;
if (!xmreg::CurrentBlockchainStatus::init_monero_blockchain())
{
cerr << "Error accessing blockchain from CurrentBlockchainStatus." << 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 page class which // create instance of page class which
// contains logic for the website // contains logic for the website

View File

@ -45,6 +45,8 @@ namespace xmreg
"enable caching of block details") "enable caching of block details")
("enable-autorefresh-option", value<bool>()->default_value(false)->implicit_value(true), ("enable-autorefresh-option", value<bool>()->default_value(false)->implicit_value(true),
"enable users to have the index page on autorefresh") "enable users to have the index page on autorefresh")
("enable-emission-monitor", value<bool>()->default_value(false)->implicit_value(true),
"enable Monero total emission monitoring thread")
("port,p", value<string>()->default_value("8081"), ("port,p", value<string>()->default_value("8081"),
"default port") "default port")
("testnet-url", value<string>()->default_value(""), ("testnet-url", value<string>()->default_value(""),

View File

@ -38,6 +38,7 @@ CurrentBlockchainStatus::init_monero_blockchain()
void void
CurrentBlockchainStatus::start_monitor_blockchain_thread() CurrentBlockchainStatus::start_monitor_blockchain_thread()
{ {
total_emission_atomic = Emission {0, 0, 0};
string emmision_saved_file = get_output_file_path(); string emmision_saved_file = get_output_file_path();
@ -64,13 +65,15 @@ CurrentBlockchainStatus::start_monitor_blockchain_thread()
{ {
while (true) while (true)
{ {
Emission current_emission = total_emission_atomic;
current_height = core_storage->get_current_blockchain_height(); current_height = core_storage->get_current_blockchain_height();
update_current_emission_amount(); update_current_emission_amount();
save_current_emission_amount(); save_current_emission_amount();
if (searched_blk_no < current_height - blockchain_chunk_size) if (current_emission.blk_no < current_height - blockchain_chunk_size)
{ {
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
} }
@ -90,9 +93,9 @@ void
CurrentBlockchainStatus::update_current_emission_amount() CurrentBlockchainStatus::update_current_emission_amount()
{ {
cout << "updating emission rate: " << current_height << endl; Emission current_emission = total_emission_atomic;
uint64_t blk_no = searched_blk_no; uint64_t blk_no = current_emission.blk_no;
uint64_t end_block = blk_no + blockchain_chunk_size; uint64_t end_block = blk_no + blockchain_chunk_size;
@ -100,8 +103,8 @@ CurrentBlockchainStatus::update_current_emission_amount()
end_block = end_block > current_blockchain_height ? current_blockchain_height : end_block; end_block = end_block > current_blockchain_height ? current_blockchain_height : end_block;
uint64_t current_emission_amount {0}; uint64_t temp_emission_amount {0};
uint64_t current_fee_amount {0}; uint64_t temp_fee_amount {0};
while (blk_no < end_block) while (blk_no < end_block)
{ {
@ -124,20 +127,19 @@ CurrentBlockchainStatus::update_current_emission_amount()
tx_fee_amount += get_tx_fee(tx); tx_fee_amount += get_tx_fee(tx);
} }
current_emission_amount += coinbase_amount - tx_fee_amount; temp_emission_amount += coinbase_amount - tx_fee_amount;
current_fee_amount += tx_fee_amount; temp_fee_amount += tx_fee_amount;
++blk_no; ++blk_no;
} }
searched_blk_no = blk_no; current_emission.coinbase += temp_emission_amount;
total_emission_amount += current_emission_amount; current_emission.fee += temp_fee_amount;
total_fee_amount += current_fee_amount; current_emission.blk_no = blk_no;
cout << "blk: " << blk_no total_emission_atomic = current_emission;
<< " total_emission_amount: " << xmr_amount_to_str(uint64_t(total_emission_amount))
<< " total_fee_amount: " << xmr_amount_to_str(uint64_t(total_fee_amount)) cout << "total emission: " << string(current_emission) << endl;
<< endl;
} }
@ -156,16 +158,9 @@ CurrentBlockchainStatus::save_current_emission_amount()
return false; return false;
} }
uint64_t check_sum = uint64_t(searched_blk_no) Emission current_emission = total_emission_atomic;
+ uint64_t(total_emission_amount)
+ uint64_t(total_fee_amount);
string out_line = to_string(searched_blk_no) out << string(current_emission) << flush;
+ "," + to_string(total_emission_amount)
+ "," + to_string(total_fee_amount)
+ "," + to_string(check_sum);
out << out_line << flush;
return true; return true;
} }
@ -195,14 +190,16 @@ CurrentBlockchainStatus::load_current_emission_amount()
return false; return false;
} }
uint64_t read_check_sum{0}; Emission emission_loaded {0, 0, 0};
uint64_t read_check_sum {0};
try try
{ {
searched_blk_no = boost::lexical_cast<uint64_t>(strs.at(0)); emission_loaded.blk_no = boost::lexical_cast<uint64_t>(strs.at(0));
total_emission_amount = boost::lexical_cast<uint64_t>(strs.at(1)); emission_loaded.coinbase = boost::lexical_cast<uint64_t>(strs.at(1));
total_fee_amount = boost::lexical_cast<uint64_t>(strs.at(2)); emission_loaded.fee = boost::lexical_cast<uint64_t>(strs.at(2));
read_check_sum = boost::lexical_cast<uint64_t>(strs.at(3)); read_check_sum = boost::lexical_cast<uint64_t>(strs.at(3));
} }
catch (boost::bad_lexical_cast &e) catch (boost::bad_lexical_cast &e)
{ {
@ -210,19 +207,17 @@ CurrentBlockchainStatus::load_current_emission_amount()
return false; return false;
} }
uint64_t check_sum = uint64_t(searched_blk_no) if (read_check_sum != emission_loaded.checksum())
+ uint64_t(total_emission_amount)
+ uint64_t(total_fee_amount);
if (read_check_sum != check_sum)
{ {
cerr << "read_check_sum != check_sum: " cerr << "read_check_sum != check_sum: "
<< read_check_sum << " != " << check_sum << read_check_sum << " != " << emission_loaded.checksum()
<< endl; << endl;
return false; return false;
} }
total_emission_atomic = emission_loaded;
return true; return true;
} }
@ -236,16 +231,10 @@ CurrentBlockchainStatus::get_output_file_path()
} }
vector<uint64_t> CurrentBlockchainStatus::Emission
CurrentBlockchainStatus::get_emission_amount() CurrentBlockchainStatus::get_emission()
{ {
return total_emission_atomic.load();
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 bool
@ -266,11 +255,7 @@ uint64_t CurrentBlockchainStatus::blockchain_chunk_size {10000};
atomic<uint64_t> CurrentBlockchainStatus::current_height {0}; atomic<uint64_t> CurrentBlockchainStatus::current_height {0};
atomic<uint64_t> CurrentBlockchainStatus::total_emission_amount {0} ; atomic<CurrentBlockchainStatus::Emission> CurrentBlockchainStatus::total_emission_atomic;
atomic<uint64_t> CurrentBlockchainStatus::total_fee_amount {0} ;
atomic<uint64_t> CurrentBlockchainStatus::searched_blk_no {0};
std::thread CurrentBlockchainStatus::m_thread; std::thread CurrentBlockchainStatus::m_thread;

View File

@ -23,6 +23,26 @@ using namespace std;
struct CurrentBlockchainStatus struct CurrentBlockchainStatus
{ {
struct Emission
{
uint64_t coinbase;
uint64_t fee;
uint64_t blk_no;
inline uint64_t
checksum() const
{
return coinbase + fee + blk_no;
}
operator
std::string() const
{
return to_string(blk_no) + "," + to_string(coinbase)
+ "," + to_string(fee) + "," + to_string(checksum());
}
};
static string blockchain_path; static string blockchain_path;
static bool testnet; static bool testnet;
@ -35,11 +55,7 @@ struct CurrentBlockchainStatus
static atomic<uint64_t> current_height; static atomic<uint64_t> current_height;
static atomic<uint64_t> total_emission_amount; static atomic<Emission> total_emission_atomic;
static atomic<uint64_t> total_fee_amount;
static atomic<uint64_t> searched_blk_no;
static std::thread m_thread; static std::thread m_thread;
@ -65,8 +81,8 @@ struct CurrentBlockchainStatus
static bool static bool
load_current_emission_amount(); load_current_emission_amount();
static vector<uint64_t> static Emission
get_emission_amount(); get_emission();
static string static string
get_output_file_path(); get_output_file_path();

View File

@ -761,17 +761,17 @@ namespace xmreg
if (CurrentBlockchainStatus::is_thread_running()) if (CurrentBlockchainStatus::is_thread_running())
{ {
vector<uint64_t> emission_values CurrentBlockchainStatus::Emission current_values
= CurrentBlockchainStatus::get_emission_amount(); = CurrentBlockchainStatus::get_emission();
string emission_blk_no = std::to_string(emission_values.at(0) - 1); string emission_blk_no = std::to_string(current_values.blk_no);
string emission_amount = xmr_amount_to_str(emission_values.at(1), "{:0.3f}"); string emission_coinbase = xmr_amount_to_str(current_values.coinbase, "{:0.3f}");
string emission_fee_amount = xmr_amount_to_str(emission_values.at(2), "{:0.3f}"); string emission_fee = xmr_amount_to_str(current_values.fee, "{:0.3f}");
context["emission"] = mstch::map { context["emission"] = mstch::map {
{"blk_no" , emission_blk_no}, {"blk_no" , emission_blk_no},
{"amount" , emission_amount}, {"amount" , emission_coinbase},
{"fee_amount", emission_fee_amount} {"fee_amount", emission_fee}
}; };
} }
else else