Skip to content

Commit

Permalink
Merge pull request #1345 from OriginTrail/prerelease/testnet
Browse files Browse the repository at this point in the history
OriginTrail Testnet Release v4.1.10
  • Loading branch information
Kuki145 authored Sep 18, 2020
2 parents 101209e + c6142a6 commit 20e5980
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 18 deletions.
74 changes: 71 additions & 3 deletions modules/network/kademlia/kademlia.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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();
});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -802,9 +826,6 @@ class Kademlia {
return false;
}

if (this.config.requireApproval && !this.approvalService.isApproved(identity)) {
return false;
}
return true;
}

Expand Down Expand Up @@ -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;
6 changes: 3 additions & 3 deletions modules/transpiler/wot/wot-otjson-transpiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class WotOtJsonTranspiler {
'@value': property.name,
},
],
properties: property.values,
properties: { data: property.values },
relations: [],
};

Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "origintrail_node",
"version": "4.1.9",
"version": "4.1.10",
"description": "OriginTrail node",
"main": ".eslintrc.js",
"config": {
Expand Down
11 changes: 0 additions & 11 deletions testnet/register-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down

0 comments on commit 20e5980

Please sign in to comment.