Skip to content

Commit

Permalink
Fix #216 the UD block was hidden due to block persistence in files
Browse files Browse the repository at this point in the history
  • Loading branch information
c-geek committed Nov 23, 2015
1 parent 1a53390 commit e878c3a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/lib/dal/fileDAL.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ function FileDAL(profile, home, localDir, myFS, parentFileDAL, dalName, loki) {
return -1;
}
let conf = getParameters(rootBlock);
let maxBlock = getMaxBlocksToStoreAsFile(conf);
let needToBeKeptBlocks = getMaxBlocksToStoreAsFile(conf);
let current = yield that.getCurrentBlockOrNull();
let currentNumber = current ? current.number : -1;
return currentNumber - maxBlock;
return currentNumber - needToBeKeptBlocks;
});
}

Expand Down
16 changes: 15 additions & 1 deletion app/lib/dal/fileDALs/BlockDAL.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Q = require('q');
var _ = require('underscore');
var co = require('co');
var constants = require('../../constants');
var logger = require('../../../../app/lib/logger')('blockdal');

const BLOCK_FILE_PREFIX = "0000000000";
const BLOCK_FOLDER_SIZE = 500;
Expand All @@ -19,6 +20,7 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) {
let collection = loki.getCollection('blocks') || loki.addCollection('blocks', { indices: ['fork', 'number', 'hash'] });
let blocksDB = getView();
let current = null;
let that = this;

this.init = () => co(function *() {
yield rootFS.makeTree('blocks/');
Expand Down Expand Up @@ -89,7 +91,10 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) {
});
};

this.lastBlockWithDividend = () => blocksDB.branchResultset().find({ dividend: { $gt: 0 } }).data()[0];
this.lastBlockWithDividend = () => {
let blocks = blocksDB.branchResultset().find({ dividend: { $gt: 0 } }).simplesort('number', true).data();
return blocks[0];
};

this.lastBlockOfIssuer = (issuer) => {
let blocksOfIssuer = blocksDB.branchResultset().find({ issuer: issuer }).simplesort('number', true).limit(1).data();
Expand Down Expand Up @@ -182,11 +187,16 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) {
function migrateOldBlocks() {
return co(function *() {
let number = yield getLowerWindowBlock();
logger.debug("Clean some blocks from memory to disk...");
logger.debug("Lower block = %s", number);
let lowerInLoki = collection.chain().simplesort('number').limit(1).data()[0];
if (!lowerInLoki) {
return;
}
logger.debug("LastUD in loki = %s", that.lastBlockWithDividend().number);
logger.debug("Lower in loki = %s", lowerInLoki.number);
let deadBlocksInLoki = number - lowerInLoki.number;
logger.debug("Dead blocks = %s", deadBlocksInLoki);
if (deadBlocksInLoki >= constants.BLOCKS_COLLECT_THRESHOLD) {
let blocksToPersist = blocksDB.branchResultset().find({
$and: [{
Expand All @@ -195,12 +205,16 @@ function BlockDAL(loki, rootFS, getLowerWindowBlock) {
number: { $lte: number }
}]
}).simplesort('number').data();
logger.debug("To store in files = %s to %s", blocksToPersist[0].number, blocksToPersist[blocksToPersist.length - 1].number);
for (let i = 0; i < blocksToPersist.length; i++) {
let block = blocksToPersist[i];
yield rootFS.makeTree(pathOfBlock(block.number));
yield rootFS.writeJSON(pathOfBlock(block.number) + blockFileName(block.number) + '.json', block);
collection.remove(block);
}
lowerInLoki = collection.chain().simplesort('number').limit(1).data()[0];
logger.debug("Lower in loki now = %s", lowerInLoki.number);
logger.debug("LastUD in loki = %s", that.lastBlockWithDividend().number);
}
});
}
Expand Down

0 comments on commit e878c3a

Please sign in to comment.