@dignetwork/dig-sdk 0.0.1-alpha.138 → 0.0.1-alpha.139
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.
|
@@ -87,7 +87,7 @@ export declare class FullNodePeer {
|
|
|
87
87
|
*/
|
|
88
88
|
private static getBestPeer;
|
|
89
89
|
/**
|
|
90
|
-
* Creates a proxy for the peer to handle errors, implement retries, and enforce
|
|
90
|
+
* Creates a proxy for the peer to handle errors, implement retries, and enforce round-robin selection.
|
|
91
91
|
* @param {Peer} peer - The Peer instance.
|
|
92
92
|
* @param {string} peerIP - The IP address of the peer.
|
|
93
93
|
* @param {number} [retryCount=0] - The current retry attempt.
|
|
@@ -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;
|
|
1
|
+
{"version":3,"file":"FullNodePeer.d.ts","sourceRoot":"","sources":["../../src/blockchain/FullNodePeer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,IAAI,EAAO,MAAM,8BAA8B,CAAC;AAkCzD;;GAEG;AACH,qBAAa,YAAY;IA6BH,OAAO,CAAC,IAAI;IA3BhC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAA6B;IAGpD,OAAO,CAAC,MAAM,CAAC,UAAU,CAAkD;IAG3E,OAAO,CAAC,MAAM,CAAC,aAAa,CAAuD;IAGnF,OAAO,CAAC,MAAM,CAAC,WAAW,CAAkC;IAG5D,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAgB;IAG/C,OAAO,CAAC,MAAM,CAAC,SAAS,CAAoC;IAG5D,OAAO,CAAC,MAAM,CAAC,WAAW,CAAoD;IAG9E,OAAO,CAAC,MAAM,CAAC,cAAc,CAAgB;IAG7C,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAa;IAG5C,OAAO;IAEP;;;OAGG;WACW,WAAW,IAAI,YAAY;IAOzC;;OAEG;IACU,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAaxC;;;;OAIG;WACiB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAM5C;;;;;;OAMG;IACH,OAAO,CAAC,MAAM,CAAC,eAAe;IAuB9B;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAM/B;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,kBAAkB;IASjC;;;;OAIG;mBACkB,eAAe;IAyFpC;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,YAAY;IAS3B;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,qBAAqB;IAgBpC;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,aAAa;IAS5B;;;OAGG;mBACkB,UAAU;IAK/B;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,UAAU;IAIzB;;;OAGG;mBACkB,WAAW;IA0HhC;;;;;;OAMG;IACH,OAAO,CAAC,MAAM,CAAC,eAAe;IAiF9B;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,uBAAuB;IAwCtC;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,aAAa;IAS5B;;;;OAIG;WACiB,mBAAmB,CACrC,cAAc,EAAE,MAAM,GACrB,OAAO,CAAC,OAAO,CAAC;IAkCnB;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,KAAK;CAGrB"}
|
|
@@ -14,7 +14,6 @@ const nanospinner_1 = require("nanospinner");
|
|
|
14
14
|
const config_1 = require("../utils/config");
|
|
15
15
|
const Environment_1 = require("../utils/Environment");
|
|
16
16
|
const node_cache_1 = __importDefault(require("node-cache"));
|
|
17
|
-
const bottleneck_1 = __importDefault(require("bottleneck"));
|
|
18
17
|
// Constants
|
|
19
18
|
const FULLNODE_PORT = 8444;
|
|
20
19
|
const LOCALHOST = "127.0.0.1";
|
|
@@ -30,7 +29,6 @@ const CACHE_DURATION = 30000; // in milliseconds
|
|
|
30
29
|
const COOLDOWN_DURATION = 300000; // 5 minutes in milliseconds
|
|
31
30
|
const MAX_PEERS_TO_FETCH = 5; // Maximum number of peers to fetch from DNS
|
|
32
31
|
const MAX_RETRIES = 3; // Maximum number of retry attempts
|
|
33
|
-
const MAX_REQUESTS_PER_MINUTE = 100; // Per-peer rate limit
|
|
34
32
|
/**
|
|
35
33
|
* FullNodePeer manages connections to full nodes, prioritizing certain peers and handling reliability.
|
|
36
34
|
*/
|
|
@@ -130,7 +128,7 @@ class FullNodePeer {
|
|
|
130
128
|
const priorityIps = [];
|
|
131
129
|
// Define prioritized peers
|
|
132
130
|
FullNodePeer.prioritizedPeers = [
|
|
133
|
-
CHIA_NODES_HOST
|
|
131
|
+
...DNS_HOSTS, // Assuming CHIA_NODES_HOST is included in DNS_HOSTS
|
|
134
132
|
LOCALHOST,
|
|
135
133
|
];
|
|
136
134
|
// Add trustedNodeIp if available
|
|
@@ -271,11 +269,6 @@ class FullNodePeer {
|
|
|
271
269
|
// Initialize or update peerInfos and availablePeers
|
|
272
270
|
for (const ip of peerIPs) {
|
|
273
271
|
if (!FullNodePeer.peerInfos.has(ip)) {
|
|
274
|
-
// Create a new Bottleneck limiter for the peer
|
|
275
|
-
const limiter = new bottleneck_1.default({
|
|
276
|
-
maxConcurrent: 1, // One request at a time per peer
|
|
277
|
-
minTime: 60000 / MAX_REQUESTS_PER_MINUTE, // 600 ms between requests for 100 requests/min
|
|
278
|
-
});
|
|
279
272
|
// Attempt to create a peer connection
|
|
280
273
|
const sslFolder = path_1.default.resolve(os_1.default.homedir(), ".dig", "ssl");
|
|
281
274
|
const certFile = path_1.default.join(sslFolder, "public_dig.crt");
|
|
@@ -310,7 +303,6 @@ class FullNodePeer {
|
|
|
310
303
|
weight: FullNodePeer.peerWeights.get(ip) || 1,
|
|
311
304
|
address: ip,
|
|
312
305
|
isConnected: true, // Mark as connected
|
|
313
|
-
limiter, // Assign the limiter
|
|
314
306
|
});
|
|
315
307
|
// Add to availablePeers
|
|
316
308
|
FullNodePeer.availablePeers.push(ip);
|
|
@@ -366,7 +358,7 @@ class FullNodePeer {
|
|
|
366
358
|
return selectedPeerInfo.peer;
|
|
367
359
|
}
|
|
368
360
|
/**
|
|
369
|
-
* Creates a proxy for the peer to handle errors, implement retries, and enforce
|
|
361
|
+
* Creates a proxy for the peer to handle errors, implement retries, and enforce round-robin selection.
|
|
370
362
|
* @param {Peer} peer - The Peer instance.
|
|
371
363
|
* @param {string} peerIP - The IP address of the peer.
|
|
372
364
|
* @param {number} [retryCount=0] - The current retry attempt.
|
|
@@ -400,58 +392,42 @@ class FullNodePeer {
|
|
|
400
392
|
return Promise.reject(error);
|
|
401
393
|
}
|
|
402
394
|
const selectedPeerInfo = FullNodePeer.peerInfos.get(selectedPeerIP);
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
395
|
+
if (!selectedPeerInfo || !selectedPeerInfo.isConnected) {
|
|
396
|
+
return Promise.reject(new Error(`Peer ${selectedPeerIP} is disconnected.`));
|
|
397
|
+
}
|
|
398
|
+
try {
|
|
399
|
+
// Bind the original method to the peer instance to preserve 'this'
|
|
400
|
+
const boundMethod = originalMethod.bind(selectedPeerInfo.peer);
|
|
401
|
+
const result = boundMethod(...args);
|
|
402
|
+
return result;
|
|
403
|
+
}
|
|
404
|
+
catch (error) {
|
|
405
|
+
console.error(`Peer ${selectedPeerIP} encountered an error: ${error.message}`);
|
|
406
|
+
// Handle disconnection and retries
|
|
407
|
+
FullNodePeer.handlePeerDisconnection(selectedPeerIP);
|
|
408
|
+
// If maximum retries reached, throw the error
|
|
409
|
+
if (retryCount >= MAX_RETRIES) {
|
|
410
|
+
console.error(`Max retries reached for method ${String(prop)} on peer ${selectedPeerIP}.`);
|
|
411
|
+
return Promise.reject(error);
|
|
417
412
|
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
// If maximum retries reached, throw the error
|
|
426
|
-
if (retryCount >= MAX_RETRIES) {
|
|
427
|
-
console.error(`Max retries reached for method ${String(prop)} on peer ${selectedPeerIP}.`);
|
|
428
|
-
throw error;
|
|
429
|
-
}
|
|
430
|
-
// Attempt to select a new peer and retry the method
|
|
431
|
-
try {
|
|
432
|
-
console.info(`Selecting a new peer to retry method ${String(prop)}...`);
|
|
433
|
-
const newPeer = await FullNodePeer.getBestPeer();
|
|
434
|
-
// Extract new peer's IP address
|
|
435
|
-
const newPeerIP = FullNodePeer.extractPeerIP(newPeer);
|
|
436
|
-
if (!newPeerIP) {
|
|
437
|
-
throw new Error("Unable to extract IP from the new peer.");
|
|
438
|
-
}
|
|
439
|
-
// Wrap the new peer with a proxy, incrementing the retry count
|
|
440
|
-
const proxiedNewPeer = FullNodePeer.createPeerProxy(newPeer, newPeerIP, retryCount + 1);
|
|
441
|
-
// Retry the method on the new peer
|
|
442
|
-
return await proxiedNewPeer[prop](...args);
|
|
443
|
-
}
|
|
444
|
-
catch (retryError) {
|
|
445
|
-
console.error(`Retry failed on a new peer: ${retryError.message}`);
|
|
446
|
-
throw retryError;
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
else {
|
|
450
|
-
// For other errors, handle normally
|
|
451
|
-
throw error;
|
|
413
|
+
// Attempt to select a new peer and retry the method
|
|
414
|
+
return FullNodePeer.getBestPeer()
|
|
415
|
+
.then((newPeer) => {
|
|
416
|
+
// Extract new peer's IP address
|
|
417
|
+
const newPeerIP = FullNodePeer.extractPeerIP(newPeer);
|
|
418
|
+
if (!newPeerIP) {
|
|
419
|
+
throw new Error("Unable to extract IP from the new peer.");
|
|
452
420
|
}
|
|
453
|
-
|
|
454
|
-
|
|
421
|
+
// Wrap the new peer with a proxy, incrementing the retry count
|
|
422
|
+
const proxiedNewPeer = FullNodePeer.createPeerProxy(newPeer, newPeerIP, retryCount + 1);
|
|
423
|
+
// Retry the method on the new peer
|
|
424
|
+
return proxiedNewPeer[prop](...args);
|
|
425
|
+
})
|
|
426
|
+
.catch((retryError) => {
|
|
427
|
+
console.error(`Retry failed on a new peer: ${retryError.message}`);
|
|
428
|
+
return Promise.reject(retryError);
|
|
429
|
+
});
|
|
430
|
+
}
|
|
455
431
|
};
|
|
456
432
|
}
|
|
457
433
|
return originalMethod;
|