Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop mining on private net on geth ethrereum

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?

like image 202
Lakshmi Avatar asked Sep 05 '25 03:09

Lakshmi


1 Answers

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.

like image 138
midhun0003 Avatar answered Sep 07 '25 23:09

midhun0003