@dignetwork/dig-sdk 0.0.1-alpha.3 → 0.0.1-alpha.4

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.
@@ -6,10 +6,11 @@ export declare class DigNetwork {
6
6
  private peerBlacklist;
7
7
  constructor(storeId: string);
8
8
  private uploadPreflight;
9
+ uploadStoreHead(digPeer: DigPeer): Promise<void>;
9
10
  uploadStore(digPeer: DigPeer): Promise<void>;
10
11
  static subscribeToStore(storeId: string): Promise<void>;
11
12
  static unsubscribeFromStore(storeId: string): void;
12
- downloadFiles(forceDownload?: boolean, renderProgressBar?: boolean): Promise<void>;
13
+ downloadFiles(forceDownload?: boolean, renderProgressBar?: boolean, skipData?: boolean): Promise<void>;
13
14
  private fetchAvailablePeers;
14
15
  private downloadHeightFile;
15
16
  private downloadManifestFile;
@@ -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;AAMpC,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;IA+ChB,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;WAgCrC,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WAQtD,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM5C,aAAa,CACxB,aAAa,GAAE,OAAe,EAC9B,iBAAiB,GAAE,OAAc,GAChC,OAAO,CAAC,IAAI,CAAC;YAmFF,mBAAmB;YAWnB,kBAAkB;YASlB,oBAAoB;YASpB,qBAAqB;YAuErB,cAAc;CAkB7B"}
1
+ {"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAMpC,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;WA+BrC,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WAQtD,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;YAqFF,mBAAmB;YAWnB,kBAAkB;YASlB,oBAAoB;YASpB,qBAAqB;YAuErB,cAAc;CAuB7B"}
@@ -63,6 +63,40 @@ class DigNetwork {
63
63
  }
64
64
  return { generationIndex, lastLocalRootHash };
65
65
  }
66
+ async uploadStoreHead(digPeer) {
67
+ // First make sure that the remote store is up to date.
68
+ const rootHistory = await this.dataStore.getRootHistory();
69
+ const localManifestHashes = await this.dataStore.getManifestHashes();
70
+ const remoteManifestFile = await digPeer.propagationServer.getStoreData("manifest.dat");
71
+ const remoteManifestHashes = remoteManifestFile.split("\n").filter(Boolean);
72
+ const onChainRootHashes = rootHistory.map((root) => root.root_hash);
73
+ // Check that remote manifest is one behind on-chain root hashes
74
+ if (remoteManifestHashes.length !== onChainRootHashes.length - 1) {
75
+ throw new Error("Remote manifest should be one behind the on-chain root. Cannot push head.");
76
+ }
77
+ // Compare each remote manifest hash with the corresponding on-chain root hash
78
+ for (let i = 0; i < remoteManifestHashes.length; i++) {
79
+ if (remoteManifestHashes[i] !== onChainRootHashes[i]) {
80
+ throw new Error(`Remote manifest does not match on-chain root at index ${i}. Cannot push head.`);
81
+ }
82
+ }
83
+ // Get the files for the latest local manifest hash
84
+ const filesToUpload = await this.dataStore.getFileSetForRootHash(localManifestHashes[localManifestHashes.length - 1]);
85
+ if (!filesToUpload.length) {
86
+ console.log("No files to upload.");
87
+ return;
88
+ }
89
+ // Upload files to the remote peer with a progress bar
90
+ await this.runProgressBar(filesToUpload.length, "Store Data", async (progress) => {
91
+ for (const filePath of filesToUpload) {
92
+ const relativePath = path
93
+ .relative(this.storeDir, filePath)
94
+ .replace(/\\/g, "/");
95
+ await digPeer.propagationServer.pushFile(filePath, relativePath);
96
+ progress.increment();
97
+ }
98
+ });
99
+ }
66
100
  // Uploads the store to a specific peer
67
101
  async uploadStore(digPeer) {
68
102
  const { generationIndex } = await this.uploadPreflight(digPeer);
@@ -74,7 +108,6 @@ class DigNetwork {
74
108
  }
75
109
  await this.runProgressBar(filesToUpload.length, "Store Data", async (progress) => {
76
110
  for (const filePath of filesToUpload) {
77
- console.log(`Uploading ${filePath}...`);
78
111
  const relativePath = path
79
112
  .relative(this.storeDir, filePath)
80
113
  .replace(/\\/g, "/");
@@ -91,11 +124,13 @@ class DigNetwork {
91
124
  await digNetwork.downloadFiles(true);
92
125
  }
93
126
  static unsubscribeFromStore(storeId) {
94
- fs.rmdirSync(path.join(config_1.DIG_FOLDER_PATH, "stores", storeId), { recursive: true });
127
+ fs.rmdirSync(path.join(config_1.DIG_FOLDER_PATH, "stores", storeId), {
128
+ recursive: true,
129
+ });
95
130
  fs.unlinkSync(path.join(config_1.DIG_FOLDER_PATH, "stores", storeId + ".json"));
96
131
  }
97
132
  // Downloads files from the network based on the manifest
98
- async downloadFiles(forceDownload = false, renderProgressBar = true) {
133
+ async downloadFiles(forceDownload = false, renderProgressBar = true, skipData = false) {
99
134
  try {
100
135
  const rootHistory = await this.dataStore.getRootHistory();
101
136
  if (!rootHistory.length)
@@ -124,10 +159,12 @@ class DigNetwork {
124
159
  const datFileContent = JSON.parse(fs.readFileSync(datFilePath, "utf-8"));
125
160
  if (datFileContent.root !== rootHash)
126
161
  throw new Error("Root hash mismatch");
127
- for (const file of Object.keys(datFileContent.files)) {
128
- const filePath = (0, hashUtils_1.getFilePathFromSha256)(datFileContent.files[file].sha256, path.join(this.storeDir, "data"));
129
- const isInDataDir = filePath.startsWith(path.join(this.storeDir, "data"));
130
- await this.downloadFileFromPeers((0, hashUtils_1.getFilePathFromSha256)(datFileContent.files[file].sha256, "data"), filePath, forceDownload || !isInDataDir);
162
+ if (!skipData) {
163
+ for (const file of Object.keys(datFileContent.files)) {
164
+ const filePath = (0, hashUtils_1.getFilePathFromSha256)(datFileContent.files[file].sha256, path.join(this.storeDir, "data"));
165
+ const isInDataDir = filePath.startsWith(path.join(this.storeDir, "data"));
166
+ await this.downloadFileFromPeers((0, hashUtils_1.getFilePathFromSha256)(datFileContent.files[file].sha256, "data"), filePath, forceDownload || !isInDataDir);
167
+ }
131
168
  }
132
169
  if (localManifestHashes[i] !== rootHash)
133
170
  newRootHashes.push(rootHash);
@@ -209,6 +246,8 @@ class DigNetwork {
209
246
  }
210
247
  async runProgressBar(total, name, task) {
211
248
  // Using 'any' to work around TypeScript issues
249
+ const oldConsoleLog = console.log;
250
+ console.log = () => { }; // Suppress console.log output
212
251
  const multiBar = new cli_progress_1.MultiBar({
213
252
  clearOnComplete: false,
214
253
  hideCursor: true,
@@ -216,7 +255,10 @@ class DigNetwork {
216
255
  noTTYOutput: true,
217
256
  }, cli_progress_1.Presets.shades_classic);
218
257
  const progress = multiBar.create(total, 0, { name });
219
- await task(progress).finally(() => multiBar.stop());
258
+ await task(progress).finally(() => {
259
+ multiBar.stop();
260
+ console.log = oldConsoleLog; // Restore console.log
261
+ });
220
262
  }
221
263
  }
222
264
  exports.DigNetwork = DigNetwork;
@@ -50,5 +50,7 @@ export declare class DataStore {
50
50
  isSynced(): Promise<boolean>;
51
51
  hasMetaWritePermissions(publicSyntheticKey?: Buffer): Promise<boolean>;
52
52
  updateMetadata(metadata: DataStoreMetadata): Promise<DataStoreDriver>;
53
+ getFileSetForRootHash(rootHash: string): Promise<string[]>;
54
+ getManifestHashes(): string[];
53
55
  }
54
56
  //# sourceMappingURL=DataStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"DataStore.d.ts","sourceRoot":"","sources":["../../src/blockchain/DataStore.ts"],"names":[],"mappings":"AAEA,OAAO,EAOL,SAAS,IAAI,eAAe,EAE5B,iBAAiB,EAIlB,MAAM,kBAAkB,CAAC;AAY1B,OAAO,EAAE,eAAe,EAAW,MAAM,UAAU,CAAC;AAGpD,OAAO,EACL,iBAAiB,EACjB,wBAAwB,EACzB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AASjD,qBAAa,SAAS;IACpB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,IAAI,CAAoB;gBAEpB,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,wBAAwB;IAiB/D,IAAW,OAAO,IAAI,MAAM,CAE3B;IAED,IAAW,IAAI,IAAI,iBAAiB,CAEnC;IAEM,QAAQ,IAAI,MAAM;IAIlB,QAAQ,IAAI,MAAM;IAIlB,SAAS,IAAI,MAAM;WAMN,cAAc,IAAI,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;WAOtD,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS;WAK1C,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;WAYnC,MAAM,CACxB,MAAM,GAAE,qBAA0B,GACjC,OAAO,CAAC,SAAS,CAAC;mBAyCA,IAAI;IA4FzB;;;OAGG;IACU,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC;IAK5C;;;OAGG;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AC1B,OAAO,CAAC,eAAe,CAAC;IA+Cd,qBAAqB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAiChE,iBAAiB,IAAI,MAAM,EAAE;CAUrC"}
@@ -13,6 +13,7 @@ const Wallet_1 = require("./Wallet");
13
13
  const config_1 = require("../utils/config");
14
14
  const coins_1 = require("./coins");
15
15
  const utils_1 = require("../utils");
16
+ const hashUtils_1 = require("../utils/hashUtils");
16
17
  const data_integrity_tree_1 = require("@dignetwork/data-integrity-tree");
17
18
  const prompts_1 = require("../prompts");
18
19
  const FileCache_1 = require("../utils/FileCache");
@@ -406,5 +407,27 @@ class DataStore {
406
407
  }
407
408
  return updateStoreResponse.newStore;
408
409
  }
410
+ async getFileSetForRootHash(rootHash) {
411
+ const datFilePath = path_1.default.join(config_1.STORE_PATH, this.storeId, `${rootHash}.dat`);
412
+ const datFileContent = JSON.parse(fs_1.default.readFileSync(datFilePath, "utf-8"));
413
+ const heightDatFilePath = path_1.default.join(config_1.STORE_PATH, this.storeId, "height.json");
414
+ const manifestFilePath = path_1.default.join(config_1.STORE_PATH, this.storeId, "manifest.dat");
415
+ const filesInvolved = [];
416
+ filesInvolved.push(manifestFilePath);
417
+ filesInvolved.push(datFilePath);
418
+ filesInvolved.push(heightDatFilePath);
419
+ for (const [fileKey, fileData] of Object.entries(datFileContent.files)) {
420
+ const filepath = path_1.default.join(config_1.STORE_PATH, this.storeId, "data", fileKey);
421
+ const filePath = (0, hashUtils_1.getFilePathFromSha256)(datFileContent.files[fileKey].sha256, path_1.default.join(config_1.STORE_PATH, this.storeId, "data"));
422
+ filesInvolved.push(filePath);
423
+ }
424
+ return filesInvolved;
425
+ }
426
+ getManifestHashes() {
427
+ const manifestFilePath = path_1.default.join(config_1.STORE_PATH, this.storeId, "manifest.dat");
428
+ return fs_1.default.existsSync(manifestFilePath)
429
+ ? fs_1.default.readFileSync(manifestFilePath, "utf-8").split("\n").filter(Boolean)
430
+ : [];
431
+ }
409
432
  }
410
433
  exports.DataStore = DataStore;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.3",
3
+ "version": "0.0.1-alpha.4",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",