From 7be77662a576eab1bac2c107903032f4f5a811f8 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 19 Sep 2024 17:07:59 -0400 Subject: [PATCH 1/3] chore(release): 0.0.1-alpha.40 --- CHANGELOG.md | 2 ++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a60370..a84748f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ 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.40](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.39...v0.0.1-alpha.40) (2024-09-19) + ### [0.0.1-alpha.39](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.38...v0.0.1-alpha.39) (2024-09-19) diff --git a/package-lock.json b/package-lock.json index 8c05870..bec400a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.39", + "version": "0.0.1-alpha.40", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.39", + "version": "0.0.1-alpha.40", "license": "ISC", "dependencies": { "@dignetwork/datalayer-driver": "^0.1.24", diff --git a/package.json b/package.json index b4d5e6f..a2ecf88 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.39", + "version": "0.0.1-alpha.40", "description": "", "type": "commonjs", "main": "./dist/index.js", From 8d6d509f9f2071d9ad4004f083e208b67c34aea2 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 19 Sep 2024 17:40:56 -0400 Subject: [PATCH 2/3] fix: findPeerWithStoreKey --- src/DigNetwork/DigNetwork.ts | 76 ++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/src/DigNetwork/DigNetwork.ts b/src/DigNetwork/DigNetwork.ts index 5958fd2..496f648 100644 --- a/src/DigNetwork/DigNetwork.ts +++ b/src/DigNetwork/DigNetwork.ts @@ -7,6 +7,10 @@ import { getFilePathFromSha256 } from "../utils/hashUtils"; import { DataStore, ServerCoin } from "../blockchain"; import { DIG_FOLDER_PATH } from "../utils/config"; import { RootHistoryItem } from "../types"; +import { promisify } from 'util'; + +const rename = promisify(fs.rename); +const unlink = promisify(fs.unlink); export class DigNetwork { private dataStore: DataStore; @@ -224,7 +228,7 @@ export class DigNetwork { ); } - await this.downloadHeightFile(forceDownload); + await this.downloadHeightFile(true); const rootHistorySorted = rootHistory .filter((item) => item.timestamp !== undefined) @@ -265,7 +269,7 @@ export class DigNetwork { `Downloading file with sha256: ${file.sha256}...` ); await this.downloadFileFromPeers( - file.sha256.match(/.{1,2}/g)!.join("/"), + `data/${file.sha256.match(/.{1,2}/g)!.join("/")}`, filePath, forceDownload ); @@ -314,77 +318,81 @@ export class DigNetwork { forceDownload ); } - + private async downloadFileFromPeers( dataPath: string, filePath: string, overwrite: boolean ): Promise { let digPeers = await this.fetchAvailablePeers(); - + const tempFilePath = `${filePath}.tmp`; + while (true) { if (!overwrite && fs.existsSync(filePath)) return; - + const blacklist = this.peerBlacklist.get(dataPath) || new Set(); - + for (const digPeer of digPeers) { if (blacklist.has(digPeer.IpAddress)) continue; - + try { // Create directory if it doesn't exist - const directory = path.dirname(filePath); + const directory = path.dirname(tempFilePath); if (!fs.existsSync(directory)) { fs.mkdirSync(directory, { recursive: true }); } - - // Stream the file data directly to the file system - const fileStream = fs.createWriteStream(filePath); - + + // Stream the file data to a temporary file + const fileStream = fs.createWriteStream(tempFilePath); + // Start streaming the data from the peer - const peerStream = await digPeer.propagationServer.streamStoreData( - dataPath - ); - - // Pipe the peer stream directly to the file system + const peerStream = await digPeer.propagationServer.streamStoreData(dataPath); + + // Pipe the peer stream to the temp file await new Promise((resolve, reject) => { peerStream.pipe(fileStream); - - peerStream.on("end", resolve); - peerStream.on("error", reject); - fileStream.on("error", reject); + + peerStream.on('end', resolve); + peerStream.on('error', reject); + fileStream.on('error', reject); }); - - if (process.env.DIG_DEBUG === "1") { + + // Rename the temp file to the final file path after successful download + await rename(tempFilePath, filePath); + + if (process.env.DIG_DEBUG === '1') { console.log(`Downloaded ${dataPath} from ${digPeer.IpAddress}`); } - + return; // Exit the method if download succeeds } catch (error) { console.warn( `Failed to download ${dataPath} from ${digPeer.IpAddress}, blacklisting peer and trying next...` ); blacklist.add(digPeer.IpAddress); + + // Clean up the temp file in case of failure + if (fs.existsSync(tempFilePath)) { + await unlink(tempFilePath); + } } } - + this.peerBlacklist.set(dataPath, blacklist); - + if (blacklist.size >= digPeers.length) { - if (process.env.DIG_DEBUG === "1") { - console.warn( - `All peers blacklisted for ${dataPath}. Refreshing peers...` - ); + if (process.env.DIG_DEBUG === '1') { + console.warn(`All peers blacklisted for ${dataPath}. Refreshing peers...`); } - + digPeers = await this.fetchAvailablePeers(); if (!digPeers.length) { - throw new Error( - `Failed to download ${dataPath}: no peers available.` - ); + throw new Error(`Failed to download ${dataPath}: no peers available.`); } } } } + private async runProgressBar( total: number, From b335eca3779387b884758ef79a55598c19b4d513 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 19 Sep 2024 17:41:33 -0400 Subject: [PATCH 3/3] chore(release): 0.0.1-alpha.41 --- 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 a84748f..f238b46 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.41](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.40...v0.0.1-alpha.41) (2024-09-19) + + +### Bug Fixes + +* findPeerWithStoreKey ([8d6d509](https://github.com/DIG-Network/dig-chia-sdk/commit/8d6d509f9f2071d9ad4004f083e208b67c34aea2)) + ### [0.0.1-alpha.40](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.39...v0.0.1-alpha.40) (2024-09-19) ### [0.0.1-alpha.39](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.38...v0.0.1-alpha.39) (2024-09-19) diff --git a/package-lock.json b/package-lock.json index bec400a..80ddaaa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.40", + "version": "0.0.1-alpha.41", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.40", + "version": "0.0.1-alpha.41", "license": "ISC", "dependencies": { "@dignetwork/datalayer-driver": "^0.1.24", diff --git a/package.json b/package.json index a2ecf88..bbf1267 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.40", + "version": "0.0.1-alpha.41", "description": "", "type": "commonjs", "main": "./dist/index.js",