29

I've created a private test network. On the genesis.json file, I've changed the difficulty to 1. Yet, mining a block still takes hours on my CPU. I suspect that is due to some configuration related to the network adaptation.

What is the correct procedure to decrease the difficulty on a private testnet, so that I can mine the first blocks in a not very powerful CPU?

3
  • 1
    If the answer answered your concern, feel free to accept it as the correct answer by checking the tick sign below the voting buttons.
    – niksmac
    Apr 1, 2016 at 13:15
  • 1
    I'm not sure this answers solves my issue. As soon as I'm able to investigate I'll either accept it or post a comment stating why it didn't work.
    – MaiaVictor
    Apr 1, 2016 at 13:16
  • 1
    Sure, if you edit your question or post any comments below the answer, i might be able to recommend a solution.
    – niksmac
    Apr 1, 2016 at 13:47

4 Answers 4

7

There are 2 ways to do this:

  • Change the value of difficulty parameter in genesis.json file to a small number(preferably set it to 0). You can refer to this example genesis file

    {
       "config": {
          "chainId": 1994,
          "homesteadBlock": 0,
          "eip155Block": 0,
          "eip158Block": 0,
          "byzantiumBlock": 0
       },
       "difficulty": "0x0", //difficulty set to zero in hexadecimal format
       "gasLimit": "0x8000000",
       "alloc": {
          "9a963d0eefeb62678d8efb48561c81e51c552797": { 
              "balance": "9606938044258990275541962092341162602522202993782792835301376" 
          },
          "30f28686aef33adbfbc13797b1d9f5a2f2759f56": { 
              "balance": "9606938044258990275541962092341162602522202993782792835301376" 
          }
       }
    }
    

Problem with this method is that when the block number increases, the difficulty also starts increasing at a high rate. So, to tackle that problem refer to the below part.

  • You can reduce the rate of block difficulty increament by making some changes in the consensus code of go-ethereum. Open consensus.go which resides at consensus/ethash/consensus.go and search for the below line.

return CalcDifficulty(chain.Config(), time, parent)

Now replace the above line with the below one.

return big.NewInt(1).

Now build the go-ethereum using make geth command.

I strongly recommend you to go through this article for much detailed implementation of the above process.

1
  • Please explain 'when the block number increases, the difficulty also starts increasing at a high rate'. I don't think the block number is related to the difficulty at all.
    – Senju
    Jun 17, 2019 at 8:40
21

You can make the difficulty static by modifying the CalcDifficulty in Geth to return a static number. Example:

func CalcDifficulty(config *ChainConfig, time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int {
    return big.NewInt(1)
}

Rebuild Geth and use your modified version.

Source: Answer to Is it possible to change the block target time?

8
  • 2
    This is the same variable we set in genesis block right? If yes what is the advantages of this method?
    – niksmac
    Jul 18, 2016 at 8:44
  • 1
    @niksmac I edited to change difficulty to 1 so mining will always be fast. Otherwise, the difficulty will gradually increase and mining will take ~15 seconds, which is what's desired in Homestead, but probably not in your private chain.
    – eth
    Jul 18, 2016 at 8:59
  • 1
    Should I make this change and modification on all nodes that are connected to me private ethereum network? or on the enode that I am connecting into? If yes, does it affect the difficulty on ongoing private blockchain network (ex: already mined 491,312 number of blocks)? @ eth♦
    – alper
    Jan 4, 2017 at 12:22
  • 2
    @Avatar Yes, I think all nodes should run the same code/protocol. Better to reset the blockchain, but if you want to continue, you may need to stop all nodes at the same block number and restart them, otherwise nodes might fork in different directions.
    – eth
    Jan 6, 2017 at 1:19
  • 1
    This overkill, because if you ar going to have to rebuild geth to apply this solution, you may as well delete you chain and restart it with a smaller difficulty specified in the genesis block.
    – fccoelho
    Aug 6, 2017 at 22:00
8

As seen in this blog

difficulty A scalar value corresponding to the difficulty level applied during the nonce discovering of this block. It defines the mining Target, which can be calculated from the previous block’s difficulty level and the timestamp. The higher the difficulty, the statistically more calculations a Miner must perform to discover a valid block. This value is used to control the Block generation time of a Blockchain, keeping the Block generation frequency within a target range. On the test network, we keep this value low to avoid waiting during tests, since the discovery of a valid Block is required to execute a transaction on the Blockchain.

The difficulty is set to 0x4000 in the genesis block, which is helping me mine block in every ~5 seconds.

I0401 07:06:16.533385    4556 ethash.go:252] Generating DAG: 98%
I0401 07:06:16.533482    4556 ethash.go:252] Generating DAG: 99%
I0401 07:06:16.533586    4556 ethash.go:252] Generating DAG: 100%
I0401 07:06:16.533668    4556 ethash.go:237] Done generating DAG for epoch 0, it took 10.562412ms

And Mining

I0401 07:06:26.062135    4556 worker.go:348] 🔨  Mined block (#219 / 9704b2d0). Wait 5 blocks for confirmation
I0401 07:06:26.062687    4556 worker.go:569] commit new work on block 220 with 0 txs & 0 uncles. Took 484.843µs
I0401 07:06:26.063166    4556 worker.go:569] commit new work on block 220 with 0 txs & 0 uncles. Took 375.001µs
I0401 07:06:32.221697    4556 worker.go:348] 🔨  Mined block (#220 / 77bb4f70). Wait 5 blocks for confirmation
I0401 07:06:32.222280    4556 worker.go:569] commit new work on block 221 with 0 txs & 0 uncles. Took 521.439µs
4
  • 1
    So can difficulty not be changed once mining on the chain has started?
    – TMOTTM
    Jul 18, 2016 at 10:51
  • 1
    @TMOTTM yeah thats how it seems.
    – niksmac
    Jul 18, 2016 at 12:39
  • 1
    So I cannot make this change if I have a ongoing private blockchain network (ex: already mined 491,312 number of blocks)? @niksmac
    – alper
    Jan 4, 2017 at 12:06
  • 1
    @Avatar thats how it seems to be.
    – niksmac
    Jan 5, 2017 at 3:48
4

You can follow this tutorial with detailed explanation: http://blog.coinfabrik.com/fast-smart-contracts-execution-ethereum-private-blockchain-development-environment/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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