@@ -239,7 +239,7 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
239
239
return errZeroBlockTime
240
240
}
241
241
// 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 )
243
243
if expected .Cmp (header .Difficulty ) != 0 {
244
244
return fmt .Errorf ("invalid difficulty: have %v, want %v" , header .Difficulty , expected )
245
245
}
@@ -283,16 +283,19 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
283
283
return nil
284
284
}
285
285
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.
289
289
//
290
290
// 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 )
294
298
}
295
- return calcDifficultyFrontier (time , parentTime , parentNumber , parentDiff )
296
299
}
297
300
298
301
// Some weird constants to avoid constant memory allocs for them.
@@ -305,15 +308,15 @@ var (
305
308
// calcDifficultyHomestead is the difficulty adjustment algorithm. It returns
306
309
// the difficulty that a new block should have when created at time given the
307
310
// 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 {
309
312
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.mediawiki
310
313
// algorithm:
311
314
// diff = (parent_diff +
312
315
// (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
313
316
// ) + 2^(periodCount - 2)
314
317
315
318
bigTime := new (big.Int ).SetUint64 (time )
316
- bigParentTime := new (big.Int ).SetUint64 ( parentTime )
319
+ bigParentTime := new (big.Int ).Set ( parent . Time )
317
320
318
321
// holds intermediate values to make the algo easier to read & audit
319
322
x := new (big.Int )
@@ -329,16 +332,16 @@ func calcDifficultyHomestead(time, parentTime uint64, parentNumber, parentDiff *
329
332
x .Set (bigMinus99 )
330
333
}
331
334
// (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 )
333
336
x .Mul (y , x )
334
- x .Add (parentDiff , x )
337
+ x .Add (parent . Difficulty , x )
335
338
336
339
// minimum difficulty can ever be (before exponential factor)
337
340
if x .Cmp (params .MinimumDifficulty ) < 0 {
338
341
x .Set (params .MinimumDifficulty )
339
342
}
340
343
// for the exponential factor
341
- periodCount := new (big.Int ).Add (parentNumber , common .Big1 )
344
+ periodCount := new (big.Int ).Add (parent . Number , common .Big1 )
342
345
periodCount .Div (periodCount , expDiffPeriod )
343
346
344
347
// the exponential factor, commonly referred to as "the bomb"
@@ -354,25 +357,25 @@ func calcDifficultyHomestead(time, parentTime uint64, parentNumber, parentDiff *
354
357
// calcDifficultyFrontier is the difficulty adjustment algorithm. It returns the
355
358
// difficulty that a new block should have when created at time given the parent
356
359
// 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 {
358
361
diff := new (big.Int )
359
- adjust := new (big.Int ).Div (parentDiff , params .DifficultyBoundDivisor )
362
+ adjust := new (big.Int ).Div (parent . Difficulty , params .DifficultyBoundDivisor )
360
363
bigTime := new (big.Int )
361
364
bigParentTime := new (big.Int )
362
365
363
366
bigTime .SetUint64 (time )
364
- bigParentTime .SetUint64 ( parentTime )
367
+ bigParentTime .Set ( parent . Time )
365
368
366
369
if bigTime .Sub (bigTime , bigParentTime ).Cmp (params .DurationLimit ) < 0 {
367
- diff .Add (parentDiff , adjust )
370
+ diff .Add (parent . Difficulty , adjust )
368
371
} else {
369
- diff .Sub (parentDiff , adjust )
372
+ diff .Sub (parent . Difficulty , adjust )
370
373
}
371
374
if diff .Cmp (params .MinimumDifficulty ) < 0 {
372
375
diff .Set (params .MinimumDifficulty )
373
376
}
374
377
375
- periodCount := new (big.Int ).Add (parentNumber , common .Big1 )
378
+ periodCount := new (big.Int ).Add (parent . Number , common .Big1 )
376
379
periodCount .Div (periodCount , expDiffPeriod )
377
380
if periodCount .Cmp (common .Big1 ) > 0 {
378
381
// diff = diff + 2^(periodCount - 2)
@@ -434,8 +437,7 @@ func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header)
434
437
if parent == nil {
435
438
return consensus .ErrUnknownAncestor
436
439
}
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 )
439
441
440
442
return nil
441
443
}
0 commit comments