github.com/theQRL/go-zond@v0.1.1/zond/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 zond 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/theQRL/go-zond/p2p" 24 "github.com/theQRL/go-zond/p2p/enode" 25 "github.com/theQRL/go-zond/zond/downloader" 26 "github.com/theQRL/go-zond/zond/protocols/snap" 27 "github.com/theQRL/go-zond/zond/protocols/zond" 28 ) 29 30 // Tests that snap sync is disabled after a successful sync cycle. 31 func TestSnapSyncDisabling66(t *testing.T) { testSnapSyncDisabling(t, zond.ETH66, snap.SNAP1) } 32 func TestSnapSyncDisabling67(t *testing.T) { testSnapSyncDisabling(t, zond.ETH67, 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 !empty.handler.snapSync.Load() { 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 full.handler.snapSync.Load() { 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 `zond` and `snap` 54 caps := []p2p.Cap{{Name: "zond", Version: ethVer}, {Name: "snap", Version: snapVer}} 55 56 emptyPipeEth, fullPipeEth := p2p.MsgPipe() 57 defer emptyPipeEth.Close() 58 defer fullPipeEth.Close() 59 60 emptyPeerEth := zond.NewPeer(ethVer, p2p.NewPeer(enode.ID{1}, "", caps), emptyPipeEth, empty.txpool) 61 fullPeerEth := zond.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 *zond.Peer) error { 66 return zond.Handle((*ethHandler)(empty.handler), peer) 67 }) 68 go full.handler.runEthPeer(fullPeerEth, func(peer *zond.Peer) error { 69 return zond.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 empty.handler.snapSync.Load() { 94 t.Fatalf("snap sync not disabled after successful synchronisation") 95 } 96 }