diff --git a/cw_bitcoin/lib/electrum.dart b/cw_bitcoin/lib/electrum.dart index 983da52d2..91fb52434 100644 --- a/cw_bitcoin/lib/electrum.dart +++ b/cw_bitcoin/lib/electrum.dart @@ -356,14 +356,18 @@ class ElectrumClient { BehaviorSubject>? tipListener; int? currentTip; Future getCurrentBlockChainTip() async { - final method = 'blockchain.headers.subscribe'; - final cb = (result) => currentTip = result['height'] as int; - if (tipListener == null) { - tipListener = subscribe(id: method, method: method); - tipListener?.listen(cb); - callWithTimeout(method: method).then(cb); + try { + final method = 'blockchain.headers.subscribe'; + final cb = (result) => currentTip = result['height'] as int; + if (tipListener == null) { + tipListener = subscribe(id: method, method: method); + tipListener?.listen(cb); + callWithTimeout(method: method).then(cb); + } + return currentTip; + } catch (e) { + return null; } - return currentTip; } BehaviorSubject? chainTipSubscribe() { diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 4e35c8e1e..a007b07fe 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -1238,8 +1238,6 @@ abstract class ElectrumWalletBase try { unspents = await electrumClient.getListUnspent(address.getScriptHash(network)); } catch (e, s) { - print(e); - print(s); return []; } diff --git a/cw_bitcoin/lib/litecoin_wallet.dart b/cw_bitcoin/lib/litecoin_wallet.dart index c526bb0c0..40d79bc95 100644 --- a/cw_bitcoin/lib/litecoin_wallet.dart +++ b/cw_bitcoin/lib/litecoin_wallet.dart @@ -167,9 +167,9 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { return; } - await subscribeForUpdates(); - await updateTransactions(); - await updateFeeRates(); + // await subscribeForUpdates(); + // await updateTransactions(); + // await updateFeeRates(); Timer.periodic(const Duration(minutes: 1), (timer) async => await updateFeeRates()); @@ -177,7 +177,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { _syncTimer?.cancel(); _syncTimer = Timer.periodic(const Duration(milliseconds: 1500), (timer) async { if (syncStatus is FailedSyncStatus) return; - final height = await electrumClient.getCurrentBlockChainTip() ?? 0; + // final height = await electrumClient.getCurrentBlockChainTip() ?? 0; + final height = 0; final resp = await _stub.status(StatusRequest()); if (resp.blockHeaderHeight < height) { int h = resp.blockHeaderHeight; @@ -208,8 +209,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { } } }); - updateUnspent(); - fetchBalances(); + // updateUnspent(); + // fetchBalances(); // this runs in the background and processes new utxos as they come in: processMwebUtxos(); } @@ -541,7 +542,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { updatedUnspentCoins.add(unspent); }); } - + unspentCoins = updatedUnspentCoins; } @@ -782,4 +783,13 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { stopSync(); startSync(); } + + Future getStub() async { + return CwMweb.stub(); + } + + Future getStatusRequest() async { + final resp = await _stub.status(StatusRequest()); + return resp; + } } diff --git a/lib/bitcoin/cw_bitcoin.dart b/lib/bitcoin/cw_bitcoin.dart index 0d7d3a1f2..5bd3c77ef 100644 --- a/lib/bitcoin/cw_bitcoin.dart +++ b/lib/bitcoin/cw_bitcoin.dart @@ -597,4 +597,16 @@ class CWBitcoin extends Bitcoin { final litecoinWallet = wallet as LitecoinWallet; return litecoinWallet.mwebEnabled; } + + @override + dynamic getMwebStub(Object wallet) { + final litecoinWallet = wallet as LitecoinWallet; + return litecoinWallet.getStub(); + } + + @override + dynamic getStatusRequest(Object wallet) { + final litecoinWallet = wallet as LitecoinWallet; + return litecoinWallet.getStatusRequest(); + } } diff --git a/lib/entities/background_tasks.dart b/lib/entities/background_tasks.dart index d7a4bbc9d..c63271435 100644 --- a/lib/entities/background_tasks.dart +++ b/lib/entities/background_tasks.dart @@ -1,5 +1,7 @@ +import 'dart:async'; import 'dart:io'; +import 'package:cake_wallet/bitcoin/bitcoin.dart'; import 'package:cake_wallet/core/wallet_loading_service.dart'; import 'package:cake_wallet/entities/preferences_key.dart'; import 'package:cake_wallet/store/settings_store.dart'; @@ -25,6 +27,11 @@ void callbackDispatcher() { try { switch (task) { case mwebSyncTaskKey: + + /// The work manager runs on a separate isolate from the main flutter isolate. + /// thus we initialize app configs first; hive, getIt, etc... + await initializeAppConfigs(); + final List ltcWallets = getIt .get() .wallets @@ -39,17 +46,51 @@ void callbackDispatcher() { var wallet = await walletLoadingService.load(ltcWallets.first.type, ltcWallets.first.name); - await wallet.startSync(); + + print("STARTING SYNC FROM BG!!"); + // await wallet.startSync(); + + // RpcClient _stub = bitcoin!.getMwebStub(); + + double syncStatus = 0.0; + + Timer? _syncTimer; + + dynamic _stub = await bitcoin!.getMwebStub(wallet); + + _syncTimer = Timer.periodic(const Duration(milliseconds: 1500), (timer) async { + // if (syncStatus is FailedSyncStatus) return; + // final height = await electrumClient.getCurrentBlockChainTip() ?? 0; + final height = 0; + dynamic resp = await bitcoin!.getStatusRequest(wallet); + int blockHeaderHeight = resp.blockHeaderHeight as int; + int mwebHeaderHeight = resp.mwebHeaderHeight as int; + int mwebUtxosHeight = resp.mwebUtxosHeight as int; + + if (blockHeaderHeight < height) { + syncStatus = blockHeaderHeight / height; + } else if (mwebHeaderHeight < height) { + syncStatus = mwebHeaderHeight / height; + } else if (mwebUtxosHeight < height) { + syncStatus = 0.999; + } else { + syncStatus = 1; + } + }); for (int i = 0;; i++) { await Future.delayed(const Duration(seconds: 1)); - if (wallet.syncStatus.progress() == 1.0) { + if (syncStatus == 1) { + print("sync done!"); break; + } else { + print("Sync status ${syncStatus}"); } if (i > 600) { return Future.error("Synchronization Timed out"); } } + _syncTimer?.cancel(); break; @@ -162,7 +203,7 @@ class BackgroundTasks { requiresDeviceIdle: syncMode.type == SyncType.unobtrusive, ); - if (Platform.isIOS) { + if (Platform.isIOS && syncMode.type == SyncType.unobtrusive) { // await Workmanager().registerOneOffTask( // moneroSyncTaskKey, // moneroSyncTaskKey, @@ -174,7 +215,7 @@ class BackgroundTasks { await Workmanager().registerOneOffTask( mwebSyncTaskKey, mwebSyncTaskKey, - initialDelay: Duration(seconds: 10), + initialDelay: Duration(seconds: 30), existingWorkPolicy: ExistingWorkPolicy.replace, inputData: inputData, constraints: constraints, diff --git a/lib/entities/load_current_wallet.dart b/lib/entities/load_current_wallet.dart index 595bc2233..bd0e68ad6 100644 --- a/lib/entities/load_current_wallet.dart +++ b/lib/entities/load_current_wallet.dart @@ -24,5 +24,5 @@ Future loadCurrentWallet() async { final wallet = await walletLoadingService.load(type, name); await appStore.changeCurrentWallet(wallet); - getIt.get().registerSyncTask(); + // getIt.get().registerSyncTask(); } diff --git a/lib/view_model/settings/sync_mode.dart b/lib/view_model/settings/sync_mode.dart index ac3ac8717..1c09369f6 100644 --- a/lib/view_model/settings/sync_mode.dart +++ b/lib/view_model/settings/sync_mode.dart @@ -10,6 +10,6 @@ class SyncMode { static final all = [ SyncMode("Disabled", SyncType.disabled, Duration.zero), SyncMode("Unobtrusive", SyncType.unobtrusive, Duration(hours: 12)), - SyncMode("Aggressive", SyncType.aggressive, Duration(hours: 3)), + SyncMode("Aggressive", SyncType.aggressive, Duration(minutes: 20)), ]; }