From 3fce8a17842375abd637ae540353eb377f44cf69 Mon Sep 17 00:00:00 2001
From: Benoit Lavenier <benoit.lavenier@e-is.pro>
Date: Mon, 5 Jun 2023 11:20:47 +0200
Subject: [PATCH] fix(sync): don't lose nodes on single download err -
 cherry-pick of commit 7a59f46 from branch `dev`

---
 .../crawler/lib/sync/RemoteSynchronizer.ts    | 12 +++-----
 .../crawler/lib/sync/p2p/p2p-candidate.ts     | 29 ++++++++++++-------
 2 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/app/modules/crawler/lib/sync/RemoteSynchronizer.ts b/app/modules/crawler/lib/sync/RemoteSynchronizer.ts
index 0dff93c71..a39350128 100644
--- a/app/modules/crawler/lib/sync/RemoteSynchronizer.ts
+++ b/app/modules/crawler/lib/sync/RemoteSynchronizer.ts
@@ -155,21 +155,17 @@ export class RemoteSynchronizer extends AbstractSynchronizer {
       // If we know this is a WS2P connection, don't try BMA
       if (access.isWS2P !== true) {
         try {
+          endpoint =
+              [port == 443 ? "BMAS" : "BASIC_MERKLED_API", host, port].join(" ") +
+              (path ? (" " + path) : "");
           const contacter = await connect(
             PeerDTO.fromJSONObject({
-              endpoints: [
-                [port == 443 ? "BMAS" : "BASIC_MERKLED_API", host, port].join(
-                  " "
-                ) + (path ? " " + path : ""),
-              ],
+              endpoints: [endpoint],
             }),
             3000
           );
           peering = await contacter.getPeer();
           api = new BMARemoteContacter(contacter);
-          endpoint =
-            [port == 443 ? "BMAS" : "BASIC_MERKLED_API", host, port].join(" ") +
-            (path ? " " + path : "");
         } catch (e) {}
       }
 
diff --git a/app/modules/crawler/lib/sync/p2p/p2p-candidate.ts b/app/modules/crawler/lib/sync/p2p/p2p-candidate.ts
index 56fca9b04..65366d71e 100644
--- a/app/modules/crawler/lib/sync/p2p/p2p-candidate.ts
+++ b/app/modules/crawler/lib/sync/p2p/p2p-candidate.ts
@@ -90,21 +90,30 @@ export class P2pCandidate {
     this.dlPromise = querablep(
       (async () => {
         // We try to download the blocks
-        let blocks: BlockDTO[] | null;
-        try {
-          blocks = await (this.api as IRemoteContacter).getBlocks(count, from);
-        } catch (e) {
-          // Unfortunately this can fail
-          blocks = null;
-          error = e;
+        let blocks: BlockDTO[] = [];
+        let tries = 5;
+        while (tries > 0) {
+          try {
+            blocks = await (this.api as IRemoteContacter).getBlocks(
+              count,
+              from
+            );
+          } catch (e) {
+            // Unfortunately this can fail
+            blocks = [];
+            this.logger.error(e);
+          }
+          if (blocks.length != count) {
+            this.logger.error("Wrong number of blocks from %s", this.hostName);
+            tries--;
+          } else {
+            break;
+          }
         }
         this.responseTimes.push(Date.now() - start);
         // Only keep a flow of 5 ttas for the node
         if (this.responseTimes.length > 5) this.responseTimes.shift();
         this.nbSuccess++;
-        if (error) {
-          throw error;
-        }
         return blocks;
       })()
     );
-- 
GitLab