@dignetwork/dig-sdk 0.0.1-alpha.106 → 0.0.1-alpha.108
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.
- package/dist/DigNetwork/PropagationServer.d.ts +1 -1
- package/dist/DigNetwork/PropagationServer.d.ts.map +1 -1
- package/dist/DigNetwork/PropagationServer.js +44 -8
- package/dist/blockchain/DataStore.d.ts +1 -1
- package/dist/blockchain/DataStore.d.ts.map +1 -1
- package/dist/blockchain/DataStore.js +22 -38
- package/dist/blockchain/StoreInfoCacheUpdater.d.ts +13 -0
- package/dist/blockchain/StoreInfoCacheUpdater.d.ts.map +1 -0
- package/dist/blockchain/StoreInfoCacheUpdater.js +143 -0
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/{asyncPool.d.ts → promiseUtils.d.ts} +9 -1
- package/dist/utils/promiseUtils.d.ts.map +1 -0
- package/dist/utils/{asyncPool.js → promiseUtils.js} +15 -0
- package/package.json +3 -1
- package/dist/utils/asyncPool.d.ts.map +0 -1
|
@@ -47,7 +47,7 @@ export declare class PropagationServer {
|
|
|
47
47
|
* Upload a file to the server by sending a PUT request.
|
|
48
48
|
* Logs progress using a local cli-progress bar.
|
|
49
49
|
*/
|
|
50
|
-
uploadFile(label: string, dataPath: string): Promise<void>;
|
|
50
|
+
uploadFile(label: string, dataPath: string, uncompress?: boolean): Promise<void>;
|
|
51
51
|
/**
|
|
52
52
|
* Commit the upload session by sending a POST request to the server.
|
|
53
53
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PropagationServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/PropagationServer.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"PropagationServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/PropagationServer.ts"],"names":[],"mappings":"A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,EACf,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM;IA0DnB;;;OAGG;IACG,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAkFlD;;OAEG;IACG,cAAc,CAClB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC;QAAE,MAAM,EAAE,OAAO,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IA4B7C;;;OAGG;IACG,YAAY,CAChB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM;IA0GjB;;OAEG;WACU,aAAa,CACxB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM;CA2FpB"}
|
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
27
|
};
|
|
@@ -13,7 +36,8 @@ const https_1 = __importDefault(require("https"));
|
|
|
13
36
|
const os_1 = __importDefault(require("os"));
|
|
14
37
|
const path_1 = __importDefault(require("path"));
|
|
15
38
|
const progress_stream_1 = __importDefault(require("progress-stream"));
|
|
16
|
-
const
|
|
39
|
+
const zlib = __importStar(require("zlib"));
|
|
40
|
+
const promiseUtils_1 = require("../utils/promiseUtils");
|
|
17
41
|
const nanospinner_1 = require("nanospinner");
|
|
18
42
|
const hashUtils_1 = require("../utils/hashUtils");
|
|
19
43
|
const ssl_1 = require("../utils/ssl");
|
|
@@ -183,7 +207,7 @@ class PropagationServer {
|
|
|
183
207
|
* Upload a file to the server by sending a PUT request.
|
|
184
208
|
* Logs progress using a local cli-progress bar.
|
|
185
209
|
*/
|
|
186
|
-
async uploadFile(label, dataPath) {
|
|
210
|
+
async uploadFile(label, dataPath, uncompress = false) {
|
|
187
211
|
const filePath = path_1.default.join(config_1.STORE_PATH, this.storeId, dataPath);
|
|
188
212
|
const { nonce, fileExists } = await this.getFileNonce(dataPath);
|
|
189
213
|
if (fileExists) {
|
|
@@ -223,8 +247,20 @@ class PropagationServer {
|
|
|
223
247
|
});
|
|
224
248
|
// Add inactivity timeout to the progress stream
|
|
225
249
|
const passThroughStream = this.addInactivityTimeout(new stream_1.PassThrough(), PropagationServer.inactivityTimeout);
|
|
226
|
-
//
|
|
227
|
-
|
|
250
|
+
// Decide whether to uncompress the file during upload
|
|
251
|
+
if (uncompress) {
|
|
252
|
+
// Create a gunzip (uncompression) stream
|
|
253
|
+
const gunzip = zlib.createGunzip();
|
|
254
|
+
// Pipe the streams: fileReadStream -> gunzip -> progressStream -> passThroughStream
|
|
255
|
+
fileReadStream
|
|
256
|
+
.pipe(gunzip)
|
|
257
|
+
.pipe(progressStream)
|
|
258
|
+
.pipe(passThroughStream);
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
// Pipe the streams: fileReadStream -> progressStream -> passThroughStream
|
|
262
|
+
fileReadStream.pipe(progressStream).pipe(passThroughStream);
|
|
263
|
+
}
|
|
228
264
|
// Use form-data to construct the request body
|
|
229
265
|
const formData = new form_data_1.default();
|
|
230
266
|
formData.append("file", passThroughStream);
|
|
@@ -311,7 +347,7 @@ class PropagationServer {
|
|
|
311
347
|
// Start the upload session
|
|
312
348
|
await propagationServer.startUploadSession(rootHash);
|
|
313
349
|
const dataStore = blockchain_1.DataStore.from(storeId);
|
|
314
|
-
const files = await dataStore.getFileSetForRootHash(rootHash);
|
|
350
|
+
const files = await dataStore.getFileSetForRootHash(rootHash, true);
|
|
315
351
|
// Prepare upload tasks
|
|
316
352
|
const uploadTasks = files.map((file) => ({
|
|
317
353
|
label: file.name,
|
|
@@ -319,8 +355,8 @@ class PropagationServer {
|
|
|
319
355
|
}));
|
|
320
356
|
// Limit the number of concurrent uploads
|
|
321
357
|
const concurrencyLimit = 10; // Adjust this number as needed
|
|
322
|
-
await (0,
|
|
323
|
-
await propagationServer.uploadFile(task.label, task.dataPath);
|
|
358
|
+
await (0, promiseUtils_1.asyncPool)(concurrencyLimit, uploadTasks, async (task) => {
|
|
359
|
+
await propagationServer.uploadFile(task.label, task.dataPath, true);
|
|
324
360
|
});
|
|
325
361
|
// Commit the session after all files have been uploaded
|
|
326
362
|
await propagationServer.commitUploadSession();
|
|
@@ -520,7 +556,7 @@ class PropagationServer {
|
|
|
520
556
|
const tempDir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "downloadStore-"));
|
|
521
557
|
try {
|
|
522
558
|
// Download files to the temporary directory
|
|
523
|
-
await (0,
|
|
559
|
+
await (0, promiseUtils_1.asyncPool)(concurrencyLimit, downloadTasks, async (task) => {
|
|
524
560
|
await propagationServer.downloadFile(task.label, task.dataPath, rootHash, tempDir);
|
|
525
561
|
});
|
|
526
562
|
// Save the rootHash.dat file to the temporary directory
|
|
@@ -59,7 +59,7 @@ export declare class DataStore {
|
|
|
59
59
|
* @param {string} rootHash - The root hash to fetch the file set.
|
|
60
60
|
* @returns {Promise<{ fileName: string, file: string }[]>} - An array of unique file objects.
|
|
61
61
|
*/
|
|
62
|
-
getFileSetForRootHash(rootHash: string): Promise<{
|
|
62
|
+
getFileSetForRootHash(rootHash: string, skipIntegirtyCheck?: boolean): Promise<{
|
|
63
63
|
name: string;
|
|
64
64
|
path: string;
|
|
65
65
|
}[]>;
|
|
@@ -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;AAWtC,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":"DataStore.d.ts","sourceRoot":"","sources":["../../src/blockchain/DataStore.ts"],"names":[],"mappings":"AAEA,OAAO,EAOL,SAAS,IAAI,eAAe,EAE5B,iBAAiB,EAIlB,MAAM,8BAA8B,CAAC;AAWtC,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;AAcjD,qBAAa,SAAS;IACpB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,IAAI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AAC,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;IA+C3B;;;;;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 StoreInfoCacheUpdater_1 = require("./StoreInfoCacheUpdater");
|
|
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);
|
|
@@ -202,47 +203,26 @@ class DataStore {
|
|
|
202
203
|
}
|
|
203
204
|
async fetchCoinInfo() {
|
|
204
205
|
try {
|
|
206
|
+
// Kick off the updater instance in the backgounf if not already running
|
|
207
|
+
StoreInfoCacheUpdater_1.StoreInfoCacheUpdater.initInstance();
|
|
205
208
|
// Initialize the cache for the current storeId's coin info
|
|
206
209
|
const storeCoinCache = new FileCache_1.FileCache(`stores`);
|
|
207
210
|
// Try to get cached store info
|
|
208
211
|
const cachedInfo = storeCoinCache.get(this.storeId);
|
|
209
212
|
if (cachedInfo) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
const latestHash = await peer.getHeaderHash(latestHeight);
|
|
222
|
-
// Serialize the store data for caching
|
|
223
|
-
const serializedLatestStore = new DataStoreSerializer_1.DataStoreSerializer(latestStore, latestHeight, latestHash).serialize();
|
|
224
|
-
// Cache updated store info
|
|
225
|
-
storeCoinCache.set(this.storeId, {
|
|
226
|
-
latestStore: serializedLatestStore,
|
|
227
|
-
latestHeight,
|
|
228
|
-
latestHash: latestHash.toString("hex"),
|
|
229
|
-
});
|
|
230
|
-
return { latestStore, latestHeight, latestHash };
|
|
231
|
-
}
|
|
232
|
-
catch {
|
|
233
|
-
// Return cached info if sync fails
|
|
234
|
-
const { latestStore, latestHeight, latestHash } = DataStoreSerializer_1.DataStoreSerializer.deserialize({
|
|
235
|
-
latestStore: cachedInfo.latestStore,
|
|
236
|
-
latestHeight: cachedInfo.latestHeight.toString(),
|
|
237
|
-
latestHash: cachedInfo.latestHash,
|
|
238
|
-
});
|
|
239
|
-
return {
|
|
240
|
-
latestStore,
|
|
241
|
-
latestHeight,
|
|
242
|
-
latestHash: latestHash,
|
|
243
|
-
};
|
|
244
|
-
}
|
|
213
|
+
// If we have cached info, return it directly
|
|
214
|
+
const { latestStore, latestHeight } = DataStoreSerializer_1.DataStoreSerializer.deserialize({
|
|
215
|
+
latestStore: cachedInfo.latestStore,
|
|
216
|
+
latestHeight: cachedInfo.latestHeight.toString(),
|
|
217
|
+
latestHash: cachedInfo.latestHash,
|
|
218
|
+
});
|
|
219
|
+
return {
|
|
220
|
+
latestStore,
|
|
221
|
+
latestHeight: cachedInfo.latestHeight,
|
|
222
|
+
latestHash: Buffer.from(cachedInfo.latestHash, "hex"),
|
|
223
|
+
};
|
|
245
224
|
}
|
|
225
|
+
// If no cached info, proceed to fetch and cache it
|
|
246
226
|
// Use getCreationHeight to retrieve height and hash information
|
|
247
227
|
const { createdAtHeight, createdAtHash } = await this.getCreationHeight();
|
|
248
228
|
// Sync store from peer
|
|
@@ -384,7 +364,7 @@ class DataStore {
|
|
|
384
364
|
* @param {string} rootHash - The root hash to fetch the file set.
|
|
385
365
|
* @returns {Promise<{ fileName: string, file: string }[]>} - An array of unique file objects.
|
|
386
366
|
*/
|
|
387
|
-
async getFileSetForRootHash(rootHash) {
|
|
367
|
+
async getFileSetForRootHash(rootHash, skipIntegirtyCheck = false) {
|
|
388
368
|
const datFilePath = path_1.default.join(config_1.STORE_PATH, this.storeId, `${rootHash}.dat`);
|
|
389
369
|
const datFileContent = JSON.parse(fs_1.default.readFileSync(datFilePath, "utf-8"));
|
|
390
370
|
// Use a Set to ensure uniqueness
|
|
@@ -393,8 +373,12 @@ class DataStore {
|
|
|
393
373
|
for (const [fileKey, fileData] of Object.entries(datFileContent.files)) {
|
|
394
374
|
const filePath = (0, hashUtils_1.getFilePathFromSha256)(datFileContent.files[fileKey].sha256, "data");
|
|
395
375
|
// Perform the integrity check
|
|
396
|
-
|
|
397
|
-
if (
|
|
376
|
+
let integrityCheck;
|
|
377
|
+
if (!skipIntegirtyCheck) {
|
|
378
|
+
integrityCheck =
|
|
379
|
+
await DataIntegrityTree_1.DataIntegrityTree.validateKeyIntegrityWithForeignTree(fileKey, datFileContent.files[fileKey].sha256, datFileContent, rootHash, path_1.default.join(config_1.STORE_PATH, this.storeId, "data"));
|
|
380
|
+
}
|
|
381
|
+
if (integrityCheck || skipIntegirtyCheck) {
|
|
398
382
|
// Add the file to the Set
|
|
399
383
|
filesInvolved.add({
|
|
400
384
|
name: Buffer.from(fileKey, "hex").toString("utf-8"),
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare class StoreInfoCacheUpdater {
|
|
2
|
+
private static instance;
|
|
3
|
+
private storeCoinCache;
|
|
4
|
+
private updateInterval;
|
|
5
|
+
private lockFilePath;
|
|
6
|
+
private releaseLock;
|
|
7
|
+
private constructor();
|
|
8
|
+
static initInstance(): StoreInfoCacheUpdater;
|
|
9
|
+
private startCacheUpdater;
|
|
10
|
+
private updateCache;
|
|
11
|
+
private setupExitHandlers;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=StoreInfoCacheUpdater.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StoreInfoCacheUpdater.d.ts","sourceRoot":"","sources":["../../src/blockchain/StoreInfoCacheUpdater.ts"],"names":[],"mappings":"AAUA,qBAAa,qBAAqB;IAChC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAwB;IAC/C,OAAO,CAAC,cAAc,CAInB;IACH,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,WAAW,CAAsC;IAEzD,OAAO;WAaO,YAAY,IAAI,qBAAqB;IAOnD,OAAO,CAAC,iBAAiB;YAIX,WAAW;IAgGzB,OAAO,CAAC,iBAAiB;CAsB1B"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.StoreInfoCacheUpdater = void 0;
|
|
27
|
+
const timers_1 = require("timers");
|
|
28
|
+
const FullNodePeer_1 = require("./FullNodePeer");
|
|
29
|
+
const utils_1 = require("../utils");
|
|
30
|
+
const DataStoreSerializer_1 = require("./DataStoreSerializer");
|
|
31
|
+
const utils_2 = require("../utils");
|
|
32
|
+
const lockfile = __importStar(require("proper-lockfile"));
|
|
33
|
+
const fs = __importStar(require("fs"));
|
|
34
|
+
const path = __importStar(require("path"));
|
|
35
|
+
const utils_3 = require("../utils");
|
|
36
|
+
class StoreInfoCacheUpdater {
|
|
37
|
+
constructor(updateIntervalInMinutes = 5) {
|
|
38
|
+
this.releaseLock = null; // Holds the release function for cleanup
|
|
39
|
+
this.storeCoinCache = new utils_1.FileCache(`stores`);
|
|
40
|
+
this.updateInterval = updateIntervalInMinutes * 60 * 1000; // Convert minutes to milliseconds
|
|
41
|
+
// Construct lock file path using the path module
|
|
42
|
+
this.lockFilePath = path.join(utils_3.DIG_FOLDER_PATH, 'store-info-cache.lock');
|
|
43
|
+
this.startCacheUpdater();
|
|
44
|
+
// Set up process exit handlers for cleanup
|
|
45
|
+
this.setupExitHandlers();
|
|
46
|
+
}
|
|
47
|
+
static initInstance() {
|
|
48
|
+
if (!StoreInfoCacheUpdater.instance) {
|
|
49
|
+
StoreInfoCacheUpdater.instance = new StoreInfoCacheUpdater();
|
|
50
|
+
}
|
|
51
|
+
return StoreInfoCacheUpdater.instance;
|
|
52
|
+
}
|
|
53
|
+
startCacheUpdater() {
|
|
54
|
+
(0, timers_1.setInterval)(() => this.updateCache(), this.updateInterval);
|
|
55
|
+
}
|
|
56
|
+
async updateCache() {
|
|
57
|
+
try {
|
|
58
|
+
// Ensure the lock file exists before attempting to lock
|
|
59
|
+
if (!fs.existsSync(this.lockFilePath)) {
|
|
60
|
+
fs.writeFileSync(this.lockFilePath, '');
|
|
61
|
+
}
|
|
62
|
+
// Acquire a file lock with stale lock duration matching the update interval
|
|
63
|
+
const release = await lockfile.lock(this.lockFilePath, {
|
|
64
|
+
retries: {
|
|
65
|
+
retries: 10, // Retry 10 times to acquire the lock
|
|
66
|
+
factor: 2,
|
|
67
|
+
minTimeout: 100,
|
|
68
|
+
maxTimeout: 1000,
|
|
69
|
+
},
|
|
70
|
+
stale: this.updateInterval, // Lock expires after the update interval
|
|
71
|
+
});
|
|
72
|
+
// Store the release function for cleanup during process exit
|
|
73
|
+
this.releaseLock = release;
|
|
74
|
+
try {
|
|
75
|
+
const storeIds = this.storeCoinCache.getCachedKeys();
|
|
76
|
+
for (const storeId of storeIds) {
|
|
77
|
+
try {
|
|
78
|
+
const cachedInfo = this.storeCoinCache.get(storeId);
|
|
79
|
+
if (!cachedInfo) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
// Deserialize the cached store info
|
|
83
|
+
const { latestStore: serializedStore, latestHeight, latestHash } = cachedInfo;
|
|
84
|
+
const { latestStore: previousInfo } = DataStoreSerializer_1.DataStoreSerializer.deserialize({
|
|
85
|
+
latestStore: serializedStore,
|
|
86
|
+
latestHeight: latestHeight.toString(),
|
|
87
|
+
latestHash: latestHash,
|
|
88
|
+
});
|
|
89
|
+
// Wrap the connection with a timeout
|
|
90
|
+
const peer = await (0, utils_2.withTimeout)(FullNodePeer_1.FullNodePeer.connect(), 60000, "Timeout connecting to FullNodePeer");
|
|
91
|
+
// Wrap the syncStore call with a timeout
|
|
92
|
+
const { latestStore, latestHeight: newHeight } = await (0, utils_2.withTimeout)(peer.syncStore(previousInfo, latestHeight, Buffer.from(latestHash, "hex"), false), 60000, `Timeout syncing store for storeId ${storeId}`);
|
|
93
|
+
// Wrap the getHeaderHash call with a timeout
|
|
94
|
+
const latestHashBuffer = await (0, utils_2.withTimeout)(peer.getHeaderHash(newHeight), 60000, `Timeout getting header hash for height ${newHeight}`);
|
|
95
|
+
// Serialize the updated store data for caching
|
|
96
|
+
const serializedLatestStore = new DataStoreSerializer_1.DataStoreSerializer(latestStore, newHeight, latestHashBuffer).serialize();
|
|
97
|
+
// Recache the updated store info
|
|
98
|
+
this.storeCoinCache.set(storeId, {
|
|
99
|
+
latestStore: serializedLatestStore,
|
|
100
|
+
latestHeight: newHeight,
|
|
101
|
+
latestHash: latestHashBuffer.toString("hex"),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
console.error(`Failed to update cache for storeId ${storeId}:`, error);
|
|
106
|
+
// Optionally handle specific errors or continue with the next storeId
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
// Always release the lock after finishing the update
|
|
112
|
+
await this.releaseLock?.();
|
|
113
|
+
this.releaseLock = null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
console.error("Failed to update store cache:", error);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
setupExitHandlers() {
|
|
121
|
+
const cleanup = async () => {
|
|
122
|
+
if (this.releaseLock) {
|
|
123
|
+
try {
|
|
124
|
+
await this.releaseLock();
|
|
125
|
+
console.log("Lock released successfully on process exit.");
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
console.error("Failed to release lock on exit:", error);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
// Listen for process exit events and call cleanup
|
|
133
|
+
process.on('SIGINT', cleanup); // Catch CTRL+C
|
|
134
|
+
process.on('SIGTERM', cleanup); // Catch termination signals
|
|
135
|
+
process.on('exit', cleanup); // On normal exit
|
|
136
|
+
process.on('uncaughtException', async (error) => {
|
|
137
|
+
console.error("Uncaught exception, cleaning up:", error);
|
|
138
|
+
await cleanup();
|
|
139
|
+
process.exit(1); // Ensure process exits after handling exception
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.StoreInfoCacheUpdater = StoreInfoCacheUpdater;
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,OAAO,CAAC;AACtB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,OAAO,CAAC;AACtB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC"}
|
package/dist/utils/index.js
CHANGED
|
@@ -27,4 +27,4 @@ __exportStar(require("./spinnerUtils"), exports);
|
|
|
27
27
|
__exportStar(require("./ssl"), exports);
|
|
28
28
|
__exportStar(require("./validationUtils"), exports);
|
|
29
29
|
__exportStar(require("./Environment"), exports);
|
|
30
|
-
__exportStar(require("./
|
|
30
|
+
__exportStar(require("./promiseUtils"), exports);
|
|
@@ -6,4 +6,12 @@
|
|
|
6
6
|
* @returns {Promise<Array<R>>} - A promise that resolves when all items have been processed.
|
|
7
7
|
*/
|
|
8
8
|
export declare function asyncPool<T, R>(limit: number, items: T[], iteratorFn: (item: T) => Promise<R>): Promise<R[]>;
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Helper function to add a timeout to a promise.
|
|
11
|
+
* @param promise The original promise.
|
|
12
|
+
* @param ms Timeout in milliseconds.
|
|
13
|
+
* @param timeoutMessage The error message when the timeout is reached.
|
|
14
|
+
* @returns Promise that resolves before the timeout or rejects with an error.
|
|
15
|
+
*/
|
|
16
|
+
export declare const withTimeout: <T>(promise: Promise<T>, ms: number, timeoutMessage: string) => Promise<T>;
|
|
17
|
+
//# sourceMappingURL=promiseUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promiseUtils.d.ts","sourceRoot":"","sources":["../../src/utils/promiseUtils.ts"],"names":[],"mappings":"AACA;;;;;;GAMG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,CAAC,EAChC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,CAAC,EAAE,EACV,UAAU,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAClC,OAAO,CAAC,CAAC,EAAE,CAAC,CAgBd;AAED;;;;;;EAMC;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,WAClB,OAAO,CAAC,CAAC,CAAC,MACf,MAAM,kBACM,MAAM,KACrB,OAAO,CAAC,CAAC,CAOX,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withTimeout = void 0;
|
|
3
4
|
exports.asyncPool = asyncPool;
|
|
4
5
|
/**
|
|
5
6
|
* Processes items in sequential batches with a concurrency limit.
|
|
@@ -21,3 +22,17 @@ async function asyncPool(limit, items, iteratorFn) {
|
|
|
21
22
|
}
|
|
22
23
|
return ret;
|
|
23
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Helper function to add a timeout to a promise.
|
|
27
|
+
* @param promise The original promise.
|
|
28
|
+
* @param ms Timeout in milliseconds.
|
|
29
|
+
* @param timeoutMessage The error message when the timeout is reached.
|
|
30
|
+
* @returns Promise that resolves before the timeout or rejects with an error.
|
|
31
|
+
*/
|
|
32
|
+
const withTimeout = (promise, ms, timeoutMessage) => {
|
|
33
|
+
return Promise.race([
|
|
34
|
+
promise,
|
|
35
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error(timeoutMessage)), ms)),
|
|
36
|
+
]);
|
|
37
|
+
};
|
|
38
|
+
exports.withTimeout = withTimeout;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dignetwork/dig-sdk",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.108",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -46,6 +46,7 @@
|
|
|
46
46
|
"nconf": "^0.12.1",
|
|
47
47
|
"node-cache": "^5.1.2",
|
|
48
48
|
"progress-stream": "^2.0.0",
|
|
49
|
+
"proper-lockfile": "^4.1.2",
|
|
49
50
|
"superagent": "^10.0.0",
|
|
50
51
|
"unzipper": "^0.12.3"
|
|
51
52
|
},
|
|
@@ -61,6 +62,7 @@
|
|
|
61
62
|
"@types/nconf": "^0.10.7",
|
|
62
63
|
"@types/node": "^22.1.0",
|
|
63
64
|
"@types/progress-stream": "^2.0.5",
|
|
65
|
+
"@types/proper-lockfile": "^4.1.4",
|
|
64
66
|
"@types/superagent": "^8.1.8",
|
|
65
67
|
"@types/unzipper": "^0.10.10",
|
|
66
68
|
"chai": "^5.1.1",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"asyncPool.d.ts","sourceRoot":"","sources":["../../src/utils/asyncPool.ts"],"names":[],"mappings":"AACA;;;;;;GAMG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,CAAC,EAChC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,CAAC,EAAE,EACV,UAAU,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAClC,OAAO,CAAC,CAAC,EAAE,CAAC,CAgBd"}
|