difficulty-fuzz.go 4.1 KB

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