difficulty-fuzz.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2020 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 difficulty
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "fmt"
  21. "io"
  22. "math/big"
  23. "github.com/ethereum/go-ethereum/consensus/ethash"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. )
  26. type fuzzer struct {
  27. input io.Reader
  28. exhausted bool
  29. debugging bool
  30. }
  31. func (f *fuzzer) read(size int) []byte {
  32. out := make([]byte, size)
  33. if _, err := f.input.Read(out); err != nil {
  34. f.exhausted = true
  35. }
  36. return out
  37. }
  38. func (f *fuzzer) readSlice(min, max int) []byte {
  39. var a uint16
  40. binary.Read(f.input, binary.LittleEndian, &a)
  41. size := min + int(a)%(max-min)
  42. out := make([]byte, size)
  43. if _, err := f.input.Read(out); err != nil {
  44. f.exhausted = true
  45. }
  46. return out
  47. }
  48. func (f *fuzzer) readUint64(min, max uint64) uint64 {
  49. if min == max {
  50. return min
  51. }
  52. var a uint64
  53. if err := binary.Read(f.input, binary.LittleEndian, &a); err != nil {
  54. f.exhausted = true
  55. }
  56. a = min + a%(max-min)
  57. return a
  58. }
  59. func (f *fuzzer) readBool() bool {
  60. return f.read(1)[0]&0x1 == 0
  61. }
  62. // The function must return
  63. // 1 if the fuzzer should increase priority of the
  64. // given input during subsequent fuzzing (for example, the input is lexically
  65. // correct and was parsed successfully);
  66. // -1 if the input must not be added to corpus even if gives new coverage; and
  67. // 0 otherwise
  68. // other values are reserved for future use.
  69. func Fuzz(data []byte) int {
  70. f := fuzzer{
  71. input: bytes.NewReader(data),
  72. exhausted: false,
  73. }
  74. return f.fuzz()
  75. }
  76. var minDifficulty = big.NewInt(0x2000)
  77. type calculator func(time uint64, parent *types.Header) *big.Int
  78. func (f *fuzzer) fuzz() int {
  79. // A parent header
  80. header := &types.Header{}
  81. if f.readBool() {
  82. header.UncleHash = types.EmptyUncleHash
  83. }
  84. // Difficulty can range between 0x2000 (2 bytes) and up to 32 bytes
  85. {
  86. diff := new(big.Int).SetBytes(f.readSlice(2, 32))
  87. if diff.Cmp(minDifficulty) < 0 {
  88. diff.Set(minDifficulty)
  89. }
  90. header.Difficulty = diff
  91. }
  92. // Number can range between 0 and up to 32 bytes (but not so that the child exceeds it)
  93. {
  94. // However, if we use astronomic numbers, then the bomb exp karatsuba calculation
  95. // in the legacy methods)
  96. // times out, so we limit it to fit within reasonable bounds
  97. number := new(big.Int).SetBytes(f.readSlice(0, 4)) // 4 bytes: 32 bits: block num max 4 billion
  98. header.Number = number
  99. }
  100. // Both parent and child time must fit within uint64
  101. var time uint64
  102. {
  103. childTime := f.readUint64(1, 0xFFFFFFFFFFFFFFFF)
  104. //fmt.Printf("childTime: %x\n",childTime)
  105. delta := f.readUint64(1, childTime)
  106. //fmt.Printf("delta: %v\n", delta)
  107. pTime := childTime - delta
  108. header.Time = pTime
  109. time = childTime
  110. }
  111. // Bomb delay will never exceed uint64
  112. bombDelay := new(big.Int).SetUint64(f.readUint64(1, 0xFFFFFFFFFFFFFFFe))
  113. if f.exhausted {
  114. return 0
  115. }
  116. for i, pair := range []struct {
  117. bigFn calculator
  118. u256Fn calculator
  119. }{
  120. {ethash.FrontierDifficultyCalulator, ethash.CalcDifficultyFrontierU256},
  121. {ethash.HomesteadDifficultyCalulator, ethash.CalcDifficultyHomesteadU256},
  122. {ethash.DynamicDifficultyCalculator(bombDelay), ethash.MakeDifficultyCalculatorU256(bombDelay)},
  123. } {
  124. want := pair.bigFn(time, header)
  125. have := pair.u256Fn(time, header)
  126. if want.Cmp(have) != 0 {
  127. panic(fmt.Sprintf("pair %d: want %x have %x\nparent.Number: %x\np.Time: %x\nc.Time: %x\nBombdelay: %v\n", i, want, have,
  128. header.Number, header.Time, time, bombDelay))
  129. }
  130. }
  131. return 1
  132. }