11

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.

  • 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 '16 at 21:57
11

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
  • would you still be able to add other commands like -rpc etc ? – jayD Mar 8 '16 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 '16 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 '16 at 20:35
6

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

4

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.

3

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?

  • 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 '16 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 '16 at 19:26
0

current geth (Geth/v1.6.7-stable-ab5646c5/linux-amd64/go1.8.1) includes the admin module, which offers both sleep and sleepBlocks:

> admin.sleep(1)  // delays 1 second
true
> miner.start(1)
INFO [03-31|14:32:39] Updated mining threads                  threads=1
INFO [03-31|14:32:39] Transaction pool price threshold updated price=18000000000
INFO [03-31|14:32:39] Starting mining operation 
INFO [03-31|14:32:39] Commit new mining work                 number=400 txs=0 uncles=0 elapsed=1.788ms
null
> admin.sleepBlocks(1); miner.stop()  // waits for 1 block to be mined
INFO [03-31|14:39:46] Successfully sealed new block            number=400 hash=d48975…49df83
INFO [03-31|14:39:46] 🔨 mined potential block                  number=400 hash=d48975…49df83
INFO [03-31|14:39:46] Commit new mining work                   number=401 txs=0 uncles=0 elapsed=46.577ms
INFO [03-31|14:39:46] Successfully sealed new block            number=401 hash=edfd30…176b63
INFO [03-31|14:39:46] 🔨 mined potential block                  number=401 hash=edfd30…176b63
INFO [03-31|14:39:46] Commit new mining work                   number=402 txs=0 uncles=0 elapsed=666.933µs
true

Your Answer

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

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