Skip to content

Commit

Permalink
Improve network validation formatting (#1344)
Browse files Browse the repository at this point in the history
* Improve node filtering based on network id

* Improve filter function format
  • Loading branch information
Kuki145 authored Sep 18, 2020
1 parent 4aae30b commit fad5a7a
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions modules/network/kademlia/kademlia.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class Kademlia {
if (!fs.existsSync(peerCacheFilePath)) {
fs.writeFileSync(peerCacheFilePath, '{}');
} else {
this._filterPeerCache(peerCacheFilePath);
this._filterContacts(peerCacheFilePath);
}

this.node.rolodex = this.node.plugin(kadence.rolodex(peerCacheFilePath));
Expand Down Expand Up @@ -277,7 +277,7 @@ class Kademlia {
if (entry) {
this.log.info(`Connected to network via ${entry}`);
this.log.info(`Discovered ${this.node.router.size} peers from seed`);
this._filterPeerCache(peerCacheFilePath);
this._filterContacts(peerCacheFilePath);
}
resolve();
});
Expand Down Expand Up @@ -893,13 +893,18 @@ class Kademlia {
}


_filterContacts() {

_filterRoutingTable() {
const message = {};
const nodesToRemove = [];

this.node.router.forEach((value, key, map) => {
if (value.length > 0) {
value.forEach((bValue, bKey, bMap) => {
if (bValue.network_id !== this.config.network.id) {
nodesToRemove.push(bKey);
} else {
message[bKey] = bValue;
}
});
}
Expand All @@ -909,18 +914,6 @@ class Kademlia {
this.node.router.removeContactByNodeId(nod);
}

const message = {};
this.node.router.forEach((value, key, map) => {
if (value.length > 0) {
value.forEach((bValue, bKey, bMap) => {
if (bValue.network_id !== this.config.network.id) {
nodesToRemove.push(bKey);
}
message[bKey] = bValue;
});
}
});

return message;
}

Expand All @@ -936,9 +929,14 @@ class Kademlia {
}
}

this._filterContacts();

fs.writeFileSync(peerCacheFilePath, JSON.stringify(peerCache));

return peerCache;
}

_filterContacts(peerCacheFilePath) {
this._filterPeerCache(peerCacheFilePath);
this._filterRoutingTable();
}
}

Expand Down

0 comments on commit fad5a7a

Please sign in to comment.