From 31b62d6ad84da67d0e2ab77a2b5ebc3ddacbc770 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Moreau?= <cem.moreau@gmail.com>
Date: Sun, 10 Jun 2018 11:41:27 +0200
Subject: [PATCH] [enh] sync: usage of `--nopeers` and `--nosbx`

---
 app/lib/common-libs/programOptions.ts |  4 ++++
 app/modules/crawler/index.ts          |  6 +++---
 app/modules/crawler/lib/sync.ts       | 31 ++++++++++++++++-----------
 3 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/app/lib/common-libs/programOptions.ts b/app/lib/common-libs/programOptions.ts
index 0274130de..4d85ae5b2 100644
--- a/app/lib/common-libs/programOptions.ts
+++ b/app/lib/common-libs/programOptions.ts
@@ -17,6 +17,8 @@ export interface ProgramOptions {
   mdb?: string
   home?: string
   notrim?: boolean
+  nosbx?: boolean
+  nopeers?: boolean
   syncTrace?: string
 }
 
@@ -24,5 +26,7 @@ export const cliprogram: ProgramOptions = {
   mdb: opts.mdb,
   home: opts.home,
   notrim: opts.notrim,
+  nosbx: opts.nosbx,
+  nopeers: opts.nopeers,
   syncTrace: opts['sync-trace'],
 }
diff --git a/app/modules/crawler/index.ts b/app/modules/crawler/index.ts
index eb34d602a..e5f5098c5 100644
--- a/app/modules/crawler/index.ts
+++ b/app/modules/crawler/index.ts
@@ -65,6 +65,7 @@ export const CrawlerDependency = {
       { value: '--nocautious',    desc: 'Do not check blocks validity during sync.'},
       { value: '--cautious',      desc: 'Check blocks validity during sync (overrides --nocautious option).'},
       { value: '--nopeers',       desc: 'Do not retrieve peers during sync.'},
+      { value: '--nosbx',         desc: 'Do not retrieve sandboxes during sync.'},
       { value: '--onlypeers',     desc: 'Will only try to sync peers.'},
       { value: '--slow',          desc: 'Download slowly the blokchcain (for low connnections).'},
       { value: '--readfilesystem',desc: 'Also read the filesystem to speed up block downloading.'},
@@ -98,7 +99,6 @@ export const CrawlerDependency = {
         const chunkLength = 0;
         const interactive = !program.nointeractive;
         const askedCautious = cautious;
-        const nopeers = program.nopeers;
         const noShufflePeers = program.noshuffle;
 
         let otherDAL = undefined
@@ -112,9 +112,9 @@ export const CrawlerDependency = {
 
         const remote = new Synchroniser(server, onHost, onPort, interactive === true, program.slow === true, otherDAL);
         if (program.onlypeers === true) {
-          return remote.syncPeers(nopeers, true, onHost, onPort)
+          return remote.syncPeers(true, onHost, onPort)
         } else {
-          return remote.sync(upTo, chunkLength, askedCautious, nopeers, noShufflePeers === true)
+          return remote.sync(upTo, chunkLength, askedCautious, noShufflePeers === true)
         }
       }
     }, {
diff --git a/app/modules/crawler/lib/sync.ts b/app/modules/crawler/lib/sync.ts
index 388b973ca..ffc453149 100644
--- a/app/modules/crawler/lib/sync.ts
+++ b/app/modules/crawler/lib/sync.ts
@@ -34,6 +34,7 @@ import {CommonConstants} from "../../../lib/common-libs/constants"
 import {Underscore} from "../../../lib/common-libs/underscore"
 import {HttpMerkleOfPeers} from "../../bma/lib/dtos"
 import {DBPeer, JSONDBPeer} from "../../../lib/db/DBPeer"
+import {cliprogram} from "../../../lib/common-libs/programOptions"
 
 const multimeter   = require('multimeter');
 const makeQuerablePromise = require('querablep');
@@ -131,7 +132,7 @@ export class Synchroniser extends stream.Duplex {
     return node.getCurrent();
   }
 
-  async sync(to:number, chunkLen:number, askedCautious = false, nopeers = false, noShufflePeers = false) {
+  async sync(to:number, chunkLen:number, askedCautious = false, noShufflePeers = false) {
 
     try {
 
@@ -164,7 +165,7 @@ export class Synchroniser extends stream.Duplex {
       // Peers (just for P2P download)
       //=======
       let peers:(JSONDBPeer|null)[] = [];
-      if (!nopeers && (to - localNumber > 1000)) { // P2P download if more than 1000 blocs
+      if (!cliprogram.nopeers && (to - localNumber > 1000)) { // P2P download if more than 1000 blocs
         this.watcher.writeStatus('Peers...');
         const merkle = await this.dal.merkleForPeers();
         const getPeers:(params:any) => Promise<HttpMerkleOfPeers> = node.getPeers.bind(node);
@@ -320,16 +321,20 @@ export class Synchroniser extends stream.Duplex {
       await this.BlockchainService.saveParametersForRootBlock(BlockDTO.fromJSONObject(rootBlock))
       this.server.dal.blockDAL.cleanCache();
 
-      //=======
-      // Sandboxes
-      //=======
-      this.watcher.writeStatus('Synchronizing the sandboxes...');
-      await pullSandboxToLocalServer(this.conf.currency, node, this.server, this.server.logger, this.watcher, 1, false)
+      if (!cliprogram.nosbx) {
+        //=======
+        // Sandboxes
+        //=======
+        this.watcher.writeStatus('Synchronizing the sandboxes...');
+        await pullSandboxToLocalServer(this.conf.currency, node, this.server, this.server.logger, this.watcher, 1, false)
+      }
 
-      //=======
-      // Peers
-      //=======
-      await this.syncPeers(nopeers, fullSync, this.host, this.port, to)
+      if (!cliprogram.nopeers) {
+        //=======
+        // Peers
+        //=======
+        await this.syncPeers(fullSync, this.host, this.port, to)
+      }
 
       // Trim the loki data
       await this.server.dal.loki.flushAndTrimData()
@@ -345,8 +350,8 @@ export class Synchroniser extends stream.Duplex {
     }
   }
 
-  async syncPeers(nopeers:boolean, fullSync:boolean, host:string, port:number, to?:number) {
-    if (!nopeers && fullSync) {
+  async syncPeers(fullSync:boolean, host:string, port:number, to?:number) {
+    if (!cliprogram.nopeers && fullSync) {
 
       const peering = await Contacter.fetchPeer(host, port, this.contacterOptions);
 
-- 
GitLab