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