Skip to content

Node handling updates and fixes #1151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/db/db_version_migration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import '../models/isar/models/blockchain_data/address.dart';
import '../models/isar/models/contact_entry.dart' as isar_contact;
import '../models/isar/models/isar_models.dart' as isar_models;
import '../models/models.dart';
import '../models/node_model.dart';
import '../services/mixins/wallet_db.dart';
import '../services/wallets_service.dart';
import '../utilities/amount/amount.dart';
Expand Down Expand Up @@ -361,6 +362,20 @@ class DbVersionMigrator with WalletDB {
// try to continue migrating
return await migrate(14, secureStore: secureStore);

case 14:
// migrate
await _v14();

// update version
await DB.instance.put<dynamic>(
boxName: DB.boxNameDBInfo,
key: "hive_data_version",
value: 15,
);

// try to continue migrating
return await migrate(15, secureStore: secureStore);

default:
// finally return
return;
Expand Down Expand Up @@ -632,4 +647,28 @@ class DbVersionMigrator with WalletDB {
await isar.close(deleteFromDisk: true);
}
}

Future<void> _v14() async {
final nodesBox = await DB.instance.hive.openBox<NodeModel>(
DB.boxNameNodeModels,
);
final primariesBox = await DB.instance.hive.openBox<NodeModel>(
DB.boxNamePrimaryNodesDeprecated,
);

final primaries = primariesBox.values;

for (final node in primaries) {
await nodesBox.put(
node.id,
node.copyWith(
loginName: node.loginName,
trusted: node.trusted,
isPrimary: true,
),
);
}

await primariesBox.deleteFromDisk();
}
}
80 changes: 39 additions & 41 deletions lib/db/hive/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class DB {
static const String boxNameTrades = "exchangeTransactionsBox";
static const String boxNameAllWalletsData = "wallets";
static const String boxNameFavoriteWallets = "favoriteWallets";
static const String boxNamePrimaryNodesDeprecated = "primaryNodes";

// in use
// TODO: migrate
static const String boxNameNodeModels = "nodeModels";
static const String boxNamePrimaryNodes = "primaryNodes";
static const String boxNameNotifications = "notificationModels";
static const String boxNameWatchedTransactions =
"watchedTxNotificationModels";
Expand Down Expand Up @@ -127,29 +127,27 @@ class DB {
_boxNodeModels = await hive.openBox<NodeModel>(boxNameNodeModels);
}

if (hive.isBoxOpen(boxNamePrimaryNodes)) {
_boxPrimaryNodes = hive.box<NodeModel>(boxNamePrimaryNodes);
} else {
_boxPrimaryNodes = await hive.openBox<NodeModel>(boxNamePrimaryNodes);
}

if (hive.isBoxOpen(boxNameAllWalletsData)) {
_boxAllWalletsData = hive.box<dynamic>(boxNameAllWalletsData);
} else {
_boxAllWalletsData = await hive.openBox<dynamic>(boxNameAllWalletsData);
}

_boxNotifications =
await hive.openBox<NotificationModel>(boxNameNotifications);
_boxWatchedTransactions =
await hive.openBox<NotificationModel>(boxNameWatchedTransactions);
_boxWatchedTrades =
await hive.openBox<NotificationModel>(boxNameWatchedTrades);
_boxNotifications = await hive.openBox<NotificationModel>(
boxNameNotifications,
);
_boxWatchedTransactions = await hive.openBox<NotificationModel>(
boxNameWatchedTransactions,
);
_boxWatchedTrades = await hive.openBox<NotificationModel>(
boxNameWatchedTrades,
);
_boxTradesV2 = await hive.openBox<Trade>(boxNameTradesV2);
_boxTradeNotes = await hive.openBox<String>(boxNameTradeNotes);
_boxTradeLookup = await hive.openBox<TradeWalletLookup>(boxNameTradeLookup);
_walletInfoSource = await hive.openBox<lib_monero_compat.WalletInfo>(
lib_monero_compat.WalletInfo.boxName);
lib_monero_compat.WalletInfo.boxName,
);
_boxFavoriteWallets = await hive.openBox<String>(boxNameFavoriteWallets);

await Future.wait([
Expand Down Expand Up @@ -183,11 +181,13 @@ class DB {

for (final entry in mapped.entries) {
if (hive.isBoxOpen(entry.value.walletId)) {
_walletBoxes[entry.value.walletId] =
hive.box<dynamic>(entry.value.walletId);
_walletBoxes[entry.value.walletId] = hive.box<dynamic>(
entry.value.walletId,
);
} else {
_walletBoxes[entry.value.walletId] =
await hive.openBox<dynamic>(entry.value.walletId);
_walletBoxes[entry.value.walletId] = await hive.openBox<dynamic>(
entry.value.walletId,
);
}
}
}
Expand All @@ -196,8 +196,9 @@ class DB {
if (_txCacheBoxes[currency.identifier]?.isOpen != true) {
_txCacheBoxes.remove(currency.identifier);
}
return _txCacheBoxes[currency.identifier] ??=
await hive.openBox<dynamic>(_boxNameTxCache(currency: currency));
return _txCacheBoxes[currency.identifier] ??= await hive.openBox<dynamic>(
_boxNameTxCache(currency: currency),
);
}

Future<void> closeTxCacheBox({required CryptoCurrency currency}) async {
Expand All @@ -210,8 +211,9 @@ class DB {
if (_setCacheBoxes[currency.identifier]?.isOpen != true) {
_setCacheBoxes.remove(currency.identifier);
}
return _setCacheBoxes[currency.identifier] ??=
await hive.openBox<dynamic>(_boxNameSetCache(currency: currency));
return _setCacheBoxes[currency.identifier] ??= await hive.openBox<dynamic>(
_boxNameSetCache(currency: currency),
);
}

Future<void> closeAnonymitySetCacheBox({
Expand All @@ -226,10 +228,8 @@ class DB {
if (_usedSerialsCacheBoxes[currency.identifier]?.isOpen != true) {
_usedSerialsCacheBoxes.remove(currency.identifier);
}
return _usedSerialsCacheBoxes[currency.identifier] ??=
await hive.openBox<dynamic>(
_boxNameUsedSerialsCache(currency: currency),
);
return _usedSerialsCacheBoxes[currency.identifier] ??= await hive
.openBox<dynamic>(_boxNameUsedSerialsCache(currency: currency));
}

Future<void> closeUsedSerialsCacheBox({
Expand Down Expand Up @@ -274,10 +274,7 @@ class DB {
List<T> values<T>({required String boxName}) =>
hive.box<T>(boxName).values.toList(growable: false);

T? get<T>({
required String boxName,
required dynamic key,
}) =>
T? get<T>({required String boxName, required dynamic key}) =>
hive.box<T>(boxName).get(key);

bool containsKey<T>({required String boxName, required dynamic key}) =>
Expand All @@ -289,19 +286,19 @@ class DB {
required String boxName,
required dynamic key,
required T value,
}) async =>
await mutex
.protect(() async => await hive.box<T>(boxName).put(key, value));
}) async => await mutex.protect(
() async => await hive.box<T>(boxName).put(key, value),
);

Future<void> add<T>({required String boxName, required T value}) async =>
await mutex.protect(() async => await hive.box<T>(boxName).add(value));

Future<void> addAll<T>({
required String boxName,
required Iterable<T> values,
}) async =>
await mutex
.protect(() async => await hive.box<T>(boxName).addAll(values));
}) async => await mutex.protect(
() async => await hive.box<T>(boxName).addAll(values),
);

Future<void> delete<T>({
required dynamic key,
Expand All @@ -325,20 +322,21 @@ class DB {
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameAddressBook);
await DB.instance.deleteBoxFromDisk(boxName: "debugInfoBox");
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameNodeModels);
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePrimaryNodes);
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameAllWalletsData);
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameNotifications);
await DB.instance
.deleteBoxFromDisk(boxName: DB.boxNameWatchedTransactions);
await DB.instance.deleteBoxFromDisk(
boxName: DB.boxNameWatchedTransactions,
);
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameWatchedTrades);
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTrades);
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTradesV2);
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTradeNotes);
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTradeLookup);
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameFavoriteWallets);
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePrefs);
await DB.instance
.deleteBoxFromDisk(boxName: DB.boxNameWalletsToDeleteOnStart);
await DB.instance.deleteBoxFromDisk(
boxName: DB.boxNameWalletsToDeleteOnStart,
);
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePriceCache);
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameDBInfo);
await DB.instance.deleteBoxFromDisk(boxName: "theme");
Expand Down
6 changes: 6 additions & 0 deletions lib/models/node_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class NodeModel {
final bool clearnetEnabled;
// @HiveField(13)
final bool forceNoTor;
// @HiveField(14)
final bool isPrimary;

NodeModel({
required this.host,
Expand All @@ -58,6 +60,7 @@ class NodeModel {
required this.isDown,
required this.torEnabled,
required this.clearnetEnabled,
required this.isPrimary,
this.forceNoTor = false,
this.loginName,
this.trusted,
Expand All @@ -77,6 +80,7 @@ class NodeModel {
bool? torEnabled,
bool? forceNoTor,
bool? clearnetEnabled,
bool? isPrimary,
}) {
return NodeModel(
host: host ?? this.host,
Expand All @@ -93,6 +97,7 @@ class NodeModel {
torEnabled: torEnabled ?? this.torEnabled,
clearnetEnabled: clearnetEnabled ?? this.clearnetEnabled,
forceNoTor: forceNoTor ?? this.forceNoTor,
isPrimary: isPrimary ?? this.isPrimary,
);
}

Expand All @@ -117,6 +122,7 @@ class NodeModel {
map['torEnabled'] = torEnabled;
map['clearEnabled'] = clearnetEnabled;
map['forceNoTor'] = forceNoTor;
map['isPrimary'] = isPrimary;
return map;
}

Expand Down
7 changes: 5 additions & 2 deletions lib/models/type_adaptors/node_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ class _NewWalletRecoveryPhraseWarningViewState
.getPrimaryNodeFor(currency: coin);

if (node == null) {
node = coin.defaultNode;
node = coin.defaultNode(isPrimary: true);
await ref
.read(nodeServiceChangeNotifierProvider)
.setPrimaryNodeFor(coin: coin, node: node);
.save(node, null, false);
}

final txTracker = TransactionNotificationTracker(walletId: info.walletId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ class _RestoreViewOnlyWalletViewState
.getPrimaryNodeFor(currency: widget.coin);

if (node == null) {
node = widget.coin.defaultNode;
node = widget.coin.defaultNode(isPrimary: true);
await ref
.read(nodeServiceChangeNotifierProvider)
.setPrimaryNodeFor(coin: widget.coin, node: node);
.save(node, null, false);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ class _RestoreWalletViewState extends ConsumerState<RestoreWalletView> {
.getPrimaryNodeFor(currency: widget.coin);

if (node == null) {
node = widget.coin.defaultNode;
node = widget.coin.defaultNode(isPrimary: true);
await ref
.read(nodeServiceChangeNotifierProvider)
.setPrimaryNodeFor(coin: widget.coin, node: node);
.save(node, null, false);
}

final txTracker = TransactionNotificationTracker(
Expand Down
Loading
Loading