From 15cf0af395ccc9009a6e9ff898c6f5d21ba4d92a Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 16 Sep 2024 18:51:54 -0400 Subject: [PATCH 1/2] feat: find any peer on the network with the store and or key --- src/DigNetwork/ContentServer.ts | 2 +- src/DigNetwork/DigNetwork.ts | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/DigNetwork/ContentServer.ts b/src/DigNetwork/ContentServer.ts index 3182b51..07d8fc2 100644 --- a/src/DigNetwork/ContentServer.ts +++ b/src/DigNetwork/ContentServer.ts @@ -83,7 +83,7 @@ export class ContentServer { } // Method to check if a specific store exists (HEAD request) - public async headStore(options?: { hasRootHash: boolean}): Promise<{ + public async headStore(options?: { hasRootHash: string}): Promise<{ success: boolean; headers?: http.IncomingHttpHeaders; }> { diff --git a/src/DigNetwork/DigNetwork.ts b/src/DigNetwork/DigNetwork.ts index 92ffff8..2b90501 100644 --- a/src/DigNetwork/DigNetwork.ts +++ b/src/DigNetwork/DigNetwork.ts @@ -157,6 +157,53 @@ export class DigNetwork { await digNetwork.downloadFiles(true); } + public static async findPeerWithStoreKey( + storeId: string, + rootHash: string, + key: string + ): Promise { + const peerBlackList: string[] = []; + const serverCoin = new ServerCoin(storeId); + + while (true) { + try { + const digPeers = await serverCoin.sampleCurrentEpoch(1, peerBlackList); + if (digPeers.length === 0) break; + + const peerIp = digPeers[0]; + const digPeer = new DigPeer(peerIp, storeId); + const storeResponse = await digPeer.contentServer.headStore({ + hasRootHash: rootHash, + }); + + if ( + storeResponse.success && + storeResponse.headers?.["x-has-rootHash"] === "true" + ) { + if (!key) return peerIp; + + const keyResponse = await digPeer.contentServer.headKey(key); + if ( + keyResponse.success && + keyResponse.headers?.["x-key-exists"] === "true" + ) { + return peerIp; + } + } + + peerBlackList.push(peerIp); + } catch (error) { + console.error( + "Error while sampling the epoch or contacting peer:", + error + ); + break; + } + } + + return null; + } + public static unsubscribeFromStore(storeId: string): void { fs.rmdirSync(path.join(DIG_FOLDER_PATH, "stores", storeId), { recursive: true, From fe44b2e7228582279456f8d8f334a4eb945570f1 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 16 Sep 2024 18:52:18 -0400 Subject: [PATCH 2/2] chore(release): 0.0.1-alpha.20 --- CHANGELOG.md | 7 +++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14c82b6..07bed38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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.20](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.19...v0.0.1-alpha.20) (2024-09-16) + + +### Features + +* find any peer on the network with the store and or key ([15cf0af](https://github.com/DIG-Network/dig-chia-sdk/commit/15cf0af395ccc9009a6e9ff898c6f5d21ba4d92a)) + ### [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) diff --git a/package-lock.json b/package-lock.json index 343056b..49fe028 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.19", + "version": "0.0.1-alpha.20", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.19", + "version": "0.0.1-alpha.20", "license": "ISC", "dependencies": { "bip39": "^3.1.0", diff --git a/package.json b/package.json index 8e1f47a..b6e86cc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.19", + "version": "0.0.1-alpha.20", "description": "", "type": "commonjs", "main": "./dist/index.js",