diff --git a/modules/network/kademlia/kademlia.js b/modules/network/kademlia/kademlia.js index adf6488bc0..3daee85bf1 100644 --- a/modules/network/kademlia/kademlia.js +++ b/modules/network/kademlia/kademlia.js @@ -213,6 +213,8 @@ class Kademlia { if (!fs.existsSync(peerCacheFilePath)) { fs.writeFileSync(peerCacheFilePath, '{}'); + } else { + this._filterContacts(peerCacheFilePath); } this.node.rolodex = this.node.plugin(kadence.rolodex(peerCacheFilePath)); @@ -242,6 +244,23 @@ class Kademlia { this._registerRoutes(); + // Override node's _updateContact method to filter contacts. + this.node._updateContact = (identity, contact) => { + try { + if (!this.validateContact(identity, contact)) { + this.log.debug(`Ignored contact ${identity}. Hostname ${contact.hostname}. Network ID ${contact.network_id}.`); + return; + } + } catch (err) { + this.log.debug(`Failed to filter contact(${identity}, ${contact}). ${err}.`); + return; + } + + // Simulate node's "super._updateContact(identity, contact)". + this.node.constructor.prototype.constructor.prototype + ._updateContact.call(this.node, identity, contact); + }; + this.node.listen(this.config.node_port, () => { this.log.notify(`OT Node listening at https://${this.node.contact.hostname}:${this.node.contact.port}`); this.kademliaUtilities.registerControlInterface(this.config, this.node); @@ -258,6 +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._filterContacts(peerCacheFilePath); } resolve(); }); @@ -420,6 +440,10 @@ class Kademlia { }); this.node.use('*', async (request, response, next) => { + if (!this.validateContact(request.contact[0], request.contact[1])) { + response.send('Contact belongs to a different network.'); + return; + } if (request.params.header) { const header = JSON.parse(request.params.header); if (header.ttl && header.from && header.to) { @@ -802,9 +826,6 @@ class Kademlia { return false; } - if (this.config.requireApproval && !this.approvalService.isApproved(identity)) { - return false; - } return true; } @@ -870,6 +891,53 @@ class Kademlia { } return null; } + + + + _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; + } + }); + } + }); + + for (const nod of nodesToRemove) { + this.node.router.removeContactByNodeId(nod); + } + + return message; + } + + _filterPeerCache(peerCacheFilePath) { + const peerCacheFile = fs.readFileSync(peerCacheFilePath); + + const peerCache = JSON.parse(peerCacheFile); + + for (const id in peerCache) { + const elem = peerCache[id]; + if (elem.network_id !== this.config.network.id) { + delete peerCache[id]; + } + } + + fs.writeFileSync(peerCacheFilePath, JSON.stringify(peerCache)); + + return peerCache; + } + + _filterContacts(peerCacheFilePath) { + this._filterPeerCache(peerCacheFilePath); + this._filterRoutingTable(); + } } module.exports = Kademlia; diff --git a/modules/transpiler/wot/wot-otjson-transpiler.js b/modules/transpiler/wot/wot-otjson-transpiler.js index 00fa5b66b8..66bbc4b94a 100644 --- a/modules/transpiler/wot/wot-otjson-transpiler.js +++ b/modules/transpiler/wot/wot-otjson-transpiler.js @@ -113,7 +113,7 @@ class WotOtJsonTranspiler { '@value': property.name, }, ], - properties: property.values, + properties: { data: property.values }, relations: [], }; @@ -149,7 +149,7 @@ class WotOtJsonTranspiler { const createRelation = (id, data) => ({ '@type': 'otRelation', relationType: 'PART_OF', - direction: 'reverse', // think about direction + direction: 'direct', // think about direction linkedObject: { '@id': id, }, @@ -317,7 +317,7 @@ class WotOtJsonTranspiler { const property = { id: otProperty.identifiers.find(x => x['@type'] === 'internal_id')['@value'], name: otProperty.identifiers.find(x => x['@type'] === 'name')['@value'], - values: properties, + values: properties.data, }; results.push(property); diff --git a/package.json b/package.json index afb431ba2a..7f6bc388f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "origintrail_node", - "version": "4.1.9", + "version": "4.1.10", "description": "OriginTrail node", "main": ".eslintrc.js", "config": { diff --git a/testnet/register-node.js b/testnet/register-node.js index bb3e2e76f5..236e92e5cc 100644 --- a/testnet/register-node.js +++ b/testnet/register-node.js @@ -242,17 +242,6 @@ function main() { logger.info('Identity given: ', process.env.ERC_725_IDENTITY); } - if (process.env.KAD_IDENTITY && process.env.KAD_IDENTITY_CHILD_INDEX) { - const identityFilePath = - path.join(localConfiguration.appDataPath, localConfiguration.identity_filepath); - const content = { - xprivkey: process.env.KAD_IDENTITY, - index: parseInt(process.env.KAD_IDENTITY_CHILD_INDEX, 10), - }; - fs.writeFileSync(identityFilePath, JSON.stringify(content, null, 4)); - logger.info('Kademlia identity given: ', process.env.KAD_IDENTITY); - } - if (process.env.IMPORT_WHITELIST) { if (!externalConfig.network) { externalConfig.network = {};