Commit b861217f authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Fix #622 Wait for the end of pulling process before trying to compute a new block

parent d099352a
......@@ -304,7 +304,6 @@ function BlockGenerator(notifier) {
// Initialize the engine
sendToProcess({ command: 'id', pubkey: pub, identifier: id });
interval = setInterval(() => {
logger.trace('%s interval to engine %s', pub.slice(0,6), id);
return sendToProcess({ command: 'idle' });
}, constants.ENGINE_IDLE_INTERVAL);
}
......
......@@ -65,6 +65,12 @@ function PermanentProver(server) {
throw 'Too high difficulty: waiting for other members to write next block';
}
const lastIssuedByUs = current.issuer == selfPubkey;
const pullingPromise = server.PeeringService.pullingPromise();
if (pullingPromise && !pullingPromise.isFulfilled()) {
logger.warn('Waiting for the end of pulling...');
yield pullingPromise;
logger.warn('Pulling done. Continue proof-of-work loop.');
}
if (lastIssuedByUs && !promiseOfWaitingBetween2BlocksOfOurs) {
promiseOfWaitingBetween2BlocksOfOurs = new Promise((resolve) => setTimeout(resolve, conf.powDelay));
logger.warn('Waiting ' + conf.powDelay + 'ms before starting to compute next block...');
......
......@@ -15,6 +15,7 @@ const hashf = require('../lib/ucp/hashf');
const rawer = require('../lib/ucp/rawer');
const pulling = require('../lib/pulling');
const constants = require('../lib/constants');
const querablep = require('../lib/querablep');
const Peer = require('../lib/entity/peer');
const Transaction = require('../lib/entity/transaction');
const AbstractService = require('./AbstractService');
......@@ -182,6 +183,8 @@ function PeeringService(server) {
syncBlock(done);
};
this.pullingPromise = () => currentSyncP;
this.pullBlocks = (pubkey) => syncBlock(null, pubkey);
const FIRST_CALL = true;
......@@ -498,7 +501,7 @@ function PeeringService(server) {
}
function syncBlock(callback, pubkey) {
currentSyncP = co(function *() {
currentSyncP = querablep(co(function *() {
let current = yield dal.getCurrentBlockOrNull();
if (current) {
pullingEvent('start', current.number);
......@@ -608,7 +611,7 @@ function PeeringService(server) {
pullingEvent('error');
logger.warn(err.code || err.stack || err.message || err);
callback && callback();
});
}));
return currentSyncP;
}
......
......@@ -106,4 +106,21 @@ describe("Continous proof-of-work", function() {
s3.until('block', 10)
];
}));
it('testing proof-of-work during a block pulling', () => co(function*() {
const res = yield toolbox.simpleNetworkOf2NodesAnd2Users({
participate: true,
powMin: 0
}), s2 = res.s1, s3 = res.s2;
yield s2.commit();
s2.conf.cpu = 1.0;
s2.startBlockComputation();
yield s2.until('block', 15);
s2.stopBlockComputation();
yield [
s3.PeeringService.pullBlocks(),
s3.startBlockComputation()
];
yield s3.expectJSON('/blockchain/current', { number: 15 });
}));
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment