Skip to content

Commit e811752

Browse files
committed
auto one time clear of possible bad used tags cache.
1 parent 70f4ef2 commit e811752

5 files changed

Lines changed: 85 additions & 65 deletions

File tree

lib/db/sqlite/firo_cache.dart

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ abstract class _FiroCache {
6565
await StackFileSystem.applicationFiroCacheSQLiteDirectory();
6666

6767
for (final network in networks) {
68-
final sparkSetCacheFile =
69-
File("${sqliteDir.path}/${sparkSetCacheFileName(network)}");
68+
final sparkSetCacheFile = File(
69+
"${sqliteDir.path}/${sparkSetCacheFileName(network)}",
70+
);
7071

71-
final sparkUsedTagsCacheFile =
72-
File("${sqliteDir.path}/${sparkUsedTagsCacheFileName(network)}");
72+
final sparkUsedTagsCacheFile = File(
73+
"${sqliteDir.path}/${sparkUsedTagsCacheFileName(network)}",
74+
);
7375

7476
if (!(await sparkSetCacheFile.exists())) {
7577
await _createSparkSetCacheDb(sparkSetCacheFile.path);
@@ -91,35 +93,40 @@ abstract class _FiroCache {
9193

9294
static Future<void> _deleteAllCache(CryptoCurrencyNetwork network) async {
9395
final start = DateTime.now();
94-
setCacheDB(network).execute(
95-
"""
96+
setCacheDB(network).execute("""
9697
DELETE FROM SparkSet;
9798
DELETE FROM SparkCoin;
9899
DELETE FROM SparkSetCoins;
99100
VACUUM;
100-
""",
101+
""");
102+
await _deleteUsedTagsCache(network);
103+
104+
Logging.instance.d(
105+
"_deleteAllCache() "
106+
"duration = ${DateTime.now().difference(start)}",
101107
);
102-
usedTagsCacheDB(network).execute(
103-
"""
108+
}
109+
110+
static Future<void> _deleteUsedTagsCache(
111+
CryptoCurrencyNetwork network,
112+
) async {
113+
final start = DateTime.now();
114+
115+
usedTagsCacheDB(network).execute("""
104116
DELETE FROM SparkUsedCoinTags;
105117
VACUUM;
106-
""",
107-
);
118+
""");
108119

109120
Logging.instance.d(
110-
"_deleteAllCache() "
121+
"_deleteUsedTagsCache() "
111122
"duration = ${DateTime.now().difference(start)}",
112123
);
113124
}
114125

115126
static Future<void> _createSparkSetCacheDb(String file) async {
116-
final db = sqlite3.open(
117-
file,
118-
mode: OpenMode.readWriteCreate,
119-
);
127+
final db = sqlite3.open(file, mode: OpenMode.readWriteCreate);
120128

121-
db.execute(
122-
"""
129+
db.execute("""
123130
CREATE TABLE SparkSet (
124131
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
125132
blockHash TEXT NOT NULL,
@@ -145,27 +152,21 @@ abstract class _FiroCache {
145152
FOREIGN KEY (setId) REFERENCES SparkSet(id),
146153
FOREIGN KEY (coinId) REFERENCES SparkCoin(id)
147154
);
148-
""",
149-
);
155+
""");
150156

151157
db.dispose();
152158
}
153159

154160
static Future<void> _createSparkUsedTagsCacheDb(String file) async {
155-
final db = sqlite3.open(
156-
file,
157-
mode: OpenMode.readWriteCreate,
158-
);
161+
final db = sqlite3.open(file, mode: OpenMode.readWriteCreate);
159162

160-
db.execute(
161-
"""
163+
db.execute("""
162164
CREATE TABLE SparkUsedCoinTags (
163165
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
164166
tag TEXT NOT NULL UNIQUE,
165167
txid TEXT NOT NULL
166168
);
167-
""",
168-
);
169+
""");
169170

170171
db.dispose();
171172
}

lib/db/sqlite/firo_cache_coordinator.dart

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ abstract class FiroCacheCoordinator {
2323
}
2424
}
2525

26-
static Future<void> clearSharedCache(CryptoCurrencyNetwork network) async {
26+
static Future<void> clearSharedCache(
27+
CryptoCurrencyNetwork network, {
28+
bool clearOnlyUsedTagsCache = false,
29+
}) async {
30+
if (clearOnlyUsedTagsCache) {
31+
return await _FiroCache._deleteUsedTagsCache(network);
32+
}
2733
return await _FiroCache._deleteAllCache(network);
2834
}
2935

@@ -38,9 +44,10 @@ abstract class FiroCacheCoordinator {
3844

3945
final setSize =
4046
(await setCacheFile.exists()) ? await setCacheFile.length() : 0;
41-
final tagsSize = (await usedTagsCacheFile.exists())
42-
? await usedTagsCacheFile.length()
43-
: 0;
47+
final tagsSize =
48+
(await usedTagsCacheFile.exists())
49+
? await usedTagsCacheFile.length()
50+
: 0;
4451

4552
Logging.instance.d("Spark cache used tags size: $tagsSize");
4653
Logging.instance.d("Spark cache anon set size: $setSize");
@@ -67,16 +74,11 @@ abstract class FiroCacheCoordinator {
6774
) async {
6875
await _tagLocks[network]!.protect(() async {
6976
final count = await FiroCacheCoordinator.getUsedCoinTagsCount(network);
70-
final unhashedTags =
71-
await client.getSparkUnhashedUsedCoinsTagsWithTxHashes(
72-
startNumber: count,
73-
);
77+
final unhashedTags = await client
78+
.getSparkUnhashedUsedCoinsTagsWithTxHashes(startNumber: count);
7479
if (unhashedTags.isNotEmpty) {
7580
await _workers[network]!.runTask(
76-
FCTask(
77-
func: FCFuncName._updateSparkUsedTagsWith,
78-
data: unhashedTags,
79-
),
81+
FCTask(func: FCFuncName._updateSparkUsedTagsWith, data: unhashedTags),
8082
);
8183
}
8284
});
@@ -98,9 +100,7 @@ abstract class FiroCacheCoordinator {
98100

99101
final prevSize = prevMeta?.size ?? 0;
100102

101-
final meta = await client.getSparkAnonymitySetMeta(
102-
coinGroupId: groupId,
103-
);
103+
final meta = await client.getSparkAnonymitySetMeta(coinGroupId: groupId);
104104

105105
progressUpdated?.call(prevSize, meta.size);
106106

@@ -141,9 +141,10 @@ abstract class FiroCacheCoordinator {
141141
coins.addAll(data);
142142
}
143143

144-
final result = coins
145-
.map((e) => RawSparkCoin.fromRPCResponse(e as List, groupId))
146-
.toList();
144+
final result =
145+
coins
146+
.map((e) => RawSparkCoin.fromRPCResponse(e as List, groupId))
147+
.toList();
147148

148149
await _workers[network]!.runTask(
149150
FCTask(
@@ -167,9 +168,7 @@ abstract class FiroCacheCoordinator {
167168
return result.map((e) => e["tag"] as String).toSet();
168169
}
169170

170-
static Future<int> getUsedCoinTagsCount(
171-
CryptoCurrencyNetwork network,
172-
) async {
171+
static Future<int> getUsedCoinTagsCount(CryptoCurrencyNetwork network) async {
173172
final result = await _Reader._getUsedCoinTagsCount(
174173
db: _FiroCache.usedTagsCacheDB(network),
175174
);
@@ -195,12 +194,7 @@ abstract class FiroCacheCoordinator {
195194
return [];
196195
}
197196
return result.rows
198-
.map(
199-
(e) => (
200-
tag: e[0] as String,
201-
txid: e[1] as String,
202-
),
203-
)
197+
.map((e) => (tag: e[0] as String, txid: e[1] as String))
204198
.toList();
205199
}
206200

@@ -230,16 +224,17 @@ abstract class FiroCacheCoordinator {
230224
String? afterBlockHash,
231225
required CryptoCurrencyNetwork network,
232226
}) async {
233-
final resultSet = afterBlockHash == null
234-
? await _Reader._getSetCoinsForGroupId(
235-
groupId,
236-
db: _FiroCache.setCacheDB(network),
237-
)
238-
: await _Reader._getSetCoinsForGroupIdAndBlockHash(
239-
groupId,
240-
afterBlockHash,
241-
db: _FiroCache.setCacheDB(network),
242-
);
227+
final resultSet =
228+
afterBlockHash == null
229+
? await _Reader._getSetCoinsForGroupId(
230+
groupId,
231+
db: _FiroCache.setCacheDB(network),
232+
)
233+
: await _Reader._getSetCoinsForGroupIdAndBlockHash(
234+
groupId,
235+
afterBlockHash,
236+
db: _FiroCache.setCacheDB(network),
237+
);
243238

244239
return resultSet
245240
.map(

lib/wallets/isar/models/wallet_info.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,6 @@ abstract class WalletInfoKeys {
521521
"duressMarkedVisibleWalletKey";
522522
static const String mwebEnabled = "mwebEnabledKey";
523523
static const String mwebScanHeight = "mwebScanHeightKey";
524+
static const String firoSparkUsedTagsCacheResetVersion =
525+
"firoSparkUsedTagsCacheResetVersionKey";
524526
}

lib/wallets/wallet/wallet.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ abstract class Wallet<T extends CryptoCurrency> {
225225
await wallet.mainDB.isar.walletInfo.put(walletInfo);
226226
});
227227

228+
if (wallet is SparkInterface) {
229+
await walletInfo.updateOtherData(
230+
newEntries: {WalletInfoKeys.firoSparkUsedTagsCacheResetVersion: 1},
231+
isar: mainDB.isar,
232+
);
233+
}
234+
228235
return wallet;
229236
}
230237

lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ mixin SparkInterface<T extends ElectrumXCurrencyInterface>
148148
@override
149149
Future<void> init() async {
150150
try {
151+
final sparkUsedTagsResetVersion =
152+
info.otherData[WalletInfoKeys.firoSparkUsedTagsCacheResetVersion]
153+
as int? ??
154+
0;
155+
if (sparkUsedTagsResetVersion == 0) {
156+
await info.updateOtherData(
157+
newEntries: {WalletInfoKeys.firoSparkUsedTagsCacheResetVersion: 1},
158+
isar: mainDB.isar,
159+
);
160+
await FiroCacheCoordinator.clearSharedCache(
161+
cryptoCurrency.network,
162+
clearOnlyUsedTagsCache: true,
163+
);
164+
}
165+
151166
Address? address = await getCurrentReceivingSparkAddress();
152167
if (address == null) {
153168
address = await generateNextSparkAddress();

0 commit comments

Comments
 (0)