@dignetwork/dig-sdk 0.0.1-alpha.130 → 0.0.1-alpha.133

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.
@@ -4,10 +4,11 @@ export declare class DigNetwork {
4
4
  private serverCoin;
5
5
  private storeDir;
6
6
  private peerBlacklist;
7
+ private static networkSyncMap;
7
8
  constructor(storeId: string);
8
9
  static subscribeToStore(storeId: string): Promise<void>;
9
10
  static getUdiContent(udi: string): Promise<void>;
10
- static findPeerWithStoreKey(storeId: string, rootHash: string, key?: string, intialBlackList?: string[]): Promise<DigPeer | null>;
11
+ static findPeerWithStoreKey(storeId: string, rootHash: string, key?: string, initialBlackList?: string[]): Promise<DigPeer | null>;
11
12
  static unsubscribeFromStore(storeId: string): void;
12
13
  static pingNetworkOfUpdate(storeId: string, rootHash: string): Promise<void>;
13
14
  syncStoreFromPeers(prioritizedPeer?: DigPeer, maxRootsToProcess?: number): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AASpC,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;WAOP,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WAQhD,aAAa,CAAC,GAAG,EAAE,MAAM;WAIzB,oBAAoB,CACtC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,MAAM,EACZ,eAAe,GAAE,MAAM,EAAO,GAC7B,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;WA2DZ,oBAAoB,CAAC,OAAO,EAAE,M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 +30,6 @@ const DigPeer_1 = require("./DigPeer");
30
30
  const blockchain_1 = require("../blockchain");
31
31
  const config_1 = require("../utils/config");
32
32
  const utils_1 = require("../utils");
33
- const util_1 = require("util");
34
- const rename = (0, util_1.promisify)(fs.rename);
35
- const unlink = (0, util_1.promisify)(fs.unlink);
36
33
  class DigNetwork {
37
34
  constructor(storeId) {
38
35
  this.dataStore = blockchain_1.DataStore.from(storeId);
@@ -50,48 +47,61 @@ class DigNetwork {
50
47
  static async getUdiContent(udi) {
51
48
  // TODO: Implement this method
52
49
  }
53
- static async findPeerWithStoreKey(storeId, rootHash, key, intialBlackList = []) {
54
- const peerBlackList = intialBlackList;
50
+ static async findPeerWithStoreKey(storeId, rootHash, key, initialBlackList = []) {
51
+ const peerBlackList = initialBlackList;
55
52
  const serverCoin = new blockchain_1.ServerCoin(storeId);
56
- let peerIp = null;
57
- // Keep sampling peers until an empty array is returned
58
53
  while (true) {
59
54
  try {
60
- // Sample a peer from the current epoch
61
- const digPeers = await serverCoin.sampleCurrentEpoch(1, peerBlackList);
55
+ // Sample 10 peers from the current epoch
56
+ const digPeers = await serverCoin.sampleCurrentEpoch(10, peerBlackList);
62
57
  // If no peers are returned, break out of the loop
63
58
  if (digPeers.length === 0) {
64
59
  console.log("No more peers found.");
65
60
  break;
66
61
  }
67
- peerIp = digPeers[0];
68
- const digPeer = new DigPeer_1.DigPeer(peerIp, storeId);
69
- // Try to fetch the head store information
70
- const { storeExists, rootHashExists } = await digPeer.propagationServer.checkStoreExists(rootHash);
71
- // If the peer has the correct root hash, check if key is required
72
- if (storeExists && rootHashExists) {
73
- console.log(`Found Peer at ${peerIp} for storeId: ${storeId}, root hash ${rootHash}`);
74
- // If no key is provided, return the peer
75
- if (!key) {
76
- return digPeer;
77
- }
78
- // If key is provided, check if the peer has it
79
- const keyResponse = await digPeer.contentServer.headKey(key, rootHash);
80
- if (keyResponse.headers?.["x-key-exists"] === "true") {
81
- return digPeer;
82
- }
62
+ // Create a race of promises for all peers
63
+ const peerPromises = digPeers.map((peerIp) => {
64
+ return new Promise(async (resolve) => {
65
+ try {
66
+ const digPeer = new DigPeer_1.DigPeer(peerIp, storeId);
67
+ const { storeExists, rootHashExists } = await digPeer.propagationServer.checkStoreExists(rootHash);
68
+ // Check if the store and root hash exist on the peer
69
+ if (storeExists && rootHashExists) {
70
+ console.log(`Found Peer at ${peerIp} for storeId: ${storeId}, root hash ${rootHash}`);
71
+ // If no key is provided, resolve the peer
72
+ if (!key) {
73
+ return resolve(digPeer);
74
+ }
75
+ // If key is provided, check if the peer has it
76
+ const keyResponse = await digPeer.contentServer.headKey(key, rootHash);
77
+ if (keyResponse.headers?.["x-key-exists"] === "true") {
78
+ return resolve(digPeer);
79
+ }
80
+ }
81
+ }
82
+ catch (error) {
83
+ console.error(`Error connecting to DIG Peer ${peerIp}.`);
84
+ }
85
+ // If the peer does not meet the criteria, resolve with null
86
+ resolve(null);
87
+ });
88
+ });
89
+ // Wait for the first valid peer that resolves
90
+ const firstValidPeer = await Promise.race(peerPromises);
91
+ // If a valid peer is found, return it
92
+ if (firstValidPeer) {
93
+ return firstValidPeer;
83
94
  }
84
- // Add peer to blacklist if it doesn't meet criteria
85
- peerBlackList.push(peerIp);
95
+ // If none of the peers were valid, add them to the blacklist
96
+ digPeers.forEach((peerIp) => peerBlackList.push(peerIp));
97
+ // Retry with the next set of peers
98
+ console.log("No valid peers found, retrying with new peers...");
86
99
  }
87
100
  catch (error) {
88
- console.error(`Error connecting to DIG Peer ${peerIp}. Resampling...`);
89
- if (peerIp) {
90
- peerBlackList.push(peerIp); // Add to blacklist if error occurs
91
- }
101
+ console.error("Error sampling peers. Resampling...");
92
102
  }
93
103
  }
94
- // Return null if no valid peer was found
104
+ // Return null if no valid peer was found after all attempts
95
105
  return null;
96
106
  }
97
107
  static unsubscribeFromStore(storeId) {
@@ -110,7 +120,12 @@ class DigNetwork {
110
120
  }
111
121
  }
112
122
  async syncStoreFromPeers(prioritizedPeer, maxRootsToProcess) {
113
- console.log("Starting file download process...");
123
+ // Check if synchronization is already active for this storeId
124
+ if (DigNetwork.networkSyncMap.get(this.dataStore.StoreId)) {
125
+ return;
126
+ }
127
+ console.log("Starting network sync for store:", this.dataStore.StoreId);
128
+ DigNetwork.networkSyncMap.set(this.dataStore.StoreId, true);
114
129
  let peerBlackList = [];
115
130
  try {
116
131
  const rootHistory = await this.dataStore.getRootHistory();
@@ -168,7 +183,6 @@ class DigNetwork {
168
183
  }
169
184
  }
170
185
  }
171
- DigNetwork.pingNetworkOfUpdate(this.dataStore.StoreId, rootInfo.root_hash);
172
186
  }
173
187
  console.log("Syncing store complete.");
174
188
  }
@@ -176,6 +190,11 @@ class DigNetwork {
176
190
  console.error("Error during syncing store from peers:", error);
177
191
  throw error;
178
192
  }
193
+ finally {
194
+ // Mark synchronization as inactive for this storeId
195
+ DigNetwork.networkSyncMap.set(this.dataStore.StoreId, false);
196
+ console.log(`Network sync for storeId: ${this.dataStore.StoreId} has completed.`);
197
+ }
179
198
  }
180
199
  // Fetches available peers for the store
181
200
  async fetchAvailablePeers() {
@@ -186,3 +205,4 @@ class DigNetwork {
186
205
  }
187
206
  }
188
207
  exports.DigNetwork = DigNetwork;
208
+ DigNetwork.networkSyncMap = new Map();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.130",
3
+ "version": "0.0.1-alpha.133",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",