Skip to content

Commit 629123d

Browse files
Merge pull request #120 from DIG-Network/release/v0.0.1-alpha.131
Release/v0.0.1 alpha.131
2 parents 753922a + 714ae8f commit 629123d

File tree

4 files changed

+71
-43
lines changed

4 files changed

+71
-43
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.131](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.130...v0.0.1-alpha.131) (2024-10-04)
6+
7+
8+
### Features
9+
10+
* speed up findpeerwith key ([9de9394](https://github.com/DIG-Network/dig-chia-sdk/commit/9de93949d0a8d9660c17f41e3e7de64238fed4ac))
11+
512
### [0.0.1-alpha.130](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.129...v0.0.1-alpha.130) (2024-10-04)
613

714

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

src/DigNetwork/DigNetwork.ts

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,63 +38,78 @@ export class DigNetwork {
3838
storeId: string,
3939
rootHash: string,
4040
key?: string,
41-
intialBlackList: string[] = []
41+
initialBlackList: string[] = []
4242
): Promise<DigPeer | null> {
43-
const peerBlackList: string[] = intialBlackList;
43+
const peerBlackList: string[] = initialBlackList;
4444
const serverCoin = new ServerCoin(storeId);
45-
let peerIp: string | null = null;
4645

47-
// Keep sampling peers until an empty array is returned
4846
while (true) {
4947
try {
50-
// Sample a peer from the current epoch
51-
const digPeers = await serverCoin.sampleCurrentEpoch(1, peerBlackList);
48+
// Sample 10 peers from the current epoch
49+
const digPeers = await serverCoin.sampleCurrentEpoch(10, peerBlackList);
5250

5351
// If no peers are returned, break out of the loop
5452
if (digPeers.length === 0) {
5553
console.log("No more peers found.");
5654
break;
5755
}
5856

59-
peerIp = digPeers[0];
60-
const digPeer = new DigPeer(peerIp, storeId);
61-
62-
// Try to fetch the head store information
63-
const { storeExists, rootHashExists } =
64-
await digPeer.propagationServer.checkStoreExists(rootHash);
57+
// Create a race of promises for all peers
58+
const peerPromises = digPeers.map((peerIp) => {
59+
return new Promise<DigPeer | null>(async (resolve) => {
60+
try {
61+
const digPeer = new DigPeer(peerIp, storeId);
62+
const { storeExists, rootHashExists } =
63+
await digPeer.propagationServer.checkStoreExists(rootHash);
64+
65+
// Check if the store and root hash exist on the peer
66+
if (storeExists && rootHashExists) {
67+
console.log(
68+
`Found Peer at ${peerIp} for storeId: ${storeId}, root hash ${rootHash}`
69+
);
70+
71+
// If no key is provided, resolve the peer
72+
if (!key) {
73+
return resolve(digPeer);
74+
}
75+
76+
// If key is provided, check if the peer has it
77+
const keyResponse = await digPeer.contentServer.headKey(
78+
key,
79+
rootHash
80+
);
81+
if (keyResponse.headers?.["x-key-exists"] === "true") {
82+
return resolve(digPeer);
83+
}
84+
}
85+
} catch (error) {
86+
console.error(`Error connecting to DIG Peer ${peerIp}.`);
87+
}
6588

66-
// If the peer has the correct root hash, check if key is required
67-
if (storeExists && rootHashExists) {
68-
console.log(
69-
`Found Peer at ${peerIp} for storeId: ${storeId}, root hash ${rootHash}`
70-
);
89+
// If the peer does not meet the criteria, resolve with null
90+
resolve(null);
91+
});
92+
});
7193

72-
// If no key is provided, return the peer
73-
if (!key) {
74-
return digPeer;
75-
}
94+
// Wait for the first valid peer that resolves
95+
const firstValidPeer = await Promise.race(peerPromises);
7696

77-
// If key is provided, check if the peer has it
78-
const keyResponse = await digPeer.contentServer.headKey(
79-
key,
80-
rootHash
81-
);
82-
if (keyResponse.headers?.["x-key-exists"] === "true") {
83-
return digPeer;
84-
}
97+
// If a valid peer is found, return it
98+
if (firstValidPeer) {
99+
return firstValidPeer;
85100
}
86101

87-
// Add peer to blacklist if it doesn't meet criteria
88-
peerBlackList.push(peerIp);
102+
// If none of the peers were valid, add them to the blacklist
103+
digPeers.forEach((peerIp) => peerBlackList.push(peerIp));
104+
105+
// Retry with the next set of peers
106+
console.log("No valid peers found, retrying with new peers...");
89107
} catch (error) {
90-
console.error(`Error connecting to DIG Peer ${peerIp}. Resampling...`);
91-
if (peerIp) {
92-
peerBlackList.push(peerIp); // Add to blacklist if error occurs
93-
}
108+
console.error("Error sampling peers. Resampling...");
94109
}
95110
}
96111

97-
// Return null if no valid peer was found
112+
// Return null if no valid peer was found after all attempts
98113
return null;
99114
}
100115

@@ -105,7 +120,10 @@ export class DigNetwork {
105120
fs.unlinkSync(path.join(DIG_FOLDER_PATH, "stores", storeId + ".json"));
106121
}
107122

108-
public static async pingNetworkOfUpdate(storeId: string, rootHash: string): Promise<void> {
123+
public static async pingNetworkOfUpdate(
124+
storeId: string,
125+
rootHash: string
126+
): Promise<void> {
109127
const serverCoin = new ServerCoin(storeId);
110128
// When an update is made, ping 10 network peers to pull updates from this store
111129
const digPeers = await serverCoin.sampleCurrentEpoch(10);
@@ -115,7 +133,7 @@ export class DigNetwork {
115133
digPeer.propagationServer.pingUpdate(rootHash),
116134
5000,
117135
`headKey timed out for peer ${digPeer.IpAddress}`
118-
)
136+
);
119137
}
120138
}
121139

@@ -213,8 +231,11 @@ export class DigNetwork {
213231
}
214232
}
215233
}
216-
217-
DigNetwork.pingNetworkOfUpdate(this.dataStore.StoreId, rootInfo.root_hash);
234+
235+
DigNetwork.pingNetworkOfUpdate(
236+
this.dataStore.StoreId,
237+
rootInfo.root_hash
238+
);
218239
}
219240

220241
console.log("Syncing store complete.");

0 commit comments

Comments
 (0)