From 38eefbfdedc268e81f2703b19a4fdb691c30eaa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Martin-Leg=C3=A8ne?= <robert@nic.ar> Date: Sun, 25 Nov 2018 19:42:55 -0300 Subject: [PATCH] Unlocking feature now works. Also snuck in a few new features. --- bin/bfaupdate.sh | 10 ++++++ bin/libbfa.js | 15 ++++++--- bin/monitor.js | 80 ++++++++++++++++++++++++++++++++++++++++++------ bin/start.sh | 48 ++++------------------------- 4 files changed, 97 insertions(+), 56 deletions(-) create mode 100644 bin/bfaupdate.sh diff --git a/bin/bfaupdate.sh b/bin/bfaupdate.sh new file mode 100644 index 0000000..f6109cc --- /dev/null +++ b/bin/bfaupdate.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# Robert Martin-Legene <robert@nic.ar> + +if [ -z "${BFAHOME}" ]; then echo "\$BFAHOME not set. Did you source bfa/bin/env ?" >&2; exit 1; fi +source ${BFAHOME}/bin/libbfa.sh || exit 1 + +set -x +cd ${BFAHOME} +git pull +npm rebuild diff --git a/bin/libbfa.js b/bin/libbfa.js index d0bf483..3b09cda 100644 --- a/bin/libbfa.js +++ b/bin/libbfa.js @@ -81,32 +81,39 @@ module.exports = class Libbfa w3.eth.extend({ //property: 'bfaclique', methods: [{ - name: 'getSigners', + name: 'bfaGetSigners', call: 'clique_getSigners', params: 0 }] }); w3.eth.extend({ methods: [{ - name: 'minerstart', + name: 'bfaMinerstart', call: 'miner_start', params: 0 }] }); w3.eth.extend({ methods: [{ - name: 'minerstop', + name: 'bfaMinerstop', call: 'miner_stop', params: 0 }] }); w3.eth.extend({ methods: [{ - name: 'adminpeers', + name: 'bfaAdminpeers', call: 'admin_peers', params: 0 }] }); + w3.eth.personal.extend({ + methods: [{ + name: 'bfalistWallets', + call: 'personal_listWallets', + params: 0 + }] + }); if ( undefined != process.env.BFAACCOUNT ) { w3.eth.defaultAccount = this.account; } diff --git a/bin/monitor.js b/bin/monitor.js index 0d5eaa0..576d795 100755 --- a/bin/monitor.js +++ b/bin/monitor.js @@ -5,16 +5,15 @@ "use strict" const Libbfa = require( process.env.BFAHOME + '/bin/libbfa.js'); +var bfa = new Libbfa(); +var web3 = bfa.newweb3(); +var now; function monitor() { - var bfa = new Libbfa(); - var web3 = bfa.newweb3(); - var now = new Date(); - //console.log(web3.eth); - web3.eth.adminpeers().then( - function(nodelist) { - var peers = []; + web3.eth.bfaAdminpeers().then( + function gotAdminPeers( nodelist ) { + var peers = []; nodelist.forEach( function(node) { if ( typeof(node.protocols.eth) == 'object' ) @@ -34,12 +33,73 @@ function monitor() { mode: 0o644 } ); }, - function(x) + function failedToGetAdminPeers(x) { console.log( x ); - process.exit(1) } ); } -monitor(); +// 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 ) + { + console.log( "Failed to get default account information." ); + return; + } + me = me.toLowerCase(); + web3.eth.isMining(). + then( + // returns a boolean whether or not we are currently mining/sealing. + function( isMining ) + { + // Get a list of clique.getSigners, so we can see if we are + // in the list of authorized sealers. + web3.eth.bfaGetSigners() + .then( + function gotListOfSealers(x) + { + var lcsealers = x.map( name => name.toLowerCase() ); + var isSigner = (lcsealers.indexOf(me) > -1); + if ( isSigner ) + { + if ( ! isMining ) + { + console.log( 'Started to seal.' ); + web3.eth.bfaMinerstart(); + } + } + else + { + if ( isMining ) + { + console.log( 'I was trying to seal, but am not authorized. Stopped trying.' ); + web3.eth.bfaMinerstop(); + } + } + }, + function failedToGetListOfSealers(x) + { + console.log(x); + } + ); + }, + function failedToGetIsMiningBool(x) + { + // Probably geth is not running. + console.log(x); + } + ); +} + +function timer() +{ + now = new Date(); + monitor(); + mayseal(); +} + +setInterval( timer, 60 * 1000 ); diff --git a/bin/start.sh b/bin/start.sh index 74a395d..0fc1bcf 100755 --- a/bin/start.sh +++ b/bin/start.sh @@ -14,39 +14,11 @@ enodeDGSI="59ae768ecdee632e0daceccb6f71b215392eba89230d626573f2fb4e9c0786c9a6610 bootDGSIv4="enode://${enodeDGSI}@[200.108.146.100]:30301" bootnodes="${bootARIUv6},${bootARIUv4},${bootUNCv4},${bootDGSIv4}" -function accountlist -{ - local accts= - local filename - for filename in ${BFANODEDIR}/keystore/*--* - do - if [ -r "$filename" -a ${#filename} -ge 40 ] - then - acct=${filename:$(( ${#filename} - 40 ))} - accts="${accts},${acct}" - fi - done - # strip leading comma - accts=${accts#,} - if [ ${#accts} -ge 40 ] - then - echo "--password /dev/null --unlock ${accts}" - fi -} - # touch the "miner" file if you are authorized to mine # If you don't want to restart after touching the file, # you can use attach.sh and issue the command: # miner.start() -function getminer -{ - if [ -e "${BFANODEDIR}/miner" ] - then - echo "--mine" - fi -} - function getsyncmode { local syncmode=$( cat "${BFANODEDIR}/syncmode" 2>/dev/null || true ) @@ -96,17 +68,9 @@ function startmonitor echo "A monitor is already running." false ) || exit - if [ -t 1 ] - then - echo Running monitor every 60 seconds. - fi - while : - do - monitor.js & - echo $! > $pidfile - wait - sleep 60 - done & + monitor.js & + echo $! > $pidfile + wait ) 9>> $pidfile } @@ -134,12 +98,11 @@ function startgeth echo '***' echo # (re)configure parameters (you never know if they changed) - flexargs="$( accountlist) $( getminer ) $( getsyncmode ) --extradata $( extradata )" + flexargs="$( getsyncmode ) --extradata $( extradata )" set -x geth \ --datadir ${BFANODEDIR} \ --networkid ${BFANETWORKID} \ - --bootnodes "${bootnodes}" \ --rpc \ --rpcport $rpcport \ --rpcapi "eth,net,web3,admin,clique,miner,personal" \ @@ -148,7 +111,8 @@ function startgeth --gcmode archive \ --cache 512 \ --verbosity ${BFAVERBOSITY:-3} \ - ${flexargs} & + ${flexargs} \ + --bootnodes "${bootnodes}" & set +x echo $! > ${BFANODEDIR}/geth.pid wait -- GitLab