Skip to content

Commit a7ed3ff

Browse files
Merge pull request #20 from DIG-Network/release/v0.0.1-alpha.20
Release/v0.0.1 alpha.20
2 parents 9821d18 + fe44b2e commit a7ed3ff

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
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.20](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.19...v0.0.1-alpha.20) (2024-09-16)
6+
7+
8+
### Features
9+
10+
* find any peer on the network with the store and or key ([15cf0af](https://github.com/DIG-Network/dig-chia-sdk/commit/15cf0af395ccc9009a6e9ff898c6f5d21ba4d92a))
11+
512
### [0.0.1-alpha.19](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.18...v0.0.1-alpha.19) (2024-09-16)
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.19",
3+
"version": "0.0.1-alpha.20",
44
"description": "",
55
"type": "commonjs",
66
"main": "./dist/index.js",

src/DigNetwork/ContentServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class ContentServer {
8383
}
8484

8585
// Method to check if a specific store exists (HEAD request)
86-
public async headStore(options?: { hasRootHash: boolean}): Promise<{
86+
public async headStore(options?: { hasRootHash: string}): Promise<{
8787
success: boolean;
8888
headers?: http.IncomingHttpHeaders;
8989
}> {

src/DigNetwork/DigNetwork.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,53 @@ export class DigNetwork {
157157
await digNetwork.downloadFiles(true);
158158
}
159159

160+
public static async findPeerWithStoreKey(
161+
storeId: string,
162+
rootHash: string,
163+
key: string
164+
): Promise<string | null> {
165+
const peerBlackList: string[] = [];
166+
const serverCoin = new ServerCoin(storeId);
167+
168+
while (true) {
169+
try {
170+
const digPeers = await serverCoin.sampleCurrentEpoch(1, peerBlackList);
171+
if (digPeers.length === 0) break;
172+
173+
const peerIp = digPeers[0];
174+
const digPeer = new DigPeer(peerIp, storeId);
175+
const storeResponse = await digPeer.contentServer.headStore({
176+
hasRootHash: rootHash,
177+
});
178+
179+
if (
180+
storeResponse.success &&
181+
storeResponse.headers?.["x-has-rootHash"] === "true"
182+
) {
183+
if (!key) return peerIp;
184+
185+
const keyResponse = await digPeer.contentServer.headKey(key);
186+
if (
187+
keyResponse.success &&
188+
keyResponse.headers?.["x-key-exists"] === "true"
189+
) {
190+
return peerIp;
191+
}
192+
}
193+
194+
peerBlackList.push(peerIp);
195+
} catch (error) {
196+
console.error(
197+
"Error while sampling the epoch or contacting peer:",
198+
error
199+
);
200+
break;
201+
}
202+
}
203+
204+
return null;
205+
}
206+
160207
public static unsubscribeFromStore(storeId: string): void {
161208
fs.rmdirSync(path.join(DIG_FOLDER_PATH, "stores", storeId), {
162209
recursive: true,

0 commit comments

Comments
 (0)