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

Merge branch 'dev'

parents 67838762 19222609
No related branches found
No related tags found
No related merge requests found
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
"use strict" "use strict"
var request = require('request'); var request = require('request');
var net = require('net');
module.exports = class Libbfa module.exports = class Libbfa
{ {
constructor() { constructor() {
this.fs = require('fs'); this.fs = require('fs');
this.Web3 = require('web3'); this.Web3 = require('web3');
var net = require('net');
// //
// BFAHOME // BFAHOME
if ( undefined == process.env.BFAHOME ) if ( undefined == process.env.BFAHOME )
...@@ -38,7 +38,7 @@ module.exports = class Libbfa ...@@ -38,7 +38,7 @@ module.exports = class Libbfa
files.push( filename ); files.push( filename );
}); });
} }
// found none? // found any?
if ( files.length > 0 ) if ( files.length > 0 )
{ {
files.sort(); files.sort();
...@@ -54,10 +54,13 @@ module.exports = class Libbfa ...@@ -54,10 +54,13 @@ module.exports = class Libbfa
this.socketurl = this.nodedir+'/geth.ipc'; // overwrite with newer ipc method this.socketurl = this.nodedir+'/geth.ipc'; // overwrite with newer ipc method
if ( this.sockettype == 'ipc' ) { if ( this.sockettype == 'ipc' ) {
this.provider = new this.Web3.providers.IpcProvider( this.nodedir+'/geth.ipc', net ); this.provider = new this.Web3.providers.IpcProvider( this.nodedir+'/geth.ipc', net );
this.req_url = 'http://unix:' + this.nodedir + '/geth.ipc:/';
} else if ( this.sockettype == 'ws' ) { } else if ( this.sockettype == 'ws' ) {
this.provider = new this.Web3.providers.WebsocketProvider( this.socketurl ); this.provider = new this.Web3.providers.WebsocketProvider( this.socketurl );
this.req_url = this.socketurl;
} else if ( this.sockettype == 'http') { } else if ( this.sockettype == 'http') {
this.provider = new this.Web3.providers.HttpProvider( this.socketurl ); this.provider = new this.Web3.providers.HttpProvider( this.socketurl );
this.req_url = this.socketurl;
} else { } else {
fatal("Unknown sockettype."); fatal("Unknown sockettype.");
} }
...@@ -92,20 +95,25 @@ module.exports = class Libbfa ...@@ -92,20 +95,25 @@ module.exports = class Libbfa
newweb3() newweb3()
{ {
var w3 = new this.Web3( this.provider ); var w3 = new this.Web3( this.provider );
var req_url = this.req_url;
var _bfa = this;
// This could just remain the same number all the time.
var unneededcounter = 1;
w3.jsonify = function( opname, params )
{
var obj = {};
obj.id = unneededcounter++;
obj.jsonrpc = "2.0";
obj.method = opname;
obj.params = params;
return obj;
};
w3.rpcreq = function( opname, params, callback ) w3.rpcreq = function( opname, params, callback )
{ {
var extra = params.join(',');
var body = JSON.parse("{"+
'"jsonrpc":"2.0",' +
'"id":1,' +
'"method":"' + opname + '",' +
'"params":[' + extra + ']'
+"}"
);
request.post({ request.post({
uri: 'http://localhost:8545', uri: req_url,
json: true, json: true,
body: body, body: w3.jsonify( opname, params ),
callback: function RPCresponse( err, obj ) callback: function RPCresponse( err, obj )
{ {
var r; var r;
...@@ -121,26 +129,74 @@ module.exports = class Libbfa ...@@ -121,26 +129,74 @@ module.exports = class Libbfa
} }
}); });
}; };
w3.req = function( opname, params, callback )
{
if ( _bfa.sockettype == 'ipc' )
{
w3.ipcreq( opname, params, callback );
}
else
{
w3.rpcreq( opname, params, callback );
}
}
w3.ipcreq = function( opname, params, callback )
{
var socket = net.connect( _bfa.socketurl );
var result;
var err;
socket.on("ready", () => {
// When the socket has been established.
// We create a new connection per request, because it
// is easier than reliably handling JSON object boundaries
// in a TCP stream .
// Writes out data and closes our end of the connection.
// Geth will reply and then close it's end.
socket.end( JSON.stringify( w3.jsonify(opname,params).valueOf()));
});
socket.on("data", (d) => {
try {
result = JSON.parse( d.toString() );
}
catch {
err = d.toString();
}
});
socket.on("timeout", () => {
socket.close();
});
socket.on("error", (e) => {
console.error(e);
err = e;
});
socket.on("close", () => {
if ( result.error && result.error.code && result.error.message )
err = 'Error ' + result.error.code + ": "+ result.error.message;
else
result = result.result;
callback( err, result );
});
}
w3.bfa = { w3.bfa = {
clique: { clique: {
getSigners: function clique_getSigners( cb ) getSigners: function clique_getSigners( cb )
{ w3.rpcreq( 'clique_getSigners', [], cb ) }, { w3.req( 'clique_getSigners', [], cb ) },
}, },
miner: { miner: {
start: function miner_start() start: function miner_start()
{ w3.rpcreq( 'miner_start', [], function(){} ) }, { w3.req( 'miner_start', [], function(){} ) },
stop: function miner_stop() stop: function miner_stop()
{ w3.rpcreq( 'miner_stop', [], function(){} ) } { w3.req( 'miner_stop', [], function(){} ) }
}, },
admin: { admin: {
peers: function admin_peers( cb ) peers: function admin_peers( cb )
{ w3.rpcreq( 'admin_peers', [], cb ) }, { w3.req( 'admin_peers', [], cb ) },
addPeer: function admin_addPeer( peer ) addPeer: function admin_addPeer( peer )
{ w3.rpcreq( 'admin_addPeer', [ "\""+peer+"\"" ], function(){} ) } { w3.req( 'admin_addPeer', [ peer ], function(){} ) }
}, },
personal: { personal: {
listWallets: function personal_listWallets( cb ) listWallets: function personal_listWallets( cb )
{ w3.rpcreq( 'personal_listWallets', [], cb ) } { w3.req( 'personal_listWallets', [], cb ) }
} }
}; };
if ( undefined != process.env.BFAACCOUNT ) { if ( undefined != process.env.BFAACCOUNT ) {
......
...@@ -275,8 +275,7 @@ if ( $result ) ...@@ -275,8 +275,7 @@ if ( $result )
$txn =~ s/^0x([a-fA-F\d]+)$/hex($1)/e; $txn =~ s/^0x([a-fA-F\d]+)$/hex($1)/e;
my $gold = rpc( $libbfa, 'eth_getBalance', qq("$account"), '"latest"' ); my $gold = rpc( $libbfa, 'eth_getBalance', qq("$account"), '"latest"' );
$gold = Math::BigInt->new( $gold ) if $gold =~ /^0x/; $gold = Math::BigInt->new( $gold ) if $gold =~ /^0x/;
#$gold = Math::BigInt->new( $gold ) if $gold =~ s/^0x([\da-fA-F]{2})/0x0000$1/; printf "Account %d: %s %-6s %3d transaction%s, %s wei.\n", $i, $account, $maymine, $txn, ($txn==1?' ':'s'), $gold;
printf "Account %d: %s %-6s %3d transaction%s, %s satoshi.\n", $i, $account, $maymine, $txn, ($txn==1?' ':'s'), $gold;
} }
} }
else else
......
...@@ -10,12 +10,15 @@ var bfa = new Libbfa(); ...@@ -10,12 +10,15 @@ var bfa = new Libbfa();
var web3 = bfa.newweb3(); var web3 = bfa.newweb3();
var lastUnlock = 0; var lastUnlock = 0;
var netid = 0; var netid = 0;
var peerscache = bfa.networkdir + '/peers.cache';
if ( bfa.fs.existsSync( bfa.networkdir + '/cache' ) )
peerscache = bfa.networkdir + '/cache/peers.cache';
function readPeersCache() function readPeersCache()
{ {
if ( ! bfa.fs.existsSync( bfa.networkdir + '/peers.cache' ) ) if ( ! bfa.fs.existsSync( peerscache ) )
return []; return [];
var data = bfa.fs.readFileSync( bfa.networkdir + '/peers.cache' ).toString(); var data = bfa.fs.readFileSync( peerscache ).toString();
var p = []; var p = [];
if ( data.length > 0 ) if ( data.length > 0 )
p = data.split(/\r?\n/); p = data.split(/\r?\n/);
...@@ -32,11 +35,10 @@ function writePeersCache( peers ) ...@@ -32,11 +35,10 @@ function writePeersCache( peers )
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.
var filename = bfa.networkdir + '/peers.cache';
var txt = peers.join("\n"); var txt = peers.join("\n");
if (txt.length > 0 && (txt.substring(txt.length-1) != "\n")) if (txt.length > 0 && (txt.substring(txt.length-1) != "\n"))
txt += "\n"; txt += "\n";
bfa.fs.writeFileSync( filename, txt, { mode: 0o644 } ); bfa.fs.writeFileSync( peerscache, txt, { mode: 0o644 } );
} }
function dnspeercachelookup() function dnspeercachelookup()
...@@ -212,7 +214,11 @@ function mayseal() ...@@ -212,7 +214,11 @@ function mayseal()
web3.bfa.clique.getSigners( web3.bfa.clique.getSigners(
function gotListOfSealers(e,x) function gotListOfSealers(e,x)
{ {
if (e) return; if (e)
{
console.error( e );
return;
}
var lcsealers = x.map( name => name.toLowerCase() ); var lcsealers = x.map( name => name.toLowerCase() );
var isSigner = (lcsealers.indexOf(me) > -1); var isSigner = (lcsealers.indexOf(me) > -1);
if ( isSigner ) if ( isSigner )
...@@ -312,7 +318,7 @@ function timer() ...@@ -312,7 +318,7 @@ function timer()
netid = x; netid = x;
} ) } )
.catch( err => { .catch( err => {
console.log("monitor.js non-fatal: "+err) console.error("monitor.js non-fatal: "+err)
}); });
return; return;
} }
......
...@@ -13,6 +13,7 @@ trap "exit 1" ERR ...@@ -13,6 +13,7 @@ trap "exit 1" ERR
trap "reaper" SIGINT SIGCHLD trap "reaper" SIGINT SIGCHLD
unset LOGDIR LOGPIPE PIDIDX unset LOGDIR LOGPIPE PIDIDX
declare -A PIDIDX declare -A PIDIDX
trap "killallprocs" SIGTERM
function reaper() function reaper()
{ {
...@@ -40,17 +41,21 @@ function reaper() ...@@ -40,17 +41,21 @@ function reaper()
max=30 max=30
} }
if [ "$VIRTUALIZATION" = "DOCKER" ] function killallprocs()
then {
echo "Some log info can be seen with: docker logs bfanode" if [ ${#PIDIDX[*]} -gt 0 ]
fi then
echo "*** Killing all remaining processes: ${PIDIDX[*]} (${!PIDIDX[*]})."
kill -KILL ${!PIDIDX[*]} 2>/dev/null || true
fi
}
# You can start as: # You can start as:
# BFAHOME=/home/bfa/bfa singlestart.sh # BFAHOME=/home/bfa/bfa singlestart.sh
# singlestart.sh /home/bfa/bfa # singlestart.sh /home/bfa/bfa
if [ -z "${BFAHOME}" -a -n "$1" -a -f "$1" ] if [ -z "${BFAHOME}" -a -n "$1" -a -f "$1" ]
then then
BFAHOME="$1" export BFAHOME="$1"
fi fi
if [ -z "${BFAHOME}" ]; then echo "\$BFAHOME not set. Did you source `dirname $0`/env ?" >&2; exit 1; fi if [ -z "${BFAHOME}" ]; then echo "\$BFAHOME not set. Did you source `dirname $0`/env ?" >&2; exit 1; fi
# #
...@@ -70,22 +75,26 @@ else ...@@ -70,22 +75,26 @@ else
fi fi
source ${BFAHOME}/bin/libbfa.sh source ${BFAHOME}/bin/libbfa.sh
echo "Logging mostly everything to ${BFANODEDIR}/log"
echo "Consider running: tail -n 1000 -F ${BFANODEDIR}/log"
if [ "$VIRTUALIZATION" = "DOCKER" ] if [ "$VIRTUALIZATION" = "DOCKER" ]
then then
echo "or: docker exec -i bfanode bfalog.sh" echo "See log info with \"docker logs\""
fi else
echo "Logging mostly everything to ${BFANODEDIR}/log"
echo "Consider running: tail -n 1000 -F ${BFANODEDIR}/log"
echo "or: bfalog.sh"
echo "*** Setting up logging." # Docker has it's own logging facility, so we will not use our own
# Clean up logging # logging functionality if we're in docker.
LOGDIR=$( mktemp -d ) echo "*** Setting up logging."
trap "rm -rf ${LOGDIR}" EXIT # Clean up logging
LOGPIPE=${LOGDIR}/logpipe LOGDIR=$( mktemp -d )
mknod ${LOGPIPE} p trap "rm -rf ${LOGDIR}" EXIT
${BFAHOME}/bin/log.sh ${BFANODEDIR}/log < ${LOGPIPE} & LOGPIPE=${LOGDIR}/logpipe
PIDIDX[$!]="log.sh" mknod ${LOGPIPE} p
exec > ${LOGPIPE} 2>&1 ${BFAHOME}/bin/log.sh ${BFANODEDIR}/log < ${LOGPIPE} &
PIDIDX[$!]="log.sh"
exec > ${LOGPIPE} 2>&1
fi
echo "*** Starting geth." echo "*** Starting geth."
# "NoPruning=true" means "--gcmode archive" # "NoPruning=true" means "--gcmode archive"
...@@ -112,8 +121,4 @@ do ...@@ -112,8 +121,4 @@ do
sleep 1 sleep 1
max=$(( $max - 1 )) max=$(( $max - 1 ))
done done
if [ ${#PIDIDX[*]} -gt 0 ] killallprocs
then
echo "*** Killing all remaining processes: ${PIDIDX[*]} (${!PIDIDX[*]})."
kill -KILL ${!PIDIDX[*]} 2>/dev/null || true
fi
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