0

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...");
        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();

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])
118000000000000000000
> eth.getBalance(eth.accounts[1])
32000000000000000000
> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(6, 'ether')})
"0xf03683266cb68f24a91e8cf5b2308dc3c960e525ae5872eaf70f20bb33b63624"
> == Pending transactions! Mining...
== No transactions! Mining stopped.

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

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.