-
Notifications
You must be signed in to change notification settings - Fork 0
/
provendbShell.js
executable file
·597 lines (549 loc) · 16.3 KB
/
provendbShell.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
# provendbShell - wrapper around mongo shell for Provendb
# Copyright (C) 2019 Southbank Software Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# @Author: Guy Harrison
#
*/
/* eslint no-var: 0 */
/* eslint no-unused-vars: 0 */
/* eslint prefer-arrow-callback: 0 */
/* eslint object-shorthand: 0 */
/* eslint vars-on-top: 0 */
/* eslint prefer-destructuring: 0 */
/* eslint no-loop-func: 0 */
/* eslint no-plusplus: 0 */
var provendbShell = {};
provendbShell.init = (pdb) => {
// Get bulk load status
pdb.bulkLoadStatus = () => {
return (pdb.runCommand({
bulkLoad: 'status'
}));
};
// Start a bulk load
pdb.bulkLoadStart = () => {
return (pdb.runCommand({
bulkLoad: 'start'
}));
};
// Stop a bulk load
pdb.bulkLoadStop = () => {
return (pdb.runCommand({
bulkLoad: 'stop'
}));
};
// Kill a bulk load
pdb.bulkLoadKill = () => {
return (pdb.runCommand({
bulkLoad: 'kill'
}));
};
// Wrapper around compact
pdb.compact = (...args) => {
const usage = 'Usage: pdb.compact(startVersion,endVersion)';
if (args.length !== 2) {
return usage;
}
const compactArgs = {
compact: {
startVersion: args[0],
endVersion: args[1]
}
};
if (provendbShell.debug) {
printjson(compactArgs);
}
return pdb.runCommand(compactArgs);
};
// Get current version
pdb.currentVersion = () => {
var cv = db
.getCollection('_provendb_currentVersion')
.findOne();
var now = new Date();
printjson(cv);
if (cv.started) {
var started = new Date(cv.started.t * 1000);
print('started time', started, (now - started) / 1000, 'sec ago');
}
if (cv.valid) {
var valid = new Date(cv.valid.t * 1000);
print('valid time', valid, (now - valid) / 1000, 'sec ago');
}
};
// Return the db object
pdb.db = () => {
return db;
};
// Wrapper around docHistory
pdb.docHistory = (collection, filter) => {
return (pdb.runCommand({
docHistory: {
collection,
filter
}
}));
};
// Execute a forget
pdb.executeForget = (...args) => {
const usage = 'Usage: db.executeForget(forgetId,password)';
if (args.length !== 2) {
return usage;
}
const forgetId = args[0];
const password = args[1];
const forgetArgs = {
forget: {
execute: {
forgetId,
password
}
}
};
if (provendbShell.debug) {
printjson(forgetArgs);
}
return pdb.runCommand(forgetArgs);
};
// Wrapper around document proof
pdb.getDocumentProof = (...args) => {
const usage = 'pdb.getDocumentProof(collection,filter[,version,format])';
let format = 'binary';
let version = 0;
if (args.length < 2 || args.length > 4) {
return usage;
}
if (args.length > 3) {
format = args[3];
}
if (args.length > 2) {
version = args[2];
} else {
version = pdb
.runCommand({
getVersion: 1
})
.version + 0;
}
const collection = args[0];
const filter = args[1];
const getDocumentProofArgs = {
getDocumentProof: {
collection,
filter,
version,
format
}
};
if (provendbShell.debug) {
printjson(getDocumentProofArgs);
}
return pdb.runCommand(getDocumentProofArgs);
};
// Wrapper arond getProof
pdb.getProof = (...args) => {
const usage = 'Usage: pdb.getProof(proofId|versionNo[,"binary"|"json",listCollections(true|false)])';
let format = 'binary';
const getProofsArgs = {};
let listCollections = false;
if (args.length > 2) {
listCollections = args[2];
}
if (args.length > 1) {
format = args[1];
}
if (args.length === 0) {
return usage;
}
const proofId = args[0];
getProofArgs = {
getProof: proofId,
format,
listCollections
};
if (provendbShell.debug) {
printjson(getProofArgs);
}
return pdb.runCommand(getProofArgs);
};
pdb.proofStatus = () =>{
const output = {};
db.getCollection('_provendb_versionProofs')
.find({}, {})
.forEach((p) => {
// print(p.status);
if (!(p.status in output)) {
output[p.status] = 1;
} else {
output[p.status] += 1;
}
});
return output;
}
// Get a proof for a particular verison
pdb.getProofForVersion = (versionId) => {
const gp4v = db
.getCollection('_provendb_versionProofs')
.find({
versionId: {
$gte: versionId
}
})
.sort({
submitted: 1
})
.limit(1);
return (gp4v);
};
// Get either the effective version or a version for a date
pdb.getVersion = (versionDate) => {
let returnObj = {};
if (versionDate) { // turn input date into timestamp
const myTimestamp = new Timestamp(Math.round(versionDate / 1000), 0);
const versionData = pdb
.getCollection('_provendb_versions').find({
ended: {
$lt: myTimestamp
}
}, {
version: 1,
_id: 0
}).sort({
ended: -1
}).limit(1).toArray();
if (versionData.length === 0) {
returnObj = {
ok: 1,
response: 'No version for date ' + versionDate,
version: null,
status: 'No version'
};
} else {
returnObj = {
ok: 1,
response: 'Version current at ' + versionDate,
version: versionData[0].version,
status: 'History'
};
}
} else {
returnObj = pdb.runCommand({
getVersion: 1
});
}
return returnObj;
};
// hide metadata
pdb.hideMetaData = () => {
return pdb.runCommand({
showMetaData: false
});
};
// Change the log level
pdb.logLevel = (level) => {
return (pdb.runCommand({
'setLogLevel': level
}));
};
// List most recent proofs
pdb.listProofs = (limit) => {
if (!limit) {
limit = 1;
}
return pdb
.getCollection('_provendb_versionProofs')
.find({}, {
_id: 0,
hash: 0,
proof: 0,
details: 0
})
.sort({
submitted: -1
})
.limit(limit)
.pretty();
};
// List provendb commands
pdb.listProvenDBCommands = () => {
return pdb.runCommand({
listProvenDBCommands: 1
});
};
// Wrapper around listStorage
pdb.listStorage = () => {
return pdb.runCommand({
listStorage: 1
});
};
// Wrapper around listVersions
pdb.listVersions = (...args) => {
const usage = 'db.listVersions([startDate,endDate,limit,sortDirection])';
let sortDirection = -1;
let limit = 10;
let endDate = new Date();
let startDate = new Date(new Date() - (10 * 365 * 24 * 3600 * 1000));
const output = [];
let listVersionsDoc = {};
if (args.length > 5) {
return usage;
}
if (args.length === 1 && typeof args[0] === 'number') {
listVersionsDoc = {
limit: args[0]
};
} else {
if (args.length > 3) {
sortDirection = args[3];
}
if (args.length > 2) {
limit = args[2];
}
if (args.length > 1) {
endDate = args[1];
}
if (args.length > 0) {
startDate = args[0];
}
listVersionsDoc = {
startDate,
endDate,
limit,
sortDirection
};
}
return (db.runCommand({
listVersions: listVersionsDoc
}));
};
// wrapper around showMetadata
pdb.metadata = (metadata) => {
pdb.runCommand({
'showMetadata': metadata
});
};
// Prepare a forget
pdb.prepareForget = (...args) => {
const usage = 'Usage: pdb.prepareForget(collection,filter [,minVersion,maxVersion,inclusiveRang' +
'e(true|false)])';
if (args.length < 2 || args.length > 5) {
return usage;
}
const collection = args[0];
const filter = args[1];
const forgetArgs = {
forget: {
prepare: {
collection,
filter
}
}
};
if (args.length > 2) {
forgetArgs.forget.prepare.minVersion = args[2];
}
if (args.length > 3) {
forgetArgs.forget.prepare.maxVersion = args[3];
}
if (args.length > 4) {
forgetArgs.forget.prepare.inclusiveRange = args[4];
}
if (provendbShell.debug) {
printjson(forgetArgs);
}
return pdb.runCommand(forgetArgs);
};
// Wrapper around rollback
pdb.rollback = () => {
return db.runCommand({
rollback: 1
});
};
// Set version to current
pdb.setCurrent = () => {
return db
.runCommand({
setVersion: 'current'
})
.version + 0;
};
// set the effective version
pdb.setVersion = (version) => {
if (!version) {
version = 'current';
}
return db
.runCommand({
setVersion: version
});
};
// show metadata
pdb.showMetaData = () => {
return pdb.runCommand({
showMetaData: true
});
};
// submit a proof
pdb.submitProof = (...args) => {
// submitProof([version],[argsDocument]) Examples: submitProof() - submit
// current version submitProof(23) - submit version 23
// submitProof({collections:'foo'}) - current version collection foo
// submitProof({23,{collections:'foo}}) - version 23, collection: foo
//
let submitArgs = {
submitProof: 0
};
const usage = 'Usage: pdb.submitProof([version],[argumentsDocument])';
if (args.length === 0) { // No arguments so submit Proof for current version
submitArgs = {
submitProof: pdb.pdbVersion()
};
} else if (args.length === 1) { // One argument
if (typeof args[0] === 'object') { // Single argument is a document
submitArgs.submitProof = pdb.pdbVersion(); // Use current version
Object.assign(submitArgs, args[0]);
} else if (typeof args[0] === 'number') { // Single argument is a number
submitArgs.submitProof = args[0];
} else {
print(usage); // Invalid usage
return null;
}
} else if (args.length === 2) { // Two arguments
if (typeof args[1] !== 'object' || typeof args[0] !== 'number') {
print(usage);
return null;
}
submitArgs.submitProof = args[0];
Object.assign(submitArgs, args[1]);
} else {
print(usage);
return null;
}
if (provendbShell.debug) {
printjson(submitArgs);
}
return pdb.runCommand(submitArgs);
};
// Wrapper around verify proof
pdb.verifyProof = (...args) => {
const usage = 'db.verifyProof([proofId|versionId,format])';
let versionId;
let proofId;
let format = 'binary';
if (args.length > 2) return usage;
if (args.length > 0) {
if (typeof args[0] === 'number') { // We have been supplied with a version, not a proofId
proofId = pdb.bestProofForVersion(args[0]);
if (proofId === null) return 'No proof exists';
} else { // First argument is a proof id
proofId = args[0];
}
}
if (args.length > 1) format = args[1];
return pdb.runCommand({
verifyProof: proofId,
format
});
};
// Return best proof for a version
pdb.bestProofForVersion = (versionId) => {
const proofs = db.getCollection('_provendb_versionProofs').find({
version: versionId,
status: 'valid',
scope: {
$ne: 'collection'
}
}).sort({
submitted: 1
}).limit(1).toArray();
if (proofs.length === 0) {
return null;
}
return proofs[0].proofId;
};
pdb.pdbVersion = () => {
const rc = pdb.getVersion();
if ('version' in rc) {
return rc.version + 0;
}
return rc;
};
pdb.closestProofForVersion = (versionId) => {
const closestProof = db.getCollection('_provendb_versionProofs').find({
version: {
$gte: versionId
},
status: 'valid'
}, {
proofId: 1
}).sort({
version: 1
}).limit(1).toArray();
if (closestProof.length > 0 && 'proofId' in closestProof[0]) {
// looks good
return closestProof[0].proofId;
} // Use that closest proof
return null;
};
};
provendbShell.debug = false;
provendbShell.init(db);
// Redefine the shell prompt
var prompt = () => {
var myPrompt;
var status;
var cv = db.runCommand({
getVersion: 1
});
if (cv.status === 'userDefined') {
status = 'history';
} else {
status = cv.status;
}
myPrompt = 'ProvenDB v' + cv.version + ' (' + status + ')> ';
return myPrompt;
};
var waitForProof = proofId => {
let proofValid = false;
let start = new Date();
let submitted=false;
do {
let getProof = db.runCommand({ getProof: proofId });
let status = getProof.proofs[0].status;
print(status);
if (status==='Submitted' && !submitted) {
submitted=true;
printjson(getProof);
}
if (status !== 'Submitted' && status !== 'Pending' && status !== 'Confirmed') proofValid = true;
else sleep(10000);
let elapsed = new Date() - start;
if (elapsed > 1500000) {
print('Timed out waiting for proof to confirm');
break;
}
} while (!proofValid);
};
var submitAndWait= (version,opts) => {
let start=new Date();
let proof=db.submitProof(version,opts);
let gp=db.getProof(proof.proofId);
printjson(gp);
waitForProof(proof.proofId);
printjson(db.getProof(proof.proofId));
print('Elapsed time ',(new Date()-start));
}