@dignetwork/dig-sdk 0.0.1-alpha.7 → 0.0.1-alpha.70
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.
- package/dist/DataIntegrityTree/DataIntegrityTree.d.ts +5 -2
- package/dist/DataIntegrityTree/DataIntegrityTree.d.ts.map +1 -1
- package/dist/DataIntegrityTree/DataIntegrityTree.js +94 -34
- package/dist/DigNetwork/ContentServer.d.ts +9 -4
- package/dist/DigNetwork/ContentServer.d.ts.map +1 -1
- package/dist/DigNetwork/ContentServer.js +55 -19
- package/dist/DigNetwork/DigNetwork.d.ts +3 -8
- package/dist/DigNetwork/DigNetwork.d.ts.map +1 -1
- package/dist/DigNetwork/DigNetwork.js +115 -171
- package/dist/DigNetwork/DigPeer.d.ts +9 -3
- package/dist/DigNetwork/DigPeer.d.ts.map +1 -1
- package/dist/DigNetwork/DigPeer.js +68 -114
- package/dist/DigNetwork/PropagationServer.d.ts +91 -33
- package/dist/DigNetwork/PropagationServer.d.ts.map +1 -1
- package/dist/DigNetwork/PropagationServer.js +414 -372
- package/dist/blockchain/DataStore.d.ts +13 -6
- package/dist/blockchain/DataStore.d.ts.map +1 -1
- package/dist/blockchain/DataStore.js +64 -91
- package/dist/blockchain/DataStoreSerializer.d.ts +1 -1
- package/dist/blockchain/DataStoreSerializer.d.ts.map +1 -1
- package/dist/blockchain/FullNodePeer.d.ts +9 -1
- package/dist/blockchain/FullNodePeer.d.ts.map +1 -1
- package/dist/blockchain/FullNodePeer.js +38 -10
- package/dist/blockchain/ServerCoin.d.ts +11 -3
- package/dist/blockchain/ServerCoin.d.ts.map +1 -1
- package/dist/blockchain/ServerCoin.js +45 -17
- package/dist/blockchain/Wallet.d.ts +2 -2
- package/dist/blockchain/Wallet.d.ts.map +1 -1
- package/dist/blockchain/Wallet.js +2 -2
- package/dist/blockchain/coins.d.ts +1 -1
- package/dist/blockchain/coins.d.ts.map +1 -1
- package/dist/blockchain/coins.js +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/ContentScanner.d.ts +63 -0
- package/dist/utils/ContentScanner.d.ts.map +1 -0
- package/dist/utils/ContentScanner.js +175 -0
- package/dist/utils/FileTransfer.d.ts +47 -0
- package/dist/utils/FileTransfer.d.ts.map +1 -0
- package/dist/utils/FileTransfer.js +209 -0
- package/dist/utils/StoreArchiveManager.d.ts +45 -0
- package/dist/utils/StoreArchiveManager.d.ts.map +1 -0
- package/dist/utils/StoreArchiveManager.js +153 -0
- package/dist/utils/config.d.ts +0 -1
- package/dist/utils/config.d.ts.map +1 -1
- package/dist/utils/config.js +2 -3
- package/dist/utils/directoryUtils.d.ts +0 -6
- package/dist/utils/directoryUtils.d.ts.map +1 -1
- package/dist/utils/directoryUtils.js +30 -11
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +0 -1
- package/package.json +12 -3
- package/dist/utils/deltaUtils.d.ts +0 -2
- package/dist/utils/deltaUtils.d.ts.map +0 -1
- package/dist/utils/deltaUtils.js +0 -83
|
@@ -26,12 +26,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.DigNetwork = void 0;
|
|
27
27
|
const fs = __importStar(require("fs"));
|
|
28
28
|
const path = __importStar(require("path"));
|
|
29
|
-
const cli_progress_1 = require("cli-progress");
|
|
30
29
|
const DigPeer_1 = require("./DigPeer");
|
|
31
|
-
const deltaUtils_1 = require("../utils/deltaUtils");
|
|
32
|
-
const hashUtils_1 = require("../utils/hashUtils");
|
|
33
30
|
const blockchain_1 = require("../blockchain");
|
|
34
31
|
const config_1 = require("../utils/config");
|
|
32
|
+
const util_1 = require("util");
|
|
33
|
+
const rename = (0, util_1.promisify)(fs.rename);
|
|
34
|
+
const unlink = (0, util_1.promisify)(fs.unlink);
|
|
35
35
|
class DigNetwork {
|
|
36
36
|
constructor(storeId) {
|
|
37
37
|
this.dataStore = blockchain_1.DataStore.from(storeId);
|
|
@@ -39,88 +39,56 @@ class DigNetwork {
|
|
|
39
39
|
this.storeDir = path.resolve(config_1.DIG_FOLDER_PATH, "stores", storeId);
|
|
40
40
|
this.peerBlacklist = new Map(); // Initialize empty map for blacklists
|
|
41
41
|
}
|
|
42
|
-
async uploadPreflight(digPeer) {
|
|
43
|
-
// Preflight check is handled internally by PropagationServer if needed
|
|
44
|
-
const { lastUploadedHash, generationIndex } = await digPeer.propagationServer.getUploadDetails();
|
|
45
|
-
const rootHistory = await this.dataStore.getLocalRootHistory();
|
|
46
|
-
if (!rootHistory || rootHistory.length === 0) {
|
|
47
|
-
throw new Error("No root hashes found. Please commit your changes first.");
|
|
48
|
-
}
|
|
49
|
-
const lastLocalRootHash = rootHistory[rootHistory.length - 1].root_hash;
|
|
50
|
-
const localGenerationIndex = rootHistory.length - 1;
|
|
51
|
-
// Handle conditions based on the upload details
|
|
52
|
-
if (lastUploadedHash !== lastLocalRootHash &&
|
|
53
|
-
generationIndex === localGenerationIndex) {
|
|
54
|
-
throw new Error("The repository seems to be corrupted. Please pull the latest changes before pushing.");
|
|
55
|
-
}
|
|
56
|
-
if (lastUploadedHash === lastLocalRootHash &&
|
|
57
|
-
generationIndex === localGenerationIndex) {
|
|
58
|
-
throw new Error("No changes detected. Skipping push.");
|
|
59
|
-
}
|
|
60
|
-
if (lastUploadedHash !== lastLocalRootHash &&
|
|
61
|
-
generationIndex > localGenerationIndex) {
|
|
62
|
-
throw new Error("Remote repository is ahead of the local repository. Please pull the latest changes before pushing.");
|
|
63
|
-
}
|
|
64
|
-
return { generationIndex, lastLocalRootHash };
|
|
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
|
-
}
|
|
100
|
-
// Uploads the store to a specific peer
|
|
101
|
-
async uploadStore(digPeer) {
|
|
102
|
-
const { generationIndex } = await this.uploadPreflight(digPeer);
|
|
103
|
-
const filesToUpload = await (0, deltaUtils_1.getDeltaFiles)(this.dataStore.StoreId, generationIndex, path.resolve(config_1.DIG_FOLDER_PATH, "stores"));
|
|
104
|
-
if (!filesToUpload.length) {
|
|
105
|
-
console.log("No files to upload.");
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
await this.runProgressBar(filesToUpload.length, "Store Data", async (progress) => {
|
|
109
|
-
for (const filePath of filesToUpload) {
|
|
110
|
-
const relativePath = path
|
|
111
|
-
.relative(this.storeDir, filePath)
|
|
112
|
-
.replace(/\\/g, "/");
|
|
113
|
-
await digPeer.propagationServer.pushFile(filePath, relativePath);
|
|
114
|
-
progress.increment();
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
42
|
static async subscribeToStore(storeId) {
|
|
119
43
|
fs.mkdirSync(path.join(config_1.DIG_FOLDER_PATH, "stores", storeId), {
|
|
120
44
|
recursive: true,
|
|
121
45
|
});
|
|
122
46
|
const digNetwork = new DigNetwork(storeId);
|
|
123
|
-
await digNetwork.
|
|
47
|
+
await digNetwork.syncStoreFromPeers();
|
|
48
|
+
}
|
|
49
|
+
static async findPeerWithStoreKey(storeId, rootHash, key, intialBlackList = []) {
|
|
50
|
+
const peerBlackList = intialBlackList;
|
|
51
|
+
const serverCoin = new blockchain_1.ServerCoin(storeId);
|
|
52
|
+
let peerIp = null;
|
|
53
|
+
// Keep sampling peers until an empty array is returned
|
|
54
|
+
while (true) {
|
|
55
|
+
try {
|
|
56
|
+
// Sample a peer from the current epoch
|
|
57
|
+
const digPeers = await serverCoin.sampleCurrentEpoch(1, peerBlackList);
|
|
58
|
+
// If no peers are returned, break out of the loop
|
|
59
|
+
if (digPeers.length === 0) {
|
|
60
|
+
console.log("No more peers found.");
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
peerIp = digPeers[0];
|
|
64
|
+
const digPeer = new DigPeer_1.DigPeer(peerIp, storeId);
|
|
65
|
+
// Try to fetch the head store information
|
|
66
|
+
const { storeExists, rootHashExists } = await digPeer.propagationServer.checkStoreExists(rootHash);
|
|
67
|
+
// If the peer has the correct root hash, check if key is required
|
|
68
|
+
if (storeExists && rootHashExists) {
|
|
69
|
+
console.log(`Found Peer at ${peerIp} for storeId: ${storeId}, root hash ${rootHash}`);
|
|
70
|
+
// If no key is provided, return the peer
|
|
71
|
+
if (!key) {
|
|
72
|
+
return digPeer;
|
|
73
|
+
}
|
|
74
|
+
// If key is provided, check if the peer has it
|
|
75
|
+
const keyResponse = await digPeer.contentServer.headKey(key, rootHash);
|
|
76
|
+
if (keyResponse.headers?.["x-key-exists"] === "true") {
|
|
77
|
+
return digPeer;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Add peer to blacklist if it doesn't meet criteria
|
|
81
|
+
peerBlackList.push(peerIp);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
console.error(`Error connecting to DIG Peer ${peerIp}. Resampling...`);
|
|
85
|
+
if (peerIp) {
|
|
86
|
+
peerBlackList.push(peerIp); // Add to blacklist if error occurs
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// Return null if no valid peer was found
|
|
91
|
+
return null;
|
|
124
92
|
}
|
|
125
93
|
static unsubscribeFromStore(storeId) {
|
|
126
94
|
fs.rmdirSync(path.join(config_1.DIG_FOLDER_PATH, "stores", storeId), {
|
|
@@ -128,55 +96,68 @@ class DigNetwork {
|
|
|
128
96
|
});
|
|
129
97
|
fs.unlinkSync(path.join(config_1.DIG_FOLDER_PATH, "stores", storeId + ".json"));
|
|
130
98
|
}
|
|
131
|
-
|
|
132
|
-
|
|
99
|
+
async syncStoreFromPeers() {
|
|
100
|
+
console.log("Starting file download process...");
|
|
101
|
+
let peerBlackList = [];
|
|
102
|
+
let selectedPeer = null;
|
|
133
103
|
try {
|
|
134
104
|
const rootHistory = await this.dataStore.getRootHistory();
|
|
135
|
-
if (!rootHistory.length)
|
|
105
|
+
if (!rootHistory.length) {
|
|
136
106
|
throw new Error("No roots found in rootHistory. Cannot proceed with file download.");
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
107
|
+
}
|
|
108
|
+
// Filter out rootInfo entries where the .dat file already exists
|
|
109
|
+
const rootHistoryFiltered = rootHistory
|
|
110
|
+
.filter((item) => item.timestamp !== undefined)
|
|
111
|
+
.filter((item) => !fs.existsSync(`${this.storeDir}/${item.root_hash}.dat`))
|
|
112
|
+
.reverse(); // Reverse to download the latest first
|
|
113
|
+
if (!rootHistoryFiltered.length) {
|
|
114
|
+
console.log("All root hashes already exist locally. No need for download.");
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
// Process filtered rootHistory sequentially
|
|
118
|
+
for (const rootInfo of rootHistoryFiltered) {
|
|
119
|
+
while (true) {
|
|
120
|
+
try {
|
|
121
|
+
// Find a peer with the store and root hash
|
|
122
|
+
selectedPeer = await DigNetwork.findPeerWithStoreKey(this.dataStore.StoreId, rootInfo.root_hash, undefined, peerBlackList);
|
|
123
|
+
if (!selectedPeer) {
|
|
124
|
+
console.error(`No peer found with root hash ${rootInfo.root_hash}. Skipping download.`);
|
|
125
|
+
break; // Exit loop if no more peers are found
|
|
126
|
+
}
|
|
127
|
+
// Ensure the selected peer has the store by checking with a HEAD request
|
|
128
|
+
const { storeExists, rootHashExists } = await selectedPeer.propagationServer.checkStoreExists(rootInfo.root_hash);
|
|
129
|
+
if (!storeExists) {
|
|
130
|
+
console.warn(`Peer ${selectedPeer.IpAddress} does not have the store. Trying another peer...`);
|
|
131
|
+
peerBlackList.push(selectedPeer.IpAddress); // Add peer to blacklist and try again
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (!rootHashExists) {
|
|
135
|
+
console.warn(`Peer ${selectedPeer.IpAddress} does not have the root hash. Trying another peer...`);
|
|
136
|
+
peerBlackList.push(selectedPeer.IpAddress); // Add peer to blacklist and try again
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
// Download the store root and all associated data from the selected peer
|
|
140
|
+
await selectedPeer.downloadStoreRoot(rootInfo.root_hash);
|
|
141
|
+
peerBlackList = []; // Clear the blacklist upon successful download
|
|
142
|
+
// Break out of the retry loop if the download succeeds
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
console.error(`Error downloading from peer. Retrying with another peer.`, error);
|
|
147
|
+
if (selectedPeer) {
|
|
148
|
+
peerBlackList.push(selectedPeer.IpAddress); // Add peer to blacklist and try again
|
|
149
|
+
}
|
|
166
150
|
}
|
|
167
151
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
progress?.increment();
|
|
152
|
+
// Process the latest root hash first, breaking after each success to handle new incoming roots
|
|
153
|
+
break;
|
|
171
154
|
}
|
|
172
|
-
if (newRootHashes.length)
|
|
173
|
-
fs.appendFileSync(localManifestPath, newRootHashes.join("\n") + "\n");
|
|
174
|
-
await this.downloadManifestFile(forceDownload);
|
|
175
|
-
progressBar?.stop();
|
|
176
155
|
console.log("Syncing store complete.");
|
|
177
156
|
}
|
|
178
157
|
catch (error) {
|
|
179
|
-
|
|
158
|
+
if (selectedPeer) {
|
|
159
|
+
peerBlackList.push(selectedPeer.IpAddress);
|
|
160
|
+
}
|
|
180
161
|
throw error;
|
|
181
162
|
}
|
|
182
163
|
}
|
|
@@ -187,74 +168,37 @@ class DigNetwork {
|
|
|
187
168
|
const peers = await this.serverCoin.sampleCurrentEpoch(10, Array.from(this.peerBlacklist.keys()));
|
|
188
169
|
return peers.map((ip) => new DigPeer_1.DigPeer(ip, this.dataStore.StoreId));
|
|
189
170
|
}
|
|
190
|
-
async downloadHeightFile(forceDownload) {
|
|
191
|
-
const heightFilePath = path.join(this.storeDir, "height.json");
|
|
192
|
-
await this.downloadFileFromPeers("height.json", heightFilePath, forceDownload);
|
|
193
|
-
}
|
|
194
|
-
async downloadManifestFile(forceDownload) {
|
|
195
|
-
const heightFilePath = path.join(this.storeDir, "manifest.dat");
|
|
196
|
-
await this.downloadFileFromPeers("manifest.dat", heightFilePath, forceDownload);
|
|
197
|
-
}
|
|
198
171
|
async downloadFileFromPeers(dataPath, filePath, overwrite) {
|
|
199
172
|
let digPeers = await this.fetchAvailablePeers();
|
|
173
|
+
const tempFilePath = `${filePath}.tmp`;
|
|
200
174
|
while (true) {
|
|
201
175
|
if (!overwrite && fs.existsSync(filePath))
|
|
202
176
|
return;
|
|
203
177
|
const blacklist = this.peerBlacklist.get(dataPath) || new Set();
|
|
204
178
|
for (const digPeer of digPeers) {
|
|
205
|
-
if (blacklist.has(digPeer.IpAddress))
|
|
206
|
-
continue;
|
|
207
179
|
try {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const peerStream = await digPeer.propagationServer.streamStoreData(dataPath);
|
|
217
|
-
// Pipe the peer stream directly to the file system
|
|
218
|
-
await new Promise((resolve, reject) => {
|
|
219
|
-
peerStream.pipe(fileStream);
|
|
220
|
-
peerStream.on("end", resolve);
|
|
221
|
-
peerStream.on("error", reject);
|
|
222
|
-
fileStream.on("error", reject);
|
|
223
|
-
});
|
|
224
|
-
if (process.env.DIG_DEBUG === "1") {
|
|
225
|
-
console.log(`Downloaded ${dataPath} from ${digPeer.IpAddress}`);
|
|
180
|
+
if (blacklist.has(digPeer.IpAddress))
|
|
181
|
+
continue;
|
|
182
|
+
// Ensure the selected peer has the store by checking with a HEAD request
|
|
183
|
+
const { storeExists } = await digPeer.propagationServer.checkStoreExists();
|
|
184
|
+
if (!storeExists) {
|
|
185
|
+
console.warn(`Peer ${digPeer.IpAddress} does not have the store. Trying another peer...`);
|
|
186
|
+
blacklist.add(digPeer.IpAddress); // Add peer to blacklist and try again
|
|
187
|
+
continue;
|
|
226
188
|
}
|
|
189
|
+
await digPeer.downloadData(dataPath);
|
|
227
190
|
return; // Exit the method if download succeeds
|
|
228
191
|
}
|
|
229
192
|
catch (error) {
|
|
230
193
|
console.warn(`Failed to download ${dataPath} from ${digPeer.IpAddress}, blacklisting peer and trying next...`);
|
|
231
194
|
blacklist.add(digPeer.IpAddress);
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
if (process.env.DIG_DEBUG === "1") {
|
|
237
|
-
console.warn(`All peers blacklisted for ${dataPath}. Refreshing peers...`);
|
|
238
|
-
}
|
|
239
|
-
digPeers = await this.fetchAvailablePeers();
|
|
240
|
-
if (!digPeers.length) {
|
|
241
|
-
throw new Error(`Failed to download ${dataPath}: no peers available.`);
|
|
195
|
+
// Clean up the temp file in case of failure
|
|
196
|
+
if (fs.existsSync(tempFilePath)) {
|
|
197
|
+
await unlink(tempFilePath);
|
|
198
|
+
}
|
|
242
199
|
}
|
|
243
200
|
}
|
|
244
201
|
}
|
|
245
202
|
}
|
|
246
|
-
async runProgressBar(total, name, task) {
|
|
247
|
-
// Using 'any' to work around TypeScript issues
|
|
248
|
-
const multiBar = new cli_progress_1.MultiBar({
|
|
249
|
-
clearOnComplete: false,
|
|
250
|
-
hideCursor: true,
|
|
251
|
-
format: "{bar} | {percentage}% | {name}",
|
|
252
|
-
noTTYOutput: true,
|
|
253
|
-
}, cli_progress_1.Presets.shades_classic);
|
|
254
|
-
const progress = multiBar.create(total, 0, { name });
|
|
255
|
-
await task(progress).finally(() => {
|
|
256
|
-
multiBar.stop();
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
203
|
}
|
|
260
204
|
exports.DigNetwork = DigNetwork;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ContentServer } from "./ContentServer";
|
|
2
2
|
import { PropagationServer } from "./PropagationServer";
|
|
3
3
|
import { IncentiveServer } from "./IncentiveServer";
|
|
4
|
+
import { Output } from "@dignetwork/datalayer-driver";
|
|
4
5
|
export declare class DigPeer {
|
|
5
6
|
private ipAddress;
|
|
6
7
|
private storeId;
|
|
@@ -12,8 +13,13 @@ export declare class DigPeer {
|
|
|
12
13
|
get propagationServer(): PropagationServer;
|
|
13
14
|
get incentiveServer(): IncentiveServer;
|
|
14
15
|
get IpAddress(): string;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
sendPayment(walletName: string, amount: bigint): Promise<void>;
|
|
16
|
+
static sendEqualBulkPayments(walletName: string, addresses: string[], totalAmount: bigint, memos: Buffer[]): Promise<void>;
|
|
17
|
+
static sendBulkPayments(walletName: string, outputs: Output[]): Promise<void>;
|
|
18
|
+
sendPayment(walletName: string, amount: bigint, memos?: Buffer[]): Promise<void>;
|
|
19
|
+
static createPaymentHint(storeId: Buffer): Buffer;
|
|
20
|
+
syncStore(): Promise<void>;
|
|
21
|
+
pushStoreRoot(storeId: string, rootHash: string): Promise<void>;
|
|
22
|
+
downloadStoreRoot(rootHash: string): Promise<void>;
|
|
23
|
+
downloadData(dataPath: string): Promise<void>;
|
|
18
24
|
}
|
|
19
25
|
//# sourceMappingURL=DigPeer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DigPeer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigPeer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"DigPeer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigPeer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAKpD,OAAO,EAKL,MAAM,EACP,MAAM,8BAA8B,CAAC;AAKtC,qBAAa,OAAO;IAClB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,cAAc,CAAgB;IACtC,OAAO,CAAC,kBAAkB,CAAoB;IAC9C,OAAO,CAAC,gBAAgB,CAAkB;gBAE9B,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAS9C,IAAW,aAAa,IAAI,aAAa,CAExC;IAGD,IAAW,iBAAiB,IAAI,iBAAiB,CAEhD;IAGD,IAAW,eAAe,IAAI,eAAe,CAE5C;IAED,IAAW,SAAS,IAAI,MAAM,CAE7B;WAEa,qBAAqB,CACjC,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EAAE,EACnB,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,MAAM,EAAE,GACd,OAAO,CAAC,IAAI,CAAC;WAuBI,gBAAgB,CAClC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EAAE,GAChB,OAAO,CAAC,IAAI,CAAC;IAwCH,WAAW,CACtB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,KAAK,GAAE,MAAM,EAAO,GACnB,OAAO,CAAC,IAAI,CAAC;WAYF,iBAAiB,CAAC,OAAO,EAAE,MAAM;IAqBlC,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;IAc1B,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI/D,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKlD,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAG3D"}
|
|
@@ -9,8 +9,7 @@ const ContentServer_1 = require("./ContentServer");
|
|
|
9
9
|
const PropagationServer_1 = require("./PropagationServer");
|
|
10
10
|
const IncentiveServer_1 = require("./IncentiveServer");
|
|
11
11
|
const blockchain_1 = require("../blockchain");
|
|
12
|
-
const
|
|
13
|
-
const datalayer_driver_1 = require("datalayer-driver");
|
|
12
|
+
const datalayer_driver_1 = require("@dignetwork/datalayer-driver");
|
|
14
13
|
const blockchain_2 = require("../blockchain");
|
|
15
14
|
const blockchain_3 = require("../blockchain");
|
|
16
15
|
const coins_1 = require("../blockchain/coins");
|
|
@@ -37,122 +36,31 @@ class DigPeer {
|
|
|
37
36
|
get IpAddress() {
|
|
38
37
|
return this.ipAddress;
|
|
39
38
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
// Ensure all hashes in root history are present in the manifest in the same order
|
|
56
|
-
for (let i = 0; i < rootHistory.length; i++) {
|
|
57
|
-
if (rootHistory[i].root_hash !== manifestHashes[i]) {
|
|
58
|
-
console.error(`Hash mismatch at index ${i}: manifest hash ${manifestHashes[i]} does not match root history hash ${rootHistory[i].root_hash}`);
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
console.log("Manifest file validated.");
|
|
63
|
-
// Fetch the .dat file content for the specified root hash from the content server
|
|
64
|
-
const datFileContent = JSON.parse(await this.propagationServer.getStoreData(`${rootHash}.dat`));
|
|
65
|
-
if (datFileContent.root !== rootHash) {
|
|
66
|
-
console.error(`Root hash in .dat file does not match: ${datFileContent.root} !== ${rootHash}`);
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
let filesIntegrityIntact = true;
|
|
70
|
-
// Validate SHA256 hashes of the specified keys using streamStoreKey
|
|
71
|
-
for (const key of keys) {
|
|
72
|
-
const fileData = datFileContent.files[key];
|
|
73
|
-
if (!fileData) {
|
|
74
|
-
console.error(`File key ${key} not found in .dat file.`);
|
|
75
|
-
filesIntegrityIntact = false;
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
// Stream the file from the propagation server and calculate the SHA256 hash on the fly
|
|
79
|
-
const hash = crypto_1.default.createHash("sha256");
|
|
80
|
-
const fileStream = await this.contentServer.streamKey(Buffer.from(key, "hex").toString("utf-8"));
|
|
81
|
-
await new Promise((resolve, reject) => {
|
|
82
|
-
fileStream.on("data", (chunk) => {
|
|
83
|
-
hash.update(chunk); // Update the hash with each chunk of data
|
|
84
|
-
});
|
|
85
|
-
fileStream.on("end", () => {
|
|
86
|
-
const calculatedHash = hash.digest("hex");
|
|
87
|
-
// Compare the calculated hash with the expected hash
|
|
88
|
-
if (calculatedHash !== fileData.sha256) {
|
|
89
|
-
console.error(`File ${key} failed SHA256 validation.`);
|
|
90
|
-
filesIntegrityIntact = false;
|
|
91
|
-
}
|
|
92
|
-
resolve();
|
|
93
|
-
});
|
|
94
|
-
fileStream.on("error", (err) => {
|
|
95
|
-
console.error(`Failed to stream file ${key}: ${err.message}`);
|
|
96
|
-
reject(err);
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
// Perform tree integrity validation using the datFileContent and the root hash
|
|
100
|
-
const treeCheck = DataIntegrityTree_1.DataIntegrityTree.validateKeyIntegrityWithForeignTree(key, fileData.sha256, datFileContent, rootHash);
|
|
101
|
-
if (!treeCheck) {
|
|
102
|
-
console.error(`Tree validation failed for file ${key}.`);
|
|
103
|
-
filesIntegrityIntact = false;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
if (!filesIntegrityIntact) {
|
|
107
|
-
console.error("Store Corrupted: Data failed SHA256 validation.");
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
console.log("Store validation successful.");
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
catch (error) {
|
|
114
|
-
console.error(`Failed to validate store: ${error.message}`);
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
async isSynced() {
|
|
119
|
-
try {
|
|
120
|
-
// Fetch the root history from the propagation server
|
|
121
|
-
const dataStore = blockchain_1.DataStore.from(this.storeId);
|
|
122
|
-
const rootHistory = await dataStore.getRootHistory();
|
|
123
|
-
if (rootHistory.length === 0) {
|
|
124
|
-
console.error("No root history found for the store.");
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
// Fetch the manifest.dat file content from the content server
|
|
128
|
-
const manifestContent = await this.propagationServer.getStoreData("manifest.dat");
|
|
129
|
-
const manifestHashes = manifestContent
|
|
130
|
-
.split("\n")
|
|
131
|
-
.filter(Boolean);
|
|
132
|
-
// Compare lengths of root history and manifest
|
|
133
|
-
return rootHistory.length === manifestHashes.length;
|
|
134
|
-
}
|
|
135
|
-
catch (error) {
|
|
136
|
-
console.error(`Failed to check sync status: ${error.message}`);
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
39
|
+
static sendEqualBulkPayments(walletName, addresses, totalAmount, memos) {
|
|
40
|
+
// Use a Set to ensure unique addresses
|
|
41
|
+
const uniqueAddresses = Array.from(new Set(addresses));
|
|
42
|
+
// Convert unique addresses to puzzle hashes
|
|
43
|
+
const puzzleHashes = uniqueAddresses.map((address) => (0, datalayer_driver_1.addressToPuzzleHash)(address));
|
|
44
|
+
// Calculate amount per puzzle hash
|
|
45
|
+
const amountPerPuzzleHash = totalAmount / BigInt(puzzleHashes.length);
|
|
46
|
+
// Create outputs array
|
|
47
|
+
const outputs = puzzleHashes.map((puzzleHash) => ({
|
|
48
|
+
puzzleHash,
|
|
49
|
+
amount: amountPerPuzzleHash,
|
|
50
|
+
memos,
|
|
51
|
+
}));
|
|
52
|
+
// Call the sendBulkPayments function with the generated outputs
|
|
53
|
+
return DigPeer.sendBulkPayments(walletName, outputs);
|
|
139
54
|
}
|
|
140
|
-
async
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
const fee = BigInt(1000);
|
|
55
|
+
static async sendBulkPayments(walletName, outputs) {
|
|
56
|
+
const feePerCondition = BigInt(1000);
|
|
57
|
+
const totalFee = feePerCondition * BigInt(outputs.length);
|
|
144
58
|
const wallet = await blockchain_3.Wallet.load(walletName);
|
|
145
59
|
const publicSyntheticKey = await wallet.getPublicSyntheticKey();
|
|
146
60
|
const peer = await blockchain_2.FullNodePeer.connect();
|
|
147
|
-
const
|
|
148
|
-
const
|
|
149
|
-
const
|
|
150
|
-
{
|
|
151
|
-
puzzleHash: paymentAddressPuzzleHash,
|
|
152
|
-
amount: amount,
|
|
153
|
-
},
|
|
154
|
-
];
|
|
155
|
-
const coinSpends = await (0, datalayer_driver_1.sendXch)(publicSyntheticKey, coins, outputs, fee);
|
|
61
|
+
const totalAmount = outputs.reduce((acc, output) => acc + output.amount, BigInt(0));
|
|
62
|
+
const coins = await (0, coins_1.selectUnspentCoins)(peer, totalAmount, totalFee, [], walletName);
|
|
63
|
+
const coinSpends = await (0, datalayer_driver_1.sendXch)(publicSyntheticKey, coins, outputs, totalFee);
|
|
156
64
|
const sig = (0, datalayer_driver_1.signCoinSpends)(coinSpends, [await wallet.getPrivateSyntheticKey()], false);
|
|
157
65
|
const err = await peer.broadcastSpend(coinSpends, [sig]);
|
|
158
66
|
if (err) {
|
|
@@ -160,5 +68,51 @@ class DigPeer {
|
|
|
160
68
|
}
|
|
161
69
|
await blockchain_2.FullNodePeer.waitForConfirmation((0, datalayer_driver_1.getCoinId)(coins[0]));
|
|
162
70
|
}
|
|
71
|
+
async sendPayment(walletName, amount, memos = []) {
|
|
72
|
+
const paymentAddress = await this.contentServer.getPaymentAddress();
|
|
73
|
+
const paymentAddressPuzzleHash = (0, datalayer_driver_1.addressToPuzzleHash)(paymentAddress);
|
|
74
|
+
const output = {
|
|
75
|
+
puzzleHash: paymentAddressPuzzleHash,
|
|
76
|
+
amount,
|
|
77
|
+
memos,
|
|
78
|
+
};
|
|
79
|
+
return DigPeer.sendBulkPayments(walletName, [output]);
|
|
80
|
+
}
|
|
81
|
+
static createPaymentHint(storeId) {
|
|
82
|
+
// Ensure the input is a 32-byte buffer
|
|
83
|
+
if (!Buffer.isBuffer(storeId) || storeId.length !== 32) {
|
|
84
|
+
throw new Error("Invalid input. Must be a 32-byte buffer.");
|
|
85
|
+
}
|
|
86
|
+
// Define the seed
|
|
87
|
+
const seed = "digpayment";
|
|
88
|
+
// Combine the seed and the original buffer
|
|
89
|
+
const combinedBuffer = Buffer.concat([Buffer.from(seed), storeId]);
|
|
90
|
+
// Apply SHA-256 hash to the combined buffer
|
|
91
|
+
const hash = crypto_1.default.createHash("sha256");
|
|
92
|
+
hash.update(combinedBuffer);
|
|
93
|
+
const transformedBuffer = hash.digest();
|
|
94
|
+
// Return the 32-byte hash as a hex string
|
|
95
|
+
return transformedBuffer;
|
|
96
|
+
}
|
|
97
|
+
async syncStore() {
|
|
98
|
+
const dataStore = blockchain_1.DataStore.from(this.storeId);
|
|
99
|
+
const rootHistory = await dataStore.getRootHistory();
|
|
100
|
+
const localRootHistory = rootHistory
|
|
101
|
+
.filter((root) => Boolean(root.synced))
|
|
102
|
+
.reverse();
|
|
103
|
+
console.log(`Syncing store ${this.storeId} with ${this.IpAddress}`);
|
|
104
|
+
for (const item of localRootHistory) {
|
|
105
|
+
await this.pushStoreRoot(this.storeId, item.root_hash);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async pushStoreRoot(storeId, rootHash) {
|
|
109
|
+
await PropagationServer_1.PropagationServer.uploadStore(storeId, rootHash, this.IpAddress);
|
|
110
|
+
}
|
|
111
|
+
async downloadStoreRoot(rootHash) {
|
|
112
|
+
await PropagationServer_1.PropagationServer.downloadStore(this.storeId, rootHash, this.IpAddress);
|
|
113
|
+
}
|
|
114
|
+
async downloadData(dataPath) {
|
|
115
|
+
await this.propagationServer.downloadFile(dataPath);
|
|
116
|
+
}
|
|
163
117
|
}
|
|
164
118
|
exports.DigPeer = DigPeer;
|