Skip to content
Snippets Groups Projects
Commit 8cb2d91f authored by inso's avatar inso
Browse files

Branch switches tests are passing

parent 6ff3bf28
Branches
Tags
No related merge requests found
"use strict";
const Q = require('q');
const co = require('co');
const _ = require('underscore');
const constant = require('./constants');
......@@ -18,7 +17,7 @@ module.exports = {
*/
dao.applyBranch = (blocks) => co(function *() {
for (const block of blocks) {
yield dao.applyMainBranch(block);
yield dao.applyMainBranch([block]);
}
return true;
});
......@@ -29,8 +28,7 @@ module.exports = {
* @param forksize The maximum length we can look at to find common root block.
* @returns {*|Promise}
*/
dao.findCommonRoot = (fork, forksize) => {
return co(function *() {
dao.findCommonRoot = (fork, forksize) => co(function *() {
let commonRoot = null;
let localCurrent = yield dao.localCurrent();
......@@ -38,7 +36,7 @@ module.exports = {
// We look between the top block that is known as fork ...
let topBlock = fork.block;
// ... and the bottom which is bounded by `forksize`
let bottomBlock = yield dao.getRemoteBlock(fork.peer, Math.max(0, localCurrent.number - forksize));
let bottomBlock = (yield dao.getRemoteBlock(fork.peer, Math.max(0, localCurrent.number - forksize)))[0];
let lookBlock = bottomBlock;
let localEquivalent = yield dao.getLocalBlock(bottomBlock.number);
let isCommonBlock = lookBlock.hash == localEquivalent.hash;
......@@ -56,7 +54,7 @@ module.exports = {
position = middle(topBlock.number, bottomBlock.number);
}
else {
let upperBlock = yield dao.getRemoteBlock(fork.peer, lookBlock.number + 1);
let upperBlock = (yield dao.getRemoteBlock(fork.peer, lookBlock.number + 1))[0];
let localUpper = yield dao.getLocalBlock(upperBlock.number);
let isCommonUpper = upperBlock.hash == localUpper.hash;
if (isCommonUpper) {
......@@ -79,16 +77,15 @@ module.exports = {
}
if (!wrongRemotechain) {
lookBlock = yield dao.getRemoteBlock(fork.peer, position);
lookBlock = (yield dao.getRemoteBlock(fork.peer, position))[0];
localEquivalent = yield dao.getLocalBlock(position);
}
} while (!commonRoot && !wrongRemotechain);
}
// Otherwise common root is unreachable
return Q(commonRoot);
return commonRoot;
});
};
return dao;
},
......
......@@ -127,7 +127,12 @@ function BlockchainService () {
this.submitBlock = (obj, doCheck, forkAllowed) => this.pushFIFO(() => checkAndAddBlock(obj, doCheck, forkAllowed));
const checkAndAddBlock = (obj, doCheck, forkAllowed) => co(function *() {
try {
Transaction.statics.setIssuers(obj.transactions);
}
catch (e) {
throw e;
}
let existing = yield dal.getBlockByNumberAndHashOrNull(obj.number, obj.hash);
if (existing) {
throw 'Already processed';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment