txfetcher_fuzzer.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 txfetcher
  17. import (
  18. "bytes"
  19. "fmt"
  20. "math/big"
  21. "math/rand"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/common/mclock"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/eth/fetcher"
  27. )
  28. var (
  29. peers []string
  30. txs []*types.Transaction
  31. )
  32. func init() {
  33. // Random is nice, but we need it deterministic
  34. rand := rand.New(rand.NewSource(0x3a29))
  35. peers = make([]string, 10)
  36. for i := 0; i < len(peers); i++ {
  37. peers[i] = fmt.Sprintf("Peer #%d", i)
  38. }
  39. txs = make([]*types.Transaction, 65536) // We need to bump enough to hit all the limits
  40. for i := 0; i < len(txs); i++ {
  41. txs[i] = types.NewTransaction(rand.Uint64(), common.Address{byte(rand.Intn(256))}, new(big.Int), 0, new(big.Int), nil)
  42. }
  43. }
  44. func Fuzz(input []byte) int {
  45. // Don't generate insanely large test cases, not much value in them
  46. if len(input) > 16*1024 {
  47. return 0
  48. }
  49. verbose := false
  50. r := bytes.NewReader(input)
  51. // Reduce the problem space for certain fuzz runs. Small tx space is better
  52. // for testing clashes and in general the fetcher, but we should still run
  53. // some tests with large spaces to hit potential issues on limits.
  54. limit, err := r.ReadByte()
  55. if err != nil {
  56. return 0
  57. }
  58. switch limit % 4 {
  59. case 0:
  60. txs = txs[:4]
  61. case 1:
  62. txs = txs[:256]
  63. case 2:
  64. txs = txs[:4096]
  65. case 3:
  66. // Full run
  67. }
  68. // Create a fetcher and hook into it's simulated fields
  69. clock := new(mclock.Simulated)
  70. rand := rand.New(rand.NewSource(0x3a29)) // Same used in package tests!!!
  71. f := fetcher.NewTxFetcherForTests(
  72. func(common.Hash) bool { return false },
  73. func(txs []*types.Transaction) []error {
  74. return make([]error, len(txs))
  75. },
  76. func(string, []common.Hash) error { return nil },
  77. clock, rand,
  78. )
  79. f.Start()
  80. defer f.Stop()
  81. // Try to throw random junk at the fetcher
  82. for {
  83. // Read the next command and abort if we're done
  84. cmd, err := r.ReadByte()
  85. if err != nil {
  86. return 0
  87. }
  88. switch cmd % 4 {
  89. case 0:
  90. // Notify a new set of transactions:
  91. // Byte 1: Peer index to announce with
  92. // Byte 2: Number of hashes to announce
  93. // Byte 3-4, 5-6, etc: Transaction indices (2 byte) to announce
  94. peerIdx, err := r.ReadByte()
  95. if err != nil {
  96. return 0
  97. }
  98. peer := peers[int(peerIdx)%len(peers)]
  99. announceCnt, err := r.ReadByte()
  100. if err != nil {
  101. return 0
  102. }
  103. announce := int(announceCnt) % (2 * len(txs)) // No point in generating too many duplicates
  104. var (
  105. announceIdxs = make([]int, announce)
  106. announces = make([]common.Hash, announce)
  107. )
  108. for i := 0; i < len(announces); i++ {
  109. annBuf := make([]byte, 2)
  110. if n, err := r.Read(annBuf); err != nil || n != 2 {
  111. return 0
  112. }
  113. announceIdxs[i] = (int(annBuf[0])*256 + int(annBuf[1])) % len(txs)
  114. announces[i] = txs[announceIdxs[i]].Hash()
  115. }
  116. if verbose {
  117. fmt.Println("Notify", peer, announceIdxs)
  118. }
  119. if err := f.Notify(peer, announces); err != nil {
  120. panic(err)
  121. }
  122. case 1:
  123. // Deliver a new set of transactions:
  124. // Byte 1: Peer index to announce with
  125. // Byte 2: Number of hashes to announce
  126. // Byte 3-4, 5-6, etc: Transaction indices (2 byte) to announce
  127. peerIdx, err := r.ReadByte()
  128. if err != nil {
  129. return 0
  130. }
  131. peer := peers[int(peerIdx)%len(peers)]
  132. deliverCnt, err := r.ReadByte()
  133. if err != nil {
  134. return 0
  135. }
  136. deliver := int(deliverCnt) % (2 * len(txs)) // No point in generating too many duplicates
  137. var (
  138. deliverIdxs = make([]int, deliver)
  139. deliveries = make([]*types.Transaction, deliver)
  140. )
  141. for i := 0; i < len(deliveries); i++ {
  142. deliverBuf := make([]byte, 2)
  143. if n, err := r.Read(deliverBuf); err != nil || n != 2 {
  144. return 0
  145. }
  146. deliverIdxs[i] = (int(deliverBuf[0])*256 + int(deliverBuf[1])) % len(txs)
  147. deliveries[i] = txs[deliverIdxs[i]]
  148. }
  149. directFlag, err := r.ReadByte()
  150. if err != nil {
  151. return 0
  152. }
  153. direct := (directFlag % 2) == 0
  154. if verbose {
  155. fmt.Println("Enqueue", peer, deliverIdxs, direct)
  156. }
  157. if err := f.Enqueue(peer, deliveries, direct); err != nil {
  158. panic(err)
  159. }
  160. case 2:
  161. // Drop a peer:
  162. // Byte 1: Peer index to drop
  163. peerIdx, err := r.ReadByte()
  164. if err != nil {
  165. return 0
  166. }
  167. peer := peers[int(peerIdx)%len(peers)]
  168. if verbose {
  169. fmt.Println("Drop", peer)
  170. }
  171. if err := f.Drop(peer); err != nil {
  172. panic(err)
  173. }
  174. case 3:
  175. // Move the simulated clock forward
  176. // Byte 1: 100ms increment to move forward
  177. tickCnt, err := r.ReadByte()
  178. if err != nil {
  179. return 0
  180. }
  181. tick := time.Duration(tickCnt) * 100 * time.Millisecond
  182. if verbose {
  183. fmt.Println("Sleep", tick)
  184. }
  185. clock.Run(tick)
  186. }
  187. }
  188. }