Skip to content

Commit 627002c

Browse files
Merge pull request #40 from cypherstack/upgrades_from_stack
Upgrades from stack
2 parents 0a4c64a + 6ed62a3 commit 627002c

48 files changed

Lines changed: 2178 additions & 1697 deletions

File tree

Some content is hidden

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

lib/db/hive/db.dart

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'dart:isolate';
33
import 'package:cw_core/wallet_info.dart' as xmr;
44
import 'package:hive/hive.dart';
55
import 'package:mutex/mutex.dart';
6-
import 'package:stackduo/models/exchange/change_now/exchange_transaction.dart';
76
import 'package:stackduo/models/exchange/response_objects/trade.dart';
87
import 'package:stackduo/models/node_model.dart';
98
import 'package:stackduo/models/notification_model.dart';
@@ -13,50 +12,53 @@ import 'package:stackduo/utilities/enums/coin_enum.dart';
1312
import 'package:stackduo/utilities/logger.dart';
1413

1514
class DB {
15+
// legacy (required for migrations)
16+
@Deprecated("Left over for migration from old versions of Stack Wallet")
1617
static const String boxNameAddressBook = "addressBook";
17-
static const String boxNameDebugInfo = "debugInfoBox";
18+
static const String boxNameTrades = "exchangeTransactionsBox";
19+
20+
// in use
21+
// TODO: migrate
1822
static const String boxNameNodeModels = "nodeModels";
1923
static const String boxNamePrimaryNodes = "primaryNodes";
2024
static const String boxNameAllWalletsData = "wallets";
2125
static const String boxNameNotifications = "notificationModels";
2226
static const String boxNameWatchedTransactions =
2327
"watchedTxNotificationModels";
2428
static const String boxNameWatchedTrades = "watchedTradesNotificationModels";
25-
static const String boxNameTrades = "exchangeTransactionsBox";
2629
static const String boxNameTradesV2 = "exchangeTradesBox";
2730
static const String boxNameTradeNotes = "tradeNotesBox";
2831
static const String boxNameTradeLookup = "tradeToTxidLookUpBox";
2932
static const String boxNameFavoriteWallets = "favoriteWallets";
30-
static const String boxNamePrefs = "prefs";
3133
static const String boxNameWalletsToDeleteOnStart = "walletsToDeleteOnStart";
3234
static const String boxNamePriceCache = "priceAPIPrice24hCache";
35+
36+
// in use (keep for now)
3337
static const String boxNameDBInfo = "dbInfo";
34-
// static const String boxNameTheme = "theme";
35-
static const String boxNameDesktopData = "desktopData";
36-
static const String boxNameBuys = "buysBox";
38+
static const String boxNamePrefs = "prefs";
39+
40+
String _boxNameTxCache({required Coin coin}) => "${coin.name}_txCache";
3741

38-
String boxNameTxCache({required Coin coin}) => "${coin.name}_txCache";
39-
String boxNameSetCache({required Coin coin}) =>
42+
// firo only
43+
String _boxNameSetCache({required Coin coin}) =>
4044
"${coin.name}_anonymitySetCache";
41-
String boxNameUsedSerialsCache({required Coin coin}) =>
45+
String _boxNameUsedSerialsCache({required Coin coin}) =>
4246
"${coin.name}_usedSerialsCache";
4347

44-
Box<String>? _boxDebugInfo;
4548
Box<NodeModel>? _boxNodeModels;
4649
Box<NodeModel>? _boxPrimaryNodes;
4750
Box<dynamic>? _boxAllWalletsData;
4851
Box<NotificationModel>? _boxNotifications;
4952
Box<NotificationModel>? _boxWatchedTransactions;
5053
Box<NotificationModel>? _boxWatchedTrades;
51-
Box<ExchangeTransaction>? _boxTrades;
5254
Box<Trade>? _boxTradesV2;
5355
Box<String>? _boxTradeNotes;
5456
Box<String>? _boxFavoriteWallets;
5557
Box<xmr.WalletInfo>? _walletInfoSource;
5658
Box<dynamic>? _boxPrefs;
5759
Box<TradeWalletLookup>? _boxTradeLookup;
5860
Box<dynamic>? _boxDBInfo;
59-
Box<String>? _boxDesktopData;
61+
// Box<String>? _boxDesktopData;
6062

6163
final Map<String, Box<dynamic>> _walletBoxes = {};
6264

@@ -99,8 +101,6 @@ class DB {
99101
_boxPrefs = await Hive.openBox<dynamic>(boxNamePrefs);
100102
}
101103

102-
_boxDebugInfo = await Hive.openBox<String>(boxNameDebugInfo);
103-
104104
if (Hive.isBoxOpen(boxNameNodeModels)) {
105105
_boxNodeModels = Hive.box<NodeModel>(boxNameNodeModels);
106106
} else {
@@ -119,19 +119,12 @@ class DB {
119119
_boxAllWalletsData = await Hive.openBox<dynamic>(boxNameAllWalletsData);
120120
}
121121

122-
if (Hive.isBoxOpen(boxNameDesktopData)) {
123-
_boxDesktopData = Hive.box<String>(boxNameDesktopData);
124-
} else {
125-
_boxDesktopData = await Hive.openBox<String>(boxNameDesktopData);
126-
}
127-
128122
_boxNotifications =
129123
await Hive.openBox<NotificationModel>(boxNameNotifications);
130124
_boxWatchedTransactions =
131125
await Hive.openBox<NotificationModel>(boxNameWatchedTransactions);
132126
_boxWatchedTrades =
133127
await Hive.openBox<NotificationModel>(boxNameWatchedTrades);
134-
_boxTrades = await Hive.openBox<ExchangeTransaction>(boxNameTrades);
135128
_boxTradesV2 = await Hive.openBox<Trade>(boxNameTradesV2);
136129
_boxTradeNotes = await Hive.openBox<String>(boxNameTradeNotes);
137130
_boxTradeLookup = await Hive.openBox<TradeWalletLookup>(boxNameTradeLookup);
@@ -142,7 +135,6 @@ class DB {
142135
await Future.wait([
143136
Hive.openBox<dynamic>(boxNamePriceCache),
144137
_loadWalletBoxes(),
145-
_loadSharedCoinCacheBoxes(),
146138
]);
147139
}
148140

@@ -174,15 +166,40 @@ class DB {
174166
}
175167
}
176168

177-
Future<void> _loadSharedCoinCacheBoxes() async {
178-
for (final coin in Coin.values) {
179-
_txCacheBoxes[coin] =
180-
await Hive.openBox<dynamic>(boxNameTxCache(coin: coin));
181-
_setCacheBoxes[coin] =
182-
await Hive.openBox<dynamic>(boxNameSetCache(coin: coin));
183-
_usedSerialsCacheBoxes[coin] =
184-
await Hive.openBox<dynamic>(boxNameUsedSerialsCache(coin: coin));
185-
}
169+
Future<Box<dynamic>> getTxCacheBox({required Coin coin}) async {
170+
return _txCacheBoxes[coin] ??=
171+
await Hive.openBox<dynamic>(_boxNameTxCache(coin: coin));
172+
}
173+
174+
Future<void> closeTxCacheBox({required Coin coin}) async {
175+
await _txCacheBoxes[coin]?.close();
176+
}
177+
178+
Future<Box<dynamic>> getAnonymitySetCacheBox({required Coin coin}) async {
179+
return _setCacheBoxes[coin] ??=
180+
await Hive.openBox<dynamic>(_boxNameSetCache(coin: coin));
181+
}
182+
183+
Future<void> closeAnonymitySetCacheBox({required Coin coin}) async {
184+
await _setCacheBoxes[coin]?.close();
185+
}
186+
187+
Future<Box<dynamic>> getUsedSerialsCacheBox({required Coin coin}) async {
188+
return _usedSerialsCacheBoxes[coin] ??=
189+
await Hive.openBox<dynamic>(_boxNameUsedSerialsCache(coin: coin));
190+
}
191+
192+
Future<void> closeUsedSerialsCacheBox({required Coin coin}) async {
193+
await _usedSerialsCacheBoxes[coin]?.close();
194+
}
195+
196+
/// Clear all cached transactions for the specified coin
197+
Future<void> clearSharedTransactionCache({required Coin coin}) async {
198+
await deleteAll<dynamic>(boxName: _boxNameTxCache(coin: coin));
199+
// if (coin == Coin.firo) {
200+
// await deleteAll<dynamic>(boxName: _boxNameSetCache(coin: coin));
201+
// await deleteAll<dynamic>(boxName: _boxNameUsedSerialsCache(coin: coin));
202+
// }
186203
}
187204

188205
/////////////////////////////////////////
@@ -243,6 +260,36 @@ class DB {
243260

244261
Future<void> deleteBoxFromDisk({required String boxName}) async =>
245262
await mutex.protect(() async => await Hive.deleteBoxFromDisk(boxName));
263+
264+
///////////////////////////////////////////////////////////////////////////
265+
Future<bool> deleteEverything() async {
266+
try {
267+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameAddressBook);
268+
await DB.instance.deleteBoxFromDisk(boxName: "debugInfoBox");
269+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameNodeModels);
270+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePrimaryNodes);
271+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameAllWalletsData);
272+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameNotifications);
273+
await DB.instance
274+
.deleteBoxFromDisk(boxName: DB.boxNameWatchedTransactions);
275+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameWatchedTrades);
276+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTrades);
277+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTradesV2);
278+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTradeNotes);
279+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTradeLookup);
280+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameFavoriteWallets);
281+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePrefs);
282+
await DB.instance
283+
.deleteBoxFromDisk(boxName: DB.boxNameWalletsToDeleteOnStart);
284+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePriceCache);
285+
await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameDBInfo);
286+
await DB.instance.deleteBoxFromDisk(boxName: "theme");
287+
return true;
288+
} catch (e, s) {
289+
Logging.instance.log("$e $s", level: LogLevel.Error);
290+
return false;
291+
}
292+
}
246293
}
247294

248295
abstract class DBKeys {

lib/electrumx_rpc/cached_electrumx.dart

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ class CachedElectrumX {
2828
required Coin coin,
2929
}) async {
3030
try {
31-
final cachedSet = DB.instance.get<dynamic>(
32-
boxName: DB.instance.boxNameSetCache(coin: coin),
33-
key: groupId) as Map?;
31+
final box = await DB.instance.getAnonymitySetCacheBox(coin: coin);
32+
final cachedSet = box.get(groupId) as Map?;
3433

3534
Map<String, dynamic> set;
3635

@@ -61,7 +60,7 @@ class CachedElectrumX {
6160
: newSet["blockHash"];
6261
for (int i = (newSet["coins"] as List).length - 1; i >= 0; i--) {
6362
dynamic newCoin = newSet["coins"][i];
64-
List translatedCoin = [];
63+
List<dynamic> translatedCoin = [];
6564
translatedCoin.add(!isHexadecimal(newCoin[0] as String)
6665
? base64ToHex(newCoin[0] as String)
6766
: newCoin[0]);
@@ -81,10 +80,7 @@ class CachedElectrumX {
8180
set["coins"].insert(0, translatedCoin);
8281
}
8382
// save set to db
84-
await DB.instance.put<dynamic>(
85-
boxName: DB.instance.boxNameSetCache(coin: coin),
86-
key: groupId,
87-
value: set);
83+
await box.put(groupId, set);
8884
Logging.instance.log(
8985
"Updated current anonymity set for ${coin.name} with group ID $groupId",
9086
level: LogLevel.Info,
@@ -120,8 +116,9 @@ class CachedElectrumX {
120116
bool verbose = true,
121117
}) async {
122118
try {
123-
final cachedTx = DB.instance.get<dynamic>(
124-
boxName: DB.instance.boxNameTxCache(coin: coin), key: txHash) as Map?;
119+
final box = await DB.instance.getTxCacheBox(coin: coin);
120+
121+
final cachedTx = box.get(txHash) as Map?;
125122
if (cachedTx == null) {
126123
final Map<String, dynamic> result = await electrumXClient
127124
.getTransaction(txHash: txHash, verbose: verbose);
@@ -131,10 +128,7 @@ class CachedElectrumX {
131128

132129
if (result["confirmations"] != null &&
133130
result["confirmations"] as int > minCacheConfirms) {
134-
await DB.instance.put<dynamic>(
135-
boxName: DB.instance.boxNameTxCache(coin: coin),
136-
key: txHash,
137-
value: result);
131+
await box.put(txHash, result);
138132
}
139133

140134
Logging.instance.log("using fetched result", level: LogLevel.Info);
@@ -156,9 +150,9 @@ class CachedElectrumX {
156150
int startNumber = 0,
157151
}) async {
158152
try {
159-
final _list = DB.instance.get<dynamic>(
160-
boxName: DB.instance.boxNameUsedSerialsCache(coin: coin),
161-
key: "serials") as List?;
153+
final box = await DB.instance.getUsedSerialsCacheBox(coin: coin);
154+
155+
final _list = box.get("serials") as List?;
162156

163157
List<String> cachedSerials =
164158
_list == null ? [] : List<String>.from(_list);
@@ -178,10 +172,9 @@ class CachedElectrumX {
178172
}
179173
cachedSerials.addAll(newSerials);
180174

181-
await DB.instance.put<dynamic>(
182-
boxName: DB.instance.boxNameUsedSerialsCache(coin: coin),
183-
key: "serials",
184-
value: cachedSerials,
175+
await box.put(
176+
"serials",
177+
cachedSerials,
185178
);
186179

187180
return cachedSerials;
@@ -195,11 +188,6 @@ class CachedElectrumX {
195188

196189
/// Clear all cached transactions for the specified coin
197190
Future<void> clearSharedTransactionCache({required Coin coin}) async {
198-
await DB.instance
199-
.deleteAll<dynamic>(boxName: DB.instance.boxNameTxCache(coin: coin));
200-
await DB.instance
201-
.deleteAll<dynamic>(boxName: DB.instance.boxNameSetCache(coin: coin));
202-
await DB.instance.deleteAll<dynamic>(
203-
boxName: DB.instance.boxNameUsedSerialsCache(coin: coin));
191+
await DB.instance.closeAnonymitySetCacheBox(coin: coin);
204192
}
205193
}

lib/main.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ void main() async {
156156
await Hive.openBox<dynamic>(DB.boxNamePrefs);
157157
await Prefs.instance.init();
158158

159+
await StackFileSystem.initThemesDir();
160+
159161
// Desktop migrate handled elsewhere (currently desktop_login_view.dart)
160162
if (!Util.isDesktop) {
161163
int dbVersion = DB.instance.get<dynamic>(
@@ -291,7 +293,7 @@ class _MaterialAppWithThemeState extends ConsumerState<MaterialAppWithTheme>
291293
}
292294

293295
ref.read(applicationThemesDirectoryPathProvider.notifier).state =
294-
(await StackFileSystem.applicationThemesDirectory()).path;
296+
StackFileSystem.themesDir!.path;
295297

296298
_notificationsService = ref.read(notificationsProvider);
297299
_nodeService = ref.read(nodeServiceChangeNotifierProvider);
@@ -386,7 +388,7 @@ class _MaterialAppWithThemeState extends ConsumerState<MaterialAppWithTheme>
386388
WidgetsBinding.instance.addPostFrameCallback((_) async {
387389
//Add themes path to provider
388390
ref.read(applicationThemesDirectoryPathProvider.notifier).state =
389-
(await StackFileSystem.applicationThemesDirectory()).path;
391+
StackFileSystem.themesDir!.path;
390392

391393
ref.read(themeProvider.state).state = ref.read(pThemeService).getTheme(
392394
themeId: themeId,

0 commit comments

Comments
 (0)