CW-1078: Electrum Wallets not reconencting correctly if the OS closes the connection (#2405)

* fix: Electrum Wallets not reconencting correctly if the OS closes the connection

* fix: Electrum connectivity bug. Enahnced checks and reconnect when needed, and Socket health logs page for logs

* fix: Add extra checks

* chore: Migrate to static log list instead of file storage

* fix: Electrum connectivity review comments

This change:
- Adds reset count for used trusted nodes
- Adds context guards for ScaffoldMessenger calls

* fix: Reset the state for _resetCount when there is a wallet switch
This commit is contained in:
David Adegoke
2025-07-31 17:24:54 +03:00
committed by GitHub
parent 723103d429
commit 186247d849
16 changed files with 740 additions and 17 deletions
@@ -0,0 +1,88 @@
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cw_core/wallet_type.dart';
class SocketHealthLogEntry {
final DateTime timestamp;
final WalletType? walletType;
final String? walletName;
final bool? isHealthy;
final String? error;
final String? syncStatus;
final bool wasReconnected;
final String trigger;
SocketHealthLogEntry({
required this.timestamp,
required this.walletType,
required this.walletName,
required this.isHealthy,
this.error,
this.syncStatus,
required this.wasReconnected,
required this.trigger,
});
String toLogString() {
final dateStr = timestamp.toIso8601String();
final status = isHealthy != null ? (isHealthy! ? 'HEALTHY' : 'UNHEALTHY') : 'UNKNOWN';
final reconnect = wasReconnected ? 'RECONNECTED' : 'NO_RECONNECT';
final errorStr = error != null ? ' | Error: $error' : '';
final syncStr = syncStatus != null ? ' | Sync: $syncStatus' : '';
return '[$dateStr] $trigger | ${walletType?.name ?? 'UNKNOWN'} | $walletName | $status | $reconnect$errorStr$syncStr';
}
}
class SocketHealthLogger {
static final SocketHealthLogger _instance = SocketHealthLogger._internal();
factory SocketHealthLogger() => _instance;
SocketHealthLogger._internal();
static const int _maxLogs = 100;
static final List<SocketHealthLogEntry> logs = [];
void addLog(SocketHealthLogEntry entry) {
try {
printV('Adding log entry: ${entry.toLogString()}');
logs.add(entry);
if (logs.length > _maxLogs * 2) {
final excessCount = logs.length - _maxLogs;
logs.removeRange(0, excessCount);
}
} catch (e) {
printV('Failed to add log: $e');
}
}
void clearLogs() {
logs.clear();
printV('Cleared all logs');
}
/// Log a health check with the given parameters
void logHealthCheck({
WalletType? walletType,
String? walletName,
bool? isHealthy,
String? error,
String? syncStatus,
required bool wasReconnected,
required String trigger,
}) {
addLog(
SocketHealthLogEntry(
timestamp: DateTime.now(),
walletType: walletType,
walletName: walletName,
isHealthy: isHealthy,
error: error,
syncStatus: syncStatus,
wasReconnected: wasReconnected,
trigger: trigger,
),
);
}
}
+5
View File
@@ -113,6 +113,11 @@ abstract class WalletBase<BalanceType extends Balance, HistoryType extends Trans
bool canSend() => true;
/// Check if the wallet's socket connection is healthy.
/// Returns true if the connection is alive, false otherwise.
/// Default implementation returns true (no-op for wallets without socket connections).
Future<bool> checkSocketHealth() async => true;
/// This is used to check if the current node is healthy by making a lightweight RPC call
/// Each wallet implementation should override this to make a single, efficient call
/// Returns true if the node is healthy, false otherwise