Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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.

### [0.0.1-alpha.148](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.147...v0.0.1-alpha.148) (2024-10-06)

### [0.0.1-alpha.147](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.146...v0.0.1-alpha.147) (2024-10-06)

### [0.0.1-alpha.146](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.145...v0.0.1-alpha.146) (2024-10-06)
Expand Down
19 changes: 0 additions & 19 deletions dig.crt

This file was deleted.

28 changes: 0 additions & 28 deletions dig.key

This file was deleted.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dignetwork/dig-sdk",
"version": "0.0.1-alpha.147",
"version": "0.0.1-alpha.148",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
65 changes: 62 additions & 3 deletions src/blockchain/FullNodePeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class FullNodePeer {
if (this.peer) return; // Already initialized

try {
const bestPeer = await FullNodePeer.getBestPeer();
const bestPeer = await this.getBestPeer();
this.peer = bestPeer;
FullNodePeer.instance = this; // Assign the initialized instance
} catch (error: any) {
Expand Down Expand Up @@ -335,7 +335,7 @@ export class FullNodePeer {
* Connects to the best available peer based on weighted selection and reliability.
* @returns {Promise<Peer>} The connected Peer instance.
*/
private static async getBestPeer(): Promise<Peer> {
private async getBestPeer(): Promise<Peer> {
const now = Date.now();

// Refresh cachedPeer if expired
Expand Down Expand Up @@ -405,13 +405,72 @@ export class FullNodePeer {

// Initialize rate limiter for this peer
FullNodePeer.peerLimiters.set(selectedPeerIP, limiter);
const proxiedPeer = this.createPeerProxy(peer, selectedPeerIP);

// Cache the peer
FullNodePeer.cachedPeer = { peer: peer, timestamp: now };

console.log(`Using Fullnode Peer: ${selectedPeerIP}`);

return peer;
return proxiedPeer;
}

private createPeerProxy(peer: Peer, peerIP: string): Peer {
return new Proxy(peer, {
get: (target, prop) => {
const originalMethod = (target as any)[prop];

if (typeof originalMethod === "function") {
return async (...args: any[]) => {
let timeoutId: NodeJS.Timeout | undefined;

// Start the timeout to forget the peer after 1 minute
const timeoutPromise = new Promise<null>((_, reject) => {
timeoutId = setTimeout(() => {
FullNodePeer.cachedPeer = null;
reject(
new Error("Operation timed out. Reconnecting to a new peer.")
);
}, 60000); // 1 minute
});

try {
// Run the original method and race it against the timeout
const result = await Promise.race([
originalMethod.apply(target, args),
timeoutPromise,
]);

// Clear the timeout if the operation succeeded
if (timeoutId) {
clearTimeout(timeoutId);
}

return result;
} catch (error: any) {
// If the error is WebSocket-related or timeout, reset the peer
if (
error.message.includes("WebSocket") ||
error.message.includes("Operation timed out")
) {
FullNodePeer.cachedPeer = null;
// @ts-ignore
FullNodePeer.memoizedFetchNewPeerIPs.cache.clear();

console.info(
`Fullnode Peer error, reconnecting to a new peer...`
);
this.handlePeerDisconnection(peerIP);
const newPeer = await this.getBestPeer();
return (newPeer as any)[prop](...args);
}
throw error;
}
};
}
return originalMethod;
},
});
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/utils/PeerRanker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import https from 'https';
import { getOrCreateSSLCerts } from './ssl';
import { asyncPool } from './promiseUtils';

/**
* Interface representing the metrics of a peer.
*/
export interface PeerMetrics {
ip: string;
latency: number; // in milliseconds
Expand Down
Loading