Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/v0.0.1 alpha.20 #20

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/DigNetwork/ContentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}> {
Expand Down
47 changes: 47 additions & 0 deletions src/DigNetwork/DigNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,53 @@ export class DigNetwork {
await digNetwork.downloadFiles(true);
}

public static async findPeerWithStoreKey(
storeId: string,
rootHash: string,
key: string
): Promise<string | null> {
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,
Expand Down
Loading