Skip to content

Commit 1554afd

Browse files
Merge pull request #117 from DIG-Network/release/v0.0.1-alpha.128
Release/v0.0.1 alpha.128
2 parents 08ac85e + eb95f5e commit 1554afd

File tree

5 files changed

+87
-11
lines changed

5 files changed

+87
-11
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
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.128](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.127...v0.0.1-alpha.128) (2024-10-04)
6+
7+
8+
### Features
9+
10+
* add ping network of update logic ([e210be2](https://github.com/DIG-Network/dig-chia-sdk/commit/e210be2d86a824003d756b8fbd30602d75250651))
11+
* add ping network of update logic ([dbff9bf](https://github.com/DIG-Network/dig-chia-sdk/commit/dbff9bfc7b416dcaf5ede8fed4d06a148c98572e))
12+
513
### [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)
614

715

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.127",
3+
"version": "0.0.1-alpha.128",
44
"description": "",
55
"type": "commonjs",
66
"main": "./dist/index.js",

src/DigNetwork/DigNetwork.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,20 @@ export class DigNetwork {
105105
fs.unlinkSync(path.join(DIG_FOLDER_PATH, "stores", storeId + ".json"));
106106
}
107107

108-
public async syncStoreFromPeers(maxRootsToProcess?: number): Promise<void> {
108+
public static async pingNetworkOfUpdate(storeId: string, rootHash: string): Promise<void> {
109+
const serverCoin = new ServerCoin(storeId);
110+
// When an update is made, ping 10 network peers to pull updates from this store
111+
const digPeers = await serverCoin.sampleCurrentEpoch(10);
112+
for (const peer of digPeers) {
113+
const digPeer = new DigPeer(peer, storeId);
114+
await digPeer.propagationServer.pingUpdate(rootHash);
115+
}
116+
}
117+
118+
public async syncStoreFromPeers(
119+
prioritizedPeer?: DigPeer,
120+
maxRootsToProcess?: number
121+
): Promise<void> {
109122
console.log("Starting file download process...");
110123
let peerBlackList: string[] = [];
111124

@@ -145,12 +158,16 @@ export class DigNetwork {
145158
while (true) {
146159
try {
147160
// Find a peer with the store and root hash
148-
selectedPeer = await DigNetwork.findPeerWithStoreKey(
149-
this.dataStore.StoreId,
150-
rootInfo.root_hash,
151-
undefined,
152-
peerBlackList
153-
);
161+
if (prioritizedPeer) {
162+
selectedPeer = prioritizedPeer;
163+
} else {
164+
selectedPeer = await DigNetwork.findPeerWithStoreKey(
165+
this.dataStore.StoreId,
166+
rootInfo.root_hash,
167+
undefined,
168+
peerBlackList
169+
);
170+
}
154171

155172
if (!selectedPeer) {
156173
console.error(

src/DigNetwork/PropagationServer.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,53 @@ export class PropagationServer {
8080
});
8181
}
8282

83+
/**
84+
* Ping the current peer about an update to the store, passing rootHash.
85+
* @param rootHash - The root hash for the store update.
86+
*/
87+
async pingUpdate(rootHash: string): Promise<void> {
88+
const spinner = createSpinner(`Pinging peer ${this.ipAddress}...`).start();
89+
90+
try {
91+
const httpsAgent = this.createHttpsAgent();
92+
const url = `https://${formatHost(this.ipAddress)}:${
93+
PropagationServer.port
94+
}/update`;
95+
96+
const config: AxiosRequestConfig = {
97+
httpsAgent,
98+
headers: {
99+
"Content-Type": "application/json",
100+
},
101+
};
102+
103+
// Data to send in the request (storeId and rootHash)
104+
const data = {
105+
storeId: this.storeId,
106+
rootHash: rootHash,
107+
updateTime: new Date().toISOString(),
108+
};
109+
110+
try {
111+
const response = await axios.post(url, data, config);
112+
console.log(green(`✔ Successfully pinged peer: ${this.ipAddress}`));
113+
spinner.success({
114+
text: green(
115+
`✔ Successfully pinged peer ${this.ipAddress} with rootHash ${rootHash}`
116+
),
117+
});
118+
return response.data;
119+
} catch (error: any) {
120+
console.error(red(`✖ Failed to ping peer: ${this.ipAddress}`));
121+
console.error(red(error.message));
122+
throw error;
123+
}
124+
} catch (error: any) {
125+
spinner.error({ text: red("✖ Error pinging network peer.") });
126+
console.error(red(error.message));
127+
}
128+
}
129+
83130
/**
84131
* Adds a custom inactivity timeout for large file transfers.
85132
*/
@@ -253,7 +300,11 @@ export class PropagationServer {
253300
* Upload a file to the server by sending a PUT request.
254301
* Logs progress using a local cli-progress bar.
255302
*/
256-
async uploadFile(label: string, dataPath: string, uncompress: boolean = false) {
303+
async uploadFile(
304+
label: string,
305+
dataPath: string,
306+
uncompress: boolean = false
307+
) {
257308
const filePath = path.join(STORE_PATH, this.storeId, dataPath);
258309

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

0 commit comments

Comments
 (0)