return false
}
-func CalcNextRequiredDifficulty(bh *legacy.BlockHeader) uint64 {
- if bh == nil {
+func CalcNextRequiredDifficulty(lastBH, prevBH *legacy.BlockHeader) uint64 {
+ return uint64(2161727821138738707)
+
+ //TODO: test it and enable it
+ if lastBH == nil {
return powMinBits
- } else if (bh.Height+1)%blocksPerRetarget != 0 {
- return bh.Bits
+ } else if (lastBH.Height+1)%blocksPerRetarget != 0 {
+ return lastBH.Bits
}
- return uint64(2161727821138738707)
+ actualTimespan := int64(lastBH.Time().Sub(prevBH.Time()).Seconds())
+ oldTarget := CompactToBig(lastBH.Bits)
+ newTarget := new(big.Int).Mul(oldTarget, big.NewInt(actualTimespan))
+ targetTimeSpan := int64(blocksPerRetarget * targetSecondsPerBlock)
+ newTarget.Div(newTarget, big.NewInt(targetTimeSpan))
+ newTargetBits := BigToCompact(newTarget)
+
+ return newTargetBits
}
// config for pow mining
powMinBits = uint64(2161727821138738707)
blocksPerRetarget = uint64(1024)
- targetSecondsPerBlock = int64(60)
+ targetSecondsPerBlock = uint64(60)
)
// define the BTM asset id, the soul asset of Bytom
PreviousBlockHash: preBlockHash,
TimestampMS: bc.Millis(time.Now()),
BlockCommitment: legacy.BlockCommitment{},
- Bits: consensus.CalcNextRequiredDifficulty(),
+ Bits: consensus.CalcNextRequiredDifficulty(nil, nil),
},
}
newSnap.PruneNonces(b.BlockHeader.TimestampMS)