github.com/theQRL/go-zond@v0.1.1/les/peer_test.go (about) 1 // Copyright 2019 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 les 18 19 import ( 20 "crypto/rand" 21 "errors" 22 "math/big" 23 "reflect" 24 "sort" 25 "testing" 26 "time" 27 28 "github.com/theQRL/go-zond/common" 29 "github.com/theQRL/go-zond/core" 30 "github.com/theQRL/go-zond/core/forkid" 31 "github.com/theQRL/go-zond/core/types" 32 "github.com/theQRL/go-zond/p2p" 33 "github.com/theQRL/go-zond/p2p/enode" 34 "github.com/theQRL/go-zond/params" 35 ) 36 37 type testServerPeerSub struct { 38 regCh chan *serverPeer 39 unregCh chan *serverPeer 40 } 41 42 func newTestServerPeerSub() *testServerPeerSub { 43 return &testServerPeerSub{ 44 regCh: make(chan *serverPeer, 1), 45 unregCh: make(chan *serverPeer, 1), 46 } 47 } 48 49 func (t *testServerPeerSub) registerPeer(p *serverPeer) { t.regCh <- p } 50 func (t *testServerPeerSub) unregisterPeer(p *serverPeer) { t.unregCh <- p } 51 52 func TestPeerSubscription(t *testing.T) { 53 peers := newServerPeerSet() 54 defer peers.close() 55 56 checkIds := func(expect []string) { 57 given := peers.ids() 58 if len(given) == 0 && len(expect) == 0 { 59 return 60 } 61 sort.Strings(given) 62 sort.Strings(expect) 63 if !reflect.DeepEqual(given, expect) { 64 t.Fatalf("all peer ids mismatch, want %v, given %v", expect, given) 65 } 66 } 67 checkPeers := func(peerCh chan *serverPeer) { 68 select { 69 case <-peerCh: 70 case <-time.NewTimer(100 * time.Millisecond).C: 71 t.Fatalf("timeout, no event received") 72 } 73 select { 74 case <-peerCh: 75 t.Fatalf("unexpected event received") 76 case <-time.NewTimer(10 * time.Millisecond).C: 77 } 78 } 79 checkIds([]string{}) 80 81 sub := newTestServerPeerSub() 82 peers.subscribe(sub) 83 84 // Generate a random id and create the peer 85 var id enode.ID 86 rand.Read(id[:]) 87 peer := newServerPeer(2, NetworkId, false, p2p.NewPeer(id, "name", nil), nil) 88 peers.register(peer) 89 90 checkIds([]string{peer.id}) 91 checkPeers(sub.regCh) 92 93 peers.unregister(peer.id) 94 checkIds([]string{}) 95 checkPeers(sub.unregCh) 96 } 97 98 type fakeChain struct{} 99 100 func (f *fakeChain) Config() *params.ChainConfig { return params.MainnetChainConfig } 101 func (f *fakeChain) Genesis() *types.Block { 102 return core.DefaultGenesisBlock().ToBlock() 103 } 104 func (f *fakeChain) CurrentHeader() *types.Header { return &types.Header{Number: big.NewInt(10000000)} } 105 106 func TestHandshake(t *testing.T) { 107 // Create a message pipe to communicate through 108 app, net := p2p.MsgPipe() 109 110 // Generate a random id and create the peer 111 var id enode.ID 112 rand.Read(id[:]) 113 114 peer1 := newClientPeer(2, NetworkId, p2p.NewPeer(id, "name", nil), net) 115 peer2 := newServerPeer(2, NetworkId, true, p2p.NewPeer(id, "name", nil), app) 116 117 var ( 118 errCh1 = make(chan error, 1) 119 errCh2 = make(chan error, 1) 120 121 td = big.NewInt(100) 122 head = common.HexToHash("deadbeef") 123 headNum = uint64(10) 124 genesis = common.HexToHash("cafebabe") 125 126 chain1, chain2 = &fakeChain{}, &fakeChain{} 127 forkID1 = forkid.NewID(chain1.Config(), chain1.Genesis(), chain1.CurrentHeader().Number.Uint64(), chain1.CurrentHeader().Time) 128 forkID2 = forkid.NewID(chain2.Config(), chain2.Genesis(), chain2.CurrentHeader().Number.Uint64(), chain2.CurrentHeader().Time) 129 filter1, filter2 = forkid.NewFilter(chain1), forkid.NewFilter(chain2) 130 ) 131 132 go func() { 133 errCh1 <- peer1.handshake(td, head, headNum, genesis, forkID1, filter1, func(list *keyValueList) { 134 var announceType uint64 = announceTypeSigned 135 *list = (*list).add("announceType", announceType) 136 }, nil) 137 }() 138 go func() { 139 errCh2 <- peer2.handshake(td, head, headNum, genesis, forkID2, filter2, nil, func(recv keyValueMap) error { 140 var reqType uint64 141 err := recv.get("announceType", &reqType) 142 if err != nil { 143 return err 144 } 145 if reqType != announceTypeSigned { 146 return errors.New("Expected announceTypeSigned") 147 } 148 return nil 149 }) 150 }() 151 152 for i := 0; i < 2; i++ { 153 select { 154 case err := <-errCh1: 155 if err != nil { 156 t.Fatalf("handshake failed, %v", err) 157 } 158 case err := <-errCh2: 159 if err != nil { 160 t.Fatalf("handshake failed, %v", err) 161 } 162 case <-time.After(time.Second): 163 t.Fatalf("timeout") 164 } 165 } 166 }