github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/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/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/common/mclock" 29 "github.com/ethereum/go-ethereum/crypto" 30 "github.com/ethereum/go-ethereum/crypto/sha3" 31 "github.com/ethereum/go-ethereum/log" 32 "github.com/ethereum/go-ethereum/p2p/netutil" 33 "github.com/ethereum/go-ethereum/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 n.state == nil { 1047 n.state = unknown //??? 1048 } 1049 next, err := n.state.handle(net, n, ev, pkt) 1050 net.transition(n, next) 1051 //fmt.Println("new state:", n.state) 1052 return err 1053 } 1054 1055 func (net *Network) checkPacket(n *Node, ev nodeEvent, pkt *ingressPacket) error { 1056 // Replay prevention checks. 1057 switch ev { 1058 case pingPacket, findnodeHashPacket, neighborsPacket: 1059 // TODO: check date is > last date seen 1060 // TODO: check ping version 1061 case pongPacket: 1062 if !bytes.Equal(pkt.data.(*pong).ReplyTok, n.pingEcho) { 1063 // fmt.Println("pong reply token mismatch") 1064 return fmt.Errorf("pong reply token mismatch") 1065 } 1066 n.pingEcho = nil 1067 } 1068 // Address validation. 1069 // TODO: Ideally we would do the following: 1070 // - reject all packets with wrong address except ping. 1071 // - for ping with new address, transition to verifywait but keep the 1072 // previous node (with old address) around. if the new one reaches known, 1073 // swap it out. 1074 return nil 1075 } 1076 1077 func (net *Network) transition(n *Node, next *nodeState) { 1078 if n.state != next { 1079 n.state = next 1080 if next.enter != nil { 1081 next.enter(net, n) 1082 } 1083 } 1084 1085 // TODO: persist/unpersist node 1086 } 1087 1088 func (net *Network) timedEvent(d time.Duration, n *Node, ev nodeEvent) { 1089 timeout := timeoutEvent{ev, n} 1090 net.timeoutTimers[timeout] = time.AfterFunc(d, func() { 1091 select { 1092 case net.timeout <- timeout: 1093 case <-net.closed: 1094 } 1095 }) 1096 } 1097 1098 func (net *Network) abortTimedEvent(n *Node, ev nodeEvent) { 1099 timer := net.timeoutTimers[timeoutEvent{ev, n}] 1100 if timer != nil { 1101 timer.Stop() 1102 delete(net.timeoutTimers, timeoutEvent{ev, n}) 1103 } 1104 } 1105 1106 func (net *Network) ping(n *Node, addr *net.UDPAddr) { 1107 //fmt.Println("ping", n.addr().String(), n.ID.String(), n.sha.Hex()) 1108 if n.pingEcho != nil || n.ID == net.tab.self.ID { 1109 //fmt.Println(" not sent") 1110 return 1111 } 1112 log.Trace("Pinging remote node", "node", n.ID) 1113 n.pingTopics = net.ticketStore.regTopicSet() 1114 n.pingEcho = net.conn.sendPing(n, addr, n.pingTopics) 1115 net.timedEvent(respTimeout, n, pongTimeout) 1116 } 1117 1118 func (net *Network) handlePing(n *Node, pkt *ingressPacket) { 1119 log.Trace("Handling remote ping", "node", n.ID) 1120 ping := pkt.data.(*ping) 1121 n.TCP = ping.From.TCP 1122 t := net.topictab.getTicket(n, ping.Topics) 1123 1124 pong := &pong{ 1125 To: makeEndpoint(n.addr(), n.TCP), // TODO: maybe use known TCP port from DB 1126 ReplyTok: pkt.hash, 1127 Expiration: uint64(time.Now().Add(expiration).Unix()), 1128 } 1129 ticketToPong(t, pong) 1130 net.conn.send(n, pongPacket, pong) 1131 } 1132 1133 func (net *Network) handleKnownPong(n *Node, pkt *ingressPacket) error { 1134 log.Trace("Handling known pong", "node", n.ID) 1135 net.abortTimedEvent(n, pongTimeout) 1136 now := mclock.Now() 1137 ticket, err := pongToTicket(now, n.pingTopics, n, pkt) 1138 if err == nil { 1139 // fmt.Printf("(%x) ticket: %+v\n", net.tab.self.ID[:8], pkt.data) 1140 net.ticketStore.addTicket(now, pkt.data.(*pong).ReplyTok, ticket) 1141 } else { 1142 log.Trace("Failed to convert pong to ticket", "err", err) 1143 } 1144 n.pingEcho = nil 1145 n.pingTopics = nil 1146 return err 1147 } 1148 1149 func (net *Network) handleQueryEvent(n *Node, ev nodeEvent, pkt *ingressPacket) (*nodeState, error) { 1150 switch ev { 1151 case findnodePacket: 1152 target := crypto.Keccak256Hash(pkt.data.(*findnode).Target[:]) 1153 results := net.tab.closest(target, bucketSize).entries 1154 net.conn.sendNeighbours(n, results) 1155 return n.state, nil 1156 case neighborsPacket: 1157 err := net.handleNeighboursPacket(n, pkt) 1158 return n.state, err 1159 case neighboursTimeout: 1160 if n.pendingNeighbours != nil { 1161 n.pendingNeighbours.reply <- nil 1162 n.pendingNeighbours = nil 1163 } 1164 n.queryTimeouts++ 1165 if n.queryTimeouts > maxFindnodeFailures && n.state == known { 1166 return contested, errors.New("too many timeouts") 1167 } 1168 return n.state, nil 1169 1170 // v5 1171 1172 case findnodeHashPacket: 1173 results := net.tab.closest(pkt.data.(*findnodeHash).Target, bucketSize).entries 1174 net.conn.sendNeighbours(n, results) 1175 return n.state, nil 1176 case topicRegisterPacket: 1177 //fmt.Println("got topicRegisterPacket") 1178 regdata := pkt.data.(*topicRegister) 1179 pong, err := net.checkTopicRegister(regdata) 1180 if err != nil { 1181 //fmt.Println(err) 1182 return n.state, fmt.Errorf("bad waiting ticket: %v", err) 1183 } 1184 net.topictab.useTicket(n, pong.TicketSerial, regdata.Topics, int(regdata.Idx), pong.Expiration, pong.WaitPeriods) 1185 return n.state, nil 1186 case topicQueryPacket: 1187 // TODO: handle expiration 1188 topic := pkt.data.(*topicQuery).Topic 1189 results := net.topictab.getEntries(topic) 1190 if _, ok := net.ticketStore.tickets[topic]; ok { 1191 results = append(results, net.tab.self) // we're not registering in our own table but if we're advertising, return ourselves too 1192 } 1193 if len(results) > 10 { 1194 results = results[:10] 1195 } 1196 var hash common.Hash 1197 copy(hash[:], pkt.hash) 1198 net.conn.sendTopicNodes(n, hash, results) 1199 return n.state, nil 1200 case topicNodesPacket: 1201 p := pkt.data.(*topicNodes) 1202 if net.ticketStore.gotTopicNodes(n, p.Echo, p.Nodes) { 1203 n.queryTimeouts++ 1204 if n.queryTimeouts > maxFindnodeFailures && n.state == known { 1205 return contested, errors.New("too many timeouts") 1206 } 1207 } 1208 return n.state, nil 1209 1210 default: 1211 return n.state, errInvalidEvent 1212 } 1213 } 1214 1215 func (net *Network) checkTopicRegister(data *topicRegister) (*pong, error) { 1216 var pongpkt ingressPacket 1217 if err := decodePacket(data.Pong, &pongpkt); err != nil { 1218 return nil, err 1219 } 1220 if pongpkt.ev != pongPacket { 1221 return nil, errors.New("is not pong packet") 1222 } 1223 if pongpkt.remoteID != net.tab.self.ID { 1224 return nil, errors.New("not signed by us") 1225 } 1226 // check that we previously authorised all topics 1227 // that the other side is trying to register. 1228 if rlpHash(data.Topics) != pongpkt.data.(*pong).TopicHash { 1229 return nil, errors.New("topic hash mismatch") 1230 } 1231 if data.Idx >= uint(len(data.Topics)) { 1232 return nil, errors.New("topic index out of range") 1233 } 1234 return pongpkt.data.(*pong), nil 1235 } 1236 1237 func rlpHash(x interface{}) (h common.Hash) { 1238 hw := sha3.NewKeccak256() 1239 rlp.Encode(hw, x) 1240 hw.Sum(h[:0]) 1241 return h 1242 } 1243 1244 func (net *Network) handleNeighboursPacket(n *Node, pkt *ingressPacket) error { 1245 if n.pendingNeighbours == nil { 1246 return errNoQuery 1247 } 1248 net.abortTimedEvent(n, neighboursTimeout) 1249 1250 req := pkt.data.(*neighbors) 1251 nodes := make([]*Node, len(req.Nodes)) 1252 for i, rn := range req.Nodes { 1253 nn, err := net.internNodeFromNeighbours(pkt.remoteAddr, rn) 1254 if err != nil { 1255 log.Debug(fmt.Sprintf("invalid neighbour (%v) from %x@%v: %v", rn.IP, n.ID[:8], pkt.remoteAddr, err)) 1256 continue 1257 } 1258 nodes[i] = nn 1259 // Start validation of query results immediately. 1260 // This fills the table quickly. 1261 // TODO: generates way too many packets, maybe do it via queue. 1262 if nn.state == unknown { 1263 net.transition(nn, verifyinit) 1264 } 1265 } 1266 // TODO: don't ignore second packet 1267 n.pendingNeighbours.reply <- nodes 1268 n.pendingNeighbours = nil 1269 // Now that this query is done, start the next one. 1270 n.startNextQuery(net) 1271 return nil 1272 }