From fac016b3b0f237b6f9b80dcea7a209355dfeed9d Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Wed, 14 Dec 2016 10:15:39 +0800 Subject: [PATCH] CurrentBlockchainStatus class moved to its own file --- src/CMakeLists.txt | 2 +- src/CurrentBlockchainStatus.h | 126 ++++++++++++++++++++++++++++++++++ src/TxSearch.h | 118 +------------------------------ 3 files changed, 128 insertions(+), 118 deletions(-) create mode 100644 src/CurrentBlockchainStatus.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5cb3c76..9675215 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,7 +5,7 @@ project(myxrm) set(SOURCE_FILES MicroCore.cpp tools.cpp - CmdLineOptions.cpp MySqlConnector.h TxSearch.h) + CmdLineOptions.cpp MySqlConnector.h TxSearch.h CurrentBlockchainStatus.h) # make static library called libmyxrm # that we are going to link to diff --git a/src/CurrentBlockchainStatus.h b/src/CurrentBlockchainStatus.h new file mode 100644 index 0000000..c7027a9 --- /dev/null +++ b/src/CurrentBlockchainStatus.h @@ -0,0 +1,126 @@ +// +// Created by mwo on 14/12/16. +// + +#ifndef RESTBED_XMR_CURRENTBLOCKCHAINSTATUS_H +#define RESTBED_XMR_CURRENTBLOCKCHAINSTATUS_H + +#include "mylmdb.h" +#include "tools.h" + +#include +#include +#include +#include +#include + + +namespace xmreg { + + +/* + * This is a thread class + */ +struct CurrentBlockchainStatus { + static string blockchain_path; + + static atomic current_height; + + static bool testnet; + + static std::thread m_thread; + + static bool is_running; + + static uint64_t refresh_block_status_every_seconds; + + // since this class monitors current status + // of the blockchain, its seems logical to + // make object for accessing the blockchain here + static xmreg::MicroCore mcore; + static cryptonote::Blockchain *core_storage; + + static + void start_monitor_blockchain_thread() { + if (!is_running) { + m_thread = std::thread{[]() { + while (true) { + current_height = get_current_blockchain_height(); + cout << "Check block height: " << current_height << endl; + std::this_thread::sleep_for(std::chrono::seconds(refresh_block_status_every_seconds)); + } + + }}; + + is_running = true; + } + } + + static inline + uint64_t + get_current_blockchain_height() { + return xmreg::MyLMDB::get_blockchain_height(blockchain_path) - 1; + } + + static void + set_blockchain_path(const string &path) { + blockchain_path = path; + } + + static void + set_testnet(bool is_testnet) { + testnet = is_testnet; + } + + static bool + init_monero_blockchain() { + // enable basic monero log output + xmreg::enable_monero_log(); + + // initialize mcore and core_storage + if (!xmreg::init_blockchain(blockchain_path, + mcore, core_storage)) { + cerr << "Error accessing blockchain." << endl; + return false; + } + + return true; + } + + static bool + get_block(uint64_t height, block &blk) { + return mcore.get_block_by_height(height, blk); + } + + static bool + get_block_txs(const block &blk, list &blk_txs) { + // get all transactions in the block found + // initialize the first list with transaction for solving + // the block i.e. coinbase tx. + blk_txs.push_back(blk.miner_tx); + + list missed_txs; + + if (!core_storage->get_transactions(blk.tx_hashes, blk_txs, missed_txs)) { + cerr << "Cant get transactions in block: " << get_block_hash(blk) << endl; + return false; + } + + return true; + } + + +}; + +// initialize static variables +atomic CurrentBlockchainStatus::current_height{0}; +string CurrentBlockchainStatus::blockchain_path{"/home/mwo/.blockchain/lmdb"}; +bool CurrentBlockchainStatus::testnet{false}; +bool CurrentBlockchainStatus::is_running{false}; +std::thread CurrentBlockchainStatus::m_thread; +uint64_t CurrentBlockchainStatus::refresh_block_status_every_seconds{60}; +xmreg::MicroCore CurrentBlockchainStatus::mcore; +cryptonote::Blockchain *CurrentBlockchainStatus::core_storage; + +} +#endif //RESTBED_XMR_CURRENTBLOCKCHAINSTATUS_H diff --git a/src/TxSearch.h b/src/TxSearch.h index 6d89cb0..33130f4 100644 --- a/src/TxSearch.h +++ b/src/TxSearch.h @@ -10,6 +10,7 @@ #include "MySqlConnector.h" #include "tools.h" #include "mylmdb.h" +#include "CurrentBlockchainStatus.h" #include #include @@ -32,123 +33,6 @@ class TxSearchException: public std::runtime_error using std::runtime_error::runtime_error; }; -/* - * This is a thread class - */ -struct CurrentBlockchainStatus -{ - static string blockchain_path; - - static atomic current_height; - - static bool testnet; - - static std::thread m_thread; - - static bool is_running; - - static uint64_t refresh_block_status_every_seconds; - - // since this class monitors current status - // of the blockchain, its seems logical to - // make object for accessing the blockchain here - static xmreg::MicroCore mcore; - static cryptonote::Blockchain* core_storage; - - static - void start_monitor_blockchain_thread() - { - if (!is_running) - { - m_thread = std::thread{ []() - { - while (true) - { - current_height = get_current_blockchain_height(); - cout << "Check block height: " << current_height << endl; - std::this_thread::sleep_for(std::chrono::seconds(refresh_block_status_every_seconds)); - } - - }}; - - is_running = true; - } - } - - static inline - uint64_t - get_current_blockchain_height() - { - return xmreg::MyLMDB::get_blockchain_height(blockchain_path) - 1; - } - - static void - set_blockchain_path(const string& path) - { - blockchain_path = path; - } - - static void - set_testnet(bool is_testnet) - { - testnet = is_testnet; - } - - static bool - init_monero_blockchain() - { - // enable basic monero log output - xmreg::enable_monero_log(); - - // initialize mcore and core_storage - if (!xmreg::init_blockchain(blockchain_path, - mcore, core_storage)) - { - cerr << "Error accessing blockchain." << endl; - return false; - } - - return true; - } - - static bool - get_block(uint64_t height, block& blk) - { - return mcore.get_block_by_height(height, blk); - } - - static bool - get_block_txs(const block& blk, list& blk_txs) - { - // get all transactions in the block found - // initialize the first list with transaction for solving - // the block i.e. coinbase tx. - blk_txs.push_back(blk.miner_tx); - - list missed_txs; - - if (!core_storage->get_transactions(blk.tx_hashes, blk_txs, missed_txs)) - { - cerr << "Cant get transactions in block: " << get_block_hash(blk) << endl; - return false; - } - - return true; - } - - -}; - -// initialize static variables -atomic CurrentBlockchainStatus::current_height {0}; -string CurrentBlockchainStatus::blockchain_path {"/home/mwo/.blockchain/lmdb"}; -bool CurrentBlockchainStatus::testnet {false}; -bool CurrentBlockchainStatus::is_running {false}; -std::thread CurrentBlockchainStatus::m_thread; -uint64_t CurrentBlockchainStatus::refresh_block_status_every_seconds {60}; -xmreg::MicroCore CurrentBlockchainStatus::mcore; -cryptonote::Blockchain* CurrentBlockchainStatus::core_storage; - class TxSearch {