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