From 3c3fc3507c5cab988407e5e5ae1a060c38647861 Mon Sep 17 00:00:00 2001 From: Oscar Mira Date: Mon, 16 Oct 2023 10:46:43 +0200 Subject: [PATCH] lib: fix wallet height handling and JNI signature --- lib/android/src/main/cpp/jni_cache.cc | 2 +- lib/android/src/main/cpp/wallet.cc | 20 ++++++++++++-------- lib/android/src/main/cpp/wallet.h | 3 ++- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/android/src/main/cpp/jni_cache.cc b/lib/android/src/main/cpp/jni_cache.cc index 0534116..6a41d8d 100644 --- a/lib/android/src/main/cpp/jni_cache.cc +++ b/lib/android/src/main/cpp/jni_cache.cc @@ -40,7 +40,7 @@ void initializeJniCache(JNIEnv* env) { "callRemoteNode", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[B)Lim/molly/monero/HttpResponse;"); WalletNative_onRefresh = walletNative - .getMethodId(env, "onRefresh", "(JZ)V"); + .getMethodId(env, "onRefresh", "(IZ)V"); WalletNative_onSuspendRefresh = walletNative .getMethodId(env, "onSuspendRefresh", "(Z)V"); diff --git a/lib/android/src/main/cpp/wallet.cc b/lib/android/src/main/cpp/wallet.cc index 0a551c0..222f160 100644 --- a/lib/android/src/main/cpp/wallet.cc +++ b/lib/android/src/main/cpp/wallet.cc @@ -97,7 +97,7 @@ bool Wallet::parseFrom(std::istream& input) { return false; if (!serialization::serialize(ar, m_wallet)) return false; - m_blockchain_height = m_wallet.get_blockchain_current_height(); + set_current_blockchain_height(m_wallet.get_blockchain_current_height()); captureTxHistorySnapshot(m_tx_history); m_account_ready = true; return true; @@ -127,6 +127,12 @@ std::string Wallet::public_address() const { return account.get_public_address_str(m_wallet.nettype()); } +void Wallet::set_current_blockchain_height(uint64_t height) { + LOG_FATAL_IF(height >= CRYPTONOTE_MAX_BLOCK_NUMBER, + "Blockchain max height reached"); + m_blockchain_height = height; +} + cryptonote::account_base& Wallet::require_account() { LOG_FATAL_IF(!m_account_ready, "Account is not initialized"); return m_wallet.get_account(); @@ -317,7 +323,7 @@ void Wallet::captureTxHistorySnapshot(std::vector& snapshot) { } void Wallet::handleNewBlock(uint64_t height, bool refresh_running) { - m_blockchain_height = height; + set_current_blockchain_height(height); if (m_balance_changed) { m_tx_history_mutex.lock(); captureTxHistorySnapshot(m_tx_history); @@ -339,8 +345,9 @@ void Wallet::notifyRefresh(bool debounce) { static std::chrono::steady_clock::time_point last_time; // If debouncing is requested and the blockchain height is a multiple of 100, it limits // the notifications to once every 200 ms. + uint32_t height = current_blockchain_height(); if (debounce) { - if (m_blockchain_height % 100 == 0) { + if (height % 100 == 0) { auto now = std::chrono::steady_clock::now(); if (now - last_time >= 200.ms) { last_time = now; @@ -352,7 +359,7 @@ void Wallet::notifyRefresh(bool debounce) { } if (!debounce) { m_callback.callVoidMethod(getJniEnv(), WalletNative_onRefresh, - m_blockchain_height, m_balance_changed); + height, m_balance_changed); } } @@ -559,10 +566,7 @@ Java_im_molly_monero_WalletNative_nativeGetCurrentBlockchainHeight( jobject thiz, jlong handle) { auto* wallet = reinterpret_cast(handle); - uint64_t height = wallet->current_blockchain_height(); - LOG_FATAL_IF(height >= CRYPTONOTE_MAX_BLOCK_NUMBER, - "Blockchain max height reached"); - return static_cast(height); + return wallet->current_blockchain_height(); } ScopedJvmLocalRef nativeToJvmTxInfo(JNIEnv* env, diff --git a/lib/android/src/main/cpp/wallet.h b/lib/android/src/main/cpp/wallet.h index 9fea087..62db960 100644 --- a/lib/android/src/main/cpp/wallet.h +++ b/lib/android/src/main/cpp/wallet.h @@ -95,7 +95,8 @@ class Wallet : tools::i_wallet2_callback { std::string public_address() const; - uint64_t current_blockchain_height() const { return m_blockchain_height; } + void set_current_blockchain_height(uint64_t height); + uint32_t current_blockchain_height() const { return static_cast(m_blockchain_height); } // Extra state that must be persistent but isn't restored by wallet2's serializer. BEGIN_SERIALIZE_OBJECT()