Ethereum Stack Exchange is a question and answer site for users of Ethereum, the crypto value and blockchain-based consensus network. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am running a local testnet with a low mining difficulty to test my contracts, and I am conscious that when I start another mining geth instance the total computation will hog my CPU.

My question is - is there a way of making the geth miner sleep for a specified period of time before mining the next block?

I could just have one node mining transactions, but I would like to emulate the real network more closely.

I am sure there are other solutions to testnet mining that I am not aware of, and they are also welcomed.

share|improve this question
    
I have a patch for geth that mines every second and I run it on a laptop. It speeds up mining by a factor of ~15x off of stock geth. If you are interested let me know (slightly OT here). I find in a heavy testing setup with mining every second that my laptop fan comes on though. But it beats waiting for forever to test 60 function points (aka transactions) I like the idea of not consuming CPU when there's nothing to do, thanks for the question – Paul S Mar 8 at 21:57
up vote 11 down vote accepted

You can write a JavaScript script that controls geth's behavior related to mining.

There is a simple script that mines only nonempty blocks on go-ethereum wiki. Another one is mine.js script from Embark Framework. This one is feature-rich and configurable for periodic mining, mining reward cap, number of mining threads and more (via glider).

Scripts are loaded with js command, for example:

geth --rpc js mining.js
share|improve this answer
    
would you still be able to add other commands like -rpc etc ? – jayD Mar 8 at 19:21
3  
yes, this example is from embark... geth --datadir="/tmp/embark" --logfile="/tmp/embark.log" --port 30303 --rpc --rpcport 8101 --rpcaddr localhost --networkid 20340 --rpccorsdomain "*" --minerthreads "1" --genesis="config/genesis/dev_genesis.json" --rpcapi "eth,web3" --maxpeers 4 --password config/password --unlock b425ff6cb67786c5d22d3277726d0c70f0664d2e js node_modules/embark-framework/js/mine.js – glider Mar 8 at 20:16
    
For reference, this mine.js file taken from the embark framework, has broader functionality: periodic mining, minimum balance mining etc. – glider Mar 8 at 20:35

You should test the Embark framework testnet. It's a specially configured blockchain that will , unlike the normal testnet , only mine when you need to mine.So if you send a transaction it will start by minning what needed for said transaction then proceed with the transaction. That's one solution:

https://github.com/iurimatias/embark-framework

embark blockchain

By default embark blockchain will mine a minimum amount of ether and will only mine when new transactions come in. This is quite usefull to keep a low CPU.

Depending on what you're working on there is also a TestRPC node that doesn't behave exactly as a blockchain but consume almost no ressource and might suffice you if you need basic operations.

Last things i can add, you might want to set a minethread to 1 as mining parameter so that it won't use your whole processor. It doesn't fix the problem the way you want it but might make it less ressource intensive enough for you to like this solution.

That be something like that :

geth -dev --rpc --rpcport 8545 --rpcaddr localhost --rpccorsdomain "*" --minerthreads "1" -unlock 0 -mine

share|improve this answer

I created the development wrapper gethdev to help with this exact issue (automatically creating a private testnet, and starting/stopping the miner automatically when needed).

To get started simply:

npm install -g gethdev
gethdev

This will create a testnet, create your first account, and start geth running with the gethdev.js script (which handles starting and stoping the miner).

You can also specify any custom arguments you want, either on the command line:

gethdev console

or by setting an environment variable (great if you always use the same set of arguments):

GETHDEV_OPTS="--datadir ~/ethereum" gethdev

Unlike Embark or other larger frameworks, this is simply a small wrapper designed to help with running geth for development projects.

share|improve this answer

I created a geth test setup (Based on 1.3.x, haven't updated yet). It mines at much faster speeds. I think I'll add the answer above to this, I like the idea of not consuming CPU when there's nothing to do.

See: What's the fastest way to mine a transaction so I can test contracts quickly?

share|improve this answer
    
I'm not sure why you would want to mine on a testnet quickly? To me it just seems like a waste of computation cycles. – glider Mar 9 at 18:37
    
Because on a testnet I'm trying to test, and I want the test to complete quickly. My current contract automated test suite has 78 transactions in it. On a standard geth node, this would take 1500 seconds, or 26 minutes to complete. On an accelerated test net with geth this takes about 3 minutes to complete. (note there's about 3x calls than transactions to verify results, and the time includes compiling and deploying contracts). I also set the difficulty to the lowest possible value. AFAICT most of the CPU geth uses is for actually processing the EVM. – Paul S Mar 9 at 19:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.