From 08fac26131ac87573863248ffa09e5c3e94cb3ab Mon Sep 17 00:00:00 2001 From: Ahmed Al-Maliki Date: Tue, 25 May 2021 21:49:10 +0300 Subject: [PATCH] Preventing pushing the current node's URL to its networkNodes. When you post a newNodeUrl to the node with the same URL it gets saved with the rest of the network URLS, for example: if you post http://localhost:3000 to http://localhost:3001/register-and-brodcast-node, you will have the URL in the networkNodes despite its being the same URL for that node. my change prevents that and it makes sure you can not post a node's URL to its self. --- dev/networkNode.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dev/networkNode.js b/dev/networkNode.js index e02ac83..b3cb613 100644 --- a/dev/networkNode.js +++ b/dev/networkNode.js @@ -127,8 +127,14 @@ app.post('/receive-new-block', function(req, res) { // register a node and broadcast it the network app.post('/register-and-broadcast-node', function(req, res) { const newNodeUrl = req.body.newNodeUrl; - if (bitcoin.networkNodes.indexOf(newNodeUrl) == -1) bitcoin.networkNodes.push(newNodeUrl); - + const nodeNotAlreadyPresent = bitcoin.networkNodes.indexOf(newNodeUrl) == -1; + const notCurrentNode = bitcoin.currentNodeUrl !== newNodeUrl; + if (nodeNotAlreadyPresent && notCurrentNode){ + bitcoin.networkNodes.push(newNodeUrl); + }else{ + res.json({note: 'Node was not added!'}); + } + const regNodesPromises = []; bitcoin.networkNodes.forEach(networkNodeUrl => { const requestOptions = {