Skip to content
Snippets Groups Projects
Commit 232fe05c authored by Adrien Béraud's avatar Adrien Béraud
Browse files

http server: add pending name cache

Change-Id: Ic80266a6243e092ac77b5da916304c443860b752
parent 774aa9e0
No related branches found
No related tags found
No related merge requests found
index.js 100644 → 100755
...@@ -61,6 +61,8 @@ var regData; ...@@ -61,6 +61,8 @@ var regData;
var regContract; var regContract;
var reg; var reg;
const cache = {};
function unlockAccount() { function unlockAccount() {
web3.personal.unlockAccount(coinbase, "toto"); web3.personal.unlockAccount(coinbase, "toto");
} }
...@@ -216,7 +218,12 @@ function startServer() { ...@@ -216,7 +218,12 @@ function startServer() {
if (err) if (err)
console.log("Name lookup error: " + err); console.log("Name lookup error: " + err);
if (isHashZero(res)) { if (isHashZero(res)) {
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"})); http_res.status(404).end(JSON.stringify({"error": "name not registred"}));
}
} else { } else {
http_res.end(JSON.stringify({"name": req.params.name,"addr": res})); http_res.end(JSON.stringify({"name": req.params.name,"addr": res}));
} }
...@@ -331,12 +338,14 @@ function startServer() { ...@@ -331,12 +338,14 @@ function startServer() {
} else { } else {
console.log("Transaction sent " + reg_c); console.log("Transaction sent " + reg_c);
// Send answer as soon as the transaction is queued // Send answer as soon as the transaction is queued
cache[req.params.name] = addr;
http_res.end(JSON.stringify({"success": true})); http_res.end(JSON.stringify({"success": true}));
web3.eth.awaitConsensus(reg_c, function(error) { web3.eth.awaitConsensus(reg_c, function(error) {
if (error) { if (error) {
console.log(error); console.log(error);
return; return;
} }
delete cache[req.params.name];
console.log("Ended registration for " + req.params.name + " -> " + addr); console.log("Ended registration for " + req.params.name + " -> " + addr);
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment