@dignetwork/dig-sdk 0.0.1-alpha.120 → 0.0.1-alpha.122
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -5,6 +5,7 @@ import { CreateStoreUserInputs } from "../types";
|
|
|
5
5
|
export declare class DataStore {
|
|
6
6
|
private storeId;
|
|
7
7
|
private tree;
|
|
8
|
+
private static activeMonitors;
|
|
8
9
|
constructor(storeId: string, options?: DataIntegrityTreeOptions);
|
|
9
10
|
get StoreId(): string;
|
|
10
11
|
get Tree(): DataIntegrityTree;
|
|
@@ -33,6 +34,14 @@ export declare class DataStore {
|
|
|
33
34
|
*/
|
|
34
35
|
private static calculateFolderSize;
|
|
35
36
|
static getAllStores(): DataStore[];
|
|
37
|
+
/**
|
|
38
|
+
* Monitors the store indefinitely by syncing it with a peer.
|
|
39
|
+
* Logs store retrieval and caching operations. If an error occurs, logs the error and restarts the process after a short delay.
|
|
40
|
+
* Only one monitor can be active per storeId.
|
|
41
|
+
* @param {string} storeId - The store identifier to monitor.
|
|
42
|
+
* @returns {Promise<void>} Never resolves; runs indefinitely.
|
|
43
|
+
*/
|
|
44
|
+
static monitorStoreIndefinitely(storeId: string): Promise<void>;
|
|
36
45
|
fetchCoinInfo(): Promise<{
|
|
37
46
|
latestStore: DataStoreDriver;
|
|
38
47
|
latestHeight: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DataStore.d.ts","sourceRoot":"","sources":["../../src/blockchain/DataStore.ts"],"names":[],"mappings":"AAEA,OAAO,EAOL,SAAS,IAAI,eAAe,EAE5B,iBAAiB,EAIlB,MAAM,8BAA8B,CAAC;AAYtC,OAAO,EAAE,eAAe,EAAW,MAAM,UAAU,CAAC;AAGpD,OAAO,EACL,iBAAiB,EACjB,wBAAwB,EACzB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":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AC;QACpC,WAAW,EAAE,eAAe,CAAC;QAC7B,YAAY,EAAE,MAAM,CAAC;QACrB,UAAU,EAAE,MAAM,CAAC;KACpB,CAAC;IAyGW,wBAAwB,IAAI,OAAO,CAAC;QAC/C,eAAe,EAAE,MAAM,CAAC;QACxB,aAAa,EAAE,MAAM,CAAC;KACvB,CAAC;IAiBW,iBAAiB,IAAI,OAAO,CAAC;QACxC,eAAe,EAAE,MAAM,CAAC;QACxB,aAAa,EAAE,MAAM,CAAC;KACvB,CAAC;YA0BY,iBAAiB;IAYlB,cAAc,CAAC,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IA4C/D,oBAAoB,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAexD,WAAW,IAAI,OAAO,CAAC,iBAAiB,CAAC;IAKzC,QAAQ,IAAI,OAAO,CAAC,OAAO,CAAC;IAM5B,uBAAuB,CAClC,kBAAkB,CAAC,EAAE,MAAM,GAC1B,OAAO,CAAC,OAAO,CAAC;IAkBN,cAAc,CACzB,QAAQ,EAAE,iBAAiB,GAC1B,OAAO,CAAC,eAAe,CAAC;IAgD3B;;;;;OAKG;IACU,qBAAqB,CAChC,QAAQ,EAAE,MAAM,EAChB,kBAAkB,GAAE,OAAe,GAClC,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CA6C7C"}
|
|
@@ -20,6 +20,7 @@ const FileCache_1 = require("../utils/FileCache");
|
|
|
20
20
|
const DataStoreSerializer_1 = require("./DataStoreSerializer");
|
|
21
21
|
const node_cache_1 = __importDefault(require("node-cache"));
|
|
22
22
|
const config_2 = require("../utils/config");
|
|
23
|
+
const utils_1 = require("../utils");
|
|
23
24
|
// Initialize the cache with a TTL of 180 seconds (3 minutes)
|
|
24
25
|
const rootHistoryCache = new node_cache_1.default({ stdTTL: 180 });
|
|
25
26
|
const stat = (0, util_1.promisify)(fs_1.default.stat);
|
|
@@ -38,6 +39,9 @@ class DataStore {
|
|
|
38
39
|
};
|
|
39
40
|
}
|
|
40
41
|
this.tree = new DataIntegrityTree_1.DataIntegrityTree(storeId, _options);
|
|
42
|
+
if (!utils_1.Environment.CLI_MODE) {
|
|
43
|
+
DataStore.monitorStoreIndefinitely(this.storeId);
|
|
44
|
+
}
|
|
41
45
|
}
|
|
42
46
|
get StoreId() {
|
|
43
47
|
return this.storeId;
|
|
@@ -200,6 +204,81 @@ class DataStore {
|
|
|
200
204
|
fs_1.default.lstatSync(path_1.default.join(config_1.STORE_PATH, folder)).isDirectory());
|
|
201
205
|
return storIds.map((storeId) => DataStore.from(storeId));
|
|
202
206
|
}
|
|
207
|
+
/**
|
|
208
|
+
* Monitors the store indefinitely by syncing it with a peer.
|
|
209
|
+
* Logs store retrieval and caching operations. If an error occurs, logs the error and restarts the process after a short delay.
|
|
210
|
+
* Only one monitor can be active per storeId.
|
|
211
|
+
* @param {string} storeId - The store identifier to monitor.
|
|
212
|
+
* @returns {Promise<void>} Never resolves; runs indefinitely.
|
|
213
|
+
*/
|
|
214
|
+
static async monitorStoreIndefinitely(storeId) {
|
|
215
|
+
// Check if a monitor is already running for this storeId
|
|
216
|
+
if (this.activeMonitors.get(storeId)) {
|
|
217
|
+
console.log("Monitor:", `Monitor already running for storeId: ${storeId}`);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
// Set the monitor as active
|
|
221
|
+
this.activeMonitors.set(storeId, true);
|
|
222
|
+
const storeCoinCache = new FileCache_1.FileCache(`stores`);
|
|
223
|
+
// Clear the cache at the start
|
|
224
|
+
console.log("Monitor:", `Clearing cache for storeId: ${storeId}`);
|
|
225
|
+
storeCoinCache.delete(storeId);
|
|
226
|
+
while (true) {
|
|
227
|
+
try {
|
|
228
|
+
console.log("Monitor:", `Connecting to peer for storeId: ${storeId}`);
|
|
229
|
+
const peer = await FullNodePeer_1.FullNodePeer.connect();
|
|
230
|
+
const cachedInfo = storeCoinCache.get(storeId);
|
|
231
|
+
if (cachedInfo) {
|
|
232
|
+
// Log cached store info retrieval
|
|
233
|
+
console.log("Monitor:", `Cached store info found for storeId: ${storeId}, syncing...`);
|
|
234
|
+
// Deserialize cached info and wait for the coin to be spent
|
|
235
|
+
const previousStore = DataStoreSerializer_1.DataStoreSerializer.deserialize({
|
|
236
|
+
latestStore: cachedInfo.latestStore,
|
|
237
|
+
latestHeight: cachedInfo.latestHeight.toString(),
|
|
238
|
+
latestHash: cachedInfo.latestHash,
|
|
239
|
+
});
|
|
240
|
+
console.log("Monitor:", `Waiting for coin to be spent for storeId: ${storeId}...`);
|
|
241
|
+
const dataStore = DataStore.from(storeId);
|
|
242
|
+
const { createdAtHeight, createdAtHash } = await dataStore.getCreationHeight();
|
|
243
|
+
await peer.waitForCoinToBeSpent((0, datalayer_driver_1.getCoinId)(previousStore.latestStore.coin), createdAtHeight, createdAtHash);
|
|
244
|
+
// Sync store and get updated details
|
|
245
|
+
console.log("Monitor:", `Syncing store for storeId: ${storeId}`);
|
|
246
|
+
const { latestStore, latestHeight } = await peer.syncStore(previousStore.latestStore, previousStore.latestHeight, previousStore.latestHash, false);
|
|
247
|
+
const latestHash = await peer.getHeaderHash(latestHeight);
|
|
248
|
+
// Serialize and cache the updated store info
|
|
249
|
+
const serializedLatestStore = new DataStoreSerializer_1.DataStoreSerializer(latestStore, latestHeight, latestHash).serialize();
|
|
250
|
+
console.log("Monitor:", `Caching updated store info for storeId: ${storeId}`);
|
|
251
|
+
storeCoinCache.set(storeId, {
|
|
252
|
+
latestStore: serializedLatestStore,
|
|
253
|
+
latestHeight,
|
|
254
|
+
latestHash: latestHash.toString("hex"),
|
|
255
|
+
});
|
|
256
|
+
continue; // Continue monitoring
|
|
257
|
+
}
|
|
258
|
+
// If no cached info exists, log and sync from the creation height
|
|
259
|
+
console.log("Monitor:", `No cached info found for storeId: ${storeId}. Retrieving creation height.`);
|
|
260
|
+
const dataStore = DataStore.from(storeId);
|
|
261
|
+
const { createdAtHeight, createdAtHash } = await dataStore.getCreationHeight();
|
|
262
|
+
// Sync store from the peer using launcher ID
|
|
263
|
+
console.log("Monitor:", `Syncing store from launcher ID for storeId: ${storeId}`);
|
|
264
|
+
const { latestStore, latestHeight } = await peer.syncStoreFromLauncherId(Buffer.from(storeId, "hex"), createdAtHeight, createdAtHash, false);
|
|
265
|
+
const latestHash = await peer.getHeaderHash(latestHeight);
|
|
266
|
+
// Serialize and cache the new store info
|
|
267
|
+
const serializedLatestStore = new DataStoreSerializer_1.DataStoreSerializer(latestStore, latestHeight, latestHash).serialize();
|
|
268
|
+
console.log("Monitor:", `Caching new store info for storeId: ${storeId}`);
|
|
269
|
+
storeCoinCache.set(storeId, {
|
|
270
|
+
latestStore: serializedLatestStore,
|
|
271
|
+
latestHeight,
|
|
272
|
+
latestHash: latestHash.toString("hex"),
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
catch (error) {
|
|
276
|
+
console.error("Monitor:", `Error in monitorStoreIndefinitely for storeId: ${storeId} - ${error.message}`);
|
|
277
|
+
// Delay before restarting to avoid rapid retries
|
|
278
|
+
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
203
282
|
async fetchCoinInfo() {
|
|
204
283
|
try {
|
|
205
284
|
// Initialize the cache for the current storeId's coin info
|
|
@@ -415,3 +494,4 @@ class DataStore {
|
|
|
415
494
|
}
|
|
416
495
|
}
|
|
417
496
|
exports.DataStore = DataStore;
|
|
497
|
+
DataStore.activeMonitors = new Map();
|