@dignetwork/dig-sdk 0.0.1-alpha.170 → 0.0.1-alpha.172
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/ContentServer.d.ts.map +1 -1
- package/dist/DigNetwork/ContentServer.js +14 -16
- package/dist/DigNetwork/PropagationServer.d.ts.map +1 -1
- package/dist/DigNetwork/PropagationServer.js +17 -19
- package/dist/blockchain/DataStore.d.ts.map +1 -1
- package/dist/blockchain/DataStore.js +4 -5
- package/dist/blockchain/FullNodePeer.d.ts.map +1 -1
- package/dist/blockchain/FullNodePeer.js +11 -10
- package/dist/blockchain/ServerCoin.d.ts.map +1 -1
- package/dist/blockchain/ServerCoin.js +6 -8
- package/dist/utils/DigCache.d.ts +38 -0
- package/dist/utils/DigCache.d.ts.map +1 -0
- package/dist/utils/DigCache.js +236 -0
- package/dist/utils/PeerRanker.js +4 -4
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/promiseUtils.d.ts +8 -0
- package/dist/utils/promiseUtils.d.ts.map +1 -1
- package/dist/utils/promiseUtils.js +27 -1
- package/package.json +2 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ContentServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/ContentServer.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"ContentServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/ContentServer.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAMlC,qBAAa,aAAa;IACxB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CA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AI;YA0EJ,SAAS;YAMT,gBAAgB;YAiChB,KAAK;YA+GL,eAAe;CA4G9B"}
|
|
@@ -7,17 +7,15 @@ exports.ContentServer = void 0;
|
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const http_1 = __importDefault(require("http"));
|
|
9
9
|
const url_1 = require("url");
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const hasRootHashCache = new node_cache_1.default({ stdTTL: 86400 });
|
|
14
|
-
const wellKnownCache = new node_cache_1.default({ stdTTL: 86400 });
|
|
10
|
+
const utils_1 = require("../utils");
|
|
11
|
+
const hasRootHashCache = new utils_1.DigCache({ stdTTL: 86400 });
|
|
12
|
+
const wellKnownCache = new utils_1.DigCache({ stdTTL: 86400 });
|
|
15
13
|
class ContentServer {
|
|
16
14
|
constructor(ipAddress, storeId) {
|
|
17
15
|
this.ipAddress = ipAddress;
|
|
18
16
|
this.storeId = storeId;
|
|
19
17
|
if (!ContentServer.certPath || !ContentServer.keyPath) {
|
|
20
|
-
const { certPath, keyPath } = (0,
|
|
18
|
+
const { certPath, keyPath } = (0, utils_1.getOrCreateSSLCerts)();
|
|
21
19
|
ContentServer.certPath = certPath;
|
|
22
20
|
ContentServer.keyPath = keyPath;
|
|
23
21
|
}
|
|
@@ -25,7 +23,7 @@ class ContentServer {
|
|
|
25
23
|
// Method to get the content of a specified key from the peer, with optional challenge query
|
|
26
24
|
async getKey(key, rootHash, challengeHex) {
|
|
27
25
|
// Construct the base URL
|
|
28
|
-
let url = `https://${(0,
|
|
26
|
+
let url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${ContentServer.port}/chia.${this.storeId}.${rootHash}/${key}`;
|
|
29
27
|
// If a challenge is provided, append it as a query parameter
|
|
30
28
|
if (challengeHex) {
|
|
31
29
|
url += `?challenge=${challengeHex}`;
|
|
@@ -35,7 +33,7 @@ class ContentServer {
|
|
|
35
33
|
// New method to get only the first chunk of the content
|
|
36
34
|
async getKeyChunk(key, rootHash) {
|
|
37
35
|
// Construct the base URL
|
|
38
|
-
let url = `https://${(0,
|
|
36
|
+
let url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${ContentServer.port}/chia.${this.storeId}.${rootHash}/${key}`;
|
|
39
37
|
return this.fetchFirstChunk(url);
|
|
40
38
|
}
|
|
41
39
|
// Method to get the payment address from the peer
|
|
@@ -64,7 +62,7 @@ class ContentServer {
|
|
|
64
62
|
return cachedResult;
|
|
65
63
|
}
|
|
66
64
|
// If not cached, proceed to fetch the .well-known information
|
|
67
|
-
const url = `https://${(0,
|
|
65
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${ContentServer.port}/.well-known`;
|
|
68
66
|
try {
|
|
69
67
|
const data = await this.fetchJson(url);
|
|
70
68
|
wellKnownCache.set(cacheKey, data);
|
|
@@ -77,12 +75,12 @@ class ContentServer {
|
|
|
77
75
|
}
|
|
78
76
|
// Method to get the list of known stores
|
|
79
77
|
async getKnownStores() {
|
|
80
|
-
const url = `https://${(0,
|
|
78
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${ContentServer.port}/.well-known/stores`;
|
|
81
79
|
return this.fetchJson(url);
|
|
82
80
|
}
|
|
83
81
|
// Method to get the index of all stores
|
|
84
82
|
async getStoresIndex() {
|
|
85
|
-
const url = `https://${(0,
|
|
83
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${ContentServer.port}/`;
|
|
86
84
|
return this.fetchJson(url);
|
|
87
85
|
}
|
|
88
86
|
// Method to get the index of keys in a store
|
|
@@ -92,7 +90,7 @@ class ContentServer {
|
|
|
92
90
|
if (rootHash) {
|
|
93
91
|
udi += `.${rootHash}`;
|
|
94
92
|
}
|
|
95
|
-
const url = `https://${(0,
|
|
93
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${ContentServer.port}/${udi}`;
|
|
96
94
|
return this.fetchJson(url);
|
|
97
95
|
}
|
|
98
96
|
catch (error) {
|
|
@@ -109,7 +107,7 @@ class ContentServer {
|
|
|
109
107
|
if (rootHash) {
|
|
110
108
|
udi += `.${rootHash}`;
|
|
111
109
|
}
|
|
112
|
-
const url = `https://${(0,
|
|
110
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${ContentServer.port}/${udi}/${key}`;
|
|
113
111
|
return this.head(url);
|
|
114
112
|
}
|
|
115
113
|
catch (error) {
|
|
@@ -122,7 +120,7 @@ class ContentServer {
|
|
|
122
120
|
// Method to check if a specific store exists (HEAD request)
|
|
123
121
|
async headStore(options) {
|
|
124
122
|
try {
|
|
125
|
-
let url = `https://${(0,
|
|
123
|
+
let url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${ContentServer.port}/chia.${this.storeId}`;
|
|
126
124
|
if (options?.hasRootHash) {
|
|
127
125
|
url += `?hasRootHash=${options.hasRootHash}`;
|
|
128
126
|
}
|
|
@@ -146,7 +144,7 @@ class ContentServer {
|
|
|
146
144
|
// Construct the cache key using storeId and rootHash
|
|
147
145
|
const cacheKey = `${this.storeId}-${rootHash}`;
|
|
148
146
|
// Check if the result is already cached
|
|
149
|
-
const cachedResult = hasRootHashCache.get(cacheKey);
|
|
147
|
+
const cachedResult = await hasRootHashCache.get(cacheKey);
|
|
150
148
|
if (cachedResult !== undefined) {
|
|
151
149
|
return cachedResult;
|
|
152
150
|
}
|
|
@@ -168,7 +166,7 @@ class ContentServer {
|
|
|
168
166
|
udi += `.${rootHash}`;
|
|
169
167
|
}
|
|
170
168
|
return new Promise((resolve, reject) => {
|
|
171
|
-
const url = `https://${(0,
|
|
169
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${ContentServer.port}/${udi}/${key}`;
|
|
172
170
|
const urlObj = new url_1.URL(url);
|
|
173
171
|
const requestOptions = {
|
|
174
172
|
hostname: urlObj.hostname,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PropagationServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/PropagationServer.ts"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IAyDzC;;OAEG;IACG,YAAY,CAChB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,OAAO,CAAA;KAAE,CAAC;IA2BlD;;;OAGG;IACG,UAAU,CACd,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,UAAU,GAAE,OAAe;IAsH7B;;OAEG;IACG,mBAAmB;IAkCzB;;OAEG;WACU,WAAW,CACtB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM;IA0DnB;;;OAGG;IACG,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAmFlD;;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;IA4GjB;;OAEG;WACU,aAAa,CACxB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM;CA2FpB"}
|
|
@@ -40,18 +40,16 @@ const zlib = __importStar(require("zlib"));
|
|
|
40
40
|
const promiseUtils_1 = require("../utils/promiseUtils");
|
|
41
41
|
const nanospinner_1 = require("nanospinner");
|
|
42
42
|
const hashUtils_1 = require("../utils/hashUtils");
|
|
43
|
-
const ssl_1 = require("../utils/ssl");
|
|
44
43
|
const colorette_1 = require("colorette");
|
|
45
44
|
const merkle_1 = require("../utils/merkle");
|
|
46
45
|
const stream_1 = require("stream");
|
|
47
46
|
const credentialsUtils_1 = require("../utils/credentialsUtils");
|
|
48
47
|
const config_1 = require("../utils/config");
|
|
49
48
|
const blockchain_1 = require("../blockchain");
|
|
50
|
-
const
|
|
51
|
-
const node_cache_1 = __importDefault(require("node-cache"));
|
|
49
|
+
const utils_1 = require("../utils");
|
|
52
50
|
// Initialize cache with a TTL of 1 week (604800 seconds)
|
|
53
|
-
const storeExistsCache = new
|
|
54
|
-
const pingUpdatecache = new
|
|
51
|
+
const storeExistsCache = new utils_1.DigCache({ stdTTL: 86400 });
|
|
52
|
+
const pingUpdatecache = new utils_1.DigCache({ stdTTL: 86400 });
|
|
55
53
|
// Helper function to trim long filenames with ellipsis and ensure consistent padding
|
|
56
54
|
function formatFilename(filename, maxLength = 30) {
|
|
57
55
|
if (!filename) {
|
|
@@ -69,7 +67,7 @@ class PropagationServer {
|
|
|
69
67
|
this.publicKey = "";
|
|
70
68
|
this.ipAddress = ipAddress;
|
|
71
69
|
// Get or create SSL certificates
|
|
72
|
-
const { certPath, keyPath } = (0,
|
|
70
|
+
const { certPath, keyPath } = (0, utils_1.getOrCreateSSLCerts)();
|
|
73
71
|
this.certPath = certPath;
|
|
74
72
|
this.keyPath = keyPath;
|
|
75
73
|
}
|
|
@@ -97,7 +95,7 @@ class PropagationServer {
|
|
|
97
95
|
async getMyPublicIp(rootHash) {
|
|
98
96
|
try {
|
|
99
97
|
const httpsAgent = this.createHttpsAgent();
|
|
100
|
-
const url = `https://${(0,
|
|
98
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${PropagationServer.port}/peer`;
|
|
101
99
|
const config = {
|
|
102
100
|
httpsAgent,
|
|
103
101
|
headers: {
|
|
@@ -125,12 +123,12 @@ class PropagationServer {
|
|
|
125
123
|
async pingUpdate(rootHash) {
|
|
126
124
|
const cacheKey = `${this.ipAddress}-${this.storeId}-${rootHash}`;
|
|
127
125
|
// Check if response for this combination is already cached
|
|
128
|
-
if (pingUpdatecache.get(cacheKey) === "successfully synced") {
|
|
126
|
+
if ((await pingUpdatecache.get(cacheKey)) === "successfully synced") {
|
|
129
127
|
return;
|
|
130
128
|
}
|
|
131
129
|
try {
|
|
132
130
|
const httpsAgent = this.createHttpsAgent();
|
|
133
|
-
const url = `https://${(0,
|
|
131
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${PropagationServer.port}/update`;
|
|
134
132
|
const config = {
|
|
135
133
|
httpsAgent,
|
|
136
134
|
headers: {
|
|
@@ -144,7 +142,7 @@ class PropagationServer {
|
|
|
144
142
|
};
|
|
145
143
|
try {
|
|
146
144
|
const response = await axios_1.default.post(url, data, config);
|
|
147
|
-
console.log((0, colorette_1.green)(`✔ Successfully pinged peer: ${this.ipAddress}`), response.data);
|
|
145
|
+
console.log((0, colorette_1.green)(`✔ Successfully pinged peer: ${this.ipAddress} ${this.storeId}`), response.data);
|
|
148
146
|
if (response.data.message === "Already Synced") {
|
|
149
147
|
// Cache the response as "successfully synced" to avoid redundant requests
|
|
150
148
|
pingUpdatecache.set(cacheKey, "successfully synced");
|
|
@@ -194,7 +192,7 @@ class PropagationServer {
|
|
|
194
192
|
? `${this.storeId}-${rootHash}`
|
|
195
193
|
: `${this.storeId}-nohash`;
|
|
196
194
|
// Check if the result is already cached
|
|
197
|
-
const cachedResult = storeExistsCache.get(cacheKey);
|
|
195
|
+
const cachedResult = await storeExistsCache.get(cacheKey);
|
|
198
196
|
if (cachedResult !== undefined) {
|
|
199
197
|
return cachedResult;
|
|
200
198
|
}
|
|
@@ -204,7 +202,7 @@ class PropagationServer {
|
|
|
204
202
|
const config = {
|
|
205
203
|
httpsAgent: this.createHttpsAgent(),
|
|
206
204
|
};
|
|
207
|
-
let url = `https://${(0,
|
|
205
|
+
let url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${PropagationServer.port}/${this.storeId}`;
|
|
208
206
|
if (rootHash) {
|
|
209
207
|
url += `?hasRootHash=${rootHash}`;
|
|
210
208
|
}
|
|
@@ -260,7 +258,7 @@ class PropagationServer {
|
|
|
260
258
|
password: this.password,
|
|
261
259
|
};
|
|
262
260
|
}
|
|
263
|
-
const url = `https://${(0,
|
|
261
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${PropagationServer.port}/upload/${this.storeId}?roothash=${rootHash}`;
|
|
264
262
|
const response = await axios_1.default.post(url, formData, config);
|
|
265
263
|
this.sessionId = response.data.sessionId;
|
|
266
264
|
spinner.success({
|
|
@@ -281,7 +279,7 @@ class PropagationServer {
|
|
|
281
279
|
const config = {
|
|
282
280
|
httpsAgent: this.createHttpsAgent(),
|
|
283
281
|
};
|
|
284
|
-
const url = `https://${(0,
|
|
282
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${PropagationServer.port}/upload/${this.storeId}/${this.sessionId}/${filename}`;
|
|
285
283
|
const response = await axios_1.default.head(url, config);
|
|
286
284
|
// Check for 'x-file-exists' header
|
|
287
285
|
const fileExists = response.headers["x-file-exists"] === "true";
|
|
@@ -367,7 +365,7 @@ class PropagationServer {
|
|
|
367
365
|
maxContentLength: Infinity,
|
|
368
366
|
maxBodyLength: Infinity,
|
|
369
367
|
};
|
|
370
|
-
const url = `https://${(0,
|
|
368
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${PropagationServer.port}/upload/${this.storeId}/${this.sessionId}/${dataPath}`;
|
|
371
369
|
// Create a promise that resolves when the progress stream ends
|
|
372
370
|
const progressPromise = new Promise((resolve, reject) => {
|
|
373
371
|
progressStream.on("end", resolve);
|
|
@@ -402,7 +400,7 @@ class PropagationServer {
|
|
|
402
400
|
}
|
|
403
401
|
: undefined,
|
|
404
402
|
};
|
|
405
|
-
const url = `https://${(0,
|
|
403
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${PropagationServer.port}/commit/${this.storeId}/${this.sessionId}`;
|
|
406
404
|
const response = await axios_1.default.post(url, {}, config);
|
|
407
405
|
spinner.success({
|
|
408
406
|
text: (0, colorette_1.green)(`Upload session ${this.sessionId} successfully committed.`),
|
|
@@ -458,7 +456,7 @@ class PropagationServer {
|
|
|
458
456
|
* Logs progress using a local cli-progress bar.
|
|
459
457
|
*/
|
|
460
458
|
async fetchFile(dataPath) {
|
|
461
|
-
const url = `https://${(0,
|
|
459
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${PropagationServer.port}/fetch/${this.storeId}/${dataPath}`;
|
|
462
460
|
const config = {
|
|
463
461
|
responseType: "stream",
|
|
464
462
|
httpsAgent: this.createHttpsAgent(),
|
|
@@ -526,7 +524,7 @@ class PropagationServer {
|
|
|
526
524
|
const config = {
|
|
527
525
|
httpsAgent: this.createHttpsAgent(),
|
|
528
526
|
};
|
|
529
|
-
const url = `https://${(0,
|
|
527
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${PropagationServer.port}/store/${this.storeId}/${rootHash}/${dataPath}`;
|
|
530
528
|
const response = await axios_1.default.head(url, config);
|
|
531
529
|
// Check the headers for file existence and size
|
|
532
530
|
const fileExists = response.headers["x-file-exists"] === "true";
|
|
@@ -546,7 +544,7 @@ class PropagationServer {
|
|
|
546
544
|
* Logs progress using a local cli-progress bar.
|
|
547
545
|
*/
|
|
548
546
|
async downloadFile(label, dataPath, rootHash, baseDir) {
|
|
549
|
-
const url = `https://${(0,
|
|
547
|
+
const url = `https://${(0, utils_1.formatHost)(this.ipAddress)}:${PropagationServer.port}/fetch/${this.storeId}/${dataPath}`;
|
|
550
548
|
let downloadPath = path_1.default.join(baseDir, dataPath);
|
|
551
549
|
// Ensure that the directory for the file exists
|
|
552
550
|
fs_1.default.mkdirSync(path_1.default.dirname(downloadPath), { recursive: true });
|
|
@@ -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,EAAE,MAAM,UAAU,CAAC;AAG3C,OAAO,EACL,iBAAiB,EACjB,wBAAwB,EACzB,MAAM,sBAAsB,C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AAO,CAAC;QACpC,WAAW,EAAE,eAAe,CAAC;QAC7B,YAAY,EAAE,MAAM,CAAC;QACrB,UAAU,EAAE,MAAM,CAAC;KACpB,CAAC;IAkCW,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;IA8C/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"}
|
|
@@ -18,12 +18,11 @@ const DataIntegrityTree_1 = require("../DataIntegrityTree");
|
|
|
18
18
|
const prompts_1 = require("../prompts");
|
|
19
19
|
const FileCache_1 = require("../utils/FileCache");
|
|
20
20
|
const DataStoreSerializer_1 = require("./DataStoreSerializer");
|
|
21
|
-
const node_cache_1 = __importDefault(require("node-cache"));
|
|
22
21
|
const config_2 = require("../utils/config");
|
|
23
22
|
const StoreMonitorRegistry_1 = require("./StoreMonitorRegistry");
|
|
23
|
+
const utils_1 = require("../utils");
|
|
24
24
|
// Initialize the cache with a TTL of 180 seconds (3 minutes)
|
|
25
|
-
const rootHistoryCache = new
|
|
26
|
-
const allStoresCache = new node_cache_1.default({ stdTTL: 15 });
|
|
25
|
+
const rootHistoryCache = new utils_1.DigCache({ stdTTL: 180 });
|
|
27
26
|
const stat = (0, util_1.promisify)(fs_1.default.stat);
|
|
28
27
|
const readdir = (0, util_1.promisify)(fs_1.default.readdir);
|
|
29
28
|
class DataStore {
|
|
@@ -223,7 +222,7 @@ class DataStore {
|
|
|
223
222
|
const deserializedStore = DataStoreSerializer_1.DataStoreSerializer.deserialize({
|
|
224
223
|
latestStore: cachedInfo.latestStore,
|
|
225
224
|
latestHeight: cachedInfo.latestHeight.toString(),
|
|
226
|
-
latestHash: cachedInfo.latestHash
|
|
225
|
+
latestHash: cachedInfo.latestHash,
|
|
227
226
|
});
|
|
228
227
|
return {
|
|
229
228
|
latestStore: deserializedStore.latestStore,
|
|
@@ -275,7 +274,7 @@ class DataStore {
|
|
|
275
274
|
rootHistoryCache.del(this.storeId);
|
|
276
275
|
}
|
|
277
276
|
// Check if the root history is cached for this storeId
|
|
278
|
-
const cachedHistory = rootHistoryCache.get(this.storeId);
|
|
277
|
+
const cachedHistory = await rootHistoryCache.get(this.storeId);
|
|
279
278
|
if (cachedHistory) {
|
|
280
279
|
return cachedHistory;
|
|
281
280
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FullNodePeer.d.ts","sourceRoot":"","sources":["../../src/blockchain/FullNodePeer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,IAAI,EAAO,MAAM,8BAA8B,CAAC;AAgCzD;;GAEG;AACH,qBAAa,YAAY;IAqCH,OAAO,CAAC,IAAI;IAnChC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAA6B;IAGpD,OAAO,CAAC,MAAM,CAAC,aAAa,CAEzB;IAGH,OAAO,CAAC,MAAM,CAAC,cAAc,
|
|
1
|
+
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U;IAIzB;;;OAGG;YACW,WAAW;IAuEzB,OAAO,CAAC,eAAe;IAqDvB;;;OAGG;WACW,uBAAuB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAmB3D;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,aAAa;IAS5B;;;;OAIG;WACiB,mBAAmB,CACrC,cAAc,EAAE,MAAM,GACrB,OAAO,CAAC,OAAO,CAAC;CAwBpB"}
|
|
@@ -13,7 +13,7 @@ const net_1 = __importDefault(require("net"));
|
|
|
13
13
|
const nanospinner_1 = require("nanospinner");
|
|
14
14
|
const config_1 = require("../utils/config");
|
|
15
15
|
const Environment_1 = require("../utils/Environment");
|
|
16
|
-
const
|
|
16
|
+
const utils_1 = require("../utils");
|
|
17
17
|
const bottleneck_1 = __importDefault(require("bottleneck"));
|
|
18
18
|
const FULLNODE_PORT = 8444;
|
|
19
19
|
const LOCALHOST = "127.0.0.1";
|
|
@@ -147,7 +147,7 @@ class FullNodePeer {
|
|
|
147
147
|
return priorityIps;
|
|
148
148
|
}
|
|
149
149
|
// Check if cached peer IPs exist
|
|
150
|
-
const cachedPeerIPs = FullNodePeer.peerIPCache.get("peerIPs");
|
|
150
|
+
const cachedPeerIPs = await FullNodePeer.peerIPCache.get("peerIPs");
|
|
151
151
|
if (cachedPeerIPs) {
|
|
152
152
|
return cachedPeerIPs;
|
|
153
153
|
}
|
|
@@ -155,14 +155,14 @@ class FullNodePeer {
|
|
|
155
155
|
const fetchedPeers = [];
|
|
156
156
|
for (const DNS_HOST of DNS_HOSTS) {
|
|
157
157
|
// Check if DNS_HOST is in failedDNSCache
|
|
158
|
-
if (FullNodePeer.failedDNSCache.has(DNS_HOST)) {
|
|
158
|
+
if (await FullNodePeer.failedDNSCache.has(DNS_HOST)) {
|
|
159
159
|
continue;
|
|
160
160
|
}
|
|
161
161
|
try {
|
|
162
162
|
let ips = [];
|
|
163
163
|
// Check if DNS_HOST's IPs are cached
|
|
164
|
-
if (FullNodePeer.dnsCache.has(DNS_HOST)) {
|
|
165
|
-
ips = FullNodePeer.dnsCache.get(DNS_HOST) || [];
|
|
164
|
+
if (await FullNodePeer.dnsCache.has(DNS_HOST)) {
|
|
165
|
+
ips = (await FullNodePeer.dnsCache.get(DNS_HOST)) || [];
|
|
166
166
|
}
|
|
167
167
|
else {
|
|
168
168
|
// Resolve DNS_HOST and cache the results
|
|
@@ -242,7 +242,8 @@ class FullNodePeer {
|
|
|
242
242
|
return availablePrioritizedPeers[0];
|
|
243
243
|
}
|
|
244
244
|
// If no prioritized peers are available, proceed with round-robin among other peers
|
|
245
|
-
const regularPeers = Array.from(FullNodePeer.peerWeights.keys()).filter((ip) => !FullNodePeer.prioritizedPeers.includes(ip) &&
|
|
245
|
+
const regularPeers = Array.from(FullNodePeer.peerWeights.keys()).filter((ip) => !FullNodePeer.prioritizedPeers.includes(ip) &&
|
|
246
|
+
!FullNodePeer.cooldownCache.has(ip));
|
|
246
247
|
if (regularPeers.length === 0) {
|
|
247
248
|
throw new Error("No available peers to connect.");
|
|
248
249
|
}
|
|
@@ -432,11 +433,11 @@ exports.FullNodePeer = FullNodePeer;
|
|
|
432
433
|
// Singleton instance
|
|
433
434
|
FullNodePeer.instance = null;
|
|
434
435
|
// Cooldown cache to exclude faulty peers temporarily
|
|
435
|
-
FullNodePeer.cooldownCache = new
|
|
436
|
+
FullNodePeer.cooldownCache = new utils_1.DigCache({
|
|
436
437
|
stdTTL: COOLDOWN_DURATION / 1000,
|
|
437
438
|
});
|
|
438
439
|
// Failed DNS hosts cooldown cache
|
|
439
|
-
FullNodePeer.failedDNSCache = new
|
|
440
|
+
FullNodePeer.failedDNSCache = new utils_1.DigCache({ stdTTL: 86400 });
|
|
440
441
|
// Peer reliability weights
|
|
441
442
|
FullNodePeer.peerWeights = new Map();
|
|
442
443
|
// List of prioritized peers
|
|
@@ -444,9 +445,9 @@ FullNodePeer.prioritizedPeers = [];
|
|
|
444
445
|
// Map to store PeerInfo
|
|
445
446
|
FullNodePeer.peerInfos = new Map();
|
|
446
447
|
// Cache for fetched peer IPs
|
|
447
|
-
FullNodePeer.peerIPCache = new
|
|
448
|
+
FullNodePeer.peerIPCache = new utils_1.DigCache({ stdTTL: CACHE_DURATION / 1000 });
|
|
448
449
|
// Cache for DNS_HOST resolved IPs with a TTL of 3 days (259200 seconds)
|
|
449
|
-
FullNodePeer.dnsCache = new
|
|
450
|
+
FullNodePeer.dnsCache = new utils_1.DigCache({
|
|
450
451
|
stdTTL: 259200,
|
|
451
452
|
checkperiod: 3600,
|
|
452
453
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServerCoin.d.ts","sourceRoot":"","sources":["../../src/blockchain/ServerCoin.ts"],"names":[],"mappings":"AACA,OAAO,EAML,UAAU,IAAI,gBAAgB,EAE/B,MAAM,8BAA8B,CAAC;AAGtC,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"ServerCoin.d.ts","sourceRoot":"","sources":["../../src/blockchain/ServerCoin.ts"],"names":[],"mappings":"AACA,OAAO,EAML,UAAU,IAAI,gBAAgB,EAE/B,MAAM,8BAA8B,CAAC;AAGtC,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAUrD,qBAAa,UAAU;IACrB,OAAO,CAAC,OAAO,CAAS;IACxB,gBAAuB,iBAAiB,eAEtC;gBAEU,OAAO,EAAE,MAAM;IAKd,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IA4D3E,kBAAkB,CAC7B,UAAU,EAAE,gBAAgB,EAC5B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAsBH,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;YAoDjD,oBAAoB;IAerB,gBAAgB,CAC3B,KAAK,EAAE,MAAM,EACb,SAAS,GAAE,MAAM,EAAO,GACvB,OAAO,CAAC,MAAM,EAAE,CAAC;IAoDP,mBAAmB,CAC9B,SAAS,GAAE,MAAM,EAAO,GACvB,OAAO,CAAC,MAAM,EAAE,CAAC;IAMP,kBAAkB,CAC7B,UAAU,GAAE,MAAU,EACtB,SAAS,GAAE,MAAM,EAAO,GACvB,OAAO,CAAC,MAAM,EAAE,CAAC;IAMP,wBAAwB,CACnC,KAAK,EAAE,MAAM,EACb,UAAU,GAAE,MAAU,EACtB,SAAS,GAAE,MAAM,EAAO,GACvB,OAAO,CAAC,MAAM,EAAE,CAAC;WAeN,eAAe,IAAI;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE;IAKpD,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAiFrD,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WA+B1C,uBAAuB;YAkE7B,sBAAsB;IAWvB,uBAAuB,CAClC,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,OAAO,CAAC;WAuBL,sBAAsB,CAAC,mBAAmB,EAAE,IAAI,GAAG;QAC/D,KAAK,EAAE,MAAM,CAAC;QACd,KAAK,EAAE,MAAM,CAAC;KACf;CA2BF"}
|
|
@@ -10,12 +10,10 @@ const FullNodePeer_1 = require("./FullNodePeer");
|
|
|
10
10
|
const Wallet_1 = require("./Wallet");
|
|
11
11
|
const NconfManager_1 = require("../utils/NconfManager");
|
|
12
12
|
const DataStore_1 = require("./DataStore");
|
|
13
|
-
const
|
|
14
|
-
const network_1 = require("../utils/network");
|
|
15
|
-
const Environment_1 = require("../utils/Environment");
|
|
13
|
+
const utils_1 = require("../utils");
|
|
16
14
|
const serverCoinCollateral = 300000000;
|
|
17
15
|
// Initialize the cache with a TTL of 300 seconds (5 minutes)
|
|
18
|
-
const serverCoinPeersCache = new
|
|
16
|
+
const serverCoinPeersCache = new utils_1.DigCache({ stdTTL: 300 });
|
|
19
17
|
class ServerCoin {
|
|
20
18
|
constructor(storeId) {
|
|
21
19
|
this.storeId = storeId;
|
|
@@ -102,7 +100,7 @@ class ServerCoin {
|
|
|
102
100
|
async getAllEpochPeers(epoch, blacklist = []) {
|
|
103
101
|
const cacheKey = `serverCoinPeers-${this.storeId}-${epoch}`;
|
|
104
102
|
// Check if the result is already cached
|
|
105
|
-
const cachedPeers = serverCoinPeersCache.get(cacheKey);
|
|
103
|
+
const cachedPeers = await serverCoinPeersCache.get(cacheKey);
|
|
106
104
|
if (cachedPeers) {
|
|
107
105
|
return cachedPeers;
|
|
108
106
|
}
|
|
@@ -123,7 +121,7 @@ class ServerCoin {
|
|
|
123
121
|
serverCoinPeers.add(peerUrl);
|
|
124
122
|
}
|
|
125
123
|
}
|
|
126
|
-
if (
|
|
124
|
+
if (utils_1.Environment.DEBUG) {
|
|
127
125
|
console.log("Server Coin Peers: ", serverCoinPeers);
|
|
128
126
|
}
|
|
129
127
|
const peerList = Array.from(serverCoinPeers);
|
|
@@ -143,12 +141,12 @@ class ServerCoin {
|
|
|
143
141
|
// Sample server coins by epoch
|
|
144
142
|
async sampleServerCoinsByEpoch(epoch, sampleSize = 5, blacklist = []) {
|
|
145
143
|
// We dont want our own IP to be included
|
|
146
|
-
const host = await (0,
|
|
144
|
+
const host = await (0, utils_1.getPublicHost)();
|
|
147
145
|
if (host) {
|
|
148
146
|
blacklist.push(host);
|
|
149
147
|
}
|
|
150
148
|
const serverCoinPeers = await this.getAllEpochPeers(epoch, blacklist);
|
|
151
|
-
if (
|
|
149
|
+
if (utils_1.Environment.DEBUG) {
|
|
152
150
|
console.log("Server Coin Peers: ", serverCoinPeers);
|
|
153
151
|
}
|
|
154
152
|
return lodash_1.default.sampleSize(serverCoinPeers, sampleSize);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import NodeCache from "node-cache";
|
|
2
|
+
export interface ICache {
|
|
3
|
+
set<T>(key: string, value: T, ttl?: number): Promise<boolean>;
|
|
4
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
5
|
+
del(key: string): Promise<number>;
|
|
6
|
+
has(key: string): Promise<boolean>;
|
|
7
|
+
flushAll(): Promise<boolean>;
|
|
8
|
+
keys(pattern?: string): Promise<string[]>;
|
|
9
|
+
ttl(key: string, ttl?: number): Promise<boolean>;
|
|
10
|
+
}
|
|
11
|
+
declare class DigCache implements ICache {
|
|
12
|
+
private cache;
|
|
13
|
+
private useRedis;
|
|
14
|
+
/**
|
|
15
|
+
* Constructor with the same signature as NodeCache.
|
|
16
|
+
* @param options - NodeCache options
|
|
17
|
+
*/
|
|
18
|
+
constructor(options?: NodeCache.Options);
|
|
19
|
+
/**
|
|
20
|
+
* Initializes the Redis connection if Redis is being used.
|
|
21
|
+
* Call this method before performing any cache operations.
|
|
22
|
+
*/
|
|
23
|
+
initialize(): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Gracefully disconnects the Redis client if Redis is being used.
|
|
26
|
+
* Call this method when your application is shutting down.
|
|
27
|
+
*/
|
|
28
|
+
disconnect(): Promise<void>;
|
|
29
|
+
set<T>(key: string, value: T, ttl?: number): Promise<boolean>;
|
|
30
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
31
|
+
del(key: string): Promise<number>;
|
|
32
|
+
has(key: string): Promise<boolean>;
|
|
33
|
+
flushAll(): Promise<boolean>;
|
|
34
|
+
keys(pattern?: string): Promise<string[]>;
|
|
35
|
+
ttl(key: string, ttl?: number): Promise<boolean>;
|
|
36
|
+
}
|
|
37
|
+
export { DigCache };
|
|
38
|
+
//# sourceMappingURL=DigCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DigCache.d.ts","sourceRoot":"","sources":["../../src/utils/DigCache.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,YAAY,CAAC;AAGnC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EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAoBlC,QAAQ,IAAI,OAAO,CAAC,OAAO,CAAC;IAoB5B,IAAI,CAAC,OAAO,GAAE,MAAY,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAmB9C,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CA0BvD;AAED,OAAO,EAAE,QAAQ,EAAE,CAAC"}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DigCache = void 0;
|
|
7
|
+
// src/cache/DigCache.ts
|
|
8
|
+
const node_cache_1 = __importDefault(require("node-cache"));
|
|
9
|
+
const redis_1 = require("redis");
|
|
10
|
+
class DigCache {
|
|
11
|
+
/**
|
|
12
|
+
* Constructor with the same signature as NodeCache.
|
|
13
|
+
* @param options - NodeCache options
|
|
14
|
+
*/
|
|
15
|
+
constructor(options) {
|
|
16
|
+
// Determine whether to use Redis based on the environment variable
|
|
17
|
+
this.useRedis = process.env.USE_REDIS === 'true';
|
|
18
|
+
if (this.useRedis) {
|
|
19
|
+
// Ensure REDIS_URL is provided
|
|
20
|
+
const redisUrl = process.env.REDIS_URL;
|
|
21
|
+
if (!redisUrl) {
|
|
22
|
+
throw new Error("REDIS_URL environment variable is not set.");
|
|
23
|
+
}
|
|
24
|
+
const client = (0, redis_1.createClient)({ url: redisUrl });
|
|
25
|
+
client.on("error", (err) => console.error("Redis Client Error", err));
|
|
26
|
+
// Initialize connection in the initialize method
|
|
27
|
+
this.cache = client;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
// Initialize NodeCache with provided options
|
|
31
|
+
this.cache = new node_cache_1.default(options);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Initializes the Redis connection if Redis is being used.
|
|
36
|
+
* Call this method before performing any cache operations.
|
|
37
|
+
*/
|
|
38
|
+
async initialize() {
|
|
39
|
+
if (this.useRedis) {
|
|
40
|
+
try {
|
|
41
|
+
await this.cache.connect();
|
|
42
|
+
console.log("Connected to Redis successfully.");
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
console.error("Failed to connect to Redis:", error);
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Gracefully disconnects the Redis client if Redis is being used.
|
|
52
|
+
* Call this method when your application is shutting down.
|
|
53
|
+
*/
|
|
54
|
+
async disconnect() {
|
|
55
|
+
if (this.useRedis) {
|
|
56
|
+
try {
|
|
57
|
+
await this.cache.quit();
|
|
58
|
+
console.log("Disconnected from Redis successfully.");
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
console.error("Error disconnecting Redis:", error);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async set(key, value, ttl) {
|
|
66
|
+
if (this.useRedis) {
|
|
67
|
+
try {
|
|
68
|
+
const serializedValue = JSON.stringify(value);
|
|
69
|
+
if (ttl !== undefined) {
|
|
70
|
+
// Redis TTL is in seconds
|
|
71
|
+
const result = await this.cache.set(key, serializedValue, { EX: ttl });
|
|
72
|
+
return result === "OK";
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const result = await this.cache.set(key, serializedValue);
|
|
76
|
+
return result === "OK";
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
console.error(`Redis set error for key "${key}":`, error);
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
try {
|
|
86
|
+
const result = this.cache.set(key, value, ttl ?? 0);
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
console.error(`NodeCache set error for key "${key}":`, error);
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async get(key) {
|
|
96
|
+
if (this.useRedis) {
|
|
97
|
+
try {
|
|
98
|
+
const value = await this.cache.get(key);
|
|
99
|
+
if (value === null) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
return JSON.parse(value);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
console.error(`Redis get error for key "${key}":`, error);
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
try {
|
|
111
|
+
const value = this.cache.get(key);
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
console.error(`NodeCache get error for key "${key}":`, error);
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async del(key) {
|
|
121
|
+
if (this.useRedis) {
|
|
122
|
+
try {
|
|
123
|
+
const result = await this.cache.del(key);
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
console.error(`Redis del error for key "${key}":`, error);
|
|
128
|
+
return 0;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
try {
|
|
133
|
+
const result = this.cache.del(key);
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
console.error(`NodeCache del error for key "${key}":`, error);
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async has(key) {
|
|
143
|
+
if (this.useRedis) {
|
|
144
|
+
try {
|
|
145
|
+
const result = await this.cache.exists(key);
|
|
146
|
+
return result > 0;
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
console.error(`Redis exists error for key "${key}":`, error);
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
try {
|
|
155
|
+
const result = this.cache.has(key);
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
console.error(`NodeCache has error for key "${key}":`, error);
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
async flushAll() {
|
|
165
|
+
if (this.useRedis) {
|
|
166
|
+
try {
|
|
167
|
+
await this.cache.flushAll();
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
console.error("Redis flushAll error:", error);
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
try {
|
|
177
|
+
this.cache.flushAll();
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
console.error("NodeCache flushAll error:", error);
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async keys(pattern = "*") {
|
|
187
|
+
if (this.useRedis) {
|
|
188
|
+
try {
|
|
189
|
+
const result = await this.cache.keys(pattern);
|
|
190
|
+
return result;
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
console.error(`Redis keys error with pattern "${pattern}":`, error);
|
|
194
|
+
return [];
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
try {
|
|
199
|
+
const result = this.cache.keys();
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
return [];
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
async ttl(key, ttl) {
|
|
208
|
+
if (this.useRedis) {
|
|
209
|
+
try {
|
|
210
|
+
if (ttl === undefined || ttl === 0) {
|
|
211
|
+
return (await this.cache.del(key)) > 0;
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
return await this.cache.expire(key, ttl);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
console.error(`Redis ttl error for key "${key}":`, error);
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
try {
|
|
224
|
+
if (ttl === undefined || ttl === 0) {
|
|
225
|
+
return this.cache.del(key) > 0;
|
|
226
|
+
}
|
|
227
|
+
return this.cache.ttl(key, ttl);
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
console.error(`NodeCache ttl error for key "${key}":`, error);
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
exports.DigCache = DigCache;
|
package/dist/utils/PeerRanker.js
CHANGED
|
@@ -7,11 +7,11 @@ exports.PeerRanker = void 0;
|
|
|
7
7
|
const axios_1 = __importDefault(require("axios"));
|
|
8
8
|
const fs_1 = __importDefault(require("fs"));
|
|
9
9
|
const https_1 = __importDefault(require("https"));
|
|
10
|
-
const
|
|
10
|
+
const DigCache_1 = require("./DigCache");
|
|
11
11
|
const ssl_1 = require("./ssl");
|
|
12
12
|
const promiseUtils_1 = require("./promiseUtils");
|
|
13
13
|
// Cache with TTL of 1 day (86400 seconds)
|
|
14
|
-
const peerCache = new
|
|
14
|
+
const peerCache = new DigCache_1.DigCache({ stdTTL: 86400 });
|
|
15
15
|
/**
|
|
16
16
|
* Utility class to rank peers based on latency and upload bandwidth using HTTPS with mTLS.
|
|
17
17
|
*/
|
|
@@ -40,7 +40,7 @@ class PeerRanker {
|
|
|
40
40
|
* @returns Promise resolving to the latency in milliseconds or rejecting if the peer fails.
|
|
41
41
|
*/
|
|
42
42
|
async measureLatency(ip) {
|
|
43
|
-
const cachedMetrics = peerCache.get(ip);
|
|
43
|
+
const cachedMetrics = await peerCache.get(ip);
|
|
44
44
|
if (cachedMetrics && cachedMetrics.latency) {
|
|
45
45
|
return cachedMetrics.latency;
|
|
46
46
|
}
|
|
@@ -92,7 +92,7 @@ class PeerRanker {
|
|
|
92
92
|
* @returns Promise resolving to the upload bandwidth in bytes per second or rejecting if the peer fails.
|
|
93
93
|
*/
|
|
94
94
|
async measureBandwidth(ip) {
|
|
95
|
-
const cachedMetrics = peerCache.get(ip);
|
|
95
|
+
const cachedMetrics = await peerCache.get(ip);
|
|
96
96
|
if (cachedMetrics && cachedMetrics.bandwidth) {
|
|
97
97
|
return cachedMetrics.bandwidth;
|
|
98
98
|
}
|
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,gBAAgB,CAAC;AAC/B,cAAc,cAAc,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;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC"}
|
package/dist/utils/index.js
CHANGED
|
@@ -16,4 +16,12 @@ export declare function asyncPool<T, R>(limit: number, items: T[], iteratorFn: (
|
|
|
16
16
|
* @returns Promise that resolves before the timeout or rejects with an error.
|
|
17
17
|
*/
|
|
18
18
|
export declare const withTimeout: <T>(promise: Promise<T>, ms: number, timeoutMessage: string) => Promise<T>;
|
|
19
|
+
/**
|
|
20
|
+
* Wraps a promise to execute a callback every 30 seconds while the promise is pending.
|
|
21
|
+
*
|
|
22
|
+
* @param {Promise<T>} promise - The promise to wrap.
|
|
23
|
+
* @param {() => void} callback - The callback function to call every 30 seconds.
|
|
24
|
+
* @returns {Promise<T>} - A new promise that resolves or rejects with the original promise's result.
|
|
25
|
+
*/
|
|
26
|
+
export declare const withIntervalCallback: <T>(promise: Promise<T>, callback: () => void) => Promise<T>;
|
|
19
27
|
//# sourceMappingURL=promiseUtils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promiseUtils.d.ts","sourceRoot":"","sources":["../../src/utils/promiseUtils.ts"],"names":[],"mappings":"AACA;;;;;;;;GAQG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,CAAC,EAClC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,CAAC,EAAE,EACV,UAAU,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,EACnC,UAAU,GAAE,MAAY,GACvB,OAAO,CAAC,CAAC,EAAE,CAAC,CAkBd;AACC;;;;;;EAMC;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,WAClB,OAAO,CAAC,CAAC,CAAC,MACf,MAAM,kBACM,MAAM,KACrB,OAAO,CAAC,CAAC,CAOX,CAAC"}
|
|
1
|
+
{"version":3,"file":"promiseUtils.d.ts","sourceRoot":"","sources":["../../src/utils/promiseUtils.ts"],"names":[],"mappings":"AACA;;;;;;;;GAQG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,CAAC,EAClC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,CAAC,EAAE,EACV,UAAU,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,EACnC,UAAU,GAAE,MAAY,GACvB,OAAO,CAAC,CAAC,EAAE,CAAC,CAkBd;AACC;;;;;;EAMC;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,WAClB,OAAO,CAAC,CAAC,CAAC,MACf,MAAM,kBACM,MAAM,KACrB,OAAO,CAAC,CAAC,CAOX,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,GAAI,CAAC,WAC3B,OAAO,CAAC,CAAC,CAAC,YACT,MAAM,IAAI,KACnB,OAAO,CAAC,CAAC,CAoBX,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.withTimeout = void 0;
|
|
3
|
+
exports.withIntervalCallback = exports.withTimeout = void 0;
|
|
4
4
|
exports.asyncPool = asyncPool;
|
|
5
5
|
/**
|
|
6
6
|
* Processes items in sequential batches with a concurrency limit.
|
|
@@ -41,3 +41,29 @@ const withTimeout = (promise, ms, timeoutMessage) => {
|
|
|
41
41
|
]);
|
|
42
42
|
};
|
|
43
43
|
exports.withTimeout = withTimeout;
|
|
44
|
+
/**
|
|
45
|
+
* Wraps a promise to execute a callback every 30 seconds while the promise is pending.
|
|
46
|
+
*
|
|
47
|
+
* @param {Promise<T>} promise - The promise to wrap.
|
|
48
|
+
* @param {() => void} callback - The callback function to call every 30 seconds.
|
|
49
|
+
* @returns {Promise<T>} - A new promise that resolves or rejects with the original promise's result.
|
|
50
|
+
*/
|
|
51
|
+
const withIntervalCallback = (promise, callback) => {
|
|
52
|
+
const intervalTime = 30000; // 30 seconds in milliseconds
|
|
53
|
+
let intervalId;
|
|
54
|
+
// Start the interval that calls the callback every 30 seconds
|
|
55
|
+
intervalId = setInterval(() => {
|
|
56
|
+
callback();
|
|
57
|
+
}, intervalTime);
|
|
58
|
+
// Return a new promise that clears the interval when the original promise settles
|
|
59
|
+
return promise
|
|
60
|
+
.then((result) => {
|
|
61
|
+
clearInterval(intervalId);
|
|
62
|
+
return result;
|
|
63
|
+
})
|
|
64
|
+
.catch((error) => {
|
|
65
|
+
clearInterval(intervalId);
|
|
66
|
+
throw error;
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
exports.withIntervalCallback = withIntervalCallback;
|
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.172",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -50,6 +50,7 @@
|
|
|
50
50
|
"node-stun": "^0.1.2",
|
|
51
51
|
"progress-stream": "^2.0.0",
|
|
52
52
|
"proper-lockfile": "^4.1.2",
|
|
53
|
+
"redis": "^4.7.0",
|
|
53
54
|
"superagent": "^10.0.0",
|
|
54
55
|
"unzipper": "^0.12.3"
|
|
55
56
|
},
|