diff --git a/app/lib/dal/fileDALs/AbstractLoki.js b/app/lib/dal/fileDALs/AbstractLoki.js index a08d671a6edf200fb2d6b84bfd003c1cf3cf2ed7..9b842386c6da223a63a5319cf845a4671b23cc3f 100644 --- a/app/lib/dal/fileDALs/AbstractLoki.js +++ b/app/lib/dal/fileDALs/AbstractLoki.js @@ -7,10 +7,14 @@ var _ = require('underscore'); module.exports = AbstractLoki; -function AbstractLoki(collection, fileDAL, view) { +function AbstractLoki(collection, fileDAL, viewFields, loki) { "use strict"; + let that = this; + let cores = getCores(); + let view = getView(); + function find(conditons) { if (view) { return view.branchResultset().find(conditons).data(); @@ -18,16 +22,6 @@ function AbstractLoki(collection, fileDAL, view) { return collection.find(conditons); } - let that = this; - let cores = [], p = fileDAL; - while (p) { - if (p.core) { - cores.push(p.core); - } - p = p.parentDAL; - } - cores = _.sortBy(cores, (b) => b.forkPointNumber); - this.IMMUTABLE_FIELDS = true; this.collection = collection; @@ -142,4 +136,51 @@ function AbstractLoki(collection, fileDAL, view) { } return Q(entity); }; + + function getCores() { + let theCores = [], p = fileDAL; + while (p) { + if (p.core) { + theCores.push(p.core); + } + p = p.parentDAL; + } + return _.sortBy(theCores, (b) => b.forkPointNumber); + } + + function getView() { + let branchView; + if (viewFields && loki) { + let blockCollection = loki.getCollection('blocks'); + let current = blockCollection.chain().find({ fork: false }).simplesort('number', true).limit(1).data()[0]; + let conditions = cores.map((b) => { + let objNumber = {}, objHash = {}; + objNumber[viewFields.block_number] = b.forkPointNumber; + objHash[viewFields.block_hash] = b.forkPointHash; + if (viewFields.block_number && viewFields.block_hash) { + return { $and: [objNumber, objHash] }; + } else if (viewFields.block_hash) { + return objHash; + } else { + return objNumber; + } + }); + if (!current) { + conditions.unshift({ + $and: [{ + block_number: 0 + }, { + block_hash: 'DA39A3EE5E6B4B0D3255BFEF95601890AFD80709' + }] + }); + } + conditions.unshift({ + block_number: { $lte: current ? current.number : -1 } + }); + branchView = collection.addDynamicView(['branch', fileDAL.name].join('_')); + branchView.applyFind({ '$or': conditions }); + branchView.conditions = conditions; + } + return branchView; + } } \ No newline at end of file diff --git a/app/lib/dal/fileDALs/CertDAL.js b/app/lib/dal/fileDALs/CertDAL.js index 2fbfe328f64d9a3a3d5532369c84c2d935223283..2c3f425f8e9d950dbed9b002a6d479d686ccaf1e 100644 --- a/app/lib/dal/fileDALs/CertDAL.js +++ b/app/lib/dal/fileDALs/CertDAL.js @@ -12,33 +12,11 @@ function CertDAL(fileDAL, loki) { "use strict"; let collection = loki.getCollection('certs') || loki.addCollection('certs', { indices: ['from', 'target', 'linked', 'written'] }); - let blockCollection = loki.getCollection('blocks'); - let current = blockCollection.chain().find({ fork: false }).simplesort('number', true).limit(1).data()[0]; - let blocks = [], p = fileDAL; - let branchView; - while (p) { - if (p.core) { - blocks.push(p.core); - } - p = p.parentDAL; - } - let conditions = blocks.map((b) => { - return { - $and: [{ - block_number: b.forkPointNumber - }, { - block_hash: b.forkPointHash - }] - }; - }); - conditions.unshift({ - block_number: { $lte: current ? current.number : -1 } - }); - branchView = collection.addDynamicView(['branch', fileDAL.name].join('_')); - branchView.applyFind({ '$or': conditions }); - branchView.conditions = conditions; - AbstractLoki.call(this, collection, fileDAL); + AbstractLoki.call(this, collection, fileDAL, { + block_number: 'block_number', + block_hash: 'block_hash' + }, loki); this.idKeys = ['sig', 'from', 'target']; this.metaProps = ['linked']; diff --git a/app/lib/dal/fileDALs/LinksDAL.js b/app/lib/dal/fileDALs/LinksDAL.js index 1b0fbe0bded3b05533b76ddd9100f4c8b675ff3e..be4a81394e5d1171c0e6dc53679a8a9374138d50 100644 --- a/app/lib/dal/fileDALs/LinksDAL.js +++ b/app/lib/dal/fileDALs/LinksDAL.js @@ -12,33 +12,11 @@ function LinksDAL(fileDAL, loki) { "use strict"; let collection = loki.getCollection('links') || loki.addCollection('links', { indices: ['source', 'target', 'block_number', 'block_hash', 'timestamp'] }); - let blockCollection = loki.getCollection('blocks'); - let current = blockCollection.chain().find({ fork: false }).simplesort('number', true).limit(1).data()[0]; - let blocks = [], p = fileDAL; - let branchView; - while (p) { - if (p.core) { - blocks.push(p.core); - } - p = p.parentDAL; - } - let conditions = blocks.map((b) => { - return { - $and: [{ - block_number: b.forkPointNumber - }, { - block_hash: b.forkPointHash - }] - }; - }); - conditions.unshift({ - block_number: { $lte: current ? current.number : -1 } - }); - branchView = collection.addDynamicView(['branchl', fileDAL.name].join('_')); - branchView.applyFind({ '$or': conditions }); - branchView.conditions = conditions; - AbstractLoki.call(this, collection, fileDAL, branchView); + AbstractLoki.call(this, collection, fileDAL, { + block_number: 'block_number', + block_hash: 'block_hash' + }, loki); this.idKeys = ['source', 'target', 'block_number', 'block_hash']; this.metaProps = ['obsolete']; @@ -68,7 +46,7 @@ function LinksDAL(fileDAL, loki) { }); this.obsoletesLinks = (minTimestamp) => { - let toObsolete = branchView.branchResultset().find({ + let toObsolete = this.lokiFind({ timestamp: { $lte: minTimestamp } }); for (let i = 0; i < toObsolete.length; i++) { diff --git a/app/lib/dal/fileDALs/MembershipDAL.js b/app/lib/dal/fileDALs/MembershipDAL.js index b5c859219b37f73c4fbfeac8e20d4734b11d3d32..4c36fd9b380bedea9e629b85aeba09689c4ec4a6 100644 --- a/app/lib/dal/fileDALs/MembershipDAL.js +++ b/app/lib/dal/fileDALs/MembershipDAL.js @@ -13,40 +13,11 @@ function MembershipDAL(fileDAL, loki) { "use strict"; let collection = loki.getCollection('memberships') || loki.addCollection('memberships', { indices: ['membership', 'issuer', 'number', 'blockNumber', 'blockHash', 'userid', 'certts', 'block', 'fpr', 'written', 'signature'] }); - let blockCollection = loki.getCollection('blocks'); - let current = blockCollection.chain().find({ fork: false }).simplesort('number', true).limit(1).data()[0]; - let blocks = [], p = fileDAL; - let branchView; - while (p) { - if (p.core) { - blocks.push(p.core); - } - p = p.parentDAL; - } - let conditions = blocks.map((b) => { - return { - $and: [{ - blockNumber: b.forkPointNumber - }, { - blockHash: b.forkPointHash - }] - }; - }); - conditions.unshift({ - blockNumber: { $lte: current ? current.number : -1 } - }); - conditions.unshift({ - $and: [{ - blockNumber: '0' - }, { - blockHash: 'DA39A3EE5E6B4B0D3255BFEF95601890AFD80709' - }] - }); - branchView = collection.addDynamicView(['branch', fileDAL.name].join('_')); - branchView.applyFind({ '$or': conditions }); - branchView.conditions = conditions; - AbstractLoki.call(this, collection, fileDAL, branchView); + AbstractLoki.call(this, collection, fileDAL, { + block_number: 'blockNumber', + block_hash: 'blockHash' + }, loki); this.idKeys = ['issuer', 'signature']; this.metaProps = ['written']; diff --git a/app/lib/dal/fileDALs/SourcesDAL.js b/app/lib/dal/fileDALs/SourcesDAL.js index eb4a1604ef0a5091168f635c76cd5521c68564d7..4077b26c57903409825d66847a223da6c268563e 100644 --- a/app/lib/dal/fileDALs/SourcesDAL.js +++ b/app/lib/dal/fileDALs/SourcesDAL.js @@ -12,33 +12,11 @@ function SourcesDAL(fileDAL, loki) { "use strict"; let collection = loki.getCollection('sources') || loki.addCollection('sources', { indices: ['pubkey', 'type', 'number', 'fingerprint', 'amount', 'block_hash'] }); - let blockCollection = loki.getCollection('blocks'); - let current = blockCollection.chain().find({ fork: false }).simplesort('number', true).limit(1).data()[0]; - let blocks = [], p = fileDAL; - let branchView; - while (p) { - if (p.core) { - blocks.push(p.core); - } - p = p.parentDAL; - } - let conditions = blocks.map((b) => { - return { - $and: [{ - number: b.forkPointNumber - }, { - block_hash: b.forkPointHash - }] - }; - }); - conditions.unshift({ - block_number: { $lte: current ? current.number : -1 } - }); - branchView = collection.addDynamicView(['branch', fileDAL.name].join('_')); - branchView.applyFind({ '$or': conditions }); - branchView.conditions = conditions; - AbstractLoki.call(this, collection, fileDAL, branchView); + AbstractLoki.call(this, collection, fileDAL, { + block_number: 'number', + block_hash: 'block_hash' + }, loki); this.idKeys = ['pubkey', 'type', 'number', 'fingerprint', 'amount']; this.metaProps = ['consumed']; diff --git a/app/lib/dal/fileDALs/TxsDAL.js b/app/lib/dal/fileDALs/TxsDAL.js index c6e44f9092b70d654001b40110a3aa719e1c145a..c34a0e1c3072198c14e916ddea56ead61bb41f6e 100644 --- a/app/lib/dal/fileDALs/TxsDAL.js +++ b/app/lib/dal/fileDALs/TxsDAL.js @@ -14,29 +14,10 @@ function TxsDAL(fileDAL, loki) { let that = this; let collection = loki.getCollection('txs') || loki.addCollection('txs', { indices: ['hash', 'block_number', 'written', 'signature', 'recipients'] }); - let blockCollection = loki.getCollection('blocks'); - let current = blockCollection.chain().find({ fork: false }).simplesort('number', true).limit(1).data()[0]; - let blocks = [], p = fileDAL; - let branchView; - while (p) { - if (p.core) { - blocks.push(p.core); - } - p = p.parentDAL; - } - let conditions = blocks.map((b) => { - return { - block_number: b.forkPointNumber - }; - }); - conditions.unshift({ - block_number: { $lte: current ? current.number : -1 } - }); - branchView = collection.addDynamicView(['branch', fileDAL.name].join('_')); - branchView.applyFind({ '$or': conditions }); - branchView.conditions = conditions; - AbstractLoki.call(this, collection, fileDAL, branchView); + AbstractLoki.call(this, collection, fileDAL, { + block_number: 'block_number' + }, loki); this.idKeys = ['hash', 'block_number']; this.metaProps = ['written', 'removed']; diff --git a/test/integration/tools/node.js b/test/integration/tools/node.js index 74aabc9c8077a2cfddf1d4db4fac9a32d0eab2dc..5320c37b75bf3c47876f9b5b2aae16e601a15837 100644 --- a/test/integration/tools/node.js +++ b/test/integration/tools/node.js @@ -100,7 +100,7 @@ function Node (dbName, options) { BlockchainService.prove(block, sigFunc, difficulty, next); }, function (provenBlock, next){ - logger.debug(provenBlock.getRawSigned()); + provenBlock && provenBlock.getRawSigned && logger.debug(provenBlock.getRawSigned()); post('/blockchain/block', { "block": provenBlock.getRawSigned() }, next);