Skip to content
Snippets Groups Projects
Commit 517188f7 authored by Robert Martin-Legene's avatar Robert Martin-Legene
Browse files

monitor.js did not save peers.cache ages - it now does again.

parent 98b54484
No related branches found
No related tags found
No related merge requests found
...@@ -25,6 +25,9 @@ function readPeersCache() ...@@ -25,6 +25,9 @@ function readPeersCache()
function writePeersCache( peers ) function writePeersCache( peers )
{ {
// max 100 entries, FIFO
if (peers.length > 100)
peers.splice( 0, peers.length - 100 );
// peers.cache is a list of peers we have connected out to in the past. // peers.cache is a list of peers we have connected out to in the past.
bfa.fs.writeFileSync( bfa.fs.writeFileSync(
bfa.networkdir + '/peers.cache', bfa.networkdir + '/peers.cache',
...@@ -47,12 +50,11 @@ function writeStatus( peers ) ...@@ -47,12 +50,11 @@ function writeStatus( peers )
function parsenode( node ) function parsenode( node )
{ {
if ( !n || !n.protocols || typeof n.protocols.eth != 'object' ) if ( !node || !node.protocols || typeof node.protocols.eth != 'object' )
return; return;
// default info - likely to get overwritten.
var n;
if ( ! node.network ) if ( ! node.network )
return { info: "<"+node.id+">" }; return { info: "<"+node.id+">" };
var n = {};
if ( typeof node.network.inbound == 'boolean' ) if ( typeof node.network.inbound == 'boolean' )
n.dir = node.network.inbound ? "in" : "out"; n.dir = node.network.inbound ? "in" : "out";
if ( typeof node.enode == 'string' ) if ( typeof node.enode == 'string' )
...@@ -74,42 +76,56 @@ function parsenode( node ) ...@@ -74,42 +76,56 @@ function parsenode( node )
function gotAdminPeers( nodelist ) function gotAdminPeers( nodelist )
{ {
var nowpeers = []; var nowpeers = [];
var peers = readPeersCache(); var peerscache = readPeersCache();
var outpeers = []; var outpeers = [];
var candidatenew = []; var currentnodes = [];
// The nodelist also contains peers which are not yet validated
// if they even belong to this network. Parsenode returns an
// object or nothing, based on our criteria
nodelist.forEach( nodelist.forEach(
function(node) { function(node) {
var n = parsenode( node ); var n = parsenode(node);
if ( n ) if ( n )
{ currentnodes.push( n );
nowpeers.push( "peer " + ( n.dir ? n.dir : '') + ": " + n.info ); }
if ( peers.indexOf( n.info ) == -1 ) );
{ currentnodes.forEach(
if ( n.dir == 'out' ) function(n) {
outpeers.push( n.info ); // Add to list of nowpeers (for stats file)
candidatenew.push( n.info ); nowpeers.push( "peer " + ( n.dir ? n.dir : '') + ": " + n.info );
} // See if this node reported by geth is already a known peers
} // from our peers.cache
if (( peerscache.indexOf( n.info ) == -1 ) && ( n.dir == 'out' ))
outpeers.push( n.info );
} }
); );
writeStatus( nowpeers ); writeStatus( nowpeers );
// write peers.cache (max 100 entries, FIFO) writePeersCache( peerscache.concat(outpeers) );
peers = peers.concat( outpeers );
if (peers.length > 100)
peers.splice( 0, peers.length - 100 );
writePeersCache( peers );
// Try to connect to a random node if we have very few peers // Try to connect to a random node if we have very few peers
if ( nowpeers.length < 5 && candidatenew.length > 0 ) if ( nowpeers.length < 5 )
{ {
var i = Math.floor( Math.random() * candidatenew.length ); var candidatenew = [];
var enode = candidatenew[i]; // find candidate nodes which we can connect to
console.log( currentnodes.forEach(
"We have " function(n) {
+ nowpeers.length if ( peerscache.indexOf( n.info ) == -1 )
+ " peer" + ( nowpeers.length==1 ? '' : 's' ) + ", so will try to connect to " candidatenew.push( n.info );
+ enode }
); );
web3.bfa.admin.addPeer( enode ); if ( candidatenew.length > 0 )
{
var i = Math.floor( Math.random() * candidatenew.length );
var enode = candidatenew[i];
console.log(
"We have "
+ nowpeers.length
+ " peer" + ( nowpeers.length==1 ? '' : 's' )
+ ", so will try to connect to "
+ enode
);
web3.bfa.admin.addPeer( enode );
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment