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