github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/eth/sync_test.go (about)

     1  // Copyright 2015 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum 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/SmartMeshFoundation/Spectrum/eth/downloader"
    25  	"github.com/SmartMeshFoundation/Spectrum/p2p"
    26  	"github.com/SmartMeshFoundation/Spectrum/p2p/discover"
    27  )
    28  
    29  // Tests that fast sync gets disabled as soon as a real block is successfully
    30  // imported into the blockchain.
    31  func TestFastSyncDisabling(t *testing.T) {
    32  	// Create a pristine protocol manager, check that fast sync is left enabled
    33  	pmEmpty := newTestProtocolManagerMust(t, downloader.FastSync, 0, nil, nil)
    34  	if atomic.LoadUint32(&pmEmpty.fastSync) == 0 {
    35  		t.Fatalf("fast sync disabled on pristine blockchain")
    36  	}
    37  	// Create a full protocol manager, check that fast sync gets disabled
    38  	pmFull := newTestProtocolManagerMust(t, downloader.FastSync, 1024, nil, nil)
    39  	if atomic.LoadUint32(&pmFull.fastSync) == 1 {
    40  		t.Fatalf("fast sync not disabled on non-empty blockchain")
    41  	}
    42  	// Sync up the two peers
    43  	io1, io2 := p2p.MsgPipe()
    44  
    45  	go pmFull.handle(pmFull.newPeer(63, p2p.NewPeer(discover.NodeID{}, "empty", nil), io2))
    46  	go pmEmpty.handle(pmEmpty.newPeer(63, p2p.NewPeer(discover.NodeID{}, "full", nil), io1))
    47  
    48  	time.Sleep(250 * time.Millisecond)
    49  	pmEmpty.synchronise(pmEmpty.peers.BestPeer())
    50  
    51  	// Check that fast sync was disabled
    52  	if atomic.LoadUint32(&pmEmpty.fastSync) == 1 {
    53  		t.Fatalf("fast sync not disabled after successful synchronisation")
    54  	}
    55  }