Skip to content

Instantly share code, notes, and snippets.

@xavierlepretre
Last active May 25, 2019 10:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xavierlepretre/601ed5a9946e12108051e11599c89a36 to your computer and use it in GitHub Desktop.
Save xavierlepretre/601ed5a9946e12108051e11599c89a36 to your computer and use it in GitHub Desktop.
web3.eth.filter("pending").watch(function() {
if (eth.mining) return;
console.log(new Date() + "-- Transactions detected, so starting mining.");
miner.start(1);
});
web3.eth.filter('latest', function(error, result) {
console.log(new Date() + "-- Got latest, so stopping mining");
miner.stop();
if (txpool.status.pending > 0) {
console.log(new Date() + "-- There were in fact pending transactions, so starting mining.");
miner.start(1);
}
});
if (txpool.status.pending) {
console.log(new Date() + "-- Pending transactions on startup, so starting mining.");
miner.start(1);
}
console.log(new Date() + "-- Started on-demand mining. Watching txpool for pending Txs..");
web3.eth.getBlockNumber(function(error, result) {
if (result.toString(10) == "0") {
console.log(new Date() + "-- empty chain. Mining first block");
miner.start(1);
}
});
@xavierlepretre
Copy link
Author

xavierlepretre commented Aug 19, 2016

Use as in:

$ geth js conditional_mining.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment