github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/eth/fetcher/fetcher_test.go (about)

     1  // Copyright 2015 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 fetcher
    18  
    19  import (
    20  	"errors"
    21  	"math/big"
    22  	"sync"
    23  	"sync/atomic"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/ShyftNetwork/go-empyrean/common"
    28  	"github.com/ShyftNetwork/go-empyrean/consensus/ethash"
    29  	"github.com/ShyftNetwork/go-empyrean/core"
    30  	"github.com/ShyftNetwork/go-empyrean/core/types"
    31  	"github.com/ShyftNetwork/go-empyrean/crypto"
    32  	"github.com/ShyftNetwork/go-empyrean/ethdb"
    33  	"github.com/ShyftNetwork/go-empyrean/params"
    34  )
    35  
    36  var (
    37  	testdb         = ethdb.NewMemDatabase()
    38  	shyftTestdb, _ = ethdb.NewShyftDatabase()
    39  	testKey, _     = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    40  	testAddress    = crypto.PubkeyToAddress(testKey.PublicKey)
    41  	genesis        = core.GenesisBlockForTesting(testdb, testAddress, big.NewInt(1000000000))
    42  	unknownBlock   = types.NewBlock(&types.Header{GasLimit: params.GenesisGasLimit}, nil, nil, nil)
    43  )
    44  
    45  // makeChain creates a chain of n blocks starting at and including parent.
    46  // the returned hash chain is ordered head->parent. In addition, every 3rd block
    47  // contains a transaction and every 5th an uncle to allow testing correct block
    48  // reassembly.
    49  func makeChain(n int, seed byte, parent *types.Block) ([]common.Hash, map[common.Hash]*types.Block) {
    50  	blocks, _ := core.GenerateChain(params.TestChainConfig, parent, ethash.NewFaker(), testdb, shyftTestdb, n, func(i int, block *core.BlockGen) {
    51  		block.SetCoinbase(common.Address{seed})
    52  
    53  		// If the block number is multiple of 3, send a bonus transaction to the miner
    54  		if parent == genesis && i%3 == 0 {
    55  			signer := types.MakeSigner(params.TestChainConfig, block.Number())
    56  			tx, err := types.SignTx(types.NewTransaction(block.TxNonce(testAddress), common.Address{seed}, big.NewInt(1000), params.TxGas, nil, nil), signer, testKey)
    57  			if err != nil {
    58  				panic(err)
    59  			}
    60  			block.AddTx(tx)
    61  		}
    62  		// If the block number is a multiple of 5, add a bonus uncle to the block
    63  		if i%5 == 0 {
    64  			block.AddUncle(&types.Header{ParentHash: block.PrevBlock(i - 1).Hash(), Number: big.NewInt(int64(i - 1))})
    65  		}
    66  	})
    67  	hashes := make([]common.Hash, n+1)
    68  	hashes[len(hashes)-1] = parent.Hash()
    69  	blockm := make(map[common.Hash]*types.Block, n+1)
    70  	blockm[parent.Hash()] = parent
    71  	for i, b := range blocks {
    72  		hashes[len(hashes)-i-2] = b.Hash()
    73  		blockm[b.Hash()] = b
    74  	}
    75  	return hashes, blockm
    76  }
    77  
    78  // fetcherTester is a test simulator for mocking out local block chain.
    79  type fetcherTester struct {
    80  	fetcher *Fetcher
    81  
    82  	hashes []common.Hash                // Hash chain belonging to the tester
    83  	blocks map[common.Hash]*types.Block // Blocks belonging to the tester
    84  	drops  map[string]bool              // Map of peers dropped by the fetcher
    85  
    86  	lock sync.RWMutex
    87  }
    88  
    89  // newTester creates a new fetcher test mocker.
    90  func newTester() *fetcherTester {
    91  	tester := &fetcherTester{
    92  		hashes: []common.Hash{genesis.Hash()},
    93  		blocks: map[common.Hash]*types.Block{genesis.Hash(): genesis},
    94  		drops:  make(map[string]bool),
    95  	}
    96  	tester.fetcher = New(tester.getBlock, tester.verifyHeader, tester.broadcastBlock, tester.chainHeight, tester.insertChain, tester.dropPeer)
    97  	tester.fetcher.Start()
    98  
    99  	return tester
   100  }
   101  
   102  // getBlock retrieves a block from the tester's block chain.
   103  func (f *fetcherTester) getBlock(hash common.Hash) *types.Block {
   104  	f.lock.RLock()
   105  	defer f.lock.RUnlock()
   106  
   107  	return f.blocks[hash]
   108  }
   109  
   110  // verifyHeader is a nop placeholder for the block header verification.
   111  func (f *fetcherTester) verifyHeader(header *types.Header) error {
   112  	return nil
   113  }
   114  
   115  // broadcastBlock is a nop placeholder for the block broadcasting.
   116  func (f *fetcherTester) broadcastBlock(block *types.Block, propagate bool) {
   117  }
   118  
   119  // chainHeight retrieves the current height (block number) of the chain.
   120  func (f *fetcherTester) chainHeight() uint64 {
   121  	f.lock.RLock()
   122  	defer f.lock.RUnlock()
   123  
   124  	return f.blocks[f.hashes[len(f.hashes)-1]].NumberU64()
   125  }
   126  
   127  // insertChain injects a new blocks into the simulated chain.
   128  func (f *fetcherTester) insertChain(blocks types.Blocks) (int, error) {
   129  	f.lock.Lock()
   130  	defer f.lock.Unlock()
   131  
   132  	for i, block := range blocks {
   133  		// Make sure the parent in known
   134  		if _, ok := f.blocks[block.ParentHash()]; !ok {
   135  			return i, errors.New("unknown parent")
   136  		}
   137  		// Discard any new blocks if the same height already exists
   138  		if block.NumberU64() <= f.blocks[f.hashes[len(f.hashes)-1]].NumberU64() {
   139  			return i, nil
   140  		}
   141  		// Otherwise build our current chain
   142  		f.hashes = append(f.hashes, block.Hash())
   143  		f.blocks[block.Hash()] = block
   144  	}
   145  	return 0, nil
   146  }
   147  
   148  // dropPeer is an emulator for the peer removal, simply accumulating the various
   149  // peers dropped by the fetcher.
   150  func (f *fetcherTester) dropPeer(peer string) {
   151  	f.lock.Lock()
   152  	defer f.lock.Unlock()
   153  
   154  	f.drops[peer] = true
   155  }
   156  
   157  // makeHeaderFetcher retrieves a block header fetcher associated with a simulated peer.
   158  func (f *fetcherTester) makeHeaderFetcher(peer string, blocks map[common.Hash]*types.Block, drift time.Duration) headerRequesterFn {
   159  	closure := make(map[common.Hash]*types.Block)
   160  	for hash, block := range blocks {
   161  		closure[hash] = block
   162  	}
   163  	// Create a function that return a header from the closure
   164  	return func(hash common.Hash) error {
   165  		// Gather the blocks to return
   166  		headers := make([]*types.Header, 0, 1)
   167  		if block, ok := closure[hash]; ok {
   168  			headers = append(headers, block.Header())
   169  		}
   170  		// Return on a new thread
   171  		go f.fetcher.FilterHeaders(peer, headers, time.Now().Add(drift))
   172  
   173  		return nil
   174  	}
   175  }
   176  
   177  // makeBodyFetcher retrieves a block body fetcher associated with a simulated peer.
   178  func (f *fetcherTester) makeBodyFetcher(peer string, blocks map[common.Hash]*types.Block, drift time.Duration) bodyRequesterFn {
   179  	closure := make(map[common.Hash]*types.Block)
   180  	for hash, block := range blocks {
   181  		closure[hash] = block
   182  	}
   183  	// Create a function that returns blocks from the closure
   184  	return func(hashes []common.Hash) error {
   185  		// Gather the block bodies to return
   186  		transactions := make([][]*types.Transaction, 0, len(hashes))
   187  		uncles := make([][]*types.Header, 0, len(hashes))
   188  
   189  		for _, hash := range hashes {
   190  			if block, ok := closure[hash]; ok {
   191  				transactions = append(transactions, block.Transactions())
   192  				uncles = append(uncles, block.Uncles())
   193  			}
   194  		}
   195  		// Return on a new thread
   196  		go f.fetcher.FilterBodies(peer, transactions, uncles, time.Now().Add(drift))
   197  
   198  		return nil
   199  	}
   200  }
   201  
   202  // verifyFetchingEvent verifies that one single event arrive on a fetching channel.
   203  func verifyFetchingEvent(t *testing.T, fetching chan []common.Hash, arrive bool) {
   204  	if arrive {
   205  		select {
   206  		case <-fetching:
   207  		case <-time.After(time.Second):
   208  			t.Fatalf("fetching timeout")
   209  		}
   210  	} else {
   211  		select {
   212  		case <-fetching:
   213  			t.Fatalf("fetching invoked")
   214  		case <-time.After(10 * time.Millisecond):
   215  		}
   216  	}
   217  }
   218  
   219  // verifyCompletingEvent verifies that one single event arrive on an completing channel.
   220  func verifyCompletingEvent(t *testing.T, completing chan []common.Hash, arrive bool) {
   221  	if arrive {
   222  		select {
   223  		case <-completing:
   224  		case <-time.After(time.Second):
   225  			t.Fatalf("completing timeout")
   226  		}
   227  	} else {
   228  		select {
   229  		case <-completing:
   230  			t.Fatalf("completing invoked")
   231  		case <-time.After(10 * time.Millisecond):
   232  		}
   233  	}
   234  }
   235  
   236  // verifyImportEvent verifies that one single event arrive on an import channel.
   237  func verifyImportEvent(t *testing.T, imported chan *types.Block, arrive bool) {
   238  	if arrive {
   239  		select {
   240  		case <-imported:
   241  		case <-time.After(time.Second):
   242  			t.Fatalf("import timeout")
   243  		}
   244  	} else {
   245  		select {
   246  		case <-imported:
   247  			t.Fatalf("import invoked")
   248  		case <-time.After(10 * time.Millisecond):
   249  		}
   250  	}
   251  }
   252  
   253  // verifyImportCount verifies that exactly count number of events arrive on an
   254  // import hook channel.
   255  func verifyImportCount(t *testing.T, imported chan *types.Block, count int) {
   256  	for i := 0; i < count; i++ {
   257  		select {
   258  		case <-imported:
   259  		case <-time.After(time.Second):
   260  			t.Fatalf("block %d: import timeout", i+1)
   261  		}
   262  	}
   263  	verifyImportDone(t, imported)
   264  }
   265  
   266  // verifyImportDone verifies that no more events are arriving on an import channel.
   267  func verifyImportDone(t *testing.T, imported chan *types.Block) {
   268  	select {
   269  	case <-imported:
   270  		t.Fatalf("extra block imported")
   271  	case <-time.After(50 * time.Millisecond):
   272  	}
   273  }
   274  
   275  // Tests that a fetcher accepts block announcements and initiates retrievals for
   276  // them, successfully importing into the local chain.
   277  func TestSequentialAnnouncements62(t *testing.T) { testSequentialAnnouncements(t, 62) }
   278  func TestSequentialAnnouncements63(t *testing.T) { testSequentialAnnouncements(t, 63) }
   279  func TestSequentialAnnouncements64(t *testing.T) { testSequentialAnnouncements(t, 64) }
   280  
   281  func testSequentialAnnouncements(t *testing.T, protocol int) {
   282  	// Create a chain of blocks to import
   283  	targetBlocks := 4 * hashLimit
   284  	hashes, blocks := makeChain(targetBlocks, 0, genesis)
   285  
   286  	tester := newTester()
   287  	headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack)
   288  	bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0)
   289  
   290  	// Iteratively announce blocks until all are imported
   291  	imported := make(chan *types.Block)
   292  	tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
   293  
   294  	for i := len(hashes) - 2; i >= 0; i-- {
   295  		tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher)
   296  		verifyImportEvent(t, imported, true)
   297  	}
   298  	verifyImportDone(t, imported)
   299  }
   300  
   301  // Tests that if blocks are announced by multiple peers (or even the same buggy
   302  // peer), they will only get downloaded at most once.
   303  func TestConcurrentAnnouncements62(t *testing.T) { testConcurrentAnnouncements(t, 62) }
   304  func TestConcurrentAnnouncements63(t *testing.T) { testConcurrentAnnouncements(t, 63) }
   305  func TestConcurrentAnnouncements64(t *testing.T) { testConcurrentAnnouncements(t, 64) }
   306  
   307  func testConcurrentAnnouncements(t *testing.T, protocol int) {
   308  	// Create a chain of blocks to import
   309  	targetBlocks := 4 * hashLimit
   310  	hashes, blocks := makeChain(targetBlocks, 0, genesis)
   311  
   312  	// Assemble a tester with a built in counter for the requests
   313  	tester := newTester()
   314  	firstHeaderFetcher := tester.makeHeaderFetcher("first", blocks, -gatherSlack)
   315  	firstBodyFetcher := tester.makeBodyFetcher("first", blocks, 0)
   316  	secondHeaderFetcher := tester.makeHeaderFetcher("second", blocks, -gatherSlack)
   317  	secondBodyFetcher := tester.makeBodyFetcher("second", blocks, 0)
   318  
   319  	counter := uint32(0)
   320  	firstHeaderWrapper := func(hash common.Hash) error {
   321  		atomic.AddUint32(&counter, 1)
   322  		return firstHeaderFetcher(hash)
   323  	}
   324  	secondHeaderWrapper := func(hash common.Hash) error {
   325  		atomic.AddUint32(&counter, 1)
   326  		return secondHeaderFetcher(hash)
   327  	}
   328  	// Iteratively announce blocks until all are imported
   329  	imported := make(chan *types.Block)
   330  	tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
   331  
   332  	for i := len(hashes) - 2; i >= 0; i-- {
   333  		tester.fetcher.Notify("first", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), firstHeaderWrapper, firstBodyFetcher)
   334  		tester.fetcher.Notify("second", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout+time.Millisecond), secondHeaderWrapper, secondBodyFetcher)
   335  		tester.fetcher.Notify("second", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout-time.Millisecond), secondHeaderWrapper, secondBodyFetcher)
   336  		verifyImportEvent(t, imported, true)
   337  	}
   338  	verifyImportDone(t, imported)
   339  
   340  	// Make sure no blocks were retrieved twice
   341  	if int(counter) != targetBlocks {
   342  		t.Fatalf("retrieval count mismatch: have %v, want %v", counter, targetBlocks)
   343  	}
   344  }
   345  
   346  // Tests that announcements arriving while a previous is being fetched still
   347  // results in a valid import.
   348  func TestOverlappingAnnouncements62(t *testing.T) { testOverlappingAnnouncements(t, 62) }
   349  func TestOverlappingAnnouncements63(t *testing.T) { testOverlappingAnnouncements(t, 63) }
   350  func TestOverlappingAnnouncements64(t *testing.T) { testOverlappingAnnouncements(t, 64) }
   351  
   352  func testOverlappingAnnouncements(t *testing.T, protocol int) {
   353  	// Create a chain of blocks to import
   354  	targetBlocks := 4 * hashLimit
   355  	hashes, blocks := makeChain(targetBlocks, 0, genesis)
   356  
   357  	tester := newTester()
   358  	headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack)
   359  	bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0)
   360  
   361  	// Iteratively announce blocks, but overlap them continuously
   362  	overlap := 16
   363  	imported := make(chan *types.Block, len(hashes)-1)
   364  	for i := 0; i < overlap; i++ {
   365  		imported <- nil
   366  	}
   367  	tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
   368  
   369  	for i := len(hashes) - 2; i >= 0; i-- {
   370  		tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher)
   371  		select {
   372  		case <-imported:
   373  		case <-time.After(time.Second):
   374  			t.Fatalf("block %d: import timeout", len(hashes)-i)
   375  		}
   376  	}
   377  	// Wait for all the imports to complete and check count
   378  	verifyImportCount(t, imported, overlap)
   379  }
   380  
   381  // Tests that announces already being retrieved will not be duplicated.
   382  func TestPendingDeduplication62(t *testing.T) { testPendingDeduplication(t, 62) }
   383  func TestPendingDeduplication63(t *testing.T) { testPendingDeduplication(t, 63) }
   384  func TestPendingDeduplication64(t *testing.T) { testPendingDeduplication(t, 64) }
   385  
   386  func testPendingDeduplication(t *testing.T, protocol int) {
   387  	// Create a hash and corresponding block
   388  	hashes, blocks := makeChain(1, 0, genesis)
   389  
   390  	// Assemble a tester with a built in counter and delayed fetcher
   391  	tester := newTester()
   392  	headerFetcher := tester.makeHeaderFetcher("repeater", blocks, -gatherSlack)
   393  	bodyFetcher := tester.makeBodyFetcher("repeater", blocks, 0)
   394  
   395  	delay := 50 * time.Millisecond
   396  	counter := uint32(0)
   397  	headerWrapper := func(hash common.Hash) error {
   398  		atomic.AddUint32(&counter, 1)
   399  
   400  		// Simulate a long running fetch
   401  		go func() {
   402  			time.Sleep(delay)
   403  			headerFetcher(hash)
   404  		}()
   405  		return nil
   406  	}
   407  	// Announce the same block many times until it's fetched (wait for any pending ops)
   408  	for tester.getBlock(hashes[0]) == nil {
   409  		tester.fetcher.Notify("repeater", hashes[0], 1, time.Now().Add(-arriveTimeout), headerWrapper, bodyFetcher)
   410  		time.Sleep(time.Millisecond)
   411  	}
   412  	time.Sleep(delay)
   413  
   414  	// Check that all blocks were imported and none fetched twice
   415  	if imported := len(tester.blocks); imported != 2 {
   416  		t.Fatalf("synchronised block mismatch: have %v, want %v", imported, 2)
   417  	}
   418  	if int(counter) != 1 {
   419  		t.Fatalf("retrieval count mismatch: have %v, want %v", counter, 1)
   420  	}
   421  }
   422  
   423  // Tests that announcements retrieved in a random order are cached and eventually
   424  // imported when all the gaps are filled in.
   425  func TestRandomArrivalImport62(t *testing.T) { testRandomArrivalImport(t, 62) }
   426  func TestRandomArrivalImport63(t *testing.T) { testRandomArrivalImport(t, 63) }
   427  func TestRandomArrivalImport64(t *testing.T) { testRandomArrivalImport(t, 64) }
   428  
   429  func testRandomArrivalImport(t *testing.T, protocol int) {
   430  	// Create a chain of blocks to import, and choose one to delay
   431  	targetBlocks := maxQueueDist
   432  	hashes, blocks := makeChain(targetBlocks, 0, genesis)
   433  	skip := targetBlocks / 2
   434  
   435  	tester := newTester()
   436  	headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack)
   437  	bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0)
   438  
   439  	// Iteratively announce blocks, skipping one entry
   440  	imported := make(chan *types.Block, len(hashes)-1)
   441  	tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
   442  
   443  	for i := len(hashes) - 1; i >= 0; i-- {
   444  		if i != skip {
   445  			tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher)
   446  			time.Sleep(time.Millisecond)
   447  		}
   448  	}
   449  	// Finally announce the skipped entry and check full import
   450  	tester.fetcher.Notify("valid", hashes[skip], uint64(len(hashes)-skip-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher)
   451  	verifyImportCount(t, imported, len(hashes)-1)
   452  }
   453  
   454  // Tests that direct block enqueues (due to block propagation vs. hash announce)
   455  // are correctly schedule, filling and import queue gaps.
   456  func TestQueueGapFill62(t *testing.T) { testQueueGapFill(t, 62) }
   457  func TestQueueGapFill63(t *testing.T) { testQueueGapFill(t, 63) }
   458  func TestQueueGapFill64(t *testing.T) { testQueueGapFill(t, 64) }
   459  
   460  func testQueueGapFill(t *testing.T, protocol int) {
   461  	// Create a chain of blocks to import, and choose one to not announce at all
   462  	targetBlocks := maxQueueDist
   463  	hashes, blocks := makeChain(targetBlocks, 0, genesis)
   464  	skip := targetBlocks / 2
   465  
   466  	tester := newTester()
   467  	headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack)
   468  	bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0)
   469  
   470  	// Iteratively announce blocks, skipping one entry
   471  	imported := make(chan *types.Block, len(hashes)-1)
   472  	tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
   473  
   474  	for i := len(hashes) - 1; i >= 0; i-- {
   475  		if i != skip {
   476  			tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher)
   477  			time.Sleep(time.Millisecond)
   478  		}
   479  	}
   480  	// Fill the missing block directly as if propagated
   481  	tester.fetcher.Enqueue("valid", blocks[hashes[skip]])
   482  	verifyImportCount(t, imported, len(hashes)-1)
   483  }
   484  
   485  // Tests that blocks arriving from various sources (multiple propagations, hash
   486  // announces, etc) do not get scheduled for import multiple times.
   487  func TestImportDeduplication62(t *testing.T) { testImportDeduplication(t, 62) }
   488  func TestImportDeduplication63(t *testing.T) { testImportDeduplication(t, 63) }
   489  func TestImportDeduplication64(t *testing.T) { testImportDeduplication(t, 64) }
   490  
   491  func testImportDeduplication(t *testing.T, protocol int) {
   492  	// Create two blocks to import (one for duplication, the other for stalling)
   493  	hashes, blocks := makeChain(2, 0, genesis)
   494  
   495  	// Create the tester and wrap the importer with a counter
   496  	tester := newTester()
   497  	headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack)
   498  	bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0)
   499  
   500  	counter := uint32(0)
   501  	tester.fetcher.insertChain = func(blocks types.Blocks) (int, error) {
   502  		atomic.AddUint32(&counter, uint32(len(blocks)))
   503  		return tester.insertChain(blocks)
   504  	}
   505  	// Instrument the fetching and imported events
   506  	fetching := make(chan []common.Hash)
   507  	imported := make(chan *types.Block, len(hashes)-1)
   508  	tester.fetcher.fetchingHook = func(hashes []common.Hash) { fetching <- hashes }
   509  	tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
   510  
   511  	// Announce the duplicating block, wait for retrieval, and also propagate directly
   512  	tester.fetcher.Notify("valid", hashes[0], 1, time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher)
   513  	<-fetching
   514  
   515  	tester.fetcher.Enqueue("valid", blocks[hashes[0]])
   516  	tester.fetcher.Enqueue("valid", blocks[hashes[0]])
   517  	tester.fetcher.Enqueue("valid", blocks[hashes[0]])
   518  
   519  	// Fill the missing block directly as if propagated, and check import uniqueness
   520  	tester.fetcher.Enqueue("valid", blocks[hashes[1]])
   521  	verifyImportCount(t, imported, 2)
   522  
   523  	if counter != 2 {
   524  		t.Fatalf("import invocation count mismatch: have %v, want %v", counter, 2)
   525  	}
   526  }
   527  
   528  // Tests that blocks with numbers much lower or higher than out current head get
   529  // discarded to prevent wasting resources on useless blocks from faulty peers.
   530  func TestDistantPropagationDiscarding(t *testing.T) {
   531  	// Create a long chain to import and define the discard boundaries
   532  	hashes, blocks := makeChain(3*maxQueueDist, 0, genesis)
   533  	head := hashes[len(hashes)/2]
   534  
   535  	low, high := len(hashes)/2+maxUncleDist+1, len(hashes)/2-maxQueueDist-1
   536  
   537  	// Create a tester and simulate a head block being the middle of the above chain
   538  	tester := newTester()
   539  
   540  	tester.lock.Lock()
   541  	tester.hashes = []common.Hash{head}
   542  	tester.blocks = map[common.Hash]*types.Block{head: blocks[head]}
   543  	tester.lock.Unlock()
   544  
   545  	// Ensure that a block with a lower number than the threshold is discarded
   546  	tester.fetcher.Enqueue("lower", blocks[hashes[low]])
   547  	time.Sleep(10 * time.Millisecond)
   548  	if !tester.fetcher.queue.Empty() {
   549  		t.Fatalf("fetcher queued stale block")
   550  	}
   551  	// Ensure that a block with a higher number than the threshold is discarded
   552  	tester.fetcher.Enqueue("higher", blocks[hashes[high]])
   553  	time.Sleep(10 * time.Millisecond)
   554  	if !tester.fetcher.queue.Empty() {
   555  		t.Fatalf("fetcher queued future block")
   556  	}
   557  }
   558  
   559  // Tests that announcements with numbers much lower or higher than out current
   560  // head get discarded to prevent wasting resources on useless blocks from faulty
   561  // peers.
   562  func TestDistantAnnouncementDiscarding62(t *testing.T) { testDistantAnnouncementDiscarding(t, 62) }
   563  func TestDistantAnnouncementDiscarding63(t *testing.T) { testDistantAnnouncementDiscarding(t, 63) }
   564  func TestDistantAnnouncementDiscarding64(t *testing.T) { testDistantAnnouncementDiscarding(t, 64) }
   565  
   566  func testDistantAnnouncementDiscarding(t *testing.T, protocol int) {
   567  	// Create a long chain to import and define the discard boundaries
   568  	hashes, blocks := makeChain(3*maxQueueDist, 0, genesis)
   569  	head := hashes[len(hashes)/2]
   570  
   571  	low, high := len(hashes)/2+maxUncleDist+1, len(hashes)/2-maxQueueDist-1
   572  
   573  	// Create a tester and simulate a head block being the middle of the above chain
   574  	tester := newTester()
   575  
   576  	tester.lock.Lock()
   577  	tester.hashes = []common.Hash{head}
   578  	tester.blocks = map[common.Hash]*types.Block{head: blocks[head]}
   579  	tester.lock.Unlock()
   580  
   581  	headerFetcher := tester.makeHeaderFetcher("lower", blocks, -gatherSlack)
   582  	bodyFetcher := tester.makeBodyFetcher("lower", blocks, 0)
   583  
   584  	fetching := make(chan struct{}, 2)
   585  	tester.fetcher.fetchingHook = func(hashes []common.Hash) { fetching <- struct{}{} }
   586  
   587  	// Ensure that a block with a lower number than the threshold is discarded
   588  	tester.fetcher.Notify("lower", hashes[low], blocks[hashes[low]].NumberU64(), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher)
   589  	select {
   590  	case <-time.After(50 * time.Millisecond):
   591  	case <-fetching:
   592  		t.Fatalf("fetcher requested stale header")
   593  	}
   594  	// Ensure that a block with a higher number than the threshold is discarded
   595  	tester.fetcher.Notify("higher", hashes[high], blocks[hashes[high]].NumberU64(), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher)
   596  	select {
   597  	case <-time.After(50 * time.Millisecond):
   598  	case <-fetching:
   599  		t.Fatalf("fetcher requested future header")
   600  	}
   601  }
   602  
   603  // Tests that peers announcing blocks with invalid numbers (i.e. not matching
   604  // the headers provided afterwards) get dropped as malicious.
   605  func TestInvalidNumberAnnouncement62(t *testing.T) { testInvalidNumberAnnouncement(t, 62) }
   606  func TestInvalidNumberAnnouncement63(t *testing.T) { testInvalidNumberAnnouncement(t, 63) }
   607  func TestInvalidNumberAnnouncement64(t *testing.T) { testInvalidNumberAnnouncement(t, 64) }
   608  
   609  func testInvalidNumberAnnouncement(t *testing.T, protocol int) {
   610  	// Create a single block to import and check numbers against
   611  	hashes, blocks := makeChain(1, 0, genesis)
   612  
   613  	tester := newTester()
   614  	badHeaderFetcher := tester.makeHeaderFetcher("bad", blocks, -gatherSlack)
   615  	badBodyFetcher := tester.makeBodyFetcher("bad", blocks, 0)
   616  
   617  	imported := make(chan *types.Block)
   618  	tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
   619  
   620  	// Announce a block with a bad number, check for immediate drop
   621  	tester.fetcher.Notify("bad", hashes[0], 2, time.Now().Add(-arriveTimeout), badHeaderFetcher, badBodyFetcher)
   622  	verifyImportEvent(t, imported, false)
   623  
   624  	tester.lock.RLock()
   625  	dropped := tester.drops["bad"]
   626  	tester.lock.RUnlock()
   627  
   628  	if !dropped {
   629  		t.Fatalf("peer with invalid numbered announcement not dropped")
   630  	}
   631  
   632  	goodHeaderFetcher := tester.makeHeaderFetcher("good", blocks, -gatherSlack)
   633  	goodBodyFetcher := tester.makeBodyFetcher("good", blocks, 0)
   634  	// Make sure a good announcement passes without a drop
   635  	tester.fetcher.Notify("good", hashes[0], 1, time.Now().Add(-arriveTimeout), goodHeaderFetcher, goodBodyFetcher)
   636  	verifyImportEvent(t, imported, true)
   637  
   638  	tester.lock.RLock()
   639  	dropped = tester.drops["good"]
   640  	tester.lock.RUnlock()
   641  
   642  	if dropped {
   643  		t.Fatalf("peer with valid numbered announcement dropped")
   644  	}
   645  	verifyImportDone(t, imported)
   646  }
   647  
   648  // Tests that if a block is empty (i.e. header only), no body request should be
   649  // made, and instead the header should be assembled into a whole block in itself.
   650  func TestEmptyBlockShortCircuit62(t *testing.T) { testEmptyBlockShortCircuit(t, 62) }
   651  func TestEmptyBlockShortCircuit63(t *testing.T) { testEmptyBlockShortCircuit(t, 63) }
   652  func TestEmptyBlockShortCircuit64(t *testing.T) { testEmptyBlockShortCircuit(t, 64) }
   653  
   654  func testEmptyBlockShortCircuit(t *testing.T, protocol int) {
   655  	// Create a chain of blocks to import
   656  	hashes, blocks := makeChain(32, 0, genesis)
   657  
   658  	tester := newTester()
   659  	headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack)
   660  	bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0)
   661  
   662  	// Add a monitoring hook for all internal events
   663  	fetching := make(chan []common.Hash)
   664  	tester.fetcher.fetchingHook = func(hashes []common.Hash) { fetching <- hashes }
   665  
   666  	completing := make(chan []common.Hash)
   667  	tester.fetcher.completingHook = func(hashes []common.Hash) { completing <- hashes }
   668  
   669  	imported := make(chan *types.Block)
   670  	tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
   671  
   672  	// Iteratively announce blocks until all are imported
   673  	for i := len(hashes) - 2; i >= 0; i-- {
   674  		tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher)
   675  
   676  		// All announces should fetch the header
   677  		verifyFetchingEvent(t, fetching, true)
   678  
   679  		// Only blocks with data contents should request bodies
   680  		verifyCompletingEvent(t, completing, len(blocks[hashes[i]].Transactions()) > 0 || len(blocks[hashes[i]].Uncles()) > 0)
   681  
   682  		// Irrelevant of the construct, import should succeed
   683  		verifyImportEvent(t, imported, true)
   684  	}
   685  	verifyImportDone(t, imported)
   686  }
   687  
   688  // Tests that a peer is unable to use unbounded memory with sending infinite
   689  // block announcements to a node, but that even in the face of such an attack,
   690  // the fetcher remains operational.
   691  func TestHashMemoryExhaustionAttack62(t *testing.T) { testHashMemoryExhaustionAttack(t, 62) }
   692  func TestHashMemoryExhaustionAttack63(t *testing.T) { testHashMemoryExhaustionAttack(t, 63) }
   693  func TestHashMemoryExhaustionAttack64(t *testing.T) { testHashMemoryExhaustionAttack(t, 64) }
   694  
   695  func testHashMemoryExhaustionAttack(t *testing.T, protocol int) {
   696  	// Create a tester with instrumented import hooks
   697  	tester := newTester()
   698  
   699  	imported, announces := make(chan *types.Block), int32(0)
   700  	tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
   701  	tester.fetcher.announceChangeHook = func(hash common.Hash, added bool) {
   702  		if added {
   703  			atomic.AddInt32(&announces, 1)
   704  		} else {
   705  			atomic.AddInt32(&announces, -1)
   706  		}
   707  	}
   708  	// Create a valid chain and an infinite junk chain
   709  	targetBlocks := hashLimit + 2*maxQueueDist
   710  	hashes, blocks := makeChain(targetBlocks, 0, genesis)
   711  	validHeaderFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack)
   712  	validBodyFetcher := tester.makeBodyFetcher("valid", blocks, 0)
   713  
   714  	attack, _ := makeChain(targetBlocks, 0, unknownBlock)
   715  	attackerHeaderFetcher := tester.makeHeaderFetcher("attacker", nil, -gatherSlack)
   716  	attackerBodyFetcher := tester.makeBodyFetcher("attacker", nil, 0)
   717  
   718  	// Feed the tester a huge hashset from the attacker, and a limited from the valid peer
   719  	for i := 0; i < len(attack); i++ {
   720  		if i < maxQueueDist {
   721  			tester.fetcher.Notify("valid", hashes[len(hashes)-2-i], uint64(i+1), time.Now(), validHeaderFetcher, validBodyFetcher)
   722  		}
   723  		tester.fetcher.Notify("attacker", attack[i], 1 /* don't distance drop */, time.Now(), attackerHeaderFetcher, attackerBodyFetcher)
   724  	}
   725  	if count := atomic.LoadInt32(&announces); count != hashLimit+maxQueueDist {
   726  		t.Fatalf("queued announce count mismatch: have %d, want %d", count, hashLimit+maxQueueDist)
   727  	}
   728  	// Wait for fetches to complete
   729  	verifyImportCount(t, imported, maxQueueDist)
   730  
   731  	// Feed the remaining valid hashes to ensure DOS protection state remains clean
   732  	for i := len(hashes) - maxQueueDist - 2; i >= 0; i-- {
   733  		tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), validHeaderFetcher, validBodyFetcher)
   734  		verifyImportEvent(t, imported, true)
   735  	}
   736  	verifyImportDone(t, imported)
   737  }
   738  
   739  // Tests that blocks sent to the fetcher (either through propagation or via hash
   740  // announces and retrievals) don't pile up indefinitely, exhausting available
   741  // system memory.
   742  func TestBlockMemoryExhaustionAttack(t *testing.T) {
   743  	// Create a tester with instrumented import hooks
   744  	tester := newTester()
   745  
   746  	imported, enqueued := make(chan *types.Block), int32(0)
   747  	tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
   748  	tester.fetcher.queueChangeHook = func(hash common.Hash, added bool) {
   749  		if added {
   750  			atomic.AddInt32(&enqueued, 1)
   751  		} else {
   752  			atomic.AddInt32(&enqueued, -1)
   753  		}
   754  	}
   755  	// Create a valid chain and a batch of dangling (but in range) blocks
   756  	targetBlocks := hashLimit + 2*maxQueueDist
   757  	hashes, blocks := makeChain(targetBlocks, 0, genesis)
   758  	attack := make(map[common.Hash]*types.Block)
   759  	for i := byte(0); len(attack) < blockLimit+2*maxQueueDist; i++ {
   760  		hashes, blocks := makeChain(maxQueueDist-1, i, unknownBlock)
   761  		for _, hash := range hashes[:maxQueueDist-2] {
   762  			attack[hash] = blocks[hash]
   763  		}
   764  	}
   765  	// Try to feed all the attacker blocks make sure only a limited batch is accepted
   766  	for _, block := range attack {
   767  		tester.fetcher.Enqueue("attacker", block)
   768  	}
   769  	time.Sleep(200 * time.Millisecond)
   770  	if queued := atomic.LoadInt32(&enqueued); queued != blockLimit {
   771  		t.Fatalf("queued block count mismatch: have %d, want %d", queued, blockLimit)
   772  	}
   773  	// Queue up a batch of valid blocks, and check that a new peer is allowed to do so
   774  	for i := 0; i < maxQueueDist-1; i++ {
   775  		tester.fetcher.Enqueue("valid", blocks[hashes[len(hashes)-3-i]])
   776  	}
   777  	time.Sleep(100 * time.Millisecond)
   778  	if queued := atomic.LoadInt32(&enqueued); queued != blockLimit+maxQueueDist-1 {
   779  		t.Fatalf("queued block count mismatch: have %d, want %d", queued, blockLimit+maxQueueDist-1)
   780  	}
   781  	// Insert the missing piece (and sanity check the import)
   782  	tester.fetcher.Enqueue("valid", blocks[hashes[len(hashes)-2]])
   783  	verifyImportCount(t, imported, maxQueueDist)
   784  
   785  	// Insert the remaining blocks in chunks to ensure clean DOS protection
   786  	for i := maxQueueDist; i < len(hashes)-1; i++ {
   787  		tester.fetcher.Enqueue("valid", blocks[hashes[len(hashes)-2-i]])
   788  		verifyImportEvent(t, imported, true)
   789  	}
   790  	verifyImportDone(t, imported)
   791  }