git.pirl.io/community/pirl@v0.0.0-20201111064343-9d3d31ff74be/consensus/ethash/pirl_diff.go (about)

     1  package ethash
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"git.pirl.io/community/pirl/core/types"
     7  	"git.pirl.io/community/pirl/params"
     8  )
     9  
    10  func calcDifficultyPirlv2(time uint64, parent *types.Header) *big.Int {
    11  
    12  	bigTime := new(big.Int).SetUint64(time)
    13  	bigParentTime := new(big.Int).SetUint64(parent.Time)
    14  
    15  	// holds intermediate values to make the algo easier to read & audit
    16  	x := new(big.Int)
    17  	y := new(big.Int)
    18  
    19  	// (2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9
    20  	x.Sub(bigTime, bigParentTime)
    21  	x.Div(x, big9)
    22  	if parent.UncleHash == types.EmptyUncleHash {
    23  		x.Sub(big1, x)
    24  	} else {
    25  		x.Sub(big2, x)
    26  	}
    27  	// max((2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9, -99)
    28  	if x.Cmp(bigMinus99) < 0 {
    29  		x.Set(bigMinus99)
    30  	}
    31  	// parent_diff + (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
    32  	y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
    33  	x.Mul(y, x)
    34  	x.Add(parent.Difficulty, x)
    35  
    36  	// minimum difficulty can ever be (before exponential factor)
    37  	if x.Cmp(params.MinimumDifficulty) < 0 {
    38  		x.Set(params.MinimumDifficulty)
    39  	}
    40  
    41  	return x
    42  }