@dignetwork/dig-sdk 0.0.1-alpha.13 → 0.0.1-alpha.130

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.
Files changed (81) hide show
  1. package/dist/DataIntegrityTree/DataIntegrityTree.d.ts +11 -2
  2. package/dist/DataIntegrityTree/DataIntegrityTree.d.ts.map +1 -1
  3. package/dist/DataIntegrityTree/DataIntegrityTree.js +106 -15
  4. package/dist/DigNetwork/ContentServer.d.ts +12 -5
  5. package/dist/DigNetwork/ContentServer.d.ts.map +1 -1
  6. package/dist/DigNetwork/ContentServer.js +172 -24
  7. package/dist/DigNetwork/DigNetwork.d.ts +5 -9
  8. package/dist/DigNetwork/DigNetwork.d.ts.map +1 -1
  9. package/dist/DigNetwork/DigNetwork.js +117 -189
  10. package/dist/DigNetwork/DigPeer.d.ts +8 -3
  11. package/dist/DigNetwork/DigPeer.d.ts.map +1 -1
  12. package/dist/DigNetwork/DigPeer.js +68 -115
  13. package/dist/DigNetwork/IncentiveServer.d.ts.map +1 -1
  14. package/dist/DigNetwork/IncentiveServer.js +6 -5
  15. package/dist/DigNetwork/PropagationServer.d.ts +83 -33
  16. package/dist/DigNetwork/PropagationServer.d.ts.map +1 -1
  17. package/dist/DigNetwork/PropagationServer.js +610 -356
  18. package/dist/blockchain/DataStore.d.ts +25 -7
  19. package/dist/blockchain/DataStore.d.ts.map +1 -1
  20. package/dist/blockchain/DataStore.js +112 -157
  21. package/dist/blockchain/DataStoreSerializer.d.ts +1 -1
  22. package/dist/blockchain/DataStoreSerializer.d.ts.map +1 -1
  23. package/dist/blockchain/FullNodePeer.d.ts +94 -9
  24. package/dist/blockchain/FullNodePeer.d.ts.map +1 -1
  25. package/dist/blockchain/FullNodePeer.js +323 -121
  26. package/dist/blockchain/ServerCoin.d.ts +13 -5
  27. package/dist/blockchain/ServerCoin.d.ts.map +1 -1
  28. package/dist/blockchain/ServerCoin.js +90 -28
  29. package/dist/blockchain/StoreMonitorRegistry.d.ts +85 -0
  30. package/dist/blockchain/StoreMonitorRegistry.d.ts.map +1 -0
  31. package/dist/blockchain/StoreMonitorRegistry.js +242 -0
  32. package/dist/blockchain/Wallet.d.ts +2 -2
  33. package/dist/blockchain/Wallet.d.ts.map +1 -1
  34. package/dist/blockchain/Wallet.js +50 -18
  35. package/dist/blockchain/coins.d.ts +1 -2
  36. package/dist/blockchain/coins.d.ts.map +1 -1
  37. package/dist/blockchain/coins.js +1 -55
  38. package/dist/blockchain/index.d.ts +1 -0
  39. package/dist/blockchain/index.d.ts.map +1 -1
  40. package/dist/blockchain/index.js +1 -0
  41. package/dist/types.d.ts +2 -0
  42. package/dist/types.d.ts.map +1 -1
  43. package/dist/utils/ContentScanner.d.ts +63 -0
  44. package/dist/utils/ContentScanner.d.ts.map +1 -0
  45. package/dist/utils/ContentScanner.js +175 -0
  46. package/dist/utils/Environment.d.ts +21 -0
  47. package/dist/utils/Environment.d.ts.map +1 -0
  48. package/dist/utils/Environment.js +92 -0
  49. package/dist/utils/FileCache.d.ts +1 -1
  50. package/dist/utils/FileCache.d.ts.map +1 -1
  51. package/dist/utils/FileCache.js +2 -2
  52. package/dist/utils/FileTransfer.d.ts +47 -0
  53. package/dist/utils/FileTransfer.d.ts.map +1 -0
  54. package/dist/utils/FileTransfer.js +209 -0
  55. package/dist/utils/NconfManager.js +2 -1
  56. package/dist/utils/StoreArchiveManager.d.ts +45 -0
  57. package/dist/utils/StoreArchiveManager.d.ts.map +1 -0
  58. package/dist/utils/StoreArchiveManager.js +153 -0
  59. package/dist/utils/config.d.ts +4 -3
  60. package/dist/utils/config.d.ts.map +1 -1
  61. package/dist/utils/config.js +12 -7
  62. package/dist/utils/directoryUtils.d.ts +6 -0
  63. package/dist/utils/directoryUtils.d.ts.map +1 -1
  64. package/dist/utils/directoryUtils.js +31 -9
  65. package/dist/utils/index.d.ts +2 -1
  66. package/dist/utils/index.d.ts.map +1 -1
  67. package/dist/utils/index.js +2 -1
  68. package/dist/utils/merkle.d.ts +2 -0
  69. package/dist/utils/merkle.d.ts.map +1 -0
  70. package/dist/utils/merkle.js +28 -0
  71. package/dist/utils/network.d.ts +1 -0
  72. package/dist/utils/network.d.ts.map +1 -1
  73. package/dist/utils/network.js +38 -11
  74. package/dist/utils/promiseUtils.d.ts +17 -0
  75. package/dist/utils/promiseUtils.d.ts.map +1 -0
  76. package/dist/utils/promiseUtils.js +38 -0
  77. package/dist/utils/ssl.js +2 -2
  78. package/package.json +16 -5
  79. package/dist/utils/deltaUtils.d.ts +0 -2
  80. package/dist/utils/deltaUtils.d.ts.map +0 -1
  81. package/dist/utils/deltaUtils.js +0 -83
@@ -7,91 +7,176 @@ exports.FullNodePeer = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const os_1 = __importDefault(require("os"));
9
9
  const fs_1 = __importDefault(require("fs"));
10
- const datalayer_driver_1 = require("datalayer-driver");
11
- const chia_server_coin_1 = require("chia-server-coin");
10
+ const datalayer_driver_1 = require("@dignetwork/datalayer-driver");
12
11
  const promises_1 = require("dns/promises");
13
12
  const net_1 = __importDefault(require("net"));
14
- const lodash_1 = require("lodash");
15
13
  const nanospinner_1 = require("nanospinner");
16
14
  const config_1 = require("../utils/config");
15
+ const Environment_1 = require("../utils/Environment");
16
+ const node_cache_1 = __importDefault(require("node-cache"));
17
+ // Constants
17
18
  const FULLNODE_PORT = 8444;
18
19
  const LOCALHOST = "127.0.0.1";
20
+ const CHIA_NODES_HOST = "chia-nodes";
19
21
  const DNS_HOSTS = [
20
22
  "dns-introducer.chia.net",
21
23
  "chia.ctrlaltdel.ch",
22
24
  "seeder.dexie.space",
23
25
  "chia.hoffmang.com",
24
26
  ];
25
- const CONNECTION_TIMEOUT = 2000;
26
- const CACHE_DURATION = 30000; // Cache duration in milliseconds
27
+ const CONNECTION_TIMEOUT = 2000; // in milliseconds
28
+ const CACHE_DURATION = 30000; // in milliseconds
29
+ const COOLDOWN_DURATION = 60000; // in milliseconds
30
+ const MAX_PEERS_TO_FETCH = 5; // Maximum number of peers to fetch from DNS
31
+ const MAX_RETRIES = 3; // Maximum number of retry attempts
32
+ /**
33
+ * FullNodePeer manages connections to full nodes, prioritizing certain peers and handling reliability.
34
+ */
27
35
  class FullNodePeer {
36
+ // Private constructor for singleton pattern
28
37
  constructor(peer) {
29
38
  this.peer = peer;
30
39
  }
40
+ /**
41
+ * Retrieves the singleton instance of FullNodePeer.
42
+ * @returns {FullNodePeer} The singleton instance.
43
+ */
44
+ static getInstance() {
45
+ if (!FullNodePeer.instance) {
46
+ FullNodePeer.instance = new FullNodePeer(null); // Temporarily set to null
47
+ }
48
+ return FullNodePeer.instance;
49
+ }
50
+ /**
51
+ * Initializes the singleton instance by connecting to the best peer.
52
+ */
53
+ async initialize() {
54
+ if (this.peer)
55
+ return; // Already initialized
56
+ try {
57
+ const bestPeer = await FullNodePeer.getBestPeer();
58
+ this.peer = bestPeer;
59
+ FullNodePeer.instance = this; // Assign the initialized instance
60
+ }
61
+ catch (error) {
62
+ console.error(`Initialization failed: ${error.message}`);
63
+ throw error;
64
+ }
65
+ }
66
+ /**
67
+ * Connects and returns the best available peer.
68
+ * Implements singleton behavior.
69
+ * @returns {Promise<Peer>} The connected Peer instance.
70
+ */
31
71
  static async connect() {
32
- const peer = await FullNodePeer.getBestPeer();
33
- return new FullNodePeer(peer).peer;
72
+ const instance = FullNodePeer.getInstance();
73
+ await instance.initialize();
74
+ return instance.peer;
34
75
  }
76
+ /**
77
+ * Checks if a given port on a host is reachable.
78
+ * @param {string} host - The host IP address.
79
+ * @param {number} port - The port number.
80
+ * @param {number} timeout - Connection timeout in milliseconds.
81
+ * @returns {Promise<boolean>} Whether the port is reachable.
82
+ */
35
83
  static isPortReachable(host, port, timeout = CONNECTION_TIMEOUT) {
36
84
  return new Promise((resolve) => {
37
85
  const socket = new net_1.default.Socket()
38
86
  .setTimeout(timeout)
39
- .once("error", () => resolve(false))
40
- .once("timeout", () => resolve(false))
87
+ .once("error", () => {
88
+ socket.destroy();
89
+ resolve(false);
90
+ })
91
+ .once("timeout", () => {
92
+ socket.destroy();
93
+ resolve(false);
94
+ })
41
95
  .connect(port, host, () => {
42
96
  socket.end();
43
97
  resolve(true);
44
98
  });
45
99
  });
46
100
  }
101
+ /**
102
+ * Validates an IPv4 address.
103
+ * @param {string} ip - The IP address to validate.
104
+ * @returns {boolean} Whether the IP address is valid.
105
+ */
47
106
  static isValidIpAddress(ip) {
48
- const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
107
+ const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/;
49
108
  return ipv4Regex.test(ip);
50
109
  }
51
110
  /**
52
- * Retrieves the TRUSTED_FULLNODE IP from the environment
53
- * and verifies if it is a valid IP address.
54
- *
55
- * @returns {string | null} The valid IP address or null if invalid
111
+ * Retrieves the TRUSTED_FULLNODE IP from the environment and verifies its validity.
112
+ * @returns {string | null} The trusted full node IP or null if invalid.
56
113
  */
57
114
  static getTrustedFullNode() {
58
- const trustedNodeIp = process.env.TRUSTED_FULLNODE || null;
115
+ const trustedNodeIp = Environment_1.Environment.TRUSTED_FULLNODE || null;
59
116
  if (trustedNodeIp && FullNodePeer.isValidIpAddress(trustedNodeIp)) {
60
117
  return trustedNodeIp;
61
118
  }
62
119
  return null;
63
120
  }
121
+ /**
122
+ * Fetches new peer IPs from DNS introducers and prioritized hosts.
123
+ * Utilizes caching to avoid redundant DNS resolutions.
124
+ * @returns {Promise<string[]>} An array of reachable peer IPs.
125
+ */
64
126
  static async fetchNewPeerIPs() {
65
127
  const trustedNodeIp = FullNodePeer.getTrustedFullNode();
66
128
  const priorityIps = [];
129
+ // Define prioritized peers
130
+ FullNodePeer.prioritizedPeers = [
131
+ ...DNS_HOSTS, // Assuming CHIA_NODES_HOST is included in DNS_HOSTS
132
+ LOCALHOST,
133
+ ];
134
+ // Add trustedNodeIp if available
135
+ if (trustedNodeIp) {
136
+ FullNodePeer.prioritizedPeers.unshift(trustedNodeIp);
137
+ }
67
138
  // Prioritize trustedNodeIp
68
139
  if (trustedNodeIp &&
140
+ !FullNodePeer.cooldownCache.has(trustedNodeIp) &&
69
141
  (await FullNodePeer.isPortReachable(trustedNodeIp, FULLNODE_PORT))) {
70
142
  priorityIps.push(trustedNodeIp);
71
143
  }
72
144
  // Prioritize LOCALHOST
73
- if (await FullNodePeer.isPortReachable(LOCALHOST, FULLNODE_PORT)) {
145
+ if (!FullNodePeer.cooldownCache.has(LOCALHOST) &&
146
+ (await FullNodePeer.isPortReachable(LOCALHOST, FULLNODE_PORT))) {
74
147
  priorityIps.push(LOCALHOST);
75
148
  }
149
+ // Prioritize CHIA_NODES_HOST
150
+ if (!FullNodePeer.cooldownCache.has(CHIA_NODES_HOST) &&
151
+ (await FullNodePeer.isPortReachable(CHIA_NODES_HOST, FULLNODE_PORT))) {
152
+ priorityIps.push(CHIA_NODES_HOST);
153
+ }
76
154
  if (priorityIps.length > 0) {
77
155
  return priorityIps;
78
156
  }
157
+ // Check if cached peer IPs exist
158
+ const cachedPeerIPs = FullNodePeer.peerIPCache.get("peerIPs");
159
+ if (cachedPeerIPs) {
160
+ return cachedPeerIPs;
161
+ }
79
162
  // Fetch peers from DNS introducers
163
+ const fetchedPeers = [];
80
164
  for (const DNS_HOST of DNS_HOSTS) {
81
165
  try {
82
166
  const ips = await (0, promises_1.resolve4)(DNS_HOST);
83
167
  if (ips && ips.length > 0) {
84
- const shuffledIps = ips.sort(() => 0.5 - Math.random());
168
+ const shuffledIps = FullNodePeer.shuffleArray(ips);
85
169
  const reachableIps = [];
86
170
  for (const ip of shuffledIps) {
87
- if (await FullNodePeer.isPortReachable(ip, FULLNODE_PORT)) {
171
+ if (!FullNodePeer.cooldownCache.has(ip) &&
172
+ (await FullNodePeer.isPortReachable(ip, FULLNODE_PORT))) {
88
173
  reachableIps.push(ip);
89
174
  }
90
- if (reachableIps.length === 5)
91
- break;
175
+ if (reachableIps.length === MAX_PEERS_TO_FETCH)
176
+ break; // Limit to MAX_PEERS_TO_FETCH peers
92
177
  }
93
178
  if (reachableIps.length > 0) {
94
- return reachableIps;
179
+ fetchedPeers.push(...reachableIps);
95
180
  }
96
181
  }
97
182
  }
@@ -99,62 +184,209 @@ class FullNodePeer {
99
184
  console.error(`Failed to resolve IPs from ${DNS_HOST}: ${error.message}`);
100
185
  }
101
186
  }
187
+ // Cache the fetched peer IPs
188
+ if (fetchedPeers.length > 0) {
189
+ FullNodePeer.peerIPCache.set("peerIPs", fetchedPeers);
190
+ return fetchedPeers;
191
+ }
102
192
  throw new Error("No reachable IPs found in any DNS records.");
103
193
  }
104
- static async getPeerIPs() {
105
- const ips = await FullNodePeer.memoizedFetchNewPeerIPs();
106
- const reachableIps = await Promise.all(ips.map(async (ip) => {
107
- if (ip && (await FullNodePeer.isPortReachable(ip, FULLNODE_PORT))) {
108
- return ip;
194
+ /**
195
+ * Shuffles an array using the Fisher-Yates algorithm.
196
+ * @param {T[]} array - The array to shuffle.
197
+ * @returns {T[]} The shuffled array.
198
+ */
199
+ static shuffleArray(array) {
200
+ const shuffled = [...array];
201
+ for (let i = shuffled.length - 1; i > 0; i--) {
202
+ const j = Math.floor(Math.random() * (i + 1));
203
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
204
+ }
205
+ return shuffled;
206
+ }
207
+ /**
208
+ * Initializes the peer weights map.
209
+ * Assigns higher initial weights to prioritized peers.
210
+ * @param {string[]} peerIPs - An array of peer IPs.
211
+ */
212
+ static initializePeerWeights(peerIPs) {
213
+ for (const ip of peerIPs) {
214
+ if (!FullNodePeer.peerWeights.has(ip)) {
215
+ if (ip === CHIA_NODES_HOST ||
216
+ ip === LOCALHOST ||
217
+ ip === FullNodePeer.getTrustedFullNode()) {
218
+ FullNodePeer.peerWeights.set(ip, 5); // Higher weight for prioritized peers
219
+ }
220
+ else {
221
+ FullNodePeer.peerWeights.set(ip, 1); // Default weight
222
+ }
223
+ }
224
+ }
225
+ }
226
+ /**
227
+ * Selects a peer based on weighted random selection.
228
+ * Prioritized peers have higher weights.
229
+ * @returns {string} The selected peer IP.
230
+ */
231
+ static selectPeerByWeight() {
232
+ const peers = Array.from(FullNodePeer.peerWeights.entries())
233
+ .filter(([ip, _]) => !FullNodePeer.cooldownCache.has(ip))
234
+ .map(([ip, weight]) => ({ ip, weight }));
235
+ const totalWeight = peers.reduce((sum, peer) => sum + peer.weight, 0);
236
+ if (totalWeight === 0) {
237
+ throw new Error("All peers are in cooldown.");
238
+ }
239
+ const random = Math.random() * totalWeight;
240
+ let cumulative = 0;
241
+ for (const peer of peers) {
242
+ cumulative += peer.weight;
243
+ if (random < cumulative) {
244
+ return peer.ip;
109
245
  }
110
- return null;
111
- })).then((results) => results.filter((ip) => ip !== null));
112
- if (reachableIps.length > 0) {
113
- return reachableIps;
114
246
  }
115
- // @ts-ignore
116
- if (FullNodePeer.memoizedFetchNewPeerIPs?.cache?.clear) {
117
- // @ts-ignore
118
- FullNodePeer.memoizedFetchNewPeerIPs.cache.clear();
247
+ // Fallback
248
+ return peers[peers.length - 1].ip;
249
+ }
250
+ /**
251
+ * Retrieves all reachable peer IPs, excluding those in cooldown.
252
+ * @returns {Promise<string[]>} An array of reachable peer IPs.
253
+ */
254
+ static async getPeerIPs() {
255
+ const peerIPs = await FullNodePeer.fetchNewPeerIPs();
256
+ return peerIPs;
257
+ }
258
+ /**
259
+ * Initializes the peer weights based on prioritization and reliability.
260
+ * @param {string[]} peerIPs - An array of peer IPs.
261
+ */
262
+ static setupPeers(peerIPs) {
263
+ FullNodePeer.initializePeerWeights(peerIPs);
264
+ }
265
+ /**
266
+ * Connects to the best available peer based on weighted selection and reliability.
267
+ * @returns {Promise<Peer>} The connected Peer instance.
268
+ */
269
+ static async getBestPeer() {
270
+ const now = Date.now();
271
+ // Refresh cachedPeer if expired
272
+ if (FullNodePeer.cachedPeer &&
273
+ now - FullNodePeer.cachedPeer.timestamp < CACHE_DURATION) {
274
+ return FullNodePeer.cachedPeer.peer;
275
+ }
276
+ // Fetch peer IPs
277
+ const peerIPs = await FullNodePeer.getPeerIPs();
278
+ // Setup peer weights with prioritization
279
+ FullNodePeer.setupPeers(peerIPs);
280
+ // Weighted random selection
281
+ let selectedPeerIP;
282
+ try {
283
+ selectedPeerIP = FullNodePeer.selectPeerByWeight();
284
+ }
285
+ catch (error) {
286
+ throw new Error(`Failed to select a peer: ${error.message}`);
119
287
  }
120
- return FullNodePeer.memoizedFetchNewPeerIPs();
288
+ // Attempt to create a peer connection
289
+ const sslFolder = path_1.default.resolve(os_1.default.homedir(), ".dig", "ssl");
290
+ const certFile = path_1.default.join(sslFolder, "public_dig.crt");
291
+ const keyFile = path_1.default.join(sslFolder, "public_dig.key");
292
+ if (!fs_1.default.existsSync(sslFolder)) {
293
+ fs_1.default.mkdirSync(sslFolder, { recursive: true });
294
+ }
295
+ const tls = new datalayer_driver_1.Tls(certFile, keyFile);
296
+ let peer;
297
+ try {
298
+ peer = await datalayer_driver_1.Peer.new(`${selectedPeerIP}:${FULLNODE_PORT}`, false, tls);
299
+ }
300
+ catch (error) {
301
+ console.error(`Failed to create peer for IP ${selectedPeerIP}: ${error.message}`);
302
+ // Add to cooldown
303
+ FullNodePeer.cooldownCache.set(selectedPeerIP, true);
304
+ // Decrease weight or remove peer
305
+ const currentWeight = FullNodePeer.peerWeights.get(selectedPeerIP) || 1;
306
+ if (currentWeight > 1) {
307
+ FullNodePeer.peerWeights.set(selectedPeerIP, currentWeight - 1);
308
+ }
309
+ else {
310
+ FullNodePeer.peerWeights.delete(selectedPeerIP);
311
+ }
312
+ throw new Error(`Unable to connect to peer ${selectedPeerIP}`);
313
+ }
314
+ // Wrap the peer with proxy to handle errors and retries
315
+ const proxiedPeer = FullNodePeer.createPeerProxy(peer, selectedPeerIP);
316
+ // Store PeerInfo
317
+ FullNodePeer.peerInfos.set(selectedPeerIP, {
318
+ peer: proxiedPeer,
319
+ weight: FullNodePeer.peerWeights.get(selectedPeerIP) || 1,
320
+ address: selectedPeerIP,
321
+ });
322
+ // Cache the peer
323
+ FullNodePeer.cachedPeer = { peer: proxiedPeer, timestamp: now };
324
+ console.log(`Using Fullnode Peer: ${selectedPeerIP}`);
325
+ return proxiedPeer;
121
326
  }
122
- static createPeerProxy(peer) {
327
+ /**
328
+ * Creates a proxy for the peer to handle errors and implement retries.
329
+ * @param {Peer} peer - The Peer instance.
330
+ * @param {string} peerIP - The IP address of the peer.
331
+ * @param {number} [retryCount=0] - The current retry attempt.
332
+ * @returns {Peer} The proxied Peer instance.
333
+ */
334
+ static createPeerProxy(peer, peerIP, retryCount = 0) {
123
335
  return new Proxy(peer, {
124
336
  get: (target, prop) => {
125
337
  const originalMethod = target[prop];
126
338
  if (typeof originalMethod === "function") {
127
339
  return async (...args) => {
128
- let timeoutId;
129
- // Start the timeout to forget the peer after 1 minute
130
- const timeoutPromise = new Promise((_, reject) => {
131
- timeoutId = setTimeout(() => {
132
- FullNodePeer.cachedPeer = null;
133
- reject(new Error("Operation timed out. Reconnecting to a new peer."));
134
- }, 60000); // 1 minute
135
- });
136
340
  try {
137
- // Run the original method and race it against the timeout
138
- const result = await Promise.race([
139
- originalMethod.apply(target, args),
140
- timeoutPromise,
141
- ]);
142
- // Clear the timeout if the operation succeeded
143
- if (timeoutId) {
144
- clearTimeout(timeoutId);
145
- }
341
+ const result = await originalMethod.apply(target, args);
342
+ // On successful operation, increase the weight slightly
343
+ const currentWeight = FullNodePeer.peerWeights.get(peerIP) || 1;
344
+ FullNodePeer.peerWeights.set(peerIP, currentWeight + 0.1); // Increment weight
146
345
  return result;
147
346
  }
148
347
  catch (error) {
149
- // If the error is WebSocket-related or timeout, reset the peer
150
- if (error.message.includes("WebSocket") || error.message.includes("Operation timed out")) {
151
- FullNodePeer.cachedPeer = null;
152
- // @ts-ignore
153
- FullNodePeer.memoizedFetchNewPeerIPs.cache.clear();
154
- const newPeer = await FullNodePeer.getBestPeer();
155
- return newPeer[prop](...args);
348
+ console.error(`Peer ${peerIP} encountered an error: ${error.message}`);
349
+ // Check if the error is related to WebSocket or Operation timed out
350
+ if (error.message.includes("WebSocket") ||
351
+ error.message.includes("Operation timed out")) {
352
+ // Add the faulty peer to the cooldown cache
353
+ FullNodePeer.cooldownCache.set(peerIP, true);
354
+ // Decrease weight or remove peer
355
+ const currentWeight = FullNodePeer.peerWeights.get(peerIP) || 1;
356
+ if (currentWeight > 1) {
357
+ FullNodePeer.peerWeights.set(peerIP, currentWeight - 1);
358
+ }
359
+ else {
360
+ FullNodePeer.peerWeights.delete(peerIP);
361
+ }
362
+ // If maximum retries reached, throw the error
363
+ if (retryCount >= MAX_RETRIES) {
364
+ console.error(`Max retries reached for method ${String(prop)} on peer ${peerIP}.`);
365
+ throw error;
366
+ }
367
+ // Attempt to select a new peer and retry the method
368
+ try {
369
+ console.info(`Selecting a new peer to retry method ${String(prop)}...`);
370
+ const newPeer = await FullNodePeer.getBestPeer();
371
+ // Extract new peer's IP address
372
+ const newPeerIP = FullNodePeer.extractPeerIP(newPeer);
373
+ if (!newPeerIP) {
374
+ throw new Error("Unable to extract IP from the new peer.");
375
+ }
376
+ // Wrap the new peer with a proxy, incrementing the retry count
377
+ const proxiedNewPeer = FullNodePeer.createPeerProxy(newPeer, newPeerIP, retryCount + 1);
378
+ // Retry the method on the new peer
379
+ return await proxiedNewPeer[prop](...args);
380
+ }
381
+ catch (retryError) {
382
+ console.error(`Retry failed on a new peer: ${retryError.message}`);
383
+ throw retryError;
384
+ }
385
+ }
386
+ else {
387
+ // For other errors, handle normally
388
+ throw error;
156
389
  }
157
- throw error;
158
390
  }
159
391
  };
160
392
  }
@@ -162,65 +394,24 @@ class FullNodePeer {
162
394
  },
163
395
  });
164
396
  }
165
- static async getBestPeer() {
166
- const now = Date.now();
167
- if (FullNodePeer.cachedPeer &&
168
- now - FullNodePeer.cachedPeer.timestamp < CACHE_DURATION) {
169
- return FullNodePeer.cachedPeer.peer;
170
- }
171
- const sslFolder = path_1.default.resolve(os_1.default.homedir(), ".dig", "ssl");
172
- const certFile = path_1.default.join(sslFolder, "public_dig.crt");
173
- const keyFile = path_1.default.join(sslFolder, "public_dig.key");
174
- if (!fs_1.default.existsSync(sslFolder)) {
175
- fs_1.default.mkdirSync(sslFolder, { recursive: true });
176
- }
177
- new chia_server_coin_1.Tls(certFile, keyFile);
178
- const peerIPs = await FullNodePeer.getPeerIPs();
179
- const trustedNodeIp = process.env.TRUSTED_FULLNODE || null;
180
- const peers = await Promise.all(peerIPs.map(async (ip) => {
181
- if (ip) {
182
- try {
183
- const peer = await datalayer_driver_1.Peer.new(`${ip}:${FULLNODE_PORT}`, false, certFile, keyFile);
184
- return FullNodePeer.createPeerProxy(peer);
185
- }
186
- catch (error) {
187
- console.error(`Failed to create peer for IP ${ip}: ${error.message}`);
188
- return null;
189
- }
397
+ /**
398
+ * Extracts the IP address from a Peer instance.
399
+ * @param {Peer} peer - The Peer instance.
400
+ * @returns {string | null} The extracted IP address or null if not found.
401
+ */
402
+ static extractPeerIP(peer) {
403
+ for (const [ip, info] of FullNodePeer.peerInfos.entries()) {
404
+ if (info.peer === peer) {
405
+ return ip;
190
406
  }
191
- return null;
192
- })).then((results) => results.filter((peer) => peer !== null));
193
- if (peers.length === 0) {
194
- throw new Error("No peers available, please try again.");
195
- }
196
- await new Promise((resolve) => setTimeout(resolve, 1000));
197
- const peakHeights = await Promise.all(peers.map((peer) => peer
198
- .getPeak()
199
- .then((height) => height)
200
- .catch((error) => {
201
- console.error(`Failed to get peak for peer: ${error.message}`);
202
- return null;
203
- })));
204
- const validHeights = peakHeights.filter((height) => height !== null);
205
- if (validHeights.length === 0) {
206
- throw new Error("No valid peak heights obtained from any peer.");
207
- }
208
- const highestPeak = Math.max(...validHeights);
209
- // Prioritize LOCALHOST and TRUSTED_NODE_IP if they have the highest peak height
210
- let bestPeerIndex = validHeights.findIndex((height, index) => height === highestPeak &&
211
- (peerIPs[index] === LOCALHOST || peerIPs[index] === trustedNodeIp));
212
- // If LOCALHOST or TRUSTED_NODE_IP don't have the highest peak, select any peer with the highest peak
213
- if (bestPeerIndex === -1) {
214
- bestPeerIndex = validHeights.findIndex((height) => height === highestPeak);
215
- }
216
- const bestPeer = peers[bestPeerIndex];
217
- FullNodePeer.cachedPeer = { peer: bestPeer, timestamp: now };
218
- console.log(`Using Peer: ${peerIPs[bestPeerIndex]}`);
219
- return bestPeer;
220
- }
221
- getPeer() {
222
- return this.peer;
407
+ }
408
+ return null;
223
409
  }
410
+ /**
411
+ * Waits for a coin to be confirmed (spent) on the blockchain.
412
+ * @param {Buffer} parentCoinInfo - The parent coin information.
413
+ * @returns {Promise<boolean>} Whether the coin was confirmed.
414
+ */
224
415
  static async waitForConfirmation(parentCoinInfo) {
225
416
  const spinner = (0, nanospinner_1.createSpinner)("Waiting for confirmation...").start();
226
417
  const peer = await FullNodePeer.connect();
@@ -236,12 +427,23 @@ class FullNodePeer {
236
427
  }
237
428
  catch (error) {
238
429
  spinner.error({ text: "Error while waiting for confirmation." });
430
+ console.error(`waitForConfirmation error: ${error.message}`);
239
431
  throw error;
240
432
  }
241
433
  }
242
434
  }
243
435
  exports.FullNodePeer = FullNodePeer;
436
+ // Singleton instance
437
+ FullNodePeer.instance = null;
438
+ // Cached peer with timestamp
244
439
  FullNodePeer.cachedPeer = null;
245
- (() => {
246
- FullNodePeer.memoizedFetchNewPeerIPs = (0, lodash_1.memoize)(FullNodePeer.fetchNewPeerIPs);
247
- })();
440
+ // Cooldown cache to exclude faulty peers temporarily
441
+ FullNodePeer.cooldownCache = new node_cache_1.default({ stdTTL: COOLDOWN_DURATION / 1000 });
442
+ // Peer reliability weights
443
+ FullNodePeer.peerWeights = new Map();
444
+ // List of prioritized peers
445
+ FullNodePeer.prioritizedPeers = [];
446
+ // Map to store PeerInfo
447
+ FullNodePeer.peerInfos = new Map();
448
+ // Cache for fetched peer IPs
449
+ FullNodePeer.peerIPCache = new node_cache_1.default({ stdTTL: CACHE_DURATION / 1000 });
@@ -1,21 +1,29 @@
1
- import { ServerCoin as ServerCoinDriver } from "datalayer-driver";
1
+ import { ServerCoin as ServerCoinDriver } from "@dignetwork/datalayer-driver";
2
2
  import { NconfManager } from "../utils/NconfManager";
3
3
  export declare class ServerCoin {
4
4
  private storeId;
5
5
  static readonly serverCoinManager: NconfManager;
6
6
  constructor(storeId: string);
7
- createForEpoch(peerIp: string): Promise<ServerCoinDriver>;
8
- saveServerCoinData(serverCoin: ServerCoinDriver, epoch: number, peerIp: string): Promise<void>;
7
+ createForEpoch(peerIp: string, rootHash: string): Promise<ServerCoinDriver>;
8
+ saveServerCoinData(serverCoin: ServerCoinDriver, epoch: number, peerIp: string, rootHash: string): Promise<void>;
9
9
  melt(epoch: number, peerIp: string): Promise<void>;
10
10
  private removeServerCoinData;
11
+ getAllEpochPeers(epoch: number, blacklist?: string[]): Promise<string[]>;
12
+ getActiveEpochPeers(blacklist?: string[]): Promise<string[]>;
11
13
  sampleCurrentEpoch(sampleSize?: number, blacklist?: string[]): Promise<string[]>;
12
14
  sampleServerCoinsByEpoch(epoch: number, sampleSize?: number, blacklist?: string[]): Promise<string[]>;
13
- static getCurrentEpoch(): number;
15
+ static getCurrentEpoch(): {
16
+ epoch: number;
17
+ round: number;
18
+ };
14
19
  ensureServerCoinExists(peerIp: string): Promise<void>;
15
20
  meltOutdatedEpochs(peerIp: string): Promise<void>;
16
21
  static meltUntrackedStoreCoins(): Promise<void>;
17
22
  private getServerCoinsForStore;
18
23
  hasEpochCoinBeenCreated(currentEpoch: number, peerIp: string): Promise<boolean>;
19
- static calculateEpoch(currentTimestampUTC: Date): number;
24
+ static calculateEpochAndRound(currentTimestampUTC: Date): {
25
+ epoch: number;
26
+ round: number;
27
+ };
20
28
  }
21
29
  //# sourceMappingURL=ServerCoin.d.ts.map
@@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