From c5669eb3121be00ef0bb326a3c32719ee8a80b0f Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Thu, 14 Apr 2016 22:39:12 +0200
Subject: [PATCH] Fix: follow a fork that with exact or more than 30 minutes of
 time advance

---
 app/service/BlockchainService.js    | 2 +-
 test/integration/branches_switch.js | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/app/service/BlockchainService.js b/app/service/BlockchainService.js
index 9e2a2b487..a42703c71 100644
--- a/app/service/BlockchainService.js
+++ b/app/service/BlockchainService.js
@@ -178,7 +178,7 @@ function BlockchainService () {
       let blocksAdvance = constants.BRANCHES.SWITCH_ON_BRANCH_AHEAD_BY_X_MINUTES / (conf.avgGenTime / 60);
       let timeAdvance = constants.BRANCHES.SWITCH_ON_BRANCH_AHEAD_BY_X_MINUTES * 60;
       // We switch only to blockchain with X_MIN advance considering both theoretical time by block + written time
-      potentials = _.filter(potentials, (p) => p.number - current.number > blocksAdvance && p.medianTime - current.medianTime > timeAdvance);
+      potentials = _.filter(potentials, (p) => p.number - current.number >= blocksAdvance && p.medianTime - current.medianTime >= timeAdvance);
       logger.trace('SWITCH: %s branches...', branches.length);
       logger.trace('SWITCH: %s potential side chains...', potentials.length);
       for (let i = 0, len = potentials.length; i < len; i++) {
diff --git a/test/integration/branches_switch.js b/test/integration/branches_switch.js
index e4df44f8c..503fb15ea 100644
--- a/test/integration/branches_switch.js
+++ b/test/integration/branches_switch.js
@@ -9,6 +9,7 @@ var rp        = require('request-promise');
 var httpTest  = require('./tools/http');
 var commit    = require('./tools/commit');
 var sync      = require('./tools/sync');
+var constants = require('./../../app/lib/constants');
 
 var expectJSON     = httpTest.expectJSON;
 var expectHttpCode = httpTest.expectHttpCode;
@@ -20,6 +21,7 @@ var commonConf = {
   currency: 'bb',
   httpLogs: true,
   forksize: 3,
+  avgGenTime: 1,
   parcatipate: false, // TODO: to remove when startGeneration will be an explicit call
   sigQty: 1
 };
@@ -80,7 +82,9 @@ describe("Switch", function() {
       // So we now have:
       // S1 01234
       // S2   `3456789
+      let oldVal = constants.BRANCHES.SWITCH_ON_BRANCH_AHEAD_BY_X_MINUTES = 0;
       yield sync(3, 8, s2, s1);
+      constants.BRANCHES.SWITCH_ON_BRANCH_AHEAD_BY_X_MINUTES = oldVal;
       // S1 should have switched to the other branch
     });
   });
-- 
GitLab