CW-974: Add support for automatic node switching. (#2361)

* feat: Add support for automatic node switching.

This change:
- Adds isTrustedNodeForSwitching property to Node class.
- Adds UI Checkbox to mark a node as trusted for automatic switching in Node create or edit form.
- Implements checkNodeHealth method in wallet classes to verify node connectivity.
- Adds NodeSwitchingService for managing automatic node switching based on health checks.
- Adds setting option for user to enable/disable automated node switching

* fix(automatic-node-switch): Set node health check interval to 5 minutes in NodeSwitchingService

* refactor: Update handler method

* feat: Automatic Node Switching Improvements

This change:
- Moves enable automatic node switch to the Manage Nodes Page
- Adds check to verify node health before attempting to sync in EVM, Sol and Tron Wallets.
- Modifies trusted ndoes list for monero
- Adds trigger for Node Health Check when app resumes from background

* fix: Increase default request timeout for Tron and update health check interval for node switch

* feat: Enhance Node health check functionality.

This change adds additional checks for token balance in EVM, Solana and Tron Wallets in checkNodeHealth.

* fix: Update health check interval to 30 seconds in NodeSwitchingService

* Update bitcoin_electrum_server_list.yml

* fix: Electru wallet not connecting to new node after switching

* fix: Automated Node Switch not working on Decred Wallet

* fix: Add more nodes for AutoSwitching

This change:
- Enables more nodes across wallet types for auto switching
- Modifies CTA for checkbox
- Renames flags to fit

* fix: Add migration function for setting up default nodes for autoswitching

* fix: Review and testing fixes

* chore: Adjust balance and history fetch timer
This commit is contained in:
David Adegoke
2025-07-25 16:42:59 +03:00
committed by GitHub
parent 8f4eed4d9b
commit 4b2af63fa5
73 changed files with 639 additions and 68 deletions
@@ -32,6 +32,7 @@ class NodeForm extends StatelessWidget {
..setLogin((editingNode!.login ?? ''))
..setSSL((editingNode!.isSSL))
..setTrusted((editingNode!.trusted))
..setIsEnabledForAutoSwitching((editingNode!.isEnabledForAutoSwitching))
..setSocksProxy((editingNode!.useSocksProxy))
..setSocksProxyAddress((editingNode!.socksProxyAddress ?? ''));
}
@@ -153,6 +154,25 @@ class NodeForm extends StatelessWidget {
),
),
SizedBox(height: 10.0),
Padding(
padding: EdgeInsets.only(top: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: [
Observer(
builder: (_) => StandardCheckbox(
value: nodeViewModel.isEnabledForAutoSwitching,
borderColor: Theme.of(context).colorScheme.surfaceContainerHighest,
iconColor: Theme.of(context).colorScheme.primary,
onChanged: (value) => nodeViewModel.isEnabledForAutoSwitching = value,
caption: S.current.enable_for_auto_switching,
),
),
],
),
),
SizedBox(height: 10.0),
if (nodeViewModel.hasAuthCredentials) ...[
Row(
children: <Widget>[
+8
View File
@@ -2,8 +2,10 @@ import 'dart:async';
import 'dart:io';
import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/core/auth_service.dart';
import 'package:cake_wallet/core/node_switching_service.dart';
import 'package:cake_wallet/core/totp_request_details.dart';
import 'package:cake_wallet/core/trade_monitor.dart';
import 'package:cake_wallet/di.dart';
import 'package:cake_wallet/utils/device_info.dart';
import 'package:cake_wallet/view_model/link_view_model.dart';
import 'package:cw_core/utils/print_verbose.dart';
@@ -29,6 +31,7 @@ class Root extends StatefulWidget {
required this.authService,
required this.linkViewModel,
required this.tradeMonitor,
required this.nodeSwitchingService,
}) : super(key: key);
final AuthenticationStore authenticationStore;
@@ -38,6 +41,7 @@ class Root extends StatefulWidget {
final Widget child;
final LinkViewModel linkViewModel;
final TradeMonitor tradeMonitor;
final NodeSwitchingService nodeSwitchingService;
@override
RootState createState() => RootState();
@@ -161,6 +165,10 @@ class RootState extends State<Root> with WidgetsBindingObserver {
}
widget.tradeMonitor.resumeTradeMonitoring();
// Trigger node health check when app resumes
widget.nodeSwitchingService.performHealthCheck();
break;
default:
break;
@@ -2,6 +2,7 @@ import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/src/screens/nodes/widgets/node_list_row.dart';
import 'package:cake_wallet/src/screens/settings/widgets/settings_switcher_cell.dart';
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
import 'package:cake_wallet/src/widgets/standard_list.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
@@ -34,7 +35,23 @@ class ManageNodesPage extends BasePage {
onTap: (_) async => await Navigator.of(context).pushNamed(Routes.newNode),
),
),
SizedBox(height: 20),
Observer(
builder: (_) => SettingsSwitcherCell(
key: ValueKey('manage_nodes_page_enable_auto_node_switching_button_key'),
title: S.current.enable_auto_node_switching,
value: isPow
? powNodeListViewModel!.enableAutomaticNodeSwitching
: nodeListViewModel!.enableAutomaticNodeSwitching,
onValueChange: (BuildContext context, bool value) {
if (isPow) {
powNodeListViewModel!.setEnableAutomaticNodeSwitching(value);
} else {
nodeListViewModel!.setEnableAutomaticNodeSwitching(value);
}
},
),
),
SizedBox(height: 8),
Observer(
builder: (BuildContext context) {
int itemsCount =
@@ -36,22 +36,22 @@ class OtherSettingsPage extends BasePage {
if (_otherSettingsViewModel.displayTransactionPriority)
_otherSettingsViewModel.walletType == WalletType.bitcoin ?
SettingsPriorityPickerCell(
title: S.current.settings_fee_priority,
items: priorityForWalletType(_otherSettingsViewModel.walletType),
displayItem: _otherSettingsViewModel.getDisplayBitcoinPriority,
selectedItem: _otherSettingsViewModel.transactionPriority,
customItemIndex: _otherSettingsViewModel.customPriorityItemIndex,
onItemSelected: _otherSettingsViewModel.onDisplayBitcoinPrioritySelected,
customValue: _otherSettingsViewModel.customBitcoinFeeRate,
maxValue: _otherSettingsViewModel.maxCustomFeeRate?.toDouble(),
title: S.current.settings_fee_priority,
items: priorityForWalletType(_otherSettingsViewModel.walletType),
displayItem: _otherSettingsViewModel.getDisplayBitcoinPriority,
selectedItem: _otherSettingsViewModel.transactionPriority,
customItemIndex: _otherSettingsViewModel.customPriorityItemIndex,
onItemSelected: _otherSettingsViewModel.onDisplayBitcoinPrioritySelected,
customValue: _otherSettingsViewModel.customBitcoinFeeRate,
maxValue: _otherSettingsViewModel.maxCustomFeeRate?.toDouble(),
) :
SettingsPickerCell(
title: S.current.settings_fee_priority,
items: priorityForWalletType(_otherSettingsViewModel.walletType),
displayItem: _otherSettingsViewModel.getDisplayPriority,
selectedItem: _otherSettingsViewModel.transactionPriority,
onItemSelected: _otherSettingsViewModel.onDisplayPrioritySelected,
),
title: S.current.settings_fee_priority,
items: priorityForWalletType(_otherSettingsViewModel.walletType),
displayItem: _otherSettingsViewModel.getDisplayPriority,
selectedItem: _otherSettingsViewModel.transactionPriority,
onItemSelected: _otherSettingsViewModel.onDisplayPrioritySelected,
),
if (_otherSettingsViewModel.changeRepresentativeEnabled)
SettingsCellWithArrow(
title: S.current.change_rep,