From 47efd85fd6b942e0cac2f7be5e28834e35258a37 Mon Sep 17 00:00:00 2001 From: Oscar Mira Date: Fri, 24 Feb 2023 21:33:48 +0100 Subject: [PATCH] lib: debounce blockchain height updates --- lib/android/src/main/cpp/wallet.cc | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/android/src/main/cpp/wallet.cc b/lib/android/src/main/cpp/wallet.cc index 47e81b8..2c25a06 100644 --- a/lib/android/src/main/cpp/wallet.cc +++ b/lib/android/src/main/cpp/wallet.cc @@ -1,6 +1,7 @@ #include "wallet.h" #include +#include #include #include @@ -14,6 +15,8 @@ namespace io = boost::iostreams; namespace monero { +using namespace std::chrono_literals; + static_assert(COIN == 1e12, "Monero atomic unit must be 1e-12 XMR"); static_assert(CRYPTONOTE_MAX_BLOCK_NUMBER == 500000000, "Min timestamp must be higher than max block height"); @@ -133,8 +136,13 @@ void Wallet::handleBalanceChanged(uint64_t at_block_height) { void Wallet::handleNewBlock(uint64_t height) { m_blockchain_height = height; - JNIEnv* env = getJniEnv(); - m_callback.callVoidMethod(env, Wallet_onRefresh, height, false); + // Notify blockchain height every one second. + static std::chrono::steady_clock::time_point last_time; + auto now = std::chrono::steady_clock::now(); + if (now - last_time >= 1.s) { + last_time = now; + m_callback.callVoidMethod(getJniEnv(), Wallet_onRefresh, height, false); + } } Wallet::Status Wallet::refreshLoopUntilSynced(bool skip_coinbase) { @@ -162,6 +170,8 @@ Wallet::Status Wallet::refreshLoopUntilSynced(bool skip_coinbase) { m_refresh_cond.wait(lock); } lock.unlock(); + // Always notify the last block height. + m_callback.callVoidMethod(getJniEnv(), Wallet_onRefresh, m_blockchain_height, false); return ret; }