From b96394de14a877fed7b21d2fc241d1822a837ee8 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Sun, 8 Oct 2017 17:52:41 +0200
Subject: [PATCH] [fix] #1132 Too many "Could not reach" errors

---
 app/lib/common-libs/constants.ts |  2 ++
 app/lib/constants.ts             |  4 ++--
 app/lib/streams/multicaster.ts   | 29 +++++++++++++++++++++++------
 3 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/app/lib/common-libs/constants.ts b/app/lib/common-libs/constants.ts
index 67e5f12c0..371262f83 100644
--- a/app/lib/common-libs/constants.ts
+++ b/app/lib/common-libs/constants.ts
@@ -150,6 +150,8 @@ export const CommonConstants = {
     CANNOT_ROOT_BLOCK_NO_MEMBERS:         { httpCode: 400, uerr: { ucode: 2018, message: "Wrong new block: cannot make a root block without members" }},
     IDENTITY_WRONGLY_SIGNED:              { httpCode: 400, uerr: { ucode: 2019, message: "Weird, the signature is wrong and in the database." }},
     TOO_OLD_IDENTITY:                     { httpCode: 400, uerr: { ucode: 2020, message: "Identity has expired and cannot be written in the blockchain anymore." }},
+    NEWER_PEER_DOCUMENT_AVAILABLE:        { httpCode: 409, uerr: { ucode: 2022, message: "A newer peer document is available" }},
+    PEER_DOCUMENT_ALREADY_KNOWN:          { httpCode: 400, uerr: { ucode: 2023, message: "Peer document already known" }},
     TX_INPUTS_OUTPUTS_NOT_EQUAL:          { httpCode: 400, uerr: { ucode: 2024, message: "Transaction inputs sum must equal outputs sum" }},
     TX_OUTPUT_SUM_NOT_EQUALS_PREV_DELTAS: { httpCode: 400, uerr: { ucode: 2025, message: "Transaction output base amount does not equal previous base deltas" }},
     BLOCKSTAMP_DOES_NOT_MATCH_A_BLOCK:    { httpCode: 400, uerr: { ucode: 2026, message: "Blockstamp does not match a block" }},
diff --git a/app/lib/constants.ts b/app/lib/constants.ts
index 4eb1fbb7c..cac2f7a07 100644
--- a/app/lib/constants.ts
+++ b/app/lib/constants.ts
@@ -58,8 +58,8 @@ module.exports = {
     IDENTITY_WRONGLY_SIGNED:              CommonConstants.ERRORS.IDENTITY_WRONGLY_SIGNED,
     TOO_OLD_IDENTITY:                     CommonConstants.ERRORS.TOO_OLD_IDENTITY,
     NO_IDTY_MATCHING_PUB_OR_UID:          { httpCode: 404, uerr: { ucode: 2021, message: "No identity matching this pubkey or uid" }},
-    NEWER_PEER_DOCUMENT_AVAILABLE:        { httpCode: 409, uerr: { ucode: 2022, message: "A newer peer document is available" }},
-    PEER_DOCUMENT_ALREADY_KNOWN:          { httpCode: 400, uerr: { ucode: 2023, message: "Peer document already known" }},
+    NEWER_PEER_DOCUMENT_AVAILABLE:        CommonConstants.ERRORS.NEWER_PEER_DOCUMENT_AVAILABLE,
+    PEER_DOCUMENT_ALREADY_KNOWN:          CommonConstants.ERRORS.PEER_DOCUMENT_ALREADY_KNOWN,
     TX_INPUTS_OUTPUTS_NOT_EQUAL:          CommonConstants.ERRORS.TX_INPUTS_OUTPUTS_NOT_EQUAL,
     TX_OUTPUT_SUM_NOT_EQUALS_PREV_DELTAS: CommonConstants.ERRORS.TX_OUTPUT_SUM_NOT_EQUALS_PREV_DELTAS,
     BLOCKSTAMP_DOES_NOT_MATCH_A_BLOCK:    CommonConstants.ERRORS.BLOCKSTAMP_DOES_NOT_MATCH_A_BLOCK,
diff --git a/app/lib/streams/multicaster.ts b/app/lib/streams/multicaster.ts
index 7ba3b8265..860a04a00 100644
--- a/app/lib/streams/multicaster.ts
+++ b/app/lib/streams/multicaster.ts
@@ -8,6 +8,7 @@ import {CertificationDTO} from "../dto/CertificationDTO"
 import {MembershipDTO} from "../dto/MembershipDTO"
 import {TransactionDTO} from "../dto/TransactionDTO"
 import {PeerDTO} from "../dto/PeerDTO"
+import {CommonConstants} from "../common-libs/constants"
 
 const request = require('request');
 const constants = require('../../lib/constants');
@@ -111,13 +112,29 @@ export class Multicaster extends stream.Transform {
       },
       getDocID: (doc:PeerDTO) => doc.keyID() + '#' + doc.blockNumber(),
       withIsolation: WITH_ISOLATION,
-      onError: (resJSON:{ peer:{ block:string, endpoints:string[] }}, peering:any, to:any) => {
-        const sentPeer = PeerDTO.fromJSONObject(peering)
-        if (PeerDTO.blockNumber(resJSON.peer.block) > sentPeer.blockNumber()) {
-          this.push({ outdated: true, peer: resJSON.peer });
-          logger.warn('Outdated peer document (%s) sent to %s', sentPeer.keyID() + '#' + sentPeer.blockNumber(), to);
+      onError: (resJSON:{
+        peer: {
+          block:string,
+          endpoints:string[]
+        },
+        ucode?:number,
+        message?:string
+      }, peering:any, to:any) => {
+        if (resJSON.ucode !== undefined && resJSON.ucode !== CommonConstants.ERRORS.NEWER_PEER_DOCUMENT_AVAILABLE.uerr.ucode) {
+          if (resJSON.ucode == CommonConstants.ERRORS.DOCUMENT_BEING_TREATED.uerr.ucode || resJSON.ucode == constants.ERRORS.PEER_DOCUMENT_ALREADY_KNOWN.uerr.ucode) {
+            return Promise.resolve()
+          } else {
+            throw Error(resJSON.message)
+          }
+        } else {
+          // Handle possibly outdated peering document
+          const sentPeer = PeerDTO.fromJSONObject(peering)
+          if (PeerDTO.blockNumber(resJSON.peer.block) > sentPeer.blockNumber()) {
+            this.push({ outdated: true, peer: resJSON.peer });
+            logger.warn('Outdated peer document (%s) sent to %s', sentPeer.keyID() + '#' + sentPeer.blockNumber(), to);
+          }
+          return Promise.resolve()
         }
-        return Promise.resolve();
       }
     })(doc, peers)
   }
-- 
GitLab