@@ -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 ,
@@ -97,6 +93,8 @@ class CachedElectrumX {
9793 "Failed to process CachedElectrumX.getAnonymitySet(): $e \n $s " ,
9894 level: LogLevel .Error );
9995 rethrow ;
96+ } finally {
97+ await DB .instance.closeAnonymitySetCacheBox (coin: coin);
10098 }
10199 }
102100
@@ -120,8 +118,9 @@ class CachedElectrumX {
120118 bool verbose = true ,
121119 }) async {
122120 try {
123- final cachedTx = DB .instance.get <dynamic >(
124- boxName: DB .instance.boxNameTxCache (coin: coin), key: txHash) as Map ? ;
121+ final box = await DB .instance.getTxCacheBox (coin: coin);
122+
123+ final cachedTx = box.get (txHash) as Map ? ;
125124 if (cachedTx == null ) {
126125 final Map <String , dynamic > result = await electrumXClient
127126 .getTransaction (txHash: txHash, verbose: verbose);
@@ -131,10 +130,7 @@ class CachedElectrumX {
131130
132131 if (result["confirmations" ] != null &&
133132 result["confirmations" ] as int > minCacheConfirms) {
134- await DB .instance.put <dynamic >(
135- boxName: DB .instance.boxNameTxCache (coin: coin),
136- key: txHash,
137- value: result);
133+ await box.put (txHash, result);
138134 }
139135
140136 Logging .instance.log ("using fetched result" , level: LogLevel .Info );
@@ -148,6 +144,8 @@ class CachedElectrumX {
148144 "Failed to process CachedElectrumX.getTransaction(): $e \n $s " ,
149145 level: LogLevel .Error );
150146 rethrow ;
147+ } finally {
148+ await DB .instance.closeTxCacheBox (coin: coin);
151149 }
152150 }
153151
@@ -156,9 +154,9 @@ class CachedElectrumX {
156154 int startNumber = 0 ,
157155 }) async {
158156 try {
159- final _list = DB .instance.get < dynamic >(
160- boxName : DB .instance. boxNameUsedSerialsCache (coin : coin),
161- key : "serials" ) as List ? ;
157+ final box = await DB .instance.getUsedSerialsCacheBox (coin : coin);
158+
159+ final _list = box. get ( "serials" ) as List ? ;
162160
163161 List <String > cachedSerials =
164162 _list == null ? [] : List <String >.from (_list);
@@ -178,10 +176,9 @@ class CachedElectrumX {
178176 }
179177 cachedSerials.addAll (newSerials);
180178
181- await DB .instance.put <dynamic >(
182- boxName: DB .instance.boxNameUsedSerialsCache (coin: coin),
183- key: "serials" ,
184- value: cachedSerials,
179+ await box.put (
180+ "serials" ,
181+ cachedSerials,
185182 );
186183
187184 return cachedSerials;
@@ -190,16 +187,13 @@ class CachedElectrumX {
190187 "Failed to process CachedElectrumX.getTransaction(): $e \n $s " ,
191188 level: LogLevel .Error );
192189 rethrow ;
190+ } finally {
191+ await DB .instance.closeUsedSerialsCacheBox (coin: coin);
193192 }
194193 }
195194
196195 /// Clear all cached transactions for the specified coin
197196 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));
197+ await DB .instance.closeAnonymitySetCacheBox (coin: coin);
204198 }
205199}
0 commit comments