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