github.com/phillinzzz/newBsc@v1.1.6/eth/sync_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 eth
    18  
    19  import (
    20  	"sync/atomic"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/phillinzzz/newBsc/eth/downloader"
    25  	"github.com/phillinzzz/newBsc/eth/protocols/eth"
    26  	"github.com/phillinzzz/newBsc/p2p"
    27  	"github.com/phillinzzz/newBsc/p2p/enode"
    28  )
    29  
    30  // Tests that fast sync is disabled after a successful sync cycle.
    31  func TestFastSyncDisabling65(t *testing.T) { testFastSyncDisabling(t, eth.ETH65) }
    32  func TestFastSyncDisabling66(t *testing.T) { testFastSyncDisabling(t, eth.ETH66) }
    33  
    34  // Tests that fast sync gets disabled as soon as a real block is successfully
    35  // imported into the blockchain.
    36  func testFastSyncDisabling(t *testing.T, protocol uint) {
    37  	t.Parallel()
    38  
    39  	// Create an empty handler and ensure it's in fast sync mode
    40  	empty := newTestHandler()
    41  	if atomic.LoadUint32(&empty.handler.fastSync) == 0 {
    42  		t.Fatalf("fast sync disabled on pristine blockchain")
    43  	}
    44  	defer empty.close()
    45  
    46  	// Create a full handler and ensure fast sync ends up disabled
    47  	full := newTestHandlerWithBlocks(1024)
    48  	if atomic.LoadUint32(&full.handler.fastSync) == 1 {
    49  		t.Fatalf("fast sync not disabled on non-empty blockchain")
    50  	}
    51  	defer full.close()
    52  
    53  	// Sync up the two handlers
    54  	emptyPipe, fullPipe := p2p.MsgPipe()
    55  	defer emptyPipe.Close()
    56  	defer fullPipe.Close()
    57  
    58  	emptyPeer := eth.NewPeer(protocol, p2p.NewPeer(enode.ID{1}, "", nil), emptyPipe, empty.txpool)
    59  	fullPeer := eth.NewPeer(protocol, p2p.NewPeer(enode.ID{2}, "", nil), fullPipe, full.txpool)
    60  	defer emptyPeer.Close()
    61  	defer fullPeer.Close()
    62  
    63  	go empty.handler.runEthPeer(emptyPeer, func(peer *eth.Peer) error {
    64  		return eth.Handle((*ethHandler)(empty.handler), peer)
    65  	})
    66  	go full.handler.runEthPeer(fullPeer, func(peer *eth.Peer) error {
    67  		return eth.Handle((*ethHandler)(full.handler), peer)
    68  	})
    69  	// Wait a bit for the above handlers to start
    70  	time.Sleep(250 * time.Millisecond)
    71  
    72  	// Check that fast sync was disabled
    73  	op := peerToSyncOp(downloader.FastSync, empty.handler.peers.peerWithHighestTD())
    74  	if err := empty.handler.doSync(op); err != nil {
    75  		t.Fatal("sync failed:", err)
    76  	}
    77  	if atomic.LoadUint32(&empty.handler.fastSync) == 1 {
    78  		t.Fatalf("fast sync not disabled after successful synchronisation")
    79  	}
    80  }