diff --git a/bin/kadence.js b/bin/kadence.js index b48bdad..9c38af1 100755 --- a/bin/kadence.js +++ b/bin/kadence.js @@ -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'; @@ -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(); } @@ -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) diff --git a/docker-compose.yml b/docker-compose.yml index 4d48d39..d45bf85 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,7 @@ services: dockerfile: ./Dockerfile.dev environment: - kadence_TestNetworkEnabled=1 + - kadence_VerboseLoggingEnabled=1 - kadence_ControlSockEnabled=0 - kadence_ControlPortEnabled=1 - kadence_TraverseNatEnabled=0 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/plugin-rolodex.js b/lib/plugin-rolodex.js index 677ab8e..a0de309 100644 --- a/lib/plugin-rolodex.js +++ b/lib/plugin-rolodex.js @@ -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'); @@ -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(); @@ -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( @@ -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) => { @@ -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); } @@ -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)); }); } @@ -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)); }); } diff --git a/test/plugin-rolodex.unit.js b/test/plugin-rolodex.unit.js index f9daf3d..6535f5d 100644 --- a/test/plugin-rolodex.unit.js +++ b/test/plugin-rolodex.unit.js @@ -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() { @@ -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:' }; @@ -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() { @@ -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();