github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/p2p/discover/table.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 // Package discover implements the Node Discovery Protocol. 19 // 20 // The Node Discovery protocol provides a way to find RLPx nodes that 21 // can be connected to. It uses a Kademlia-like protocol to maintain a 22 // distributed database of the IDs and endpoints of all listening 23 // nodes. 24 package discover 25 26 import ( 27 crand "crypto/rand" 28 "encoding/binary" 29 "fmt" 30 mrand "math/rand" 31 "net" 32 "sort" 33 "sync" 34 "time" 35 36 "github.com/AigarNetwork/aigar/common" 37 "github.com/AigarNetwork/aigar/log" 38 "github.com/AigarNetwork/aigar/p2p/enode" 39 "github.com/AigarNetwork/aigar/p2p/netutil" 40 ) 41 42 const ( 43 alpha = 3 // Kademlia concurrency factor 44 bucketSize = 16 // Kademlia bucket size 45 maxReplacements = 10 // Size of per-bucket replacement list 46 47 // We keep buckets for the upper 1/15 of distances because 48 // it's very unlikely we'll ever encounter a node that's closer. 49 hashBits = len(common.Hash{}) * 8 50 nBuckets = hashBits / 15 // Number of buckets 51 bucketMinDistance = hashBits - nBuckets // Log distance of closest bucket 52 53 // IP address limits. 54 bucketIPLimit, bucketSubnet = 2, 24 // at most 2 addresses from the same /24 55 tableIPLimit, tableSubnet = 10, 24 56 57 refreshInterval = 30 * time.Minute 58 revalidateInterval = 10 * time.Second 59 copyNodesInterval = 30 * time.Second 60 seedMinTableTime = 5 * time.Minute 61 seedCount = 30 62 seedMaxAge = 5 * 24 * time.Hour 63 ) 64 65 // Table is the 'node table', a Kademlia-like index of neighbor nodes. The table keeps 66 // itself up-to-date by verifying the liveness of neighbors and requesting their node 67 // records when announcements of a new record version are received. 68 type Table struct { 69 mutex sync.Mutex // protects buckets, bucket content, nursery, rand 70 buckets [nBuckets]*bucket // index of known nodes by distance 71 nursery []*node // bootstrap nodes 72 rand *mrand.Rand // source of randomness, periodically reseeded 73 ips netutil.DistinctNetSet 74 75 log log.Logger 76 db *enode.DB // database of known nodes 77 net transport 78 refreshReq chan chan struct{} 79 initDone chan struct{} 80 closeReq chan struct{} 81 closed chan struct{} 82 83 nodeAddedHook func(*node) // for testing 84 } 85 86 // transport is implemented by the UDP transports. 87 type transport interface { 88 Self() *enode.Node 89 RequestENR(*enode.Node) (*enode.Node, error) 90 lookupRandom() []*enode.Node 91 lookupSelf() []*enode.Node 92 ping(*enode.Node) (seq uint64, err error) 93 } 94 95 // bucket contains nodes, ordered by their last activity. the entry 96 // that was most recently active is the first element in entries. 97 type bucket struct { 98 entries []*node // live entries, sorted by time of last contact 99 replacements []*node // recently seen nodes to be used if revalidation fails 100 ips netutil.DistinctNetSet 101 } 102 103 func newTable(t transport, db *enode.DB, bootnodes []*enode.Node, log log.Logger) (*Table, error) { 104 tab := &Table{ 105 net: t, 106 db: db, 107 refreshReq: make(chan chan struct{}), 108 initDone: make(chan struct{}), 109 closeReq: make(chan struct{}), 110 closed: make(chan struct{}), 111 rand: mrand.New(mrand.NewSource(0)), 112 ips: netutil.DistinctNetSet{Subnet: tableSubnet, Limit: tableIPLimit}, 113 log: log, 114 } 115 if err := tab.setFallbackNodes(bootnodes); err != nil { 116 return nil, err 117 } 118 for i := range tab.buckets { 119 tab.buckets[i] = &bucket{ 120 ips: netutil.DistinctNetSet{Subnet: bucketSubnet, Limit: bucketIPLimit}, 121 } 122 } 123 tab.seedRand() 124 tab.loadSeedNodes() 125 126 return tab, nil 127 } 128 129 func (tab *Table) self() *enode.Node { 130 return tab.net.Self() 131 } 132 133 func (tab *Table) seedRand() { 134 var b [8]byte 135 crand.Read(b[:]) 136 137 tab.mutex.Lock() 138 tab.rand.Seed(int64(binary.BigEndian.Uint64(b[:]))) 139 tab.mutex.Unlock() 140 } 141 142 // ReadRandomNodes fills the given slice with random nodes from the table. The results 143 // are guaranteed to be unique for a single invocation, no node will appear twice. 144 func (tab *Table) ReadRandomNodes(buf []*enode.Node) (n int) { 145 if !tab.isInitDone() { 146 return 0 147 } 148 tab.mutex.Lock() 149 defer tab.mutex.Unlock() 150 151 var nodes []*enode.Node 152 for _, b := range &tab.buckets { 153 for _, n := range b.entries { 154 nodes = append(nodes, unwrapNode(n)) 155 } 156 } 157 // Shuffle. 158 for i := 0; i < len(nodes); i++ { 159 j := tab.rand.Intn(len(nodes)) 160 nodes[i], nodes[j] = nodes[j], nodes[i] 161 } 162 return copy(buf, nodes) 163 } 164 165 // getNode returns the node with the given ID or nil if it isn't in the table. 166 func (tab *Table) getNode(id enode.ID) *enode.Node { 167 tab.mutex.Lock() 168 defer tab.mutex.Unlock() 169 170 b := tab.bucket(id) 171 for _, e := range b.entries { 172 if e.ID() == id { 173 return unwrapNode(e) 174 } 175 } 176 return nil 177 } 178 179 // close terminates the network listener and flushes the node database. 180 func (tab *Table) close() { 181 close(tab.closeReq) 182 <-tab.closed 183 } 184 185 // setFallbackNodes sets the initial points of contact. These nodes 186 // are used to connect to the network if the table is empty and there 187 // are no known nodes in the database. 188 func (tab *Table) setFallbackNodes(nodes []*enode.Node) error { 189 for _, n := range nodes { 190 if err := n.ValidateComplete(); err != nil { 191 return fmt.Errorf("bad bootstrap node %q: %v", n, err) 192 } 193 } 194 tab.nursery = wrapNodes(nodes) 195 return nil 196 } 197 198 // isInitDone returns whether the table's initial seeding procedure has completed. 199 func (tab *Table) isInitDone() bool { 200 select { 201 case <-tab.initDone: 202 return true 203 default: 204 return false 205 } 206 } 207 208 func (tab *Table) refresh() <-chan struct{} { 209 done := make(chan struct{}) 210 select { 211 case tab.refreshReq <- done: 212 case <-tab.closeReq: 213 close(done) 214 } 215 return done 216 } 217 218 // loop schedules runs of doRefresh, doRevalidate and copyLiveNodes. 219 func (tab *Table) loop() { 220 var ( 221 revalidate = time.NewTimer(tab.nextRevalidateTime()) 222 refresh = time.NewTicker(refreshInterval) 223 copyNodes = time.NewTicker(copyNodesInterval) 224 refreshDone = make(chan struct{}) // where doRefresh reports completion 225 revalidateDone chan struct{} // where doRevalidate reports completion 226 waiting = []chan struct{}{tab.initDone} // holds waiting callers while doRefresh runs 227 ) 228 defer refresh.Stop() 229 defer revalidate.Stop() 230 defer copyNodes.Stop() 231 232 // Start initial refresh. 233 go tab.doRefresh(refreshDone) 234 235 loop: 236 for { 237 select { 238 case <-refresh.C: 239 tab.seedRand() 240 if refreshDone == nil { 241 refreshDone = make(chan struct{}) 242 go tab.doRefresh(refreshDone) 243 } 244 case req := <-tab.refreshReq: 245 waiting = append(waiting, req) 246 if refreshDone == nil { 247 refreshDone = make(chan struct{}) 248 go tab.doRefresh(refreshDone) 249 } 250 case <-refreshDone: 251 for _, ch := range waiting { 252 close(ch) 253 } 254 waiting, refreshDone = nil, nil 255 case <-revalidate.C: 256 revalidateDone = make(chan struct{}) 257 go tab.doRevalidate(revalidateDone) 258 case <-revalidateDone: 259 revalidate.Reset(tab.nextRevalidateTime()) 260 revalidateDone = nil 261 case <-copyNodes.C: 262 go tab.copyLiveNodes() 263 case <-tab.closeReq: 264 break loop 265 } 266 } 267 268 if refreshDone != nil { 269 <-refreshDone 270 } 271 for _, ch := range waiting { 272 close(ch) 273 } 274 if revalidateDone != nil { 275 <-revalidateDone 276 } 277 close(tab.closed) 278 } 279 280 // doRefresh performs a lookup for a random target to keep buckets full. seed nodes are 281 // inserted if the table is empty (initial bootstrap or discarded faulty peers). 282 func (tab *Table) doRefresh(done chan struct{}) { 283 defer close(done) 284 285 // Load nodes from the database and insert 286 // them. This should yield a few previously seen nodes that are 287 // (hopefully) still alive. 288 tab.loadSeedNodes() 289 290 // Run self lookup to discover new neighbor nodes. 291 tab.net.lookupSelf() 292 293 // The Kademlia paper specifies that the bucket refresh should 294 // perform a lookup in the least recently used bucket. We cannot 295 // adhere to this because the findnode target is a 512bit value 296 // (not hash-sized) and it is not easily possible to generate a 297 // sha3 preimage that falls into a chosen bucket. 298 // We perform a few lookups with a random target instead. 299 for i := 0; i < 3; i++ { 300 tab.net.lookupRandom() 301 } 302 } 303 304 func (tab *Table) loadSeedNodes() { 305 seeds := wrapNodes(tab.db.QuerySeeds(seedCount, seedMaxAge)) 306 seeds = append(seeds, tab.nursery...) 307 for i := range seeds { 308 seed := seeds[i] 309 age := log.Lazy{Fn: func() interface{} { return time.Since(tab.db.LastPongReceived(seed.ID(), seed.IP())) }} 310 tab.log.Trace("Found seed node in database", "id", seed.ID(), "addr", seed.addr(), "age", age) 311 tab.addSeenNode(seed) 312 } 313 } 314 315 // doRevalidate checks that the last node in a random bucket is still live and replaces or 316 // deletes the node if it isn't. 317 func (tab *Table) doRevalidate(done chan<- struct{}) { 318 defer func() { done <- struct{}{} }() 319 320 last, bi := tab.nodeToRevalidate() 321 if last == nil { 322 // No non-empty bucket found. 323 return 324 } 325 326 // Ping the selected node and wait for a pong. 327 remoteSeq, err := tab.net.ping(unwrapNode(last)) 328 329 // Also fetch record if the node replied and returned a higher sequence number. 330 if last.Seq() < remoteSeq { 331 n, err := tab.net.RequestENR(unwrapNode(last)) 332 if err != nil { 333 tab.log.Debug("ENR request failed", "id", last.ID(), "addr", last.addr(), "err", err) 334 } else { 335 last = &node{Node: *n, addedAt: last.addedAt, livenessChecks: last.livenessChecks} 336 } 337 } 338 339 tab.mutex.Lock() 340 defer tab.mutex.Unlock() 341 b := tab.buckets[bi] 342 if err == nil { 343 // The node responded, move it to the front. 344 last.livenessChecks++ 345 tab.log.Debug("Revalidated node", "b", bi, "id", last.ID(), "checks", last.livenessChecks) 346 tab.bumpInBucket(b, last) 347 return 348 } 349 // No reply received, pick a replacement or delete the node if there aren't 350 // any replacements. 351 if r := tab.replace(b, last); r != nil { 352 tab.log.Debug("Replaced dead node", "b", bi, "id", last.ID(), "ip", last.IP(), "checks", last.livenessChecks, "r", r.ID(), "rip", r.IP()) 353 } else { 354 tab.log.Debug("Removed dead node", "b", bi, "id", last.ID(), "ip", last.IP(), "checks", last.livenessChecks) 355 } 356 } 357 358 // nodeToRevalidate returns the last node in a random, non-empty bucket. 359 func (tab *Table) nodeToRevalidate() (n *node, bi int) { 360 tab.mutex.Lock() 361 defer tab.mutex.Unlock() 362 363 for _, bi = range tab.rand.Perm(len(tab.buckets)) { 364 b := tab.buckets[bi] 365 if len(b.entries) > 0 { 366 last := b.entries[len(b.entries)-1] 367 return last, bi 368 } 369 } 370 return nil, 0 371 } 372 373 func (tab *Table) nextRevalidateTime() time.Duration { 374 tab.mutex.Lock() 375 defer tab.mutex.Unlock() 376 377 return time.Duration(tab.rand.Int63n(int64(revalidateInterval))) 378 } 379 380 // copyLiveNodes adds nodes from the table to the database if they have been in the table 381 // longer then minTableTime. 382 func (tab *Table) copyLiveNodes() { 383 tab.mutex.Lock() 384 defer tab.mutex.Unlock() 385 386 now := time.Now() 387 for _, b := range &tab.buckets { 388 for _, n := range b.entries { 389 if n.livenessChecks > 0 && now.Sub(n.addedAt) >= seedMinTableTime { 390 tab.db.UpdateNode(unwrapNode(n)) 391 } 392 } 393 } 394 } 395 396 // closest returns the n nodes in the table that are closest to the 397 // given id. The caller must hold tab.mutex. 398 func (tab *Table) closest(target enode.ID, nresults int, checklive bool) *nodesByDistance { 399 // This is a very wasteful way to find the closest nodes but 400 // obviously correct. I believe that tree-based buckets would make 401 // this easier to implement efficiently. 402 close := &nodesByDistance{target: target} 403 for _, b := range &tab.buckets { 404 for _, n := range b.entries { 405 if checklive && n.livenessChecks == 0 { 406 continue 407 } 408 close.push(n, nresults) 409 } 410 } 411 return close 412 } 413 414 // len returns the number of nodes in the table. 415 func (tab *Table) len() (n int) { 416 tab.mutex.Lock() 417 defer tab.mutex.Unlock() 418 419 for _, b := range &tab.buckets { 420 n += len(b.entries) 421 } 422 return n 423 } 424 425 // bucket returns the bucket for the given node ID hash. 426 func (tab *Table) bucket(id enode.ID) *bucket { 427 d := enode.LogDist(tab.self().ID(), id) 428 if d <= bucketMinDistance { 429 return tab.buckets[0] 430 } 431 return tab.buckets[d-bucketMinDistance-1] 432 } 433 434 // addSeenNode adds a node which may or may not be live to the end of a bucket. If the 435 // bucket has space available, adding the node succeeds immediately. Otherwise, the node is 436 // added to the replacements list. 437 // 438 // The caller must not hold tab.mutex. 439 func (tab *Table) addSeenNode(n *node) { 440 if n.ID() == tab.self().ID() { 441 return 442 } 443 444 tab.mutex.Lock() 445 defer tab.mutex.Unlock() 446 b := tab.bucket(n.ID()) 447 if contains(b.entries, n.ID()) { 448 // Already in bucket, don't add. 449 return 450 } 451 if len(b.entries) >= bucketSize { 452 // Bucket full, maybe add as replacement. 453 tab.addReplacement(b, n) 454 return 455 } 456 if !tab.addIP(b, n.IP()) { 457 // Can't add: IP limit reached. 458 return 459 } 460 // Add to end of bucket: 461 b.entries = append(b.entries, n) 462 b.replacements = deleteNode(b.replacements, n) 463 n.addedAt = time.Now() 464 if tab.nodeAddedHook != nil { 465 tab.nodeAddedHook(n) 466 } 467 } 468 469 // addVerifiedNode adds a node whose existence has been verified recently to the front of a 470 // bucket. If the node is already in the bucket, it is moved to the front. If the bucket 471 // has no space, the node is added to the replacements list. 472 // 473 // There is an additional safety measure: if the table is still initializing the node 474 // is not added. This prevents an attack where the table could be filled by just sending 475 // ping repeatedly. 476 // 477 // The caller must not hold tab.mutex. 478 func (tab *Table) addVerifiedNode(n *node) { 479 if !tab.isInitDone() { 480 return 481 } 482 if n.ID() == tab.self().ID() { 483 return 484 } 485 486 tab.mutex.Lock() 487 defer tab.mutex.Unlock() 488 b := tab.bucket(n.ID()) 489 if tab.bumpInBucket(b, n) { 490 // Already in bucket, moved to front. 491 return 492 } 493 if len(b.entries) >= bucketSize { 494 // Bucket full, maybe add as replacement. 495 tab.addReplacement(b, n) 496 return 497 } 498 if !tab.addIP(b, n.IP()) { 499 // Can't add: IP limit reached. 500 return 501 } 502 // Add to front of bucket. 503 b.entries, _ = pushNode(b.entries, n, bucketSize) 504 b.replacements = deleteNode(b.replacements, n) 505 n.addedAt = time.Now() 506 if tab.nodeAddedHook != nil { 507 tab.nodeAddedHook(n) 508 } 509 } 510 511 // delete removes an entry from the node table. It is used to evacuate dead nodes. 512 func (tab *Table) delete(node *node) { 513 tab.mutex.Lock() 514 defer tab.mutex.Unlock() 515 516 tab.deleteInBucket(tab.bucket(node.ID()), node) 517 } 518 519 func (tab *Table) addIP(b *bucket, ip net.IP) bool { 520 if netutil.IsLAN(ip) { 521 return true 522 } 523 if !tab.ips.Add(ip) { 524 tab.log.Debug("IP exceeds table limit", "ip", ip) 525 return false 526 } 527 if !b.ips.Add(ip) { 528 tab.log.Debug("IP exceeds bucket limit", "ip", ip) 529 tab.ips.Remove(ip) 530 return false 531 } 532 return true 533 } 534 535 func (tab *Table) removeIP(b *bucket, ip net.IP) { 536 if netutil.IsLAN(ip) { 537 return 538 } 539 tab.ips.Remove(ip) 540 b.ips.Remove(ip) 541 } 542 543 func (tab *Table) addReplacement(b *bucket, n *node) { 544 for _, e := range b.replacements { 545 if e.ID() == n.ID() { 546 return // already in list 547 } 548 } 549 if !tab.addIP(b, n.IP()) { 550 return 551 } 552 var removed *node 553 b.replacements, removed = pushNode(b.replacements, n, maxReplacements) 554 if removed != nil { 555 tab.removeIP(b, removed.IP()) 556 } 557 } 558 559 // replace removes n from the replacement list and replaces 'last' with it if it is the 560 // last entry in the bucket. If 'last' isn't the last entry, it has either been replaced 561 // with someone else or became active. 562 func (tab *Table) replace(b *bucket, last *node) *node { 563 if len(b.entries) == 0 || b.entries[len(b.entries)-1].ID() != last.ID() { 564 // Entry has moved, don't replace it. 565 return nil 566 } 567 // Still the last entry. 568 if len(b.replacements) == 0 { 569 tab.deleteInBucket(b, last) 570 return nil 571 } 572 r := b.replacements[tab.rand.Intn(len(b.replacements))] 573 b.replacements = deleteNode(b.replacements, r) 574 b.entries[len(b.entries)-1] = r 575 tab.removeIP(b, last.IP()) 576 return r 577 } 578 579 // bumpInBucket moves the given node to the front of the bucket entry list 580 // if it is contained in that list. 581 func (tab *Table) bumpInBucket(b *bucket, n *node) bool { 582 for i := range b.entries { 583 if b.entries[i].ID() == n.ID() { 584 if !n.IP().Equal(b.entries[i].IP()) { 585 // Endpoint has changed, ensure that the new IP fits into table limits. 586 tab.removeIP(b, b.entries[i].IP()) 587 if !tab.addIP(b, n.IP()) { 588 // It doesn't, put the previous one back. 589 tab.addIP(b, b.entries[i].IP()) 590 return false 591 } 592 } 593 // Move it to the front. 594 copy(b.entries[1:], b.entries[:i]) 595 b.entries[0] = n 596 return true 597 } 598 } 599 return false 600 } 601 602 func (tab *Table) deleteInBucket(b *bucket, n *node) { 603 b.entries = deleteNode(b.entries, n) 604 tab.removeIP(b, n.IP()) 605 } 606 607 func contains(ns []*node, id enode.ID) bool { 608 for _, n := range ns { 609 if n.ID() == id { 610 return true 611 } 612 } 613 return false 614 } 615 616 // pushNode adds n to the front of list, keeping at most max items. 617 func pushNode(list []*node, n *node, max int) ([]*node, *node) { 618 if len(list) < max { 619 list = append(list, nil) 620 } 621 removed := list[len(list)-1] 622 copy(list[1:], list) 623 list[0] = n 624 return list, removed 625 } 626 627 // deleteNode removes n from list. 628 func deleteNode(list []*node, n *node) []*node { 629 for i := range list { 630 if list[i].ID() == n.ID() { 631 return append(list[:i], list[i+1:]...) 632 } 633 } 634 return list 635 } 636 637 // nodesByDistance is a list of nodes, ordered by distance to target. 638 type nodesByDistance struct { 639 entries []*node 640 target enode.ID 641 } 642 643 // push adds the given node to the list, keeping the total size below maxElems. 644 func (h *nodesByDistance) push(n *node, maxElems int) { 645 ix := sort.Search(len(h.entries), func(i int) bool { 646 return enode.DistCmp(h.target, h.entries[i].ID(), n.ID()) > 0 647 }) 648 if len(h.entries) < maxElems { 649 h.entries = append(h.entries, n) 650 } 651 if ix == len(h.entries) { 652 // farther away than all nodes we already have. 653 // if there was room for it, the node is now the last element. 654 } else { 655 // slide existing entries down to make room 656 // this will overwrite the entry we just appended. 657 copy(h.entries[ix+1:], h.entries[ix:]) 658 h.entries[ix] = n 659 } 660 }