Skip to content

Commit c571086

Browse files
Merge pull request #1151 from cypherstack/monero-connections
Node handling updates and fixes
2 parents 491fd47 + b9d15c8 commit c571086

File tree

61 files changed

+579
-536
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+579
-536
lines changed

lib/db/db_version_migration.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import '../models/isar/models/blockchain_data/address.dart';
1919
import '../models/isar/models/contact_entry.dart' as isar_contact;
2020
import '../models/isar/models/isar_models.dart' as isar_models;
2121
import '../models/models.dart';
22+
import '../models/node_model.dart';
2223
import '../services/mixins/wallet_db.dart';
2324
import '../services/wallets_service.dart';
2425
import '../utilities/amount/amount.dart';
@@ -361,6 +362,20 @@ class DbVersionMigrator with WalletDB {
361362
// try to continue migrating
362363
return await migrate(14, secureStore: secureStore);
363364

365+
case 14:
366+
// migrate
367+
await _v14();
368+
369+
// update version
370+
await DB.instance.put<dynamic>(
371+
boxName: DB.boxNameDBInfo,
372+
key: "hive_data_version",
373+
value: 15,
374+
);
375+
376+
// try to continue migrating
377+
return await migrate(15, secureStore: secureStore);
378+
364379
default:
365380
// finally return
366381
return;
@@ -632,4 +647,28 @@ class DbVersionMigrator with WalletDB {
632647
await isar.close(deleteFromDisk: true);
633648
}
634649
}
650+
651+
Future<void> _v14() async {
652+
final nodesBox = await DB.instance.hive.openBox<NodeModel>(
653+
DB.boxNameNodeModels,
654+
);
655+
final primariesBox = await DB.instance.hive.openBox<NodeModel>(
656+
DB.boxNamePrimaryNodesDeprecated,
657+
);
658+
659+
final primaries = primariesBox.values;
660+
661+
for (final node in primaries) {
662+
await nodesBox.put(
663+
node.id,
664+
node.copyWith(
665+
loginName: node.loginName,
666+
trusted: node.trusted,
667+
isPrimary: true,
668+
),
669+
);
670+
}
671+
672+
await primariesBox.deleteFromDisk();
673+
}
635674
}

lib/db/hive/db.dart

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class DB {
3333
static const String boxNameTrades = "exchangeTransactionsBox";
3434
static const String boxNameAllWalletsData = "wallets";
3535
static const String boxNameFavoriteWallets = "favoriteWallets";
36+
static const String boxNamePrimaryNodesDeprecated = "primaryNodes";
3637

3738
// in use
3839
// TODO: migrate
3940
static const String boxNameNodeModels = "nodeModels";
40-
static const String boxNamePrimaryNodes = "primaryNodes";
4141
static const String boxNameNotifications = "notificationModels";
4242
static const String boxNameWatchedTransactions =
4343
"watchedTxNotificationModels";
@@ -127,29 +127,27 @@ class DB {
127127
_boxNodeModels = await hive.openBox<NodeModel>(boxNameNodeModels);
128128
}
129129

130-
if (hive.isBoxOpen(boxNamePrimaryNodes)) {
131-
_boxPrimaryNodes = hive.box<NodeModel>(boxNamePrimaryNodes);
132-
} else {
133-
_boxPrimaryNodes = await hive.openBox<NodeModel>(boxNamePrimaryNodes);
134-
}
135-
136130
if (hive.isBoxOpen(boxNameAllWalletsData)) {
137131
_boxAllWalletsData = hive.box<dynamic>(boxNameAllWalletsData);
138132
} else {
139133
_boxAllWalletsData = await hive.openBox<dynamic>(boxNameAllWalletsData);
140134
}
141135

142-
_boxNotifications =
143-
await hive.openBox<NotificationModel>(boxNameNotifications);
144-
_boxWatchedTransactions =
145-
await hive.openBox<NotificationModel>(boxNameWatchedTransactions);
146-
_boxWatchedTrades =
147-
await hive.openBox<NotificationModel>(boxNameWatchedTrades);
136+
_boxNotifications = await hive.openBox<NotificationModel>(
137+
boxNameNotifications,
138+
);
139+
_boxWatchedTransactions = await hive.openBox<NotificationModel>(
140+
boxNameWatchedTransactions,
141+
);
142+
_boxWatchedTrades = await hive.openBox<NotificationModel>(
143+
boxNameWatchedTrades,
144+
);
148145
_boxTradesV2 = await hive.openBox<Trade>(boxNameTradesV2);
149146
_boxTradeNotes = await hive.openBox<String>(boxNameTradeNotes);
150147
_boxTradeLookup = await hive.openBox<TradeWalletLookup>(boxNameTradeLookup);
151148
_walletInfoSource = await hive.openBox<lib_monero_compat.WalletInfo>(
152-
lib_monero_compat.WalletInfo.boxName);
149+
lib_monero_compat.WalletInfo.boxName,
150+
);
153151
_boxFavoriteWallets = await hive.openBox<String>(boxNameFavoriteWallets);
154152

155153
await Future.wait([
@@ -183,11 +181,13 @@ class DB {
183181

184182
for (final entry in mapped.entries) {
185183
if (hive.isBoxOpen(entry.value.walletId)) {
186-
_walletBoxes[entry.value.walletId] =
187-
hive.box<dynamic>(entry.value.walletId);
184+
_walletBoxes[entry.value.walletId] = hive.box<dynamic>(
185+
entry.value.walletId,
186+
);
188187
} else {
189-
_walletBoxes[entry.value.walletId] =
190-
await hive.openBox<dynamic>(entry.value.walletId);
188+
_walletBoxes[entry.value.walletId] = await hive.openBox<dynamic>(
189+
entry.value.walletId,
190+
);
191191
}
192192
}
193193
}
@@ -196,8 +196,9 @@ class DB {
196196
if (_txCacheBoxes[currency.identifier]?.isOpen != true) {
197197
_txCacheBoxes.remove(currency.identifier);
198198
}
199-
return _txCacheBoxes[currency.identifier] ??=
200-
await hive.openBox<dynamic>(_boxNameTxCache(currency: currency));
199+
return _txCacheBoxes[currency.identifier] ??= await hive.openBox<dynamic>(
200+
_boxNameTxCache(currency: currency),
201+
);
201202
}
202203

203204
Future<void> closeTxCacheBox({required CryptoCurrency currency}) async {
@@ -210,8 +211,9 @@ class DB {
210211
if (_setCacheBoxes[currency.identifier]?.isOpen != true) {
211212
_setCacheBoxes.remove(currency.identifier);
212213
}
213-
return _setCacheBoxes[currency.identifier] ??=
214-
await hive.openBox<dynamic>(_boxNameSetCache(currency: currency));
214+
return _setCacheBoxes[currency.identifier] ??= await hive.openBox<dynamic>(
215+
_boxNameSetCache(currency: currency),
216+
);
215217
}
216218

217219
Future<void> closeAnonymitySetCacheBox({
@@ -226,10 +228,8 @@ class DB {
226228
if (_usedSerialsCacheBoxes[currency.identifier]?.isOpen != true) {
227229
_usedSerialsCacheBoxes.remove(currency.identifier);
228230
}
229-
return _usedSerialsCacheBoxes[currency.identifier] ??=
230-
await hive.openBox<dynamic>(
231-
_boxNameUsedSerialsCache(currency: currency),
232-
);
231+
return _usedSerialsCacheBoxes[currency.identifier] ??= await hive
232+
.openBox<dynamic>(_boxNameUsedSerialsCache(currency: currency));
233233
}
234234

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

277-
T? get<T>({
278-
required String boxName,
279-
required dynamic key,
280-
}) =>
277+
T? get<T>({required String boxName, required dynamic key}) =>
281278
hive.box<T>(boxName).get(key);
282279

283280
bool containsKey<T>({required String boxName, required dynamic key}) =>
@@ -289,19 +286,19 @@ class DB {
289286
required String boxName,
290287
required dynamic key,
291288
required T value,
292-
}) async =>
293-
await mutex
294-
.protect(() async => await hive.box<T>(boxName).put(key, value));
289+
}) async => await mutex.protect(
290+
() async => await hive.box<T>(boxName).put(key, value),
291+
);
295292

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

299296
Future<void> addAll<T>({
300297
required String boxName,
301298
required Iterable<T> values,
302-
}) async =>
303-
await mutex
304-
.protect(() async => await hive.box<T>(boxName).addAll(values));
299+
}) async => await mutex.protect(
300+
() async => await hive.box<T>(boxName).addAll(values),
301+
);
305302

306303
Future<void> delete<T>({
307304
required dynamic key,
@@ -325,20 +322,21 @@ class DB {
325322
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameAddressBook);
326323
await DB.instance.deleteBoxFromDisk(boxName: "debugInfoBox");
327324
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameNodeModels);
328-
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePrimaryNodes);
329325
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameAllWalletsData);
330326
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameNotifications);
331-
await DB.instance
332-
.deleteBoxFromDisk(boxName: DB.boxNameWatchedTransactions);
327+
await DB.instance.deleteBoxFromDisk(
328+
boxName: DB.boxNameWatchedTransactions,
329+
);
333330
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameWatchedTrades);
334331
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTrades);
335332
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTradesV2);
336333
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTradeNotes);
337334
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTradeLookup);
338335
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameFavoriteWallets);
339336
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePrefs);
340-
await DB.instance
341-
.deleteBoxFromDisk(boxName: DB.boxNameWalletsToDeleteOnStart);
337+
await DB.instance.deleteBoxFromDisk(
338+
boxName: DB.boxNameWalletsToDeleteOnStart,
339+
);
342340
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePriceCache);
343341
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameDBInfo);
344342
await DB.instance.deleteBoxFromDisk(boxName: "theme");

lib/models/node_model.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class NodeModel {
4545
final bool clearnetEnabled;
4646
// @HiveField(13)
4747
final bool forceNoTor;
48+
// @HiveField(14)
49+
final bool isPrimary;
4850

4951
NodeModel({
5052
required this.host,
@@ -58,6 +60,7 @@ class NodeModel {
5860
required this.isDown,
5961
required this.torEnabled,
6062
required this.clearnetEnabled,
63+
required this.isPrimary,
6164
this.forceNoTor = false,
6265
this.loginName,
6366
this.trusted,
@@ -77,6 +80,7 @@ class NodeModel {
7780
bool? torEnabled,
7881
bool? forceNoTor,
7982
bool? clearnetEnabled,
83+
bool? isPrimary,
8084
}) {
8185
return NodeModel(
8286
host: host ?? this.host,
@@ -93,6 +97,7 @@ class NodeModel {
9397
torEnabled: torEnabled ?? this.torEnabled,
9498
clearnetEnabled: clearnetEnabled ?? this.clearnetEnabled,
9599
forceNoTor: forceNoTor ?? this.forceNoTor,
100+
isPrimary: isPrimary ?? this.isPrimary,
96101
);
97102
}
98103

@@ -117,6 +122,7 @@ class NodeModel {
117122
map['torEnabled'] = torEnabled;
118123
map['clearEnabled'] = clearnetEnabled;
119124
map['forceNoTor'] = forceNoTor;
125+
map['isPrimary'] = isPrimary;
120126
return map;
121127
}
122128

lib/models/type_adaptors/node_model.g.dart

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/pages/add_wallet_views/new_wallet_recovery_phrase_warning_view/new_wallet_recovery_phrase_warning_view.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ class _NewWalletRecoveryPhraseWarningViewState
165165
.getPrimaryNodeFor(currency: coin);
166166

167167
if (node == null) {
168-
node = coin.defaultNode;
168+
node = coin.defaultNode(isPrimary: true);
169169
await ref
170170
.read(nodeServiceChangeNotifierProvider)
171-
.setPrimaryNodeFor(coin: coin, node: node);
171+
.save(node, null, false);
172172
}
173173

174174
final txTracker = TransactionNotificationTracker(walletId: info.walletId);

lib/pages/add_wallet_views/restore_wallet_view/restore_view_only_wallet_view.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ class _RestoreViewOnlyWalletViewState
198198
.getPrimaryNodeFor(currency: widget.coin);
199199

200200
if (node == null) {
201-
node = widget.coin.defaultNode;
201+
node = widget.coin.defaultNode(isPrimary: true);
202202
await ref
203203
.read(nodeServiceChangeNotifierProvider)
204-
.setPrimaryNodeFor(coin: widget.coin, node: node);
204+
.save(node, null, false);
205205
}
206206

207207
try {

lib/pages/add_wallet_views/restore_wallet_view/restore_wallet_view.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,10 @@ class _RestoreWalletViewState extends ConsumerState<RestoreWalletView> {
298298
.getPrimaryNodeFor(currency: widget.coin);
299299

300300
if (node == null) {
301-
node = widget.coin.defaultNode;
301+
node = widget.coin.defaultNode(isPrimary: true);
302302
await ref
303303
.read(nodeServiceChangeNotifierProvider)
304-
.setPrimaryNodeFor(coin: widget.coin, node: node);
304+
.save(node, null, false);
305305
}
306306

307307
final txTracker = TransactionNotificationTracker(

0 commit comments

Comments
 (0)