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

[fix] #875 Reuse INDEX computation

parent 1ed31d98
Branches
Tags
No related merge requests found
...@@ -35,19 +35,19 @@ module.exports = class DuniterBlockchain extends MiscIndexedBlockchain { ...@@ -35,19 +35,19 @@ module.exports = class DuniterBlockchain extends MiscIndexedBlockchain {
checkBlock(block, withPoWAndSignature, conf, dal) { checkBlock(block, withPoWAndSignature, conf, dal) {
return co(function*() { return co(function*() {
const index = indexer.localIndex(block, conf)
if (withPoWAndSignature) { if (withPoWAndSignature) {
yield Q.nbind(rules.CHECK.ASYNC.ALL_LOCAL, rules, block, conf); yield Q.nbind(rules.CHECK.ASYNC.ALL_LOCAL, rules, block, conf, index);
} }
else { else {
yield Q.nbind(rules.CHECK.ASYNC.ALL_LOCAL_BUT_POW, rules, block, conf); yield Q.nbind(rules.CHECK.ASYNC.ALL_LOCAL_BUT_POW, rules, block, conf, index);
} }
const index = indexer.localIndex(block, conf); const HEAD = yield indexer.completeGlobalScope(block, conf, index, dal);
const HEAD_1 = yield dal.bindexDAL.head(1);
const mindex = indexer.mindex(index); const mindex = indexer.mindex(index);
const iindex = indexer.iindex(index); const iindex = indexer.iindex(index);
const sindex = indexer.sindex(index); const sindex = indexer.sindex(index);
const cindex = indexer.cindex(index); const cindex = indexer.cindex(index);
const HEAD = yield indexer.completeGlobalScope(block, conf, index, dal);
const HEAD_1 = yield dal.bindexDAL.head(1);
// BR_G49 // BR_G49
if (indexer.ruleVersion(HEAD, HEAD_1) === false) throw Error('ruleVersion'); if (indexer.ruleVersion(HEAD, HEAD_1) === false) throw Error('ruleVersion');
// BR_G50 // BR_G50
...@@ -169,10 +169,11 @@ module.exports = class DuniterBlockchain extends MiscIndexedBlockchain { ...@@ -169,10 +169,11 @@ module.exports = class DuniterBlockchain extends MiscIndexedBlockchain {
// Check the local rules // Check the local rules
// Enrich with the global index // Enrich with the global index
// Check the global rules // Check the global rules
return { index, HEAD }
}) })
} }
pushBlock(obj, index, conf, dal, logger) { pushBlock(obj, index, HEAD, conf, dal, logger) {
// const supra = super // const supra = super
const that = this const that = this
return co(function*() { return co(function*() {
...@@ -183,7 +184,7 @@ module.exports = class DuniterBlockchain extends MiscIndexedBlockchain { ...@@ -183,7 +184,7 @@ module.exports = class DuniterBlockchain extends MiscIndexedBlockchain {
try { try {
const currentBlock = yield dal.getCurrentBlockOrNull(); const currentBlock = yield dal.getCurrentBlockOrNull();
block.fork = false; block.fork = false;
yield that.saveBlockData(currentBlock, block, conf, dal, logger); yield that.saveBlockData(currentBlock, block, conf, dal, logger, index, HEAD);
try { try {
yield DuniterBlockchain.pushStatsForBlocks([block], dal); yield DuniterBlockchain.pushStatsForBlocks([block], dal);
...@@ -205,14 +206,14 @@ module.exports = class DuniterBlockchain extends MiscIndexedBlockchain { ...@@ -205,14 +206,14 @@ module.exports = class DuniterBlockchain extends MiscIndexedBlockchain {
}) })
} }
saveBlockData(current, block, conf, dal, logger) { saveBlockData(current, block, conf, dal, logger, index, HEAD) {
const that = this const that = this
return co(function*() { return co(function*() {
if (block.number == 0) { if (block.number == 0) {
yield that.saveParametersForRoot(block, conf, dal); yield that.saveParametersForRoot(block, conf, dal);
} }
const indexes = yield dal.generateIndexes(block, conf); const indexes = yield dal.generateIndexes(block, conf, index, HEAD);
// Newcomers // Newcomers
yield that.createNewcomers(indexes.iindex, dal, logger); yield that.createNewcomers(indexes.iindex, dal, logger);
......
...@@ -99,8 +99,8 @@ function BlockchainContext() { ...@@ -99,8 +99,8 @@ function BlockchainContext() {
this.checkBlock = (block, withPoWAndSignature) => blockchain.checkBlock(block, withPoWAndSignature, conf, dal) this.checkBlock = (block, withPoWAndSignature) => blockchain.checkBlock(block, withPoWAndSignature, conf, dal)
this.addBlock = (obj) => co(function*() { this.addBlock = (obj, index, HEAD) => co(function*() {
const block = yield blockchain.pushBlock(obj, null, conf, dal, logger) const block = yield blockchain.pushBlock(obj, index, HEAD, conf, dal, logger)
vHEAD_1 = vHEAD = HEADrefreshed = null vHEAD_1 = vHEAD = HEADrefreshed = null
return block return block
}) })
...@@ -125,8 +125,8 @@ function BlockchainContext() { ...@@ -125,8 +125,8 @@ function BlockchainContext() {
throw constants.ERRORS.NO_POTENTIAL_FORK_AS_NEXT; throw constants.ERRORS.NO_POTENTIAL_FORK_AS_NEXT;
} }
const block = forks[0]; const block = forks[0];
yield that.checkBlock(block, constants.WITH_SIGNATURES_AND_POW); const { index, HEAD } = yield that.checkBlock(block, constants.WITH_SIGNATURES_AND_POW);
yield that.addBlock(block); yield that.addBlock(block, index, HEAD);
logger.debug('Applied block #%s', block.number); logger.debug('Applied block #%s', block.number);
}); });
......
...@@ -238,8 +238,8 @@ module.exports = (blockchain, conf, dal, logger) => { ...@@ -238,8 +238,8 @@ module.exports = (blockchain, conf, dal, logger) => {
yield dal.walletDAL.insertBatch(walletsToRecord) yield dal.walletDAL.insertBatch(walletsToRecord)
// Last block: cautious mode to trigger all the INDEX expiry mechanisms // Last block: cautious mode to trigger all the INDEX expiry mechanisms
yield blockchain.checkBlock(block, constants.WITH_SIGNATURES_AND_POW, conf, dal) const { index, HEAD } = yield blockchain.checkBlock(block, constants.WITH_SIGNATURES_AND_POW, conf, dal)
yield blockchain.pushBlock(block, null, conf, dal, logger) yield blockchain.pushBlock(block, index, HEAD, conf, dal, logger)
// Clean temporary variables // Clean temporary variables
sync_bindex = []; sync_bindex = [];
......
...@@ -531,13 +531,16 @@ function FileDAL(params) { ...@@ -531,13 +531,16 @@ function FileDAL(params) {
]; ];
}); });
this.generateIndexes = (block, conf) => co(function*() { this.generateIndexes = (block, conf, index, HEAD) => co(function*() {
const index = indexer.localIndex(block, conf); // We need to recompute the indexes for block#0
if (!index || !HEAD || HEAD.number == 0) {
index = indexer.localIndex(block, conf)
HEAD = yield indexer.completeGlobalScope(block, conf, index, that)
}
let mindex = indexer.mindex(index); let mindex = indexer.mindex(index);
let iindex = indexer.iindex(index); let iindex = indexer.iindex(index);
let sindex = indexer.sindex(index); let sindex = indexer.sindex(index);
let cindex = indexer.cindex(index); let cindex = indexer.cindex(index);
const HEAD = yield indexer.completeGlobalScope(block, conf, index, that);
sindex = sindex.concat(yield indexer.ruleIndexGenDividend(HEAD, that)); sindex = sindex.concat(yield indexer.ruleIndexGenDividend(HEAD, that));
sindex = sindex.concat(yield indexer.ruleIndexGarbageSmallAccounts(HEAD, sindex, that)); sindex = sindex.concat(yield indexer.ruleIndexGarbageSmallAccounts(HEAD, sindex, that));
cindex = cindex.concat(yield indexer.ruleIndexGenCertificationExpiry(HEAD, that)); cindex = cindex.concat(yield indexer.ruleIndexGenCertificationExpiry(HEAD, that));
......
...@@ -20,7 +20,7 @@ _.extend(rules.HELPERS, global_rules.HELPERS); ...@@ -20,7 +20,7 @@ _.extend(rules.HELPERS, global_rules.HELPERS);
rules.ALIAS = { rules.ALIAS = {
ALL_LOCAL: (block, conf) => co(function *() { ALL_LOCAL: (block, conf, index) => co(function *() {
yield rules.LOCAL.checkParameters(block); yield rules.LOCAL.checkParameters(block);
yield rules.LOCAL.checkProofOfWork(block); yield rules.LOCAL.checkProofOfWork(block);
yield rules.LOCAL.checkInnerHash(block); yield rules.LOCAL.checkInnerHash(block);
...@@ -30,17 +30,17 @@ rules.ALIAS = { ...@@ -30,17 +30,17 @@ rules.ALIAS = {
yield rules.LOCAL.checkBlockSignature(block); yield rules.LOCAL.checkBlockSignature(block);
yield rules.LOCAL.checkBlockTimes(block, conf); yield rules.LOCAL.checkBlockTimes(block, conf);
yield rules.LOCAL.checkIdentitiesSignature(block); yield rules.LOCAL.checkIdentitiesSignature(block);
yield rules.LOCAL.checkIdentitiesUserIDConflict(block, conf); yield rules.LOCAL.checkIdentitiesUserIDConflict(block, conf, index);
yield rules.LOCAL.checkIdentitiesPubkeyConflict(block, conf); yield rules.LOCAL.checkIdentitiesPubkeyConflict(block, conf, index);
yield rules.LOCAL.checkIdentitiesMatchJoin(block, conf); yield rules.LOCAL.checkIdentitiesMatchJoin(block, conf, index);
yield rules.LOCAL.checkMembershipUnicity(block, conf); yield rules.LOCAL.checkMembershipUnicity(block, conf, index);
yield rules.LOCAL.checkRevokedUnicity(block, conf); yield rules.LOCAL.checkRevokedUnicity(block, conf, index);
yield rules.LOCAL.checkRevokedAreExcluded(block, conf); yield rules.LOCAL.checkRevokedAreExcluded(block, conf, index);
yield rules.LOCAL.checkMembershipsSignature(block); yield rules.LOCAL.checkMembershipsSignature(block);
yield rules.LOCAL.checkPubkeyUnicity(block); yield rules.LOCAL.checkPubkeyUnicity(block);
yield rules.LOCAL.checkCertificationOneByIssuer(block, conf); yield rules.LOCAL.checkCertificationOneByIssuer(block, conf, index);
yield rules.LOCAL.checkCertificationUnicity(block, conf); yield rules.LOCAL.checkCertificationUnicity(block, conf, index);
yield rules.LOCAL.checkCertificationIsntForLeaverOrExcluded(block, conf); yield rules.LOCAL.checkCertificationIsntForLeaverOrExcluded(block, conf, index);
yield rules.LOCAL.checkTxVersion(block); yield rules.LOCAL.checkTxVersion(block);
yield rules.LOCAL.checkTxIssuers(block); yield rules.LOCAL.checkTxIssuers(block);
yield rules.LOCAL.checkTxSources(block); yield rules.LOCAL.checkTxSources(block);
...@@ -49,7 +49,7 @@ rules.ALIAS = { ...@@ -49,7 +49,7 @@ rules.ALIAS = {
yield rules.LOCAL.checkTxSignature(block); yield rules.LOCAL.checkTxSignature(block);
}), }),
ALL_LOCAL_BUT_POW_AND_SIGNATURE: (block, conf) => co(function *() { ALL_LOCAL_BUT_POW_AND_SIGNATURE: (block, conf, index) => co(function *() {
yield rules.LOCAL.checkParameters(block); yield rules.LOCAL.checkParameters(block);
yield rules.LOCAL.checkInnerHash(block); yield rules.LOCAL.checkInnerHash(block);
yield rules.LOCAL.checkPreviousHash(block); yield rules.LOCAL.checkPreviousHash(block);
...@@ -57,17 +57,17 @@ rules.ALIAS = { ...@@ -57,17 +57,17 @@ rules.ALIAS = {
yield rules.LOCAL.checkUnitBase(block); yield rules.LOCAL.checkUnitBase(block);
yield rules.LOCAL.checkBlockTimes(block, conf); yield rules.LOCAL.checkBlockTimes(block, conf);
yield rules.LOCAL.checkIdentitiesSignature(block); yield rules.LOCAL.checkIdentitiesSignature(block);
yield rules.LOCAL.checkIdentitiesUserIDConflict(block, conf); yield rules.LOCAL.checkIdentitiesUserIDConflict(block, conf, index);
yield rules.LOCAL.checkIdentitiesPubkeyConflict(block, conf); yield rules.LOCAL.checkIdentitiesPubkeyConflict(block, conf, index);
yield rules.LOCAL.checkIdentitiesMatchJoin(block, conf); yield rules.LOCAL.checkIdentitiesMatchJoin(block, conf, index);
yield rules.LOCAL.checkMembershipUnicity(block, conf); yield rules.LOCAL.checkMembershipUnicity(block, conf, index);
yield rules.LOCAL.checkRevokedUnicity(block, conf); yield rules.LOCAL.checkRevokedUnicity(block, conf, index);
yield rules.LOCAL.checkRevokedAreExcluded(block, conf); yield rules.LOCAL.checkRevokedAreExcluded(block, conf, index);
yield rules.LOCAL.checkMembershipsSignature(block); yield rules.LOCAL.checkMembershipsSignature(block);
yield rules.LOCAL.checkPubkeyUnicity(block); yield rules.LOCAL.checkPubkeyUnicity(block);
yield rules.LOCAL.checkCertificationOneByIssuer(block, conf); yield rules.LOCAL.checkCertificationOneByIssuer(block, conf, index);
yield rules.LOCAL.checkCertificationUnicity(block, conf); yield rules.LOCAL.checkCertificationUnicity(block, conf, index);
yield rules.LOCAL.checkCertificationIsntForLeaverOrExcluded(block, conf); yield rules.LOCAL.checkCertificationIsntForLeaverOrExcluded(block, conf, index);
yield rules.LOCAL.checkTxVersion(block); yield rules.LOCAL.checkTxVersion(block);
yield rules.LOCAL.checkTxIssuers(block); yield rules.LOCAL.checkTxIssuers(block);
yield rules.LOCAL.checkTxSources(block); yield rules.LOCAL.checkTxSources(block);
...@@ -85,11 +85,11 @@ rules.CHECK = { ...@@ -85,11 +85,11 @@ rules.CHECK = {
}; };
function checkLocal(contract) { function checkLocal(contract) {
return (b, conf, done) => { return (b, conf, index, done) => {
return co(function *() { return co(function *() {
try { try {
const block = Block.fromJSON(b); const block = Block.fromJSON(b);
yield contract(block, conf); yield contract(block, conf, index);
done && done(); done && done();
} catch (err) { } catch (err) {
if (done) return done(err); if (done) return done(err);
......
...@@ -105,8 +105,7 @@ rules.FUNCTIONS = { ...@@ -105,8 +105,7 @@ rules.FUNCTIONS = {
return true; return true;
}), }),
checkIdentitiesUserIDConflict: (block, conf) => co(function *() { checkIdentitiesUserIDConflict: (block, conf, index) => co(function *() {
const index = indexer.localIndex(block, conf);
const creates = indexer.iindexCreate(index); const creates = indexer.iindexCreate(index);
const uids = _.chain(creates).pluck('uid').uniq().value(); const uids = _.chain(creates).pluck('uid').uniq().value();
if (creates.length !== uids.length) { if (creates.length !== uids.length) {
...@@ -115,8 +114,7 @@ rules.FUNCTIONS = { ...@@ -115,8 +114,7 @@ rules.FUNCTIONS = {
return true; return true;
}), }),
checkIdentitiesPubkeyConflict: (block, conf) => co(function *() { checkIdentitiesPubkeyConflict: (block, conf, index) => co(function *() {
const index = indexer.localIndex(block, conf);
const creates = indexer.iindexCreate(index); const creates = indexer.iindexCreate(index);
const pubkeys = _.chain(creates).pluck('pub').uniq().value(); const pubkeys = _.chain(creates).pluck('pub').uniq().value();
if (creates.length !== pubkeys.length) { if (creates.length !== pubkeys.length) {
...@@ -125,8 +123,7 @@ rules.FUNCTIONS = { ...@@ -125,8 +123,7 @@ rules.FUNCTIONS = {
return true; return true;
}), }),
checkIdentitiesMatchJoin: (block, conf) => co(function *() { checkIdentitiesMatchJoin: (block, conf, index) => co(function *() {
const index = indexer.localIndex(block, conf);
const icreates = indexer.iindexCreate(index); const icreates = indexer.iindexCreate(index);
const mcreates = indexer.mindexCreate(index); const mcreates = indexer.mindexCreate(index);
for (const icreate of icreates) { for (const icreate of icreates) {
...@@ -138,8 +135,7 @@ rules.FUNCTIONS = { ...@@ -138,8 +135,7 @@ rules.FUNCTIONS = {
return true; return true;
}), }),
checkRevokedAreExcluded: (block, conf) => co(function *() { checkRevokedAreExcluded: (block, conf, index) => co(function *() {
const index = indexer.localIndex(block, conf);
const iindex = indexer.iindex(index); const iindex = indexer.iindex(index);
const mindex = indexer.mindex(index); const mindex = indexer.mindex(index);
const revocations = _.chain(mindex) const revocations = _.chain(mindex)
...@@ -155,17 +151,16 @@ rules.FUNCTIONS = { ...@@ -155,17 +151,16 @@ rules.FUNCTIONS = {
return true; return true;
}), }),
checkRevokedUnicity: (block, conf) => co(function *() { checkRevokedUnicity: (block, conf, index) => co(function *() {
try { try {
yield rules.FUNCTIONS.checkMembershipUnicity(block, conf); yield rules.FUNCTIONS.checkMembershipUnicity(block, conf, index);
} catch (e) { } catch (e) {
throw Error('A single revocation per member is allowed'); throw Error('A single revocation per member is allowed');
} }
return true; return true;
}), }),
checkMembershipUnicity: (block, conf) => co(function *() { checkMembershipUnicity: (block, conf, index) => co(function *() {
const index = indexer.localIndex(block, conf);
const mindex = indexer.mindex(index); const mindex = indexer.mindex(index);
const pubkeys = _.chain(mindex).pluck('pub').uniq().value(); const pubkeys = _.chain(mindex).pluck('pub').uniq().value();
if (pubkeys.length !== mindex.length) { if (pubkeys.length !== mindex.length) {
...@@ -245,9 +240,8 @@ rules.FUNCTIONS = { ...@@ -245,9 +240,8 @@ rules.FUNCTIONS = {
return true; return true;
}), }),
checkCertificationOneByIssuer: (block, conf) => co(function *() { checkCertificationOneByIssuer: (block, conf, index) => co(function *() {
if (block.number > 0) { if (block.number > 0) {
const index = indexer.localIndex(block, conf);
const cindex = indexer.cindex(index); const cindex = indexer.cindex(index);
const certFromA = _.uniq(cindex.map((row) => row.issuer)); const certFromA = _.uniq(cindex.map((row) => row.issuer));
if (certFromA.length !== cindex.length) { if (certFromA.length !== cindex.length) {
...@@ -257,8 +251,7 @@ rules.FUNCTIONS = { ...@@ -257,8 +251,7 @@ rules.FUNCTIONS = {
return true; return true;
}), }),
checkCertificationUnicity: (block, conf) => co(function *() { checkCertificationUnicity: (block, conf, index) => co(function *() {
const index = indexer.localIndex(block, conf);
const cindex = indexer.cindex(index); const cindex = indexer.cindex(index);
const certAtoB = _.uniq(cindex.map((row) => row.issuer + row.receiver)); const certAtoB = _.uniq(cindex.map((row) => row.issuer + row.receiver));
if (certAtoB.length !== cindex.length) { if (certAtoB.length !== cindex.length) {
...@@ -267,8 +260,7 @@ rules.FUNCTIONS = { ...@@ -267,8 +260,7 @@ rules.FUNCTIONS = {
return true; return true;
}), }),
checkCertificationIsntForLeaverOrExcluded: (block, conf) => co(function *() { checkCertificationIsntForLeaverOrExcluded: (block, conf, index) => co(function *() {
const index = indexer.localIndex(block, conf);
const cindex = indexer.cindex(index); const cindex = indexer.cindex(index);
const iindex = indexer.iindex(index); const iindex = indexer.iindex(index);
const mindex = indexer.mindex(index); const mindex = indexer.mindex(index);
......
...@@ -130,9 +130,11 @@ function BlockchainService (server) { ...@@ -130,9 +130,11 @@ function BlockchainService (server) {
if (followsCurrent) { if (followsCurrent) {
// try to add it on main blockchain // try to add it on main blockchain
if (doCheck) { if (doCheck) {
yield mainContext.checkBlock(obj, constants.WITH_SIGNATURES_AND_POW); const { index, HEAD } = yield mainContext.checkBlock(obj, constants.WITH_SIGNATURES_AND_POW);
} return yield mainContext.addBlock(obj, index, HEAD)
} else {
return yield mainContext.addBlock(obj) return yield mainContext.addBlock(obj)
}
} else if (forkAllowed) { } else if (forkAllowed) {
// add it as side chain // add it as side chain
if (current.number - obj.number + 1 >= conf.forksize) { if (current.number - obj.number + 1 >= conf.forksize) {
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
const co = require('co'); const co = require('co');
const should = require('should'); const should = require('should');
const parsers = require('duniter-common').parsers; const parsers = require('duniter-common').parsers;
const indexer = require('../../app/lib/indexer')
const rules = require('../../app/lib/rules') const rules = require('../../app/lib/rules')
const blocks = require('../data/blocks.js'); const blocks = require('../data/blocks.js');
const parser = parsers.parseBlock; const parser = parsers.parseBlock;
...@@ -84,7 +85,8 @@ function test (rule, raw, expectedMessage) { ...@@ -84,7 +85,8 @@ function test (rule, raw, expectedMessage) {
try { try {
let obj = parser.syncWrite(raw); let obj = parser.syncWrite(raw);
let block = Block.fromJSON(obj); let block = Block.fromJSON(obj);
yield rule(block, conf); // conf parameter is not always used let index = indexer.localIndex(block, conf)
yield rule(block, conf, index); // conf parameter is not always used
if (expectedMessage) { if (expectedMessage) {
throw 'Test should have thrown an error'; throw 'Test should have thrown an error';
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment