From 222aa54b6111abdcbfb471cfd4ebfec53d4ccb88 Mon Sep 17 00:00:00 2001 From: androm3da Date: Wed, 31 Jan 2018 07:15:17 -0600 Subject: [PATCH] Remove QTimers Fixes #527 Credit @CathalT --- rai/node/bootstrap.cpp | 10 +-- rai/qt/qt.cpp | 197 ++++++++++++++++++++++++++--------------- 2 files changed, 133 insertions(+), 74 deletions(-) diff --git a/rai/node/bootstrap.cpp b/rai/node/bootstrap.cpp index 25060201..54b6fe39 100644 --- a/rai/node/bootstrap.cpp +++ b/rai/node/bootstrap.cpp @@ -12,7 +12,6 @@ constexpr double bootstrap_minimum_termination_time = 30.0; constexpr unsigned bootstrap_max_new_connections = 10; constexpr unsigned bootstrap_peer_frontier_minimum = rai::rai_network == rai::rai_networks::rai_live_network ? 339000 : 0; - rai::block_synchronization::block_synchronization (boost::log::sources::logger_mt & log_a) : log (log_a) { @@ -970,14 +969,15 @@ struct block_rate_cmp unsigned rai::bootstrap_attempt::target_connections (size_t pulls_remaining) { - if (node->config.bootstrap_connections >= node->config.bootstrap_connections_max) { - return std::max(1U, node->config.bootstrap_connections_max); + if (node->config.bootstrap_connections >= node->config.bootstrap_connections_max) + { + return std::max (1U, node->config.bootstrap_connections_max); } - + // Only scale up to bootstrap_connections_max for large pulls. double step = std::min (1.0, std::max (0.0, (double)pulls_remaining / bootstrap_connection_scale_target)); double target = (double)node->config.bootstrap_connections + (double)(node->config.bootstrap_connections_max - node->config.bootstrap_connections) * step; - return std::max(1U, (unsigned)(target + 0.5f)); + return std::max (1U, (unsigned)(target + 0.5f)); } void rai::bootstrap_attempt::populate_connections () diff --git a/rai/qt/qt.cpp b/rai/qt/qt.cpp index 1a01a4e6..3b683713 100644 --- a/rai/qt/qt.cpp +++ b/rai/qt/qt.cpp @@ -95,8 +95,10 @@ wallet (wallet_a) QObject::connect (copy_button, &QPushButton::clicked, [this]() { this->wallet.application.clipboard ()->setText (QString (this->wallet.account.to_account ().c_str ())); copy_button->setText ("Copied!"); - QTimer::singleShot (std::chrono::milliseconds (2000).count (), [this]() { - copy_button->setText ("Copy"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (2), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + copy_button->setText ("Copy"); + })); }); }); } @@ -186,18 +188,22 @@ wallet (wallet_a) show_button_success (*create_account); create_account->setText ("New account was created"); refresh (); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this]() { - show_button_ok (*create_account); - create_account->setText ("Create account"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_button_ok (*create_account); + create_account->setText ("Create account"); + })); }); } else { show_button_error (*create_account); create_account->setText ("Wallet is locked, unlock it to create account"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this]() { - show_button_ok (*create_account); - create_account->setText ("Create account"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_button_ok (*create_account); + create_account->setText ("Create account"); + })); }); } }); @@ -213,9 +219,11 @@ wallet (wallet_a) this->wallet.application.clipboard ()->setText (QString (seed.data.to_string ().c_str ())); show_button_success (*backup_seed); backup_seed->setText ("Seed was copied to clipboard"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this]() { - show_button_ok (*backup_seed); - backup_seed->setText ("Copy wallet seed to clipboard"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_button_ok (*backup_seed); + backup_seed->setText ("Copy wallet seed to clipboard"); + })); }); } else @@ -223,9 +231,11 @@ wallet (wallet_a) this->wallet.application.clipboard ()->setText (""); show_button_error (*backup_seed); backup_seed->setText ("Wallet is locked, unlock it to enable the backup"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this]() { - show_button_ok (*backup_seed); - backup_seed->setText ("Copy wallet seed to clipboard"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_button_ok (*backup_seed); + backup_seed->setText ("Copy wallet seed to clipboard"); + })); }); } }); @@ -373,10 +383,12 @@ wallet (wallet_a) show_line_error (*seed); show_button_error (*import_seed); import_seed->setText ("Wallet is locked, unlock it to enable the import"); - QTimer::singleShot (std::chrono::milliseconds (10000).count (), [this]() { - show_line_ok (*seed); - show_button_ok (*import_seed); - import_seed->setText ("Import seed"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (10), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_line_ok (*seed); + show_button_ok (*import_seed); + import_seed->setText ("Import seed"); + })); }); } } @@ -410,9 +422,11 @@ wallet (wallet_a) show_button_success (*import_seed); import_seed->setText ("Successful import of seed"); this->wallet.refresh (); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this]() { - show_button_ok (*import_seed); - import_seed->setText ("Import seed"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_button_ok (*import_seed); + import_seed->setText ("Import seed"); + })); }); } } @@ -428,9 +442,11 @@ wallet (wallet_a) { import_seed->setText ("Incorrect seed. Only HEX characters allowed"); } - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this]() { - show_button_ok (*import_seed); - import_seed->setText ("Import seed"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_button_ok (*import_seed); + import_seed->setText ("Import seed"); + })); }); } } @@ -439,9 +455,11 @@ wallet (wallet_a) show_line_error (*clear_line); show_button_error (*import_seed); import_seed->setText ("Type words 'clear keys'"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this]() { - show_button_ok (*import_seed); - import_seed->setText ("Import seed"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_button_ok (*import_seed); + import_seed->setText ("Import seed"); + })); }); } }); @@ -635,8 +653,10 @@ void rai_qt::block_viewer::rebroadcast_action (rai::uint256_union const & hash_a if (!successor.is_zero ()) { done = false; - QTimer::singleShot (std::chrono::milliseconds (1000).count (), [this, successor]() { - rebroadcast_action (successor); + wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (1), [this, successor]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this, successor]() { + rebroadcast_action (successor); + })); }); } } @@ -971,11 +991,16 @@ void rai_qt::wallet::start () { show_button_error (*this_l->send_blocks_send); this_l->send_blocks_send->setText ("Wallet is locked, unlock it to send"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this_w]() { + this_l->node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this_w]() { if (auto this_l = this_w.lock ()) { - show_button_ok (*this_l->send_blocks_send); - this_l->send_blocks_send->setText ("Send"); + this_l->application.postEvent (&this_l->processor, new eventloop_event ([this_w]() { + if (auto this_l = this_w.lock ()) + { + show_button_ok (*this_l->send_blocks_send); + this_l->send_blocks_send->setText ("Send"); + } + })); } }); } @@ -985,11 +1010,16 @@ void rai_qt::wallet::start () show_line_error (*this_l->send_count); show_button_error (*this_l->send_blocks_send); this_l->send_blocks_send->setText ("Not enough balance"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this_w]() { + this_l->node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this_w]() { if (auto this_l = this_w.lock ()) { - show_button_ok (*this_l->send_blocks_send); - this_l->send_blocks_send->setText ("Send"); + this_l->application.postEvent (&this_l->processor, new eventloop_event ([this_w]() { + if (auto this_l = this_w.lock ()) + { + show_button_ok (*this_l->send_blocks_send); + this_l->send_blocks_send->setText ("Send"); + } + })); } }); } @@ -999,11 +1029,16 @@ void rai_qt::wallet::start () show_line_error (*this_l->send_account); show_button_error (*this_l->send_blocks_send); this_l->send_blocks_send->setText ("Bad destination account"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this_w]() { + this_l->node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this_w]() { if (auto this_l = this_w.lock ()) { - show_button_ok (*this_l->send_blocks_send); - this_l->send_blocks_send->setText ("Send"); + this_l->application.postEvent (&this_l->processor, new eventloop_event ([this_w]() { + if (auto this_l = this_w.lock ()) + { + show_button_ok (*this_l->send_blocks_send); + this_l->send_blocks_send->setText ("Send"); + } + })); } }); } @@ -1013,12 +1048,20 @@ void rai_qt::wallet::start () show_line_error (*this_l->send_count); show_button_error (*this_l->send_blocks_send); this_l->send_blocks_send->setText ("Amount too big"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this_w]() { + this_l->node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this_w]() { if (auto this_l = this_w.lock ()) { show_line_ok (*this_l->send_account); show_button_ok (*this_l->send_blocks_send); this_l->send_blocks_send->setText ("Send"); + this_l->application.postEvent (&this_l->processor, new eventloop_event ([this_w]() { + if (auto this_l = this_w.lock ()) + { + show_line_ok (*this_l->send_account); + show_button_ok (*this_l->send_blocks_send); + this_l->send_blocks_send->setText ("Send"); + } + })); } }); } @@ -1028,11 +1071,16 @@ void rai_qt::wallet::start () show_line_error (*this_l->send_count); show_button_error (*this_l->send_blocks_send); this_l->send_blocks_send->setText ("Bad amount number"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this_w]() { + this_l->node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this_w]() { if (auto this_l = this_w.lock ()) { - show_button_ok (*this_l->send_blocks_send); - this_l->send_blocks_send->setText ("Send"); + this_l->application.postEvent (&this_l->processor, new eventloop_event ([this_w]() { + if (auto this_l = this_w.lock ()) + { + show_button_ok (*this_l->send_blocks_send); + this_l->send_blocks_send->setText ("Send"); + } + })); } }); } @@ -1304,9 +1352,11 @@ wallet (wallet_a) show_button_success (*change); change->setText ("Password was changed"); update_locked (false, false); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this]() { - show_button_ok (*change); - change->setText ("Set/Change password"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_button_ok (*change); + change->setText ("Set/Change password"); + })); }); } else @@ -1320,9 +1370,11 @@ wallet (wallet_a) { show_button_error (*change); change->setText ("Wallet is locked, unlock it"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this]() { - show_button_ok (*change); - change->setText ("Set/Change password"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_button_ok (*change); + change->setText ("Set/Change password"); + })); }); } }); @@ -1341,21 +1393,25 @@ wallet (wallet_a) auto block (this->wallet.wallet_m->change_sync (this->wallet.account, representative_l)); change_rep->setEnabled (true); show_button_success (*change_rep); - change_rep->setText ("Representative was changed"); + change_rep->setText ("Represenative was changed"); current_representative->setText (QString (representative_l.to_account_split ().c_str ())); new_representative->clear (); - QTimer::singleShot (std::chrono::seconds (5000).count (), [this]() { - show_button_ok (*change_rep); - change_rep->setText ("Change representative"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_button_ok (*change_rep); + change_rep->setText ("Change representative"); + })); }); } else { show_button_error (*change_rep); change_rep->setText ("Wallet is locked, unlock it"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this]() { - show_button_ok (*change_rep); - change_rep->setText ("Change representative"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_button_ok (*change_rep); + change_rep->setText ("Change representative"); + })); }); } } @@ -1364,10 +1420,12 @@ wallet (wallet_a) show_line_error (*new_representative); show_button_error (*change_rep); change_rep->setText ("Invalid account"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this]() { - show_line_ok (*new_representative); - show_button_ok (*change_rep); - change_rep->setText ("Change representative"); + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_line_ok (*new_representative); + show_button_ok (*change_rep); + change_rep->setText ("Change representative"); + })); }); } }); @@ -1401,17 +1459,18 @@ wallet (wallet_a) show_line_error (*password); show_button_error (*lock_toggle); lock_toggle->setText ("Invalid password"); - QTimer::singleShot (std::chrono::milliseconds (5000).count (), [this]() { - show_line_ok (*password); - show_button_ok (*lock_toggle); - - // if wallet is still not unlocked by now, change button text - rai::transaction transaction (this->wallet.wallet_m->store.environment, nullptr, true); - if (!this->wallet.wallet_m->store.valid_password (transaction)) - { - lock_toggle->setText ("Unlock"); - } + this->wallet.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [this]() { + this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this]() { + show_line_ok (*password); + show_button_ok (*lock_toggle); + // if wallet is still not unlocked by now, change button text + rai::transaction transaction (this->wallet.wallet_m->store.environment, nullptr, true); + if (!this->wallet.wallet_m->store.valid_password (transaction)) + { + lock_toggle->setText ("Unlock"); + } + })); }); } }