difficulty.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package ethash
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/core/types"
  20. "github.com/holiman/uint256"
  21. )
  22. const (
  23. // frontierDurationLimit is for Frontier:
  24. // The decision boundary on the blocktime duration used to determine
  25. // whether difficulty should go up or down.
  26. frontierDurationLimit = 13
  27. // minimumDifficulty The minimum that the difficulty may ever be.
  28. minimumDifficulty = 131072
  29. // expDiffPeriod is the exponential difficulty period
  30. expDiffPeriodUint = 100000
  31. // difficultyBoundDivisorBitShift is the bound divisor of the difficulty (2048),
  32. // This constant is the right-shifts to use for the division.
  33. difficultyBoundDivisor = 11
  34. )
  35. // CalcDifficultyFrontierU256 is the difficulty adjustment algorithm. It returns the
  36. // difficulty that a new block should have when created at time given the parent
  37. // block's time and difficulty. The calculation uses the Frontier rules.
  38. func CalcDifficultyFrontierU256(time uint64, parent *types.Header) *big.Int {
  39. /*
  40. Algorithm
  41. block_diff = pdiff + pdiff / 2048 * (1 if time - ptime < 13 else -1) + int(2^((num // 100000) - 2))
  42. Where:
  43. - pdiff = parent.difficulty
  44. - ptime = parent.time
  45. - time = block.timestamp
  46. - num = block.number
  47. */
  48. pDiff, _ := uint256.FromBig(parent.Difficulty) // pDiff: pdiff
  49. adjust := pDiff.Clone()
  50. adjust.Rsh(adjust, difficultyBoundDivisor) // adjust: pDiff / 2048
  51. if time-parent.Time < frontierDurationLimit {
  52. pDiff.Add(pDiff, adjust)
  53. } else {
  54. pDiff.Sub(pDiff, adjust)
  55. }
  56. if pDiff.LtUint64(minimumDifficulty) {
  57. pDiff.SetUint64(minimumDifficulty)
  58. }
  59. // 'pdiff' now contains:
  60. // pdiff + pdiff / 2048 * (1 if time - ptime < 13 else -1)
  61. if periodCount := (parent.Number.Uint64() + 1) / expDiffPeriodUint; periodCount > 1 {
  62. // diff = diff + 2^(periodCount - 2)
  63. expDiff := adjust.SetOne()
  64. expDiff.Lsh(expDiff, uint(periodCount-2)) // expdiff: 2 ^ (periodCount -2)
  65. pDiff.Add(pDiff, expDiff)
  66. }
  67. return pDiff.ToBig()
  68. }
  69. // CalcDifficultyHomesteadU256 is the difficulty adjustment algorithm. It returns
  70. // the difficulty that a new block should have when created at time given the
  71. // parent block's time and difficulty. The calculation uses the Homestead rules.
  72. func CalcDifficultyHomesteadU256(time uint64, parent *types.Header) *big.Int {
  73. /*
  74. https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.md
  75. Algorithm:
  76. block_diff = pdiff + pdiff / 2048 * max(1 - (time - ptime) / 10, -99) + 2 ^ int((num / 100000) - 2))
  77. Our modification, to use unsigned ints:
  78. block_diff = pdiff - pdiff / 2048 * max((time - ptime) / 10 - 1, 99) + 2 ^ int((num / 100000) - 2))
  79. Where:
  80. - pdiff = parent.difficulty
  81. - ptime = parent.time
  82. - time = block.timestamp
  83. - num = block.number
  84. */
  85. pDiff, _ := uint256.FromBig(parent.Difficulty) // pDiff: pdiff
  86. adjust := pDiff.Clone()
  87. adjust.Rsh(adjust, difficultyBoundDivisor) // adjust: pDiff / 2048
  88. x := (time - parent.Time) / 10 // (time - ptime) / 10)
  89. var neg = true
  90. if x == 0 {
  91. x = 1
  92. neg = false
  93. } else if x >= 100 {
  94. x = 99
  95. } else {
  96. x = x - 1
  97. }
  98. z := new(uint256.Int).SetUint64(x)
  99. adjust.Mul(adjust, z) // adjust: (pdiff / 2048) * max((time - ptime) / 10 - 1, 99)
  100. if neg {
  101. pDiff.Sub(pDiff, adjust) // pdiff - pdiff / 2048 * max((time - ptime) / 10 - 1, 99)
  102. } else {
  103. pDiff.Add(pDiff, adjust) // pdiff + pdiff / 2048 * max((time - ptime) / 10 - 1, 99)
  104. }
  105. if pDiff.LtUint64(minimumDifficulty) {
  106. pDiff.SetUint64(minimumDifficulty)
  107. }
  108. // for the exponential factor, a.k.a "the bomb"
  109. // diff = diff + 2^(periodCount - 2)
  110. if periodCount := (1 + parent.Number.Uint64()) / expDiffPeriodUint; periodCount > 1 {
  111. expFactor := adjust.Lsh(adjust.SetOne(), uint(periodCount-2))
  112. pDiff.Add(pDiff, expFactor)
  113. }
  114. return pDiff.ToBig()
  115. }
  116. // MakeDifficultyCalculatorU256 creates a difficultyCalculator with the given bomb-delay.
  117. // the difficulty is calculated with Byzantium rules, which differs from Homestead in
  118. // how uncles affect the calculation
  119. func MakeDifficultyCalculatorU256(bombDelay *big.Int) func(time uint64, parent *types.Header) *big.Int {
  120. // Note, the calculations below looks at the parent number, which is 1 below
  121. // the block number. Thus we remove one from the delay given
  122. bombDelayFromParent := bombDelay.Uint64() - 1
  123. return func(time uint64, parent *types.Header) *big.Int {
  124. /*
  125. https://github.com/ethereum/EIPs/issues/100
  126. pDiff = parent.difficulty
  127. BLOCK_DIFF_FACTOR = 9
  128. a = pDiff + (pDiff // BLOCK_DIFF_FACTOR) * adj_factor
  129. b = min(parent.difficulty, MIN_DIFF)
  130. child_diff = max(a,b )
  131. */
  132. x := (time - parent.Time) / 9 // (block_timestamp - parent_timestamp) // 9
  133. c := uint64(1) // if parent.unclehash == emptyUncleHashHash
  134. if parent.UncleHash != types.EmptyUncleHash {
  135. c = 2
  136. }
  137. xNeg := x >= c
  138. if xNeg {
  139. // x is now _negative_ adjustment factor
  140. x = x - c // - ( (t-p)/p -( 2 or 1) )
  141. } else {
  142. x = c - x // (2 or 1) - (t-p)/9
  143. }
  144. if x > 99 {
  145. x = 99 // max(x, 99)
  146. }
  147. // parent_diff + (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
  148. y := new(uint256.Int)
  149. y.SetFromBig(parent.Difficulty) // y: p_diff
  150. pDiff := y.Clone() // pdiff: p_diff
  151. z := new(uint256.Int).SetUint64(x) //z : +-adj_factor (either pos or negative)
  152. y.Rsh(y, difficultyBoundDivisor) // y: p__diff / 2048
  153. z.Mul(y, z) // z: (p_diff / 2048 ) * (+- adj_factor)
  154. if xNeg {
  155. y.Sub(pDiff, z) // y: parent_diff + parent_diff/2048 * adjustment_factor
  156. } else {
  157. y.Add(pDiff, z) // y: parent_diff + parent_diff/2048 * adjustment_factor
  158. }
  159. // minimum difficulty can ever be (before exponential factor)
  160. if y.LtUint64(minimumDifficulty) {
  161. y.SetUint64(minimumDifficulty)
  162. }
  163. // calculate a fake block number for the ice-age delay
  164. // Specification: https://eips.ethereum.org/EIPS/eip-1234
  165. var pNum = parent.Number.Uint64()
  166. if pNum >= bombDelayFromParent {
  167. if fakeBlockNumber := pNum - bombDelayFromParent; fakeBlockNumber >= 2*expDiffPeriodUint {
  168. z.SetOne()
  169. z.Lsh(z, uint(fakeBlockNumber/expDiffPeriodUint-2))
  170. y.Add(z, y)
  171. }
  172. }
  173. return y.ToBig()
  174. }
  175. }