From 2b300f64830d0d5a362ac721db3e5007fe43a78b Mon Sep 17 00:00:00 2001 From: Abhishek Sinha Date: Wed, 6 Feb 2019 20:35:17 +0530 Subject: [PATCH] added initial functions for kademlia --- supernode/index.html | 102 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 4 deletions(-) diff --git a/supernode/index.html b/supernode/index.html index 567e5b2..ba66e3e 100644 --- a/supernode/index.html +++ b/supernode/index.html @@ -9142,7 +9142,10 @@ } const incumbent = node.contacts[index] - const selection = this.arbiter(incumbent, contact) + + /***************Change made by Abhishek*************/ + //const selection = this.arbiter(incumbent, contact) + const selection = localbitcoinplusplus.kademlia.arbiter(incumbent, contact); // if the selection is our old contact and the candidate is some new // contact, then there is nothing to do if (selection === incumbent && incumbent !== contact) return @@ -9753,7 +9756,49 @@ } } - + + /*Modified functions from https://github.com/tristanls/k-bucket */ + localbitcoinplusplus.kademlia = { + + decodeBase58Address: function(blockchain, address) { + let k = bitjs[blockchain].Base58.decode(address) + k.shift() + k.splice(-4, 4) + return Crypto.util.bytesToHex(k) + }, + addContact: function(id, data) { + const nodeId = new Uint8Array(id.length) + for (let i = 0, len = nodeId.length; i < len; ++i) { + nodeId[i] = id.charCodeAt(i) + } + const contact = { + id: nodeId, + data: data + }; + KBucket.add(contact) + }, + addNewUserNodeInKbucket: function(blockchain, address, data) { + const decodedId = this.decodeBase58Address(blockchain, address); + const addNewUserNode = this.addContact(decodedId, data); + return addNewUserNode; + }, + arbiter: function(incumbent, candidate) { + // we create a new object so that our selection is guaranteed to replace + // the incumbent + const merged = { + id: incumbent.id, // incumbent.id === candidate.id within an arbiter + data: incumbent.data + } + + Object.keys(candidate.data).forEach(workerNodeId => { + merged.data[workerNodeId] = candidate.data[workerNodeId]; + }) + + return merged; + }, + + } + + + \ No newline at end of file