From 21e8d088df981e86f6c114faf29e3cb6cd9e2bdf Mon Sep 17 00:00:00 2001 From: Codex Bot Date: Sun, 19 Apr 2026 19:27:37 +0200 Subject: [PATCH] Reduce false local node start failures --- lib/app.dart | 29 ++++++++++++++++---- lib/services/windows_local_node_service.dart | 26 +++++++++++++++--- lib/ui/screens/settings_screen.dart | 17 ++++++++---- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/lib/app.dart b/lib/app.dart index 725d9d0..70c3859 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -147,22 +147,39 @@ class _PeyaAppState extends ConsumerState { return; } } - final attempts = config.nodeConfig.mode == NodeMode.local ? 5 : 1; + final connected = await _connectAndSyncWithRetry( + config, + attempts: config.nodeConfig.mode == NodeMode.local ? 10 : 1, + ); + if (!connected) { + ref.read(loggerProvider).w('Failed to connect node after retries.'); + } + } + + Future _connectAndSyncWithRetry( + AppConfig config, { + required int attempts, + }) async { for (var attempt = 0; attempt < attempts; attempt++) { try { await ref .read(walletControllerProvider.notifier) .connectNode(config.nodeConfig); + } catch (_) {} + final connected = await ref.read(walletRepositoryProvider).isConnected(); + if (connected) { await ref.read(walletControllerProvider.notifier).syncNow(); - return; - } catch (error) { - if (attempt + 1 >= attempts) { - ref.read(loggerProvider).w('Failed to connect node: $error'); - return; + final syncedState = ref.read(walletControllerProvider); + if (syncedState.syncStatus.lastSync != null || + await ref.read(walletRepositoryProvider).isConnected()) { + return true; } + } + if (attempt + 1 < attempts) { await Future.delayed(const Duration(seconds: 1)); } } + return false; } Future _applyStartMinimized(AppConfig config) async { diff --git a/lib/services/windows_local_node_service.dart b/lib/services/windows_local_node_service.dart index ac8f394..2dcef45 100644 --- a/lib/services/windows_local_node_service.dart +++ b/lib/services/windows_local_node_service.dart @@ -70,7 +70,20 @@ class WindowsLocalNodeService implements LocalNodeService { } _startFuture = _startNodeInternal(config); try { - return await _startFuture!; + final started = await _startFuture!; + if (started) { + return true; + } + final eventuallyAvailable = await _waitForRpc( + config, + attempts: 8, + logOnFailure: false, + ); + if (eventuallyAvailable) { + _logger.i('Local node RPC became available after delayed startup.'); + return true; + } + return false; } finally { _startFuture = null; } @@ -159,15 +172,20 @@ class WindowsLocalNodeService implements LocalNodeService { return result is int ? result : null; } - Future _waitForRpc(LocalNodeConfig config) async { - const attempts = 20; + Future _waitForRpc( + LocalNodeConfig config, { + int attempts = 20, + bool logOnFailure = true, + }) async { for (var i = 0; i < attempts; i++) { if (await _isRpcAvailable(config)) { return true; } await Future.delayed(const Duration(seconds: 1)); } - _logger.w('Local node did not become available after ${attempts}s.'); + if (logOnFailure) { + _logger.w('Local node did not become available after ${attempts}s.'); + } return false; } diff --git a/lib/ui/screens/settings_screen.dart b/lib/ui/screens/settings_screen.dart index 1564d02..c2b2137 100644 --- a/lib/ui/screens/settings_screen.dart +++ b/lib/ui/screens/settings_screen.dart @@ -391,17 +391,24 @@ class _LocalNodeControlsState extends ConsumerState<_LocalNodeControls> { final nodeConfig = config.nodeConfig; final walletState = ref.read(walletControllerProvider); if (walletState.walletInfo != null && nodeConfig.mode == NodeMode.local) { - for (var attempt = 0; attempt < 5; attempt++) { + for (var attempt = 0; attempt < 10; attempt++) { try { await ref.read(walletControllerProvider.notifier).connectNode(nodeConfig); - await ref.read(walletControllerProvider.notifier).syncNow(); - break; } catch (_) { - if (attempt == 4) { + } + final connected = await ref.read(walletRepositoryProvider).isConnected(); + if (connected) { + await ref.read(walletControllerProvider.notifier).syncNow(); + final state = ref.read(walletControllerProvider); + if (state.syncStatus.lastSync != null || + await ref.read(walletRepositoryProvider).isConnected()) { break; } - await Future.delayed(const Duration(seconds: 1)); } + if (attempt == 9) { + break; + } + await Future.delayed(const Duration(seconds: 1)); } } return ok;