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.131 #120

Merged
merged 2 commits into from
Oct 4, 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.131](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.130...v0.0.1-alpha.131) (2024-10-04)


### Features

* speed up findpeerwith key ([9de9394](https://github.com/DIG-Network/dig-chia-sdk/commit/9de93949d0a8d9660c17f41e3e7de64238fed4ac))

### [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)


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.130",
"version": "0.0.1-alpha.131",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
101 changes: 61 additions & 40 deletions src/DigNetwork/DigNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,63 +38,78 @@ export class DigNetwork {
storeId: string,
rootHash: string,
key?: string,
intialBlackList: string[] = []
initialBlackList: string[] = []
): Promise<DigPeer | null> {
const peerBlackList: string[] = intialBlackList;
const peerBlackList: string[] = initialBlackList;
const serverCoin = new ServerCoin(storeId);
let peerIp: string | null = null;

// Keep sampling peers until an empty array is returned
while (true) {
try {
// Sample a peer from the current epoch
const digPeers = await serverCoin.sampleCurrentEpoch(1, peerBlackList);
// Sample 10 peers from the current epoch
const digPeers = await serverCoin.sampleCurrentEpoch(10, peerBlackList);

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

peerIp = digPeers[0];
const digPeer = new DigPeer(peerIp, storeId);

// Try to fetch the head store information
const { storeExists, rootHashExists } =
await digPeer.propagationServer.checkStoreExists(rootHash);
// Create a race of promises for all peers
const peerPromises = digPeers.map((peerIp) => {
return new Promise<DigPeer | null>(async (resolve) => {
try {
const digPeer = new DigPeer(peerIp, storeId);
const { storeExists, rootHashExists } =
await digPeer.propagationServer.checkStoreExists(rootHash);

// Check if the store and root hash exist on the peer
if (storeExists && rootHashExists) {
console.log(
`Found Peer at ${peerIp} for storeId: ${storeId}, root hash ${rootHash}`
);

// If no key is provided, resolve the peer
if (!key) {
return resolve(digPeer);
}

// If key is provided, check if the peer has it
const keyResponse = await digPeer.contentServer.headKey(
key,
rootHash
);
if (keyResponse.headers?.["x-key-exists"] === "true") {
return resolve(digPeer);
}
}
} catch (error) {
console.error(`Error connecting to DIG Peer ${peerIp}.`);
}

// If the peer has the correct root hash, check if key is required
if (storeExists && rootHashExists) {
console.log(
`Found Peer at ${peerIp} for storeId: ${storeId}, root hash ${rootHash}`
);
// If the peer does not meet the criteria, resolve with null
resolve(null);
});
});

// If no key is provided, return the peer
if (!key) {
return digPeer;
}
// Wait for the first valid peer that resolves
const firstValidPeer = await Promise.race(peerPromises);

// If key is provided, check if the peer has it
const keyResponse = await digPeer.contentServer.headKey(
key,
rootHash
);
if (keyResponse.headers?.["x-key-exists"] === "true") {
return digPeer;
}
// If a valid peer is found, return it
if (firstValidPeer) {
return firstValidPeer;
}

// Add peer to blacklist if it doesn't meet criteria
peerBlackList.push(peerIp);
// If none of the peers were valid, add them to the blacklist
digPeers.forEach((peerIp) => peerBlackList.push(peerIp));

// Retry with the next set of peers
console.log("No valid peers found, retrying with new peers...");
} catch (error) {
console.error(`Error connecting to DIG Peer ${peerIp}. Resampling...`);
if (peerIp) {
peerBlackList.push(peerIp); // Add to blacklist if error occurs
}
console.error("Error sampling peers. Resampling...");
}
}

// Return null if no valid peer was found
// Return null if no valid peer was found after all attempts
return null;
}

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

public static async pingNetworkOfUpdate(storeId: string, rootHash: string): Promise<void> {
public static async pingNetworkOfUpdate(
storeId: string,
rootHash: string
): Promise<void> {
const serverCoin = new ServerCoin(storeId);
// When an update is made, ping 10 network peers to pull updates from this store
const digPeers = await serverCoin.sampleCurrentEpoch(10);
Expand All @@ -115,7 +133,7 @@ export class DigNetwork {
digPeer.propagationServer.pingUpdate(rootHash),
5000,
`headKey timed out for peer ${digPeer.IpAddress}`
)
);
}
}

Expand Down Expand Up @@ -213,8 +231,11 @@ export class DigNetwork {
}
}
}

DigNetwork.pingNetworkOfUpdate(this.dataStore.StoreId, rootInfo.root_hash);

DigNetwork.pingNetworkOfUpdate(
this.dataStore.StoreId,
rootInfo.root_hash
);
}

console.log("Syncing store complete.");
Expand Down
Loading