CurrentBlockchainStatus refactor for independency injection

pull/93/merge
moneroexamples 6 years ago
parent 65b9d989f1
commit 1407ce0c0f

@ -71,27 +71,26 @@ catch (const std::exception& e)
//cast port number in string to uint16
uint16_t app_port = boost::lexical_cast<uint16_t>(*port_opt);
// get the network type
cryptonote::network_type nettype = testnet ?
cryptonote::network_type::TESTNET : stagenet ?
cryptonote::network_type::STAGENET : cryptonote::network_type::MAINNET;
// set blockchain status monitoring thread parameters
xmreg::CurrentBlockchainStatus::net_type
= nettype;
xmreg::CurrentBlockchainStatus::do_not_relay
= do_not_relay;
xmreg::CurrentBlockchainStatus::refresh_block_status_every_seconds
= config_json["refresh_block_status_every_seconds"];
xmreg::CurrentBlockchainStatus::blocks_search_lookahead
= config_json["blocks_search_lookahead"];
xmreg::CurrentBlockchainStatus::max_number_of_blocks_to_import
= config_json["max_number_of_blocks_to_import"];
xmreg::CurrentBlockchainStatus::search_thread_life_in_seconds
= config_json["search_thread_life_in_seconds"];
xmreg::CurrentBlockchainStatus::import_fee
= config_json["wallet_import"]["fee"];
// create blockchainsetup instance and set its parameters
// suc as blockchain status monitoring thread parameters
xmreg::BlockchainSetup bc_setup;
bc_setup.net_type = nettype;
bc_setup.do_not_relay = do_not_relay;
bc_setup.refresh_block_status_every_seconds = config_json["refresh_block_status_every_seconds"];
bc_setup.blocks_search_lookahead = config_json["blocks_search_lookahead"];
bc_setup.max_number_of_blocks_to_import = config_json["max_number_of_blocks_to_import"];
bc_setup.search_thread_life_in_seconds = config_json["search_thread_life_in_seconds"];
bc_setup.import_fee = config_json["wallet_import"]["fee"];
string deamon_url;
@ -105,25 +104,25 @@ switch (nettype)
case cryptonote::network_type::MAINNET:
blockchain_path = path(config_json["blockchain-path"]["mainnet"].get<string>());
deamon_url = config_json["daemon-url"]["mainnet"];
xmreg::CurrentBlockchainStatus::import_payment_address_str
bc_setup.import_payment_address_str
= config_json["wallet_import"]["mainnet"]["address"];
xmreg::CurrentBlockchainStatus::import_payment_viewkey_str
bc_setup.import_payment_viewkey_str
= config_json["wallet_import"]["mainnet"]["viewkey"];
break;
case cryptonote::network_type::TESTNET:
blockchain_path = path(config_json["blockchain-path"]["testnet"].get<string>());
deamon_url = config_json["daemon-url"]["testnet"];
xmreg::CurrentBlockchainStatus::import_payment_address_str
bc_setup.import_payment_address_str
= config_json["wallet_import"]["testnet"]["address"];
xmreg::CurrentBlockchainStatus::import_payment_viewkey_str
bc_setup.import_payment_viewkey_str
= config_json["wallet_import"]["testnet"]["viewkey"];
break;
case cryptonote::network_type::STAGENET:
blockchain_path = path(config_json["blockchain-path"]["stagenet"].get<string>());
deamon_url = config_json["daemon-url"]["stagenet"];
xmreg::CurrentBlockchainStatus::import_payment_address_str
bc_setup.import_payment_address_str
= config_json["wallet_import"]["stagenet"]["address"];
xmreg::CurrentBlockchainStatus::import_payment_viewkey_str
bc_setup.import_payment_viewkey_str
= config_json["wallet_import"]["stagenet"]["viewkey"];
break;
default:
@ -139,12 +138,10 @@ if (!xmreg::get_blockchain_path(blockchain_path, nettype))
}
// set remaining blockchain status variables that depend on the network type
xmreg::CurrentBlockchainStatus::blockchain_path
= blockchain_path.string();
xmreg::CurrentBlockchainStatus::deamon_url
= deamon_url;
bc_setup.blockchain_path = blockchain_path.string();
bc_setup.deamon_url = deamon_url;
cout << "Blockchain path: " << blockchain_path.string() << endl;
cout << "Using blockchain path: " << blockchain_path.string() << endl;
// set mysql/mariadb connection details
@ -155,19 +152,13 @@ xmreg::MySqlConnector::password = config_json["database"]["password"];
xmreg::MySqlConnector::dbname = config_json["database"]["dbname"];
// try connecting to the mysql
shared_ptr<xmreg::MySqlAccounts> mysql_accounts;
// once we have all the parameters for the blockchain and our backend
// we can create and instance of CurrentBlockchainStatus class.
// we are going to this through a shared pointer. This way we will
// have only once instance of this class, which we can easly inject
// and pass around other class which need to access blockchain data
try
{
// MySqlAccounts will try connecting to the mysql database
mysql_accounts = make_shared<xmreg::MySqlAccounts>();
}
catch(std::exception const& e)
{
cerr << e.what() << '\n';
return EXIT_FAILURE;
}
auto current_bc_status = make_shared<xmreg::CurrentBlockchainStatus>(bc_setup);
// since CurrentBlockchainStatus class monitors current status
@ -178,7 +169,7 @@ catch(std::exception const& e)
// There are here, and this is the only class that
// has direct access to blockchain and talks (using rpc calls)
// with the deamon.
if (!xmreg::CurrentBlockchainStatus::init_monero_blockchain())
if (!current_bc_status->init_monero_blockchain())
{
cerr << "Error accessing blockchain." << endl;
return EXIT_FAILURE;
@ -188,12 +179,25 @@ if (!xmreg::CurrentBlockchainStatus::init_monero_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();
current_bc_status->start_monitor_blockchain_thread();
// try connecting to the mysql
shared_ptr<xmreg::MySqlAccounts> mysql_accounts;
try
{
// MySqlAccounts will try connecting to the mysql database
mysql_accounts = make_shared<xmreg::MySqlAccounts>(current_bc_status);
}
catch(std::exception const& e)
{
cerr << e.what() << '\n';
return EXIT_FAILURE;
}
// create REST JSON API services
xmreg::YourMoneroRequests open_monero(mysql_accounts);
xmreg::YourMoneroRequests open_monero(mysql_accounts, current_bc_status);
// create Open Monero APIs
MAKE_RESOURCE(login);

@ -0,0 +1,5 @@
//
// Created by mwo on 10/07/18.
//
#include "BlockchainSetup.h"

@ -0,0 +1,54 @@
//
// Created by mwo on 10/07/18.
//
#ifndef OPENMONERO_BLOCKCHAINSETUP_H
#define OPENMONERO_BLOCKCHAINSETUP_H
#include "monero_headers.h"
#include <string>
namespace xmreg
{
using namespace crypto;
using namespace cryptonote;
using namespace std;
class BlockchainSetup
{
public:
string blockchain_path;
string deamon_url;
network_type net_type;
bool do_not_relay;
uint64_t refresh_block_status_every_seconds;
uint64_t blocks_search_lookahead;
uint64_t max_number_of_blocks_to_import;
uint64_t search_thread_life_in_seconds;
string import_payment_address_str;
string import_payment_viewkey_str;
uint64_t import_fee;
uint64_t spendable_age;
uint64_t spendable_age_coinbase;
address_parse_info import_payment_address;
secret_key import_payment_viewkey;
};
}
#endif //OPENMONERO_BLOCKCHAINSETUP_H

@ -14,7 +14,8 @@ set(SOURCE_FILES
TxSearch.cpp
rpccalls.cpp
OutputInputIdentification.cpp
version.h.in)
version.h.in
BlockchainSetup.cpp)
# make static library called libmyxrm
# that we are going to link to

@ -18,56 +18,38 @@
namespace xmreg
{
CurrentBlockchainStatus::CurrentBlockchainStatus(BlockchainSetup _bc_setup)
: bc_setup {_bc_setup}
{
// initialize static variables
atomic<uint64_t> CurrentBlockchainStatus::current_height{0};
string CurrentBlockchainStatus::blockchain_path{"/home/mwo/.blockchain/lmdb"};
string CurrentBlockchainStatus::deamon_url{"http:://127.0.0.1:18081"};
network_type CurrentBlockchainStatus::net_type {network_type::MAINNET};
bool CurrentBlockchainStatus::do_not_relay{false};
bool CurrentBlockchainStatus::is_running{false};
std::thread CurrentBlockchainStatus::m_thread;
uint64_t CurrentBlockchainStatus::refresh_block_status_every_seconds{10};
uint64_t CurrentBlockchainStatus::blocks_search_lookahead {100};
uint64_t CurrentBlockchainStatus::max_number_of_blocks_to_import{8000};
uint64_t CurrentBlockchainStatus::search_thread_life_in_seconds {600}; // 10 minutes
vector<pair<uint64_t, transaction>> CurrentBlockchainStatus::mempool_txs;
string CurrentBlockchainStatus::import_payment_address_str;
string CurrentBlockchainStatus::import_payment_viewkey_str;
uint64_t CurrentBlockchainStatus::import_fee {10000000000}; // 0.01 xmr
address_parse_info CurrentBlockchainStatus::import_payment_address;
secret_key CurrentBlockchainStatus::import_payment_viewkey;
map<string, unique_ptr<TxSearch>> CurrentBlockchainStatus::searching_threads;
cryptonote::Blockchain* CurrentBlockchainStatus::core_storage;
unique_ptr<xmreg::MicroCore> CurrentBlockchainStatus::mcore;
}
void
CurrentBlockchainStatus::start_monitor_blockchain_thread()
{
network_type net_type = CurrentBlockchainStatus::net_type;
network_type net_type = bc_setup.net_type;
TxSearch::set_search_thread_life(search_thread_life_in_seconds);
TxSearch::set_search_thread_life(bc_setup.search_thread_life_in_seconds);
if (!import_payment_address_str.empty() && !import_payment_viewkey_str.empty())
if (!bc_setup.import_payment_address_str.empty() && !bc_setup.import_payment_viewkey_str.empty())
{
if (!xmreg::parse_str_address(
import_payment_address_str,
import_payment_address,
CurrentBlockchainStatus::net_type))
bc_setup.import_payment_address_str,
bc_setup.import_payment_address,
net_type))
{
cerr << "Cant parse address_str: "
<< import_payment_address_str
<< bc_setup.import_payment_address_str
<< endl;
return;
}
if (!xmreg::parse_str_secret_key(
import_payment_viewkey_str,
import_payment_viewkey))
bc_setup.import_payment_viewkey_str,
bc_setup.import_payment_viewkey))
{
cerr << "Cant parse the viewkey_str: "
<< import_payment_viewkey_str
<< bc_setup.import_payment_viewkey_str
<< endl;
return;
}
@ -75,7 +57,7 @@ CurrentBlockchainStatus::start_monitor_blockchain_thread()
if (!is_running)
{
m_thread = std::thread{[]()
m_thread = std::thread{[this]()
{
while (true)
{
@ -87,7 +69,7 @@ CurrentBlockchainStatus::start_monitor_blockchain_thread()
clean_search_thread_map();
std::this_thread::sleep_for(
std::chrono::seconds(
refresh_block_status_every_seconds));
bc_setup.refresh_block_status_every_seconds));
}
}};
@ -104,7 +86,7 @@ CurrentBlockchainStatus::get_current_blockchain_height()
try
{
return xmreg::MyLMDB::get_blockchain_height(blockchain_path) - 1;
return xmreg::MyLMDB::get_blockchain_height(bc_setup.blockchain_path) - 1;
}
catch(std::exception& e)
{
@ -131,7 +113,7 @@ CurrentBlockchainStatus::init_monero_blockchain()
mcore = unique_ptr<xmreg::MicroCore>(new xmreg::MicroCore{});
// initialize the core using the blockchain path
if (!mcore->init(blockchain_path, net_type))
if (!mcore->init(bc_setup.blockchain_path, bc_setup.net_type))
return false;
// get the high level Blockchain object to interact
@ -177,7 +159,7 @@ CurrentBlockchainStatus::is_tx_spendtime_unlocked(
// XXX: this needs to be fast, so we'd need to get the starting heights
// from the daemon to be correct once voting kicks in
uint64_t v2height = net_type == TESTNET ? 624634 : net_type == STAGENET ? (uint64_t)-1/*TODO*/ : 1009827;
uint64_t v2height = bc_setup.net_type == TESTNET ? 624634 : bc_setup.net_type == STAGENET ? (uint64_t)-1/*TODO*/ : 1009827;
uint64_t leeway = block_height < v2height
? CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V1
@ -358,8 +340,9 @@ string
CurrentBlockchainStatus::get_account_integrated_address_as_str(
crypto::hash8 const& payment_id8)
{
return cryptonote::get_account_integrated_address_as_str(net_type,
import_payment_address.address, payment_id8);
return cryptonote::get_account_integrated_address_as_str(
bc_setup.net_type,
bc_setup.import_payment_address.address, payment_id8);
}
string
@ -405,7 +388,7 @@ CurrentBlockchainStatus::get_random_outputs(
const uint64_t& outs_count,
vector<COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount>& found_outputs)
{
rpccalls rpc {deamon_url};
rpccalls rpc {bc_setup.deamon_url};
string error_msg;
@ -425,7 +408,7 @@ CurrentBlockchainStatus::get_output(
const uint64_t global_output_index,
COMMAND_RPC_GET_OUTPUTS_BIN::outkey& output_info)
{
rpccalls rpc {deamon_url};
rpccalls rpc {bc_setup.deamon_url};
string error_msg;
@ -442,7 +425,7 @@ CurrentBlockchainStatus::get_output(
bool
CurrentBlockchainStatus::get_dynamic_per_kb_fee_estimate(uint64_t& fee_estimated)
{
rpccalls rpc {deamon_url};
rpccalls rpc {bc_setup.deamon_url};
string error_msg;
@ -461,7 +444,7 @@ CurrentBlockchainStatus::get_dynamic_per_kb_fee_estimate(uint64_t& fee_estimated
bool
CurrentBlockchainStatus::commit_tx(const string& tx_blob, string& error_msg, bool do_not_relay)
{
rpccalls rpc {deamon_url};
rpccalls rpc {bc_setup.deamon_url};
if (!rpc.commit_tx(tx_blob, error_msg, do_not_relay))
{
@ -475,7 +458,7 @@ CurrentBlockchainStatus::commit_tx(const string& tx_blob, string& error_msg, boo
bool
CurrentBlockchainStatus::read_mempool()
{
rpccalls rpc {deamon_url};
rpccalls rpc {bc_setup.deamon_url};
string error_msg;
@ -615,11 +598,11 @@ CurrentBlockchainStatus::search_if_payment_made(
// to create, so called, derived key.
key_derivation derivation;
if (!generate_key_derivation(tx_pub_key, import_payment_viewkey, derivation))
if (!generate_key_derivation(tx_pub_key, bc_setup.import_payment_viewkey, derivation))
{
cerr << "Cant get derived key for: " << "\n"
<< "pub_tx_key: " << tx_pub_key << " and "
<< "prv_view_key" << import_payment_viewkey << endl;
<< "prv_view_key" << bc_setup.import_payment_viewkey << endl;
return false;
}
@ -630,7 +613,7 @@ CurrentBlockchainStatus::search_if_payment_made(
if (decrypted_payment_id8 != null_hash8)
{
if (!mcore->get_device()->decrypt_payment_id(
decrypted_payment_id8, tx_pub_key, import_payment_viewkey))
decrypted_payment_id8, tx_pub_key, bc_setup.import_payment_viewkey))
{
cerr << "Cant decrypt decrypted_payment_id8: "
<< pod_to_hex(decrypted_payment_id8) << "\n";
@ -677,7 +660,7 @@ CurrentBlockchainStatus::search_if_payment_made(
derive_public_key(derivation,
output_idx_in_tx,
import_payment_address.address.m_spend_public_key,
bc_setup.import_payment_address.address.m_spend_public_key,
generated_tx_pubkey);
// check if generated public key matches the current output's key
@ -698,7 +681,7 @@ CurrentBlockchainStatus::search_if_payment_made(
r = decode_ringct(tx.rct_signatures,
tx_pub_key,
import_payment_viewkey,
bc_setup.import_payment_viewkey,
output_idx_in_tx,
tx.rct_signatures.ecdhInfo[output_idx_in_tx].mask,
rct_amount);
@ -784,11 +767,11 @@ CurrentBlockchainStatus::start_tx_search_thread(XmrAccount acc)
// make a tx_search object for the given xmr account
//searching_threads.emplace(acc.address, new TxSearch(acc)); // does not work on older gcc
// such as the one in ubuntu 16.04
searching_threads[acc.address] = unique_ptr<TxSearch>(new TxSearch(acc));
searching_threads[acc.address] = make_unique<TxSearch>(acc, shared_from_this());
}
catch (const std::exception& e)
{
cerr << "Faild created a search thread " << endl;
cerr << "Faild created a search thread: " << e.what() << endl;
return false;
}

@ -9,6 +9,8 @@
#include "MicroCore.h"
#include "ssqlses.h"
#include "BlockchainSetup.h"
#include "TxSearch.h"
#include <iostream>
#include <memory>
@ -21,7 +23,6 @@ namespace xmreg {
using namespace std;
class TxSearch;
class XmrAccount;
class MySqlAccounts;
@ -29,11 +30,16 @@ static mutex searching_threads_map_mtx;
static mutex getting_mempool_txs;
/*
* This is a thread class
* This is a thread class. Probably it should be singleton, as we want
* only onstance of this class, but instead I will just make one instance of
* it in main.cpp and will be injecting it around. Copies are disabled, so
* we should not make a copy by accident. Moves are enabled though.
*
* This way its much easier to mock it for unit testing.
*/
struct CurrentBlockchainStatus
class CurrentBlockchainStatus : public std::enable_shared_from_this<CurrentBlockchainStatus>
{
public:
// vector of mempool transactions that all threads
// can refer to
// recieved_time, tx
@ -43,220 +49,224 @@ struct CurrentBlockchainStatus
// tx_hash , tx, height , timestamp, is_coinbase
using txs_tuple_t = std::tuple<crypto::hash, transaction, uint64_t, uint64_t, bool>;
static string blockchain_path;
static atomic<uint64_t> current_height;
static string deamon_url;
static network_type net_type;
static bool do_not_relay;
static std::thread m_thread;
static bool is_running;
static uint64_t refresh_block_status_every_seconds;
static uint64_t blocks_search_lookahead;
static uint64_t max_number_of_blocks_to_import;
static uint64_t search_thread_life_in_seconds;
atomic<uint64_t> current_height;
static string import_payment_address_str;
static string import_payment_viewkey_str;
static uint64_t import_fee;
static uint64_t spendable_age;
static uint64_t spendable_age_coinbase;
bool is_running;
static address_parse_info import_payment_address;
static secret_key import_payment_viewkey;
CurrentBlockchainStatus(BlockchainSetup _bc_setup);
// vector of mempool transactions that all threads
// can refer to
// <recieved_time, transaction>
static mempool_txs_t 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, unique_ptr<TxSearch>> searching_threads;
// since this class monitors current status
// of the blockchain, its seems logical to
// make object for accessing the blockchain here
static unique_ptr<xmreg::MicroCore> mcore;
static cryptonote::Blockchain *core_storage;
virtual void
start_monitor_blockchain_thread();
static
void start_monitor_blockchain_thread();
static uint64_t
virtual uint64_t
get_current_blockchain_height();
static void
virtual void
update_current_blockchain_height();
static bool
virtual bool
init_monero_blockchain();
static bool
virtual bool
is_tx_unlocked(uint64_t unlock_time, uint64_t block_height);
static bool
virtual bool
is_tx_spendtime_unlocked(uint64_t unlock_time, uint64_t block_height);
static bool
virtual bool
get_block(uint64_t height, block &blk);
static vector<block>
virtual vector<block>
get_blocks_range(uint64_t const& h1, uint64_t const& h2);
static bool
virtual bool
get_block_txs(const block &blk,
vector<transaction> &blk_txs,
vector<crypto::hash>& missed_txs);
static bool
virtual bool
get_txs(vector<crypto::hash> const& txs_to_get,
vector<transaction>& txs,
vector<crypto::hash>& missed_txs);
static bool
virtual bool
tx_exist(const crypto::hash& tx_hash);
static bool
virtual bool
tx_exist(const crypto::hash& tx_hash, uint64_t& tx_index);
static bool
virtual bool
tx_exist(const string& tx_hash_str, uint64_t& tx_index);
static bool
virtual bool
tx_exist(const string& tx_hash_str);
static bool
virtual bool
get_tx_with_output(uint64_t output_idx, uint64_t amount,
transaction& tx, uint64_t& output_idx_in_tx);
static bool
virtual bool
get_output_keys(const uint64_t& amount,
const vector<uint64_t>& absolute_offsets,
vector<cryptonote::output_data_t>& outputs);
static string
virtual string
get_account_integrated_address_as_str(crypto::hash8 const& payment_id8);
static string
virtual string
get_account_integrated_address_as_str(string const& payment_id8_str);
static bool
virtual bool
get_output(const uint64_t amount,
const uint64_t global_output_index,
COMMAND_RPC_GET_OUTPUTS_BIN::outkey& output_info);
static bool
virtual bool
get_amount_specific_indices(const crypto::hash& tx_hash,
vector<uint64_t>& out_indices);
static bool
virtual bool
get_random_outputs(const vector<uint64_t>& amounts,
const uint64_t& outs_count,
vector<COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount>& found_outputs);
static bool
virtual bool
get_dynamic_per_kb_fee_estimate(uint64_t& fee_estimated);
static bool
commit_tx(const string& tx_blob, string& error_msg, bool do_not_relay = false);
virtual bool
commit_tx(const string& tx_blob, string& error_msg,
bool do_not_relay = false);
static bool
virtual bool
read_mempool();
static vector<pair<uint64_t, transaction>>
virtual vector<pair<uint64_t, transaction>>
get_mempool_txs();
static bool
virtual bool
search_if_payment_made(
const string& payment_id_str,
const uint64_t& desired_amount,
string& tx_hash_with_payment);
static string
virtual string
get_payment_id_as_string(const transaction& tx);
static output_data_t
virtual output_data_t
get_output_key(uint64_t amount,
uint64_t global_amount_index);
// definitions of these function are at the end of this file
// due to forward declaraions of TxSearch
static bool
virtual bool
start_tx_search_thread(XmrAccount acc);
static bool
virtual bool
ping_search_thread(const string& address);
static bool
virtual bool
search_thread_exist(const string& address);
static bool
virtual bool
get_xmr_address_viewkey(const string& address_str,
address_parse_info& address,
secret_key& viewkey);
static bool
virtual bool
find_txs_in_mempool(const string& address_str,
json& transactions);
static bool
virtual bool
find_tx_in_mempool(crypto::hash const& tx_hash,
transaction& tx);
static bool
virtual bool
find_key_images_in_mempool(std::vector<txin_v> const& vin);
static bool
virtual bool
find_key_images_in_mempool(transaction const& tx);
static bool
virtual bool
get_tx(crypto::hash const& tx_hash, transaction& tx);
static bool
virtual bool
get_tx(string const& tx_hash_str, transaction& tx);
static bool
virtual bool
get_tx_block_height(crypto::hash const& tx_hash, int64_t& tx_height);
static bool
virtual bool
set_new_searched_blk_no(const string& address,
uint64_t new_value);
static bool
virtual bool
get_searched_blk_no(const string& address,
uint64_t& searched_blk_no);
static bool
virtual bool
get_known_outputs_keys(string const& address,
unordered_map<public_key, uint64_t>& known_outputs_keys);
static void
virtual void
clean_search_thread_map();
/*
* The frontend requires rct field to work
* the filed consisitct of rct_pk, mask, and amount.
*/
static tuple<string, string, string>
virtual tuple<string, string, string>
construct_output_rct_field(
const uint64_t global_amount_index,
const uint64_t out_amount);
static bool
get_txs_in_blocks(vector<block> const& blocks, vector<txs_tuple_t>& txs_data);
inline virtual BlockchainSetup const&
get_bc_setup() const
{
return bc_setup;
}
virtual bool
get_txs_in_blocks(vector<block> const& blocks,
vector<txs_tuple_t>& txs_data);
// default destructor is fine
virtual ~CurrentBlockchainStatus() = default;
// we dont want any copies of the objects of this class
CurrentBlockchainStatus(CurrentBlockchainStatus const&) = delete;
CurrentBlockchainStatus& operator= (CurrentBlockchainStatus const&) = delete;
// but we want moves, default ones should be fine
CurrentBlockchainStatus(CurrentBlockchainStatus&&) = default;
CurrentBlockchainStatus& operator= (CurrentBlockchainStatus&&) = default;
protected:
// parameters used to connect/read monero blockchain
BlockchainSetup bc_setup;
// since this class monitors current status
// of the blockchain, its seems logical to
// make object for accessing the blockchain here
unique_ptr<xmreg::MicroCore> mcore;
cryptonote::Blockchain *core_storage;
// vector of mempool transactions that all threads
// can refer to
// <recieved_time, transaction>
mempool_txs_t 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.
map<string, unique_ptr<TxSearch>> searching_threads;
// thread that will be dispachaed and will keep monitoring blockchain
// and mempool changes
std::thread m_thread;
};

@ -214,7 +214,8 @@ MysqlPayments::select_by_payment_id(const string& payment_id, vector<XmrPayment>
return false;
}
MySqlAccounts::MySqlAccounts()
MySqlAccounts::MySqlAccounts(shared_ptr<CurrentBlockchainStatus> _current_bc_status)
: current_bc_status {_current_bc_status}
{
// create connection to the mysql
conn = make_shared<MySqlConnector>();
@ -222,7 +223,9 @@ MySqlAccounts::MySqlAccounts()
_init();
}
MySqlAccounts::MySqlAccounts(shared_ptr<MySqlConnector> _conn)
MySqlAccounts::MySqlAccounts(shared_ptr<CurrentBlockchainStatus> _current_bc_status,
shared_ptr<MySqlConnector> _conn)
: current_bc_status {_current_bc_status}
{
conn = _conn;
@ -484,7 +487,7 @@ MySqlAccounts::select_txs_for_account_spendability_check(
if (bool {tx.spendable} == false)
{
if (CurrentBlockchainStatus::is_tx_unlocked(tx.unlock_time, tx.height))
if (current_bc_status->is_tx_unlocked(tx.unlock_time, tx.height))
{
// this tx was before marked as unspendable, but now
// it is spendable. Meaning, that its older than 10 blocks.
@ -514,7 +517,7 @@ MySqlAccounts::select_txs_for_account_spendability_check(
uint64_t blockchain_tx_id {0};
CurrentBlockchainStatus::tx_exist(tx.hash, blockchain_tx_id);
current_bc_status->tx_exist(tx.hash, blockchain_tx_id);
if (blockchain_tx_id != tx.blockchain_tx_id)
{

@ -31,6 +31,7 @@ class XmrPayment;
class XmrAccount;
class TxSearch;
class Table;
class CurrentBlockchainStatus;
class MysqlInputs
@ -113,12 +114,14 @@ class MySqlAccounts
shared_ptr<MysqlPayments> mysql_payment;
shared_ptr<CurrentBlockchainStatus> current_bc_status;
public:
MySqlAccounts();
MySqlAccounts(shared_ptr<CurrentBlockchainStatus> _current_bc_status);
MySqlAccounts(shared_ptr<MySqlConnector> _conn);
MySqlAccounts(shared_ptr<CurrentBlockchainStatus> _current_bc_status,
shared_ptr<MySqlConnector> _conn);
bool
select(const string& address, XmrAccount& account);

@ -13,8 +13,9 @@ OutputInputIdentification::OutputInputIdentification(
const secret_key* _v,
const transaction* _tx,
crypto::hash const& _tx_hash,
bool is_coinbase)
: total_received {0}, mixin_no {0}
bool is_coinbase,
std::shared_ptr<CurrentBlockchainStatus> _current_bc_status)
: total_received {0}, mixin_no {0}, current_bc_status {_current_bc_status}
{
address_info = _a;
viewkey = _v;
@ -165,10 +166,9 @@ OutputInputIdentification::identify_inputs(
// get public keys of outputs used in the mixins that match to the offests
std::vector<cryptonote::output_data_t> mixin_outputs;
if (!CurrentBlockchainStatus::get_output_keys(in_key.amount,
absolute_offsets,
mixin_outputs))
if (!current_bc_status->get_output_keys(in_key.amount,
absolute_offsets,
mixin_outputs))
{
cerr << "Mixins key images not found" << endl;
continue;

@ -99,11 +99,14 @@ public:
vector<output_info> identified_outputs;
vector<input_info> identified_inputs;
std::shared_ptr<CurrentBlockchainStatus> current_bc_status;
OutputInputIdentification(const address_parse_info* _a,
const secret_key* _v,
const transaction* _tx,
crypto::hash const& _tx_hash,
bool is_coinbase);
bool is_coinbase,
std::shared_ptr<CurrentBlockchainStatus> _current_bc_status);
/**
* FIRST step. search for the incoming xmr using address, viewkey and

@ -15,14 +15,15 @@
namespace xmreg
{
TxSearch::TxSearch(XmrAccount& _acc)
TxSearch::TxSearch(XmrAccount& _acc, std::shared_ptr<CurrentBlockchainStatus> _current_bc_status)
: current_bc_status {_current_bc_status}
{
acc = make_shared<XmrAccount>(_acc);
// creates an mysql connection for this thread
xmr_accounts = make_shared<MySqlAccounts>();
xmr_accounts = make_shared<MySqlAccounts>(current_bc_status);
network_type net_type = CurrentBlockchainStatus::net_type;
network_type net_type = current_bc_status->get_bc_setup().net_type;
if (!xmreg::parse_str_address(acc->address, address, net_type))
{
@ -55,7 +56,7 @@ TxSearch::search()
last_ping_timestamp = current_timestamp;
uint64_t blocks_lookahead = CurrentBlockchainStatus::blocks_search_lookahead;
uint64_t blocks_lookahead = current_bc_status->get_bc_setup().blocks_search_lookahead;
// we put everything in massive catch, as there are plenty ways in which
// an exceptions can be thrown here. Mostly from mysql.
@ -68,12 +69,12 @@ TxSearch::search()
{
uint64_t loop_timestamp {current_timestamp};
uint64_t last_block_height = CurrentBlockchainStatus::current_height;
uint64_t last_block_height = current_bc_status->current_height;
uint64_t h1 = searched_blk_no;
uint64_t h2 = std::min(h1 + blocks_lookahead - 1, last_block_height);
vector<block> blocks = CurrentBlockchainStatus::get_blocks_range(h1, h2);
vector<block> blocks = current_bc_status->get_blocks_range(h1, h2);
if (blocks.empty())
{
@ -81,7 +82,7 @@ TxSearch::search()
std::this_thread::sleep_for(
std::chrono::seconds(
CurrentBlockchainStatus::refresh_block_status_every_seconds)
current_bc_status->get_bc_setup().refresh_block_status_every_seconds)
);
loop_timestamp = get_current_timestamp();
@ -111,7 +112,7 @@ TxSearch::search()
vector<CurrentBlockchainStatus::txs_tuple_t> txs_data;
if (!CurrentBlockchainStatus::get_txs_in_blocks(blocks, txs_data))
if (!current_bc_status->get_txs_in_blocks(blocks, txs_data))
{
cout << "Cant get tx in blocks from " << h1 << " to " << h2 << '\n';
return;
@ -150,13 +151,14 @@ TxSearch::search()
// Class that is responsible for identification of our outputs
// and inputs in a given tx.
OutputInputIdentification oi_identification {&address, &viewkey, &tx,
tx_hash, is_coinbase};
tx_hash, is_coinbase,
current_bc_status};
// flag indicating whether the txs in the given block are spendable.
// this is true when block number is more than 10 blocks from current
// blockchain height.
bool is_spendable = CurrentBlockchainStatus::is_tx_unlocked(
bool is_spendable = current_bc_status->is_tx_unlocked(
tx.unlock_time, blk_height);
@ -205,7 +207,7 @@ TxSearch::search()
}
if (!CurrentBlockchainStatus::tx_exist(tx_hash, blockchain_tx_id))
if (!current_bc_status->tx_exist(tx_hash, blockchain_tx_id))
{
cerr << "Tx " << oi_identification.get_tx_hash_str()
<< " " << pod_to_hex(tx_hash)
@ -240,7 +242,7 @@ TxSearch::search()
tx_data.is_rct = oi_identification.is_rct;
tx_data.rct_type = oi_identification.rct_type;
tx_data.spendable = is_spendable;
tx_data.payment_id = CurrentBlockchainStatus::get_payment_id_as_string(tx);
tx_data.payment_id = current_bc_status->get_payment_id_as_string(tx);
tx_data.mixin = oi_identification.get_mixin_no();
tx_data.timestamp = *blk_timestamp_mysql_format;
@ -249,7 +251,7 @@ TxSearch::search()
tx_mysql_id = xmr_accounts->insert(tx_data);
// get amount specific (i.e., global) indices of outputs
if (!CurrentBlockchainStatus::get_amount_specific_indices(
if (!current_bc_status->get_amount_specific_indices(
tx_hash, amount_specific_indices))
{
cerr << "cant get_amount_specific_indices!" << endl;
@ -348,7 +350,7 @@ TxSearch::search()
if (blockchain_tx_id == 0)
{
if (!CurrentBlockchainStatus::tx_exist(oi_identification.tx_hash, blockchain_tx_id))
if (!current_bc_status->tx_exist(oi_identification.tx_hash, blockchain_tx_id))
{
cerr << "Tx " << oi_identification.get_tx_hash_str()
<< "not found in blockchain !" << '\n';
@ -427,7 +429,7 @@ TxSearch::search()
tx_data.is_rct = oi_identification.is_rct;
tx_data.rct_type = oi_identification.rct_type;
tx_data.spendable = is_spendable;
tx_data.payment_id = CurrentBlockchainStatus::get_payment_id_as_string(tx);
tx_data.payment_id = current_bc_status->get_payment_id_as_string(tx);
tx_data.mixin = oi_identification.get_mixin_no();
tx_data.timestamp = *blk_timestamp_mysql_format;
@ -591,7 +593,7 @@ TxSearch::find_txs_in_mempool(
{
json j_transactions = json::array();
uint64_t current_height = CurrentBlockchainStatus::get_current_blockchain_height();
uint64_t current_height = current_bc_status->get_current_blockchain_height();
known_outputs_t known_outputs_keys_copy = get_known_outputs_keys();
@ -602,7 +604,7 @@ TxSearch::find_txs_in_mempool(
// time in a single connection.
// so we create local connection here, only to be used in this method.
shared_ptr<MySqlAccounts> local_xmr_accounts = make_shared<MySqlAccounts>();
shared_ptr<MySqlAccounts> local_xmr_accounts = make_shared<MySqlAccounts>(current_bc_status);
for (const pair<uint64_t, transaction>& mtx: mempool_txs)
{
@ -617,7 +619,8 @@ TxSearch::find_txs_in_mempool(
// Class that is resposnible for idenficitaction of our outputs
// and inputs in a given tx.
OutputInputIdentification oi_identification {&address, &viewkey, &tx,
tx_hash, coinbase};
tx_hash, coinbase,
current_bc_status};
// FIRSt step. to search for the incoming xmr, we use address, viewkey and
// outputs public key.
@ -645,7 +648,7 @@ TxSearch::find_txs_in_mempool(
// just to indicate to frontend that this
// tx is younger than 10 blocks so that
// it shows unconfirmed message.
j_tx["payment_id"] = CurrentBlockchainStatus::get_payment_id_as_string(tx);
j_tx["payment_id"] = current_bc_status->get_payment_id_as_string(tx);
j_tx["coinbase"] = false; // mempool tx are not coinbase, so always false
j_tx["is_rct"] = oi_identification.is_rct;
j_tx["rct_type"] = oi_identification.rct_type;
@ -733,7 +736,7 @@ TxSearch::find_txs_in_mempool(
// just to indicate to frontend that this
// tx is younger than 10 blocks so that
// it shows unconfirmed message.
j_tx["payment_id"] = CurrentBlockchainStatus::get_payment_id_as_string(tx);
j_tx["payment_id"] = current_bc_status->get_payment_id_as_string(tx);
j_tx["coinbase"] = false; // mempool tx are not coinbase, so always false
j_tx["is_rct"] = oi_identification.is_rct;
j_tx["rct_type"] = oi_identification.rct_type;

@ -68,7 +68,9 @@ class TxSearch
// its better to when each thread has its own mysql connection object.
// this way if one thread crashes, it want take down
// connection for the entire service
shared_ptr<MySqlAccounts> xmr_accounts;
std::shared_ptr<MySqlAccounts> xmr_accounts;
std::shared_ptr<CurrentBlockchainStatus> current_bc_status;
// address and viewkey for this search thread.
address_parse_info address;
@ -76,7 +78,7 @@ class TxSearch
public:
TxSearch(XmrAccount& _acc);
TxSearch(XmrAccount& _acc, std::shared_ptr<CurrentBlockchainStatus> _current_bc_status);
void
search();

@ -28,8 +28,10 @@ handel_::operator()(const shared_ptr< Session > session)
YourMoneroRequests::YourMoneroRequests(shared_ptr<MySqlAccounts> _acc):
xmr_accounts {_acc}
YourMoneroRequests::YourMoneroRequests(
shared_ptr<MySqlAccounts> _acc,
shared_ptr<CurrentBlockchainStatus> _current_bc_status):
xmr_accounts {_acc}, current_bc_status {_current_bc_status}
{
// mysql connection will timeout after few hours
@ -95,7 +97,7 @@ YourMoneroRequests::login(const shared_ptr<Session> session, const Bytes & body)
// createing the account
block last_blk;
if (CurrentBlockchainStatus::get_block(current_blockchain_height, last_blk))
if (current_bc_status->get_block(current_blockchain_height, last_blk))
{
current_blockchain_timestamp = last_blk.timestamp;
}
@ -307,7 +309,7 @@ YourMoneroRequests::get_address_txs(const shared_ptr< Session > session, const B
json j_mempool_tx;
if (CurrentBlockchainStatus::find_txs_in_mempool(
if (current_bc_status->find_txs_in_mempool(
xmr_address, j_mempool_tx))
{
if(!j_mempool_tx.empty())
@ -412,11 +414,11 @@ 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.
CurrentBlockchainStatus::ping_search_thread(xmr_address);
current_bc_status->ping_search_thread(xmr_address);
uint64_t current_searched_blk_no {0};
if (CurrentBlockchainStatus::get_searched_blk_no(xmr_address, current_searched_blk_no))
if (current_bc_status->get_searched_blk_no(xmr_address, current_searched_blk_no))
{
// if current_searched_blk_no is higher than what is in mysql, update it
// in the search thread. This may occure when manually editing scanned_block_height
@ -425,7 +427,7 @@ YourMoneroRequests::get_address_info(const shared_ptr< Session > session, const
if (current_searched_blk_no > acc.scanned_block_height + 10)
{
CurrentBlockchainStatus::set_new_searched_blk_no(xmr_address, acc.scanned_block_height);
current_bc_status->set_new_searched_blk_no(xmr_address, acc.scanned_block_height);
}
}
@ -568,7 +570,7 @@ YourMoneroRequests::get_unspent_outs(const shared_ptr< Session > session, const
uint64_t total_outputs_amount {0};
uint64_t current_blockchain_height
= CurrentBlockchainStatus::get_current_blockchain_height();
= current_bc_status->get_current_blockchain_height();
vector<XmrTransaction> txs;
@ -586,7 +588,7 @@ YourMoneroRequests::get_unspent_outs(const shared_ptr< Session > session, const
// thus no reason to return them to the frontend
// for constructing a tx.
if (!CurrentBlockchainStatus::is_tx_unlocked(tx.unlock_time, tx.height))
if (!current_bc_status->is_tx_unlocked(tx.unlock_time, tx.height))
{
continue;
}
@ -623,7 +625,7 @@ YourMoneroRequests::get_unspent_outs(const shared_ptr< Session > session, const
uint64_t amount = (tx.is_rct ? 0 : out.amount);
output_data_t od =
CurrentBlockchainStatus::get_output_key(
current_bc_status->get_output_key(
amount, global_amount_index);
string rtc_outpk = pod_to_hex(od.commitment);
@ -681,7 +683,7 @@ YourMoneroRequests::get_unspent_outs(const shared_ptr< Session > session, const
uint64_t fee_estimated {DYNAMIC_FEE_PER_KB_BASE_FEE};
if (CurrentBlockchainStatus::get_dynamic_per_kb_fee_estimate(fee_estimated))
if (current_bc_status->get_dynamic_per_kb_fee_estimate(fee_estimated))
{
j_response["per_kb_fee"] = fee_estimated;
}
@ -758,7 +760,7 @@ YourMoneroRequests::get_random_outs(const shared_ptr< Session > session, const B
vector<COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount> found_outputs;
if (CurrentBlockchainStatus::get_random_outputs(amounts, count, found_outputs))
if (current_bc_status->get_random_outputs(amounts, count, found_outputs))
{
json& j_amount_outs = j_response["amount_outs"];
@ -775,7 +777,7 @@ YourMoneroRequests::get_random_outs(const shared_ptr< Session > session, const B
uint64_t global_amount_index = out.global_amount_index;
tuple<string, string, string>
rct_field = CurrentBlockchainStatus::construct_output_rct_field(
rct_field = current_bc_status->construct_output_rct_field(
global_amount_index, outs.amount);
string rct = std::get<0>(rct_field) // rct_pk
@ -796,7 +798,7 @@ YourMoneroRequests::get_random_outs(const shared_ptr< Session > session, const B
} // for (const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& outs: found_outputs)
} // if (CurrentBlockchainStatus::get_random_outputs(amounts, count, found_outputs))
} // if (current_bc_status->get_random_outputs(amounts, count, found_outputs))
else
{
j_response["status"] = "error";
@ -851,7 +853,7 @@ YourMoneroRequests::submit_raw_tx(const shared_ptr< Session > session, const Byt
return;
}
if (CurrentBlockchainStatus::find_key_images_in_mempool(tx_to_be_submitted))
if (current_bc_status->find_key_images_in_mempool(tx_to_be_submitted))
{
j_response["status"] = "error";
j_response["error"] = "Tx uses your outputs that area already in the mempool. "
@ -860,9 +862,9 @@ YourMoneroRequests::submit_raw_tx(const shared_ptr< Session > session, const Byt
return;
}
if (!CurrentBlockchainStatus::commit_tx(
if (!current_bc_status->commit_tx(
raw_tx_blob, error_msg,
CurrentBlockchainStatus::do_not_relay))
current_bc_status->get_bc_setup().do_not_relay))
{
j_response["status"] = "error";
j_response["error"] = error_msg;
@ -890,17 +892,17 @@ YourMoneroRequests::import_wallet_request(const shared_ptr< Session > session, c
json j_response;
j_response["request_fulfilled"] = false;
j_response["import_fee"] = CurrentBlockchainStatus::import_fee;
j_response["import_fee"] = current_bc_status->get_bc_setup().import_fee;
j_response["status"] = "error";
j_response["error"] = "Some error occured";
// if CurrentBlockchainStatus:: is zero, we just import the wallet.
// if current_bc_status-> is zero, we just import the wallet.
// we dont care about any databases or anything, as importin all wallet is free.
// just reset the scanned block height in mysql and finish.
if (CurrentBlockchainStatus::import_fee == 0)
if (current_bc_status->get_bc_setup().import_fee == 0)
{
// change search blk number in the search thread
if (!CurrentBlockchainStatus::set_new_searched_blk_no(xmr_address, 0))
if (!current_bc_status->set_new_searched_blk_no(xmr_address, 0))
{
cerr << "Updating searched_blk_no failed!" << endl;
j_response["error"] = "Updating searched_blk_no failed!";
@ -963,7 +965,7 @@ YourMoneroRequests::import_wallet_request(const shared_ptr< Session > session, c
bool request_fulfilled = bool {xmr_payment.request_fulfilled};
string integrated_address =
CurrentBlockchainStatus::get_account_integrated_address_as_str(
current_bc_status->get_account_integrated_address_as_str(
xmr_payment.payment_id);
j_response["payment_id"] = xmr_payment.payment_id;
@ -980,7 +982,7 @@ YourMoneroRequests::import_wallet_request(const shared_ptr< Session > session, c
{
// check if it has just been done now
// if yes, mark it in mysql
if(CurrentBlockchainStatus::search_if_payment_made(
if(current_bc_status->search_if_payment_made(
xmr_payment.payment_id,
xmr_payment.import_fee,
tx_hash_with_payment))
@ -1012,7 +1014,7 @@ YourMoneroRequests::import_wallet_request(const shared_ptr< Session > session, c
request_fulfilled = true;
// change search blk number in the search thread
if (!CurrentBlockchainStatus::set_new_searched_blk_no(xmr_address, 0))
if (!current_bc_status->set_new_searched_blk_no(xmr_address, 0))
{
cerr << "Updating searched_blk_no failed!" << endl;
j_response["error"] = "Updating searched_blk_no failed!";
@ -1036,7 +1038,7 @@ YourMoneroRequests::import_wallet_request(const shared_ptr< Session > session, c
j_response["error"] = "Updating payment mysql failed!";
}
} // if(CurrentBlockchainStatus::search_if_payment_made(
} // if(current_bc_status->search_if_payment_made(
} // if (!request_fulfilled)
else
@ -1062,7 +1064,7 @@ YourMoneroRequests::import_wallet_request(const shared_ptr< Session > session, c
crypto::hash8 random_payment_id8 = crypto::rand<crypto::hash8>();
string integrated_address =
CurrentBlockchainStatus::get_account_integrated_address_as_str(
current_bc_status->get_account_integrated_address_as_str(
random_payment_id8);
XmrPayment xmr_payment;
@ -1070,7 +1072,7 @@ YourMoneroRequests::import_wallet_request(const shared_ptr< Session > session, c
xmr_payment.id = mysqlpp::null;
xmr_payment.account_id = acc.id.data;
xmr_payment.payment_id = pod_to_hex(random_payment_id8);
xmr_payment.import_fee = CurrentBlockchainStatus::import_fee; // xmr
xmr_payment.import_fee = current_bc_status->get_bc_setup().import_fee; // xmr
xmr_payment.request_fulfilled = false;
xmr_payment.tx_hash = ""; // no tx_hash yet with the payment
xmr_payment.payment_address = integrated_address;
@ -1151,10 +1153,10 @@ YourMoneroRequests::import_recent_wallet_request(const shared_ptr< Session > ses
// make sure that we dont import more that the maximum alowed no of blocks
no_blocks_to_import = std::min(no_blocks_to_import,
CurrentBlockchainStatus::max_number_of_blocks_to_import);
current_bc_status->get_bc_setup().max_number_of_blocks_to_import);
no_blocks_to_import = std::min(no_blocks_to_import,
CurrentBlockchainStatus::get_current_blockchain_height());
current_bc_status->get_current_blockchain_height());
XmrAccount acc;
@ -1175,7 +1177,7 @@ YourMoneroRequests::import_recent_wallet_request(const shared_ptr< Session > ses
if (xmr_accounts->update(acc, updated_acc))
{
// change search blk number in the search thread
if (!CurrentBlockchainStatus::set_new_searched_blk_no(xmr_address,
if (!current_bc_status->set_new_searched_blk_no(xmr_address,
updated_acc.scanned_block_height))
{
cerr << "Updating searched_blk_no failed!" << endl;
@ -1264,12 +1266,12 @@ YourMoneroRequests::get_tx(const shared_ptr< Session > session, const Bytes & bo
bool tx_in_mempool {false};
if (!CurrentBlockchainStatus::get_tx(tx_hash, tx))
if (!current_bc_status->get_tx(tx_hash, tx))
{
// if tx not found in the blockchain, check if its in mempool
vector<pair<uint64_t, transaction>> mempool_txs =
CurrentBlockchainStatus::get_mempool_txs();
current_bc_status->get_mempool_txs();
//cout << "serach mempool" << endl;
@ -1362,7 +1364,7 @@ YourMoneroRequests::get_tx(const shared_ptr< Session > session, const Bytes & bo
int64_t no_confirmations {-1};
if (CurrentBlockchainStatus::get_tx_block_height(tx_hash, tx_height))
if (current_bc_status->get_tx_block_height(tx_hash, tx_height))
{
// get the current blockchain height. Just to check
uint64_t bc_height = get_current_blockchain_height();
@ -1386,10 +1388,10 @@ YourMoneroRequests::get_tx(const shared_ptr< Session > session, const Bytes & bo
// it differently than before. Its not great, since we reinvent the wheel
// but its worth double checking the mysql data, and also allows for new
// implementation in the frontend.
if (CurrentBlockchainStatus::get_xmr_address_viewkey(xmr_address, address_info, viewkey))
if (current_bc_status->get_xmr_address_viewkey(xmr_address, address_info, viewkey))
{
OutputInputIdentification oi_identification {&address_info, &viewkey, &tx,
tx_hash, coinbase};
tx_hash, coinbase, current_bc_status};
oi_identification.identify_outputs();
@ -1484,7 +1486,7 @@ YourMoneroRequests::get_tx(const shared_ptr< Session > session, const Bytes & bo
unordered_map<public_key, uint64_t> known_outputs_keys;
if (CurrentBlockchainStatus::get_known_outputs_keys(
if (current_bc_status->get_known_outputs_keys(
xmr_address, known_outputs_keys))
{
// we got known_outputs_keys from the search thread.
@ -1494,7 +1496,7 @@ YourMoneroRequests::get_tx(const shared_ptr< Session > session, const Bytes & bo
// Class that is resposnible for idenficitaction of our outputs
// and inputs in a given tx.
OutputInputIdentification oi_identification
{&address_info, &viewkey, &tx, tx_hash, coinbase};
{&address_info, &viewkey, &tx, tx_hash, coinbase, current_bc_status};
// no need mutex here, as this will be exectued only after
// the above. there is no threads here.
@ -1532,14 +1534,14 @@ YourMoneroRequests::get_tx(const shared_ptr< Session > session, const Bytes & bo
j_response["spent_outputs"] = j_spent_outputs;
} //if (CurrentBlockchainStatus::get_known_outputs_keys(
} //if (current_bc_status->get_known_outputs_keys(
// xmr_address, known_outputs_keys))
} // else
} // if (xmr_accounts->select(xmr_address, acc))
} // if (CurrentBlockchainStatus::get_xmr_address_viewkey(address_str, address, viewkey)
} // if (current_bc_status->get_xmr_address_viewkey(address_str, address, viewkey)
j_response["tx_height"] = tx_height;
j_response["no_confirmations"] = no_confirmations;
@ -1570,8 +1572,8 @@ YourMoneroRequests::get_version(const shared_ptr< Session > session, const Bytes
{"git_branch_name" , string {GIT_BRANCH_NAME}},
{"monero_version_full" , string {MONERO_VERSION_FULL}},
{"api" , OPENMONERO_RPC_VERSION},
{"testnet" , CurrentBlockchainStatus::net_type == network_type::TESTNET},
{"network_type" , CurrentBlockchainStatus::net_type},
{"testnet" , current_bc_status->get_bc_setup().net_type == network_type::TESTNET},
{"network_type" , current_bc_status->get_bc_setup().net_type},
{"blockchain_height" , get_current_blockchain_height()}
};
@ -1652,7 +1654,7 @@ YourMoneroRequests::body_to_json(const Bytes & body)
uint64_t
YourMoneroRequests::get_current_blockchain_height()
{
return CurrentBlockchainStatus::get_current_blockchain_height();
return current_bc_status->get_current_blockchain_height();
}
bool
@ -1695,7 +1697,7 @@ YourMoneroRequests::login_and_start_search_thread(
// to do anything except looking for tx and updating mysql
// with relative tx information
if (CurrentBlockchainStatus::start_tx_search_thread(acc))
if (current_bc_status->start_tx_search_thread(acc))
{
j_response["status"] = "success";
j_response["new_address"] = false;

@ -63,11 +63,12 @@ class YourMoneroRequests
// this manages all mysql queries
shared_ptr<MySqlAccounts> xmr_accounts;
shared_ptr<CurrentBlockchainStatus> current_bc_status;
public:
YourMoneroRequests(shared_ptr<MySqlAccounts> _acc);
YourMoneroRequests(shared_ptr<MySqlAccounts> _acc,
shared_ptr<CurrentBlockchainStatus> _current_bc_status);
/**
* A login request handler.

@ -93,7 +93,7 @@ TEST(MYSQL_CONNECTION, CantConnect)
try
{
auto xmr_accounts = std::make_shared<xmreg::MySqlAccounts>();
auto xmr_accounts = std::make_shared<xmreg::MySqlAccounts>(nullptr);
}
catch(...) {
EXPECT_TRUE(true);
@ -122,7 +122,7 @@ TEST(MYSQL_CONNECTION, CanConnect)
try
{
auto xmr_accounts = std::make_shared<xmreg::MySqlAccounts>();
auto xmr_accounts = std::make_shared<xmreg::MySqlAccounts>(nullptr);
// try to connect again
// it should not perform the connection again, bust just return true;
@ -153,7 +153,7 @@ public:
new mysqlpp::MultiStatementsOption(true));
// MySqlAccounts will try connecting to the mysql database
xmr_accounts = std::make_shared<xmreg::MySqlAccounts>(conn);
xmr_accounts = std::make_shared<xmreg::MySqlAccounts>(current_bc_status, conn);
}
catch (std::exception const &e)
{
@ -217,17 +217,20 @@ protected:
virtual void SetUp()
{
current_bc_status = nullptr;
initDatabase();
}
static std::string db_data;
static json db_config;
static std::shared_ptr<xmreg::MySqlAccounts> xmr_accounts;
static std::shared_ptr<xmreg::CurrentBlockchainStatus> current_bc_status;
};
std::string MYSQL_TEST::db_data;
json MYSQL_TEST::db_config;
std::shared_ptr<xmreg::MySqlAccounts> MYSQL_TEST::xmr_accounts;
std::shared_ptr<xmreg::CurrentBlockchainStatus> MYSQL_TEST::current_bc_status {nullptr};
TEST_F(MYSQL_TEST, Connection)
@ -1160,6 +1163,9 @@ TEST_F(MYSQL_TEST, UpdatePayment)
EXPECT_EQ(payments2.at(0).tx_hash, updated_payment.tx_hash);
}
//TEST(TEST_CHAIN, GenerateTestChain)
//{
// uint64_t ts_start = 1338224400;

Loading…
Cancel
Save