From e7ff2deaf01f55e326277d57f13736c9a48c89d1 Mon Sep 17 00:00:00 2001 From: Robert Martin-Legene <robert@nic.ar> Date: Mon, 27 May 2019 18:56:26 -0300 Subject: [PATCH] No funciono' el autoconnect a peers --- bin/monitor.js | 81 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/bin/monitor.js b/bin/monitor.js index 7ebd99b..b5cf795 100755 --- a/bin/monitor.js +++ b/bin/monitor.js @@ -9,13 +9,14 @@ var bfa = new Libbfa(); var web3 = bfa.newweb3(); var lastUnlock = 0; -function readPeersCache() +function readPeersCache() { if ( ! bfa.fs.existsSync( bfa.networkdir + '/peers.cache' ) ) return []; var data = bfa.fs.readFileSync( bfa.networkdir + '/peers.cache' ).toString(); + var p = []; if ( data.length > 0 ) - var p = data.split(/\r?\n/); + p = data.split(/\r?\n/); // for some odd reason, I keep seeing empty entries for ( var i = p.length; i > 0; i-- ) if ( p[i] == '' ) @@ -23,20 +24,20 @@ function readPeersCache() return p; } -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. - bfa.fs.writeFileSync( - bfa.networkdir + '/peers.cache', - peers.join("\n") + "\n", - { mode: 0o644 } - ); + var filename = bfa.networkdir + '/peers.cache'; + var txt = peers.join("\n"); + if (txt.length > 0 && (txt.substring(txt.length-1) != "\n")) + txt += "\n"; + bfa.fs.writeFileSync( filename, txt, { mode: 0o644 } ); } -function writeStatus( peers ) +function writeStatus( peers ) { // write network/status bfa.fs.writeFileSync( @@ -77,7 +78,7 @@ function gotAdminPeers( nodelist ) { var nowpeers = []; var peerscache = readPeersCache(); - var outpeers = []; + var newoutpeers = []; var currentnodes = []; // The nodelist also contains peers which are not yet validated @@ -97,20 +98,27 @@ function gotAdminPeers( nodelist ) // 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 ); + newoutpeers.push( n.info ); } ); writeStatus( nowpeers ); - writePeersCache( peerscache.concat(outpeers) ); + writePeersCache( newoutpeers.concat(peerscache) ); // Try to connect to a random node if we have very few peers if ( nowpeers.length < 5 ) { var candidatenew = []; // find candidate nodes which we can connect to - currentnodes.forEach( - function(n) { - if ( peerscache.indexOf( n.info ) == -1 ) - candidatenew.push( n.info ); + // (it comes from peers.cache) + peerscache.forEach( + function(acachedpeer) { + if ( + // Add "a cached peer" to "candidate new" peers + // if the cached peer is not in the list of current node. + currentnodes.find( function(element) { element.info != acachedpeer } ) + ) + { + candidatenew.push( acachedpeer ); + } } ); if ( candidatenew.length > 0 ) @@ -134,19 +142,17 @@ function peerlist() web3.bfa.admin.peers( gotAdminPeers ); } +function mayseal() // Function to determine if our defaultAccount is allowed to seal/mine. // It will adjust the behaviour accordingly, i.e. stop or start mining. -function mayseal() { var me = web3.eth.defaultAccount; if ( undefined == me ) - { // Failed to get default account information. me = 'xxxx' - } me = me.toLowerCase(); - web3.eth.isMining(). - then( + web3.eth.isMining() + .then( // returns a boolean whether or not we are currently mining/sealing. function( isMining ) { @@ -179,7 +185,7 @@ function mayseal() function failedToGetIsMiningBool(x) { // Probably geth is not running. - throw new Error(x); + //throw new Error(x); } ); } @@ -188,6 +194,7 @@ function unlock() { if ( lastUnlock + 600 > Date.now() / 1000 ) return; + var unlockedsomething = false; web3.bfa.personal.listWallets( function pushone(x) { @@ -208,13 +215,39 @@ function unlock() var addr = wallets[i].accounts[j].address; var promise = web3.eth.personal.unlockAccount( addr, "", 0 ) - .catch( error => { } ); + .then( x => { + if ( x ) + { + console.log( "Unlocked " + addr ); + } + } ) + .catch( error => {} ); promises.push( promise ); + unlockedsomething = true; } } - lastUnlock = Date.now() / 1000; + Promise.all( promises ) + .then( + function() + { + if ( unlockedsomething ) + { + web3.eth.isMining() + .then( + function() + { + web3.bfa.miner.stop(); + web3.bfa.miner.start(); + } + ) + } + }, + function() + {} + ); } ); + lastUnlock = Date.now() / 1000; } function timer() -- GitLab