@@ -239,7 +239,7 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
239239 return errZeroBlockTime
240240 }
241241 // Verify the block's difficulty based in it's timestamp and parent's difficulty
242- expected := CalcDifficulty (chain .Config (), header .Time .Uint64 (), parent . Time . Uint64 (), parent . Number , parent . Difficulty )
242+ expected := CalcDifficulty (chain .Config (), header .Time .Uint64 (), parent )
243243 if expected .Cmp (header .Difficulty ) != 0 {
244244 return fmt .Errorf ("invalid difficulty: have %v, want %v" , header .Difficulty , expected )
245245 }
@@ -283,16 +283,19 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
283283 return nil
284284}
285285
286- // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
287- // that a new block should have when created at time given the parent block's time
288- // and difficulty.
286+ // CalcDifficulty is the difficulty adjustment algorithm. It returns
287+ // the difficulty that a new block should have when created at time
288+ // given the parent block's time and difficulty.
289289//
290290// TODO (karalabe): Move the chain maker into this package and make this private!
291- func CalcDifficulty (config * params.ChainConfig , time , parentTime uint64 , parentNumber , parentDiff * big.Int ) * big.Int {
292- if config .IsHomestead (new (big.Int ).Add (parentNumber , common .Big1 )) {
293- return calcDifficultyHomestead (time , parentTime , parentNumber , parentDiff )
291+ func CalcDifficulty (config * params.ChainConfig , time uint64 , parent * types.Header ) * big.Int {
292+ next := new (big.Int ).Add (parent .Number , common .Big1 )
293+ switch {
294+ case config .IsHomestead (next ):
295+ return calcDifficultyHomestead (time , parent )
296+ default :
297+ return calcDifficultyFrontier (time , parent )
294298 }
295- return calcDifficultyFrontier (time , parentTime , parentNumber , parentDiff )
296299}
297300
298301// Some weird constants to avoid constant memory allocs for them.
@@ -305,15 +308,15 @@ var (
305308// calcDifficultyHomestead is the difficulty adjustment algorithm. It returns
306309// the difficulty that a new block should have when created at time given the
307310// parent block's time and difficulty. The calculation uses the Homestead rules.
308- func calcDifficultyHomestead (time , parentTime uint64 , parentNumber , parentDiff * big. Int ) * big.Int {
311+ func calcDifficultyHomestead (time uint64 , parent * types. Header ) * big.Int {
309312 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.mediawiki
310313 // algorithm:
311314 // diff = (parent_diff +
312315 // (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
313316 // ) + 2^(periodCount - 2)
314317
315318 bigTime := new (big.Int ).SetUint64 (time )
316- bigParentTime := new (big.Int ).SetUint64 ( parentTime )
319+ bigParentTime := new (big.Int ).Set ( parent . Time )
317320
318321 // holds intermediate values to make the algo easier to read & audit
319322 x := new (big.Int )
@@ -329,16 +332,16 @@ func calcDifficultyHomestead(time, parentTime uint64, parentNumber, parentDiff *
329332 x .Set (bigMinus99 )
330333 }
331334 // (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
332- y .Div (parentDiff , params .DifficultyBoundDivisor )
335+ y .Div (parent . Difficulty , params .DifficultyBoundDivisor )
333336 x .Mul (y , x )
334- x .Add (parentDiff , x )
337+ x .Add (parent . Difficulty , x )
335338
336339 // minimum difficulty can ever be (before exponential factor)
337340 if x .Cmp (params .MinimumDifficulty ) < 0 {
338341 x .Set (params .MinimumDifficulty )
339342 }
340343 // for the exponential factor
341- periodCount := new (big.Int ).Add (parentNumber , common .Big1 )
344+ periodCount := new (big.Int ).Add (parent . Number , common .Big1 )
342345 periodCount .Div (periodCount , expDiffPeriod )
343346
344347 // the exponential factor, commonly referred to as "the bomb"
@@ -354,25 +357,25 @@ func calcDifficultyHomestead(time, parentTime uint64, parentNumber, parentDiff *
354357// calcDifficultyFrontier is the difficulty adjustment algorithm. It returns the
355358// difficulty that a new block should have when created at time given the parent
356359// block's time and difficulty. The calculation uses the Frontier rules.
357- func calcDifficultyFrontier (time , parentTime uint64 , parentNumber , parentDiff * big. Int ) * big.Int {
360+ func calcDifficultyFrontier (time uint64 , parent * types. Header ) * big.Int {
358361 diff := new (big.Int )
359- adjust := new (big.Int ).Div (parentDiff , params .DifficultyBoundDivisor )
362+ adjust := new (big.Int ).Div (parent . Difficulty , params .DifficultyBoundDivisor )
360363 bigTime := new (big.Int )
361364 bigParentTime := new (big.Int )
362365
363366 bigTime .SetUint64 (time )
364- bigParentTime .SetUint64 ( parentTime )
367+ bigParentTime .Set ( parent . Time )
365368
366369 if bigTime .Sub (bigTime , bigParentTime ).Cmp (params .DurationLimit ) < 0 {
367- diff .Add (parentDiff , adjust )
370+ diff .Add (parent . Difficulty , adjust )
368371 } else {
369- diff .Sub (parentDiff , adjust )
372+ diff .Sub (parent . Difficulty , adjust )
370373 }
371374 if diff .Cmp (params .MinimumDifficulty ) < 0 {
372375 diff .Set (params .MinimumDifficulty )
373376 }
374377
375- periodCount := new (big.Int ).Add (parentNumber , common .Big1 )
378+ periodCount := new (big.Int ).Add (parent . Number , common .Big1 )
376379 periodCount .Div (periodCount , expDiffPeriod )
377380 if periodCount .Cmp (common .Big1 ) > 0 {
378381 // diff = diff + 2^(periodCount - 2)
@@ -434,8 +437,7 @@ func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header)
434437 if parent == nil {
435438 return consensus .ErrUnknownAncestor
436439 }
437- header .Difficulty = CalcDifficulty (chain .Config (), header .Time .Uint64 (),
438- parent .Time .Uint64 (), parent .Number , parent .Difficulty )
440+ header .Difficulty = CalcDifficulty (chain .Config (), header .Time .Uint64 (), parent )
439441
440442 return nil
441443}
0 commit comments