Skip to content

Instantly share code, notes, and snippets.

@niksmac
Created April 1, 2016 01:51
Show Gist options
  • Save niksmac/a22254ef5b6560cf584cb401d8b2b33d to your computer and use it in GitHub Desktop.
Save niksmac/a22254ef5b6560cf584cb401d8b2b33d to your computer and use it in GitHub Desktop.
Ethereum Blockchain Mine only when there is a transaction
var mining_threads = 1
function checkWork() {
if (eth.getBlock("pending").transactions.length > 0) {
if (eth.mining) return;
console.log("== Pending transactions! Mining...");
miner.start(mining_threads);
} else {
miner.stop(0); // This param means nothing
console.log("== No transactions! Mining stopped.");
}
}
eth.filter("latest", function(err, block) { checkWork(); });
eth.filter("pending", function(err, block) { checkWork(); });
checkWork();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment