-
Notifications
You must be signed in to change notification settings - Fork 3
/
handlers.js
503 lines (425 loc) · 13.1 KB
/
handlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
const _ = require("lodash");
const fs = require("fs-extra");
const path = require("path");
const changeListGenerator = require("chia-changelist-generator");
const wallet = require("./rpcs/wallet");
const { encodeHex } = require("./utils/hex-utils");
const Datalayer = require("chia-datalayer");
const datalayerMirror = require("chia-datalayer-mirror-tools");
const {
getConfig,
CONFIG_FILENAME,
DEFAULT_CONFIG,
} = require("./utils/config-loader");
const { checkForNewerVersion } = require("./utils/version-utils");
const {
checkChiaConfigIpHost,
checkFilePropagationServerReachable,
} = require("./utils/connectivity-utils");
const { logInfo, logError } = require("./utils/console-tools");
const { log } = require("console");
async function generateCleanUpChangeList() {
const config = getConfig();
const datalayer = new Datalayer(config);
const fileList = await datalayer.getKeys({ id: config.store_id });
// Read the contents of the directory
const filesInDir = await fs.readdir(config.deploy_dir);
// Convert filenames in the directory to hexadecimal and create a set for faster lookup
const filesInDirHex = new Set(
filesInDir.map((fileName) => encodeHex(fileName))
);
// Filter out files that exist in the directory
const filteredFileList = fileList.keys
.filter((key) => {
const hexKey = key.replace("0x", "");
return !filesInDirHex.has(hexKey);
})
.map((key) => ({ key: key.replace("0x", "") }));
const cleanUpChangeList = await changeListGenerator.generateChangeList(
config.store_id,
"delete",
filteredFileList,
{ chunkChangeList: true }
);
return cleanUpChangeList;
}
async function deployHandler() {
try {
await checkForNewerVersion();
await checkChiaConfigIpHostHandler();
await checkFilePropagationServerReachableHandler();
const config = getConfig();
if (config.store_id === null) {
logError(
"A store_id is not defined in sprout.config.json. Please run 'create-store' command to create a new store."
);
return;
}
if (!(await wallet.walletIsSynced())) {
logError(
"The wallet is not synced. Please wait for it to sync and try again."
);
return;
}
if (!fs.existsSync(config.deploy_dir)) {
logError(
`The directory "${config.deploy_dir}" specified in sprout.config.json does not exist. Please specify a valid directory.`
);
return;
}
changeListGenerator.configure(config);
const datalayer = new Datalayer(config);
if (!config.ignore_orphans) {
const cleanUpChangeList = await generateCleanUpChangeList();
let cleanupCounter = 1;
logInfo("Cleaning up orphaned files.");
for (const chunk of cleanUpChangeList) {
logInfo(
`Sending cleanup chunk #${cleanupCounter} of ${
cleanUpChangeList.length
} to datalayer. Size ${Buffer.byteLength(
JSON.stringify(chunk),
"utf-8"
)}`
);
await datalayer.updateDataStore({
id: config.store_id,
changelist: _.flatten(chunk),
submit_on_chain: false
});
cleanupCounter++;
}
}
await datalayer.submitPendingRoot({
id: config.store_id,
});
await walkDirAndCreateFileList(config.deploy_dir, config.store_id);
await datalayer.submitPendingRoot({
id: config.store_id,
});
logInfo("Deploy operation completed successfully.");
} catch (error) {
console.trace(error);
logError(error.message);
} finally {
process.exit();
}
}
async function initHandler() {
try {
await checkForNewerVersion();
if (fs.existsSync(CONFIG_FILENAME)) {
logInfo(
"A sprout.config.json file already exists in the current directory."
);
} else {
fs.writeJsonSync(CONFIG_FILENAME, DEFAULT_CONFIG, { spaces: 2 });
logInfo(
"A new sprout.config.json file has been created with the default configuration."
);
}
} catch (error) {
logError(error.message);
} finally {
process.exit();
}
}
async function mirrorStoreHandler() {
try {
await checkForNewerVersion();
await wallet.waitForAllTransactionsToConfirm();
const config = getConfig();
if (!config.store_id) {
logError("No store_id specified in sprout.config.json");
return;
}
if (!_.isNil(config.mirror_url_override)) {
const datalayer = new Datalayer(config);
const response = await datalayer.addMirror({
id: config.store_id,
urls: [config.mirror_url_override],
amount: config.default_mirror_coin_amount,
fee: config.default_fee,
});
if (response.success === false) {
logError("Failed to add mirror");
return;
}
} else {
datalayerMirror.configure(config);
const response = await datalayerMirror.addMirrorForCurrentHost(
config.store_id,
config.forceIp4Mirror || false
);
if (response.success === false) {
logError("Failed to add mirror");
return;
}
}
await wallet.waitForAllTransactionsToConfirm();
logInfo("Mirror added successfully");
} catch (error) {
console.trace(error);
logError(error.message);
} finally {
process.exit();
}
}
async function createStoreHandler(isNew = false) {
try {
await checkForNewerVersion();
const config = getConfig();
if (!isNew && config.store_id !== null) {
logError(
"A store_id already exists in sprout.config.json, use the -new flag if you want to overwrite it."
);
return;
}
if (!(await wallet.walletIsSynced())) {
logError(
"The wallet is not synced, please wait for it to sync and try again"
);
return;
}
logInfo("Creating new store, please wait for the transaction to complete");
const datalayer = new Datalayer(config);
const response = await datalayer.createDataStore();
if (!response.success) {
logError("Failed to create new store");
return;
}
const storeId = response.id;
await wallet.waitForAllTransactionsToConfirm();
fs.writeJsonSync(
CONFIG_FILENAME,
{ ...config, store_id: storeId },
{ spaces: 2 }
);
logInfo(`Created new store with id ${storeId}`);
} catch (error) {
console.trace(error);
logError(error.message);
} finally {
process.exit();
}
}
async function cleanStoreHandler() {
try {
await checkForNewerVersion();
const config = getConfig();
if (config.store_id === null) {
logError("No store_id specified in sprout.config.json");
return;
}
if (!(await wallet.walletIsSynced())) {
logError("The wallet is not synced. Please sync and try again.");
return;
}
const datalayer = new Datalayer(config);
changeListGenerator.configure(config);
const fileList = await datalayer.getKeys({ id: config.store_id });
const chunkedChangelist = await changeListGenerator.generateChangeList(
config.store_id,
"delete",
fileList.keys.map((key) => ({ key })),
{ chunkChangeList: true }
);
logInfo(
`Deleting items from datalayer in ${chunkedChangelist.length} transaction(s).`
);
for (const chunk of chunkedChangelist) {
await datalayer.updateDataStore({
id: config.store_id,
changelist: chunk,
submit_on_chain: false
});
}
logInfo("Done deleting items from store.");
} catch (error) {
console.trace(error);
logError(error.message);
} finally {
process.exit();
}
}
async function walkDirAndCreateFileList(
dirPath,
storeId,
existingKeys,
rootDir = dirPath
) {
const files = fs.readdirSync(dirPath);
let fileList = [];
const config = getConfig();
changeListGenerator.configure(config);
const datalayer = new Datalayer(config);
const batchSize = config.num_files_processed_per_batch ?? 100;
for (let i = 0; i < files.length; i += batchSize) {
const fileBatch = files.slice(i, i + batchSize);
for (const file of fileBatch) {
const filePath = path.join(dirPath, file);
if (fs.statSync(filePath).isDirectory()) {
const subdirChangeList = await walkDirAndCreateFileList(
filePath,
storeId,
existingKeys,
rootDir
);
fileList.push(...subdirChangeList);
} else {
const relativeFilePath = path.relative(rootDir, filePath);
const fileSize = fs.statSync(filePath).size;
// 1 MB in bytes is 1024 * 1024 bytes
const oneMB = 1024 * 1024; // Bytes
const chunkSize = config.maximum_rpc_payload_size / 2 - oneMB;
if (fileSize > chunkSize) {
changeListGenerator.configure(config);
const datalayer = new Datalayer(config);
// If file size is more than 22 MB, read in chunks
const fileStream = fs.createReadStream(filePath, {
highWaterMark: chunkSize,
});
let index = 1;
let chunkKeys = [];
for await (const chunk of fileStream) {
const chunkKey = `${relativeFilePath.replace(
/\\/g,
"/"
)}.part${index}`;
chunkKeys.push(chunkKey);
// Push the chunks while we are processing them so we can keep the mempry footprint down for extremely large files
const partialFileChangeList =
await changeListGenerator.generateChangeList(
config.store_id,
"upsert",
[
{
key: encodeHex(chunkKey),
value: chunk.toString("hex"),
},
],
{ chunkChangeList: false }
);
console.log(
`Pushing partial file to datalayer: ${chunkKey} - ${Buffer.byteLength(
chunk,
"utf-8"
)}`
);
await datalayer.updateDataStore({
id: config.store_id,
changelist: partialFileChangeList,
submit_on_chain: false
});
index++;
}
fileList.push({
key: encodeHex(relativeFilePath.replace(/\\/g, "/")),
value: encodeHex(
JSON.stringify({
type: "multipart",
parts: chunkKeys,
})
),
});
} else {
// If file size is 22 MB or less, read the entire file
const contentBuffer = fs.readFileSync(filePath);
content = contentBuffer.toString("hex");
fileList.push({
key: encodeHex(relativeFilePath.replace(/\\/g, "/")),
value: content,
});
}
}
}
// Process the current batch
if (fileList.length > 0) {
const chunkedChangelist = await changeListGenerator.generateChangeList(
storeId,
"upsert",
fileList,
{ chunkChangeList: true }
);
let chunkCounter = 1;
for (const chunk of chunkedChangelist) {
logInfo(
`Sending chunk #${chunkCounter} of ${
chunkedChangelist.length
} to datalayer. Size ${Buffer.byteLength(
JSON.stringify(chunk),
"utf-8"
)}`
);
await datalayer.updateDataStore({
id: storeId,
changelist: chunk,
submit_on_chain: false
});
chunkCounter++;
}
}
fileList = [];
}
return fileList;
}
async function checkChiaConfigIpHostHandler() {
try {
const result = await checkChiaConfigIpHost();
console.log(`Chia Config IP Host Check: ${result}`);
return result;
} catch (error) {
console.error(
"Error in Chia Config IP Host Check. Details:",
error.message
);
console.error(error.stack);
return false;
}
}
async function checkFilePropagationServerReachableHandler() {
let attempts = 0;
const maxAttempts = 3; // Retry logic, if applicable
while (attempts < maxAttempts) {
try {
const result = await checkFilePropagationServerReachable();
console.log(`File Propagation Server Reachability Check: ${result}`);
return true;
} catch (error) {
attempts++;
console.error(
`Attempt ${attempts} - Error in File Propagation Server Reachability Check. Details:`,
error.message
);
console.error(error.stack); // More detailed error logging
if (attempts >= maxAttempts) {
console.error("Max attempts reached. Failing gracefully.");
return false;
}
}
}
}
async function runConnectionCheckHandler() {
try {
await checkForNewerVersion();
const configSetup = await checkChiaConfigIpHostHandler();
const ipVisible = await checkFilePropagationServerReachableHandler();
if (configSetup && ipVisible) {
logInfo(`Congrats! Your Local Mirror is Discoverable`);
} else {
logError(`Oh No! Your Local Mirror is Not Discoverable`);
}
} finally {
process.exit();
}
}
module.exports = {
deployHandler,
initHandler,
createStoreHandler,
cleanStoreHandler,
checkChiaConfigIpHostHandler,
checkFilePropagationServerReachableHandler,
runConnectionCheckHandler,
mirrorStoreHandler,
};