use last block optimization

This commit is contained in:
- 2024-12-28 14:17:06 +01:00
parent 71afc659a8
commit c2fa492480
3 changed files with 11 additions and 9 deletions

View File

@ -214,8 +214,9 @@ struct MyWalletListener : Monero::WalletListener {
/**
* @brief newBlock - called when new block received
* @param height - block height
* @param last - true if the block is the last block in the batch
*/
void newBlock(uint64_t height) {
void newBlock(uint64_t height, bool last) {
std::lock_guard<std::mutex> lock(_listenerMutex);
if (jlistener == nullptr) return;
//LOGD("newBlock");
@ -224,9 +225,10 @@ struct MyWalletListener : Monero::WalletListener {
if (envStat == JNI_ERR) return;
jlong h = static_cast<jlong>(height);
jboolean l = static_cast<jboolean>(last);
jmethodID listenerClass_newBlock = jenv->GetMethodID(class_WalletListener, "newBlock",
"(J)V");
jenv->CallVoidMethod(jlistener, listenerClass_newBlock, h);
"(JZ)V");
jenv->CallVoidMethod(jlistener, listenerClass_newBlock, h, l);
detachJVM(jenv, envStat);
}

View File

@ -45,7 +45,7 @@ interface WalletListener {
*
* @param height - block height
*/
fun newBlock(height: Long)
fun newBlock(height: Long, last: Boolean)
/**
* updated - generic callback, called when any event (sent/received/block reveived/etc) happened with the wallet;

View File

@ -467,16 +467,16 @@ class WalletService : Service(), WalletListener, DefaultLifecycleObserver {
Timber.d("Unconfirmed money received callback")
}
override fun newBlock(height: Long) {
Timber.d("New block callback at height $height")
override fun newBlock(height: Long, last: Boolean) {
Timber.d("New block callback at height $height, is last: $last")
// Monero heights are fucked up. Wallet and blockchain heights are always +1 to real height, but this callback receives real height
daemonHeight.updateAndGet { if (it > 0 && it < (height + 1)) (height + 1) else it }
val wallet = getWalletOrThrow()
if (wallet.isSynchronized) {
if (last || wallet.isSynchronized) {
Timber.d("Storing and updating wallet because it is either synchronized or the block is the last one of the batch")
wallet.store()
updateWallet(wallet)
}
// TODO: optimize to call this only on the last block of the batch
updateWallet(wallet)
}
override fun updated() {