mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-12-22 11:29:23 +00:00
close coin cache hive boxes when not in use
This commit is contained in:
parent
42abebe1d2
commit
b7b43e3380
5 changed files with 66 additions and 72 deletions
|
@ -26,6 +26,7 @@ class DB {
|
|||
// legacy (required for migrations)
|
||||
@Deprecated("Left over for migration from old versions of Stack Wallet")
|
||||
static const String boxNameAddressBook = "addressBook";
|
||||
static const String boxNameTrades = "exchangeTransactionsBox";
|
||||
|
||||
// in use
|
||||
// TODO: migrate
|
||||
|
@ -36,7 +37,6 @@ class DB {
|
|||
static const String boxNameWatchedTransactions =
|
||||
"watchedTxNotificationModels";
|
||||
static const String boxNameWatchedTrades = "watchedTradesNotificationModels";
|
||||
static const String boxNameTrades = "exchangeTransactionsBox";
|
||||
static const String boxNameTradesV2 = "exchangeTradesBox";
|
||||
static const String boxNameTradeNotes = "tradeNotesBox";
|
||||
static const String boxNameTradeLookup = "tradeToTxidLookUpBox";
|
||||
|
@ -48,10 +48,12 @@ class DB {
|
|||
static const String boxNameDBInfo = "dbInfo";
|
||||
static const String boxNamePrefs = "prefs";
|
||||
|
||||
String boxNameTxCache({required Coin coin}) => "${coin.name}_txCache";
|
||||
String boxNameSetCache({required Coin coin}) =>
|
||||
String _boxNameTxCache({required Coin coin}) => "${coin.name}_txCache";
|
||||
|
||||
// firo only
|
||||
String _boxNameSetCache({required Coin coin}) =>
|
||||
"${coin.name}_anonymitySetCache";
|
||||
String boxNameUsedSerialsCache({required Coin coin}) =>
|
||||
String _boxNameUsedSerialsCache({required Coin coin}) =>
|
||||
"${coin.name}_usedSerialsCache";
|
||||
|
||||
Box<NodeModel>? _boxNodeModels;
|
||||
|
@ -146,7 +148,6 @@ class DB {
|
|||
await Future.wait([
|
||||
Hive.openBox<dynamic>(boxNamePriceCache),
|
||||
_loadWalletBoxes(),
|
||||
_loadSharedCoinCacheBoxes(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -178,14 +179,39 @@ class DB {
|
|||
}
|
||||
}
|
||||
|
||||
Future<void> _loadSharedCoinCacheBoxes() async {
|
||||
for (final coin in Coin.values) {
|
||||
_txCacheBoxes[coin] =
|
||||
await Hive.openBox<dynamic>(boxNameTxCache(coin: coin));
|
||||
_setCacheBoxes[coin] =
|
||||
await Hive.openBox<dynamic>(boxNameSetCache(coin: coin));
|
||||
_usedSerialsCacheBoxes[coin] =
|
||||
await Hive.openBox<dynamic>(boxNameUsedSerialsCache(coin: coin));
|
||||
Future<Box<dynamic>> getTxCacheBox({required Coin coin}) async {
|
||||
return _txCacheBoxes[coin] ??=
|
||||
await Hive.openBox<dynamic>(_boxNameTxCache(coin: coin));
|
||||
}
|
||||
|
||||
Future<void> closeTxCacheBox({required Coin coin}) async {
|
||||
await _txCacheBoxes[coin]?.close();
|
||||
}
|
||||
|
||||
Future<Box<dynamic>> getAnonymitySetCacheBox({required Coin coin}) async {
|
||||
return _setCacheBoxes[coin] ??=
|
||||
await Hive.openBox<dynamic>(_boxNameSetCache(coin: coin));
|
||||
}
|
||||
|
||||
Future<void> closeAnonymitySetCacheBox({required Coin coin}) async {
|
||||
await _setCacheBoxes[coin]?.close();
|
||||
}
|
||||
|
||||
Future<Box<dynamic>> getUsedSerialsCacheBox({required Coin coin}) async {
|
||||
return _usedSerialsCacheBoxes[coin] ??=
|
||||
await Hive.openBox<dynamic>(_boxNameUsedSerialsCache(coin: coin));
|
||||
}
|
||||
|
||||
Future<void> closeUsedSerialsCacheBox({required Coin coin}) async {
|
||||
await _usedSerialsCacheBoxes[coin]?.close();
|
||||
}
|
||||
|
||||
/// Clear all cached transactions for the specified coin
|
||||
Future<void> clearSharedTransactionCache({required Coin coin}) async {
|
||||
await deleteAll<dynamic>(boxName: _boxNameTxCache(coin: coin));
|
||||
if (coin == Coin.firo) {
|
||||
await deleteAll<dynamic>(boxName: _boxNameSetCache(coin: coin));
|
||||
await deleteAll<dynamic>(boxName: _boxNameUsedSerialsCache(coin: coin));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -38,9 +38,8 @@ class CachedElectrumX {
|
|||
required Coin coin,
|
||||
}) async {
|
||||
try {
|
||||
final cachedSet = DB.instance.get<dynamic>(
|
||||
boxName: DB.instance.boxNameSetCache(coin: coin),
|
||||
key: groupId) as Map?;
|
||||
final box = await DB.instance.getAnonymitySetCacheBox(coin: coin);
|
||||
final cachedSet = box.get(groupId) as Map?;
|
||||
|
||||
Map<String, dynamic> set;
|
||||
|
||||
|
@ -71,7 +70,7 @@ class CachedElectrumX {
|
|||
: newSet["blockHash"];
|
||||
for (int i = (newSet["coins"] as List).length - 1; i >= 0; i--) {
|
||||
dynamic newCoin = newSet["coins"][i];
|
||||
List translatedCoin = [];
|
||||
List<dynamic> translatedCoin = [];
|
||||
translatedCoin.add(!isHexadecimal(newCoin[0] as String)
|
||||
? base64ToHex(newCoin[0] as String)
|
||||
: newCoin[0]);
|
||||
|
@ -91,10 +90,7 @@ class CachedElectrumX {
|
|||
set["coins"].insert(0, translatedCoin);
|
||||
}
|
||||
// save set to db
|
||||
await DB.instance.put<dynamic>(
|
||||
boxName: DB.instance.boxNameSetCache(coin: coin),
|
||||
key: groupId,
|
||||
value: set);
|
||||
await box.put(groupId, set);
|
||||
Logging.instance.log(
|
||||
"Updated current anonymity set for ${coin.name} with group ID $groupId",
|
||||
level: LogLevel.Info,
|
||||
|
@ -107,6 +103,8 @@ class CachedElectrumX {
|
|||
"Failed to process CachedElectrumX.getAnonymitySet(): $e\n$s",
|
||||
level: LogLevel.Error);
|
||||
rethrow;
|
||||
} finally {
|
||||
await DB.instance.closeAnonymitySetCacheBox(coin: coin);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,8 +128,9 @@ class CachedElectrumX {
|
|||
bool verbose = true,
|
||||
}) async {
|
||||
try {
|
||||
final cachedTx = DB.instance.get<dynamic>(
|
||||
boxName: DB.instance.boxNameTxCache(coin: coin), key: txHash) as Map?;
|
||||
final box = await DB.instance.getTxCacheBox(coin: coin);
|
||||
|
||||
final cachedTx = box.get(txHash) as Map?;
|
||||
if (cachedTx == null) {
|
||||
final Map<String, dynamic> result = await electrumXClient
|
||||
.getTransaction(txHash: txHash, verbose: verbose);
|
||||
|
@ -141,10 +140,7 @@ class CachedElectrumX {
|
|||
|
||||
if (result["confirmations"] != null &&
|
||||
result["confirmations"] as int > minCacheConfirms) {
|
||||
await DB.instance.put<dynamic>(
|
||||
boxName: DB.instance.boxNameTxCache(coin: coin),
|
||||
key: txHash,
|
||||
value: result);
|
||||
await box.put(txHash, result);
|
||||
}
|
||||
|
||||
Logging.instance.log("using fetched result", level: LogLevel.Info);
|
||||
|
@ -158,6 +154,8 @@ class CachedElectrumX {
|
|||
"Failed to process CachedElectrumX.getTransaction(): $e\n$s",
|
||||
level: LogLevel.Error);
|
||||
rethrow;
|
||||
} finally {
|
||||
await DB.instance.closeTxCacheBox(coin: coin);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -166,9 +164,9 @@ class CachedElectrumX {
|
|||
int startNumber = 0,
|
||||
}) async {
|
||||
try {
|
||||
final _list = DB.instance.get<dynamic>(
|
||||
boxName: DB.instance.boxNameUsedSerialsCache(coin: coin),
|
||||
key: "serials") as List?;
|
||||
final box = await DB.instance.getUsedSerialsCacheBox(coin: coin);
|
||||
|
||||
final _list = box.get("serials") as List?;
|
||||
|
||||
List<String> cachedSerials =
|
||||
_list == null ? [] : List<String>.from(_list);
|
||||
|
@ -188,10 +186,9 @@ class CachedElectrumX {
|
|||
}
|
||||
cachedSerials.addAll(newSerials);
|
||||
|
||||
await DB.instance.put<dynamic>(
|
||||
boxName: DB.instance.boxNameUsedSerialsCache(coin: coin),
|
||||
key: "serials",
|
||||
value: cachedSerials,
|
||||
await box.put(
|
||||
"serials",
|
||||
cachedSerials,
|
||||
);
|
||||
|
||||
return cachedSerials;
|
||||
|
@ -200,16 +197,13 @@ class CachedElectrumX {
|
|||
"Failed to process CachedElectrumX.getTransaction(): $e\n$s",
|
||||
level: LogLevel.Error);
|
||||
rethrow;
|
||||
} finally {
|
||||
await DB.instance.closeUsedSerialsCacheBox(coin: coin);
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear all cached transactions for the specified coin
|
||||
Future<void> clearSharedTransactionCache({required Coin coin}) async {
|
||||
await DB.instance
|
||||
.deleteAll<dynamic>(boxName: DB.instance.boxNameTxCache(coin: coin));
|
||||
await DB.instance
|
||||
.deleteAll<dynamic>(boxName: DB.instance.boxNameSetCache(coin: coin));
|
||||
await DB.instance.deleteAll<dynamic>(
|
||||
boxName: DB.instance.boxNameUsedSerialsCache(coin: coin));
|
||||
await DB.instance.closeAnonymitySetCacheBox(coin: coin);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -178,10 +178,8 @@ class DbVersionMigrator with WalletDB {
|
|||
|
||||
case 3:
|
||||
// clear possible broken firo cache
|
||||
await DB.instance.deleteBoxFromDisk(
|
||||
boxName: DB.instance.boxNameSetCache(coin: Coin.firo));
|
||||
await DB.instance.deleteBoxFromDisk(
|
||||
boxName: DB.instance.boxNameUsedSerialsCache(coin: Coin.firo));
|
||||
await DB.instance.clearSharedTransactionCache(coin: Coin.firo);
|
||||
|
||||
|
||||
// update version
|
||||
await DB.instance.put<dynamic>(
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:hive_test/hive_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:stackwallet/db/hive/db.dart';
|
||||
import 'package:stackwallet/electrumx_rpc/cached_electrumx.dart';
|
||||
import 'package:stackwallet/electrumx_rpc/electrumx.dart';
|
||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||
|
@ -17,10 +15,10 @@ void main() {
|
|||
group("tests using mock hive", () {
|
||||
setUp(() async {
|
||||
await setUpTestHive();
|
||||
await Hive.openBox<dynamic>(
|
||||
DB.instance.boxNameUsedSerialsCache(coin: Coin.firo));
|
||||
await Hive.openBox<dynamic>(DB.instance.boxNameSetCache(coin: Coin.firo));
|
||||
await Hive.openBox<dynamic>(DB.instance.boxNameTxCache(coin: Coin.firo));
|
||||
// await Hive.openBox<dynamic>(
|
||||
// DB.instance.boxNameUsedSerialsCache(coin: Coin.firo));
|
||||
// await Hive.openBox<dynamic>(DB.instance.boxNameSetCache(coin: Coin.firo));
|
||||
// await Hive.openBox<dynamic>(DB.instance.boxNameTxCache(coin: Coin.firo));
|
||||
});
|
||||
group("getAnonymitySet", () {
|
||||
// test("empty set cache call", () async {
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hive_test/hive_test.dart';
|
||||
import 'package:stackwallet/db/hive/db.dart';
|
||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||
|
||||
void main() {
|
||||
group("DB box names", () {
|
||||
test("address book", () => expect(DB.boxNameAddressBook, "addressBook"));
|
||||
test("debug info", () => expect(DB.boxNameDebugInfo, "debugInfoBox"));
|
||||
test("nodes", () => expect(DB.boxNameNodeModels, "nodeModels"));
|
||||
test("primary nodes", () => expect(DB.boxNamePrimaryNodes, "primaryNodes"));
|
||||
test("wallets info", () => expect(DB.boxNameAllWalletsData, "wallets"));
|
||||
|
@ -33,26 +31,6 @@ void main() {
|
|||
expect(DB.boxNameWalletsToDeleteOnStart, "walletsToDeleteOnStart"));
|
||||
test("price cache",
|
||||
() => expect(DB.boxNamePriceCache, "priceAPIPrice24hCache"));
|
||||
|
||||
test("boxNameTxCache", () {
|
||||
for (final coin in Coin.values) {
|
||||
expect(DB.instance.boxNameTxCache(coin: coin), "${coin.name}_txCache");
|
||||
}
|
||||
});
|
||||
|
||||
test("boxNameSetCache", () {
|
||||
for (final coin in Coin.values) {
|
||||
expect(DB.instance.boxNameSetCache(coin: coin),
|
||||
"${coin.name}_anonymitySetCache");
|
||||
}
|
||||
});
|
||||
|
||||
test("boxNameUsedSerialsCache", () {
|
||||
for (final coin in Coin.values) {
|
||||
expect(DB.instance.boxNameUsedSerialsCache(coin: coin),
|
||||
"${coin.name}_usedSerialsCache");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group("tests requiring test hive environment", () {
|
||||
|
|
Loading…
Reference in a new issue