forked from hive-engine/steemsmartcontracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.js
1074 lines (944 loc) · 35.4 KB
/
Database.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable max-len */
/* eslint-disable no-await-in-loop */
const SHA256 = require('crypto-js/sha256');
const enchex = require('crypto-js/enc-hex');
const log = require('loglevel');
const validator = require('validator');
const { MongoClient } = require('mongodb');
const { EJSON } = require('bson');
const { CONSTANTS } = require('../libs/Constants');
// Change this to turn on hash logging.
const enableHashLogging = false;
function validateIndexName(indexName) {
if (typeof indexName !== 'string') {
return false;
}
const indexNameParts = indexName.split('.');
return indexNameParts.every(p => p.length > 0 && validator.isAlphanumeric(p));
}
function validateIndexSpec(spec) {
if (typeof spec === 'string') return validateIndexName(spec);
if (typeof spec === 'object') {
return spec.name && validator.isAlphanumeric(spec.name) && typeof spec.index === 'object'
&& Object.keys(spec.index).every(indexName => validateIndexName(indexName))
&& Object.values(spec.index).every(asc => asc === 1 || asc === -1);
}
return false;
}
async function indexInformation(tableData) {
try {
return await tableData.indexInformation();
} catch (err) {
// Fails if collection was created in the same
// Mongo transaction. Proceed without indexes.
return {};
}
}
function objectCacheKey(contract, table, object) {
if (contract === 'mining' && table === 'miningPower') {
return `${contract}_${table}_${object.id}_${object.account}`;
}
return null;
}
function adjustQueryForPrimaryKey(query, customPrimaryKey) {
const primaryKeyQuery = {};
let usePrimaryKey = true;
customPrimaryKey.forEach((k) => {
if (k in query) {
primaryKeyQuery[k] = query[k];
} else {
usePrimaryKey = false;
}
});
if (usePrimaryKey) {
query._id = primaryKeyQuery; // eslint-disable-line no-underscore-dangle, no-param-reassign
}
}
class Database {
constructor() {
this.database = null;
this.chain = null;
this.databaseHash = '';
this.client = null;
this.session = null;
this.contractCache = {};
this.objectCache = {};
this.lightNode = false;
this.blocksToKeep = 864000; // this only applies if lightNode is true
}
startSession() {
this.session = this.client.startSession();
this.contractCache = {};
this.objectCache = {};
return this.session;
}
async endSession() {
if (this.session) {
await this.session.endSession();
this.session = null;
}
}
async initSequence(name, startID = 1) {
const sequences = this.database.collection('sequences');
await sequences.insertOne({ _id: name, seq: startID }, { session: this.session });
}
async getNextSequence(name) {
const sequences = this.database.collection('sequences');
const sequence = await sequences.findOneAndUpdate(
{ _id: name }, { $inc: { seq: 1 } }, { new: true, session: this.session },
);
return sequence.value.seq;
}
async getLastSequence(name) {
const sequences = this.database.collection('sequences');
const sequence = await sequences.findOne({ _id: name }, { session: this.session });
return sequence.seq;
}
async getContractCollection(contract, name) {
const contractInDb = await this.findContract({ name: contract });
if (contractInDb && contractInDb.tables[name] !== undefined) {
return this.database.collection(name);
}
return null;
}
getCollection(name) {
return new Promise((resolve) => {
this.database.collection(name, { strict: true }, (err, collection) => {
// collection does not exist
if (err) {
resolve(null);
} else {
resolve(collection);
}
});
});
}
async init(databaseURL, databaseName, lightNode = false, blocksToKeep = 864000) {
// init the database
this.client = await MongoClient.connect(databaseURL, { useNewUrlParser: true, useUnifiedTopology: true });
this.database = await this.client.db(databaseName);
this.lightNode = lightNode;
this.blocksToKeep = blocksToKeep; // this only applies if lightNode is true
// await database.dropDatabase();
// return
// get the chain collection and init the chain if not done yet
const coll = await this.getCollection('chain');
if (coll === null) {
await this.initSequence('chain', 0);
this.chain = await this.database.createCollection('chain', { session: this.session });
await this.database.createCollection('transactions', { session: this.session });
await this.database.createCollection('contracts', { session: this.session });
} else {
this.chain = coll;
}
const contractsConfigColl = await this.getCollection('contracts_config');
if (contractsConfigColl === null) {
const newContractsConfigColl = await this.database.createCollection('contracts_config', { session: this.session });
// WARNING: Do not add any more entries to this initial configuration.
// Future contracts must use the contract action 'registerTick'.
await newContractsConfigColl.insertOne({
contractTicks: CONSTANTS.INITIAL_CONTRACT_TICKS,
}, { session: this.session });
}
}
close() {
this.client.close();
}
async insertGenesisBlock(genesisBlock) {
// eslint-disable-next-line
genesisBlock._id = await this.getNextSequence('chain');
await this.chain.insertOne(genesisBlock, { session: this.session });
}
async addTransactions(block) {
const transactionsTable = this.database.collection('transactions');
const { transactions } = block;
const nbTransactions = transactions.length;
for (let index = 0; index < nbTransactions; index += 1) {
const transaction = transactions[index];
const transactionToSave = {
_id: transaction.transactionId,
blockNumber: block.blockNumber,
index,
};
await transactionsTable.insertOne(transactionToSave, { session: this.session }); // eslint-disable-line no-await-in-loop
}
}
async updateTableHash(contract, table) {
const contractInDb = await this.findContract({ name: contract });
if (contractInDb && contractInDb.tables[table] !== undefined) {
const tableHash = contractInDb.tables[table].hash;
contractInDb.tables[table].hash = SHA256(tableHash).toString(enchex);
const oldDatabaseHash = this.databaseHash;
this.databaseHash = SHA256(this.databaseHash + contractInDb.tables[table].hash)
.toString(enchex);
if (enableHashLogging) {
log.info(`updated hash of ${table} to ${contractInDb.tables[table].hash}`); // eslint-disable-line no-console
log.info(`updated db hash from ${oldDatabaseHash} to ${this.databaseHash}`); // eslint-disable-line no-console
}
}
}
initDatabaseHash(previousDatabaseHash) {
this.databaseHash = previousDatabaseHash;
}
getDatabaseHash() {
return this.databaseHash;
}
async getTransactionInfo(txid) {
const transactionsTable = this.database.collection('transactions');
const transaction = await transactionsTable.findOne({ _id: txid }, { session: this.session });
let result = null;
if (transaction) {
const { index, blockNumber } = transaction;
const block = await this.getBlockInfo(blockNumber);
if (block) {
result = Object.assign({}, { blockNumber }, block.transactions[index]);
}
}
return result;
}
async addBlock(block) {
const finalBlock = block;
finalBlock._id = await this.getNextSequence('chain'); // eslint-disable-line no-underscore-dangle
await this.chain.insertOne(finalBlock, { session: this.session });
await this.addTransactions(finalBlock);
}
async noteHashChange(refHiveBlockNumber) {
const lastBlock = await this.getLatestBlockInfo();
if (!lastBlock.otherHashChangeRefHiveBlocks) {
lastBlock.otherHashChangeRefHiveBlocks = [];
}
lastBlock.otherHashChangeRefHiveBlocks.push(refHiveBlockNumber);
await this.chain.updateOne({ _id: lastBlock._id }, { $set: { otherHashChangeRefHiveBlocks: lastBlock.otherHashChangeRefHiveBlocks } }, { session: this.session }); // eslint-disable-line no-underscore-dangle
}
async getLatestBlockInfo() {
try {
const _idNewBlock = await this.getLastSequence('chain'); // eslint-disable-line no-underscore-dangle
const latestBlock = await this.chain.findOne({ _id: _idNewBlock - 1 }, { session: this.session });
return latestBlock;
} catch (error) {
// eslint-disable-next-line no-console
log.error(error);
return null;
}
}
async getLatestBlockMetadata() {
try {
const _idNewBlock = await this.getLastSequence('chain'); // eslint-disable-line no-underscore-dangle
const latestBlock = await this.chain.findOne({ _id: _idNewBlock - 1 }, { session: this.session });
if (latestBlock) {
latestBlock.transactions = [];
latestBlock.virtualTransactions = [];
}
return latestBlock;
} catch (error) {
// eslint-disable-next-line no-console
log.error(error);
return null;
}
}
async getBlockInfo(blockNumber) {
try {
const block = typeof blockNumber === 'number' && Number.isInteger(blockNumber)
? await this.chain.findOne({ _id: blockNumber }, { session: this.session })
: null;
return block;
} catch (error) {
// eslint-disable-next-line no-console
log.error(error);
return null;
}
}
/**
* Mark a block as verified by a witness
* @param {Integer} blockNumber block umber to mark verified
* @param {String} witness name of the witness that verified the block
*/
async verifyBlock(payload) {
try {
const {
blockNumber,
witness,
roundSignature,
signingKey,
round,
roundHash,
} = payload;
const block = await this.chain.findOne({ _id: blockNumber }, { session: this.session });
if (block) {
block.witness = witness;
block.round = round;
block.roundHash = roundHash;
block.signingKey = signingKey;
block.roundSignature = roundSignature;
await this.chain.updateOne(
{ _id: block._id }, // eslint-disable-line no-underscore-dangle
{ $set: block }, { session: this.session },
);
} else {
// eslint-disable-next-line no-console
log.error('verifyBlock', blockNumber, 'does not exist');
}
} catch (error) {
// eslint-disable-next-line no-console
log.error(error);
}
}
/**
* Get the information of a contract (owner, source code, etc...)
* @param {String} contract name of the contract
* @returns {Object} returns the contract info if it exists, null otherwise
*/
async findContract(payload) {
const { name } = payload;
if (this.session && this.contractCache[name]) {
return this.contractCache[name];
}
try {
if (name && typeof name === 'string') {
const contracts = this.database.collection('contracts');
const contractInDb = await contracts.findOne({ _id: name }, { session: this.session });
if (contractInDb) {
this.contractCache[name] = contractInDb;
return contractInDb;
}
}
return null;
} catch (error) {
// eslint-disable-next-line no-console
log.error(error);
return null;
}
}
/**
* add a smart contract to the database
* @param {String} _id _id of the contract
* @param {String} owner owner of the contract
* @param {String} code code of the contract
* @param {String} tables tables linked to the contract
*/
async addContract(payload) {
const {
_id,
owner,
code,
tables,
} = payload;
if (_id && typeof _id === 'string'
&& owner && typeof owner === 'string'
&& code && typeof code === 'string'
&& tables && typeof tables === 'object') {
const contracts = this.database.collection('contracts');
await contracts.insertOne(payload, { session: this.session });
}
}
/**
* update a smart contract in the database
* @param {String} _id _id of the contract
* @param {String} owner owner of the contract
* @param {String} code code of the contract
* @param {String} tables tables linked to the contract
*/
async updateContract(payload) {
const {
_id,
owner,
code,
tables,
} = payload;
if (_id && typeof _id === 'string'
&& owner && typeof owner === 'string'
&& code && typeof code === 'string'
&& tables && typeof tables === 'object') {
const contracts = this.database.collection('contracts');
await this.flushContractCache();
const contract = await contracts.findOne({ _id, owner }, { session: this.session });
if (contract !== null) {
await contracts.updateOne({ _id }, { $set: payload }, { session: this.session });
}
}
}
/**
* Get contracts configuration data.
*/
async getContractsConfig() {
const contractsConfig = await this.getCollection('contracts_config');
return contractsConfig.findOne({}, { session: this.session });
}
/**
* Update contracts configuration data.
* @param {Object} config data.
*/
async updateContractsConfig(config) {
const contractsConfig = await this.getCollection('contracts_config');
await contractsConfig.updateOne({}, { $set: config }, { session: this.session });
}
/**
* Add a table to the database
* @param {String} contractName name of the contract
* @param {String} tableName name of the table
* @param {Array} indexes array of string containing the name of the indexes to create
* @param {Object} params extra table creation parameters:
* - primaryKey { Array<String> } array of string keys comprising the primary key
*/
async createTable(payload) {
const {
contractName, tableName, indexes, params,
} = payload;
let result = false;
// check that the params are correct
// each element of the indexes array have to be a string if defined
if (validator.isAlphanumeric(tableName)
&& Array.isArray(indexes)
&& (indexes.length === 0
|| (indexes.length > 0 && indexes.every(el => validateIndexSpec(el))))
&& (!params.primaryKey
|| (Array.isArray(params.primaryKey) && params.primaryKey.length > 0
&& params.primaryKey.every(el => validator.isAlphanumeric(el))))) {
const finalTableName = `${contractName}_${tableName}`;
// get the table from the database
let table = await this.getContractCollection(contractName, finalTableName);
if (table === null) {
// if it doesn't exist, create it (with the binary indexes)
await this.initSequence(finalTableName);
await this.database.createCollection(finalTableName, { session: this.session });
table = this.database.collection(finalTableName);
if (indexes.length > 0) {
const nbIndexes = indexes.length;
for (let i = 0; i < nbIndexes; i += 1) {
const index = indexes[i];
const indexOptions = { session: this.session };
let finalIndex = {};
if (typeof index === 'object') {
indexOptions.name = index.name;
finalIndex = index.index;
} else {
finalIndex[index] = 1;
}
await table.createIndex(finalIndex, indexOptions);
}
}
result = true;
}
} else {
log.warn(`Table invalid, was not created, payload: ${JSON.stringify(payload)}`); // eslint-disable-line no-console
}
return result;
}
/**
* Add indexes to an existing table
* @param {String} contractName name of the contract
* @param {String} tableName name of the table
* @param {Array} indexes array of string containing the name of the indexes to create
*/
async addIndexes(payload) {
const { contractName, tableName, indexes } = payload;
let result = 0;
// check that the params are correct
// each element of the indexes array have to be a string if defined
if (validator.isAlphanumeric(tableName)
&& Array.isArray(indexes)
&& (indexes.length === 0
|| (indexes.length > 0 && indexes.every(el => validateIndexSpec(el))))) {
const finalTableName = `${contractName}_${tableName}`;
// get the table from the database
const table = await this.getContractCollection(contractName, finalTableName);
if (table !== null) {
if (indexes.length > 0) {
const nbIndexes = indexes.length;
const tableIndexes = await indexInformation(table);
for (let i = 0; i < nbIndexes; i += 1) {
const index = indexes[i];
// Do not do this within session, cannot add indexes in same tx.
const indexOptions = {};
let finalIndex = {};
let createIndex = true;
if (typeof index === 'object') {
if (tableIndexes[index.name] !== undefined) {
log.info(`Index with name ${index.name} already exists for ${finalTableName}`); // eslint-disable-line no-console
createIndex = false;
} else {
indexOptions.name = index.name;
finalIndex = index.index;
}
} else if (tableIndexes[`${index}_1`] !== undefined) {
log.info(`Index ${index} already exists for ${finalTableName}`); // eslint-disable-line no-console
createIndex = false;
} else {
finalIndex[index] = 1;
}
if (createIndex) {
await table.createIndex(finalIndex, indexOptions);
result += 1;
}
}
}
}
}
return result;
}
/**
* retrieve records from the table of a contract
* @param {String} contract contract name
* @param {String} table table name
* @param {JSON} query query to perform on the table
* @param {Integer} limit limit the number of records to retrieve
* @param {Integer} offset offset applied to the records set
* @param {Array<Object>} indexes array of index definitions { index: string, descending: boolean }
* @returns {Array<Object>} returns an array of objects if records found, an empty array otherwise
*/
async find(payload) {
try {
const {
contract,
table,
query,
limit,
offset,
indexes,
} = payload;
log.info('Find payload ', JSON.stringify(payload));
await this.flushCache();
const lim = limit || 1000;
const off = offset || 0;
const ind = indexes || [];
let result = null;
if (contract && typeof contract === 'string'
&& table && typeof table === 'string'
&& query && typeof query === 'object'
&& Array.isArray(ind)
&& (ind.length === 0
|| (ind.length > 0
&& ind.every(el => el.index && typeof el.index === 'string'
&& el.descending !== undefined && typeof el.descending === 'boolean')))
&& Number.isInteger(lim)
&& Number.isInteger(off)
&& lim > 0 && lim <= 1000
&& off >= 0) {
const finalTableName = `${contract}_${table}`;
const contractInDb = await this.findContract({ name: contract });
let tableData = null;
if (contractInDb && contractInDb.tables[finalTableName] !== undefined) {
tableData = this.database.collection(finalTableName);
}
if (tableData) {
const customPrimaryKey = contractInDb.tables[finalTableName].primaryKey;
if (customPrimaryKey) {
adjustQueryForPrimaryKey(query, customPrimaryKey);
}
// if there is an index passed, check if it exists
if (ind.length > 0) {
const tableIndexes = await indexInformation(tableData);
const sort = [];
if (ind.every(el => tableIndexes[`${el.index}_1`] !== undefined || el.index === '$loki' || el.index === '_id' || tableIndexes[el.index] !== undefined)) {
ind.forEach((el) => {
if (tableIndexes[el.index] !== undefined) {
tableIndexes[el.index].forEach((indexPart) => {
const indexField = indexPart[0];
const indexSort = indexPart[1];
if (el.descending === true) {
sort.push([indexField, indexSort === 1 ? 'desc' : 'asc']);
} else {
sort.push([indexField, indexSort === 1 ? 'asc' : 'desc']);
}
});
} else {
sort.push([el.index === '$loki' ? '_id' : el.index, el.descending === true ? 'desc' : 'asc']);
}
});
} else {
// This can happen when creating a table and using find with index all in the same transaction
// and should be rare in production. Otherwise, contract code is asking for an index that does
// not exist.
log.info(`Index ${JSON.stringify(ind)} not available for ${finalTableName}`); // eslint-disable-line no-console
}
if (sort.findIndex(el => el[0] === '_id') < 0) {
sort.push(['_id', 'asc']);
}
result = await tableData.find(EJSON.deserialize(query), {
limit: lim,
skip: off,
sort,
session: this.session,
}).toArray();
result = EJSON.serialize(result);
} else {
result = await tableData.find(EJSON.deserialize(query), {
limit: lim,
skip: off,
sort: ['_id', 'asc'],
session: this.session,
}).toArray();
result = EJSON.serialize(result);
}
}
}
return result;
} catch (error) {
// eslint-disable-next-line no-console
log.error(error);
return null;
}
}
/**
* retrieve a record from the table of a contract
* @param {String} contract contract name
* @param {String} table table name
* @param {JSON} query query to perform on the table
* @returns {Object} returns a record if it exists, null otherwise
*/
async findOne(payload) { // eslint-disable-line no-unused-vars
try {
const { contract, table, query } = payload;
log.info('findOne payload ', payload);
let result = null;
if (contract && typeof contract === 'string'
&& table && typeof table === 'string'
&& query && typeof query === 'object') {
if (query.$loki) {
query._id = query.$loki; // eslint-disable-line no-underscore-dangle
delete query.$loki;
}
const finalTableName = `${contract}_${table}`;
const contractInDb = await this.findContract({ name: contract });
let tableData = null;
if (contractInDb && contractInDb.tables[finalTableName] !== undefined) {
tableData = this.database.collection(finalTableName);
}
if (tableData) {
const customPrimaryKey = contractInDb.tables[finalTableName].primaryKey;
if (customPrimaryKey) {
adjustQueryForPrimaryKey(query, customPrimaryKey);
}
if (this.session) {
const cacheKey = objectCacheKey(contract, table, query);
if (cacheKey) {
if (this.objectCache[cacheKey]) {
return this.objectCache[cacheKey];
}
} else {
await this.flushCache();
}
}
result = await tableData.findOne(EJSON.deserialize(query), { session: this.session });
if (result) {
result = EJSON.serialize(result);
}
}
}
return result;
} catch (error) {
// eslint-disable-next-line no-console
log.error(error);
return null;
}
}
/**
* insert a record in the table of a contract
* @param {String} contract contract name
* @param {String} table table name
* @param {String} record record to save in the table
*/
async insert(payload) { // eslint-disable-line no-unused-vars
const { contract, table, record } = payload;
const finalTableName = `${contract}_${table}`;
let finalRecord = null;
const contractInDb = await this.findContract({ name: contract });
if (contractInDb && contractInDb.tables[finalTableName] !== undefined) {
const tableInDb = this.database.collection(finalTableName);
if (tableInDb) {
finalRecord = EJSON.deserialize(record);
const customPrimaryKey = contractInDb.tables[finalTableName].primaryKey;
if (customPrimaryKey) {
finalRecord._id = {}; // eslint-disable-line no-underscore-dangle
customPrimaryKey.forEach((k) => {
finalRecord._id[k] = finalRecord[k]; // eslint-disable-line no-underscore-dangle
});
} else {
finalRecord._id = await this.getNextSequence(finalTableName); // eslint-disable-line
}
await tableInDb.insertOne(finalRecord, { session: this.session });
await this.updateTableHash(contract, finalTableName);
}
}
return finalRecord;
}
/**
* remove a record in the table of a contract
* @param {String} contract contract name
* @param {String} table table name
* @param {String} record record to remove from the table
*/
async remove(payload) { // eslint-disable-line no-unused-vars
const { contract, table, record } = payload;
const finalTableName = `${contract}_${table}`;
const contractInDb = await this.findContract({ name: contract });
if (contractInDb && contractInDb.tables[finalTableName] !== undefined) {
const tableInDb = this.database.collection(finalTableName);
if (tableInDb) {
await this.updateTableHash(contract, finalTableName);
await tableInDb.deleteOne({ _id: record._id }, { session: this.session }); // eslint-disable-line no-underscore-dangle
const cacheKey = objectCacheKey(contract, table, record);
if (cacheKey && this.objectCache[cacheKey]) {
delete this.objectCache[cacheKey];
}
}
}
}
/**
* update a record in the table of a contract
* @param {String} contract contract name
* @param {String} table table name
* @param {String} record record to update in the table
* @param {String} unsets record fields to be removed (optional)
*/
async update(payload, cache = true) {
const {
contract, table, record, unsets,
} = payload;
const finalTableName = `${contract}_${table}`;
const contractInDb = await this.findContract({ name: contract });
if (contractInDb && contractInDb.tables[finalTableName] !== undefined) {
const tableInDb = this.database.collection(finalTableName);
if (tableInDb) {
if (unsets && !cache) {
await this.flushCache();
} else if (cache) {
const cacheKey = objectCacheKey(contract, table, record);
if (cacheKey) {
this.objectCache[cacheKey] = record;
await this.updateTableHash(contract, finalTableName);
return;
}
}
if (cache) {
// Do not re-update table hash when flushing cache.
await this.updateTableHash(contract, finalTableName);
}
if (unsets) {
await tableInDb.updateOne({ _id: record._id }, { $set: EJSON.deserialize(record), $unset: EJSON.deserialize(unsets) }, { upsert: true, session: this.session }); // eslint-disable-line
} else {
await tableInDb.updateOne({ _id: record._id }, { $set: EJSON.deserialize(record) }, { upsert: true, session: this.session }); // eslint-disable-line
}
}
}
}
async flushCache() {
if (!this.session) {
return;
}
const keys = Object.keys(this.objectCache);
for (let i = 0; i < keys.length; i += 1) {
const k = keys[i];
const keyParts = k.split('_');
const payload = {
contract: keyParts[0],
table: keyParts[1],
record: this.objectCache[k],
};
await this.update(payload, false);
}
this.objectCache = {};
}
async flushContractCache() {
if (!this.session) {
return;
}
const contracts = this.database.collection('contracts');
const keys = Object.keys(this.contractCache);
for (let i = 0; i < keys.length; i += 1) {
const k = keys[i];
await contracts.updateOne({ _id: k }, { $set: this.contractCache[k] }, { session: this.session });
}
this.contractCache = {};
}
/**
* get the details of a smart contract table
* @param {String} contract contract name
* @param {String} table table name
* @param {String} record record to update in the table
* @returns {Object} returns the table details if it exists, null otherwise
*/
async getTableDetails(payload) {
const { contract, table } = payload;
const finalTableName = `${contract}_${table}`;
const contractInDb = await this.findContract({ name: contract });
let tableDetails = null;
if (contractInDb && contractInDb.tables[finalTableName] !== undefined) {
const tableInDb = this.database.collection(finalTableName);
if (tableInDb) {
tableDetails = Object.assign({}, contractInDb.tables[finalTableName]);
tableDetails.indexes = await tableInDb.indexInformation();
}
}
return tableDetails;
}
/**
* check if a table exists
* @param {String} contract contract name
* @param {String} table table name
* @returns {Object} returns true if the table exists, false otherwise
*/
async tableExists(payload) {
const { contract, table } = payload;
const finalTableName = `${contract}_${table}`;
let result = false;
const contractInDb = await this.findContract({ name: contract });
if (contractInDb && contractInDb.tables[finalTableName] !== undefined) {
const tableInDb = this.database.collection(finalTableName);
if (tableInDb) {
result = true;
}
}
return result;
}
/**
* retrieve records from the table
* @param {String} table table name
* @param {JSON} query query to perform on the table
* @param {Integer} limit limit the number of records to retrieve
* @param {Integer} offset offset applied to the records set
* @param {Array<Object>} indexes array of index definitions { index: string, descending: boolean }
* @returns {Array<Object>} returns an array of objects if records found, an empty array otherwise
*/
async dfind(payload, callback) { // eslint-disable-line no-unused-vars
const {
table,
query,
limit,
offset,
indexes,
} = payload;
await this.flushCache();
const lim = limit || 1000;
const off = offset || 0;
const ind = indexes || [];
const tableInDb = await this.getCollection(table);
let records = [];
if (tableInDb) {
if (ind.length > 0) {
records = await tableInDb.find(EJSON.deserialize(query), {
limit: lim,
skip: off,
sort: ind.map(el => [el.index === '$loki' ? '_id' : el.index, el.descending === true ? 'desc' : 'asc']),
session: this.session,
});
records = EJSON.serialize(records);
} else {
records = await tableInDb.find(EJSON.deserialize(query), {
limit: lim,
skip: off,
session: this.session,
});
records = EJSON.serialize(records);
}
}
return records;
}
/**
* retrieve a record from the table
* @param {String} table table name
* @param {JSON} query query to perform on the table
* @returns {Object} returns a record if it exists, null otherwise
*/
async dfindOne(payload) {
const { table, query } = payload;
await this.flushCache();
const tableInDb = await this.getCollection(table);
let record = null;
if (query.$loki) {
query._id = query.$loki; // eslint-disable-line no-underscore-dangle
delete query.$loki;
}
if (tableInDb) {
record = await tableInDb.findOne(EJSON.deserialize(query), { session: this.session });
record = EJSON.serialize(record);
}
return record;
}
/**
* insert a record
* @param {String} table table name
* @param {String} record record to save in the table
*/
async dinsert(payload) {
const { table, record } = payload;
const tableInDb = this.database.collection(table);
const finalRecord = record;
finalRecord._id = await this.getNextSequence(table); // eslint-disable-line
await tableInDb.insertOne(EJSON.deserialize(finalRecord), { session: this.session });
await this.updateTableHash(table.split('_')[0], table.split('_')[1]);
return finalRecord;
}
/**
* update a record in the table
* @param {String} table table name
* @param {String} record record to update in the table
*/
async dupdate(payload) {
const { table, record } = payload;