Skip to content

Commit 41ac8dd

Browse files
authored
Merge pull request #17675 from holiman/eip1234
Eip1234
2 parents c1345b0 + 7efb12d commit 41ac8dd

1 file changed

Lines changed: 82 additions & 60 deletions

File tree

‎consensus/ethash/consensus.go‎

Lines changed: 82 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,24 @@ import (
3838

3939
// Ethash proof-of-work protocol constants.
4040
var (
41-
FrontierBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block
42-
ByzantiumBlockReward = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium
43-
maxUncles = 2 // Maximum number of uncles allowed in a single block
44-
allowedFutureBlockTime = 15 * time.Second // Max time from current time allowed for blocks, before they're considered future blocks
41+
FrontierBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block
42+
ByzantiumBlockReward = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium
43+
ConstantinopleBlockReward = big.NewInt(2e+18) // Block reward in wei for successfully mining a block upward from Constantinople
44+
maxUncles = 2 // Maximum number of uncles allowed in a single block
45+
allowedFutureBlockTime = 15 * time.Second // Max time from current time allowed for blocks, before they're considered future blocks
46+
47+
// calcDifficultyConstantinople is the difficulty adjustment algorithm for Constantinople.
48+
// It returns the difficulty that a new block should have when created at time given the
49+
// parent block's time and difficulty. The calculation uses the Byzantium rules, but with
50+
// bomb offset 5M.
51+
// Specification EIP-1234: https://eips.ethereum.org/EIPS/eip-1234
52+
calcDifficultyConstantinople = makeDifficultyCalculator(big.NewInt(5000000))
53+
54+
// calcDifficultyByzantium is the difficulty adjustment algorithm. It returns
55+
// the difficulty that a new block should have when created at time given the
56+
// parent block's time and difficulty. The calculation uses the Byzantium rules.
57+
// Specification EIP-649: https://eips.ethereum.org/EIPS/eip-649
58+
calcDifficultyByzantium = makeDifficultyCalculator(big.NewInt(3000000))
4559
)
4660

4761
// Various error messages to mark blocks invalid. These should be private to
@@ -299,6 +313,8 @@ func (ethash *Ethash) CalcDifficulty(chain consensus.ChainReader, time uint64, p
299313
func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int {
300314
next := new(big.Int).Add(parent.Number, big1)
301315
switch {
316+
case config.IsConstantinople(next):
317+
return calcDifficultyConstantinople(time, parent)
302318
case config.IsByzantium(next):
303319
return calcDifficultyByzantium(time, parent)
304320
case config.IsHomestead(next):
@@ -316,66 +332,69 @@ var (
316332
big9 = big.NewInt(9)
317333
big10 = big.NewInt(10)
318334
bigMinus99 = big.NewInt(-99)
319-
big2999999 = big.NewInt(2999999)
320335
)
321336

322-
// calcDifficultyByzantium is the difficulty adjustment algorithm. It returns
323-
// the difficulty that a new block should have when created at time given the
324-
// parent block's time and difficulty. The calculation uses the Byzantium rules.
325-
func calcDifficultyByzantium(time uint64, parent *types.Header) *big.Int {
326-
// https://github.com/ethereum/EIPs/issues/100.
327-
// algorithm:
328-
// diff = (parent_diff +
329-
// (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
330-
// ) + 2^(periodCount - 2)
331-
332-
bigTime := new(big.Int).SetUint64(time)
333-
bigParentTime := new(big.Int).Set(parent.Time)
334-
335-
// holds intermediate values to make the algo easier to read & audit
336-
x := new(big.Int)
337-
y := new(big.Int)
338-
339-
// (2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9
340-
x.Sub(bigTime, bigParentTime)
341-
x.Div(x, big9)
342-
if parent.UncleHash == types.EmptyUncleHash {
343-
x.Sub(big1, x)
344-
} else {
345-
x.Sub(big2, x)
346-
}
347-
// max((2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9, -99)
348-
if x.Cmp(bigMinus99) < 0 {
349-
x.Set(bigMinus99)
350-
}
351-
// parent_diff + (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
352-
y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
353-
x.Mul(y, x)
354-
x.Add(parent.Difficulty, x)
355-
356-
// minimum difficulty can ever be (before exponential factor)
357-
if x.Cmp(params.MinimumDifficulty) < 0 {
358-
x.Set(params.MinimumDifficulty)
359-
}
360-
// calculate a fake block number for the ice-age delay:
361-
// https://github.com/ethereum/EIPs/pull/669
362-
// fake_block_number = max(0, block.number - 3_000_000)
363-
fakeBlockNumber := new(big.Int)
364-
if parent.Number.Cmp(big2999999) >= 0 {
365-
fakeBlockNumber = fakeBlockNumber.Sub(parent.Number, big2999999) // Note, parent is 1 less than the actual block number
366-
}
367-
// for the exponential factor
368-
periodCount := fakeBlockNumber
369-
periodCount.Div(periodCount, expDiffPeriod)
337+
// makeDifficultyCalculator creates a difficultyCalculator with the given bomb-delay.
338+
// the difficulty is calculated with Byzantium rules, which differs from Homestead in
339+
// how uncles affect the calculation
340+
func makeDifficultyCalculator(bombDelay *big.Int) func(time uint64, parent *types.Header) *big.Int {
341+
// Note, the calculations below looks at the parent number, which is 1 below
342+
// the block number. Thus we remove one from the delay given
343+
bombDelayFromParent := new(big.Int).Sub(bombDelay, big1)
344+
return func(time uint64, parent *types.Header) *big.Int {
345+
// https://github.com/ethereum/EIPs/issues/100.
346+
// algorithm:
347+
// diff = (parent_diff +
348+
// (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
349+
// ) + 2^(periodCount - 2)
350+
351+
bigTime := new(big.Int).SetUint64(time)
352+
bigParentTime := new(big.Int).Set(parent.Time)
353+
354+
// holds intermediate values to make the algo easier to read & audit
355+
x := new(big.Int)
356+
y := new(big.Int)
357+
358+
// (2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9
359+
x.Sub(bigTime, bigParentTime)
360+
x.Div(x, big9)
361+
if parent.UncleHash == types.EmptyUncleHash {
362+
x.Sub(big1, x)
363+
} else {
364+
x.Sub(big2, x)
365+
}
366+
// max((2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9, -99)
367+
if x.Cmp(bigMinus99) < 0 {
368+
x.Set(bigMinus99)
369+
}
370+
// parent_diff + (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
371+
y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
372+
x.Mul(y, x)
373+
x.Add(parent.Difficulty, x)
374+
375+
// minimum difficulty can ever be (before exponential factor)
376+
if x.Cmp(params.MinimumDifficulty) < 0 {
377+
x.Set(params.MinimumDifficulty)
378+
}
379+
// calculate a fake block number for the ice-age delay
380+
// Specification: https://eips.ethereum.org/EIPS/eip-1234
381+
fakeBlockNumber := new(big.Int)
382+
if parent.Number.Cmp(bombDelayFromParent) >= 0 {
383+
fakeBlockNumber = fakeBlockNumber.Sub(parent.Number, bombDelayFromParent)
384+
}
385+
// for the exponential factor
386+
periodCount := fakeBlockNumber
387+
periodCount.Div(periodCount, expDiffPeriod)
370388

371-
// the exponential factor, commonly referred to as "the bomb"
372-
// diff = diff + 2^(periodCount - 2)
373-
if periodCount.Cmp(big1) > 0 {
374-
y.Sub(periodCount, big2)
375-
y.Exp(big2, y, nil)
376-
x.Add(x, y)
389+
// the exponential factor, commonly referred to as "the bomb"
390+
// diff = diff + 2^(periodCount - 2)
391+
if periodCount.Cmp(big1) > 0 {
392+
y.Sub(periodCount, big2)
393+
y.Exp(big2, y, nil)
394+
x.Add(x, y)
395+
}
396+
return x
377397
}
378-
return x
379398
}
380399

381400
// calcDifficultyHomestead is the difficulty adjustment algorithm. It returns
@@ -592,6 +611,9 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header
592611
if config.IsByzantium(header.Number) {
593612
blockReward = ByzantiumBlockReward
594613
}
614+
if config.IsConstantinople(header.Number) {
615+
blockReward = ConstantinopleBlockReward
616+
}
595617
// Accumulate the rewards for the miner and any included uncles
596618
reward := new(big.Int).Set(blockReward)
597619
r := new(big.Int)

0 commit comments

Comments
 (0)