Skip to content

Commit e5f75d8

Browse files
Merge pull request #149 from DIG-Network/release/v0.0.1-alpha.165
Release/v0.0.1 alpha.165
2 parents c64ca87 + 24b3c1f commit e5f75d8

File tree

4 files changed

+50
-34
lines changed

4 files changed

+50
-34
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
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.165](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.164...v0.0.1-alpha.165) (2024-10-08)
6+
7+
8+
### Features
9+
10+
* cache DNS hosts with fullnode peers ([2b26c03](https://github.com/DIG-Network/dig-chia-sdk/commit/2b26c030d35e7239236f4d9f36307714788302aa))
11+
512
### [0.0.1-alpha.164](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.163...v0.0.1-alpha.164) (2024-10-07)
613

714
### [0.0.1-alpha.163](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.162...v0.0.1-alpha.163) (2024-10-07)

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.164",
3+
"version": "0.0.1-alpha.165",
44
"description": "",
55
"type": "commonjs",
66
"main": "./dist/index.js",

src/blockchain/FullNodePeer.ts

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import Bottleneck from "bottleneck";
1212

1313
const FULLNODE_PORT = 8444;
1414
const LOCALHOST = "127.0.0.1";
15-
const CHIA_NODES_HOST = "chia-nodes";
1615
const DNS_HOSTS = [
1716
"dns-introducer.chia.net",
1817
"chia.ctrlaltdel.ch",
@@ -42,7 +41,9 @@ export class FullNodePeer {
4241
private static instance: FullNodePeer | null = null;
4342

4443
// Cooldown cache to exclude faulty peers temporarily
45-
private static cooldownCache = new NodeCache({ stdTTL: COOLDOWN_DURATION / 1000 });
44+
private static cooldownCache = new NodeCache({
45+
stdTTL: COOLDOWN_DURATION / 1000,
46+
});
4647

4748
// Failed DNS hosts cooldown cache
4849
private static failedDNSCache = new NodeCache({ stdTTL: 86400 });
@@ -59,6 +60,12 @@ export class FullNodePeer {
5960
// Cache for fetched peer IPs
6061
private static peerIPCache = new NodeCache({ stdTTL: CACHE_DURATION / 1000 });
6162

63+
// Cache for DNS_HOST resolved IPs with a TTL of 3 days (259200 seconds)
64+
private static dnsCache = new NodeCache({
65+
stdTTL: 259200,
66+
checkperiod: 3600,
67+
});
68+
6269
// Map to store rate limiters per peer IP
6370
private static peerLimiters: Map<string, Bottleneck> = new Map();
6471

@@ -171,7 +178,7 @@ export class FullNodePeer {
171178
const priorityIps: string[] = [];
172179

173180
// Define prioritized peers
174-
FullNodePeer.prioritizedPeers = [CHIA_NODES_HOST, LOCALHOST];
181+
FullNodePeer.prioritizedPeers = [LOCALHOST];
175182

176183
// Add trustedNodeIp if available
177184
if (trustedNodeIp) {
@@ -195,14 +202,6 @@ export class FullNodePeer {
195202
priorityIps.push(LOCALHOST);
196203
}
197204

198-
// Prioritize CHIA_NODES_HOST
199-
if (
200-
!FullNodePeer.cooldownCache.has(CHIA_NODES_HOST) &&
201-
(await FullNodePeer.isPortReachable(CHIA_NODES_HOST, FULLNODE_PORT))
202-
) {
203-
priorityIps.push(CHIA_NODES_HOST);
204-
}
205-
206205
if (priorityIps.length > 0) {
207206
return priorityIps;
208207
}
@@ -218,19 +217,31 @@ export class FullNodePeer {
218217
for (const DNS_HOST of DNS_HOSTS) {
219218
// Check if DNS_HOST is in failedDNSCache
220219
if (FullNodePeer.failedDNSCache.has(DNS_HOST)) {
221-
console.warn(`Skipping DNS host ${DNS_HOST} due to recent failure.`);
222220
continue;
223221
}
224222

225223
try {
226-
const ips = await resolve4(DNS_HOST);
227-
if (ips && ips.length > 0) {
224+
let ips: string[] = [];
225+
226+
// Check if DNS_HOST's IPs are cached
227+
if (FullNodePeer.dnsCache.has(DNS_HOST)) {
228+
ips = FullNodePeer.dnsCache.get<string[]>(DNS_HOST) || [];
229+
} else {
230+
// Resolve DNS_HOST and cache the results
231+
ips = await resolve4(DNS_HOST);
232+
if (ips && ips.length > 0) {
233+
FullNodePeer.dnsCache.set(DNS_HOST, ips);
234+
}
235+
}
236+
237+
if (ips.length > 0) {
228238
const shuffledIps = FullNodePeer.shuffleArray(ips);
229239
const reachableIps: string[] = [];
230240

231241
for (const ip of shuffledIps) {
232242
if (
233243
!FullNodePeer.cooldownCache.has(ip) &&
244+
FullNodePeer.isValidIpAddress(ip) &&
234245
(await FullNodePeer.isPortReachable(ip, FULLNODE_PORT))
235246
) {
236247
reachableIps.push(ip);
@@ -243,13 +254,15 @@ export class FullNodePeer {
243254
}
244255
}
245256
} catch (error: any) {
246-
console.error(`Failed to resolve IPs from ${DNS_HOST}: ${error.message}`);
257+
console.error(
258+
`Failed to resolve IPs from ${DNS_HOST}: ${error.message}`
259+
);
247260
// Add DNS_HOST to failedDNSCache for cooldown
248261
FullNodePeer.failedDNSCache.set(DNS_HOST, true);
249262
}
250263
}
251264

252-
// Cache the fetched peer IPs
265+
// Cache the fetched peer IPs if any
253266
if (fetchedPeers.length > 0) {
254267
FullNodePeer.peerIPCache.set("peerIPs", fetchedPeers);
255268
return fetchedPeers;
@@ -281,7 +294,6 @@ export class FullNodePeer {
281294
for (const ip of peerIPs) {
282295
if (!FullNodePeer.peerWeights.has(ip)) {
283296
if (
284-
ip === CHIA_NODES_HOST ||
285297
ip === LOCALHOST ||
286298
ip === FullNodePeer.getTrustedFullNode()
287299
) {
@@ -417,7 +429,9 @@ export class FullNodePeer {
417429
const timeoutPromise = new Promise<null>((_, reject) => {
418430
timeoutId = setTimeout(() => {
419431
reject(
420-
new Error("Operation timed out. Reconnecting to a new fullnode peer.")
432+
new Error(
433+
"Operation timed out. Reconnecting to a new fullnode peer."
434+
)
421435
);
422436
}, 60000); // 1 minute
423437
});
@@ -476,7 +490,9 @@ export class FullNodePeer {
476490
// Remove the limiter
477491
FullNodePeer.peerLimiters.delete(peerIP);
478492

479-
console.warn(`Fullnode Peer ${peerIP} has been marked as disconnected and is in cooldown.`);
493+
console.warn(
494+
`Fullnode Peer ${peerIP} has been marked as disconnected and is in cooldown.`
495+
);
480496
}
481497

482498
/**
@@ -519,21 +535,14 @@ export class FullNodePeer {
519535
throw new Error("Failed to extract peer IP.");
520536
}
521537

522-
const limiter = FullNodePeer.peerLimiters.get(peerIP);
523-
if (!limiter) {
524-
spinner.error({ text: "No rate limiter found for the fullnode peer." });
525-
throw new Error("No rate limiter found for the peer.");
526-
}
527-
528538
try {
529539
while (true) {
530540
// Schedule the isCoinSpent method call through the limiter
531-
const confirmed = await limiter.schedule(() =>
532-
peer.isCoinSpent(
533-
parentCoinInfo,
534-
MIN_HEIGHT,
535-
Buffer.from(MIN_HEIGHT_HEADER_HASH, "hex")
536-
)
541+
542+
const confirmed = await peer.isCoinSpent(
543+
parentCoinInfo,
544+
MIN_HEIGHT,
545+
Buffer.from(MIN_HEIGHT_HEADER_HASH, "hex")
537546
);
538547

539548
if (confirmed) {

0 commit comments

Comments
 (0)