Use chrono literals

new_rpc
moneroexamples 6 years ago
parent 25a2884f59
commit a42327b1fb

@ -169,7 +169,7 @@ catch(std::exception const& e)
xmreg::MysqlPing mysql_ping { xmreg::MysqlPing mysql_ping {
mysql_accounts->get_connection(), mysql_accounts->get_connection(),
bc_setup.mysql_ping_every_seconds}; bc_setup.mysql_ping_every};
xmreg::ThreadRAII mysql_ping_thread( xmreg::ThreadRAII mysql_ping_thread(
std::thread(std::ref(mysql_ping)), std::thread(std::ref(mysql_ping)),

@ -158,18 +158,18 @@ BlockchainSetup::read_config(string config_json_path)
void void
BlockchainSetup::_init() BlockchainSetup::_init()
{ {
refresh_block_status_every_seconds refresh_block_status_every
= config_json["refresh_block_status_every_seconds"]; = seconds {config_json["refresh_block_status_every_seconds"]};
blocks_search_lookahead blocks_search_lookahead
= config_json["blocks_search_lookahead"]; = config_json["blocks_search_lookahead"];
max_number_of_blocks_to_import max_number_of_blocks_to_import
= config_json["max_number_of_blocks_to_import"]; = config_json["max_number_of_blocks_to_import"];
search_thread_life_in_seconds search_thread_life
= config_json["search_thread_life_in_seconds"]; = seconds {config_json["search_thread_life_in_seconds"]};
import_fee import_fee
= config_json["wallet_import"]["fee"]; = config_json["wallet_import"]["fee"];
mysql_ping_every_seconds mysql_ping_every
= config_json["mysql_ping_every_seconds"]; = seconds {config_json["mysql_ping_every_seconds"]};
get_blockchain_path(); get_blockchain_path();

@ -17,6 +17,8 @@ using namespace crypto;
using namespace cryptonote; using namespace cryptonote;
using namespace std; using namespace std;
using chrono::seconds;
class BlockchainSetup class BlockchainSetup
{ {
public: public:
@ -37,25 +39,23 @@ public:
network_type net_type; network_type net_type;
bool do_not_relay; bool do_not_relay {false};
uint64_t refresh_block_status_every_seconds;
uint64_t blocks_search_lookahead;
uint64_t max_number_of_blocks_to_import; seconds refresh_block_status_every {10};
seconds search_thread_life {120};
seconds mysql_ping_every {300};
uint64_t search_thread_life_in_seconds; uint64_t blocks_search_lookahead {200};
uint64_t mysql_ping_every_seconds; uint64_t max_number_of_blocks_to_import {132000};
string import_payment_address_str; string import_payment_address_str;
string import_payment_viewkey_str; string import_payment_viewkey_str;
uint64_t import_fee; uint64_t import_fee {0};
uint64_t spendable_age; uint64_t spendable_age {10};
uint64_t spendable_age_coinbase; uint64_t spendable_age_coinbase {60};
address_parse_info import_payment_address; address_parse_info import_payment_address;
secret_key import_payment_viewkey; secret_key import_payment_viewkey;

@ -26,7 +26,7 @@ void
CurrentBlockchainStatus::monitor_blockchain() CurrentBlockchainStatus::monitor_blockchain()
{ {
TxSearch::set_search_thread_life( TxSearch::set_search_thread_life(
bc_setup.search_thread_life_in_seconds); bc_setup.search_thread_life);
stop_blockchain_monitor_loop = false; stop_blockchain_monitor_loop = false;
@ -49,8 +49,7 @@ CurrentBlockchainStatus::monitor_blockchain()
clean_search_thread_map(); clean_search_thread_map();
std::this_thread::sleep_for( std::this_thread::sleep_for(
std::chrono::seconds( bc_setup.refresh_block_status_every);
bc_setup.refresh_block_status_every_seconds));
} }
is_running = false; is_running = false;
@ -440,6 +439,7 @@ CurrentBlockchainStatus::get_mempool_txs()
return mempool_txs; return mempool_txs;
} }
//@todo search_if_payment_made is way too long!
bool bool
CurrentBlockchainStatus::search_if_payment_made( CurrentBlockchainStatus::search_if_payment_made(
const string& payment_id_str, const string& payment_id_str,
@ -516,7 +516,8 @@ CurrentBlockchainStatus::search_if_payment_made(
if (!hex_to_pod(tx_payment_id_str, encrypted_payment_id8)) if (!hex_to_pod(tx_payment_id_str, encrypted_payment_id8))
{ {
OMERROR << "Failed parsing hex to pod for encrypted_payment_id8"; OMERROR << "Failed parsing hex to pod for "
"encrypted_payment_id8";
continue; continue;
} }
@ -773,7 +774,8 @@ CurrentBlockchainStatus::get_known_outputs_keys(
} }
known_outputs_keys = get_search_thread(address).get_known_outputs_keys(); known_outputs_keys = get_search_thread(address)
.get_known_outputs_keys();
return true; return true;
} }
@ -802,8 +804,10 @@ CurrentBlockchainStatus::get_xmr_address_viewkey(
return false; return false;
} }
address = get_search_thread(address_str).get_xmr_address_viewkey().first; address = get_search_thread(address_str)
viewkey = get_search_thread(address_str).get_xmr_address_viewkey().second; .get_xmr_address_viewkey().first;
viewkey = get_search_thread(address_str)
.get_xmr_address_viewkey().second;
return true; return true;
} }
@ -961,7 +965,8 @@ CurrentBlockchainStatus::get_search_thread(string const& acc_address)
if (it == searching_threads.end()) if (it == searching_threads.end())
{ {
OMERROR << "Search thread does not exisit for addr: " << acc_address; OMERROR << "Search thread does not exisit for addr: "
<< acc_address;
throw std::runtime_error("Trying to accesses " throw std::runtime_error("Trying to accesses "
"non-existing search thread"); "non-existing search thread");
} }

@ -13,7 +13,7 @@ namespace xmreg
MysqlPing::MysqlPing( MysqlPing::MysqlPing(
std::shared_ptr<MySqlConnector> _conn, std::shared_ptr<MySqlConnector> _conn,
uint64_t _ping_time) seconds _ping_time)
: conn {_conn}, ping_time {_ping_time} : conn {_conn}, ping_time {_ping_time}
{} {}

@ -14,13 +14,17 @@
namespace xmreg namespace xmreg
{ {
using chrono::seconds;
using namespace literals::chrono_literals;
class MysqlPing class MysqlPing
{ {
public: public:
enum class StopReason {NotYetStopped, PingFailed, PointerExpired}; enum class StopReason {NotYetStopped, PingFailed, PointerExpired};
MysqlPing(std::shared_ptr<MySqlConnector> _conn, uint64_t _ping_time = 300); MysqlPing(std::shared_ptr<MySqlConnector> _conn,
seconds _ping_time = 300s);
void operator()(); void operator()();
void stop() {keep_looping = false;} void stop() {keep_looping = false;}
@ -33,7 +37,7 @@ public:
private: private:
std::weak_ptr<MySqlConnector> conn; std::weak_ptr<MySqlConnector> conn;
uint64_t ping_time; // in seconds seconds ping_time; // in seconds
atomic<bool> keep_looping {true}; atomic<bool> keep_looping {true};
atomic<uint64_t> counter {0}; atomic<uint64_t> counter {0};
atomic<StopReason> why_stoped {StopReason::NotYetStopped}; atomic<StopReason> why_stoped {StopReason::NotYetStopped};

@ -46,7 +46,7 @@ TxSearch::TxSearch(XmrAccount& _acc,
// this accont // this accont
searched_blk_no = acc->scanned_block_height; searched_blk_no = acc->scanned_block_height;
last_ping_timestamp = 0; last_ping_timestamp = 0s;
ping(); ping();
} }
@ -55,7 +55,7 @@ void
TxSearch::operator()() TxSearch::operator()()
{ {
uint64_t current_timestamp = get_current_timestamp(); seconds current_timestamp = get_current_timestamp();
last_ping_timestamp = current_timestamp; last_ping_timestamp = current_timestamp;
@ -71,7 +71,7 @@ TxSearch::operator()()
{ {
while(continue_search) while(continue_search)
{ {
uint64_t loop_timestamp {current_timestamp}; seconds loop_timestamp {current_timestamp};
uint64_t last_block_height = current_bc_status->current_height; uint64_t last_block_height = current_bc_status->current_height;
@ -88,7 +88,7 @@ TxSearch::operator()()
std::this_thread::sleep_for( std::this_thread::sleep_for(
std::chrono::seconds( std::chrono::seconds(
current_bc_status->get_bc_setup() current_bc_status->get_bc_setup()
.refresh_block_status_every_seconds) .refresh_block_status_every)
); );
loop_timestamp = get_current_timestamp(); loop_timestamp = get_current_timestamp();
@ -600,20 +600,21 @@ TxSearch::get_searched_blk_no() const
return searched_blk_no; return searched_blk_no;
} }
inline uint64_t inline seconds
TxSearch::get_current_timestamp() const TxSearch::get_current_timestamp() const
{ {
return chrono::duration_cast<chrono::seconds>( return chrono::duration_cast<seconds>(
chrono::system_clock::now().time_since_epoch()).count(); chrono::system_clock::now().time_since_epoch());
} }
void void
TxSearch::ping() TxSearch::ping()
{ {
OMINFO << "New last_ping_timestamp: " << last_ping_timestamp; OMINFO << "New last_ping_timestamp: "
<< last_ping_timestamp.count();
last_ping_timestamp = chrono::duration_cast<chrono::seconds>( last_ping_timestamp = chrono::duration_cast<seconds>(
chrono::system_clock::now().time_since_epoch()).count(); chrono::system_clock::now().time_since_epoch());
} }
bool bool
@ -836,7 +837,7 @@ TxSearch::get_xmr_address_viewkey() const
void void
TxSearch::set_search_thread_life(uint64_t life_seconds) TxSearch::set_search_thread_life(seconds life_seconds)
{ {
thread_search_life = life_seconds; thread_search_life = life_seconds;
} }
@ -866,6 +867,6 @@ TxSearch::delete_existing_tx_if_exists(string const& tx_hash)
// default value of static veriables // default value of static veriables
uint64_t TxSearch::thread_search_life {600}; seconds TxSearch::thread_search_life {600};
} }

@ -14,6 +14,9 @@ namespace xmreg
using namespace std; using namespace std;
using chrono::seconds;
using namespace literals::chrono_literals;
class XmrAccount; class XmrAccount;
class MySqlAccounts; class MySqlAccounts;
@ -40,7 +43,7 @@ private:
// how long should the search thread be live after no request // how long should the search thread be live after no request
// are coming from the frontend. For example, when a user finishes // are coming from the frontend. For example, when a user finishes
// using the service. // using the service.
static uint64_t thread_search_life; // in seconds static seconds thread_search_life;
bool continue_search {true}; bool continue_search {true};
@ -51,7 +54,7 @@ private:
mutex getting_eptr; mutex getting_eptr;
mutex getting_known_outputs_keys; mutex getting_known_outputs_keys;
uint64_t last_ping_timestamp; seconds last_ping_timestamp;
atomic<uint64_t> searched_blk_no; atomic<uint64_t> searched_blk_no;
@ -98,7 +101,7 @@ public:
virtual uint64_t virtual uint64_t
get_searched_blk_no() const; get_searched_blk_no() const;
virtual uint64_t virtual seconds
get_current_timestamp() const; get_current_timestamp() const;
virtual void virtual void
@ -160,7 +163,7 @@ public:
get_xmr_address_viewkey() const; get_xmr_address_viewkey() const;
static void static void
set_search_thread_life(uint64_t life_seconds); set_search_thread_life(seconds life_seconds);
virtual bool virtual bool
delete_existing_tx_if_exists(string const& tx_hash); delete_existing_tx_if_exists(string const& tx_hash);

@ -14,8 +14,6 @@ namespace
class BCSTATUS_TEST : public ::testing::TestWithParam<network_type> class BCSTATUS_TEST : public ::testing::TestWithParam<network_type>
{ {
public: public:
@ -1496,7 +1494,7 @@ TEST_P(BCSTATUS_TEST, MonitorBlockchain)
// set refresh rate to 1 second as we dont wont to wait long // set refresh rate to 1 second as we dont wont to wait long
xmreg::BlockchainSetup bs = bcs->get_bc_setup(); xmreg::BlockchainSetup bs = bcs->get_bc_setup();
bs.refresh_block_status_every_seconds = 1; bs.refresh_block_status_every = 1s;
bcs->set_bc_setup(bs); bcs->set_bc_setup(bs);
bcs->is_running = false; bcs->is_running = false;

@ -159,7 +159,7 @@ public:
db_config = readin_config(); db_config = readin_config();
if (db_config.empty()) if (db_config.empty())
FAIL() << "Cant readin_config()"; FAIL() << "Cant read in_config()";
xmreg::MySqlConnector::url = db_config["url"]; xmreg::MySqlConnector::url = db_config["url"];
xmreg::MySqlConnector::port = db_config["port"]; xmreg::MySqlConnector::port = db_config["port"];
@ -1502,7 +1502,7 @@ TEST_F(MYSQL_TEST, MysqlPingThreadStopsOnPingFailure)
auto conn = xmr_accounts->get_connection(); auto conn = xmr_accounts->get_connection();
// create ping functor that will be pinging mysql every 1 second // create ping functor that will be pinging mysql every 1 second
xmreg::MysqlPing mysql_ping {conn, 1}; xmreg::MysqlPing mysql_ping {conn, 1s};
{ {
// create ping thread and start pinging // create ping thread and start pinging
@ -1543,7 +1543,7 @@ TEST_F(MYSQL_TEST, MysqlPingThreadStopsOnPointerExpiry)
ASSERT_TRUE(new_conn->get_connection().connected()); ASSERT_TRUE(new_conn->get_connection().connected());
// create ping functor that will be pinging mysql every 1 second // create ping functor that will be pinging mysql every 1 second
xmreg::MysqlPing mysql_ping {new_conn, 1}; xmreg::MysqlPing mysql_ping {new_conn, 1s};
{ {
// create ping thread and start pinging // create ping thread and start pinging

Loading…
Cancel
Save