2

a title is pretty straightforward. I need to set up miners on a private blockchain to mine blocks as fast as possible.

I have compiled a go-ethereum client with a constant value of difficulty but seems that mining complexity still grows. What directions should I look into to achieve the goal? Thanks

2
  • what did you modify in the code? provide more details
    – Badr Bellaj
    Jan 15, 2017 at 21:05
  • @BadrBellaj I modified func CalcDifficulty(...) to always return return big.NewInt(1).
    – forik
    Jan 16, 2017 at 6:58

1 Answer 1

3
  1. Ensure you use geth that you have built, not geth installed somewhere in system.
  2. All nodes in your private network must have the same version of geth.
  3. Here is the patch to mine 1 block per 1 second (I'm not sure, but I think it is as fast as possible):

    diff --git a/core/block_validator.go b/core/block_validator.go
    index 65f9753..2b19c64 100644
    --- a/core/block_validator.go
    +++ b/core/block_validator.go
    @@ -263,11 +263,7 @@ func ValidateHeader(config *params.ChainConfig, pow pow.PoW, header *types.Heade
     // the difficulty that a new block should have when created at time
     // given the parent block's time and difficulty.
     func CalcDifficulty(config *params.ChainConfig, time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int {
    -       if config.IsHomestead(new(big.Int).Add(parentNumber, common.Big1)) {
    -               return calcDifficultyHomestead(time, parentTime, parentNumber, parentDiff)
    -       } else {
    -               return calcDifficultyFrontier(time, parentTime, parentNumber, parentDiff)
    -       }
    +       return big.NewInt(1)
     }
    
     func calcDifficultyHomestead(time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int {
    diff --git a/miner/worker.go b/miner/worker.go
    index 77e4e02..e92ff62 100644
    --- a/miner/worker.go
    +++ b/miner/worker.go
    @@ -422,7 +422,7 @@ func (self *worker) commitNewWork() {
                    tstamp = parent.Time().Int64() + 1
            }
            // this will ensure we're not going off too far in the future
    -       if now := time.Now().Unix(); tstamp > now+4 {
    +       if now := time.Now().Unix(); tstamp > now {
                    wait := time.Duration(tstamp-now) * time.Second
                    glog.V(logger.Info).Infoln("We are too far in the future. Waiting for", wait)
                    time.Sleep(wait)
    
  4. Mine with only thread

    miner.start(1);

UPDATE

Diff for Geth 1.6.1

diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go
index 2930032..d141678 100644
--- a/consensus/ethash/consensus.go
+++ b/consensus/ethash/consensus.go
@@ -289,10 +289,11 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
 //
 // TODO (karalabe): Move the chain maker into this package and make this private!
 func CalcDifficulty(config *params.ChainConfig, time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int {
-       if config.IsHomestead(new(big.Int).Add(parentNumber, common.Big1)) {
-               return calcDifficultyHomestead(time, parentTime, parentNumber, parentDiff)
-       }
-       return calcDifficultyFrontier(time, parentTime, parentNumber, parentDiff)
+       // if config.IsHomestead(new(big.Int).Add(parentNumber, common.Big1)) {
+       //      return calcDifficultyHomestead(time, parentTime, parentNumber, parentDiff)
+       // }
+       // return calcDifficultyFrontier(time, parentTime, parentNumber, parentDiff)
+       return big.NewInt(1)
 }

 // Some weird constants to avoid constant memory allocs for them.
diff --git a/miner/worker.go b/miner/worker.go
index 01241b3..f1b3839 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -407,7 +407,7 @@ func (self *worker) commitNewWork() {
                tstamp = parent.Time().Int64() + 1
        }
        // this will ensure we're not going off too far in the future
-       if now := time.Now().Unix(); tstamp > now+1 {
+       if now := time.Now().Unix(); tstamp > now {
                wait := time.Duration(tstamp-now) * time.Second
                log.Info("Mining too far in the future", "wait", common.PrettyDuration(wait))
                time.Sleep(wait)
5
  • Basically, you did the same thing which I've done, I also always return big.NewInt(1)
    – forik
    Feb 14, 2017 at 15:46
  • @forik With one addition. If you do not change commitNewWork() you will mine 5 blocks very fast, and then wait for 5 seconds to mine another 5 blocks. So you will have annoying 5 seconds pauses in mining process. Feb 14, 2017 at 16:13
  • Why now + 4 and now + 1? What do the 1 and 4 do?
    – 0xcaff
    May 24, 2017 at 16:57
  • Is it reliable? I mean, is there any risk (f.e. forks) in having such fast block time?
    – user197675
    Jul 16, 2017 at 16:49
  • I'm sure you should not use this approach for any purposes except faster development Jul 18, 2017 at 11:03

Your Answer

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

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