github.com/theQRL/go-zond@v0.1.1/p2p/dnsdisc/client_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 dnsdisc 18 19 import ( 20 "context" 21 "crypto/ecdsa" 22 "errors" 23 "reflect" 24 "testing" 25 "time" 26 27 "github.com/davecgh/go-spew/spew" 28 "github.com/theQRL/go-zond/common/hexutil" 29 "github.com/theQRL/go-zond/common/mclock" 30 "github.com/theQRL/go-zond/crypto" 31 "github.com/theQRL/go-zond/internal/testlog" 32 "github.com/theQRL/go-zond/log" 33 "github.com/theQRL/go-zond/p2p/enode" 34 "github.com/theQRL/go-zond/p2p/enr" 35 ) 36 37 var signingKeyForTesting, _ = crypto.ToECDSA(hexutil.MustDecode("0xdc599867fc513f8f5e2c2c9c489cde5e71362d1d9ec6e693e0de063236ed1240")) 38 39 func TestClientSyncTree(t *testing.T) { 40 nodes := []string{ 41 "enr:-HW4QOFzoVLaFJnNhbgMoDXPnOvcdVuj7pDpqRvh6BRDO68aVi5ZcjB3vzQRZH2IcLBGHzo8uUN3snqmgTiE56CH3AMBgmlkgnY0iXNlY3AyNTZrMaECC2_24YYkYHEgdzxlSNKQEnHhuNAbNlMlWJxrJxbAFvA", 42 "enr:-HW4QAggRauloj2SDLtIHN1XBkvhFZ1vtf1raYQp9TBW2RD5EEawDzbtSmlXUfnaHcvwOizhVYLtr7e6vw7NAf6mTuoCgmlkgnY0iXNlY3AyNTZrMaECjrXI8TLNXU0f8cthpAMxEshUyQlK-AM0PW2wfrnacNI", 43 "enr:-HW4QLAYqmrwllBEnzWWs7I5Ev2IAs7x_dZlbYdRdMUx5EyKHDXp7AV5CkuPGUPdvbv1_Ms1CPfhcGCvSElSosZmyoqAgmlkgnY0iXNlY3AyNTZrMaECriawHKWdDRk2xeZkrOXBQ0dfMFLHY4eENZwdufn1S1o", 44 } 45 46 r := mapResolver{ 47 "n": "enrtree-root:v1 e=JWXYDBPXYWG6FX3GMDIBFA6CJ4 l=C7HRFPF3BLGF3YR4DY5KX3SMBE seq=1 sig=o908WmNp7LibOfPsr4btQwatZJ5URBr2ZAuxvK4UWHlsB9sUOTJQaGAlLPVAhM__XJesCHxLISo94z5Z2a463gA", 48 "C7HRFPF3BLGF3YR4DY5KX3SMBE.n": "enrtree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org", 49 "JWXYDBPXYWG6FX3GMDIBFA6CJ4.n": "enrtree-branch:2XS2367YHAXJFGLZHVAWLQD4ZY,H4FHT4B454P6UXFD7JCYQ5PWDY,MHTDO6TMUBRIA2XWG5LUDACK24", 50 "2XS2367YHAXJFGLZHVAWLQD4ZY.n": nodes[0], 51 "H4FHT4B454P6UXFD7JCYQ5PWDY.n": nodes[1], 52 "MHTDO6TMUBRIA2XWG5LUDACK24.n": nodes[2], 53 } 54 var ( 55 wantNodes = sortByID(parseNodes(nodes)) 56 wantLinks = []string{"enrtree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org"} 57 wantSeq = uint(1) 58 ) 59 60 c := NewClient(Config{Resolver: r, Logger: testlog.Logger(t, log.LvlTrace)}) 61 stree, err := c.SyncTree("enrtree://AKPYQIUQIL7PSIACI32J7FGZW56E5FKHEFCCOFHILBIMW3M6LWXS2@n") 62 if err != nil { 63 t.Fatal("sync error:", err) 64 } 65 if !reflect.DeepEqual(sortByID(stree.Nodes()), wantNodes) { 66 t.Errorf("wrong nodes in synced tree:\nhave %v\nwant %v", spew.Sdump(stree.Nodes()), spew.Sdump(wantNodes)) 67 } 68 if !reflect.DeepEqual(stree.Links(), wantLinks) { 69 t.Errorf("wrong links in synced tree: %v", stree.Links()) 70 } 71 if stree.Seq() != wantSeq { 72 t.Errorf("synced tree has wrong seq: %d", stree.Seq()) 73 } 74 } 75 76 // In this test, syncing the tree fails because it contains an invalid ENR entry. 77 func TestClientSyncTreeBadNode(t *testing.T) { 78 // var b strings.Builder 79 // b.WriteString(enrPrefix) 80 // b.WriteString("-----") 81 // badHash := subdomain(&b) 82 // tree, _ := MakeTree(3, nil, []string{"enrtree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org"}) 83 // tree.entries[badHash] = &b 84 // tree.root.eroot = badHash 85 // url, _ := tree.Sign(signingKeyForTesting, "n") 86 // fmt.Println(url) 87 // fmt.Printf("%#v\n", tree.ToTXT("n")) 88 89 r := mapResolver{ 90 "n": "enrtree-root:v1 e=INDMVBZEEQ4ESVYAKGIYU74EAA l=C7HRFPF3BLGF3YR4DY5KX3SMBE seq=3 sig=Vl3AmunLur0JZ3sIyJPSH6A3Vvdp4F40jWQeCmkIhmcgwE4VC5U9wpK8C_uL_CMY29fd6FAhspRvq2z_VysTLAA", 91 "C7HRFPF3BLGF3YR4DY5KX3SMBE.n": "enrtree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org", 92 "INDMVBZEEQ4ESVYAKGIYU74EAA.n": "enr:-----", 93 } 94 c := NewClient(Config{Resolver: r, Logger: testlog.Logger(t, log.LvlTrace)}) 95 _, err := c.SyncTree("enrtree://AKPYQIUQIL7PSIACI32J7FGZW56E5FKHEFCCOFHILBIMW3M6LWXS2@n") 96 wantErr := nameError{name: "INDMVBZEEQ4ESVYAKGIYU74EAA.n", err: entryError{typ: "enr", err: errInvalidENR}} 97 if err != wantErr { 98 t.Fatalf("expected sync error %q, got %q", wantErr, err) 99 } 100 } 101 102 // This test checks that randomIterator finds all entries. 103 func TestIterator(t *testing.T) { 104 var ( 105 keys = testKeys(30) 106 nodes = testNodes(keys) 107 tree, url = makeTestTree("n", nodes, nil) 108 r = mapResolver(tree.ToTXT("n")) 109 ) 110 111 c := NewClient(Config{ 112 Resolver: r, 113 Logger: testlog.Logger(t, log.LvlTrace), 114 RateLimit: 500, 115 }) 116 it, err := c.NewIterator(url) 117 if err != nil { 118 t.Fatal(err) 119 } 120 121 checkIterator(t, it, nodes) 122 } 123 124 func TestIteratorCloseWithoutNext(t *testing.T) { 125 tree1, url1 := makeTestTree("t1", nil, nil) 126 c := NewClient(Config{Resolver: newMapResolver(tree1.ToTXT("t1"))}) 127 it, err := c.NewIterator(url1) 128 if err != nil { 129 t.Fatal(err) 130 } 131 132 it.Close() 133 ok := it.Next() 134 if ok { 135 t.Fatal("Next returned true after Close") 136 } 137 } 138 139 // This test checks if closing randomIterator races. 140 func TestIteratorClose(t *testing.T) { 141 var ( 142 keys = testKeys(500) 143 nodes = testNodes(keys) 144 tree1, url1 = makeTestTree("t1", nodes, nil) 145 ) 146 147 c := NewClient(Config{Resolver: newMapResolver(tree1.ToTXT("t1"))}) 148 it, err := c.NewIterator(url1) 149 if err != nil { 150 t.Fatal(err) 151 } 152 153 done := make(chan struct{}) 154 go func() { 155 for it.Next() { 156 _ = it.Node() 157 } 158 close(done) 159 }() 160 161 time.Sleep(50 * time.Millisecond) 162 it.Close() 163 <-done 164 } 165 166 // This test checks that randomIterator traverses linked trees as well as explicitly added trees. 167 func TestIteratorLinks(t *testing.T) { 168 var ( 169 keys = testKeys(40) 170 nodes = testNodes(keys) 171 tree1, url1 = makeTestTree("t1", nodes[:10], nil) 172 tree2, url2 = makeTestTree("t2", nodes[10:], []string{url1}) 173 ) 174 175 c := NewClient(Config{ 176 Resolver: newMapResolver(tree1.ToTXT("t1"), tree2.ToTXT("t2")), 177 Logger: testlog.Logger(t, log.LvlTrace), 178 RateLimit: 500, 179 }) 180 it, err := c.NewIterator(url2) 181 if err != nil { 182 t.Fatal(err) 183 } 184 185 checkIterator(t, it, nodes) 186 } 187 188 // This test verifies that randomIterator re-checks the root of the tree to catch 189 // updates to nodes. 190 func TestIteratorNodeUpdates(t *testing.T) { 191 var ( 192 clock = new(mclock.Simulated) 193 keys = testKeys(30) 194 nodes = testNodes(keys) 195 resolver = newMapResolver() 196 c = NewClient(Config{ 197 Resolver: resolver, 198 Logger: testlog.Logger(t, log.LvlTrace), 199 RecheckInterval: 20 * time.Minute, 200 RateLimit: 500, 201 }) 202 ) 203 c.clock = clock 204 tree1, url := makeTestTree("n", nodes[:25], nil) 205 it, err := c.NewIterator(url) 206 if err != nil { 207 t.Fatal(err) 208 } 209 210 // Sync the original tree. 211 resolver.add(tree1.ToTXT("n")) 212 checkIterator(t, it, nodes[:25]) 213 214 // Ensure RandomNode returns the new nodes after the tree is updated. 215 updateSomeNodes(keys, nodes) 216 tree2, _ := makeTestTree("n", nodes, nil) 217 resolver.clear() 218 resolver.add(tree2.ToTXT("n")) 219 t.Log("tree updated") 220 221 clock.Run(c.cfg.RecheckInterval + 1*time.Second) 222 checkIterator(t, it, nodes) 223 } 224 225 // This test checks that the tree root is rechecked when a couple of leaf 226 // requests have failed. The test is just like TestIteratorNodeUpdates, but 227 // without advancing the clock by recheckInterval after the tree update. 228 func TestIteratorRootRecheckOnFail(t *testing.T) { 229 var ( 230 clock = new(mclock.Simulated) 231 keys = testKeys(30) 232 nodes = testNodes(keys) 233 resolver = newMapResolver() 234 c = NewClient(Config{ 235 Resolver: resolver, 236 Logger: testlog.Logger(t, log.LvlTrace), 237 RecheckInterval: 20 * time.Minute, 238 RateLimit: 500, 239 // Disabling the cache is required for this test because the client doesn't 240 // notice leaf failures if all records are cached. 241 CacheLimit: 1, 242 }) 243 ) 244 c.clock = clock 245 tree1, url := makeTestTree("n", nodes[:25], nil) 246 it, err := c.NewIterator(url) 247 if err != nil { 248 t.Fatal(err) 249 } 250 251 // Sync the original tree. 252 resolver.add(tree1.ToTXT("n")) 253 checkIterator(t, it, nodes[:25]) 254 255 // Ensure RandomNode returns the new nodes after the tree is updated. 256 updateSomeNodes(keys, nodes) 257 tree2, _ := makeTestTree("n", nodes, nil) 258 resolver.clear() 259 resolver.add(tree2.ToTXT("n")) 260 t.Log("tree updated") 261 262 checkIterator(t, it, nodes) 263 } 264 265 // This test checks that the iterator works correctly when the tree is initially empty. 266 func TestIteratorEmptyTree(t *testing.T) { 267 var ( 268 clock = new(mclock.Simulated) 269 keys = testKeys(1) 270 nodes = testNodes(keys) 271 resolver = newMapResolver() 272 c = NewClient(Config{ 273 Resolver: resolver, 274 Logger: testlog.Logger(t, log.LvlTrace), 275 RecheckInterval: 20 * time.Minute, 276 RateLimit: 500, 277 }) 278 ) 279 c.clock = clock 280 tree1, url := makeTestTree("n", nil, nil) 281 tree2, _ := makeTestTree("n", nodes, nil) 282 resolver.add(tree1.ToTXT("n")) 283 284 // Start the iterator. 285 node := make(chan *enode.Node, 1) 286 it, err := c.NewIterator(url) 287 if err != nil { 288 t.Fatal(err) 289 } 290 go func() { 291 it.Next() 292 node <- it.Node() 293 }() 294 295 // Wait for the client to get stuck in waitForRootUpdates. 296 clock.WaitForTimers(1) 297 298 // Now update the root. 299 resolver.add(tree2.ToTXT("n")) 300 301 // Wait for it to pick up the root change. 302 clock.Run(c.cfg.RecheckInterval) 303 select { 304 case n := <-node: 305 if n.ID() != nodes[0].ID() { 306 t.Fatalf("wrong node returned") 307 } 308 case <-time.After(5 * time.Second): 309 t.Fatal("it.Next() did not unblock within 5s of real time") 310 } 311 } 312 313 // updateSomeNodes applies ENR updates to some of the given nodes. 314 func updateSomeNodes(keys []*ecdsa.PrivateKey, nodes []*enode.Node) { 315 for i, n := range nodes[:len(nodes)/2] { 316 r := n.Record() 317 r.Set(enr.IP{127, 0, 0, 1}) 318 r.SetSeq(55) 319 enode.SignV4(r, keys[i]) 320 n2, _ := enode.New(enode.ValidSchemes, r) 321 nodes[i] = n2 322 } 323 } 324 325 // This test verifies that randomIterator re-checks the root of the tree to catch 326 // updates to links. 327 func TestIteratorLinkUpdates(t *testing.T) { 328 var ( 329 clock = new(mclock.Simulated) 330 keys = testKeys(30) 331 nodes = testNodes(keys) 332 resolver = newMapResolver() 333 c = NewClient(Config{ 334 Resolver: resolver, 335 Logger: testlog.Logger(t, log.LvlTrace), 336 RecheckInterval: 20 * time.Minute, 337 RateLimit: 500, 338 }) 339 ) 340 c.clock = clock 341 tree3, url3 := makeTestTree("t3", nodes[20:30], nil) 342 tree2, url2 := makeTestTree("t2", nodes[10:20], nil) 343 tree1, url1 := makeTestTree("t1", nodes[0:10], []string{url2}) 344 resolver.add(tree1.ToTXT("t1")) 345 resolver.add(tree2.ToTXT("t2")) 346 resolver.add(tree3.ToTXT("t3")) 347 348 it, err := c.NewIterator(url1) 349 if err != nil { 350 t.Fatal(err) 351 } 352 353 // Sync tree1 using RandomNode. 354 checkIterator(t, it, nodes[:20]) 355 356 // Add link to tree3, remove link to tree2. 357 tree1, _ = makeTestTree("t1", nodes[:10], []string{url3}) 358 resolver.add(tree1.ToTXT("t1")) 359 t.Log("tree1 updated") 360 361 clock.Run(c.cfg.RecheckInterval + 1*time.Second) 362 363 var wantNodes []*enode.Node 364 wantNodes = append(wantNodes, tree1.Nodes()...) 365 wantNodes = append(wantNodes, tree3.Nodes()...) 366 checkIterator(t, it, wantNodes) 367 368 // Check that linked trees are GCed when they're no longer referenced. 369 knownTrees := it.(*randomIterator).trees 370 if len(knownTrees) != 2 { 371 t.Errorf("client knows %d trees, want 2", len(knownTrees)) 372 } 373 } 374 375 func checkIterator(t *testing.T, it enode.Iterator, wantNodes []*enode.Node) { 376 t.Helper() 377 378 var ( 379 want = make(map[enode.ID]*enode.Node) 380 maxCalls = len(wantNodes) * 3 381 calls = 0 382 ) 383 for _, n := range wantNodes { 384 want[n.ID()] = n 385 } 386 for ; len(want) > 0 && calls < maxCalls; calls++ { 387 if !it.Next() { 388 t.Fatalf("Next returned false (call %d)", calls) 389 } 390 n := it.Node() 391 delete(want, n.ID()) 392 } 393 t.Logf("checkIterator called Next %d times to find %d nodes", calls, len(wantNodes)) 394 for _, n := range want { 395 t.Errorf("iterator didn't discover node %v", n.ID()) 396 } 397 } 398 399 func makeTestTree(domain string, nodes []*enode.Node, links []string) (*Tree, string) { 400 tree, err := MakeTree(1, nodes, links) 401 if err != nil { 402 panic(err) 403 } 404 url, err := tree.Sign(signingKeyForTesting, domain) 405 if err != nil { 406 panic(err) 407 } 408 return tree, url 409 } 410 411 // testKeys creates deterministic private keys for testing. 412 func testKeys(n int) []*ecdsa.PrivateKey { 413 keys := make([]*ecdsa.PrivateKey, n) 414 for i := 0; i < n; i++ { 415 key, err := crypto.GenerateKey() 416 if err != nil { 417 panic("can't generate key: " + err.Error()) 418 } 419 keys[i] = key 420 } 421 return keys 422 } 423 424 func testNodes(keys []*ecdsa.PrivateKey) []*enode.Node { 425 nodes := make([]*enode.Node, len(keys)) 426 for i, key := range keys { 427 record := new(enr.Record) 428 record.SetSeq(uint64(i)) 429 enode.SignV4(record, key) 430 n, err := enode.New(enode.ValidSchemes, record) 431 if err != nil { 432 panic(err) 433 } 434 nodes[i] = n 435 } 436 return nodes 437 } 438 439 type mapResolver map[string]string 440 441 func newMapResolver(maps ...map[string]string) mapResolver { 442 mr := make(mapResolver, len(maps)) 443 for _, m := range maps { 444 mr.add(m) 445 } 446 return mr 447 } 448 449 func (mr mapResolver) clear() { 450 for k := range mr { 451 delete(mr, k) 452 } 453 } 454 455 func (mr mapResolver) add(m map[string]string) { 456 for k, v := range m { 457 mr[k] = v 458 } 459 } 460 461 func (mr mapResolver) LookupTXT(ctx context.Context, name string) ([]string, error) { 462 if record, ok := mr[name]; ok { 463 return []string{record}, nil 464 } 465 return nil, errors.New("not found") 466 } 467 468 func parseNodes(rec []string) []*enode.Node { 469 var ns []*enode.Node 470 for _, r := range rec { 471 var n enode.Node 472 if err := n.UnmarshalText([]byte(r)); err != nil { 473 panic(err) 474 } 475 ns = append(ns, &n) 476 } 477 return ns 478 }