From 455832fab84d9191158e752e89b908cfe56463b6 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Sun, 15 Jan 2017 15:10:11 +0800 Subject: [PATCH] searching_threads moved to CurrentBlockchainStatus class --- src/CurrentBlockchainStatus.cpp | 83 ++++++++++++++++++++++++++++++++- src/CurrentBlockchainStatus.h | 23 +++++++++ src/MySqlAccounts.cpp | 77 +----------------------------- src/MySqlAccounts.h | 29 +----------- src/TxSearch.cpp | 2 + src/YourMoneroRequests.cpp | 6 +-- 6 files changed, 112 insertions(+), 108 deletions(-) diff --git a/src/CurrentBlockchainStatus.cpp b/src/CurrentBlockchainStatus.cpp index 08403a1..a5abfc9 100644 --- a/src/CurrentBlockchainStatus.cpp +++ b/src/CurrentBlockchainStatus.cpp @@ -8,6 +8,8 @@ #include "tools.h" #include "mylmdb.h" #include "rpccalls.h" +#include "MySqlAccounts.h" +#include "TxSearch.h" namespace xmreg { @@ -30,6 +32,7 @@ string CurrentBlockchainStatus::import_payment_viewkey; uint64_t CurrentBlockchainStatus::import_fee {10000000000}; // 0.01 xmr account_public_address CurrentBlockchainStatus::address; secret_key CurrentBlockchainStatus::viewkey; +map> CurrentBlockchainStatus::searching_threads; void CurrentBlockchainStatus::start_monitor_blockchain_thread() @@ -72,8 +75,10 @@ CurrentBlockchainStatus::start_monitor_blockchain_thread() cout << "Check block height: " << current_height; cout << " no of mempool txs: " << mempool_txs.size(); cout << endl; - //clean_search_thread_map(); - std::this_thread::sleep_for(std::chrono::seconds(refresh_block_status_every_seconds)); + clean_search_thread_map(); + std::this_thread::sleep_for( + std::chrono::seconds( + refresh_block_status_every_seconds)); } }}; @@ -433,4 +438,78 @@ CurrentBlockchainStatus::get_payment_id_as_string(const transaction& tx) +bool +CurrentBlockchainStatus::start_tx_search_thread(XmrAccount acc) +{ + std::lock_guard lck (searching_threads_map_mtx); + + if (searching_threads.count(acc.address) > 0) + { + // thread for this address exist, dont make new one + cout << "Thread exisist, dont make new one" << endl; + return false; + } + + // make a tx_search object for the given xmr account + searching_threads[acc.address] = make_shared(acc); + + // start the thread for the created object + std::thread t1 {&TxSearch::search, searching_threads[acc.address].get()}; + t1.detach(); + + return true; +} + +bool +CurrentBlockchainStatus::ping_search_thread(const string& address) +{ + std::lock_guard lck (searching_threads_map_mtx); + + if (searching_threads.count(address) == 0) + { + // thread does not exist + cout << "does not exist" << endl; + return false; + } + + searching_threads[address].get()->ping(); + + return true; +} + +bool +CurrentBlockchainStatus::set_new_searched_blk_no(const string& address, uint64_t new_value) +{ + std::lock_guard lck (searching_threads_map_mtx); + + if (searching_threads.count(address) == 0) + { + // thread does not exist + cout << " thread does not exist" << endl; + return false; + } + + searching_threads[address].get()->set_searched_blk_no(new_value); + + return true; +} + + +void +CurrentBlockchainStatus::clean_search_thread_map() +{ + std::lock_guard lck (searching_threads_map_mtx); + + for (auto st: searching_threads) + { + if (st.second->still_searching() == false) + { + cout << st.first << " still searching: " << st.second->still_searching() << endl; + searching_threads.erase(st.first); + } + } +} + + + } \ No newline at end of file diff --git a/src/CurrentBlockchainStatus.h b/src/CurrentBlockchainStatus.h index 2d87f18..abfa0ec 100644 --- a/src/CurrentBlockchainStatus.h +++ b/src/CurrentBlockchainStatus.h @@ -5,8 +5,10 @@ #ifndef RESTBED_XMR_CURRENTBLOCKCHAINSTATUS_H #define RESTBED_XMR_CURRENTBLOCKCHAINSTATUS_H +#define MYSQLPP_SSQLS_NO_STATICS 1 #include "MicroCore.h" +#include "ssqlses.h" #include #include @@ -17,12 +19,14 @@ namespace xmreg { +using namespace std; class TxSearch; class XmrAccount; class MySqlAccounts; +static mutex searching_threads_map_mtx; static mutex getting_mempool_txs; /* @@ -56,6 +60,11 @@ struct CurrentBlockchainStatus // can refer to static vector mempool_txs; + // map that will keep track of search threads. In the + // map, key is address to which a running thread belongs to. + // make it static to guarantee only one such map exist. + static map> searching_threads; + // since this class monitors current status // of the blockchain, its seems logical to // make object for accessing the blockchain here @@ -119,6 +128,20 @@ struct CurrentBlockchainStatus + // definitions of these function are at the end of this file + // due to forward declaraions of TxSearch + static bool + start_tx_search_thread(XmrAccount acc); + + static bool + ping_search_thread(const string& address); + + static bool + set_new_searched_blk_no(const string& address, uint64_t new_value); + + static void + clean_search_thread_map(); + }; diff --git a/src/MySqlAccounts.cpp b/src/MySqlAccounts.cpp index ff78723..e1c43d7 100644 --- a/src/MySqlAccounts.cpp +++ b/src/MySqlAccounts.cpp @@ -6,6 +6,7 @@ #include "MySqlAccounts.h" #include "TxSearch.h" +#include "CurrentBlockchainStatus.h" #include "ssqlses.h" @@ -612,8 +613,6 @@ MysqlPayments::update(XmrPayment& payment_orginal, XmrPayment& payment_new) } -map> MySqlAccounts::searching_threads; - MySqlAccounts::MySqlAccounts() { // create connection to the mysql @@ -922,80 +921,6 @@ MySqlAccounts::update(XmrAccount& acc_orginal, XmrAccount& acc_new) -bool -MySqlAccounts::start_tx_search_thread(XmrAccount acc) -{ - std::lock_guard lck (searching_threads_map_mtx); - - if (searching_threads.count(acc.address) > 0) - { - // thread for this address exist, dont make new one - cout << "Thread exisist, dont make new one" << endl; - return false; - } - - // make a tx_search object for the given xmr account - searching_threads[acc.address] = make_shared(acc); - - // start the thread for the created object - std::thread t1 {&TxSearch::search, searching_threads[acc.address].get()}; - t1.detach(); - - return true; -} - -bool -MySqlAccounts::ping_search_thread(const string& address) -{ - std::lock_guard lck (searching_threads_map_mtx); - - if (searching_threads.count(address) == 0) - { - // thread does not exist - cout << "does not exist" << endl; - return false; - } - - searching_threads[address].get()->ping(); - - return true; -} - -bool -MySqlAccounts::set_new_searched_blk_no(const string& address, uint64_t new_value) -{ - std::lock_guard lck (searching_threads_map_mtx); - - if (searching_threads.count(address) == 0) - { - // thread does not exist - cout << " thread does not exist" << endl; - return false; - } - - searching_threads[address].get()->set_searched_blk_no(new_value); - - return true; -} - - -void -MySqlAccounts::clean_search_thread_map() -{ - std::lock_guard lck (searching_threads_map_mtx); - - for (auto st: searching_threads) - { - if (st.second->still_searching() == false) - { - cout << st.first << " still searching: " << st.second->still_searching() << endl; - searching_threads.erase(st.first); - } - } -} - - - } \ No newline at end of file diff --git a/src/MySqlAccounts.h b/src/MySqlAccounts.h index fa6812f..ec0d479 100644 --- a/src/MySqlAccounts.h +++ b/src/MySqlAccounts.h @@ -7,13 +7,12 @@ #include "tools.h" #include "MySqlConnector.h" -#include "CurrentBlockchainStatus.h" + #include #include -#include -#include + namespace xmreg @@ -33,8 +32,6 @@ class XmrAccount; class TxSearch; -static mutex searching_threads_map_mtx; - class MysqlTransactionWithOutsAndIns { @@ -179,10 +176,6 @@ class MySqlAccounts shared_ptr mysql_tx_inout; - // map that will keep track of search threads. In the - // map, key is address to which a running thread belongs to. - // make it static to guarantee only one such map exist. - static map> searching_threads; public: @@ -213,12 +206,10 @@ public: bool select_txs(const uint64_t& account_id, vector& txs); - bool select_txs_with_inputs_and_outputs(const uint64_t& account_id, vector& txs); - bool select_outputs(const uint64_t& account_id, vector& outs); @@ -257,26 +248,10 @@ public: uint64_t get_total_recieved(const uint64_t& account_id); - bool update(XmrAccount& acc_orginal, XmrAccount& acc_new); - // definitions of these function are at the end of this file - // due to forward declaraions of TxSearch - static bool - start_tx_search_thread(XmrAccount acc); - - static bool - ping_search_thread(const string& address); - - static bool - set_new_searched_blk_no(const string& address, uint64_t new_value); - - //@TODO: Need to use it somewhere - static void - clean_search_thread_map(); - }; diff --git a/src/TxSearch.cpp b/src/TxSearch.cpp index 89f7274..7fa4ddf 100644 --- a/src/TxSearch.cpp +++ b/src/TxSearch.cpp @@ -10,6 +10,8 @@ #include "ssqlses.h" +#include "CurrentBlockchainStatus.h" + namespace xmreg { diff --git a/src/YourMoneroRequests.cpp b/src/YourMoneroRequests.cpp index 9957f32..e39c569 100644 --- a/src/YourMoneroRequests.cpp +++ b/src/YourMoneroRequests.cpp @@ -123,7 +123,7 @@ YourMoneroRequests::login(const shared_ptr session, const Bytes & body) // to do anything except looking for tx and updating mysql // with relative tx information - if (MySqlAccounts::start_tx_search_thread(acc)) + if (CurrentBlockchainStatus::start_tx_search_thread(acc)) { cout << "Search thread started" << endl; } @@ -258,7 +258,7 @@ YourMoneroRequests::get_address_info(const shared_ptr< Session > session, const { // ping the search thread that we still need it. // otherwise it will finish after some time. - MySqlAccounts::ping_search_thread(xmr_address); + CurrentBlockchainStatus::ping_search_thread(xmr_address); j_response["total_received"] = acc.total_received; j_response["start_height"] = acc.start_height; @@ -549,7 +549,7 @@ YourMoneroRequests::import_wallet_request(const shared_ptr< Session > session, c request_fulfilled = true; // change search blk number in the search thread - if (!MySqlAccounts::set_new_searched_blk_no(xmr_address, 0)) + if (!CurrentBlockchainStatus::set_new_searched_blk_no(xmr_address, 0)) { cerr << "Updating searched_blk_no failed!" << endl; j_response["status"] = "Updating searched_blk_no failed!";