From 871d6ce4346bffcc72f55edf4a0fbeb58f9c43ed Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Fri, 16 Dec 2016 08:05:54 +0800 Subject: [PATCH] testnet flag passed to CurrentBlockchainStatus --- main.cpp | 19 ++- src/CMakeLists.txt | 2 +- src/MySqlAccounts.h | 320 +++++++++++++++++++++++++++++++++++++++++++ src/MySqlConnector.h | 297 ++------------------------------------- src/TxSearch.h | 2 +- 5 files changed, 339 insertions(+), 301 deletions(-) create mode 100644 src/MySqlAccounts.h diff --git a/main.cpp b/main.cpp index ba912a3..b2367f1 100644 --- a/main.cpp +++ b/main.cpp @@ -1,18 +1,17 @@ -#include - -#include -#include - -#include "ext/restbed/source/restbed" - - #include "src/CmdLineOptions.h" #include "src/MicroCore.h" -#include "src/MySqlConnector.h" +#include "src/MySqlAccounts.h" #include "src/YourMoneroRequests.h" #include "src/tools.h" #include "src/TxSearch.h" +#include "ext/restbed/source/restbed" + + +#include +#include +#include + using namespace std; using namespace restbed; @@ -67,7 +66,7 @@ xmreg::MySqlConnector::dbname = "yourmonero"; // setup blockchain status monitoring thread xmreg::CurrentBlockchainStatus::set_blockchain_path(blockchain_path.string()); -xmreg::CurrentBlockchainStatus::set_testnet(false); +xmreg::CurrentBlockchainStatus::set_testnet(testnet); xmreg::CurrentBlockchainStatus::refresh_block_status_every_seconds = 30; // since CurrentBlockchainStatus class monitors current status diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5837df4..9e73152 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 CurrentBlockchainStatus.h ssqlses.h) + CmdLineOptions.cpp MySqlConnector.h TxSearch.h CurrentBlockchainStatus.h ssqlses.h MySqlAccounts.h) # make static library called libmyxrm # that we are going to link to diff --git a/src/MySqlAccounts.h b/src/MySqlAccounts.h new file mode 100644 index 0000000..d77c989 --- /dev/null +++ b/src/MySqlAccounts.h @@ -0,0 +1,320 @@ +// +// Created by mwo on 16/12/16. +// + +#ifndef RESTBED_XMR_MYSQLACCOUNTS_H +#define RESTBED_XMR_MYSQLACCOUNTS_H + + +#include "tools.h" +#include "ssqlses.h" +#include "MySqlConnector.h" + +#include +#include + + +#include +#include + + +namespace xmreg +{ + +using namespace mysqlpp; +using namespace std; +using namespace nlohmann; + + +class MysqlTransactions +{ + + shared_ptr conn; + +public: + + MysqlTransactions(shared_ptr _conn): conn {_conn} + {} + + bool + select(const uint64_t& address_id, vector& txs) + { +// +// static shared_ptr query; +// +// if (!query) +// { +// Query q = MySqlConnector::getInstance().query( +// XmrTransaction::SELECT_STMT); +// q.parse(); +// query = shared_ptr(new Query(q)); +// } + + Query query = conn->query(XmrTransaction::SELECT_STMT); + query.parse(); + + try + { + query.storein(txs, address_id); + + if (!txs.empty()) + { + return true; + } + } + catch (mysqlpp::Exception& e) + { + MYSQL_EXCEPTION_MSG(e); + } + catch (std::exception& e) + { + MYSQL_EXCEPTION_MSG(e); + } + + return false; + } + + + uint64_t + insert(const XmrTransaction& tx_data) + { + +// static shared_ptr query; +// +// if (!query) +// { +// Query q = MySqlConnector::getInstance().query(XmrTransaction::INSERT_STMT); +// q.parse(); +// query = shared_ptr(new Query(q)); +// } + + + Query query = conn->query(XmrTransaction::INSERT_STMT); + query.parse(); + + // cout << query << endl; + + try + { + SimpleResult sr = query.execute(tx_data.hash, + tx_data.account_id, + tx_data.total_received, + tx_data.total_sent, + tx_data.unlock_time, + tx_data.height, + tx_data.coinbase, + tx_data.payment_id, + tx_data.mixin, + tx_data.timestamp); + + if (sr.rows() == 1) + return sr.insert_id(); + + } + catch (mysqlpp::Exception& e) + { + MYSQL_EXCEPTION_MSG(e); + return 0; + } + + return 0; + } + + +}; + + +class MySqlAccounts +{ + + shared_ptr conn; + + shared_ptr mysql_tx; + +public: + + + MySqlAccounts() + { + cout << "MySqlAccounts() makes new connection" << endl; + conn = make_shared(); + mysql_tx = make_shared(conn); + } + + + bool + select(const string& address, XmrAccount& account) + { + +// static shared_ptr query; +// +// if (!query) +// { +// Query q = MySqlConnector::getInstance().query(XmrAccount::SELECT_STMT); +// q.parse(); +// query = shared_ptr(new Query(q)); +// } + + Query query = conn->query(XmrAccount::SELECT_STMT); + query.parse(); + + try + { + vector res; + query.storein(res, address); + + if (!res.empty()) + { + account = res.at(0); + return true; + } + + } + catch (mysqlpp::Exception& e) + { + MYSQL_EXCEPTION_MSG(e); + } + catch (std::exception& e) + { + MYSQL_EXCEPTION_MSG(e); + } + + return false; + } + + bool + select(const int64_t& acc_id, XmrAccount& account) + { + + //static shared_ptr query; + +// if (!query) +// { +// Query q = MySqlConnector::getInstance().query(XmrAccount::SELECT_STMT2); +// q.parse(); +// query = shared_ptr(new Query(q)); +// } + + Query query = conn->query(XmrAccount::SELECT_STMT2); + query.parse(); + + try + { + vector res; + query.storein(res, acc_id); + + if (!res.empty()) + { + account = res.at(0); + return true; + } + + } + catch (mysqlpp::Exception& e) + { + MYSQL_EXCEPTION_MSG(e); + } + + return false; + } + + uint64_t + insert(const string& address, const uint64_t& scanned_block_height = 0) + { + + // static shared_ptr query; + +// if (!query) +// { +// Query q = MySqlConnector::getInstance().query(XmrAccount::INSERT_STMT); +// q.parse(); +// query = shared_ptr(new Query(q)); +// } + + + Query query = conn->query(XmrAccount::INSERT_STMT); + query.parse(); + + // cout << query << endl; + + try + { + SimpleResult sr = query.execute(address, scanned_block_height); + + if (sr.rows() == 1) + return sr.insert_id(); + + } + catch (mysqlpp::Exception& e) + { + MYSQL_EXCEPTION_MSG(e); + return 0; + } + + return 0; + } + + uint64_t + insert_tx(const XmrTransaction& tx_data) + { + return mysql_tx->insert(tx_data); + } + + bool + select_txs(const string& xmr_address, vector& txs) + { + // having address first get its address_id + + + // a placeholder for exciting or new account data + xmreg::XmrAccount acc; + + // select this account if its existing one + if (!select(xmr_address, acc)) + { + cerr << "Address" << xmr_address << "does not exist in database" << endl; + return false; + } + + + return mysql_tx->select(acc.id, txs); + } + + bool + select_txs(const uint64_t& account_id, vector& txs) + { + return mysql_tx->select(account_id, txs); + } + + + bool + update(XmrAccount& acc_orginal, XmrAccount& acc_new) + { + + Query query = conn->query(); + + try + { + query.update(acc_orginal, acc_new); + + SimpleResult sr = query.execute(); + + if (sr.rows() == 1) + return true; + } + catch (mysqlpp::Exception& e) + { + MYSQL_EXCEPTION_MSG(e); + return false; + } + + return false; + } + +}; + + +} + + +#endif //RESTBED_XMR_MYSQLACCOUNTS_H diff --git a/src/MySqlConnector.h b/src/MySqlConnector.h index de089ff..c3cef62 100644 --- a/src/MySqlConnector.h +++ b/src/MySqlConnector.h @@ -30,10 +30,12 @@ using namespace nlohmann; /* - * This is singleton class that connects - * and holds connection to our mysql. + * This is that creates connections + * our mysql database. * - * Only one instance of it can exist. + * Each thread has its own connection. + * This way when something breaks in one thread, + * other threads can still operate on the mysql. * */ class MySqlConnector @@ -53,10 +55,11 @@ public: if (conn.connected()) return; - if (conn.connect(dbname.c_str(), url.c_str(), + if (!conn.connect(dbname.c_str(), url.c_str(), username.c_str(), password.c_str())) { - cout << "Connection to Mysql successful" << endl; + cerr << "Connection to Mysql failed!" << endl; + return; } } @@ -72,8 +75,6 @@ public: return conn.query(qstr); } - - virtual ~MySqlConnector() { conn.disconnect(); @@ -85,293 +86,11 @@ string MySqlConnector::username; string MySqlConnector::password; string MySqlConnector::dbname; -class MysqlTransactions -{ - - shared_ptr conn; - -public: - - MysqlTransactions(shared_ptr _conn): conn {_conn} - {} - - bool - select(const uint64_t& address_id, vector& txs) - { -// -// static shared_ptr query; -// -// if (!query) -// { -// Query q = MySqlConnector::getInstance().query( -// XmrTransaction::SELECT_STMT); -// q.parse(); -// query = shared_ptr(new Query(q)); -// } - - Query query = conn->query(XmrTransaction::SELECT_STMT); - query.parse(); - - try - { - query.storein(txs, address_id); - - if (!txs.empty()) - { - return true; - } - } - catch (mysqlpp::Exception& e) - { - MYSQL_EXCEPTION_MSG(e); - } - catch (std::exception& e) - { - MYSQL_EXCEPTION_MSG(e); - } - - return false; - } - - - uint64_t - insert(const XmrTransaction& tx_data) - { - -// static shared_ptr query; -// -// if (!query) -// { -// Query q = MySqlConnector::getInstance().query(XmrTransaction::INSERT_STMT); -// q.parse(); -// query = shared_ptr(new Query(q)); -// } - - - Query query = conn->query(XmrTransaction::INSERT_STMT); - query.parse(); - - // cout << query << endl; - - try - { - SimpleResult sr = query.execute(tx_data.hash, - tx_data.account_id, - tx_data.total_received, - tx_data.total_sent, - tx_data.unlock_time, - tx_data.height, - tx_data.coinbase, - tx_data.payment_id, - tx_data.mixin, - tx_data.timestamp); - - if (sr.rows() == 1) - return sr.insert_id(); - - } - catch (mysqlpp::Exception& e) - { - MYSQL_EXCEPTION_MSG(e); - return 0; - } - - return 0; - } - - -}; - - - -class MySqlAccounts -{ - - shared_ptr conn; - - shared_ptr mysql_tx; - -public: - - - MySqlAccounts() - { - cout << "MySqlAccounts() makes new connection" << endl; - conn = make_shared(); - mysql_tx = make_shared(conn); - } - - - bool - select(const string& address, XmrAccount& account) - { - -// static shared_ptr query; -// -// if (!query) -// { -// Query q = MySqlConnector::getInstance().query(XmrAccount::SELECT_STMT); -// q.parse(); -// query = shared_ptr(new Query(q)); -// } - - Query query = conn->query(XmrAccount::SELECT_STMT); - query.parse(); - - try - { - vector res; - query.storein(res, address); - - if (!res.empty()) - { - account = res.at(0); - return true; - } - - } - catch (mysqlpp::Exception& e) - { - MYSQL_EXCEPTION_MSG(e); - } - catch (std::exception& e) - { - MYSQL_EXCEPTION_MSG(e); - } - - return false; - } - - bool - select(const int64_t& acc_id, XmrAccount& account) - { - - //static shared_ptr query; - -// if (!query) -// { -// Query q = MySqlConnector::getInstance().query(XmrAccount::SELECT_STMT2); -// q.parse(); -// query = shared_ptr(new Query(q)); -// } - - Query query = conn->query(XmrAccount::SELECT_STMT2); - query.parse(); - - try - { - vector res; - query.storein(res, acc_id); - - if (!res.empty()) - { - account = res.at(0); - return true; - } - - } - catch (mysqlpp::Exception& e) - { - MYSQL_EXCEPTION_MSG(e); - } - - return false; - } - - uint64_t - insert(const string& address, const uint64_t& scanned_block_height = 0) - { - - // static shared_ptr query; - -// if (!query) -// { -// Query q = MySqlConnector::getInstance().query(XmrAccount::INSERT_STMT); -// q.parse(); -// query = shared_ptr(new Query(q)); -// } - - - Query query = conn->query(XmrAccount::INSERT_STMT); - query.parse(); - - // cout << query << endl; - - try - { - SimpleResult sr = query.execute(address, scanned_block_height); - - if (sr.rows() == 1) - return sr.insert_id(); - - } - catch (mysqlpp::Exception& e) - { - MYSQL_EXCEPTION_MSG(e); - return 0; - } - - return 0; - } - - uint64_t - insert_tx(const XmrTransaction& tx_data) - { - return mysql_tx->insert(tx_data); - } - - bool - select_txs(const string& xmr_address, vector& txs) - { - // having address first get its address_id - - - // a placeholder for exciting or new account data - xmreg::XmrAccount acc; - - // select this account if its existing one - if (!select(xmr_address, acc)) - { - cerr << "Address" << xmr_address << "does not exist in database" << endl; - return false; - } - return mysql_tx->select(acc.id, txs); - } - - bool - select_txs(const uint64_t& account_id, vector& txs) - { - return mysql_tx->select(account_id, txs); - } - - - bool - update(XmrAccount& acc_orginal, XmrAccount& acc_new) - { - - Query query = conn->query(); - try - { - query.update(acc_orginal, acc_new); - - SimpleResult sr = query.execute(); - - if (sr.rows() == 1) - return true; - } - catch (mysqlpp::Exception& e) - { - MYSQL_EXCEPTION_MSG(e); - return false; - } - return false; - } -}; } diff --git a/src/TxSearch.h b/src/TxSearch.h index 4fbf6b4..6b81d2b 100644 --- a/src/TxSearch.h +++ b/src/TxSearch.h @@ -7,7 +7,7 @@ -#include "MySqlConnector.h" +#include "MySqlAccounts.h" #include "tools.h" #include "mylmdb.h" #include "CurrentBlockchainStatus.h"