@dignetwork/dig-sdk 0.0.1-alpha.130 → 0.0.1-alpha.131
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.
|
@@ -7,7 +7,7 @@ export declare class DigNetwork {
|
|
|
7
7
|
constructor(storeId: string);
|
|
8
8
|
static subscribeToStore(storeId: string): Promise<void>;
|
|
9
9
|
static getUdiContent(udi: string): Promise<void>;
|
|
10
|
-
static findPeerWithStoreKey(storeId: string, rootHash: string, key?: string,
|
|
10
|
+
static findPeerWithStoreKey(storeId: string, rootHash: string, key?: string, initialBlackList?: string[]): Promise<DigPeer | null>;
|
|
11
11
|
static unsubscribeFromStore(storeId: string): void;
|
|
12
12
|
static pingNetworkOfUpdate(storeId: string, rootHash: string): Promise<void>;
|
|
13
13
|
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,
|
|
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,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;IA0GH,mBAAmB,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;CAUvD"}
|
|
@@ -50,48 +50,61 @@ class DigNetwork {
|
|
|
50
50
|
static async getUdiContent(udi) {
|
|
51
51
|
// TODO: Implement this method
|
|
52
52
|
}
|
|
53
|
-
static async findPeerWithStoreKey(storeId, rootHash, key,
|
|
54
|
-
const peerBlackList =
|
|
53
|
+
static async findPeerWithStoreKey(storeId, rootHash, key, initialBlackList = []) {
|
|
54
|
+
const peerBlackList = initialBlackList;
|
|
55
55
|
const serverCoin = new blockchain_1.ServerCoin(storeId);
|
|
56
|
-
let peerIp = null;
|
|
57
|
-
// Keep sampling peers until an empty array is returned
|
|
58
56
|
while (true) {
|
|
59
57
|
try {
|
|
60
|
-
// Sample
|
|
61
|
-
const digPeers = await serverCoin.sampleCurrentEpoch(
|
|
58
|
+
// Sample 10 peers from the current epoch
|
|
59
|
+
const digPeers = await serverCoin.sampleCurrentEpoch(10, peerBlackList);
|
|
62
60
|
// If no peers are returned, break out of the loop
|
|
63
61
|
if (digPeers.length === 0) {
|
|
64
62
|
console.log("No more peers found.");
|
|
65
63
|
break;
|
|
66
64
|
}
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
65
|
+
// Create a race of promises for all peers
|
|
66
|
+
const peerPromises = digPeers.map((peerIp) => {
|
|
67
|
+
return new Promise(async (resolve) => {
|
|
68
|
+
try {
|
|
69
|
+
const digPeer = new DigPeer_1.DigPeer(peerIp, storeId);
|
|
70
|
+
const { storeExists, rootHashExists } = await digPeer.propagationServer.checkStoreExists(rootHash);
|
|
71
|
+
// Check if the store and root hash exist on the peer
|
|
72
|
+
if (storeExists && rootHashExists) {
|
|
73
|
+
console.log(`Found Peer at ${peerIp} for storeId: ${storeId}, root hash ${rootHash}`);
|
|
74
|
+
// If no key is provided, resolve the peer
|
|
75
|
+
if (!key) {
|
|
76
|
+
return resolve(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 resolve(digPeer);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
console.error(`Error connecting to DIG Peer ${peerIp}.`);
|
|
87
|
+
}
|
|
88
|
+
// If the peer does not meet the criteria, resolve with null
|
|
89
|
+
resolve(null);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
// Wait for the first valid peer that resolves
|
|
93
|
+
const firstValidPeer = await Promise.race(peerPromises);
|
|
94
|
+
// If a valid peer is found, return it
|
|
95
|
+
if (firstValidPeer) {
|
|
96
|
+
return firstValidPeer;
|
|
83
97
|
}
|
|
84
|
-
//
|
|
85
|
-
peerBlackList.push(peerIp);
|
|
98
|
+
// If none of the peers were valid, add them to the blacklist
|
|
99
|
+
digPeers.forEach((peerIp) => peerBlackList.push(peerIp));
|
|
100
|
+
// Retry with the next set of peers
|
|
101
|
+
console.log("No valid peers found, retrying with new peers...");
|
|
86
102
|
}
|
|
87
103
|
catch (error) {
|
|
88
|
-
console.error(
|
|
89
|
-
if (peerIp) {
|
|
90
|
-
peerBlackList.push(peerIp); // Add to blacklist if error occurs
|
|
91
|
-
}
|
|
104
|
+
console.error("Error sampling peers. Resampling...");
|
|
92
105
|
}
|
|
93
106
|
}
|
|
94
|
-
// Return null if no valid peer was found
|
|
107
|
+
// Return null if no valid peer was found after all attempts
|
|
95
108
|
return null;
|
|
96
109
|
}
|
|
97
110
|
static unsubscribeFromStore(storeId) {
|