Skip to content

Commit

Permalink
First step of cleaning fileDal with a lot of impacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Insoleet committed Jun 25, 2016
1 parent 3cd2cd1 commit c9cd9a4
Show file tree
Hide file tree
Showing 33 changed files with 390 additions and 533 deletions.
2 changes: 1 addition & 1 deletion app/controllers/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function BlockchainBinding (server) {
});

this.difficulties = () => co(function *() {
const current = yield server.dal.getCurrent();
const current = yield server.dal.getCurrentBlockOrNull();
const number = (current && current.number) || 0;
const issuers = yield server.dal.getUniqueIssuersBetween(number - 1 - conf.blocksRot, number - 1);
const difficulties = [];
Expand Down
2 changes: 1 addition & 1 deletion app/lib/computation/blockGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function BlockGenerator(mainContext, prover) {
const unsignedBlock = block || (yield that.nextBlock());
const sigF = sigFunc || signature.sync(pair);
const trialLevel = trial || (yield rules.HELPERS.getTrialLevel(selfPubkey, conf, dal));
return prover.prove(unsignedBlock, sigF, trialLevel, null, (manualValues && manualValues.time) || null);
return prover.prove(unsignedBlock, sigF, trialLevel, (manualValues && manualValues.time) || null);
});

/**
Expand Down
3 changes: 1 addition & 2 deletions app/lib/computation/blockProver.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function BlockGenerator() {
};
});

this.prove = function (block, sigFunc, difficulty, done, forcedTime) {
this.prove = function (block, sigFunc, difficulty, forcedTime) {

var remainder = difficulty % 16;
var nbZeros = (difficulty - remainder) / 16;
Expand All @@ -90,7 +90,6 @@ function BlockGenerator() {
powWorker.setOnPoW(function(err, powBlock) {
var theBlock = (powBlock && new Block(powBlock)) || null;
resolve(theBlock);
done && done(null, theBlock);
});

powWorker.setOnError((err) => {
Expand Down
6 changes: 2 additions & 4 deletions app/lib/computation/blockchainContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function BlockchainContext() {
const start = new Date();
const block = new Block(obj);
try {
const currentBlock = yield Q.nbind(that.current, that);
const currentBlock = yield that.current();
block.fork = false;
yield saveBlockData(currentBlock, block);
return block;
Expand Down Expand Up @@ -115,9 +115,7 @@ function BlockchainContext() {
return found;
};

this.current = (done) => {
return dal.getCurrentBlockOrNull(done);
};
this.current = () => dal.getCurrentBlockOrNull();

const saveBlockData = (current, block) => co(function*() {
yield updateBlocksComputedVars(current, block);
Expand Down
154 changes: 60 additions & 94 deletions app/lib/dal/fileDAL.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
"use strict";
var Q = require('q');
var co = require('co');
var _ = require('underscore');
var hashf = require('../ucp/hashf');
var wotb = require('../wot');
var logger = require('../logger')('filedal');
var directory = require('../system/directory');
var Configuration = require('../entity/configuration');
var Membership = require('../entity/membership');
var Merkle = require('../entity/merkle');
var Transaction = require('../entity/transaction');
var constants = require('../constants');
var ConfDAL = require('./fileDALs/confDAL');
var StatDAL = require('./fileDALs/statDAL');
var IndicatorsDAL = require('./fileDALs/IndicatorsDAL');
var CFSStorage = require('./fileDALs/AbstractCFS');
const Q = require('q');
const co = require('co');
const _ = require('underscore');
const hashf = require('../ucp/hashf');
const wotb = require('../wot');
const logger = require('../logger')('filedal');
const directory = require('../system/directory');
const Configuration = require('../entity/configuration');
const Membership = require('../entity/membership');
const Merkle = require('../entity/merkle');
const Transaction = require('../entity/transaction');
const constants = require('../constants');
const ConfDAL = require('./fileDALs/confDAL');
const StatDAL = require('./fileDALs/statDAL');
const IndicatorsDAL = require('./fileDALs/IndicatorsDAL');
const CFSStorage = require('./fileDALs/AbstractCFS');

module.exports = (params) => {
return new FileDAL(params);
};

function FileDAL(params) {

let rootPath = params.home;
let myFS = params.fs;
let sqlite = params.dbf();
let wotbInstance = params.wotb;
let that = this;
const rootPath = params.home;
const myFS = params.fs;
const sqlite = params.dbf();
const wotbInstance = params.wotb;
const that = this;

this.profile = 'DAL';
this.wotb = wotbInstance;
Expand Down Expand Up @@ -75,46 +75,40 @@ function FileDAL(params) {
}
};

var currency = '';
let currency = '';

this.init = () => co(function *() {
let dalNames = _.keys(that.newDals);
const dalNames = _.keys(that.newDals);
for (let i = 0; i < dalNames.length; i++) {
let dal = that.newDals[dalNames[i]];
const dal = that.newDals[dalNames[i]];
yield dal.init();
}
logger.debug("Upgrade database...");
yield that.metaDAL.upgradeDatabase();
let latestMember = yield that.idtyDAL.getLatestMember();
const latestMember = yield that.idtyDAL.getLatestMember();
if (latestMember && that.wotb.getWoTSize() > latestMember.wotb_id + 1) {
logger.warn('Maintenance: cleaning wotb...');
while (that.wotb.getWoTSize() > latestMember.wotb_id + 1) {
that.wotb.removeNode();
}
}
// Update the maximum certifications count a member can issue into the C++ addon
let currencyParams = yield that.getParameters();
const currencyParams = yield that.getParameters();
if (currencyParams && currencyParams.sigStock !== undefined && currencyParams.sigStock !== null) {
that.wotb.setMaxCert(currencyParams.sigStock);
}
});

this.getDBVersion = () => that.metaDAL.getVersion();

this.getCurrency = function() {
return currency;
};
this.getCurrency = () => currency;

that.writeFileOfBlock = function(block) {
return that.blockDAL.saveBlock(block);
};
that.writeFileOfBlock = (block) => that.blockDAL.saveBlock(block);

this.writeSideFileOfBlock = (block) =>
that.blockDAL.saveSideBlock(block);

this.listAllPeers = function() {
return that.peerDAL.listAll();
};
this.listAllPeers = () => that.peerDAL.listAll();

function nullIfError(promise, done) {
return promise
Expand Down Expand Up @@ -147,57 +141,37 @@ function FileDAL(params) {
});
}

this.getPeer = function(pubkey) {
return that.peerDAL.getPeer(pubkey)
.catch(function() {
throw Error('Unknown peer ' + pubkey);
});
};
this.getPeer = (pubkey) => co(function*() {
try {
return that.peerDAL.getPeer(pubkey)
} catch (err) {
throw Error('Unknown peer ' + pubkey);
}
});

this.getBlock = function(number, done) {
return that.blockDAL.getBlock(number)
.catch(function(){
throw 'Block ' + number + ' not found';
})
.then(function(block){
done && done(null, block || null);
return block;
})
.catch(function(err){
done && done(err);
throw err;
});
};
this.getBlock = (number) => co(function*() {
const block = yield that.blockDAL.getBlock(number);
return block || null;
});

this.getAbsoluteBlockByNumberAndHash = (number, hash) =>
that.blockDAL.getAbsoluteBlock(number, hash);

this.getBlockByNumberAndHash = function(number, hash, done) {
return that.getBlock(number)
.then(function(block){
if (block.hash != hash) throw "Not found";
else return block;
})
.catch(function(){
throw 'Block ' + [number, hash].join('-') + ' not found';
})
.then(function(block){
done && done(null, block);
this.getBlockByNumberAndHash = (number, hash) => co(function*(){
try {
const block = yield that.getBlock(number);
if (!block || block.hash != hash)
throw "Not found";
else
return block;
})
.catch(function(err){
done && done(err);
throw err;
});
};
} catch (err) {
throw 'Block ' + [number, hash].join('-') + ' not found';
}
});

this.getBlockByNumberAndHashOrNull = function(number, hash) {
return nullIfError(that.getBlock(number)
.then(function(block){
if (!block || block.hash != hash) throw "Not found";
else return block;
}));
};
this.getBlockByNumberAndHashOrNull = (number, hash) => co(function*(){
return yield nullIfError(that.getBlockByNumberAndHash(number, hash));
});

this.getChainabilityBlock = (currentTime, sigPeriod) => co(function *() {
// AGE = current_time - block_time
Expand All @@ -216,24 +190,16 @@ function FileDAL(params) {
});


this.getCurrentBlockOrNull = function(done) {
return nullIfErrorIs(that.getBlockCurrent(), constants.ERROR.BLOCK.NO_CURRENT_BLOCK, done);
};

this.getCurrent = this.getCurrentBlockOrNull;
this.getCurrentBlockOrNull = () => co(function*() {
return nullIfErrorIs(that.getBlockCurrent(), constants.ERROR.BLOCK.NO_CURRENT_BLOCK);
});

this.getPromoted = function(number, done) {
return that.getBlock(number, done);
};
this.getPromoted = (number) => that.getBlock(number);

// Block
this.lastUDBlock = function() {
return Q(that.blockDAL.lastBlockWithDividend());
};
this.lastUDBlock = () => that.blockDAL.lastBlockWithDividend();

this.getRootBlock = function(done) {
return that.getBlock(0, done);
};
this.getRootBlock = () => that.getBlock(0);

this.lastBlockOfIssuer = function(issuer) {
return that.blockDAL.lastBlockOfIssuer(issuer);
Expand All @@ -251,7 +217,7 @@ function FileDAL(params) {
};

this.getBlockFrom = function(number) {
return that.getCurrent()
return that.getCurrentBlockOrNull()
.then(function(current){
return that.getBlocksBetween(number, current.number);
});
Expand Down
Loading

0 comments on commit c9cd9a4

Please sign in to comment.