github.com/luckypickle/go-ethereum-vet@v1.14.2/p2p/discv5/net.go (about) 1 // Copyright 2016 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 discv5 18 19 import ( 20 "bytes" 21 "crypto/ecdsa" 22 "errors" 23 "fmt" 24 "net" 25 "time" 26 27 "github.com/luckypickle/go-ethereum-vet/common" 28 "github.com/luckypickle/go-ethereum-vet/common/mclock" 29 "github.com/luckypickle/go-ethereum-vet/crypto" 30 "github.com/luckypickle/go-ethereum-vet/crypto/sha3" 31 "github.com/luckypickle/go-ethereum-vet/log" 32 "github.com/luckypickle/go-ethereum-vet/p2p/netutil" 33 "github.com/luckypickle/go-ethereum-vet/rlp" 34 ) 35 36 var ( 37 errInvalidEvent = errors.New("invalid in current state") 38 errNoQuery = errors.New("no pending query") 39 ) 40 41 const ( 42 autoRefreshInterval = 1 * time.Hour 43 bucketRefreshInterval = 1 * time.Minute 44 seedCount = 30 45 seedMaxAge = 5 * 24 * time.Hour 46 lowPort = 1024 47 ) 48 49 const testTopic = "foo" 50 51 const ( 52 printTestImgLogs = false 53 ) 54 55 // Network manages the table and all protocol interaction. 56 type Network struct { 57 db *nodeDB // database of known nodes 58 conn transport 59 netrestrict *netutil.Netlist 60 61 closed chan struct{} // closed when loop is done 62 closeReq chan struct{} // 'request to close' 63 refreshReq chan []*Node // lookups ask for refresh on this channel 64 refreshResp chan (<-chan struct{}) // ...and get the channel to block on from this one 65 read chan ingressPacket // ingress packets arrive here 66 timeout chan timeoutEvent 67 queryReq chan *findnodeQuery // lookups submit findnode queries on this channel 68 tableOpReq chan func() 69 tableOpResp chan struct{} 70 topicRegisterReq chan topicRegisterReq 71 topicSearchReq chan topicSearchReq 72 73 // State of the main loop. 74 tab *Table 75 topictab *topicTable 76 ticketStore *ticketStore 77 nursery []*Node 78 nodes map[NodeID]*Node // tracks active nodes with state != known 79 timeoutTimers map[timeoutEvent]*time.Timer 80 81 // Revalidation queues. 82 // Nodes put on these queues will be pinged eventually. 83 slowRevalidateQueue []*Node 84 fastRevalidateQueue []*Node 85 86 // Buffers for state transition. 87 sendBuf []*ingressPacket 88 } 89 90 // transport is implemented by the UDP transport. 91 // it is an interface so we can test without opening lots of UDP 92 // sockets and without generating a private key. 93 type transport interface { 94 sendPing(remote *Node, remoteAddr *net.UDPAddr, topics []Topic) (hash []byte) 95 sendNeighbours(remote *Node, nodes []*Node) 96 sendFindnodeHash(remote *Node, target common.Hash) 97 sendTopicRegister(remote *Node, topics []Topic, topicIdx int, pong []byte) 98 sendTopicNodes(remote *Node, queryHash common.Hash, nodes []*Node) 99 100 send(remote *Node, ptype nodeEvent, p interface{}) (hash []byte) 101 102 localAddr() *net.UDPAddr 103 Close() 104 } 105 106 type findnodeQuery struct { 107 remote *Node 108 target common.Hash 109 reply chan<- []*Node 110 nresults int // counter for received nodes 111 } 112 113 type topicRegisterReq struct { 114 add bool 115 topic Topic 116 } 117 118 type topicSearchReq struct { 119 topic Topic 120 found chan<- *Node 121 lookup chan<- bool 122 delay time.Duration 123 } 124 125 type topicSearchResult struct { 126 target lookupInfo 127 nodes []*Node 128 } 129 130 type timeoutEvent struct { 131 ev nodeEvent 132 node *Node 133 } 134 135 func newNetwork(conn transport, ourPubkey ecdsa.PublicKey, dbPath string, netrestrict *netutil.Netlist) (*Network, error) { 136 ourID := PubkeyID(&ourPubkey) 137 138 var db *nodeDB 139 if dbPath != "<no database>" { 140 var err error 141 if db, err = newNodeDB(dbPath, Version, ourID); err != nil { 142 return nil, err 143 } 144 } 145 146 tab := newTable(ourID, conn.localAddr()) 147 net := &Network{ 148 db: db, 149 conn: conn, 150 netrestrict: netrestrict, 151 tab: tab, 152 topictab: newTopicTable(db, tab.self), 153 ticketStore: newTicketStore(), 154 refreshReq: make(chan []*Node), 155 refreshResp: make(chan (<-chan struct{})), 156 closed: make(chan struct{}), 157 closeReq: make(chan struct{}), 158 read: make(chan ingressPacket, 100), 159 timeout: make(chan timeoutEvent), 160 timeoutTimers: make(map[timeoutEvent]*time.Timer), 161 tableOpReq: make(chan func()), 162 tableOpResp: make(chan struct{}), 163 queryReq: make(chan *findnodeQuery), 164 topicRegisterReq: make(chan topicRegisterReq), 165 topicSearchReq: make(chan topicSearchReq), 166 nodes: make(map[NodeID]*Node), 167 } 168 go net.loop() 169 return net, nil 170 } 171 172 // Close terminates the network listener and flushes the node database. 173 func (net *Network) Close() { 174 net.conn.Close() 175 select { 176 case <-net.closed: 177 case net.closeReq <- struct{}{}: 178 <-net.closed 179 } 180 } 181 182 // Self returns the local node. 183 // The returned node should not be modified by the caller. 184 func (net *Network) Self() *Node { 185 return net.tab.self 186 } 187 188 // ReadRandomNodes fills the given slice with random nodes from the 189 // table. It will not write the same node more than once. The nodes in 190 // the slice are copies and can be modified by the caller. 191 func (net *Network) ReadRandomNodes(buf []*Node) (n int) { 192 net.reqTableOp(func() { n = net.tab.readRandomNodes(buf) }) 193 return n 194 } 195 196 // SetFallbackNodes sets the initial points of contact. These nodes 197 // are used to connect to the network if the table is empty and there 198 // are no known nodes in the database. 199 func (net *Network) SetFallbackNodes(nodes []*Node) error { 200 nursery := make([]*Node, 0, len(nodes)) 201 for _, n := range nodes { 202 if err := n.validateComplete(); err != nil { 203 return fmt.Errorf("bad bootstrap/fallback node %q (%v)", n, err) 204 } 205 // Recompute cpy.sha because the node might not have been 206 // created by NewNode or ParseNode. 207 cpy := *n 208 cpy.sha = crypto.Keccak256Hash(n.ID[:]) 209 nursery = append(nursery, &cpy) 210 } 211 net.reqRefresh(nursery) 212 return nil 213 } 214 215 // Resolve searches for a specific node with the given ID. 216 // It returns nil if the node could not be found. 217 func (net *Network) Resolve(targetID NodeID) *Node { 218 result := net.lookup(crypto.Keccak256Hash(targetID[:]), true) 219 for _, n := range result { 220 if n.ID == targetID { 221 return n 222 } 223 } 224 return nil 225 } 226 227 // Lookup performs a network search for nodes close 228 // to the given target. It approaches the target by querying 229 // nodes that are closer to it on each iteration. 230 // The given target does not need to be an actual node 231 // identifier. 232 // 233 // The local node may be included in the result. 234 func (net *Network) Lookup(targetID NodeID) []*Node { 235 return net.lookup(crypto.Keccak256Hash(targetID[:]), false) 236 } 237 238 func (net *Network) lookup(target common.Hash, stopOnMatch bool) []*Node { 239 var ( 240 asked = make(map[NodeID]bool) 241 seen = make(map[NodeID]bool) 242 reply = make(chan []*Node, alpha) 243 result = nodesByDistance{target: target} 244 pendingQueries = 0 245 ) 246 // Get initial answers from the local node. 247 result.push(net.tab.self, bucketSize) 248 for { 249 // Ask the α closest nodes that we haven't asked yet. 250 for i := 0; i < len(result.entries) && pendingQueries < alpha; i++ { 251 n := result.entries[i] 252 if !asked[n.ID] { 253 asked[n.ID] = true 254 pendingQueries++ 255 net.reqQueryFindnode(n, target, reply) 256 } 257 } 258 if pendingQueries == 0 { 259 // We have asked all closest nodes, stop the search. 260 break 261 } 262 // Wait for the next reply. 263 select { 264 case nodes := <-reply: 265 for _, n := range nodes { 266 if n != nil && !seen[n.ID] { 267 seen[n.ID] = true 268 result.push(n, bucketSize) 269 if stopOnMatch && n.sha == target { 270 return result.entries 271 } 272 } 273 } 274 pendingQueries-- 275 case <-time.After(respTimeout): 276 // forget all pending requests, start new ones 277 pendingQueries = 0 278 reply = make(chan []*Node, alpha) 279 } 280 } 281 return result.entries 282 } 283 284 func (net *Network) RegisterTopic(topic Topic, stop <-chan struct{}) { 285 select { 286 case net.topicRegisterReq <- topicRegisterReq{true, topic}: 287 case <-net.closed: 288 return 289 } 290 select { 291 case <-net.closed: 292 case <-stop: 293 select { 294 case net.topicRegisterReq <- topicRegisterReq{false, topic}: 295 case <-net.closed: 296 } 297 } 298 } 299 300 func (net *Network) SearchTopic(topic Topic, setPeriod <-chan time.Duration, found chan<- *Node, lookup chan<- bool) { 301 for { 302 select { 303 case <-net.closed: 304 return 305 case delay, ok := <-setPeriod: 306 select { 307 case net.topicSearchReq <- topicSearchReq{topic: topic, found: found, lookup: lookup, delay: delay}: 308 case <-net.closed: 309 return 310 } 311 if !ok { 312 return 313 } 314 } 315 } 316 } 317 318 func (net *Network) reqRefresh(nursery []*Node) <-chan struct{} { 319 select { 320 case net.refreshReq <- nursery: 321 return <-net.refreshResp 322 case <-net.closed: 323 return net.closed 324 } 325 } 326 327 func (net *Network) reqQueryFindnode(n *Node, target common.Hash, reply chan []*Node) bool { 328 q := &findnodeQuery{remote: n, target: target, reply: reply} 329 select { 330 case net.queryReq <- q: 331 return true 332 case <-net.closed: 333 return false 334 } 335 } 336 337 func (net *Network) reqReadPacket(pkt ingressPacket) { 338 select { 339 case net.read <- pkt: 340 case <-net.closed: 341 } 342 } 343 344 func (net *Network) reqTableOp(f func()) (called bool) { 345 select { 346 case net.tableOpReq <- f: 347 <-net.tableOpResp 348 return true 349 case <-net.closed: 350 return false 351 } 352 } 353 354 // TODO: external address handling. 355 356 type topicSearchInfo struct { 357 lookupChn chan<- bool 358 period time.Duration 359 } 360 361 const maxSearchCount = 5 362 363 func (net *Network) loop() { 364 var ( 365 refreshTimer = time.NewTicker(autoRefreshInterval) 366 bucketRefreshTimer = time.NewTimer(bucketRefreshInterval) 367 refreshDone chan struct{} // closed when the 'refresh' lookup has ended 368 ) 369 370 // Tracking the next ticket to register. 371 var ( 372 nextTicket *ticketRef 373 nextRegisterTimer *time.Timer 374 nextRegisterTime <-chan time.Time 375 ) 376 defer func() { 377 if nextRegisterTimer != nil { 378 nextRegisterTimer.Stop() 379 } 380 }() 381 resetNextTicket := func() { 382 ticket, timeout := net.ticketStore.nextFilteredTicket() 383 if nextTicket != ticket { 384 nextTicket = ticket 385 if nextRegisterTimer != nil { 386 nextRegisterTimer.Stop() 387 nextRegisterTime = nil 388 } 389 if ticket != nil { 390 nextRegisterTimer = time.NewTimer(timeout) 391 nextRegisterTime = nextRegisterTimer.C 392 } 393 } 394 } 395 396 // Tracking registration and search lookups. 397 var ( 398 topicRegisterLookupTarget lookupInfo 399 topicRegisterLookupDone chan []*Node 400 topicRegisterLookupTick = time.NewTimer(0) 401 searchReqWhenRefreshDone []topicSearchReq 402 searchInfo = make(map[Topic]topicSearchInfo) 403 activeSearchCount int 404 ) 405 topicSearchLookupDone := make(chan topicSearchResult, 100) 406 topicSearch := make(chan Topic, 100) 407 <-topicRegisterLookupTick.C 408 409 statsDump := time.NewTicker(10 * time.Second) 410 411 loop: 412 for { 413 resetNextTicket() 414 415 select { 416 case <-net.closeReq: 417 log.Trace("<-net.closeReq") 418 break loop 419 420 // Ingress packet handling. 421 case pkt := <-net.read: 422 //fmt.Println("read", pkt.ev) 423 log.Trace("<-net.read") 424 n := net.internNode(&pkt) 425 prestate := n.state 426 status := "ok" 427 if err := net.handle(n, pkt.ev, &pkt); err != nil { 428 status = err.Error() 429 } 430 log.Trace("", "msg", log.Lazy{Fn: func() string { 431 return fmt.Sprintf("<<< (%d) %v from %x@%v: %v -> %v (%v)", 432 net.tab.count, pkt.ev, pkt.remoteID[:8], pkt.remoteAddr, prestate, n.state, status) 433 }}) 434 // TODO: persist state if n.state goes >= known, delete if it goes <= known 435 436 // State transition timeouts. 437 case timeout := <-net.timeout: 438 log.Trace("<-net.timeout") 439 if net.timeoutTimers[timeout] == nil { 440 // Stale timer (was aborted). 441 continue 442 } 443 delete(net.timeoutTimers, timeout) 444 prestate := timeout.node.state 445 status := "ok" 446 if err := net.handle(timeout.node, timeout.ev, nil); err != nil { 447 status = err.Error() 448 } 449 log.Trace("", "msg", log.Lazy{Fn: func() string { 450 return fmt.Sprintf("--- (%d) %v for %x@%v: %v -> %v (%v)", 451 net.tab.count, timeout.ev, timeout.node.ID[:8], timeout.node.addr(), prestate, timeout.node.state, status) 452 }}) 453 454 // Querying. 455 case q := <-net.queryReq: 456 log.Trace("<-net.queryReq") 457 if !q.start(net) { 458 q.remote.deferQuery(q) 459 } 460 461 // Interacting with the table. 462 case f := <-net.tableOpReq: 463 log.Trace("<-net.tableOpReq") 464 f() 465 net.tableOpResp <- struct{}{} 466 467 // Topic registration stuff. 468 case req := <-net.topicRegisterReq: 469 log.Trace("<-net.topicRegisterReq") 470 if !req.add { 471 net.ticketStore.removeRegisterTopic(req.topic) 472 continue 473 } 474 net.ticketStore.addTopic(req.topic, true) 475 // If we're currently waiting idle (nothing to look up), give the ticket store a 476 // chance to start it sooner. This should speed up convergence of the radius 477 // determination for new topics. 478 // if topicRegisterLookupDone == nil { 479 if topicRegisterLookupTarget.target == (common.Hash{}) { 480 log.Trace("topicRegisterLookupTarget == null") 481 if topicRegisterLookupTick.Stop() { 482 <-topicRegisterLookupTick.C 483 } 484 target, delay := net.ticketStore.nextRegisterLookup() 485 topicRegisterLookupTarget = target 486 topicRegisterLookupTick.Reset(delay) 487 } 488 489 case nodes := <-topicRegisterLookupDone: 490 log.Trace("<-topicRegisterLookupDone") 491 net.ticketStore.registerLookupDone(topicRegisterLookupTarget, nodes, func(n *Node) []byte { 492 net.ping(n, n.addr()) 493 return n.pingEcho 494 }) 495 target, delay := net.ticketStore.nextRegisterLookup() 496 topicRegisterLookupTarget = target 497 topicRegisterLookupTick.Reset(delay) 498 topicRegisterLookupDone = nil 499 500 case <-topicRegisterLookupTick.C: 501 log.Trace("<-topicRegisterLookupTick") 502 if (topicRegisterLookupTarget.target == common.Hash{}) { 503 target, delay := net.ticketStore.nextRegisterLookup() 504 topicRegisterLookupTarget = target 505 topicRegisterLookupTick.Reset(delay) 506 topicRegisterLookupDone = nil 507 } else { 508 topicRegisterLookupDone = make(chan []*Node) 509 target := topicRegisterLookupTarget.target 510 go func() { topicRegisterLookupDone <- net.lookup(target, false) }() 511 } 512 513 case <-nextRegisterTime: 514 log.Trace("<-nextRegisterTime") 515 net.ticketStore.ticketRegistered(*nextTicket) 516 //fmt.Println("sendTopicRegister", nextTicket.t.node.addr().String(), nextTicket.t.topics, nextTicket.idx, nextTicket.t.pong) 517 net.conn.sendTopicRegister(nextTicket.t.node, nextTicket.t.topics, nextTicket.idx, nextTicket.t.pong) 518 519 case req := <-net.topicSearchReq: 520 if refreshDone == nil { 521 log.Trace("<-net.topicSearchReq") 522 info, ok := searchInfo[req.topic] 523 if ok { 524 if req.delay == time.Duration(0) { 525 delete(searchInfo, req.topic) 526 net.ticketStore.removeSearchTopic(req.topic) 527 } else { 528 info.period = req.delay 529 searchInfo[req.topic] = info 530 } 531 continue 532 } 533 if req.delay != time.Duration(0) { 534 var info topicSearchInfo 535 info.period = req.delay 536 info.lookupChn = req.lookup 537 searchInfo[req.topic] = info 538 net.ticketStore.addSearchTopic(req.topic, req.found) 539 topicSearch <- req.topic 540 } 541 } else { 542 searchReqWhenRefreshDone = append(searchReqWhenRefreshDone, req) 543 } 544 545 case topic := <-topicSearch: 546 if activeSearchCount < maxSearchCount { 547 activeSearchCount++ 548 target := net.ticketStore.nextSearchLookup(topic) 549 go func() { 550 nodes := net.lookup(target.target, false) 551 topicSearchLookupDone <- topicSearchResult{target: target, nodes: nodes} 552 }() 553 } 554 period := searchInfo[topic].period 555 if period != time.Duration(0) { 556 go func() { 557 time.Sleep(period) 558 topicSearch <- topic 559 }() 560 } 561 562 case res := <-topicSearchLookupDone: 563 activeSearchCount-- 564 if lookupChn := searchInfo[res.target.topic].lookupChn; lookupChn != nil { 565 lookupChn <- net.ticketStore.radius[res.target.topic].converged 566 } 567 net.ticketStore.searchLookupDone(res.target, res.nodes, func(n *Node, topic Topic) []byte { 568 if n.state != nil && n.state.canQuery { 569 return net.conn.send(n, topicQueryPacket, topicQuery{Topic: topic}) // TODO: set expiration 570 } else { 571 if n.state == unknown { 572 net.ping(n, n.addr()) 573 } 574 return nil 575 } 576 }) 577 578 case <-statsDump.C: 579 log.Trace("<-statsDump.C") 580 /*r, ok := net.ticketStore.radius[testTopic] 581 if !ok { 582 fmt.Printf("(%x) no radius @ %v\n", net.tab.self.ID[:8], time.Now()) 583 } else { 584 topics := len(net.ticketStore.tickets) 585 tickets := len(net.ticketStore.nodes) 586 rad := r.radius / (maxRadius/10000+1) 587 fmt.Printf("(%x) topics:%d radius:%d tickets:%d @ %v\n", net.tab.self.ID[:8], topics, rad, tickets, time.Now()) 588 }*/ 589 590 tm := mclock.Now() 591 for topic, r := range net.ticketStore.radius { 592 if printTestImgLogs { 593 rad := r.radius / (maxRadius/1000000 + 1) 594 minrad := r.minRadius / (maxRadius/1000000 + 1) 595 fmt.Printf("*R %d %v %016x %v\n", tm/1000000, topic, net.tab.self.sha[:8], rad) 596 fmt.Printf("*MR %d %v %016x %v\n", tm/1000000, topic, net.tab.self.sha[:8], minrad) 597 } 598 } 599 for topic, t := range net.topictab.topics { 600 wp := t.wcl.nextWaitPeriod(tm) 601 if printTestImgLogs { 602 fmt.Printf("*W %d %v %016x %d\n", tm/1000000, topic, net.tab.self.sha[:8], wp/1000000) 603 } 604 } 605 606 // Periodic / lookup-initiated bucket refresh. 607 case <-refreshTimer.C: 608 log.Trace("<-refreshTimer.C") 609 // TODO: ideally we would start the refresh timer after 610 // fallback nodes have been set for the first time. 611 if refreshDone == nil { 612 refreshDone = make(chan struct{}) 613 net.refresh(refreshDone) 614 } 615 case <-bucketRefreshTimer.C: 616 target := net.tab.chooseBucketRefreshTarget() 617 go func() { 618 net.lookup(target, false) 619 bucketRefreshTimer.Reset(bucketRefreshInterval) 620 }() 621 case newNursery := <-net.refreshReq: 622 log.Trace("<-net.refreshReq") 623 if newNursery != nil { 624 net.nursery = newNursery 625 } 626 if refreshDone == nil { 627 refreshDone = make(chan struct{}) 628 net.refresh(refreshDone) 629 } 630 net.refreshResp <- refreshDone 631 case <-refreshDone: 632 log.Trace("<-net.refreshDone", "table size", net.tab.count) 633 if net.tab.count != 0 { 634 refreshDone = nil 635 list := searchReqWhenRefreshDone 636 searchReqWhenRefreshDone = nil 637 go func() { 638 for _, req := range list { 639 net.topicSearchReq <- req 640 } 641 }() 642 } else { 643 refreshDone = make(chan struct{}) 644 net.refresh(refreshDone) 645 } 646 } 647 } 648 log.Trace("loop stopped") 649 650 log.Debug(fmt.Sprintf("shutting down")) 651 if net.conn != nil { 652 net.conn.Close() 653 } 654 if refreshDone != nil { 655 // TODO: wait for pending refresh. 656 //<-refreshResults 657 } 658 // Cancel all pending timeouts. 659 for _, timer := range net.timeoutTimers { 660 timer.Stop() 661 } 662 if net.db != nil { 663 net.db.close() 664 } 665 close(net.closed) 666 } 667 668 // Everything below runs on the Network.loop goroutine 669 // and can modify Node, Table and Network at any time without locking. 670 671 func (net *Network) refresh(done chan<- struct{}) { 672 var seeds []*Node 673 if net.db != nil { 674 seeds = net.db.querySeeds(seedCount, seedMaxAge) 675 } 676 if len(seeds) == 0 { 677 seeds = net.nursery 678 } 679 if len(seeds) == 0 { 680 log.Trace("no seed nodes found") 681 time.AfterFunc(time.Second*10, func() { close(done) }) 682 return 683 } 684 for _, n := range seeds { 685 log.Debug("", "msg", log.Lazy{Fn: func() string { 686 var age string 687 if net.db != nil { 688 age = time.Since(net.db.lastPong(n.ID)).String() 689 } else { 690 age = "unknown" 691 } 692 return fmt.Sprintf("seed node (age %s): %v", age, n) 693 }}) 694 n = net.internNodeFromDB(n) 695 if n.state == unknown { 696 net.transition(n, verifyinit) 697 } 698 // Force-add the seed node so Lookup does something. 699 // It will be deleted again if verification fails. 700 net.tab.add(n) 701 } 702 // Start self lookup to fill up the buckets. 703 go func() { 704 net.Lookup(net.tab.self.ID) 705 close(done) 706 }() 707 } 708 709 // Node Interning. 710 711 func (net *Network) internNode(pkt *ingressPacket) *Node { 712 if n := net.nodes[pkt.remoteID]; n != nil { 713 n.IP = pkt.remoteAddr.IP 714 n.UDP = uint16(pkt.remoteAddr.Port) 715 n.TCP = uint16(pkt.remoteAddr.Port) 716 return n 717 } 718 n := NewNode(pkt.remoteID, pkt.remoteAddr.IP, uint16(pkt.remoteAddr.Port), uint16(pkt.remoteAddr.Port)) 719 n.state = unknown 720 net.nodes[pkt.remoteID] = n 721 return n 722 } 723 724 func (net *Network) internNodeFromDB(dbn *Node) *Node { 725 if n := net.nodes[dbn.ID]; n != nil { 726 return n 727 } 728 n := NewNode(dbn.ID, dbn.IP, dbn.UDP, dbn.TCP) 729 n.state = unknown 730 net.nodes[n.ID] = n 731 return n 732 } 733 734 func (net *Network) internNodeFromNeighbours(sender *net.UDPAddr, rn rpcNode) (n *Node, err error) { 735 if rn.ID == net.tab.self.ID { 736 return nil, errors.New("is self") 737 } 738 if rn.UDP <= lowPort { 739 return nil, errors.New("low port") 740 } 741 n = net.nodes[rn.ID] 742 if n == nil { 743 // We haven't seen this node before. 744 n, err = nodeFromRPC(sender, rn) 745 if net.netrestrict != nil && !net.netrestrict.Contains(n.IP) { 746 return n, errors.New("not contained in netrestrict whitelist") 747 } 748 if err == nil { 749 n.state = unknown 750 net.nodes[n.ID] = n 751 } 752 return n, err 753 } 754 if !n.IP.Equal(rn.IP) || n.UDP != rn.UDP || n.TCP != rn.TCP { 755 if n.state == known { 756 // reject address change if node is known by us 757 err = fmt.Errorf("metadata mismatch: got %v, want %v", rn, n) 758 } else { 759 // accept otherwise; this will be handled nicer with signed ENRs 760 n.IP = rn.IP 761 n.UDP = rn.UDP 762 n.TCP = rn.TCP 763 } 764 } 765 return n, err 766 } 767 768 // nodeNetGuts is embedded in Node and contains fields. 769 type nodeNetGuts struct { 770 // This is a cached copy of sha3(ID) which is used for node 771 // distance calculations. This is part of Node in order to make it 772 // possible to write tests that need a node at a certain distance. 773 // In those tests, the content of sha will not actually correspond 774 // with ID. 775 sha common.Hash 776 777 // State machine fields. Access to these fields 778 // is restricted to the Network.loop goroutine. 779 state *nodeState 780 pingEcho []byte // hash of last ping sent by us 781 pingTopics []Topic // topic set sent by us in last ping 782 deferredQueries []*findnodeQuery // queries that can't be sent yet 783 pendingNeighbours *findnodeQuery // current query, waiting for reply 784 queryTimeouts int 785 } 786 787 func (n *nodeNetGuts) deferQuery(q *findnodeQuery) { 788 n.deferredQueries = append(n.deferredQueries, q) 789 } 790 791 func (n *nodeNetGuts) startNextQuery(net *Network) { 792 if len(n.deferredQueries) == 0 { 793 return 794 } 795 nextq := n.deferredQueries[0] 796 if nextq.start(net) { 797 n.deferredQueries = append(n.deferredQueries[:0], n.deferredQueries[1:]...) 798 } 799 } 800 801 func (q *findnodeQuery) start(net *Network) bool { 802 // Satisfy queries against the local node directly. 803 if q.remote == net.tab.self { 804 closest := net.tab.closest(crypto.Keccak256Hash(q.target[:]), bucketSize) 805 q.reply <- closest.entries 806 return true 807 } 808 if q.remote.state.canQuery && q.remote.pendingNeighbours == nil { 809 net.conn.sendFindnodeHash(q.remote, q.target) 810 net.timedEvent(respTimeout, q.remote, neighboursTimeout) 811 q.remote.pendingNeighbours = q 812 return true 813 } 814 // If the node is not known yet, it won't accept queries. 815 // Initiate the transition to known. 816 // The request will be sent later when the node reaches known state. 817 if q.remote.state == unknown { 818 net.transition(q.remote, verifyinit) 819 } 820 return false 821 } 822 823 // Node Events (the input to the state machine). 824 825 type nodeEvent uint 826 827 //go:generate stringer -type=nodeEvent 828 829 const ( 830 831 // Packet type events. 832 // These correspond to packet types in the UDP protocol. 833 pingPacket = iota + 1 834 pongPacket 835 findnodePacket 836 neighborsPacket 837 findnodeHashPacket 838 topicRegisterPacket 839 topicQueryPacket 840 topicNodesPacket 841 842 // Non-packet events. 843 // Event values in this category are allocated outside 844 // the packet type range (packet types are encoded as a single byte). 845 pongTimeout nodeEvent = iota + 256 846 pingTimeout 847 neighboursTimeout 848 ) 849 850 // Node State Machine. 851 852 type nodeState struct { 853 name string 854 handle func(*Network, *Node, nodeEvent, *ingressPacket) (next *nodeState, err error) 855 enter func(*Network, *Node) 856 canQuery bool 857 } 858 859 func (s *nodeState) String() string { 860 return s.name 861 } 862 863 var ( 864 unknown *nodeState 865 verifyinit *nodeState 866 verifywait *nodeState 867 remoteverifywait *nodeState 868 known *nodeState 869 contested *nodeState 870 unresponsive *nodeState 871 ) 872 873 func init() { 874 unknown = &nodeState{ 875 name: "unknown", 876 enter: func(net *Network, n *Node) { 877 net.tab.delete(n) 878 n.pingEcho = nil 879 // Abort active queries. 880 for _, q := range n.deferredQueries { 881 q.reply <- nil 882 } 883 n.deferredQueries = nil 884 if n.pendingNeighbours != nil { 885 n.pendingNeighbours.reply <- nil 886 n.pendingNeighbours = nil 887 } 888 n.queryTimeouts = 0 889 }, 890 handle: func(net *Network, n *Node, ev nodeEvent, pkt *ingressPacket) (*nodeState, error) { 891 switch ev { 892 case pingPacket: 893 net.handlePing(n, pkt) 894 net.ping(n, pkt.remoteAddr) 895 return verifywait, nil 896 default: 897 return unknown, errInvalidEvent 898 } 899 }, 900 } 901 902 verifyinit = &nodeState{ 903 name: "verifyinit", 904 enter: func(net *Network, n *Node) { 905 net.ping(n, n.addr()) 906 }, 907 handle: func(net *Network, n *Node, ev nodeEvent, pkt *ingressPacket) (*nodeState, error) { 908 switch ev { 909 case pingPacket: 910 net.handlePing(n, pkt) 911 return verifywait, nil 912 case pongPacket: 913 err := net.handleKnownPong(n, pkt) 914 return remoteverifywait, err 915 case pongTimeout: 916 return unknown, nil 917 default: 918 return verifyinit, errInvalidEvent 919 } 920 }, 921 } 922 923 verifywait = &nodeState{ 924 name: "verifywait", 925 handle: func(net *Network, n *Node, ev nodeEvent, pkt *ingressPacket) (*nodeState, error) { 926 switch ev { 927 case pingPacket: 928 net.handlePing(n, pkt) 929 return verifywait, nil 930 case pongPacket: 931 err := net.handleKnownPong(n, pkt) 932 return known, err 933 case pongTimeout: 934 return unknown, nil 935 default: 936 return verifywait, errInvalidEvent 937 } 938 }, 939 } 940 941 remoteverifywait = &nodeState{ 942 name: "remoteverifywait", 943 enter: func(net *Network, n *Node) { 944 net.timedEvent(respTimeout, n, pingTimeout) 945 }, 946 handle: func(net *Network, n *Node, ev nodeEvent, pkt *ingressPacket) (*nodeState, error) { 947 switch ev { 948 case pingPacket: 949 net.handlePing(n, pkt) 950 return remoteverifywait, nil 951 case pingTimeout: 952 return known, nil 953 default: 954 return remoteverifywait, errInvalidEvent 955 } 956 }, 957 } 958 959 known = &nodeState{ 960 name: "known", 961 canQuery: true, 962 enter: func(net *Network, n *Node) { 963 n.queryTimeouts = 0 964 n.startNextQuery(net) 965 // Insert into the table and start revalidation of the last node 966 // in the bucket if it is full. 967 last := net.tab.add(n) 968 if last != nil && last.state == known { 969 // TODO: do this asynchronously 970 net.transition(last, contested) 971 } 972 }, 973 handle: func(net *Network, n *Node, ev nodeEvent, pkt *ingressPacket) (*nodeState, error) { 974 switch ev { 975 case pingPacket: 976 net.handlePing(n, pkt) 977 return known, nil 978 case pongPacket: 979 err := net.handleKnownPong(n, pkt) 980 return known, err 981 default: 982 return net.handleQueryEvent(n, ev, pkt) 983 } 984 }, 985 } 986 987 contested = &nodeState{ 988 name: "contested", 989 canQuery: true, 990 enter: func(net *Network, n *Node) { 991 net.ping(n, n.addr()) 992 }, 993 handle: func(net *Network, n *Node, ev nodeEvent, pkt *ingressPacket) (*nodeState, error) { 994 switch ev { 995 case pongPacket: 996 // Node is still alive. 997 err := net.handleKnownPong(n, pkt) 998 return known, err 999 case pongTimeout: 1000 net.tab.deleteReplace(n) 1001 return unresponsive, nil 1002 case pingPacket: 1003 net.handlePing(n, pkt) 1004 return contested, nil 1005 default: 1006 return net.handleQueryEvent(n, ev, pkt) 1007 } 1008 }, 1009 } 1010 1011 unresponsive = &nodeState{ 1012 name: "unresponsive", 1013 canQuery: true, 1014 handle: func(net *Network, n *Node, ev nodeEvent, pkt *ingressPacket) (*nodeState, error) { 1015 switch ev { 1016 case pingPacket: 1017 net.handlePing(n, pkt) 1018 return known, nil 1019 case pongPacket: 1020 err := net.handleKnownPong(n, pkt) 1021 return known, err 1022 default: 1023 return net.handleQueryEvent(n, ev, pkt) 1024 } 1025 }, 1026 } 1027 } 1028 1029 // handle processes packets sent by n and events related to n. 1030 func (net *Network) handle(n *Node, ev nodeEvent, pkt *ingressPacket) error { 1031 //fmt.Println("handle", n.addr().String(), n.state, ev) 1032 if pkt != nil { 1033 if err := net.checkPacket(n, ev, pkt); err != nil { 1034 //fmt.Println("check err:", err) 1035 return err 1036 } 1037 // Start the background expiration goroutine after the first 1038 // successful communication. Subsequent calls have no effect if it 1039 // is already running. We do this here instead of somewhere else 1040 // so that the search for seed nodes also considers older nodes 1041 // that would otherwise be removed by the expirer. 1042 if net.db != nil { 1043 net.db.ensureExpirer() 1044 } 1045 } 1046 if ev == pongTimeout { 1047 n.pingEcho = nil // clean up if pongtimeout 1048 } 1049 if n.state == nil { 1050 n.state = unknown //??? 1051 } 1052 next, err := n.state.handle(net, n, ev, pkt) 1053 net.transition(n, next) 1054 //fmt.Println("new state:", n.state) 1055 return err 1056 } 1057 1058 func (net *Network) checkPacket(n *Node, ev nodeEvent, pkt *ingressPacket) error { 1059 // Replay prevention checks. 1060 switch ev { 1061 case pingPacket, findnodeHashPacket, neighborsPacket: 1062 // TODO: check date is > last date seen 1063 // TODO: check ping version 1064 case pongPacket: 1065 if !bytes.Equal(pkt.data.(*pong).ReplyTok, n.pingEcho) { 1066 // fmt.Println("pong reply token mismatch") 1067 return fmt.Errorf("pong reply token mismatch") 1068 } 1069 n.pingEcho = nil 1070 } 1071 // Address validation. 1072 // TODO: Ideally we would do the following: 1073 // - reject all packets with wrong address except ping. 1074 // - for ping with new address, transition to verifywait but keep the 1075 // previous node (with old address) around. if the new one reaches known, 1076 // swap it out. 1077 return nil 1078 } 1079 1080 func (net *Network) transition(n *Node, next *nodeState) { 1081 if n.state != next { 1082 n.state = next 1083 if next.enter != nil { 1084 next.enter(net, n) 1085 } 1086 } 1087 1088 // TODO: persist/unpersist node 1089 } 1090 1091 func (net *Network) timedEvent(d time.Duration, n *Node, ev nodeEvent) { 1092 timeout := timeoutEvent{ev, n} 1093 net.timeoutTimers[timeout] = time.AfterFunc(d, func() { 1094 select { 1095 case net.timeout <- timeout: 1096 case <-net.closed: 1097 } 1098 }) 1099 } 1100 1101 func (net *Network) abortTimedEvent(n *Node, ev nodeEvent) { 1102 timer := net.timeoutTimers[timeoutEvent{ev, n}] 1103 if timer != nil { 1104 timer.Stop() 1105 delete(net.timeoutTimers, timeoutEvent{ev, n}) 1106 } 1107 } 1108 1109 func (net *Network) ping(n *Node, addr *net.UDPAddr) { 1110 //fmt.Println("ping", n.addr().String(), n.ID.String(), n.sha.Hex()) 1111 if n.pingEcho != nil || n.ID == net.tab.self.ID { 1112 //fmt.Println(" not sent") 1113 return 1114 } 1115 log.Trace("Pinging remote node", "node", n.ID) 1116 n.pingTopics = net.ticketStore.regTopicSet() 1117 n.pingEcho = net.conn.sendPing(n, addr, n.pingTopics) 1118 net.timedEvent(respTimeout, n, pongTimeout) 1119 } 1120 1121 func (net *Network) handlePing(n *Node, pkt *ingressPacket) { 1122 log.Trace("Handling remote ping", "node", n.ID) 1123 ping := pkt.data.(*ping) 1124 n.TCP = ping.From.TCP 1125 t := net.topictab.getTicket(n, ping.Topics) 1126 1127 pong := &pong{ 1128 To: makeEndpoint(n.addr(), n.TCP), // TODO: maybe use known TCP port from DB 1129 ReplyTok: pkt.hash, 1130 Expiration: uint64(time.Now().Add(expiration).Unix()), 1131 } 1132 ticketToPong(t, pong) 1133 net.conn.send(n, pongPacket, pong) 1134 } 1135 1136 func (net *Network) handleKnownPong(n *Node, pkt *ingressPacket) error { 1137 log.Trace("Handling known pong", "node", n.ID) 1138 net.abortTimedEvent(n, pongTimeout) 1139 now := mclock.Now() 1140 ticket, err := pongToTicket(now, n.pingTopics, n, pkt) 1141 if err == nil { 1142 // fmt.Printf("(%x) ticket: %+v\n", net.tab.self.ID[:8], pkt.data) 1143 net.ticketStore.addTicket(now, pkt.data.(*pong).ReplyTok, ticket) 1144 } else { 1145 log.Trace("Failed to convert pong to ticket", "err", err) 1146 } 1147 n.pingEcho = nil 1148 n.pingTopics = nil 1149 return err 1150 } 1151 1152 func (net *Network) handleQueryEvent(n *Node, ev nodeEvent, pkt *ingressPacket) (*nodeState, error) { 1153 switch ev { 1154 case findnodePacket: 1155 target := crypto.Keccak256Hash(pkt.data.(*findnode).Target[:]) 1156 results := net.tab.closest(target, bucketSize).entries 1157 net.conn.sendNeighbours(n, results) 1158 return n.state, nil 1159 case neighborsPacket: 1160 err := net.handleNeighboursPacket(n, pkt) 1161 return n.state, err 1162 case neighboursTimeout: 1163 if n.pendingNeighbours != nil { 1164 n.pendingNeighbours.reply <- nil 1165 n.pendingNeighbours = nil 1166 } 1167 n.queryTimeouts++ 1168 if n.queryTimeouts > maxFindnodeFailures && n.state == known { 1169 return contested, errors.New("too many timeouts") 1170 } 1171 return n.state, nil 1172 1173 // v5 1174 1175 case findnodeHashPacket: 1176 results := net.tab.closest(pkt.data.(*findnodeHash).Target, bucketSize).entries 1177 net.conn.sendNeighbours(n, results) 1178 return n.state, nil 1179 case topicRegisterPacket: 1180 //fmt.Println("got topicRegisterPacket") 1181 regdata := pkt.data.(*topicRegister) 1182 pong, err := net.checkTopicRegister(regdata) 1183 if err != nil { 1184 //fmt.Println(err) 1185 return n.state, fmt.Errorf("bad waiting ticket: %v", err) 1186 } 1187 net.topictab.useTicket(n, pong.TicketSerial, regdata.Topics, int(regdata.Idx), pong.Expiration, pong.WaitPeriods) 1188 return n.state, nil 1189 case topicQueryPacket: 1190 // TODO: handle expiration 1191 topic := pkt.data.(*topicQuery).Topic 1192 results := net.topictab.getEntries(topic) 1193 if _, ok := net.ticketStore.tickets[topic]; ok { 1194 results = append(results, net.tab.self) // we're not registering in our own table but if we're advertising, return ourselves too 1195 } 1196 if len(results) > 10 { 1197 results = results[:10] 1198 } 1199 var hash common.Hash 1200 copy(hash[:], pkt.hash) 1201 net.conn.sendTopicNodes(n, hash, results) 1202 return n.state, nil 1203 case topicNodesPacket: 1204 p := pkt.data.(*topicNodes) 1205 if net.ticketStore.gotTopicNodes(n, p.Echo, p.Nodes) { 1206 n.queryTimeouts++ 1207 if n.queryTimeouts > maxFindnodeFailures && n.state == known { 1208 return contested, errors.New("too many timeouts") 1209 } 1210 } 1211 return n.state, nil 1212 1213 default: 1214 return n.state, errInvalidEvent 1215 } 1216 } 1217 1218 func (net *Network) checkTopicRegister(data *topicRegister) (*pong, error) { 1219 var pongpkt ingressPacket 1220 if err := decodePacket(data.Pong, &pongpkt); err != nil { 1221 return nil, err 1222 } 1223 if pongpkt.ev != pongPacket { 1224 return nil, errors.New("is not pong packet") 1225 } 1226 if pongpkt.remoteID != net.tab.self.ID { 1227 return nil, errors.New("not signed by us") 1228 } 1229 // check that we previously authorised all topics 1230 // that the other side is trying to register. 1231 if rlpHash(data.Topics) != pongpkt.data.(*pong).TopicHash { 1232 return nil, errors.New("topic hash mismatch") 1233 } 1234 if int(data.Idx) < 0 || int(data.Idx) >= len(data.Topics) { 1235 return nil, errors.New("topic index out of range") 1236 } 1237 return pongpkt.data.(*pong), nil 1238 } 1239 1240 func rlpHash(x interface{}) (h common.Hash) { 1241 hw := sha3.NewKeccak256() 1242 rlp.Encode(hw, x) 1243 hw.Sum(h[:0]) 1244 return h 1245 } 1246 1247 func (net *Network) handleNeighboursPacket(n *Node, pkt *ingressPacket) error { 1248 if n.pendingNeighbours == nil { 1249 return errNoQuery 1250 } 1251 net.abortTimedEvent(n, neighboursTimeout) 1252 1253 req := pkt.data.(*neighbors) 1254 nodes := make([]*Node, len(req.Nodes)) 1255 for i, rn := range req.Nodes { 1256 nn, err := net.internNodeFromNeighbours(pkt.remoteAddr, rn) 1257 if err != nil { 1258 log.Debug(fmt.Sprintf("invalid neighbour (%v) from %x@%v: %v", rn.IP, n.ID[:8], pkt.remoteAddr, err)) 1259 continue 1260 } 1261 nodes[i] = nn 1262 // Start validation of query results immediately. 1263 // This fills the table quickly. 1264 // TODO: generates way too many packets, maybe do it via queue. 1265 if nn.state == unknown { 1266 net.transition(nn, verifyinit) 1267 } 1268 } 1269 // TODO: don't ignore second packet 1270 n.pendingNeighbours.reply <- nodes 1271 n.pendingNeighbours = nil 1272 // Now that this query is done, start the next one. 1273 n.startNextQuery(net) 1274 return nil 1275 }