Since on my private test newtork based on go-ethereum (geth) I want only the miner thread to start when there are pending transactions, I am using the following script:

var mining_threads = 1

function checkWork() {
    if (eth.getBlock("pending").transactions.length > 0) {
        if (eth.mining) return;
        console.log("== Pending transactions! Mining...");
    } 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(); });


However, it does start and stop the mining process, but not quite as expected. When I send a new transaction, the miner thread starts. Because of mining diffculty being intentionally low, it mines some additional coins before getting stopped. How can I stop this unintended behavior of mining additional coins, and just mine the submitted transaction?

> eth.getBalance(eth.accounts[0])
> eth.getBalance(eth.accounts[1])
> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(6, 'ether')})
> == Pending transactions! Mining...
== No transactions! Mining stopped.

> eth.getBalance(eth.accounts[0])
> eth.getBalance(eth.accounts[1])

As you can see, accounts[0] has got just 1 coin deducted, while accounts[1] got 6 coins added. I presume additional 5 coins are also getting mined and added to accounts[0] before the mining process stops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.