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