Skip to content

Commit

Permalink
fix some rolodex bugs and turn eclipse prevention back on
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyannehall committed Nov 20, 2024
1 parent 26870ea commit f908225
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
6 changes: 4 additions & 2 deletions bin/kadence.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env sh
':' //; exec "$(command -v nodejs || command -v node)" "$0" "$@"
':' //; exec "$(command -v node || command -v nodejs)" "$0" "$@"

'use strict';

Expand Down Expand Up @@ -196,8 +196,10 @@ async function _init() {
await identity.solve();
fs.writeFileSync(config.IdentityNoncePath, identity.nonce.toString());
fs.writeFileSync(config.IdentityProofPath, identity.proof);
logger.info('identity solution found!');
}
logger.info(`identity proof is ${identity.proof.toString('hex')} / ${identity.nonce}`);
init();
}
Expand Down Expand Up @@ -272,7 +274,7 @@ async function init() {
node.content = node.plugin(kadence.contentaddress({
valueEncoding: 'hex'
}));
// node.eclipse = node.plugin(kadence.eclipse(identity)); // NB: Equihash is busted
node.eclipse = node.plugin(kadence.eclipse(identity)); // NB: Equihash is busted
node.rolodex = node.plugin(kadence.rolodex(config.EmbeddedPeerCachePath));
// Check if we need to enable the churn filter plugin (experimental)
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
dockerfile: ./Dockerfile.dev
environment:
- kadence_TestNetworkEnabled=1
- kadence_VerboseLoggingEnabled=1
- kadence_ControlSockEnabled=0
- kadence_ControlPortEnabled=1
- kadence_TraverseNatEnabled=0
Expand All @@ -24,6 +25,7 @@ services:
dockerfile: ./Dockerfile.dev
environment:
- kadence_TestNetworkEnabled=1
- kadence_VerboseLoggingEnabled=1
- kadence_TraverseNatEnabled=0
- kadence_NodePublicAddress=kadence-2
- kadence_NetworkBootstrapNodes=http://kadence-1:5274
Expand All @@ -38,6 +40,7 @@ services:
dockerfile: ./Dockerfile.dev
environment:
- kadence_TestNetworkEnabled=1
- kadence_VerboseLoggingEnabled=1
- kadence_TraverseNatEnabled=0
- kadence_NodePublicAddress=kadence-3
- kadence_NetworkBootstrapNodes=http://kadence-1:5274
Expand All @@ -52,6 +55,7 @@ services:
dockerfile: ./Dockerfile.dev
environment:
- kadence_TestNetworkEnabled=1
- kadence_VerboseLoggingEnabled=1
- kadence_TraverseNatEnabled=0
- kadence_NodePublicAddress=kadence-4
- kadence_NetworkBootstrapNodes=http://kadence-1:5274
Expand All @@ -66,6 +70,7 @@ services:
dockerfile: ./Dockerfile.dev
environment:
- kadence_TestNetworkEnabled=1
- kadence_VerboseLoggingEnabled=1
- kadence_TraverseNatEnabled=0
- kadence_NodePublicAddress=kadence-5
- kadence_NetworkBootstrapNodes=http://kadence-1:5274
Expand Down
26 changes: 18 additions & 8 deletions lib/plugin-rolodex.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

'use strict';

const fs = require('fs');
const ms = require('ms');
const fs = require('node:fs');
const utils = require('./utils');
const { EventEmitter } = require('node:events');

Expand Down Expand Up @@ -32,12 +33,12 @@ class RolodexPlugin extends EventEmitter {
super();

this._peerCacheFilePath = peerCacheFilePath;
this._cache = {};
this._cache = { t: 0 };
this.node = node;

// When a contact is added to the routing table, cache it
this.node.router.events.on('add', identity => {
this.node.logger.debug(`updating cached peer profile ${identity}`);
this.node.logger.info(`updating cached peer profile ${identity}`);
const contact = this.node.router.getContactByNodeId(identity);
if (contact) {
contact.timestamp = Date.now();
Expand Down Expand Up @@ -83,7 +84,7 @@ class RolodexPlugin extends EventEmitter {
_syncToFile() {
return new Promise((resolve, reject) => {
if (!this._peerCacheFilePath) {
return resolve();
return reject(new Error('No peer cache path defined'));
}

fs.writeFile(
Expand All @@ -106,7 +107,11 @@ class RolodexPlugin extends EventEmitter {
_syncFromFile() {
return new Promise((resolve, reject) => {
if (!this._peerCacheFilePath) {
return resolve();
return reject(new Error('No peer cache path defined'));
}

if (!fs.existsSync(this._peerCacheFilePath)) {
fs.writeFileSync(this._peerCacheFilePath, JSON.stringify(this._cache));
}

fs.readFile(this._peerCacheFilePath, (err, data) => {
Expand All @@ -115,7 +120,11 @@ class RolodexPlugin extends EventEmitter {
}

try {
this._cache = JSON.parse(data.toString());
let _diskCache = JSON.parse(data.toString());

if (this._cache.t - _diskCache.t < ms('1m')) {
this._cache = JSON.parse(data.toString());
}
} catch (err) {
return reject(err);
}
Expand Down Expand Up @@ -189,7 +198,8 @@ class RolodexPlugin extends EventEmitter {
setExternalPeerInfo(identity, data) {
return new Promise((resolve) => {
this._cache[`${RolodexPlugin.EXTERNAL_PREFIX}:${identity}`] = data;
resolve(data);
this._cache.t = Date.now();
setImmediate(() => resolve(data));
});
}

Expand All @@ -202,7 +212,7 @@ class RolodexPlugin extends EventEmitter {
setInternalPeerInfo(identity, data) {
return new Promise((resolve) => {
this._cache[`${RolodexPlugin.INTERNAL_PREFIX}:${identity}`] = data;
resolve(data);
setImmediate(() => resolve(data));
});
}

Expand Down
19 changes: 10 additions & 9 deletions test/plugin-rolodex.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const rolodex = require('../lib/plugin-rolodex');
const sinon = require('sinon');
const RoutingTable = require('../lib/routing-table');
const utils = require('../lib/utils');
const path = require('path');
const os = require('os');
const path = require('node:path');
const os = require('node:os');


describe('@module kadence/rolodex', function() {
Expand All @@ -33,12 +33,12 @@ describe('@module kadence/rolodex', function() {

it('should store the contact in the db', function(done) {
let contact1 = {
hostname: 'localhost',
hostname: '127.0.0.1',
port: 8080,
protocol: 'http:'
};
let contact2 = {
hostname: 'localhost',
hostname: '127.0.0.1',
port: 8081,
protocol: 'http:'
};
Expand All @@ -47,12 +47,13 @@ describe('@module kadence/rolodex', function() {
node.router.addContactByNodeId(nodeid2, contact2);
setTimeout(function() {
plugin.getBootstrapCandidates().then(function(peers) {
expect(peers[0]).to.equal(`http://localhost:8081/#${nodeid2}`);
expect(peers[1]).to.equal(`http://localhost:8080/#${nodeid1}`);
console.log(peers)
expect(peers[0]).to.equal(`http://127.0.0.1:8081/#${nodeid2}`);
expect(peers[1]).to.equal(`http://127.0.0.1:8080/#${nodeid1}`);
done();
}, done);
}, 20);
}, 20);
}, 100);
}, 100);
});

describe('@class RolodexPlugin', function() {
Expand All @@ -61,7 +62,7 @@ describe('@module kadence/rolodex', function() {

it('should return the peer info', function(done) {
plugin.getExternalPeerInfo(nodeid1).then(contact => {
expect(contact.hostname).to.equal('localhost');
expect(contact.hostname).to.equal('127.0.0.1');
expect(contact.port).to.equal(8080);
expect(contact.protocol).to.equal('http:');
done();
Expand Down

0 comments on commit f908225

Please sign in to comment.