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.32 #30

Merged
merged 4 commits into from
Sep 19, 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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

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.32](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.31...v0.0.1-alpha.32) (2024-09-19)


### Features

* improved download file sync ([feec923](https://github.com/DIG-Network/dig-chia-sdk/commit/feec9239004eaa9d2fa8e5d9b0c55ac056bd7e77))

### [0.0.1-alpha.31](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.30...v0.0.1-alpha.31) (2024-09-18)

### [0.0.1-alpha.30](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.29...v0.0.1-alpha.30) (2024-09-18)


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.30",
"version": "0.0.1-alpha.32",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
104 changes: 43 additions & 61 deletions src/DigNetwork/DigNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getDeltaFiles } from "../utils/deltaUtils";
import { getFilePathFromSha256 } from "../utils/hashUtils";
import { DataStore, ServerCoin } from "../blockchain";
import { DIG_FOLDER_PATH } from "../utils/config";
import { RootHistoryItem } from "../types";

export class DigNetwork {
private dataStore: DataStore;
Expand Down Expand Up @@ -160,9 +161,10 @@ export class DigNetwork {
public static async findPeerWithStoreKey(
storeId: string,
rootHash: string,
key?: string
key?: string,
intialBlackList: string[] = []
): Promise<string | null> {
const peerBlackList: string[] = [];
const peerBlackList: string[] = intialBlackList;
const serverCoin = new ServerCoin(storeId);

while (true) {
Expand Down Expand Up @@ -218,80 +220,60 @@ export class DigNetwork {
skipData: boolean = false
): Promise<void> {
try {
const rootHistory = await this.dataStore.getRootHistory();
const rootHistory: RootHistoryItem[] =
await this.dataStore.getRootHistory();
if (!rootHistory.length)
throw new Error(
"No roots found in rootHistory. Cannot proceed with file download."
);

await this.downloadHeightFile(forceDownload);

const localManifestPath = path.join(this.storeDir, "manifest.dat");
const localManifestHashes = fs.existsSync(localManifestPath)
? fs.readFileSync(localManifestPath, "utf-8").trim().split("\n")
: [];

const progressBar = renderProgressBar
? new MultiBar(
{
clearOnComplete: false,
hideCursor: true,
format: "Syncing Store | {bar} | {percentage}%",
noTTYOutput: true,
},
Presets.shades_classic
)
: null;

const progress = progressBar
? progressBar.create(rootHistory.length, 0)
: null;
const newRootHashes: string[] = [];

for (let i = 0; i < rootHistory.length; i++) {
const { root_hash: rootHash } = rootHistory[i];
const datFilePath = path.join(this.storeDir, `${rootHash}.dat`);

await this.downloadFileFromPeers(
`${rootHash}.dat`,
datFilePath,
forceDownload
);
const rootHistorySorted = rootHistory
.filter((item) => item.timestamp !== undefined)
.sort((a, b) => (b.timestamp as number) - (a.timestamp as number));

const datFileContent = JSON.parse(
fs.readFileSync(datFilePath, "utf-8")
// Process rootHistory sequentially
for (const rootInfo of rootHistorySorted) {
const peerIp = await DigNetwork.findPeerWithStoreKey(
this.dataStore.StoreId,
rootInfo.root_hash
);
if (datFileContent.root !== rootHash)
throw new Error("Root hash mismatch");

if (!skipData) {
for (const file of Object.keys(datFileContent.files)) {
const filePath = getFilePathFromSha256(
datFileContent.files[file].sha256,
path.join(this.storeDir, "data")
);
const isInDataDir = filePath.startsWith(
path.join(this.storeDir, "data")
);
await this.downloadFileFromPeers(
getFilePathFromSha256(datFileContent.files[file].sha256, "data"),
filePath,
forceDownload || !isInDataDir
);
}
if (!peerIp) {
console.error(
`No peer found with root hash ${rootInfo.root_hash}. Skipping download.`
);
continue; // Skip to the next rootInfo
}

if (localManifestHashes[i] !== rootHash) newRootHashes.push(rootHash);
const digPeer = new DigPeer(peerIp, this.dataStore.StoreId);
const rootResponse = await digPeer.propagationServer.getStoreData(
`${rootInfo.root_hash}.dat`
);
const root = JSON.parse(rootResponse);

progress?.increment();
if (!skipData) {
await Promise.all(
root.files.map(async (file: any) => {
const filePath = getFilePathFromSha256(
file.sha256,
this.storeDir
);
if (!fs.existsSync(filePath) || forceDownload) {
console.log(`Downloading ${file.sha256}...`);
await this.downloadFileFromPeers(
file.sha256.match(/.{1,2}/g)!.join("/"),
filePath,
forceDownload
);
}
})
);
}
}

if (newRootHashes.length)
fs.appendFileSync(localManifestPath, newRootHashes.join("\n") + "\n");

await this.downloadManifestFile(forceDownload);

progressBar?.stop();
await this.downloadManifestFile(true);

console.log("Syncing store complete.");
} catch (error: any) {
Expand Down
2 changes: 1 addition & 1 deletion src/DigNetwork/DigPeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export class DigPeer {
}

// Define the seed
const seed = "dig";
const seed = "digpayment";

// Combine the seed and the original buffer
const combinedBuffer = Buffer.concat([Buffer.from(seed), storeId]);
Expand Down
Loading