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

No funciono' el autoconnect a peers

parent 33d5a71a
No related branches found
No related tags found
No related merge requests found
...@@ -9,13 +9,14 @@ var bfa = new Libbfa(); ...@@ -9,13 +9,14 @@ var bfa = new Libbfa();
var web3 = bfa.newweb3(); var web3 = bfa.newweb3();
var lastUnlock = 0; var lastUnlock = 0;
function readPeersCache() function readPeersCache()
{ {
if ( ! bfa.fs.existsSync( bfa.networkdir + '/peers.cache' ) ) if ( ! bfa.fs.existsSync( bfa.networkdir + '/peers.cache' ) )
return []; return [];
var data = bfa.fs.readFileSync( bfa.networkdir + '/peers.cache' ).toString(); var data = bfa.fs.readFileSync( bfa.networkdir + '/peers.cache' ).toString();
var p = [];
if ( data.length > 0 ) 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 some odd reason, I keep seeing empty entries
for ( var i = p.length; i > 0; i-- ) for ( var i = p.length; i > 0; i-- )
if ( p[i] == '' ) if ( p[i] == '' )
...@@ -23,20 +24,20 @@ function readPeersCache() ...@@ -23,20 +24,20 @@ function readPeersCache()
return p; return p;
} }
function writePeersCache( peers ) function writePeersCache( peers )
{ {
// max 100 entries, FIFO // max 100 entries, FIFO
if (peers.length > 100) if (peers.length > 100)
peers.splice( 0, 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( var filename = bfa.networkdir + '/peers.cache';
bfa.networkdir + '/peers.cache', var txt = peers.join("\n");
peers.join("\n") + "\n", if (txt.length > 0 && (txt.substring(txt.length-1) != "\n"))
{ mode: 0o644 } txt += "\n";
); bfa.fs.writeFileSync( filename, txt, { mode: 0o644 } );
} }
function writeStatus( peers ) function writeStatus( peers )
{ {
// write network/status // write network/status
bfa.fs.writeFileSync( bfa.fs.writeFileSync(
...@@ -77,7 +78,7 @@ function gotAdminPeers( nodelist ) ...@@ -77,7 +78,7 @@ function gotAdminPeers( nodelist )
{ {
var nowpeers = []; var nowpeers = [];
var peerscache = readPeersCache(); var peerscache = readPeersCache();
var outpeers = []; var newoutpeers = [];
var currentnodes = []; var currentnodes = [];
// The nodelist also contains peers which are not yet validated // The nodelist also contains peers which are not yet validated
...@@ -97,20 +98,27 @@ function gotAdminPeers( nodelist ) ...@@ -97,20 +98,27 @@ function gotAdminPeers( nodelist )
// See if this node reported by geth is already a known peers // See if this node reported by geth is already a known peers
// from our peers.cache // from our peers.cache
if (( peerscache.indexOf( n.info ) == -1 ) && ( n.dir == 'out' )) if (( peerscache.indexOf( n.info ) == -1 ) && ( n.dir == 'out' ))
outpeers.push( n.info ); newoutpeers.push( n.info );
} }
); );
writeStatus( nowpeers ); writeStatus( nowpeers );
writePeersCache( peerscache.concat(outpeers) ); writePeersCache( newoutpeers.concat(peerscache) );
// 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 ) if ( nowpeers.length < 5 )
{ {
var candidatenew = []; var candidatenew = [];
// find candidate nodes which we can connect to // find candidate nodes which we can connect to
currentnodes.forEach( // (it comes from peers.cache)
function(n) { peerscache.forEach(
if ( peerscache.indexOf( n.info ) == -1 ) function(acachedpeer) {
candidatenew.push( n.info ); 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 ) if ( candidatenew.length > 0 )
...@@ -134,19 +142,17 @@ function peerlist() ...@@ -134,19 +142,17 @@ function peerlist()
web3.bfa.admin.peers( gotAdminPeers ); web3.bfa.admin.peers( gotAdminPeers );
} }
function mayseal()
// Function to determine if our defaultAccount is allowed to seal/mine. // Function to determine if our defaultAccount is allowed to seal/mine.
// It will adjust the behaviour accordingly, i.e. stop or start mining. // It will adjust the behaviour accordingly, i.e. stop or start mining.
function mayseal()
{ {
var me = web3.eth.defaultAccount; var me = web3.eth.defaultAccount;
if ( undefined == me ) if ( undefined == me )
{
// Failed to get default account information. // Failed to get default account information.
me = 'xxxx' me = 'xxxx'
}
me = me.toLowerCase(); me = me.toLowerCase();
web3.eth.isMining(). web3.eth.isMining()
then( .then(
// returns a boolean whether or not we are currently mining/sealing. // returns a boolean whether or not we are currently mining/sealing.
function( isMining ) function( isMining )
{ {
...@@ -179,7 +185,7 @@ function mayseal() ...@@ -179,7 +185,7 @@ function mayseal()
function failedToGetIsMiningBool(x) function failedToGetIsMiningBool(x)
{ {
// Probably geth is not running. // Probably geth is not running.
throw new Error(x); //throw new Error(x);
} }
); );
} }
...@@ -188,6 +194,7 @@ function unlock() ...@@ -188,6 +194,7 @@ function unlock()
{ {
if ( lastUnlock + 600 > Date.now() / 1000 ) if ( lastUnlock + 600 > Date.now() / 1000 )
return; return;
var unlockedsomething = false;
web3.bfa.personal.listWallets( web3.bfa.personal.listWallets(
function pushone(x) function pushone(x)
{ {
...@@ -208,13 +215,39 @@ function unlock() ...@@ -208,13 +215,39 @@ function unlock()
var addr = wallets[i].accounts[j].address; var addr = wallets[i].accounts[j].address;
var promise = var promise =
web3.eth.personal.unlockAccount( addr, "", 0 ) web3.eth.personal.unlockAccount( addr, "", 0 )
.catch( error => { } ); .then( x => {
if ( x )
{
console.log( "Unlocked " + addr );
}
} )
.catch( error => {} );
promises.push( promise ); 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() function timer()
......
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