From 680876785195e0762aa095635d44b8486c8897d9 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Mon, 9 Oct 2017 15:04:57 +0200
Subject: [PATCH] [fix] #1142 Avoid possible memory leak with HEAD cache

---
 app/modules/ws2p/lib/WS2PCluster.ts | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/app/modules/ws2p/lib/WS2PCluster.ts b/app/modules/ws2p/lib/WS2PCluster.ts
index 5543a640b..d607e9c2f 100644
--- a/app/modules/ws2p/lib/WS2PCluster.ts
+++ b/app/modules/ws2p/lib/WS2PCluster.ts
@@ -116,8 +116,8 @@ export class WS2PCluster {
             // More recent?
             if (!this.headsCache[fullId] || parseInt(this.headsCache[fullId].blockstamp) < parseInt(blockstamp)) {
               // Check that issuer is a member and that the block exists
-              const memberKey = await this.isMemberKey(pub)
-              if (memberKey) {
+              const isAllowed = pub === this.server.conf.pair.pub || this.isConnectedKey(pub) || (await this.isMemberKey(pub))
+              if (isAllowed) {
                 const exists = await this.existsBlock(blockstamp)
                 if (exists) {
                   this.headsCache[fullId] = { blockstamp, message, sig }
@@ -152,8 +152,8 @@ export class WS2PCluster {
             // More recent?
             if (!this.headsCache[fullId] || parseInt(this.headsCache[fullId].blockstamp) < parseInt(blockstamp)) {
               // Check that issuer is a member and that the block exists
-              const memberKey = await this.isMemberKey(pub)
-              if (memberKey) {
+              const isAllowed = pub === this.server.conf.pair.pub || this.isConnectedKey(pub) || (await this.isMemberKey(pub))
+              if (isAllowed) {
                 const exists = await this.existsBlock(blockstamp)
                 if (exists) {
                   this.headsCache[fullId] = { blockstamp, message, sig }
@@ -195,11 +195,17 @@ export class WS2PCluster {
       // Do we have this block in the DB?
       isMember = !!(await this.server.dal.isMember(pub))
     }
-    // Update the last time it was checked
-    this.memberkeysCache[pub] = Date.now()
+    if (isMember) {
+      // Update the last time it was checked
+      this.memberkeysCache[pub] = Date.now()
+    }
     return isMember
   }
 
+  private isConnectedKey(pub:string) {
+    return this.getConnectedPubkeys().indexOf(pub) !== -1
+  }
+
   private async existsBlock(blockstamp:string) {
     let exists = false
     if (this.blockstampsCache[blockstamp]) {
-- 
GitLab