github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/consensus/ethash/consensus_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package ethash
    18  
    19  import (
    20  	"encoding/binary"
    21  	"encoding/json"
    22  	"log"
    23  	"math/big"
    24  	"math/rand"
    25  	"os"
    26  	"path/filepath"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/fff-chain/go-fff/common"
    31  	"github.com/fff-chain/go-fff/common/math"
    32  	"github.com/fff-chain/go-fff/core/types"
    33  	"github.com/fff-chain/go-fff/params"
    34  )
    35  
    36  type diffTest struct {
    37  	ParentTimestamp    uint64
    38  	ParentDifficulty   *big.Int
    39  	CurrentTimestamp   uint64
    40  	CurrentBlocknumber *big.Int
    41  	CurrentDifficulty  *big.Int
    42  }
    43  
    44  func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
    45  	var ext struct {
    46  		ParentTimestamp    string
    47  		ParentDifficulty   string
    48  		CurrentTimestamp   string
    49  		CurrentBlocknumber string
    50  		CurrentDifficulty  string
    51  	}
    52  	if err := json.Unmarshal(b, &ext); err != nil {
    53  		return err
    54  	}
    55  
    56  	d.ParentTimestamp = math.MustParseUint64(ext.ParentTimestamp)
    57  	d.ParentDifficulty = math.MustParseBig256(ext.ParentDifficulty)
    58  	d.CurrentTimestamp = math.MustParseUint64(ext.CurrentTimestamp)
    59  	d.CurrentBlocknumber = math.MustParseBig256(ext.CurrentBlocknumber)
    60  	d.CurrentDifficulty = math.MustParseBig256(ext.CurrentDifficulty)
    61  
    62  	return nil
    63  }
    64  
    65  func TestCalcDifficulty(t *testing.T) {
    66  	file, err := os.Open(filepath.Join("..", "..", "tests", "testdata", "BasicTests", "difficulty.json"))
    67  	if err != nil {
    68  		t.Skip(err)
    69  	}
    70  	defer file.Close()
    71  
    72  	tests := make(map[string]diffTest)
    73  	err = json.NewDecoder(file).Decode(&tests)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	config := &params.ChainConfig{HomesteadBlock: big.NewInt(1150000)}
    79  
    80  	for name, test := range tests {
    81  		number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1))
    82  		diff := CalcDifficulty(config, test.CurrentTimestamp, &types.Header{
    83  			Number:     number,
    84  			Time:       test.ParentTimestamp,
    85  			Difficulty: test.ParentDifficulty,
    86  		})
    87  		if diff.Cmp(test.CurrentDifficulty) != 0 {
    88  			t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", diff)
    89  		}
    90  	}
    91  }
    92  func TestMakeDataset(t *testing.T) {
    93  	df,_:=new(big.Int).SetString("13812444150502336",0)
    94  
    95  	z:=calcDifficultyEip23866(uint64(time.Now().Unix()),&types.Header{
    96  		ParentHash:  common.Hash{},
    97  		UncleHash:   common.Hash{},
    98  		Coinbase:    common.Address{},
    99  		Root:        common.Hash{},
   100  		TxHash:      common.Hash{},
   101  		ReceiptHash: common.Hash{},
   102  		Bloom:       types.Bloom{},
   103  		Difficulty:  df,
   104  		Number:      big.NewInt(16022565),
   105  		GasLimit:    0,
   106  		GasUsed:     0,
   107  		Time:        1651827829,
   108  		Extra:       nil,
   109  		MixDigest:   common.Hash{},
   110  		Nonce:       types.BlockNonce{},
   111  	})
   112  	log.Println(z)
   113  }
   114  
   115  func randSlice(min, max uint32) []byte {
   116  	var b = make([]byte, 4)
   117  	rand.Read(b)
   118  	a := binary.LittleEndian.Uint32(b)
   119  	size := min + a%(max-min)
   120  	out := make([]byte, size)
   121  	rand.Read(out)
   122  	return out
   123  }
   124  
   125  func TestDifficultyCalculators(t *testing.T) {
   126  	rand.Seed(2)
   127  	for i := 0; i < 5000; i++ {
   128  		// 1 to 300 seconds diff
   129  		var timeDelta = uint64(1 + rand.Uint32()%3000)
   130  		diffBig := big.NewInt(0).SetBytes(randSlice(2, 10))
   131  		if diffBig.Cmp(params.MinimumDifficulty) < 0 {
   132  			diffBig.Set(params.MinimumDifficulty)
   133  		}
   134  		//rand.Read(difficulty)
   135  		header := &types.Header{
   136  			Difficulty: diffBig,
   137  			Number:     new(big.Int).SetUint64(rand.Uint64() % 50_000_000),
   138  			Time:       rand.Uint64() - timeDelta,
   139  		}
   140  		if rand.Uint32()&1 == 0 {
   141  			header.UncleHash = types.EmptyUncleHash
   142  		}
   143  		bombDelay := new(big.Int).SetUint64(rand.Uint64() % 50_000_000)
   144  		for i, pair := range []struct {
   145  			bigFn  func(time uint64, parent *types.Header) *big.Int
   146  			u256Fn func(time uint64, parent *types.Header) *big.Int
   147  		}{
   148  			{FrontierDifficultyCalulator, CalcDifficultyFrontierU256},
   149  			{HomesteadDifficultyCalulator, CalcDifficultyHomesteadU256},
   150  			{DynamicDifficultyCalculator(bombDelay), MakeDifficultyCalculatorU256(bombDelay)},
   151  		} {
   152  			time := header.Time + timeDelta
   153  			want := pair.bigFn(time, header)
   154  			have := pair.u256Fn(time, header)
   155  			if want.BitLen() > 256 {
   156  				continue
   157  			}
   158  			if want.Cmp(have) != 0 {
   159  				t.Fatalf("pair %d: want %x have %x\nparent.Number: %x\np.Time: %x\nc.Time: %x\nBombdelay: %v\n", i, want, have,
   160  					header.Number, header.Time, time, bombDelay)
   161  			}
   162  		}
   163  	}
   164  }
   165  
   166  func BenchmarkDifficultyCalculator(b *testing.B) {
   167  	x1 := makeDifficultyCalculator(big.NewInt(1000000))
   168  	x2 := MakeDifficultyCalculatorU256(big.NewInt(1000000))
   169  	h := &types.Header{
   170  		ParentHash: common.Hash{},
   171  		UncleHash:  types.EmptyUncleHash,
   172  		Difficulty: big.NewInt(0xffffff),
   173  		Number:     big.NewInt(500000),
   174  		Time:       1000000,
   175  	}
   176  	b.Run("big-frontier", func(b *testing.B) {
   177  		b.ReportAllocs()
   178  		for i := 0; i < b.N; i++ {
   179  			calcDifficultyFrontier(1000014, h)
   180  		}
   181  	})
   182  	b.Run("u256-frontier", func(b *testing.B) {
   183  		b.ReportAllocs()
   184  		for i := 0; i < b.N; i++ {
   185  			CalcDifficultyFrontierU256(1000014, h)
   186  		}
   187  	})
   188  	b.Run("big-homestead", func(b *testing.B) {
   189  		b.ReportAllocs()
   190  		for i := 0; i < b.N; i++ {
   191  			calcDifficultyHomestead(1000014, h)
   192  		}
   193  	})
   194  	b.Run("u256-homestead", func(b *testing.B) {
   195  		b.ReportAllocs()
   196  		for i := 0; i < b.N; i++ {
   197  			CalcDifficultyHomesteadU256(1000014, h)
   198  		}
   199  	})
   200  	b.Run("big-generic", func(b *testing.B) {
   201  		b.ReportAllocs()
   202  		for i := 0; i < b.N; i++ {
   203  			x1(1000014, h)
   204  		}
   205  	})
   206  	b.Run("u256-generic", func(b *testing.B) {
   207  		b.ReportAllocs()
   208  		for i := 0; i < b.N; i++ {
   209  			x2(1000014, h)
   210  		}
   211  	})
   212  }