github.com/ethereum/go-ethereum@v1.16.1/tests/fuzzers/txfetcher/txfetcher_fuzzer.go (about)

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