@dignetwork/dig-sdk 0.0.1-alpha.136 → 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 per-peer throttling.
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;AAqCzD;;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;IAuB9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O,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
  */
@@ -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 per-peer throttling.
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,56 +392,42 @@ class FullNodePeer {
400
392
  return Promise.reject(error);
401
393
  }
402
394
  const selectedPeerInfo = FullNodePeer.peerInfos.get(selectedPeerIP);
403
- // Schedule the method call via the selected peer's limiter
404
- return selectedPeerInfo.limiter.schedule(async () => {
405
- const peerInfo = FullNodePeer.peerInfos.get(selectedPeerIP);
406
- if (!peerInfo || !peerInfo.isConnected) {
407
- throw new Error(`Cannot perform operation: Peer ${selectedPeerIP} is disconnected.`);
408
- }
409
- try {
410
- const result = await originalMethod.apply(peerInfo.peer, args);
411
- // On successful operation, increase the weight slightly
412
- const currentWeight = FullNodePeer.peerWeights.get(selectedPeerIP) || 1;
413
- FullNodePeer.peerWeights.set(selectedPeerIP, currentWeight + 0.1); // Increment weight
414
- return result;
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);
415
412
  }
416
- catch (error) {
417
- console.error(`Peer ${selectedPeerIP} encountered an error: ${error.message}`);
418
- // Check if the error is related to WebSocket or Operation timed out
419
- if (error.message.includes("WebSocket") ||
420
- error.message.includes("Operation timed out")) {
421
- // Handle the disconnection and mark the peer accordingly
422
- FullNodePeer.handlePeerDisconnection(selectedPeerIP);
423
- // If maximum retries reached, throw the error
424
- if (retryCount >= MAX_RETRIES) {
425
- console.error(`Max retries reached for method ${String(prop)} on peer ${selectedPeerIP}.`);
426
- throw error;
427
- }
428
- // Attempt to select a new peer and retry the method
429
- try {
430
- console.info(`Selecting a new peer to retry method ${String(prop)}...`);
431
- const newPeer = await FullNodePeer.getBestPeer();
432
- // Extract new peer's IP address
433
- const newPeerIP = FullNodePeer.extractPeerIP(newPeer);
434
- if (!newPeerIP) {
435
- throw new Error("Unable to extract IP from the new peer.");
436
- }
437
- // Wrap the new peer with a proxy, incrementing the retry count
438
- const proxiedNewPeer = FullNodePeer.createPeerProxy(newPeer, newPeerIP, retryCount + 1);
439
- // Retry the method on the new peer
440
- return await proxiedNewPeer[prop](...args);
441
- }
442
- catch (retryError) {
443
- console.error(`Retry failed on a new peer: ${retryError.message}`);
444
- throw retryError;
445
- }
446
- }
447
- else {
448
- // For other errors, handle normally
449
- 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.");
450
420
  }
451
- }
452
- });
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
+ }
453
431
  };
454
432
  }
455
433
  return originalMethod;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.136",
3
+ "version": "0.0.1-alpha.139",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",