github.com/ethereum/go-ethereum@v1.10.9/les/fetcher_test.go (about) 1 // Copyright 2020 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 "math/big" 21 "testing" 22 "time" 23 24 "github.com/ethereum/go-ethereum/consensus/ethash" 25 "github.com/ethereum/go-ethereum/core" 26 "github.com/ethereum/go-ethereum/core/rawdb" 27 "github.com/ethereum/go-ethereum/core/types" 28 "github.com/ethereum/go-ethereum/p2p/enode" 29 ) 30 31 // verifyImportEvent verifies that one single event arrive on an import channel. 32 func verifyImportEvent(t *testing.T, imported chan interface{}, arrive bool) { 33 if arrive { 34 select { 35 case <-imported: 36 case <-time.After(time.Second): 37 t.Fatalf("import timeout") 38 } 39 } else { 40 select { 41 case <-imported: 42 t.Fatalf("import invoked") 43 case <-time.After(20 * time.Millisecond): 44 } 45 } 46 } 47 48 // verifyImportDone verifies that no more events are arriving on an import channel. 49 func verifyImportDone(t *testing.T, imported chan interface{}) { 50 select { 51 case <-imported: 52 t.Fatalf("extra block imported") 53 case <-time.After(50 * time.Millisecond): 54 } 55 } 56 57 // verifyChainHeight verifies the chain height is as expected. 58 func verifyChainHeight(t *testing.T, fetcher *lightFetcher, height uint64) { 59 local := fetcher.chain.CurrentHeader().Number.Uint64() 60 if local != height { 61 t.Fatalf("chain height mismatch, got %d, want %d", local, height) 62 } 63 } 64 65 func TestSequentialAnnouncementsLes2(t *testing.T) { testSequentialAnnouncements(t, 2) } 66 func TestSequentialAnnouncementsLes3(t *testing.T) { testSequentialAnnouncements(t, 3) } 67 68 func testSequentialAnnouncements(t *testing.T, protocol int) { 69 netconfig := testnetConfig{ 70 blocks: 4, 71 protocol: protocol, 72 nopruning: true, 73 } 74 s, c, teardown := newClientServerEnv(t, netconfig) 75 defer teardown() 76 77 // Create connected peer pair, the initial signal from LES server 78 // is discarded to prevent syncing. 79 p1, _, err := newTestPeerPair("peer", protocol, s.handler, c.handler, true) 80 if err != nil { 81 t.Fatalf("Failed to create peer pair %v", err) 82 } 83 importCh := make(chan interface{}) 84 c.handler.fetcher.newHeadHook = func(header *types.Header) { 85 importCh <- header 86 } 87 for i := uint64(1); i <= s.backend.Blockchain().CurrentHeader().Number.Uint64(); i++ { 88 header := s.backend.Blockchain().GetHeaderByNumber(i) 89 hash, number := header.Hash(), header.Number.Uint64() 90 td := rawdb.ReadTd(s.db, hash, number) 91 92 announce := announceData{hash, number, td, 0, nil} 93 if p1.cpeer.announceType == announceTypeSigned { 94 announce.sign(s.handler.server.privateKey) 95 } 96 p1.cpeer.sendAnnounce(announce) 97 verifyImportEvent(t, importCh, true) 98 } 99 verifyImportDone(t, importCh) 100 verifyChainHeight(t, c.handler.fetcher, 4) 101 } 102 103 func TestGappedAnnouncementsLes2(t *testing.T) { testGappedAnnouncements(t, 2) } 104 func TestGappedAnnouncementsLes3(t *testing.T) { testGappedAnnouncements(t, 3) } 105 106 func testGappedAnnouncements(t *testing.T, protocol int) { 107 netconfig := testnetConfig{ 108 blocks: 4, 109 protocol: protocol, 110 nopruning: true, 111 } 112 s, c, teardown := newClientServerEnv(t, netconfig) 113 defer teardown() 114 115 // Create connected peer pair, the initial signal from LES server 116 // is discarded to prevent syncing. 117 peer, _, err := newTestPeerPair("peer", protocol, s.handler, c.handler, true) 118 if err != nil { 119 t.Fatalf("Failed to create peer pair %v", err) 120 } 121 done := make(chan *types.Header, 1) 122 c.handler.fetcher.newHeadHook = func(header *types.Header) { done <- header } 123 124 // Prepare announcement by latest header. 125 latest := s.backend.Blockchain().CurrentHeader() 126 hash, number := latest.Hash(), latest.Number.Uint64() 127 td := rawdb.ReadTd(s.db, hash, number) 128 129 // Sign the announcement if necessary. 130 announce := announceData{hash, number, td, 0, nil} 131 if peer.cpeer.announceType == announceTypeSigned { 132 announce.sign(s.handler.server.privateKey) 133 } 134 peer.cpeer.sendAnnounce(announce) 135 136 <-done // Wait syncing 137 verifyChainHeight(t, c.handler.fetcher, 4) 138 139 // Send a reorged announcement 140 blocks, _ := core.GenerateChain(rawdb.ReadChainConfig(s.db, s.backend.Blockchain().Genesis().Hash()), s.backend.Blockchain().GetBlockByNumber(3), 141 ethash.NewFaker(), s.db, 2, func(i int, gen *core.BlockGen) { 142 gen.OffsetTime(-9) // higher block difficulty 143 }) 144 s.backend.Blockchain().InsertChain(blocks) 145 146 <-done // Wait syncing 147 verifyChainHeight(t, c.handler.fetcher, 5) 148 } 149 150 func TestTrustedAnnouncementsLes2(t *testing.T) { testTrustedAnnouncement(t, 2) } 151 func TestTrustedAnnouncementsLes3(t *testing.T) { testTrustedAnnouncement(t, 3) } 152 153 func testTrustedAnnouncement(t *testing.T, protocol int) { 154 var ( 155 servers []*testServer 156 teardowns []func() 157 nodes []*enode.Node 158 ids []string 159 cpeers []*clientPeer 160 speers []*serverPeer 161 ) 162 for i := 0; i < 10; i++ { 163 s, n, teardown := newTestServerPeer(t, 10, protocol) 164 165 servers = append(servers, s) 166 nodes = append(nodes, n) 167 teardowns = append(teardowns, teardown) 168 169 // A half of them are trusted servers. 170 if i < 5 { 171 ids = append(ids, n.String()) 172 } 173 } 174 netconfig := testnetConfig{ 175 protocol: protocol, 176 nopruning: true, 177 ulcServers: ids, 178 ulcFraction: 60, 179 } 180 _, c, teardown := newClientServerEnv(t, netconfig) 181 defer teardown() 182 defer func() { 183 for i := 0; i < len(teardowns); i++ { 184 teardowns[i]() 185 } 186 }() 187 // Connect all server instances. 188 for i := 0; i < len(servers); i++ { 189 sp, cp, err := connect(servers[i].handler, nodes[i].ID(), c.handler, protocol, true) 190 if err != nil { 191 t.Fatalf("connect server and client failed, err %s", err) 192 } 193 cpeers = append(cpeers, cp) 194 speers = append(speers, sp) 195 } 196 newHead := make(chan *types.Header, 1) 197 c.handler.fetcher.newHeadHook = func(header *types.Header) { newHead <- header } 198 199 check := func(height []uint64, expected uint64, callback func()) { 200 for i := 0; i < len(height); i++ { 201 for j := 0; j < len(servers); j++ { 202 h := servers[j].backend.Blockchain().GetHeaderByNumber(height[i]) 203 hash, number := h.Hash(), h.Number.Uint64() 204 td := rawdb.ReadTd(servers[j].db, hash, number) 205 206 // Sign the announcement if necessary. 207 announce := announceData{hash, number, td, 0, nil} 208 p := cpeers[j] 209 if p.announceType == announceTypeSigned { 210 announce.sign(servers[j].handler.server.privateKey) 211 } 212 p.sendAnnounce(announce) 213 } 214 } 215 if callback != nil { 216 callback() 217 } 218 verifyChainHeight(t, c.handler.fetcher, expected) 219 } 220 check([]uint64{1}, 1, func() { <-newHead }) // Sequential announcements 221 check([]uint64{4}, 4, func() { <-newHead }) // ULC-style light syncing, rollback untrusted headers 222 check([]uint64{10}, 10, func() { <-newHead }) // Sync the whole chain. 223 } 224 225 func TestInvalidAnnouncesLES2(t *testing.T) { testInvalidAnnounces(t, lpv2) } 226 func TestInvalidAnnouncesLES3(t *testing.T) { testInvalidAnnounces(t, lpv3) } 227 func TestInvalidAnnouncesLES4(t *testing.T) { testInvalidAnnounces(t, lpv4) } 228 229 func testInvalidAnnounces(t *testing.T, protocol int) { 230 netconfig := testnetConfig{ 231 blocks: 4, 232 protocol: protocol, 233 nopruning: true, 234 } 235 s, c, teardown := newClientServerEnv(t, netconfig) 236 defer teardown() 237 238 // Create connected peer pair, the initial signal from LES server 239 // is discarded to prevent syncing. 240 peer, _, err := newTestPeerPair("peer", lpv3, s.handler, c.handler, true) 241 if err != nil { 242 t.Fatalf("Failed to create peer pair %v", err) 243 } 244 done := make(chan *types.Header, 1) 245 c.handler.fetcher.newHeadHook = func(header *types.Header) { done <- header } 246 247 // Prepare announcement by latest header. 248 headerOne := s.backend.Blockchain().GetHeaderByNumber(1) 249 hash, number := headerOne.Hash(), headerOne.Number.Uint64() 250 td := big.NewInt(200) // bad td 251 252 // Sign the announcement if necessary. 253 announce := announceData{hash, number, td, 0, nil} 254 if peer.cpeer.announceType == announceTypeSigned { 255 announce.sign(s.handler.server.privateKey) 256 } 257 peer.cpeer.sendAnnounce(announce) 258 <-done // Wait syncing 259 260 // Ensure the bad peer is evicited 261 if c.handler.backend.peers.len() != 0 { 262 t.Fatalf("Failed to evict invalid peer") 263 } 264 }