@dignetwork/dig-sdk 0.0.1-alpha.30 → 0.0.1-alpha.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -9,7 +9,7 @@ export declare class DigNetwork {
|
|
|
9
9
|
uploadStoreHead(digPeer: DigPeer): Promise<void>;
|
|
10
10
|
uploadStore(digPeer: DigPeer): Promise<void>;
|
|
11
11
|
static subscribeToStore(storeId: string): Promise<void>;
|
|
12
|
-
static findPeerWithStoreKey(storeId: string, rootHash: string, key?: string): Promise<string | null>;
|
|
12
|
+
static findPeerWithStoreKey(storeId: string, rootHash: string, key?: string, intialBlackList?: string[]): Promise<string | null>;
|
|
13
13
|
static unsubscribeFromStore(storeId: string): void;
|
|
14
14
|
downloadFiles(forceDownload?: boolean, renderProgressBar?: boolean, skipData?: boolean): Promise<void>;
|
|
15
15
|
private fetchAvailablePeers;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAOpC,qBAAa,UAAU;IACrB,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,aAAa,CAA2B;gBAEpC,OAAO,EAAE,MAAM;YAOb,eAAe;IA8ChB,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAsDhD,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;WA6BrC,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WAQhD,oBAAoB,CACtC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,MAAM,EACZ,eAAe,GAAE,MAAM,EAAO,GAC7B,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;WA2CX,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAQ5C,aAAa,CACxB,aAAa,GAAE,OAAe,EAC9B,iBAAiB,GAAE,OAAc,EACjC,QAAQ,GAAE,OAAe,GACxB,OAAO,CAAC,IAAI,CAAC;YAiEF,mBAAmB;YAWnB,kBAAkB;YASlB,oBAAoB;YASpB,qBAAqB;YAuErB,cAAc;CAoB7B"}
|
|
@@ -122,8 +122,8 @@ class DigNetwork {
|
|
|
122
122
|
const digNetwork = new DigNetwork(storeId);
|
|
123
123
|
await digNetwork.downloadFiles(true);
|
|
124
124
|
}
|
|
125
|
-
static async findPeerWithStoreKey(storeId, rootHash, key) {
|
|
126
|
-
const peerBlackList =
|
|
125
|
+
static async findPeerWithStoreKey(storeId, rootHash, key, intialBlackList = []) {
|
|
126
|
+
const peerBlackList = intialBlackList;
|
|
127
127
|
const serverCoin = new blockchain_1.ServerCoin(storeId);
|
|
128
128
|
while (true) {
|
|
129
129
|
try {
|
|
@@ -167,44 +167,30 @@ class DigNetwork {
|
|
|
167
167
|
if (!rootHistory.length)
|
|
168
168
|
throw new Error("No roots found in rootHistory. Cannot proceed with file download.");
|
|
169
169
|
await this.downloadHeightFile(forceDownload);
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
? progressBar.create(rootHistory.length, 0)
|
|
184
|
-
: null;
|
|
185
|
-
const newRootHashes = [];
|
|
186
|
-
for (let i = 0; i < rootHistory.length; i++) {
|
|
187
|
-
const { root_hash: rootHash } = rootHistory[i];
|
|
188
|
-
const datFilePath = path.join(this.storeDir, `${rootHash}.dat`);
|
|
189
|
-
await this.downloadFileFromPeers(`${rootHash}.dat`, datFilePath, forceDownload);
|
|
190
|
-
const datFileContent = JSON.parse(fs.readFileSync(datFilePath, "utf-8"));
|
|
191
|
-
if (datFileContent.root !== rootHash)
|
|
192
|
-
throw new Error("Root hash mismatch");
|
|
170
|
+
const rootHistorySorted = rootHistory
|
|
171
|
+
.filter((item) => item.timestamp !== undefined)
|
|
172
|
+
.sort((a, b) => b.timestamp - a.timestamp);
|
|
173
|
+
// Process rootHistory sequentially
|
|
174
|
+
for (const rootInfo of rootHistorySorted) {
|
|
175
|
+
const peerIp = await DigNetwork.findPeerWithStoreKey(this.dataStore.StoreId, rootInfo.root_hash);
|
|
176
|
+
if (!peerIp) {
|
|
177
|
+
console.error(`No peer found with root hash ${rootInfo.root_hash}. Skipping download.`);
|
|
178
|
+
continue; // Skip to the next rootInfo
|
|
179
|
+
}
|
|
180
|
+
const digPeer = new DigPeer_1.DigPeer(peerIp, this.dataStore.StoreId);
|
|
181
|
+
const rootResponse = await digPeer.propagationServer.getStoreData(`${rootInfo.root_hash}.dat`);
|
|
182
|
+
const root = JSON.parse(rootResponse);
|
|
193
183
|
if (!skipData) {
|
|
194
|
-
|
|
195
|
-
const filePath = (0, hashUtils_1.getFilePathFromSha256)(
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
184
|
+
await Promise.all(root.files.map(async (file) => {
|
|
185
|
+
const filePath = (0, hashUtils_1.getFilePathFromSha256)(file.sha256, this.storeDir);
|
|
186
|
+
if (!fs.existsSync(filePath) || forceDownload) {
|
|
187
|
+
console.log(`Downloading ${file.sha256}...`);
|
|
188
|
+
await this.downloadFileFromPeers(file.sha256.match(/.{1,2}/g).join("/"), filePath, forceDownload);
|
|
189
|
+
}
|
|
190
|
+
}));
|
|
199
191
|
}
|
|
200
|
-
if (localManifestHashes[i] !== rootHash)
|
|
201
|
-
newRootHashes.push(rootHash);
|
|
202
|
-
progress?.increment();
|
|
203
192
|
}
|
|
204
|
-
|
|
205
|
-
fs.appendFileSync(localManifestPath, newRootHashes.join("\n") + "\n");
|
|
206
|
-
await this.downloadManifestFile(forceDownload);
|
|
207
|
-
progressBar?.stop();
|
|
193
|
+
await this.downloadManifestFile(true);
|
|
208
194
|
console.log("Syncing store complete.");
|
|
209
195
|
}
|
|
210
196
|
catch (error) {
|
|
@@ -185,7 +185,7 @@ class DigPeer {
|
|
|
185
185
|
throw new Error("Invalid input. Must be a 32-byte buffer.");
|
|
186
186
|
}
|
|
187
187
|
// Define the seed
|
|
188
|
-
const seed = "
|
|
188
|
+
const seed = "digpayment";
|
|
189
189
|
// Combine the seed and the original buffer
|
|
190
190
|
const combinedBuffer = Buffer.concat([Buffer.from(seed), storeId]);
|
|
191
191
|
// Apply SHA-256 hash to the combined buffer
|