Skip to content

Commit 0997049

Browse files
Merge pull request #109 from DIG-Network/release/v0.0.1-alpha.120
Release/v0.0.1 alpha.120
2 parents 89a4fb2 + d509e09 commit 0997049

File tree

4 files changed

+55
-18
lines changed

4 files changed

+55
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
### [0.0.1-alpha.120](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.119...v0.0.1-alpha.120) (2024-10-03)
6+
57
### [0.0.1-alpha.119](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.118...v0.0.1-alpha.119) (2024-10-03)
68

79
### [0.0.1-alpha.118](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.117...v0.0.1-alpha.118) (2024-10-03)

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dignetwork/dig-sdk",
3-
"version": "0.0.1-alpha.119",
3+
"version": "0.0.1-alpha.120",
44
"description": "",
55
"type": "commonjs",
66
"main": "./dist/index.js",

src/blockchain/DataStore.ts

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -319,34 +319,69 @@ export class DataStore {
319319
latestStore: ReturnType<DataStoreSerializer["serialize"]>;
320320
latestHeight: number;
321321
latestHash: string;
322-
}>(`stores`, USER_DIR_PATH);
322+
}>(`stores`);
323323

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);
327326

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;
330334

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(
334337
{
335-
latestStore: cachedInfo.latestStore,
336-
latestHeight: cachedInfo.latestHeight.toString(),
337-
latestHash: cachedInfo.latestHash,
338+
latestStore: serializedStore,
339+
latestHeight: previousHeight.toString(),
340+
latestHash: previousHash,
338341
}
339342
);
340343

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+
});
341377
return {
342378
latestStore,
343-
latestHeight: cachedInfo.latestHeight,
344-
latestHash: Buffer.from(cachedInfo.latestHash, "hex"),
379+
latestHeight,
380+
latestHash: latestHash,
345381
};
346382
}
347383
}
348384

349-
// If no cached info, proceed to fetch and cache it
350385
// Use getCreationHeight to retrieve height and hash information
351386
const { createdAtHeight, createdAtHash } = await this.getCreationHeight();
352387

0 commit comments

Comments
 (0)