@@ -319,34 +319,69 @@ export class DataStore {
319
319
latestStore : ReturnType < DataStoreSerializer [ "serialize" ] > ;
320
320
latestHeight : number ;
321
321
latestHash : string ;
322
- } > ( `stores` , USER_DIR_PATH ) ;
322
+ } > ( `stores` ) ;
323
323
324
- if ( ! Environment . CLI_MODE ) {
325
- // Kick off the updater instance in the backgounf if not already running
326
- StoreInfoCacheUpdater . initInstance ( ) ;
324
+ // Try to get cached store info
325
+ const cachedInfo = storeCoinCache . get ( this . storeId ) ;
327
326
328
- // Try to get cached store info
329
- const cachedInfo = storeCoinCache . get ( this . storeId ) ;
327
+ if ( cachedInfo ) {
328
+ try {
329
+ const {
330
+ latestStore : serializedStore ,
331
+ latestHeight : previousHeight ,
332
+ latestHash : previousHash ,
333
+ } = cachedInfo ;
330
334
331
- if ( cachedInfo ) {
332
- // If we have cached info, return it directly
333
- const { latestStore } = DataStoreSerializer . deserialize (
335
+ // Deserialize the stored data using DataStoreSerializer
336
+ const { latestStore : previousInfo } = DataStoreSerializer . deserialize (
334
337
{
335
- latestStore : cachedInfo . latestStore ,
336
- latestHeight : cachedInfo . latestHeight . toString ( ) ,
337
- latestHash : cachedInfo . latestHash ,
338
+ latestStore : serializedStore ,
339
+ latestHeight : previousHeight . toString ( ) ,
340
+ latestHash : previousHash ,
338
341
}
339
342
) ;
340
343
344
+ // Sync with peer if necessary
345
+ const peer = await FullNodePeer . connect ( ) ;
346
+ const { latestStore, latestHeight } = await peer . syncStore (
347
+ previousInfo ,
348
+ previousHeight ,
349
+ Buffer . from ( previousHash , "hex" ) ,
350
+ false
351
+ ) ;
352
+ const latestHash = await peer . getHeaderHash ( latestHeight ) ;
353
+
354
+ // Serialize the store data for caching
355
+ const serializedLatestStore = new DataStoreSerializer (
356
+ latestStore ,
357
+ latestHeight ,
358
+ latestHash
359
+ ) . serialize ( ) ;
360
+
361
+ // Cache updated store info
362
+ storeCoinCache . set ( this . storeId , {
363
+ latestStore : serializedLatestStore ,
364
+ latestHeight,
365
+ latestHash : latestHash . toString ( "hex" ) ,
366
+ } ) ;
367
+
368
+ return { latestStore, latestHeight, latestHash } ;
369
+ } catch {
370
+ // Return cached info if sync fails
371
+ const { latestStore, latestHeight, latestHash } =
372
+ DataStoreSerializer . deserialize ( {
373
+ latestStore : cachedInfo . latestStore ,
374
+ latestHeight : cachedInfo . latestHeight . toString ( ) ,
375
+ latestHash : cachedInfo . latestHash ,
376
+ } ) ;
341
377
return {
342
378
latestStore,
343
- latestHeight : cachedInfo . latestHeight ,
344
- latestHash : Buffer . from ( cachedInfo . latestHash , "hex" ) ,
379
+ latestHeight,
380
+ latestHash : latestHash ,
345
381
} ;
346
382
}
347
383
}
348
384
349
- // If no cached info, proceed to fetch and cache it
350
385
// Use getCreationHeight to retrieve height and hash information
351
386
const { createdAtHeight, createdAtHash } = await this . getCreationHeight ( ) ;
352
387
0 commit comments