diff --git a/lib/services/coins/epiccash/epiccash_wallet.dart b/lib/services/coins/epiccash/epiccash_wallet.dart index 08758b03f..44865e195 100644 --- a/lib/services/coins/epiccash/epiccash_wallet.dart +++ b/lib/services/coins/epiccash/epiccash_wallet.dart @@ -437,7 +437,10 @@ class EpicCashWallet extends CoinServiceAPI ), ); }); - Logging.instance.log("result?: $result", level: LogLevel.Info); + Logging.instance.log( + "cancel $txSlateId result: $result", + level: LogLevel.Info, + ); return result; } catch (e, s) { Logging.instance.log("$e, $s", level: LogLevel.Error); @@ -661,25 +664,8 @@ class EpicCashWallet extends CoinServiceAPI await epicUpdateLastScannedBlock(await getRestoreHeight()); - if (!await startScans()) { - refreshMutex = false; - GlobalEventBus.instance.fire( - NodeConnectionStatusChangedEvent( - NodeConnectionStatus.disconnected, - walletId, - coin, - ), - ); - GlobalEventBus.instance.fire( - WalletSyncStatusChangedEvent( - WalletSyncStatus.unableToSync, - walletId, - coin, - ), - ); - return; - } - await refresh(); + await _startScans(); + GlobalEventBus.instance.fire( WalletSyncStatusChangedEvent( WalletSyncStatus.synced, @@ -688,12 +674,23 @@ class EpicCashWallet extends CoinServiceAPI ), ); } catch (e, s) { + GlobalEventBus.instance.fire( + WalletSyncStatusChangedEvent( + WalletSyncStatus.unableToSync, + walletId, + coin, + ), + ); + + Logging.instance.log( + "Exception rethrown from fullRescan(): $e\n$s", + level: LogLevel.Error, + printFullLength: true, + ); + rethrow; + } finally { refreshMutex = false; - Logging.instance - .log("$e, $s", level: LogLevel.Error, printFullLength: true); } - refreshMutex = false; - return; } @override @@ -1159,58 +1156,97 @@ class EpicCashWallet extends CoinServiceAPI // TODO: refresh anything that needs to be refreshed/updated due to epicbox info changed } - Future startScans() async { + Future _startScans() async { try { + //First stop the current listener if (ListenerManager.pointer != null) { + Logging.instance + .log("LISTENER HANDLER IS NOT NULL ....", level: LogLevel.Info); + Logging.instance + .log("STOPPING ANY WALLET LISTENER ....", level: LogLevel.Info); epicboxListenerStop(ListenerManager.pointer!); } - final wallet = await _secureStore.read(key: '${_walletId}_wallet'); - var restoreHeight = epicGetRestoreHeight(); - var chainHeight = await this.chainHeight; - if (epicGetLastScannedBlock() == null) { - await epicUpdateLastScannedBlock(await getRestoreHeight()); - } - int lastScannedBlock = epicGetLastScannedBlock()!; - const MAX_PER_LOOP = 10000; - await getSyncPercent; - for (; lastScannedBlock < chainHeight;) { - chainHeight = await this.chainHeight; - lastScannedBlock = epicGetLastScannedBlock()!; - Logging.instance.log( - "chainHeight: $chainHeight, restoreHeight: $restoreHeight, lastScannedBlock: $lastScannedBlock", - level: LogLevel.Info); - int? nextScannedBlock; - await m.protect(() async { - ReceivePort receivePort = await getIsolate({ - "function": "scanOutPuts", - "wallet": wallet!, - "startHeight": lastScannedBlock, - "numberOfBlocks": MAX_PER_LOOP, - }, name: walletName); + // max number of blocks to scan per loop iteration + const scanChunkSize = 10000; - var message = await receivePort.first; - if (message is String) { - Logging.instance - .log("this is a string $message", level: LogLevel.Error); - stop(receivePort); - throw Exception("scanOutPuts isolate failed"); + // force firing of scan progress event + await getSyncPercent; + + // fetch current chain height and last scanned block (should be the + // restore height if full rescan or a wallet restore) + int chainHeight = await this.chainHeight; + int lastScannedBlock = + epicGetLastScannedBlock() ?? await getRestoreHeight(); + + // loop while scanning in chain in chunks (of blocks?) + while (lastScannedBlock < chainHeight) { + Logging.instance.log( + "chainHeight: $chainHeight, lastScannedBlock: $lastScannedBlock", + level: LogLevel.Info, + ); + + final int nextScannedBlock = await m.protect(() async { + ReceivePort? receivePort; + try { + receivePort = await getIsolate({ + "function": "scanOutPuts", + "wallet": wallet!, + "startHeight": lastScannedBlock, + "numberOfBlocks": scanChunkSize, + }, name: walletName); + + // get response + final message = await receivePort.first; + + // check for error message + if (message is String) { + throw Exception("scanOutPuts isolate failed: $message"); + } + + // attempt to grab next scanned block number + final nextScanned = int.tryParse(message['outputs'] as String); + if (nextScanned == null) { + throw Exception( + "scanOutPuts failed to parse next scanned block number from: $message", + ); + } + + return nextScanned; + } catch (_) { + rethrow; + } finally { + if (receivePort != null) { + // kill isolate + stop(receivePort); + } } - nextScannedBlock = int.parse(message['outputs'] as String); - stop(receivePort); - Logging.instance - .log('Closing scanOutPuts!\n $message', level: LogLevel.Info); }); - await epicUpdateLastScannedBlock(nextScannedBlock!); + + // update local cache + await epicUpdateLastScannedBlock(nextScannedBlock); + + // force firing of scan progress event await getSyncPercent; + + // update while loop condition variables + chainHeight = await this.chainHeight; + lastScannedBlock = nextScannedBlock; } - Logging.instance.log("successfully at the tip", level: LogLevel.Info); + + Logging.instance.log( + "_startScans successfully at the tip", + level: LogLevel.Info, + ); + //Once scanner completes restart listener await listenToEpicbox(); - return true; } catch (e, s) { - Logging.instance.log("$e, $s", level: LogLevel.Warning); - return false; + Logging.instance.log( + "_startScans failed: $e\n$s", + level: LogLevel.Error, + ); + rethrow; } } @@ -1490,24 +1526,7 @@ class EpicCashWallet extends CoinServiceAPI final int curAdd = await setCurrentIndex(); await _getReceivingAddressForIndex(curAdd); - if (!await startScans()) { - refreshMutex = false; - GlobalEventBus.instance.fire( - NodeConnectionStatusChangedEvent( - NodeConnectionStatus.disconnected, - walletId, - coin, - ), - ); - GlobalEventBus.instance.fire( - WalletSyncStatusChangedEvent( - WalletSyncStatus.unableToSync, - walletId, - coin, - ), - ); - return; - } + await _startScans(); unawaited(startSync());