searching_threads moved to CurrentBlockchainStatus class

pull/2/head
moneroexamples 7 years ago
parent 663581714a
commit 455832fab8

@ -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<string, shared_ptr<TxSearch>> 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<std::mutex> 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<TxSearch>(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<std::mutex> 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<std::mutex> 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<std::mutex> 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);
}
}
}
}

@ -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 <iostream>
#include <memory>
@ -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<transaction> 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<string, shared_ptr<TxSearch>> 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();
};

@ -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<string, shared_ptr<TxSearch>> 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<std::mutex> 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<TxSearch>(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<std::mutex> 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<std::mutex> 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<std::mutex> 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);
}
}
}
}

@ -7,13 +7,12 @@
#include "tools.h"
#include "MySqlConnector.h"
#include "CurrentBlockchainStatus.h"
#include <iostream>
#include <memory>
#include <mutex>
#include <thread>
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<MysqlTransactionWithOutsAndIns> 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<string, shared_ptr<TxSearch>> searching_threads;
public:
@ -213,12 +206,10 @@ public:
bool
select_txs(const uint64_t& account_id, vector<XmrTransaction>& txs);
bool
select_txs_with_inputs_and_outputs(const uint64_t& account_id,
vector<XmrTransactionWithOutsAndIns>& txs);
bool
select_outputs(const uint64_t& account_id, vector<XmrOutput>& 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();
};

@ -10,6 +10,8 @@
#include "ssqlses.h"
#include "CurrentBlockchainStatus.h"
namespace xmreg
{

@ -123,7 +123,7 @@ YourMoneroRequests::login(const shared_ptr<Session> 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!";

Loading…
Cancel
Save