Skip to content
Snippets Groups Projects
Commit 063b68d9 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Fix #239 and #235 confusion between branch/fork block lookups

parent 8cf4e5b8
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,7 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) { ...@@ -19,6 +19,7 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) {
let collection = loki.getCollection('blocks') || loki.addCollection('blocks', { indices: ['fork', 'number', 'hash'] }); let collection = loki.getCollection('blocks') || loki.addCollection('blocks', { indices: ['fork', 'number', 'hash'] });
let blocksDB = getView(); let blocksDB = getView();
let forksDB = getForkView();
let current = null; let current = null;
let that = this; let that = this;
...@@ -67,7 +68,7 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) { ...@@ -67,7 +68,7 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) {
this.collection = collection; this.collection = collection;
this.getBlocks = (start, end) => { this.getBlocks = (start, end) => {
let lowerInLoki = collection.chain().simplesort('number').limit(1).data()[0]; let lowerInLoki = blocksDB.branchResultset().simplesort('number').limit(1).data()[0];
let lokiBlocks = blocksDB.branchResultset().find({ let lokiBlocks = blocksDB.branchResultset().find({
$and: [{ $and: [{
number: { $gte: start } number: { $gte: start }
...@@ -95,12 +96,7 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) { ...@@ -95,12 +96,7 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) {
}; };
this.getForkBlocks = () => this.getForkBlocks = () =>
Q(this.collection.find({ Q(forksDB.branchResultset().find({ wrong: false }).data());
$and: [
{ fork: true },
{ wrong: false }
]
}));
this.saveBunch = (blocks, inFiles) => { this.saveBunch = (blocks, inFiles) => {
if (!inFiles) { if (!inFiles) {
...@@ -175,8 +171,8 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) { ...@@ -175,8 +171,8 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) {
collection.update(found); collection.update(found);
}); });
current = previousBlock; current = previousBlock;
let lowerInLoki = collection.chain().simplesort('number').limit(1).data()[0]; let lowerInLoki = blocksDB.branchResultset().simplesort('number').limit(1).data()[0];
if (lowerInLoki.number > 0) { if (lowerInLoki && lowerInLoki.number > 0) {
let newLower = yield that.getBlock(lowerInLoki.number - 1); let newLower = yield that.getBlock(lowerInLoki.number - 1);
yield rootFS.remove(pathOfBlock(newLower.number) + blockFileName(newLower.number) + '.json'); yield rootFS.remove(pathOfBlock(newLower.number) + blockFileName(newLower.number) + '.json');
collection.insert(newLower); collection.insert(newLower);
...@@ -187,7 +183,7 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) { ...@@ -187,7 +183,7 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) {
let number = yield getLowerWindowBlock(); let number = yield getLowerWindowBlock();
logger.debug("Clean some blocks from memory to disk..."); logger.debug("Clean some blocks from memory to disk...");
logger.debug("Lower block = %s", number); logger.debug("Lower block = %s", number);
let lowerInLoki = collection.chain().simplesort('number').limit(1).data()[0]; let lowerInLoki = blocksDB.branchResultset().simplesort('number').limit(1).data()[0];
if (!lowerInLoki) { if (!lowerInLoki) {
return; return;
} }
...@@ -212,7 +208,7 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) { ...@@ -212,7 +208,7 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) {
yield rootFS.writeJSON(pathOfBlock(block.number) + blockFileName(block.number) + '.json', block); yield rootFS.writeJSON(pathOfBlock(block.number) + blockFileName(block.number) + '.json', block);
collection.remove(block); collection.remove(block);
} }
lowerInLoki = collection.chain().simplesort('number').limit(1).data()[0]; lowerInLoki = blocksDB.branchResultset().simplesort('number').limit(1).data()[0];
logger.debug("Lower in loki now = %s", lowerInLoki.number); logger.debug("Lower in loki now = %s", lowerInLoki.number);
logger.debug("LastUD in loki = %s", that.lastBlockWithDividend().number); logger.debug("LastUD in loki = %s", that.lastBlockWithDividend().number);
} }
...@@ -227,6 +223,12 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) { ...@@ -227,6 +223,12 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) {
return view; return view;
} }
function getForkView() {
let view = collection.addDynamicView('forks');
view.applyFind({ fork: true });
return view;
}
function folderOfBlock(blockNumber) { function folderOfBlock(blockNumber) {
return (Math.floor(blockNumber / BLOCK_FOLDER_SIZE) + 1) * BLOCK_FOLDER_SIZE; return (Math.floor(blockNumber / BLOCK_FOLDER_SIZE) + 1) * BLOCK_FOLDER_SIZE;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment