Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/v0.0.1 alpha.98 #88

Merged
merged 2 commits into from
Sep 26, 2024
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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.98](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.97...v0.0.1-alpha.98) (2024-09-26)


### Features

* allow override of the ssl location for fullnode peers ([a16f720](https://github.com/DIG-Network/dig-chia-sdk/commit/a16f720126a10f54dd55cc0454952701e0cae317))

### [0.0.1-alpha.97](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.96...v0.0.1-alpha.97) (2024-09-26)


Expand Down
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.97",
"version": "0.0.1-alpha.98",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
74 changes: 59 additions & 15 deletions src/blockchain/FullNodePeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,26 @@ export class FullNodePeer {
private static deprioritizedIps: Set<string> = new Set(); // New set for deprioritized IPs

static {
FullNodePeer.memoizedFetchNewPeerIPs = memoize(FullNodePeer.fetchNewPeerIPs);
FullNodePeer.memoizedFetchNewPeerIPs = memoize(
FullNodePeer.fetchNewPeerIPs
);
}

private constructor(peer: Peer) {
this.peer = peer;
}

public static async connect(): Promise<Peer> {
const peer = await FullNodePeer.getBestPeer();
/**
* Connect to the best peer, optionally using custom certificate and key file paths.
* @param {string} [certFilePath] - Optional custom certificate file path.
* @param {string} [keyFilePath] - Optional custom key file path.
* @returns {Promise<Peer>} The connected peer.
*/
public static async connect(
certFilePath?: string,
keyFilePath?: string
): Promise<Peer> {
const peer = await FullNodePeer.getBestPeer(certFilePath, keyFilePath);
return new FullNodePeer(peer).peer;
}

Expand Down Expand Up @@ -132,7 +143,9 @@ export class FullNodePeer {
}
}
} catch (error: any) {
console.error(`Failed to resolve IPs from ${DNS_HOST}: ${error.message}`);
console.error(
`Failed to resolve IPs from ${DNS_HOST}: ${error.message}`
);
}
}
throw new Error("No reachable IPs found in any DNS records.");
Expand Down Expand Up @@ -178,7 +191,9 @@ export class FullNodePeer {
const timeoutPromise = new Promise<null>((_, reject) => {
timeoutId = setTimeout(() => {
FullNodePeer.cachedPeer = null;
reject(new Error("Operation timed out. Reconnecting to a new peer."));
reject(
new Error("Operation timed out. Reconnecting to a new peer.")
);
}, 60000); // 1 minute
});

Expand All @@ -197,12 +212,17 @@ export class FullNodePeer {
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")) {
if (
error.message.includes("WebSocket") ||
error.message.includes("Operation timed out")
) {
FullNodePeer.cachedPeer = null;
// @ts-ignore
FullNodePeer.memoizedFetchNewPeerIPs.cache.clear();
FullNodePeer.deprioritizedIps.clear();
console.info(`Fullnode Peer error, reconnecting to a new peer...`);
console.info(
`Fullnode Peer error, reconnecting to a new peer...`
);
const newPeer = await FullNodePeer.getBestPeer();
return (newPeer as any)[prop](...args);
}
Expand All @@ -215,7 +235,10 @@ export class FullNodePeer {
});
}

private static async getBestPeer(): Promise<Peer> {
private static async getBestPeer(
certFilePath?: string,
keyFilePath?: string
): Promise<Peer> {
const now = Date.now();

if (
Expand All @@ -225,12 +248,29 @@ export class FullNodePeer {
return FullNodePeer.cachedPeer.peer;
}

const sslFolder = path.resolve(os.homedir(), ".dig", "ssl");
const certFile = path.join(sslFolder, "public_dig.crt");
const keyFile = path.join(sslFolder, "public_dig.key");
let certFile: string;
let keyFile: string;

// If certFilePath or keyFilePath is provided, ensure both are provided
if (certFilePath && keyFilePath) {
certFile = certFilePath;
keyFile = keyFilePath;

if (!fs.existsSync(sslFolder)) {
fs.mkdirSync(sslFolder, { recursive: true });
if (!fs.existsSync(certFile)) {
throw new Error(`Certificate file not found: ${certFile}`);
}
if (!fs.existsSync(keyFile)) {
throw new Error(`Key file not found: ${keyFile}`);
}
} else {
// Use default paths if no custom paths are provided
const sslFolder = path.resolve(os.homedir(), ".dig", "ssl");
certFile = path.join(sslFolder, "public_dig.crt");
keyFile = path.join(sslFolder, "public_dig.key");

if (!fs.existsSync(sslFolder)) {
throw new Error(`SSL folder not found: ${sslFolder}`);
}
}

new Tls(certFile, keyFile);
Expand Down Expand Up @@ -258,7 +298,9 @@ export class FullNodePeer {
);
return FullNodePeer.createPeerProxy(peer);
} catch (error: any) {
console.error(`Failed to create peer for IP ${ip}: ${error.message}`);
console.error(
`Failed to create peer for IP ${ip}: ${error.message}`
);
return null;
}
}
Expand Down Expand Up @@ -299,7 +341,9 @@ export class FullNodePeer {
(height, index) =>
height === highestPeak &&
!FullNodePeer.deprioritizedIps.has(peerIPs[index]) && // Exclude deprioritized IPs
(peerIPs[index] === LOCALHOST || peerIPs[index] === trustedNodeIp || peerIPs[index] === CHIA_NODES_HOST)
(peerIPs[index] === LOCALHOST ||
peerIPs[index] === trustedNodeIp ||
peerIPs[index] === CHIA_NODES_HOST)
);

// If LOCALHOST, TRUSTED_NODE_IP, or CHIA_NODES_HOST don't have the highest peak, select any peer with the highest peak
Expand Down
Loading