@dignetwork/dig-sdk 0.0.1-alpha.131 → 0.0.1-alpha.134
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,6 +4,7 @@ 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>;
|
|
@@ -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;
|
|
1
|
+
{"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAKpC,qBAAa,UAAU;IACrB,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,aAAa,CAA2B;IAChD,OAAO,CAAC,MAAM,CAAC,cAAc,CAAmC;gBAEpD,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,gBAAgB,GAAE,MAAM,EAAO,GAC9B,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;WA0EZ,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;WAOrC,mBAAmB,CACrC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAcH,kBAAkB,CAC7B,eAAe,CAAC,EAAE,OAAO,EACzB,iBAAiB,CAAC,EAAE,MAAM,GACzB,OAAO,CAAC,IAAI,CAAC;IA8GH,mBAAmB,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;CAUvD"}
|
|
@@ -30,9 +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);
|
|
@@ -123,7 +120,12 @@ class DigNetwork {
|
|
|
123
120
|
}
|
|
124
121
|
}
|
|
125
122
|
async syncStoreFromPeers(prioritizedPeer, maxRootsToProcess) {
|
|
126
|
-
|
|
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);
|
|
127
129
|
let peerBlackList = [];
|
|
128
130
|
try {
|
|
129
131
|
const rootHistory = await this.dataStore.getRootHistory();
|
|
@@ -181,7 +183,6 @@ class DigNetwork {
|
|
|
181
183
|
}
|
|
182
184
|
}
|
|
183
185
|
}
|
|
184
|
-
DigNetwork.pingNetworkOfUpdate(this.dataStore.StoreId, rootInfo.root_hash);
|
|
185
186
|
}
|
|
186
187
|
console.log("Syncing store complete.");
|
|
187
188
|
}
|
|
@@ -189,6 +190,11 @@ class DigNetwork {
|
|
|
189
190
|
console.error("Error during syncing store from peers:", error);
|
|
190
191
|
throw error;
|
|
191
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
|
+
}
|
|
192
198
|
}
|
|
193
199
|
// Fetches available peers for the store
|
|
194
200
|
async fetchAvailablePeers() {
|
|
@@ -199,3 +205,4 @@ class DigNetwork {
|
|
|
199
205
|
}
|
|
200
206
|
}
|
|
201
207
|
exports.DigNetwork = DigNetwork;
|
|
208
|
+
DigNetwork.networkSyncMap = new Map();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PropagationServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/PropagationServer.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,MAAM,OAAO,CAAC;AAY1B,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAkBrC,qBAAa,iBAAiB;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,GAAG,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAE7B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAQ;IACpC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAAQ;gBAErC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAY9C;;OAEG;IACG,gBAAgB;IAOtB;;OAEG;IACH,gBAAgB;IAQhB;;;OAGG;IACG,aAAa,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E,OAAO,CAAA;KAAE,CAAC;IA4C7D;;OAEG;IACG,kBAAkB,CAAC,QAAQ,EAAE,MAAM;IAyDzC;;OAEG;IACG,YAAY,CAChB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,OAAO,CAAA;KAAE,CAAC;IA2BlD;;;OAGG;IACG,UAAU,CACd,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,UAAU,GAAE,OAAe;IAsH7B;;OAEG;IACG,mBAAmB;IAkCzB;;OAEG;WACU,WAAW,CACtB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM;IA0DnB;;;OAGG;IACG,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAkFlD;;OAEG;IACG,cAAc,CAClB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC;QAAE,MAAM,EAAE,OAAO,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IA4B7C;;;OAGG;IACG,YAAY,CAChB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM;IA0GjB;;OAEG;WACU,aAAa,CACxB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM;CA2FpB"}
|
|
@@ -120,7 +120,6 @@ class PropagationServer {
|
|
|
120
120
|
* @param rootHash - The root hash for the store update.
|
|
121
121
|
*/
|
|
122
122
|
async pingUpdate(rootHash) {
|
|
123
|
-
const spinner = (0, nanospinner_1.createSpinner)(`Pinging peer ${this.ipAddress}...`).start();
|
|
124
123
|
try {
|
|
125
124
|
const httpsAgent = this.createHttpsAgent();
|
|
126
125
|
const url = `https://${(0, network_1.formatHost)(this.ipAddress)}:${PropagationServer.port}/update`;
|
|
@@ -129,6 +128,10 @@ class PropagationServer {
|
|
|
129
128
|
headers: {
|
|
130
129
|
"Content-Type": "application/json",
|
|
131
130
|
},
|
|
131
|
+
validateStatus: (status) => {
|
|
132
|
+
// Accept all status codes to handle them manually
|
|
133
|
+
return true;
|
|
134
|
+
},
|
|
132
135
|
};
|
|
133
136
|
// Data to send in the request (storeId and rootHash)
|
|
134
137
|
const data = {
|
|
@@ -138,20 +141,37 @@ class PropagationServer {
|
|
|
138
141
|
};
|
|
139
142
|
try {
|
|
140
143
|
const response = await axios_1.default.post(url, data, config);
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
144
|
+
if (response.status === 200) {
|
|
145
|
+
console.log((0, colorette_1.green)(`✔ Peer was up to date: ${this.ipAddress}`));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
else if (response.status === 400 &&
|
|
149
|
+
response.data?.error?.includes("does not exist")) {
|
|
150
|
+
console.log((0, colorette_1.yellow)(`⚠ Peer ${this.ipAddress} does not have store ${this.storeId}. Notifying for update.`));
|
|
151
|
+
// You can implement additional logic here if needed, such as retrying or logging
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
console.error((0, colorette_1.red)(`✖ Unexpected response from peer ${this.ipAddress}: ${response.status} ${response.statusText}`));
|
|
156
|
+
throw new Error(`Unexpected response: ${response.status} ${response.statusText}`);
|
|
157
|
+
}
|
|
146
158
|
}
|
|
147
159
|
catch (error) {
|
|
160
|
+
if (error.response) {
|
|
161
|
+
// Server responded with a status other than 2xx
|
|
162
|
+
if (error.response.status === 400 &&
|
|
163
|
+
error.response.data?.error?.includes("does not exist")) {
|
|
164
|
+
console.log((0, colorette_1.yellow)(`⚠ Peer ${this.ipAddress} does not have store ${this.storeId}. Notifying for update.`));
|
|
165
|
+
// You can implement additional logic here if needed
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
148
169
|
console.error((0, colorette_1.red)(`✖ Failed to ping peer: ${this.ipAddress}`));
|
|
149
170
|
console.error((0, colorette_1.red)(error.message));
|
|
150
171
|
throw error;
|
|
151
172
|
}
|
|
152
173
|
}
|
|
153
174
|
catch (error) {
|
|
154
|
-
spinner.error({ text: (0, colorette_1.red)("✖ Error pinging network peer.") });
|
|
155
175
|
console.error((0, colorette_1.red)(error.message));
|
|
156
176
|
}
|
|
157
177
|
}
|