From 0ed0158d72ff60fc6e983e0197a33f7768e2fd17 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Thu, 21 Aug 2014 13:31:46 +0200
Subject: [PATCH] Add compute-distances command

---
 app/models/key.js              |  7 +++++++
 app/models/link.js             | 10 ++++++++++
 app/service/KeychainService.js | 19 +++++++++++++++++++
 bin/ucoind                     | 11 +++++++++++
 4 files changed, 47 insertions(+)

diff --git a/app/models/key.js b/app/models/key.js
index 4c0bae5fe..814da9dbf 100644
--- a/app/models/key.js
+++ b/app/models/key.js
@@ -146,4 +146,11 @@ KeySchema.statics.unsetKicked = function(fingerprint, done){
   });
 };
 
+KeySchema.statics.undistanceEveryKey = function(done){
+  var Key = this.model('Key');
+  Key.update({}, { kick: false, distanced: [] }, function (err) {
+    done(err);
+  });
+};
+
 module.exports = KeySchema;
diff --git a/app/models/link.js b/app/models/link.js
index 174daa474..ba17beb1f 100644
--- a/app/models/link.js
+++ b/app/models/link.js
@@ -30,6 +30,16 @@ LinkSchema.statics.obsoletes = function (minTimestamp, done) {
   });
 }
 
+/**
+* Unmark obsolete from all the links
+**/
+LinkSchema.statics.unobsoletesAllLinks = function (done) {
+  var Link = this.model('Link');
+  Link.update({}, { obsolete: false }, function (err) {
+    done(err);
+  });
+}
+
 /**
 * Mark as obsolete the links with an age equal to or below a given date
 **/
diff --git a/app/service/KeychainService.js b/app/service/KeychainService.js
index 71fb8a37a..08fe9eb82 100644
--- a/app/service/KeychainService.js
+++ b/app/service/KeychainService.js
@@ -1065,6 +1065,25 @@ function KeyService (conn, conf, PublicKeyService) {
     done(null, block);
   }
 
+  this.computeDistances = function (done) {
+    var current;
+    async.waterfall([
+      function (next) {
+        KeyBlock.current(next);
+      },
+      function (currentBlock, next) {
+        current = currentBlock;
+        Link.unobsoletesAllLinks(next);
+      },
+      function (next) {
+        Key.undistanceEveryKey(next);
+      },
+      function (next) {
+        computeObsoleteLinks(current, next);
+      }
+    ], done);
+  }
+
   this.prove = function (block, sigFunc, nbZeros, done) {
     var powRegexp = new RegExp('^0{' + nbZeros + '}');
     var pow = "", sig = "", raw = "";
diff --git a/bin/ucoind b/bin/ucoind
index e2f4166f7..6b15fdee6 100755
--- a/bin/ucoind
+++ b/bin/ucoind
@@ -303,6 +303,17 @@ program
   .description('Tries to generate a newcomers (#2+) keyblock, containing only newcomers changes')
   .action(service(DO_NOT_LISTEN_HTTP, ucoin.createWOTServer, generateAndSend("generateNewcomers")));
 
+program
+  .command('compute-distances')
+  .description('Recompute distance between each member and the whole WoT and mark outdistanced ones as kicked')
+  .action(service(DO_NOT_LISTEN_HTTP, ucoin.createWOTServer, function (server, conf) {
+    server.KeychainService.computeDistances(function (err) {
+      if (err) logger.error(err);
+      server.disconnect();
+      process.exit();
+    });
+  }));
+
 function generateAndSend (generationMethod) {
   return function (host, port, difficulty, server, conf) {
     async.waterfall([
-- 
GitLab