4

I have started a geth client using the below client(I had already created two accounts.:

geth --datadir datadir --networkid 123 --rpc --rpcaddr="localhost" --rpccorsdomain="*" --unlock <my account> --minerthreads="1" --maxpeers=0 --mine console

I opened the the ethereum wallet and deployed the smart contract from there. The transactionId and the contract address is received on my geth console.

Then I started my Dapp and created the instances of the contract and I am calling the contract invoking a contract function through web3 API. The contract function gets invoked but the transaction does not get submitted in the block unless I start mining. Hence I started miner.start() This started mining numerous blocks.

My question is where are these blocks coming from if I have my own private net and have submitted only one transaction. This is adding too many blocks and my blocksize is increasing unnecessarily. How to mine only the transaction I have submitted?

jrbedard
  • 3,662
  • 5
  • 30
  • 34
Lakshmi
  • 101
  • 1
  • 2

2 Answers2

2

save the below code as startmine.js file

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

And Start the geth Private network with following options

geth .......... . . . . . . .   --rpcapi="web3,eth,miner" --preload "startmine.js" console

This will run miner.start() automatically when you have any pending transactions.

midhun0003
  • 603
  • 1
  • 10
  • 26
  • i dont think this will work . even if the callback is happening and the else block being executed (stopping the miner) , the empty block still gets mined – redeemed Feb 19 '18 at 10:59
  • @redeemed in empty block there will not be any pending transactions so the mining will stop – midhun0003 Feb 21 '18 at 11:59
  • The mining will stop , but the empty blocks will be queued up in the background , and also unless miner is started the callbacks wont be fired . For that reason when i start the miner to make the callbacks happen manually , the empty blocks previously queued will immediately gets mined – redeemed Feb 21 '18 at 14:06
  • 1
    @redeemed the above code i have tested for my private network. without using this code my block number will reach above 1 lak , now its below 10k. I have tested it for several days. Please don't argue . – midhun0003 Feb 22 '18 at 04:42
  • Looks like this was answered [here](https://ethereum.stackexchange.com/questions/3151/how-to-make-miner-to-mine-only-when-there-are-pending-transactions) – Justin Jul 11 '18 at 19:44
0

If you're using POA, with more recent versions of geth (at least 1.9.25), setting the period to 0 will stop mining and make the node wait for transactions before mining a new block. This is nice for private networks or experimental environments.

genesis.json:

....
"clique": {
  "period": 0,
  "epoch": 30000
},
....

Answered previously by M Gopal

DogEatDog
  • 2,899
  • 2
  • 36
  • 65
  • My private network with this configuration also stop mining too. And I seem to figure out the reason is "the private network should have two nodes at least". After adding the second node, the mining starts and both nodes mine the block with round-robin order. – VincentHuang Sep 11 '21 at 07:42