From 232fe05cdf345f98f5277f781f5ccd60c7d853e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Sat, 29 Sep 2018 13:07:54 -0400
Subject: [PATCH] http server: add pending name cache

Change-Id: Ic80266a6243e092ac77b5da916304c443860b752
---
 index.js | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
 mode change 100644 => 100755 index.js

diff --git a/index.js b/index.js
old mode 100644
new mode 100755
index 22fa722..541c429
--- a/index.js
+++ b/index.js
@@ -61,6 +61,8 @@ var regData;
 var regContract;
 var reg;
 
+const cache = {};
+
 function unlockAccount() {
     web3.personal.unlockAccount(coinbase, "toto");
 }
@@ -216,7 +218,12 @@ function startServer() {
                     if (err)
                         console.log("Name lookup error: " + err);
                     if (isHashZero(res)) {
-                        http_res.status(404).end(JSON.stringify({"error": "name not registred"}));
+                        const cachedAddress = cache[req.params.name];
+                        if (cachedAddress != undefined) {
+                            http_res.end(JSON.stringify({"name": req.params.name,"addr": cachedAddress}));
+                        } else {
+                            http_res.status(404).end(JSON.stringify({"error": "name not registred"}));
+                        }
                     } else {
                         http_res.end(JSON.stringify({"name": req.params.name,"addr": res}));
                     }
@@ -331,12 +338,14 @@ function startServer() {
                                     } else {
                                         console.log("Transaction sent " + reg_c);
                                         // Send answer as soon as the transaction is queued
+                                        cache[req.params.name] = addr;
                                         http_res.end(JSON.stringify({"success": true}));
                                         web3.eth.awaitConsensus(reg_c, function(error) {
                                             if (error) {
                                                 console.log(error);
                                                 return;
                                             }
+                                            delete cache[req.params.name];
                                             console.log("Ended registration for " + req.params.name + " -> " + addr);
                                         });
                                     }
-- 
GitLab