mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-05 10:29:23 +00:00
Cw 868 fix false synchronised status when the socket connection fails due to network issues (#1892)
* prevent setting Synced status when the connection is lost * fallback for UTXO fetch failures * minor fix
This commit is contained in:
parent
20d30013d0
commit
b79ef988c8
2 changed files with 64 additions and 11 deletions
|
@ -235,7 +235,7 @@ class ElectrumClient {
|
||||||
return [];
|
return [];
|
||||||
});
|
});
|
||||||
|
|
||||||
Future<List<Map<String, dynamic>>> getListUnspent(String scriptHash) async {
|
Future<List<Map<String, dynamic>>?> getListUnspent(String scriptHash) async {
|
||||||
final result = await call(method: 'blockchain.scripthash.listunspent', params: [scriptHash]);
|
final result = await call(method: 'blockchain.scripthash.listunspent', params: [scriptHash]);
|
||||||
|
|
||||||
if (result is List) {
|
if (result is List) {
|
||||||
|
@ -248,7 +248,7 @@ class ElectrumClient {
|
||||||
}).toList();
|
}).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Map<String, dynamic>>> getMempool(String scriptHash) =>
|
Future<List<Map<String, dynamic>>> getMempool(String scriptHash) =>
|
||||||
|
|
|
@ -478,6 +478,7 @@ abstract class ElectrumWalletBase
|
||||||
if (alwaysScan == true) {
|
if (alwaysScan == true) {
|
||||||
_setListeners(walletInfo.restoreHeight);
|
_setListeners(walletInfo.restoreHeight);
|
||||||
} else {
|
} else {
|
||||||
|
if (syncStatus is LostConnectionSyncStatus) return;
|
||||||
syncStatus = SyncedSyncStatus();
|
syncStatus = SyncedSyncStatus();
|
||||||
}
|
}
|
||||||
} catch (e, stacktrace) {
|
} catch (e, stacktrace) {
|
||||||
|
@ -1361,6 +1362,10 @@ abstract class ElectrumWalletBase
|
||||||
Future<void> updateAllUnspents() async {
|
Future<void> updateAllUnspents() async {
|
||||||
List<BitcoinUnspent> updatedUnspentCoins = [];
|
List<BitcoinUnspent> updatedUnspentCoins = [];
|
||||||
|
|
||||||
|
final previousUnspentCoins = List<BitcoinUnspent>.from(unspentCoins.where((utxo) =>
|
||||||
|
utxo.bitcoinAddressRecord.type != SegwitAddresType.mweb &&
|
||||||
|
utxo.bitcoinAddressRecord is! BitcoinSilentPaymentAddressRecord));
|
||||||
|
|
||||||
if (hasSilentPaymentsScanning) {
|
if (hasSilentPaymentsScanning) {
|
||||||
// Update unspents stored from scanned silent payment transactions
|
// Update unspents stored from scanned silent payment transactions
|
||||||
transactionHistory.transactions.values.forEach((tx) {
|
transactionHistory.transactions.values.forEach((tx) {
|
||||||
|
@ -1377,13 +1382,27 @@ abstract class ElectrumWalletBase
|
||||||
if (addr is! BitcoinSilentPaymentAddressRecord) addr.balance = 0;
|
if (addr is! BitcoinSilentPaymentAddressRecord) addr.balance = 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
await Future.wait(walletAddresses.allAddresses
|
final addressFutures = walletAddresses.allAddresses
|
||||||
.where((element) => element.type != SegwitAddresType.mweb)
|
.where((element) => element.type != SegwitAddresType.mweb)
|
||||||
.map((address) async {
|
.map((address) => fetchUnspent(address))
|
||||||
updatedUnspentCoins.addAll(await fetchUnspent(address));
|
.toList();
|
||||||
}));
|
|
||||||
|
|
||||||
unspentCoins = updatedUnspentCoins;
|
final results = await Future.wait(addressFutures);
|
||||||
|
final failedCount = results.where((result) => result == null).length;
|
||||||
|
|
||||||
|
if (failedCount == 0) {
|
||||||
|
for (final result in results) {
|
||||||
|
updatedUnspentCoins.addAll(result!);
|
||||||
|
}
|
||||||
|
unspentCoins = updatedUnspentCoins;
|
||||||
|
} else {
|
||||||
|
unspentCoins = handleFailedUtxoFetch(
|
||||||
|
failedCount: failedCount,
|
||||||
|
previousUnspentCoins: previousUnspentCoins,
|
||||||
|
updatedUnspentCoins: updatedUnspentCoins,
|
||||||
|
results: results,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
final currentWalletUnspentCoins =
|
final currentWalletUnspentCoins =
|
||||||
unspentCoinsInfo.values.where((element) => element.walletId == id);
|
unspentCoinsInfo.values.where((element) => element.walletId == id);
|
||||||
|
@ -1396,6 +1415,38 @@ abstract class ElectrumWalletBase
|
||||||
await _refreshUnspentCoinsInfo();
|
await _refreshUnspentCoinsInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<BitcoinUnspent> handleFailedUtxoFetch({
|
||||||
|
required int failedCount,
|
||||||
|
required List<BitcoinUnspent> previousUnspentCoins,
|
||||||
|
required List<BitcoinUnspent> updatedUnspentCoins,
|
||||||
|
required List<List<BitcoinUnspent>?> results,
|
||||||
|
}) {
|
||||||
|
|
||||||
|
if (failedCount == results.length) {
|
||||||
|
printV("All UTXOs failed to fetch, falling back to previous UTXOs");
|
||||||
|
return previousUnspentCoins;
|
||||||
|
}
|
||||||
|
|
||||||
|
final successfulUtxos = <BitcoinUnspent>[];
|
||||||
|
for (final result in results) {
|
||||||
|
if (result != null) {
|
||||||
|
successfulUtxos.addAll(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (failedCount > 0 && successfulUtxos.isEmpty) {
|
||||||
|
printV("Some UTXOs failed, but no successful UTXOs, falling back to previous UTXOs");
|
||||||
|
return previousUnspentCoins;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (failedCount > 0) {
|
||||||
|
printV("Some UTXOs failed, updating with successful UTXOs");
|
||||||
|
updatedUnspentCoins.addAll(successfulUtxos);
|
||||||
|
}
|
||||||
|
|
||||||
|
return updatedUnspentCoins;
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> updateCoins(List<BitcoinUnspent> newUnspentCoins) async {
|
Future<void> updateCoins(List<BitcoinUnspent> newUnspentCoins) async {
|
||||||
if (newUnspentCoins.isEmpty) {
|
if (newUnspentCoins.isEmpty) {
|
||||||
return;
|
return;
|
||||||
|
@ -1427,15 +1478,17 @@ abstract class ElectrumWalletBase
|
||||||
@action
|
@action
|
||||||
Future<void> updateUnspentsForAddress(BitcoinAddressRecord address) async {
|
Future<void> updateUnspentsForAddress(BitcoinAddressRecord address) async {
|
||||||
final newUnspentCoins = await fetchUnspent(address);
|
final newUnspentCoins = await fetchUnspent(address);
|
||||||
await updateCoins(newUnspentCoins);
|
await updateCoins(newUnspentCoins ?? []);
|
||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
Future<List<BitcoinUnspent>> fetchUnspent(BitcoinAddressRecord address) async {
|
Future<List<BitcoinUnspent>?> fetchUnspent(BitcoinAddressRecord address) async {
|
||||||
List<Map<String, dynamic>> unspents = [];
|
|
||||||
List<BitcoinUnspent> updatedUnspentCoins = [];
|
List<BitcoinUnspent> updatedUnspentCoins = [];
|
||||||
|
|
||||||
unspents = await electrumClient.getListUnspent(address.getScriptHash(network));
|
final unspents = await electrumClient.getListUnspent(address.getScriptHash(network));
|
||||||
|
|
||||||
|
// Failed to fetch unspents
|
||||||
|
if (unspents == null) return null;
|
||||||
|
|
||||||
await Future.wait(unspents.map((unspent) async {
|
await Future.wait(unspents.map((unspent) async {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in a new issue