mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
Defuse difficulty bomb before block 3M
+ New difficulty calculation will be activated on CLOHF1
This commit is contained in:
parent
c0b28cd7e8
commit
94809905ea
1 changed files with 43 additions and 0 deletions
|
|
@ -306,6 +306,8 @@ func (ethash *Ethash) CalcDifficulty(chain consensus.ChainReader, time uint64, p
|
||||||
func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int {
|
func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int {
|
||||||
next := new(big.Int).Add(parent.Number, big1)
|
next := new(big.Int).Add(parent.Number, big1)
|
||||||
switch {
|
switch {
|
||||||
|
case config.IsCLOHF1(next):
|
||||||
|
return calcDifficultyCLOHF1(time, parent)
|
||||||
case config.IsBombDisposal(next):
|
case config.IsBombDisposal(next):
|
||||||
return calcDifficultyBombDisposal(time, parent)
|
return calcDifficultyBombDisposal(time, parent)
|
||||||
case config.IsByzantium(next):
|
case config.IsByzantium(next):
|
||||||
|
|
@ -330,6 +332,47 @@ var (
|
||||||
big2999999 = big.NewInt(2999999)
|
big2999999 = big.NewInt(2999999)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// calcDifficultyCLOHF1 is the difficulty adjustment algorithm. It returns
|
||||||
|
// the difficulty that a new block should have when created at time given the
|
||||||
|
// parent block's time and difficulty. The calculation uses the CLOHF1 rules.
|
||||||
|
func calcDifficultyCLOHF1(time uint64, parent *types.Header) *big.Int {
|
||||||
|
// https://github.com/ethereum/EIPs/issues/100.
|
||||||
|
// algorithm:
|
||||||
|
// diff = (parent_diff +
|
||||||
|
// (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
|
||||||
|
// )
|
||||||
|
|
||||||
|
bigTime := new(big.Int).SetUint64(time)
|
||||||
|
bigParentTime := new(big.Int).Set(parent.Time)
|
||||||
|
|
||||||
|
// holds intermediate values to make the algo easier to read & audit
|
||||||
|
x := new(big.Int)
|
||||||
|
y := new(big.Int)
|
||||||
|
|
||||||
|
// (2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9
|
||||||
|
x.Sub(bigTime, bigParentTime)
|
||||||
|
x.Div(x, big9)
|
||||||
|
if parent.UncleHash == types.EmptyUncleHash {
|
||||||
|
x.Sub(big1, x)
|
||||||
|
} else {
|
||||||
|
x.Sub(big2, x)
|
||||||
|
}
|
||||||
|
// max((2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9, -99)
|
||||||
|
if x.Cmp(bigMinus99) < 0 {
|
||||||
|
x.Set(bigMinus99)
|
||||||
|
}
|
||||||
|
// parent_diff + (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
|
||||||
|
y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
|
||||||
|
x.Mul(y, x)
|
||||||
|
x.Add(parent.Difficulty, x)
|
||||||
|
|
||||||
|
// minimum difficulty can ever be (before exponential factor)
|
||||||
|
if x.Cmp(params.MinimumDifficulty) < 0 {
|
||||||
|
x.Set(params.MinimumDifficulty)
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
// calcDifficultyByzantium is the difficulty adjustment algorithm. It returns
|
// calcDifficultyByzantium is the difficulty adjustment algorithm. It returns
|
||||||
// the difficulty that a new block should have when created at time given the
|
// the difficulty that a new block should have when created at time given the
|
||||||
// parent block's time and difficulty. The calculation uses the Byzantium rules.
|
// parent block's time and difficulty. The calculation uses the Byzantium rules.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue