From b8c21e9aad83149e625c7cf58d220e0abbb7c50f Mon Sep 17 00:00:00 2001 From: Igor Mandrigin Date: Tue, 16 Mar 2021 12:14:35 +0100 Subject: [PATCH] fix consensus tests --- consensus/ethash/consensus_test.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/consensus/ethash/consensus_test.go b/consensus/ethash/consensus_test.go index be7b6816b..2b874fad4 100644 --- a/consensus/ethash/consensus_test.go +++ b/consensus/ethash/consensus_test.go @@ -101,6 +101,12 @@ func randSlice(min, max uint32) []byte { func TestDifficultyCalculators(t *testing.T) { rand.Seed(2) + + wrap := func(f func(time, parentTime uint64, parentDifficulty, parentNumber *big.Int) *big.Int) func(time, parentTime uint64, parentDifficulty, parentNumber *big.Int, unkleHash common.Hash) *big.Int { + return func(time, parentTime uint64, parentDifficulty, parentNumber *big.Int, unkleHash common.Hash) *big.Int { + return f(time, parentTime, parentDifficulty, parentNumber) + } + } for i := 0; i < 5000; i++ { // 1 to 300 seconds diff var timeDelta = uint64(1 + rand.Uint32()%3000) @@ -119,15 +125,16 @@ func TestDifficultyCalculators(t *testing.T) { } bombDelay := new(big.Int).SetUint64(rand.Uint64() % 50_000_000) for i, pair := range []struct { - bigFn func(time uint64, parent *types.Header) *big.Int + bigFn func(time, parentTime uint64, parentDifficulty, parentNumber *big.Int, uncleHash common.Hash) *big.Int u256Fn func(time uint64, parent *types.Header) *big.Int }{ - {FrontierDifficultyCalulator, CalcDifficultyFrontierU256}, - {HomesteadDifficultyCalulator, CalcDifficultyHomesteadU256}, + {wrap(FrontierDifficultyCalulator), CalcDifficultyFrontierU256}, + {wrap(HomesteadDifficultyCalulator), CalcDifficultyHomesteadU256}, {DynamicDifficultyCalculator(bombDelay), MakeDifficultyCalculatorU256(bombDelay)}, } { time := header.Time + timeDelta - want := pair.bigFn(time, header) + + want := pair.bigFn(time, header.Time, header.Difficulty, header.Number, header.UncleHash) have := pair.u256Fn(time, header) if want.BitLen() > 256 { continue @@ -153,7 +160,7 @@ func BenchmarkDifficultyCalculator(b *testing.B) { b.Run("big-frontier", func(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { - calcDifficultyFrontier(1000014, h) + calcDifficultyFrontier(1000014, h.Time, h.Difficulty, h.Number) } }) b.Run("u256-frontier", func(b *testing.B) { @@ -165,7 +172,7 @@ func BenchmarkDifficultyCalculator(b *testing.B) { b.Run("big-homestead", func(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { - calcDifficultyHomestead(1000014, h) + calcDifficultyHomestead(1000014, h.Time, h.Difficulty, h.Number) } }) b.Run("u256-homestead", func(b *testing.B) { @@ -177,7 +184,7 @@ func BenchmarkDifficultyCalculator(b *testing.B) { b.Run("big-generic", func(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { - x1(1000014, h) + x1(1000014, h.Time, h.Difficulty, h.Number, h.UncleHash) } }) b.Run("u256-generic", func(b *testing.B) {