Last active
June 3, 2017 01:46
-
-
Save TravisMullen/b7f2530e16d348635dc98e42ef3edec3 to your computer and use it in GitHub Desktop.
Smart Mining Ethernode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global eth, miner, admin, net, web3 */ | |
// For Ethereum Networks with light activity | |
// automatically enable and disbale miner | |
// | |
// set var `thread` in global geth instance to change CPUs | |
// | |
// in geth console `geth attach` | |
// > loadScript('eth-smart-mine.js') | |
// | |
// adhered to standardjs.com/ | |
// (function (eth, miner, admin, net, web3) { | |
'use strict' | |
var threads = 1 | |
// change fallback for correct networkid!! | |
eth.defaultAccount = eth.accounts[0] || '0xbcf647384f0d9fac697264449fe3baf1508e5350' | |
function checkWork (_err, _block) { | |
if (_err) { | |
console.log('ERROR xx ', _err) | |
return | |
} | |
console.log('+ ~ + ~ + + ~ + ~ + + ~ + ~ + + ~ + ~ + ~ + + ~ + ~ + + ~ + ~ +') | |
console.log('Block ', JSON.stringify(_block)) | |
console.log('Pending Transactions', eth.pendingTransactions.length) | |
if (eth.pendingTransactions.length > 0) { | |
if (eth.mining) return | |
console.log('== Pending transactions! Mining. Threads: ', threads) | |
miner.start(threads) | |
} else { | |
miner.stop() | |
console.log('== No transactions! Mining stopped.') | |
} | |
console.log('+ ~ + ~ + + ~ + ~ + + ~ + ~ + + ~ + ~ + ~ + + ~ + ~ + + ~ + ~ +') | |
} | |
eth.filter('latest', function (err, block) { | |
checkWork(err, block) | |
}) | |
eth.filter('pending', function (err, block) { | |
checkWork(err, block) | |
}) | |
// check for base to be set! | |
if (!eth.coinbase) { | |
miner.setEtherbase(eth.defaultAccount) | |
} | |
if (net.listening) { | |
console.log('+ ~ + ~ + + ~ + ~ + + ~ + ~ + + ~ + ~ + ~ + + ~ + ~ + + ~ + ~ +') | |
console.log(' * Smart Mining Active *') | |
console.log('+ ~ + ~ + + ~ + ~ + + ~ + ~ + + ~ + ~ + ~ + + ~ + ~ + + ~ + ~ +') | |
console.log('Etherbase ', eth.coinbase) | |
console.log('CPU Threads ', threads) | |
console.log('Network Id ', net.version) | |
console.log('Peers ', admin.peers.length) | |
console.log('+ ~ + ~ + + ~ + ~ + + ~ + ~ + + ~ + ~ + ~ + + ~ + ~ + + ~ + ~ +') | |
} else { | |
console.log(net) | |
console.log('You are not connected to an Ethereum JSON-RPC') | |
} | |
function setThreads (_ts) { | |
threads = _ts | |
return threads | |
} | |
// helpers | |
function balance (acct) { | |
return web3.fromWei(web3.eth.getBalance(acct), 'ether').toNumber() | |
} | |
// get all balances | |
function checkAllBalances () { | |
var totalBal = 0 | |
console.log('+ ~ + ~ + + ~ + ~ + + ~ + ~ + + ~ + ~ + ~ + + ~ + ~ + + ~ + ~ +') | |
console.log(' * All Balances *') | |
for (var acctNum in eth.accounts) { | |
var acct = eth.accounts[acctNum] | |
var acctBal = web3.fromWei(eth.getBalance(acct), 'ether') | |
totalBal += parseFloat(acctBal) | |
console.log('+ ~ + ~ + + ~ + ~ + + ~ + ~ + + ~ + ~ + ~ + + ~ + ~ + + ~ + ~ +') | |
console.log('Reference ', acct) | |
console.log('Account Number ', acctNum) | |
console.log('Balance ' + acctBal + ' ETH') | |
console.log('+ ~ + ~ + + ~ + ~ + + ~ + ~ + + ~ + ~ + ~ + + ~ + ~ + + ~ + ~ +') | |
} | |
console.log('TOTAL ' + totalBal + ' ETH') | |
console.log('+ ~ + ~ + + ~ + ~ + + ~ + ~ + + ~ + ~ + ~ + + ~ + ~ + + ~ + ~ +') | |
} | |
// return { | |
// balance: balance, | |
// checkAllBalances: checkAllBalances, | |
// checkWork: checkWork, | |
// threads: threads, | |
// setThreads: setThreads | |
// } | |
// })(eth, miner, admin, net, web3) | |
// var tools = { /* ignore */ | |
// balance: balance, | |
// checkAllBalances: checkAllBalances, | |
// checkWork: checkWork, | |
// threads: threads, | |
// setThreads: setThreads | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment