github.com/EgonCoin/EgonChain@v1.10.16/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/EgonCoin/EgonChain/eth/downloader"
    25  	"github.com/EgonCoin/EgonChain/eth/protocols/eth"
    26  	"github.com/EgonCoin/EgonChain/eth/protocols/snap"
    27  	"github.com/EgonCoin/EgonChain/p2p"
    28  	"github.com/EgonCoin/EgonChain/p2p/enode"
    29  )
    30  
    31  // Tests that snap sync is disabled after a successful sync cycle.
    32  func TestSnapSyncDisabling66(t *testing.T) { testSnapSyncDisabling(t, eth.ETH66, snap.SNAP1) }
    33  
    34  // Tests that snap sync gets disabled as soon as a real block is successfully
    35  // imported into the blockchain.
    36  func testSnapSyncDisabling(t *testing.T, ethVer uint, snapVer uint) {
    37  	t.Parallel()
    38  
    39  	// Create an empty handler and ensure it's in snap sync mode
    40  	empty := newTestHandler()
    41  	if atomic.LoadUint32(&empty.handler.snapSync) == 0 {
    42  		t.Fatalf("snap sync disabled on pristine blockchain")
    43  	}
    44  	defer empty.close()
    45  
    46  	// Create a full handler and ensure snap sync ends up disabled
    47  	full := newTestHandlerWithBlocks(1024)
    48  	if atomic.LoadUint32(&full.handler.snapSync) == 1 {
    49  		t.Fatalf("snap sync not disabled on non-empty blockchain")
    50  	}
    51  	defer full.close()
    52  
    53  	// Sync up the two handlers via both `eth` and `snap`
    54  	caps := []p2p.Cap{{Name: "eth", Version: ethVer}, {Name: "snap", Version: snapVer}}
    55  
    56  	emptyPipeEth, fullPipeEth := p2p.MsgPipe()
    57  	defer emptyPipeEth.Close()
    58  	defer fullPipeEth.Close()
    59  
    60  	emptyPeerEth := eth.NewPeer(ethVer, p2p.NewPeer(enode.ID{1}, "", caps), emptyPipeEth, empty.txpool)
    61  	fullPeerEth := eth.NewPeer(ethVer, p2p.NewPeer(enode.ID{2}, "", caps), fullPipeEth, full.txpool)
    62  	defer emptyPeerEth.Close()
    63  	defer fullPeerEth.Close()
    64  
    65  	go empty.handler.runEthPeer(emptyPeerEth, func(peer *eth.Peer) error {
    66  		return eth.Handle((*ethHandler)(empty.handler), peer)
    67  	})
    68  	go full.handler.runEthPeer(fullPeerEth, func(peer *eth.Peer) error {
    69  		return eth.Handle((*ethHandler)(full.handler), peer)
    70  	})
    71  
    72  	emptyPipeSnap, fullPipeSnap := p2p.MsgPipe()
    73  	defer emptyPipeSnap.Close()
    74  	defer fullPipeSnap.Close()
    75  
    76  	emptyPeerSnap := snap.NewPeer(snapVer, p2p.NewPeer(enode.ID{1}, "", caps), emptyPipeSnap)
    77  	fullPeerSnap := snap.NewPeer(snapVer, p2p.NewPeer(enode.ID{2}, "", caps), fullPipeSnap)
    78  
    79  	go empty.handler.runSnapExtension(emptyPeerSnap, func(peer *snap.Peer) error {
    80  		return snap.Handle((*snapHandler)(empty.handler), peer)
    81  	})
    82  	go full.handler.runSnapExtension(fullPeerSnap, func(peer *snap.Peer) error {
    83  		return snap.Handle((*snapHandler)(full.handler), peer)
    84  	})
    85  	// Wait a bit for the above handlers to start
    86  	time.Sleep(250 * time.Millisecond)
    87  
    88  	// Check that snap sync was disabled
    89  	op := peerToSyncOp(downloader.SnapSync, empty.handler.peers.peerWithHighestTD())
    90  	if err := empty.handler.doSync(op); err != nil {
    91  		t.Fatal("sync failed:", err)
    92  	}
    93  	if atomic.LoadUint32(&empty.handler.snapSync) == 1 {
    94  		t.Fatalf("snap sync not disabled after successful synchronisation")
    95  	}
    96  }