Skip to content

Commit 61e6a24

Browse files
committed
fix: serialize updateNode() with mutex for Monero-family wallets
1 parent 7b1f979 commit 61e6a24

3 files changed

Lines changed: 156 additions & 132 deletions

File tree

‎lib/wallets/wallet/intermediate/lib_monero_wallet.dart‎

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -517,24 +517,46 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>
517517
return;
518518
}
519519

520-
final node = getCurrentNode();
520+
await _updateNodeMutex.protect(() async {
521+
if (wallet == null) {
522+
return;
523+
}
521524

522-
if (await _torNodeMismatchGuard(node)) {
523-
throw Exception("TOR – clearnet mismatch");
524-
}
525+
final node = getCurrentNode();
525526

526-
final host = node.host.endsWith(".onion")
527-
? node.host
528-
: Uri.parse(node.host).host;
529-
final ({InternetAddress host, int port})? proxy =
530-
AppConfig.hasFeature(AppFeature.tor) && prefs.useTor && !node.forceNoTor
531-
? TorService.sharedInstance.getProxyInfo()
532-
: null;
527+
if (await _torNodeMismatchGuard(node)) {
528+
throw Exception("TOR – clearnet mismatch");
529+
}
533530

534-
_setSyncStatus(lib_monero_compat.ConnectingSyncStatus());
535-
try {
536-
if (_requireMutex) {
537-
await _torConnectingLock.protect(() async {
531+
final host = node.host.endsWith(".onion")
532+
? node.host
533+
: Uri.parse(node.host).host;
534+
final ({InternetAddress host, int port})? proxy =
535+
AppConfig.hasFeature(AppFeature.tor) &&
536+
prefs.useTor &&
537+
!node.forceNoTor
538+
? TorService.sharedInstance.getProxyInfo()
539+
: null;
540+
541+
_setSyncStatus(lib_monero_compat.ConnectingSyncStatus());
542+
try {
543+
if (_requireMutex) {
544+
await _torConnectingLock.protect(() async {
545+
await csMonero.connect(
546+
wallet!,
547+
daemonAddress: "$host:${node.port}",
548+
daemonUsername: node.loginName,
549+
daemonPassword: await node.getPassword(secureStorageInterface),
550+
trusted: node.trusted ?? false,
551+
useSSL: node.useSSL,
552+
socksProxyAddress: node.forceNoTor
553+
? null
554+
: proxy == null
555+
? null
556+
: "${proxy.host.address}:${proxy.port}",
557+
);
558+
});
559+
} else {
538560
await csMonero.connect(
539561
wallet!,
540562
daemonAddress: "$host:${node.port}",
@@ -548,37 +570,21 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>
548570
? null
549571
: "${proxy.host.address}:${proxy.port}",
550572
);
551-
});
552-
} else {
553-
await csMonero.connect(
554-
wallet!,
555-
daemonAddress: "$host:${node.port}",
556-
daemonUsername: node.loginName,
557-
daemonPassword: await node.getPassword(secureStorageInterface),
558-
trusted: node.trusted ?? false,
559-
useSSL: node.useSSL,
560-
socksProxyAddress: node.forceNoTor
561-
? null
562-
: proxy == null
563-
? null
564-
: "${proxy.host.address}:${proxy.port}",
573+
}
574+
await csMonero.startSyncing(wallet!);
575+
await csMonero.startListeners(wallet!);
576+
csMonero.startAutoSaving(wallet!);
577+
578+
_setSyncStatus(lib_monero_compat.ConnectedSyncStatus());
579+
} catch (e, s) {
580+
_setSyncStatus(lib_monero_compat.FailedSyncStatus());
581+
Logging.instance.e(
582+
"Exception caught in $runtimeType.updateNode(): ",
583+
error: e,
584+
stackTrace: s,
565585
);
566586
}
567-
await csMonero.startSyncing(wallet!);
568-
await csMonero.startListeners(wallet!);
569-
csMonero.startAutoSaving(wallet!);
570-
571-
_setSyncStatus(lib_monero_compat.ConnectedSyncStatus());
572-
} catch (e, s) {
573-
_setSyncStatus(lib_monero_compat.FailedSyncStatus());
574-
Logging.instance.e(
575-
"Exception caught in $runtimeType.updateNode(): ",
576-
error: e,
577-
stackTrace: s,
578-
);
579-
}
580-
581-
return;
587+
});
582588
}
583589

584590
@override
@@ -1650,4 +1656,6 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>
16501656

16511657
final Mutex _torConnectingLock = Mutex();
16521658
bool _requireMutex = false;
1659+
1660+
final Mutex _updateNodeMutex = Mutex();
16531661
}

‎lib/wallets/wallet/intermediate/lib_salvium_wallet.dart‎

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -494,24 +494,46 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>
494494
return;
495495
}
496496

497-
final node = getCurrentNode();
497+
await _updateNodeMutex.protect(() async {
498+
if (wallet == null) {
499+
return;
500+
}
498501

499-
if (_torNodeMismatchGuard(node)) {
500-
throw Exception("TOR – clearnet mismatch");
501-
}
502+
final node = getCurrentNode();
502503

503-
final host = node.host.endsWith(".onion")
504-
? node.host
505-
: Uri.parse(node.host).host;
506-
final ({InternetAddress host, int port})? proxy =
507-
AppConfig.hasFeature(AppFeature.tor) && prefs.useTor && !node.forceNoTor
508-
? TorService.sharedInstance.getProxyInfo()
509-
: null;
504+
if (_torNodeMismatchGuard(node)) {
505+
throw Exception("TOR – clearnet mismatch");
506+
}
510507

511-
_setSyncStatus(ConnectingSyncStatus());
512-
try {
513-
if (_requireMutex) {
514-
await _torConnectingLock.protect(() async {
508+
final host = node.host.endsWith(".onion")
509+
? node.host
510+
: Uri.parse(node.host).host;
511+
final ({InternetAddress host, int port})? proxy =
512+
AppConfig.hasFeature(AppFeature.tor) &&
513+
prefs.useTor &&
514+
!node.forceNoTor
515+
? TorService.sharedInstance.getProxyInfo()
516+
: null;
517+
518+
_setSyncStatus(ConnectingSyncStatus());
519+
try {
520+
if (_requireMutex) {
521+
await _torConnectingLock.protect(() async {
522+
await csSalvium.connect(
523+
wallet!,
524+
daemonAddress: "$host:${node.port}",
525+
daemonUsername: node.loginName,
526+
daemonPassword: await node.getPassword(secureStorageInterface),
527+
trusted: node.trusted ?? false,
528+
useSSL: node.useSSL,
529+
socksProxyAddress: node.forceNoTor
530+
? null
531+
: proxy == null
532+
? null
533+
: "${proxy.host.address}:${proxy.port}",
534+
);
535+
});
536+
} else {
515537
await csSalvium.connect(
516538
wallet!,
517539
daemonAddress: "$host:${node.port}",
@@ -525,37 +547,21 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>
525547
? null
526548
: "${proxy.host.address}:${proxy.port}",
527549
);
528-
});
529-
} else {
530-
await csSalvium.connect(
531-
wallet!,
532-
daemonAddress: "$host:${node.port}",
533-
daemonUsername: node.loginName,
534-
daemonPassword: await node.getPassword(secureStorageInterface),
535-
trusted: node.trusted ?? false,
536-
useSSL: node.useSSL,
537-
socksProxyAddress: node.forceNoTor
538-
? null
539-
: proxy == null
540-
? null
541-
: "${proxy.host.address}:${proxy.port}",
550+
}
551+
csSalvium.startSyncing(wallet!);
552+
csSalvium.startListeners(wallet!);
553+
csSalvium.startAutoSaving(wallet!);
554+
555+
// _setSyncStatus(ConnectedSyncStatus());
556+
} catch (e, s) {
557+
// _setSyncStatus(FailedSyncStatus());
558+
Logging.instance.e(
559+
"Exception caught in $runtimeType.updateNode(): ",
560+
error: e,
561+
stackTrace: s,
542562
);
543563
}
544-
csSalvium.startSyncing(wallet!);
545-
csSalvium.startListeners(wallet!);
546-
csSalvium.startAutoSaving(wallet!);
547-
548-
// _setSyncStatus(ConnectedSyncStatus());
549-
} catch (e, s) {
550-
// _setSyncStatus(FailedSyncStatus());
551-
Logging.instance.e(
552-
"Exception caught in $runtimeType.updateNode(): ",
553-
error: e,
554-
stackTrace: s,
555-
);
556-
}
557-
558-
return;
564+
});
559565
}
560566

561567
@override
@@ -1617,6 +1623,8 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>
16171623

16181624
final Mutex _torConnectingLock = Mutex();
16191625
bool _requireMutex = false;
1626+
1627+
final Mutex _updateNodeMutex = Mutex();
16201628
}
16211629

16221630
String _libSalviumWalletPasswordKey(String walletName) =>

‎lib/wallets/wallet/intermediate/lib_wownero_wallet.dart‎

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -519,24 +519,46 @@ abstract class LibWowneroWallet<T extends CryptonoteCurrency>
519519
return;
520520
}
521521

522-
final node = getCurrentNode();
522+
await _updateNodeMutex.protect(() async {
523+
if (wallet == null) {
524+
return;
525+
}
523526

524-
if (_torNodeMismatchGuard(node)) {
525-
throw Exception("TOR – clearnet mismatch");
526-
}
527+
final node = getCurrentNode();
527528

528-
final host = node.host.endsWith(".onion")
529-
? node.host
530-
: Uri.parse(node.host).host;
531-
final ({InternetAddress host, int port})? proxy =
532-
AppConfig.hasFeature(AppFeature.tor) && prefs.useTor && !node.forceNoTor
533-
? TorService.sharedInstance.getProxyInfo()
534-
: null;
529+
if (_torNodeMismatchGuard(node)) {
530+
throw Exception("TOR – clearnet mismatch");
531+
}
535532

536-
_setSyncStatus(lib_monero_compat.ConnectingSyncStatus());
537-
try {
538-
if (_requireMutex) {
539-
await _torConnectingLock.protect(() async {
533+
final host = node.host.endsWith(".onion")
534+
? node.host
535+
: Uri.parse(node.host).host;
536+
final ({InternetAddress host, int port})? proxy =
537+
AppConfig.hasFeature(AppFeature.tor) &&
538+
prefs.useTor &&
539+
!node.forceNoTor
540+
? TorService.sharedInstance.getProxyInfo()
541+
: null;
542+
543+
_setSyncStatus(lib_monero_compat.ConnectingSyncStatus());
544+
try {
545+
if (_requireMutex) {
546+
await _torConnectingLock.protect(() async {
547+
await csWownero.connect(
548+
wallet!,
549+
daemonAddress: "$host:${node.port}",
550+
daemonUsername: node.loginName,
551+
daemonPassword: await node.getPassword(secureStorageInterface),
552+
trusted: node.trusted ?? false,
553+
useSSL: node.useSSL,
554+
socksProxyAddress: node.forceNoTor
555+
? null
556+
: proxy == null
557+
? null
558+
: "${proxy.host.address}:${proxy.port}",
559+
);
560+
});
561+
} else {
540562
await csWownero.connect(
541563
wallet!,
542564
daemonAddress: "$host:${node.port}",
@@ -550,37 +572,21 @@ abstract class LibWowneroWallet<T extends CryptonoteCurrency>
550572
? null
551573
: "${proxy.host.address}:${proxy.port}",
552574
);
553-
});
554-
} else {
555-
await csWownero.connect(
556-
wallet!,
557-
daemonAddress: "$host:${node.port}",
558-
daemonUsername: node.loginName,
559-
daemonPassword: await node.getPassword(secureStorageInterface),
560-
trusted: node.trusted ?? false,
561-
useSSL: node.useSSL,
562-
socksProxyAddress: node.forceNoTor
563-
? null
564-
: proxy == null
565-
? null
566-
: "${proxy.host.address}:${proxy.port}",
575+
}
576+
csWownero.startSyncing(wallet!);
577+
csWownero.startListeners(wallet!);
578+
csWownero.startAutoSaving(wallet!);
579+
580+
_setSyncStatus(lib_monero_compat.ConnectedSyncStatus());
581+
} catch (e, s) {
582+
_setSyncStatus(lib_monero_compat.FailedSyncStatus());
583+
Logging.instance.e(
584+
"Exception caught in $runtimeType.updateNode(): ",
585+
error: e,
586+
stackTrace: s,
567587
);
568588
}
569-
csWownero.startSyncing(wallet!);
570-
csWownero.startListeners(wallet!);
571-
csWownero.startAutoSaving(wallet!);
572-
573-
_setSyncStatus(lib_monero_compat.ConnectedSyncStatus());
574-
} catch (e, s) {
575-
_setSyncStatus(lib_monero_compat.FailedSyncStatus());
576-
Logging.instance.e(
577-
"Exception caught in $runtimeType.updateNode(): ",
578-
error: e,
579-
stackTrace: s,
580-
);
581-
}
582-
583-
return;
589+
});
584590
}
585591

586592
@override
@@ -1628,4 +1634,6 @@ abstract class LibWowneroWallet<T extends CryptonoteCurrency>
16281634

16291635
final Mutex _torConnectingLock = Mutex();
16301636
bool _requireMutex = false;
1637+
1638+
final Mutex _updateNodeMutex = Mutex();
16311639
}

0 commit comments

Comments
 (0)