consensus_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. package ethash
  17. import (
  18. "encoding/binary"
  19. "encoding/json"
  20. "math/big"
  21. "math/rand"
  22. "os"
  23. "path/filepath"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/common/math"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/params"
  29. )
  30. type diffTest struct {
  31. ParentTimestamp uint64
  32. ParentDifficulty *big.Int
  33. CurrentTimestamp uint64
  34. CurrentBlocknumber *big.Int
  35. CurrentDifficulty *big.Int
  36. }
  37. func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
  38. var ext struct {
  39. ParentTimestamp string
  40. ParentDifficulty string
  41. CurrentTimestamp string
  42. CurrentBlocknumber string
  43. CurrentDifficulty string
  44. }
  45. if err := json.Unmarshal(b, &ext); err != nil {
  46. return err
  47. }
  48. d.ParentTimestamp = math.MustParseUint64(ext.ParentTimestamp)
  49. d.ParentDifficulty = math.MustParseBig256(ext.ParentDifficulty)
  50. d.CurrentTimestamp = math.MustParseUint64(ext.CurrentTimestamp)
  51. d.CurrentBlocknumber = math.MustParseBig256(ext.CurrentBlocknumber)
  52. d.CurrentDifficulty = math.MustParseBig256(ext.CurrentDifficulty)
  53. return nil
  54. }
  55. func TestCalcDifficulty(t *testing.T) {
  56. file, err := os.Open(filepath.Join("..", "..", "tests", "testdata", "BasicTests", "difficulty.json"))
  57. if err != nil {
  58. t.Skip(err)
  59. }
  60. defer file.Close()
  61. tests := make(map[string]diffTest)
  62. err = json.NewDecoder(file).Decode(&tests)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. config := &params.ChainConfig{HomesteadBlock: big.NewInt(1150000)}
  67. for name, test := range tests {
  68. number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1))
  69. diff := CalcDifficulty(config, test.CurrentTimestamp, &types.Header{
  70. Number: number,
  71. Time: test.ParentTimestamp,
  72. Difficulty: test.ParentDifficulty,
  73. })
  74. if diff.Cmp(test.CurrentDifficulty) != 0 {
  75. t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", diff)
  76. }
  77. }
  78. }
  79. func randSlice(min, max uint32) []byte {
  80. var b = make([]byte, 4)
  81. rand.Read(b)
  82. a := binary.LittleEndian.Uint32(b)
  83. size := min + a%(max-min)
  84. out := make([]byte, size)
  85. rand.Read(out)
  86. return out
  87. }
  88. func TestDifficultyCalculators(t *testing.T) {
  89. rand.Seed(2)
  90. for i := 0; i < 5000; i++ {
  91. // 1 to 300 seconds diff
  92. var timeDelta = uint64(1 + rand.Uint32()%3000)
  93. diffBig := big.NewInt(0).SetBytes(randSlice(2, 10))
  94. if diffBig.Cmp(params.MinimumDifficulty) < 0 {
  95. diffBig.Set(params.MinimumDifficulty)
  96. }
  97. //rand.Read(difficulty)
  98. header := &types.Header{
  99. Difficulty: diffBig,
  100. Number: new(big.Int).SetUint64(rand.Uint64() % 50_000_000),
  101. Time: rand.Uint64() - timeDelta,
  102. }
  103. if rand.Uint32()&1 == 0 {
  104. header.UncleHash = types.EmptyUncleHash
  105. }
  106. bombDelay := new(big.Int).SetUint64(rand.Uint64() % 50_000_000)
  107. for i, pair := range []struct {
  108. bigFn func(time uint64, parent *types.Header) *big.Int
  109. u256Fn func(time uint64, parent *types.Header) *big.Int
  110. }{
  111. {FrontierDifficultyCalulator, CalcDifficultyFrontierU256},
  112. {HomesteadDifficultyCalulator, CalcDifficultyHomesteadU256},
  113. {DynamicDifficultyCalculator(bombDelay), MakeDifficultyCalculatorU256(bombDelay)},
  114. } {
  115. time := header.Time + timeDelta
  116. want := pair.bigFn(time, header)
  117. have := pair.u256Fn(time, header)
  118. if want.BitLen() > 256 {
  119. continue
  120. }
  121. if want.Cmp(have) != 0 {
  122. t.Fatalf("pair %d: want %x have %x\nparent.Number: %x\np.Time: %x\nc.Time: %x\nBombdelay: %v\n", i, want, have,
  123. header.Number, header.Time, time, bombDelay)
  124. }
  125. }
  126. }
  127. }
  128. func BenchmarkDifficultyCalculator(b *testing.B) {
  129. x1 := makeDifficultyCalculator(big.NewInt(1000000))
  130. x2 := MakeDifficultyCalculatorU256(big.NewInt(1000000))
  131. h := &types.Header{
  132. ParentHash: common.Hash{},
  133. UncleHash: types.EmptyUncleHash,
  134. Difficulty: big.NewInt(0xffffff),
  135. Number: big.NewInt(500000),
  136. Time: 1000000,
  137. }
  138. b.Run("big-frontier", func(b *testing.B) {
  139. b.ReportAllocs()
  140. for i := 0; i < b.N; i++ {
  141. calcDifficultyFrontier(1000014, h)
  142. }
  143. })
  144. b.Run("u256-frontier", func(b *testing.B) {
  145. b.ReportAllocs()
  146. for i := 0; i < b.N; i++ {
  147. CalcDifficultyFrontierU256(1000014, h)
  148. }
  149. })
  150. b.Run("big-homestead", func(b *testing.B) {
  151. b.ReportAllocs()
  152. for i := 0; i < b.N; i++ {
  153. calcDifficultyHomestead(1000014, h)
  154. }
  155. })
  156. b.Run("u256-homestead", func(b *testing.B) {
  157. b.ReportAllocs()
  158. for i := 0; i < b.N; i++ {
  159. CalcDifficultyHomesteadU256(1000014, h)
  160. }
  161. })
  162. b.Run("big-generic", func(b *testing.B) {
  163. b.ReportAllocs()
  164. for i := 0; i < b.N; i++ {
  165. x1(1000014, h)
  166. }
  167. })
  168. b.Run("u256-generic", func(b *testing.B) {
  169. b.ReportAllocs()
  170. for i := 0; i < b.N; i++ {
  171. x2(1000014, h)
  172. }
  173. })
  174. }