Skip to content

Commit 8c60329

Browse files
Merge pull request #126 from DIG-Network/release/v0.0.1-alpha.139
Release/v0.0.1 alpha.139
2 parents bfae7fb + 8cc2015 commit 8c60329

File tree

4 files changed

+44
-69
lines changed

4 files changed

+44
-69
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
### [0.0.1-alpha.139](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.138...v0.0.1-alpha.139) (2024-10-05)
6+
57
### [0.0.1-alpha.138](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.137...v0.0.1-alpha.138) (2024-10-05)
68

79
### [0.0.1-alpha.137](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.136...v0.0.1-alpha.137) (2024-10-05)

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dignetwork/dig-sdk",
3-
"version": "0.0.1-alpha.138",
3+
"version": "0.0.1-alpha.139",
44
"description": "",
55
"type": "commonjs",
66
"main": "./dist/index.js",

src/blockchain/FullNodePeer.ts

Lines changed: 39 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { createSpinner } from "nanospinner";
88
import { MIN_HEIGHT, MIN_HEIGHT_HEADER_HASH } from "../utils/config";
99
import { Environment } from "../utils/Environment";
1010
import NodeCache from "node-cache";
11-
import Bottleneck from "bottleneck";
1211

1312
// Constants
1413
const FULLNODE_PORT = 8444;
@@ -25,17 +24,15 @@ const CACHE_DURATION = 30000; // in milliseconds
2524
const COOLDOWN_DURATION = 300000; // 5 minutes in milliseconds
2625
const MAX_PEERS_TO_FETCH = 5; // Maximum number of peers to fetch from DNS
2726
const MAX_RETRIES = 3; // Maximum number of retry attempts
28-
const MAX_REQUESTS_PER_MINUTE = 100; // Per-peer rate limit
2927

3028
/**
31-
* Represents a peer with its reliability weight, address, and rate limiter.
29+
* Represents a peer with its reliability weight and address.
3230
*/
3331
interface PeerInfo {
3432
peer: Peer;
3533
weight: number;
3634
address: string;
3735
isConnected: boolean; // Indicates if the peer is currently connected
38-
limiter: Bottleneck; // Rate limiter for the peer
3936
}
4037

4138
/**
@@ -175,7 +172,7 @@ export class FullNodePeer {
175172

176173
// Define prioritized peers
177174
FullNodePeer.prioritizedPeers = [
178-
CHIA_NODES_HOST,
175+
...DNS_HOSTS, // Assuming CHIA_NODES_HOST is included in DNS_HOSTS
179176
LOCALHOST,
180177
];
181178

@@ -348,12 +345,6 @@ export class FullNodePeer {
348345
// Initialize or update peerInfos and availablePeers
349346
for (const ip of peerIPs) {
350347
if (!FullNodePeer.peerInfos.has(ip)) {
351-
// Create a new Bottleneck limiter for the peer
352-
const limiter = new Bottleneck({
353-
maxConcurrent: 1, // One request at a time per peer
354-
minTime: 60000 / MAX_REQUESTS_PER_MINUTE, // 600 ms between requests for 100 requests/min
355-
});
356-
357348
// Attempt to create a peer connection
358349
const sslFolder = path.resolve(os.homedir(), ".dig", "ssl");
359350
const certFile = path.join(sslFolder, "public_dig.crt");
@@ -391,7 +382,6 @@ export class FullNodePeer {
391382
weight: FullNodePeer.peerWeights.get(ip) || 1,
392383
address: ip,
393384
isConnected: true, // Mark as connected
394-
limiter, // Assign the limiter
395385
});
396386

397387
// Add to availablePeers
@@ -457,7 +447,7 @@ export class FullNodePeer {
457447
}
458448

459449
/**
460-
* Creates a proxy for the peer to handle errors, implement retries, and enforce per-peer throttling.
450+
* Creates a proxy for the peer to handle errors, implement retries, and enforce round-robin selection.
461451
* @param {Peer} peer - The Peer instance.
462452
* @param {string} peerIP - The IP address of the peer.
463453
* @param {number} [retryCount=0] - The current retry attempt.
@@ -495,65 +485,48 @@ export class FullNodePeer {
495485

496486
const selectedPeerInfo = FullNodePeer.peerInfos.get(selectedPeerIP)!;
497487

498-
// Schedule the method call via the selected peer's limiter
499-
return selectedPeerInfo.limiter.schedule(async () => {
500-
const peerInfo = FullNodePeer.peerInfos.get(selectedPeerIP);
501-
if (!peerInfo || !peerInfo.isConnected) {
502-
throw new Error(`Cannot perform operation: Peer ${selectedPeerIP} is disconnected.`);
503-
}
504-
505-
try {
506-
// Bind the original method to the peer instance to preserve 'this'
507-
const boundMethod = originalMethod.bind(peerInfo.peer);
508-
const result = await boundMethod(...args);
509-
// On successful operation, increase the weight slightly
510-
const currentWeight = FullNodePeer.peerWeights.get(selectedPeerIP) || 1;
511-
FullNodePeer.peerWeights.set(selectedPeerIP, currentWeight + 0.1); // Increment weight
512-
return result;
513-
} catch (error: any) {
514-
console.error(`Peer ${selectedPeerIP} encountered an error: ${error.message}`);
515-
516-
// Check if the error is related to WebSocket or Operation timed out
517-
if (
518-
error.message.includes("WebSocket") ||
519-
error.message.includes("Operation timed out")
520-
) {
521-
// Handle the disconnection and mark the peer accordingly
522-
FullNodePeer.handlePeerDisconnection(selectedPeerIP);
523-
524-
// If maximum retries reached, throw the error
525-
if (retryCount >= MAX_RETRIES) {
526-
console.error(`Max retries reached for method ${String(prop)} on peer ${selectedPeerIP}.`);
527-
throw error;
528-
}
488+
if (!selectedPeerInfo || !selectedPeerInfo.isConnected) {
489+
return Promise.reject(new Error(`Peer ${selectedPeerIP} is disconnected.`));
490+
}
529491

530-
// Attempt to select a new peer and retry the method
531-
try {
532-
console.info(`Selecting a new peer to retry method ${String(prop)}...`);
533-
const newPeer = await FullNodePeer.getBestPeer();
492+
try {
493+
// Bind the original method to the peer instance to preserve 'this'
494+
const boundMethod = originalMethod.bind(selectedPeerInfo.peer);
495+
const result = boundMethod(...args);
496+
return result;
497+
} catch (error: any) {
498+
console.error(`Peer ${selectedPeerIP} encountered an error: ${error.message}`);
534499

535-
// Extract new peer's IP address
536-
const newPeerIP = FullNodePeer.extractPeerIP(newPeer);
500+
// Handle disconnection and retries
501+
FullNodePeer.handlePeerDisconnection(selectedPeerIP);
537502

538-
if (!newPeerIP) {
539-
throw new Error("Unable to extract IP from the new peer.");
540-
}
503+
// If maximum retries reached, throw the error
504+
if (retryCount >= MAX_RETRIES) {
505+
console.error(`Max retries reached for method ${String(prop)} on peer ${selectedPeerIP}.`);
506+
return Promise.reject(error);
507+
}
541508

542-
// Wrap the new peer with a proxy, incrementing the retry count
543-
const proxiedNewPeer = FullNodePeer.createPeerProxy(newPeer, newPeerIP, retryCount + 1);
509+
// Attempt to select a new peer and retry the method
510+
return FullNodePeer.getBestPeer()
511+
.then((newPeer) => {
512+
// Extract new peer's IP address
513+
const newPeerIP = FullNodePeer.extractPeerIP(newPeer);
544514

545-
// Retry the method on the new peer
546-
return await (proxiedNewPeer as any)[prop](...args);
547-
} catch (retryError: any) {
548-
console.error(`Retry failed on a new peer: ${retryError.message}`);
549-
throw retryError;
515+
if (!newPeerIP) {
516+
throw new Error("Unable to extract IP from the new peer.");
550517
}
551-
} else {
552-
// For other errors, handle normally
553-
throw error;
554-
}
555-
}
556-
});
518+
519+
// Wrap the new peer with a proxy, incrementing the retry count
520+
const proxiedNewPeer = FullNodePeer.createPeerProxy(newPeer, newPeerIP, retryCount + 1);
521+
522+
// Retry the method on the new peer
523+
return (proxiedNewPeer as any)[prop](...args);
524+
})
525+
.catch((retryError: any) => {
526+
console.error(`Retry failed on a new peer: ${retryError.message}`);
527+
return Promise.reject(retryError);
528+
});
529+
}
557530
};
558531
}
559532
return originalMethod;

0 commit comments

Comments
 (0)