Skip to content

Commit

Permalink
Merge pull request #117 from DIG-Network/release/v0.0.1-alpha.128
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.128
  • Loading branch information
MichaelTaylor3D authored Oct 4, 2024
2 parents 08ac85e + eb95f5e commit 1554afd
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

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.128](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.127...v0.0.1-alpha.128) (2024-10-04)


### Features

* add ping network of update logic ([e210be2](https://github.com/DIG-Network/dig-chia-sdk/commit/e210be2d86a824003d756b8fbd30602d75250651))
* add ping network of update logic ([dbff9bf](https://github.com/DIG-Network/dig-chia-sdk/commit/dbff9bfc7b416dcaf5ede8fed4d06a148c98572e))

### [0.0.1-alpha.127](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.126...v0.0.1-alpha.127) (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.127",
"version": "0.0.1-alpha.128",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
31 changes: 24 additions & 7 deletions src/DigNetwork/DigNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,20 @@ export class DigNetwork {
fs.unlinkSync(path.join(DIG_FOLDER_PATH, "stores", storeId + ".json"));
}

public async syncStoreFromPeers(maxRootsToProcess?: number): 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);
for (const peer of digPeers) {
const digPeer = new DigPeer(peer, storeId);
await digPeer.propagationServer.pingUpdate(rootHash);
}
}

public async syncStoreFromPeers(
prioritizedPeer?: DigPeer,
maxRootsToProcess?: number
): Promise<void> {
console.log("Starting file download process...");
let peerBlackList: string[] = [];

Expand Down Expand Up @@ -145,12 +158,16 @@ export class DigNetwork {
while (true) {
try {
// Find a peer with the store and root hash
selectedPeer = await DigNetwork.findPeerWithStoreKey(
this.dataStore.StoreId,
rootInfo.root_hash,
undefined,
peerBlackList
);
if (prioritizedPeer) {
selectedPeer = prioritizedPeer;
} else {
selectedPeer = await DigNetwork.findPeerWithStoreKey(
this.dataStore.StoreId,
rootInfo.root_hash,
undefined,
peerBlackList
);
}

if (!selectedPeer) {
console.error(
Expand Down
53 changes: 52 additions & 1 deletion src/DigNetwork/PropagationServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,53 @@ export class PropagationServer {
});
}

/**
* Ping the current peer about an update to the store, passing rootHash.
* @param rootHash - The root hash for the store update.
*/
async pingUpdate(rootHash: string): Promise<void> {
const spinner = createSpinner(`Pinging peer ${this.ipAddress}...`).start();

try {
const httpsAgent = this.createHttpsAgent();
const url = `https://${formatHost(this.ipAddress)}:${
PropagationServer.port
}/update`;

const config: AxiosRequestConfig = {
httpsAgent,
headers: {
"Content-Type": "application/json",
},
};

// Data to send in the request (storeId and rootHash)
const data = {
storeId: this.storeId,
rootHash: rootHash,
updateTime: new Date().toISOString(),
};

try {
const response = await axios.post(url, data, config);
console.log(green(`✔ Successfully pinged peer: ${this.ipAddress}`));
spinner.success({
text: green(
`✔ Successfully pinged peer ${this.ipAddress} with rootHash ${rootHash}`
),
});
return response.data;
} catch (error: any) {
console.error(red(`✖ Failed to ping peer: ${this.ipAddress}`));
console.error(red(error.message));
throw error;
}
} catch (error: any) {
spinner.error({ text: red("✖ Error pinging network peer.") });
console.error(red(error.message));
}
}

/**
* Adds a custom inactivity timeout for large file transfers.
*/
Expand Down Expand Up @@ -253,7 +300,11 @@ export class PropagationServer {
* Upload a file to the server by sending a PUT request.
* Logs progress using a local cli-progress bar.
*/
async uploadFile(label: string, dataPath: string, uncompress: boolean = false) {
async uploadFile(
label: string,
dataPath: string,
uncompress: boolean = false
) {
const filePath = path.join(STORE_PATH, this.storeId, dataPath);

const { nonce, fileExists } = await this.getFileNonce(dataPath);
Expand Down

0 comments on commit 1554afd

Please sign in to comment.