From 5c9a902b1ff5e10f2b9d1c722ff8df442cd29d4d Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Thu, 12 Sep 2024 22:53:04 -0700 Subject: [PATCH] [skip ci] save --- cw_bitcoin/lib/litecoin_wallet.dart | 115 +++++++++++++--------------- 1 file changed, 55 insertions(+), 60 deletions(-) diff --git a/cw_bitcoin/lib/litecoin_wallet.dart b/cw_bitcoin/lib/litecoin_wallet.dart index ec73cf98f..0cde7ab03 100644 --- a/cw_bitcoin/lib/litecoin_wallet.dart +++ b/cw_bitcoin/lib/litecoin_wallet.dart @@ -244,9 +244,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { print("STARTING SYNC - MWEB ENABLED: $mwebEnabled"); syncStatus = SyncronizingSyncStatus(); await subscribeForUpdates(); - await updateTransactions(); - await updateFeeRates(); + await updateFeeRates(); _feeRatesTimer?.cancel(); _feeRatesTimer = Timer.periodic(const Duration(minutes: 1), (timer) async => await updateFeeRates()); @@ -265,74 +264,70 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { } await waitForMwebAddresses(); - await getStub(); + await processMwebUtxos(); + await updateTransactions(); await updateUnspent(); await updateBalance(); _syncTimer?.cancel(); - // delay the timer by a second so we don't overrride the restoreheight if one is set - Timer(const Duration(seconds: 2), () async { - _syncTimer = Timer.periodic(const Duration(milliseconds: 1500), (timer) async { - if (syncStatus is FailedSyncStatus) return; + _syncTimer = Timer.periodic(const Duration(milliseconds: 1500), (timer) async { + if (syncStatus is FailedSyncStatus) return; - final nodeHeight = - await electrumClient.getCurrentBlockChainTip() ?? 0; // current block height of our node - final resp = await _stub.status(StatusRequest()); + final nodeHeight = + await electrumClient.getCurrentBlockChainTip() ?? 0; // current block height of our node + final resp = await _stub.status(StatusRequest()); - if (resp.blockHeaderHeight < nodeHeight) { - int h = resp.blockHeaderHeight; - syncStatus = SyncingSyncStatus(nodeHeight - h, h / nodeHeight); - } else if (resp.mwebHeaderHeight < nodeHeight) { - int h = resp.mwebHeaderHeight; - syncStatus = SyncingSyncStatus(nodeHeight - h, h / nodeHeight); - } else if (resp.mwebUtxosHeight < nodeHeight) { - syncStatus = SyncingSyncStatus(1, 0.999); - } else { - // prevent unnecessary reaction triggers: - if (syncStatus is! SyncedSyncStatus) { - syncStatus = SyncedSyncStatus(); - } - - if (resp.mwebUtxosHeight > walletInfo.restoreHeight) { - await walletInfo.updateRestoreHeight(resp.mwebUtxosHeight); - await checkMwebUtxosSpent(); - // update the confirmations for each transaction: - for (final transaction in transactionHistory.transactions.values) { - if (transaction.isPending) continue; - int txHeight = transaction.height ?? resp.mwebUtxosHeight; - final confirmations = (resp.mwebUtxosHeight - txHeight) + 1; - if (transaction.confirmations == confirmations) continue; - transaction.confirmations = confirmations; - transactionHistory.addOne(transaction); - } - await transactionHistory.save(); + if (resp.blockHeaderHeight < nodeHeight) { + int h = resp.blockHeaderHeight; + syncStatus = SyncingSyncStatus(nodeHeight - h, h / nodeHeight); + } else if (resp.mwebHeaderHeight < nodeHeight) { + int h = resp.mwebHeaderHeight; + syncStatus = SyncingSyncStatus(nodeHeight - h, h / nodeHeight); + } else if (resp.mwebUtxosHeight < nodeHeight) { + syncStatus = SyncingSyncStatus(1, 0.999); + } else { + // prevent unnecessary reaction triggers: + if (syncStatus is! SyncedSyncStatus) { + syncStatus = SyncedSyncStatus(); + } + + if (resp.mwebUtxosHeight > walletInfo.restoreHeight) { + await walletInfo.updateRestoreHeight(resp.mwebUtxosHeight); + await checkMwebUtxosSpent(); + // update the confirmations for each transaction: + for (final transaction in transactionHistory.transactions.values) { + if (transaction.isPending) continue; + int txHeight = transaction.height ?? resp.mwebUtxosHeight; + final confirmations = (resp.mwebUtxosHeight - txHeight) + 1; + if (transaction.confirmations == confirmations) continue; + transaction.confirmations = confirmations; + transactionHistory.addOne(transaction); } + await transactionHistory.save(); } - }); - - // setup a watch dog to restart the sync process if it gets stuck: - List lastFewProgresses = []; - Timer.periodic(const Duration(seconds: 10), (timer) async { - if (syncStatus is! SyncingSyncStatus) return; - if (syncStatus.progress() > 0.98) return; - lastFewProgresses.add(syncStatus.progress()); - if (lastFewProgresses.length < 4) return; - // limit list size to 4: - while (lastFewProgresses.length > 4) { - lastFewProgresses.removeAt(0); - } - // if the progress is the same over the last 40 seconds, restart the sync: - if (lastFewProgresses.every((p) => p == lastFewProgresses.first)) { - print("mweb syncing is stuck, restarting..."); - await stopSync(); - startSync(); - timer.cancel(); - } - }); + } + }); + + // setup a watch dog to restart the sync process if it gets stuck: + List lastFewProgresses = []; + Timer.periodic(const Duration(seconds: 10), (timer) async { + if (syncStatus is! SyncingSyncStatus) return; + if (syncStatus.progress() > 0.98) return; + lastFewProgresses.add(syncStatus.progress()); + if (lastFewProgresses.length < 4) return; + // limit list size to 4: + while (lastFewProgresses.length > 4) { + lastFewProgresses.removeAt(0); + } + // if the progress is the same over the last 40 seconds, restart the sync: + if (lastFewProgresses.every((p) => p == lastFewProgresses.first)) { + print("mweb syncing is stuck, restarting..."); + await stopSync(); + startSync(); + timer.cancel(); + } }); - // this runs in the background and processes new utxos as they come in: - processMwebUtxos(); } @action