cached spark used coin tags electrumx call

This commit is contained in:
julian 2023-12-13 16:15:59 -06:00
parent e1241372bf
commit cf2114b7a3
5 changed files with 72 additions and 13 deletions

View file

@ -57,6 +57,8 @@ class DB {
"${coin.name}_anonymitySetSparkCache";
String _boxNameUsedSerialsCache({required Coin coin}) =>
"${coin.name}_usedSerialsCache";
String _boxNameSparkUsedCoinsTagsCache({required Coin coin}) =>
"${coin.name}_sparkUsedCoinsTagsCache";
Box<NodeModel>? _boxNodeModels;
Box<NodeModel>? _boxPrimaryNodes;
@ -79,6 +81,7 @@ class DB {
final Map<Coin, Box<dynamic>> _setCacheBoxes = {};
final Map<Coin, Box<dynamic>> _setSparkCacheBoxes = {};
final Map<Coin, Box<dynamic>> _usedSerialsCacheBoxes = {};
final Map<Coin, Box<dynamic>> _getSparkUsedCoinsTagsCacheBoxes = {};
// exposed for monero
Box<xmr.WalletInfo> get moneroWalletInfoBox => _walletInfoSource!;
@ -221,6 +224,16 @@ class DB {
await Hive.openBox<dynamic>(_boxNameUsedSerialsCache(coin: coin));
}
Future<Box<dynamic>> getSparkUsedCoinsTagsCacheBox(
{required Coin coin}) async {
if (_getSparkUsedCoinsTagsCacheBoxes[coin]?.isOpen != true) {
_getSparkUsedCoinsTagsCacheBoxes.remove(coin);
}
return _getSparkUsedCoinsTagsCacheBoxes[coin] ??=
await Hive.openBox<dynamic>(
_boxNameSparkUsedCoinsTagsCache(coin: coin));
}
Future<void> closeUsedSerialsCacheBox({required Coin coin}) async {
await _usedSerialsCacheBoxes[coin]?.close();
}
@ -232,6 +245,8 @@ class DB {
await deleteAll<dynamic>(boxName: _boxNameSetCache(coin: coin));
await deleteAll<dynamic>(boxName: _boxNameSetSparkCache(coin: coin));
await deleteAll<dynamic>(boxName: _boxNameUsedSerialsCache(coin: coin));
await deleteAll<dynamic>(
boxName: _boxNameSparkUsedCoinsTagsCache(coin: coin));
}
}

View file

@ -154,7 +154,7 @@ class CachedElectrumXClient {
return set;
} catch (e, s) {
Logging.instance.log(
"Failed to process CachedElectrumX.getAnonymitySet(): $e\n$s",
"Failed to process CachedElectrumX.getSparkAnonymitySet(): $e\n$s",
level: LogLevel.Error);
rethrow;
}
@ -251,8 +251,55 @@ class CachedElectrumXClient {
return resultingList;
} catch (e, s) {
Logging.instance.log(
"Failed to process CachedElectrumX.getTransaction(): $e\n$s",
level: LogLevel.Error);
"Failed to process CachedElectrumX.getUsedCoinSerials(): $e\n$s",
level: LogLevel.Error,
);
rethrow;
}
}
Future<Set<String>> getSparkUsedCoinsTags({
required Coin coin,
}) async {
try {
final box = await DB.instance.getSparkUsedCoinsTagsCacheBox(coin: coin);
final _list = box.get("tags") as List?;
Set<String> cachedTags =
_list == null ? {} : List<String>.from(_list).toSet();
final startNumber = max(
0,
cachedTags.length - 100, // 100 being some arbitrary buffer
);
final tags = await electrumXClient.getSparkUsedCoinsTags(
startNumber: startNumber,
);
// final newSerials = List<String>.from(serials["serials"] as List)
// .map((e) => !isHexadecimal(e) ? base64ToHex(e) : e)
// .toSet();
// ensure we are getting some overlap so we know we are not missing any
if (cachedTags.isNotEmpty && tags.isNotEmpty) {
assert(cachedTags.intersection(tags).isNotEmpty);
}
cachedTags.addAll(tags);
await box.put(
"tags",
cachedTags.toList(),
);
return cachedTags;
} catch (e, s) {
Logging.instance.log(
"Failed to process CachedElectrumX.getSparkUsedCoinsTags(): $e\n$s",
level: LogLevel.Error,
);
rethrow;
}
}

View file

@ -908,7 +908,7 @@ class ElectrumXClient {
/// Takes [startNumber], if it is 0, we get the full set,
/// otherwise the used tags after that number
Future<Map<String, dynamic>> getSparkUsedCoinsTags({
Future<Set<String>> getSparkUsedCoinsTags({
String? requestID,
required int startNumber,
}) async {
@ -921,7 +921,8 @@ class ElectrumXClient {
],
requestTimeout: const Duration(minutes: 2),
);
return Map<String, dynamic>.from(response["result"] as Map);
final map = Map<String, dynamic>.from(response["result"] as Map);
return Set<String>.from(map["tags"] as List);
} catch (e) {
Logging.instance.log(e, level: LogLevel.Error);
rethrow;

View file

@ -441,7 +441,7 @@ class HiddenSettings extends StatelessWidget {
.getSparkUsedCoinsTags(startNumber: 0);
print(
"usedCoinsTags['tags'].length: ${usedCoinsTags["tags"].length}");
"usedCoinsTags['tags'].length: ${usedCoinsTags.length}");
Util.printJson(
usedCoinsTags, "usedCoinsTags");
} catch (e, s) {

View file

@ -430,15 +430,11 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
groupId: latestSparkCoinId.toString(),
coin: info.coin,
),
electrumXClient.getSparkUsedCoinsTags(
startNumber: 0,
),
electrumXCachedClient.getSparkUsedCoinsTags(coin: info.coin),
]);
final anonymitySet = futureResults[0];
final spentCoinTags = List<String>.from(
futureResults[1]["tags"] as List,
).toSet();
final anonymitySet = futureResults[0] as Map<String, dynamic>;
final spentCoinTags = futureResults[1] as Set<String>;
// find our coins
final List<SparkCoin> myCoins = [];