From 3c041eed5bcb863aaa1c4705970b9a4ff608ef51 Mon Sep 17 00:00:00 2001 From: Rafael Saes Date: Sat, 11 May 2024 10:14:14 -0300 Subject: [PATCH] feat: fix resyncing --- cw_bitcoin/lib/electrum_wallet.dart | 40 ++++++++++++++--------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 0539de1a0..0a569625e 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -212,6 +212,8 @@ abstract class ElectrumWalletBase _setListeners(walletInfo.restoreHeight, chainTipParam: _currentChainTip); } } else { + alwaysScan = false; + _isolate?.then((runningIsolate) => runningIsolate.kill(priority: Isolate.immediate)); if (electrumClient.isConnected) { @@ -405,7 +407,11 @@ abstract class ElectrumWalletBase await updateFeeRates(); - syncStatus = SyncedSyncStatus(); + if (alwaysScan == true) { + _setListeners(walletInfo.restoreHeight); + } else { + syncStatus = SyncedSyncStatus(); + } } catch (e, stacktrace) { print(stacktrace); print(e.toString()); @@ -426,13 +432,6 @@ abstract class ElectrumWalletBase @action @override Future connectToNode({required Node node}) async { - final differentNode = this.node?.uri != node.uri || this.node?.useSSL != node.useSSL; - - if (differentNode) { - _scripthashesUpdateSubject = {}; - _chainTipUpdateSubject = null; - } - this.node = node; try { @@ -440,19 +439,17 @@ abstract class ElectrumWalletBase await electrumClient.close(); - await Timer(Duration(seconds: differentNode ? 0 : 15), () async { - electrumClient.onConnectionStatusChange = (bool isConnected) async { - if (syncStatus is SyncingSyncStatus) return; + electrumClient.onConnectionStatusChange = (bool isConnected) async { + if (syncStatus is SyncingSyncStatus) return; - if (isConnected && syncStatus is! SyncedSyncStatus) { - syncStatus = ConnectedSyncStatus(); - } else if (!isConnected) { - syncStatus = LostConnectionSyncStatus(); - } - }; + if (isConnected && syncStatus is! SyncedSyncStatus) { + syncStatus = ConnectedSyncStatus(); + } else if (!isConnected) { + syncStatus = LostConnectionSyncStatus(); + } + }; - await electrumClient.connectToUri(node.uri, useSSL: node.useSSL); - }); + await electrumClient.connectToUri(node.uri, useSSL: node.useSSL); } catch (e) { print(e.toString()); syncStatus = FailedSyncStatus(); @@ -1747,7 +1744,7 @@ abstract class ElectrumWalletBase Future _setInitialHeight() async { if (_chainTipUpdateSubject != null) return; - _chainTipUpdateSubject = await electrumClient.chainTipSubscribe(); + _chainTipUpdateSubject = electrumClient.chainTipSubscribe(); _chainTipUpdateSubject?.listen((e) async { final event = e as Map; final height = int.parse(event['height'].toString()); @@ -1758,7 +1755,7 @@ abstract class ElectrumWalletBase await walletInfo.updateRestoreHeight(_currentChainTip!); } - if (alwaysScan == true) { + if (alwaysScan == true && syncStatus is SyncedSyncStatus) { _setListeners(walletInfo.restoreHeight); } }); @@ -1988,6 +1985,7 @@ Future startRefresh(ScanData scanData) async { SyncedTipSyncStatus(scanData.chainTip), )); await tweaksSubscription!.close(); + await electrumClient.close(); } }); }