gitee.com/liu-zhao234568/cntest@v1.0.0/les/ulc_test.go (about) 1 // Copyright 2018 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 "fmt" 22 "net" 23 "testing" 24 "time" 25 26 "gitee.com/liu-zhao234568/cntest/crypto" 27 "gitee.com/liu-zhao234568/cntest/p2p" 28 "gitee.com/liu-zhao234568/cntest/p2p/enode" 29 ) 30 31 func TestULCAnnounceThresholdLes2(t *testing.T) { testULCAnnounceThreshold(t, 2) } 32 func TestULCAnnounceThresholdLes3(t *testing.T) { testULCAnnounceThreshold(t, 3) } 33 34 func testULCAnnounceThreshold(t *testing.T, protocol int) { 35 // todo figure out why it takes fetcher so longer to fetcher the announced header. 36 t.Skip("Sometimes it can failed") 37 var cases = []struct { 38 height []int 39 threshold int 40 expect uint64 41 }{ 42 {[]int{1}, 100, 1}, 43 {[]int{0, 0, 0}, 100, 0}, 44 {[]int{1, 2, 3}, 30, 3}, 45 {[]int{1, 2, 3}, 60, 2}, 46 {[]int{3, 2, 1}, 67, 1}, 47 {[]int{3, 2, 1}, 100, 1}, 48 } 49 for _, testcase := range cases { 50 var ( 51 servers []*testServer 52 teardowns []func() 53 nodes []*enode.Node 54 ids []string 55 ) 56 for i := 0; i < len(testcase.height); i++ { 57 s, n, teardown := newTestServerPeer(t, 0, protocol) 58 59 servers = append(servers, s) 60 nodes = append(nodes, n) 61 teardowns = append(teardowns, teardown) 62 ids = append(ids, n.String()) 63 } 64 c, teardown := newTestLightPeer(t, protocol, ids, testcase.threshold) 65 66 // Connect all servers. 67 for i := 0; i < len(servers); i++ { 68 connect(servers[i].handler, nodes[i].ID(), c.handler, protocol) 69 } 70 for i := 0; i < len(servers); i++ { 71 for j := 0; j < testcase.height[i]; j++ { 72 servers[i].backend.Commit() 73 } 74 } 75 time.Sleep(1500 * time.Millisecond) // Ensure the fetcher has done its work. 76 head := c.handler.backend.blockchain.CurrentHeader().Number.Uint64() 77 if head != testcase.expect { 78 t.Fatalf("chain height mismatch, want %d, got %d", testcase.expect, head) 79 } 80 81 // Release all servers and client resources. 82 teardown() 83 for i := 0; i < len(teardowns); i++ { 84 teardowns[i]() 85 } 86 } 87 } 88 89 func connect(server *serverHandler, serverId enode.ID, client *clientHandler, protocol int) (*serverPeer, *clientPeer, error) { 90 // Create a message pipe to communicate through 91 app, net := p2p.MsgPipe() 92 93 var id enode.ID 94 rand.Read(id[:]) 95 96 peer1 := newServerPeer(protocol, NetworkId, true, p2p.NewPeer(serverId, "", nil), net) // Mark server as trusted 97 peer2 := newClientPeer(protocol, NetworkId, p2p.NewPeer(id, "", nil), app) 98 99 // Start the peerLight on a new thread 100 errc1 := make(chan error, 1) 101 errc2 := make(chan error, 1) 102 go func() { 103 select { 104 case <-server.closeCh: 105 errc1 <- p2p.DiscQuitting 106 case errc1 <- server.handle(peer2): 107 } 108 }() 109 go func() { 110 select { 111 case <-client.closeCh: 112 errc1 <- p2p.DiscQuitting 113 case errc1 <- client.handle(peer1): 114 } 115 }() 116 117 select { 118 case <-time.After(time.Millisecond * 100): 119 case err := <-errc1: 120 return nil, nil, fmt.Errorf("peerLight handshake error: %v", err) 121 case err := <-errc2: 122 return nil, nil, fmt.Errorf("peerFull handshake error: %v", err) 123 } 124 return peer1, peer2, nil 125 } 126 127 // newTestServerPeer creates server peer. 128 func newTestServerPeer(t *testing.T, blocks int, protocol int) (*testServer, *enode.Node, func()) { 129 netconfig := testnetConfig{ 130 blocks: blocks, 131 protocol: protocol, 132 nopruning: true, 133 } 134 s, _, teardown := newClientServerEnv(t, netconfig) 135 key, err := crypto.GenerateKey() 136 if err != nil { 137 t.Fatal("generate key err:", err) 138 } 139 s.handler.server.privateKey = key 140 n := enode.NewV4(&key.PublicKey, net.ParseIP("127.0.0.1"), 35000, 35000) 141 return s, n, teardown 142 } 143 144 // newTestLightPeer creates node with light sync mode 145 func newTestLightPeer(t *testing.T, protocol int, ulcServers []string, ulcFraction int) (*testClient, func()) { 146 netconfig := testnetConfig{ 147 protocol: protocol, 148 ulcServers: ulcServers, 149 ulcFraction: ulcFraction, 150 nopruning: true, 151 } 152 _, c, teardown := newClientServerEnv(t, netconfig) 153 return c, teardown 154 }