2018-03-07 02:15:44 +01:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
2018-03-09 01:50:18 +01:00
|
|
|
"bytes"
|
2018-05-19 19:05:30 +02:00
|
|
|
"encoding/json"
|
2018-03-24 00:18:00 +01:00
|
|
|
"fmt"
|
2018-03-09 01:50:18 +01:00
|
|
|
"net"
|
2018-03-07 02:15:44 +01:00
|
|
|
"sort"
|
2018-05-19 19:05:30 +02:00
|
|
|
"strconv"
|
2018-03-24 00:18:00 +01:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2018-05-13 22:02:46 +02:00
|
|
|
"time"
|
2018-03-09 01:50:18 +01:00
|
|
|
|
2018-05-24 23:49:43 +02:00
|
|
|
"github.com/lbryio/lbry.go/errors"
|
2018-03-09 01:50:18 +01:00
|
|
|
|
2018-03-24 00:18:00 +01:00
|
|
|
"github.com/lyoshenka/bencode"
|
2018-05-30 03:38:55 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-03-07 02:15:44 +01:00
|
|
|
)
|
|
|
|
|
2018-05-22 18:16:01 +02:00
|
|
|
// TODO: if routing table is ever empty (aka the node is isolated), it should re-bootstrap
|
|
|
|
|
|
|
|
// TODO: use a tree with bucket splitting instead of a fixed bucket list. include jack's optimization (see link in commit mesg)
|
|
|
|
// https://github.com/lbryio/lbry/pull/1211/commits/341b27b6d21ac027671d42458826d02735aaae41
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Contact is a type representation of another node that a specific node is in communication with.
|
2018-04-28 02:16:12 +02:00
|
|
|
type Contact struct {
|
2018-05-19 19:05:30 +02:00
|
|
|
ID Bitmap
|
|
|
|
IP net.IP
|
|
|
|
Port int
|
2018-04-05 22:05:28 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Equals returns T/F if two contacts are the same.
|
2018-05-22 18:27:49 +02:00
|
|
|
func (c Contact) Equals(other Contact) bool {
|
|
|
|
return c.ID == other.ID
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Addr returns the UPD Address of the contact.
|
2018-04-28 02:16:12 +02:00
|
|
|
func (c Contact) Addr() *net.UDPAddr {
|
2018-05-19 19:05:30 +02:00
|
|
|
return &net.UDPAddr{IP: c.IP, Port: c.Port}
|
2018-03-09 01:50:18 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// String returns the concatenated short hex encoded string of its ID + @ + string represention of its UPD Address.
|
2018-04-28 02:16:12 +02:00
|
|
|
func (c Contact) String() string {
|
2018-05-19 19:05:30 +02:00
|
|
|
return c.ID.HexShort() + "@" + c.Addr().String()
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// MarshalCompact returns the compact byte slice representation of a contact.
|
2018-04-28 02:16:12 +02:00
|
|
|
func (c Contact) MarshalCompact() ([]byte, error) {
|
2018-05-19 19:05:30 +02:00
|
|
|
if c.IP.To4() == nil {
|
2018-03-09 01:50:18 +01:00
|
|
|
return nil, errors.Err("ip not set")
|
|
|
|
}
|
2018-05-19 19:05:30 +02:00
|
|
|
if c.Port < 0 || c.Port > 65535 {
|
2018-03-09 01:50:18 +01:00
|
|
|
return nil, errors.Err("invalid port")
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2018-05-19 19:05:30 +02:00
|
|
|
buf.Write(c.IP.To4())
|
|
|
|
buf.WriteByte(byte(c.Port >> 8))
|
|
|
|
buf.WriteByte(byte(c.Port))
|
|
|
|
buf.Write(c.ID[:])
|
2018-03-09 01:50:18 +01:00
|
|
|
|
2018-03-09 22:43:30 +01:00
|
|
|
if buf.Len() != compactNodeInfoLength {
|
2018-03-09 01:50:18 +01:00
|
|
|
return nil, errors.Err("i dont know how this happened")
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// UnmarshalCompact unmarshals the compact byte slice representation of a contact.
|
2018-04-28 02:16:12 +02:00
|
|
|
func (c *Contact) UnmarshalCompact(b []byte) error {
|
2018-03-09 22:43:30 +01:00
|
|
|
if len(b) != compactNodeInfoLength {
|
|
|
|
return errors.Err("invalid compact length")
|
2018-03-09 01:50:18 +01:00
|
|
|
}
|
2018-05-19 19:05:30 +02:00
|
|
|
c.IP = net.IPv4(b[0], b[1], b[2], b[3]).To4()
|
|
|
|
c.Port = int(uint16(b[5]) | uint16(b[4])<<8)
|
|
|
|
c.ID = BitmapFromBytesP(b[6:])
|
2018-03-09 01:50:18 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// MarshalBencode returns the serialized byte slice representation of a contact.
|
2018-04-28 02:16:12 +02:00
|
|
|
func (c Contact) MarshalBencode() ([]byte, error) {
|
2018-05-19 19:05:30 +02:00
|
|
|
return bencode.EncodeBytes([]interface{}{c.ID, c.IP.String(), c.Port})
|
2018-03-09 01:50:18 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// UnmarshalBencode unmarshals the serialized byte slice into the appropriate fields of the contact.
|
2018-04-28 02:16:12 +02:00
|
|
|
func (c *Contact) UnmarshalBencode(b []byte) error {
|
2018-03-09 01:50:18 +01:00
|
|
|
var raw []bencode.RawMessage
|
|
|
|
err := bencode.DecodeBytes(b, &raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(raw) != 3 {
|
|
|
|
return errors.Err("contact must have 3 elements; got %d", len(raw))
|
|
|
|
}
|
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
err = bencode.DecodeBytes(raw[0], &c.ID)
|
2018-03-09 01:50:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ipStr string
|
|
|
|
err = bencode.DecodeBytes(raw[1], &ipStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-19 19:05:30 +02:00
|
|
|
c.IP = net.ParseIP(ipStr).To4()
|
|
|
|
if c.IP == nil {
|
2018-03-09 01:50:18 +01:00
|
|
|
return errors.Err("invalid IP")
|
|
|
|
}
|
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
err = bencode.DecodeBytes(raw[2], &c.Port)
|
2018-03-09 01:50:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-28 02:16:12 +02:00
|
|
|
type sortedContact struct {
|
|
|
|
contact Contact
|
2018-04-05 22:05:28 +02:00
|
|
|
xorDistanceToTarget Bitmap
|
2018-03-09 01:50:18 +01:00
|
|
|
}
|
|
|
|
|
2018-04-28 02:16:12 +02:00
|
|
|
type byXorDistance []sortedContact
|
2018-03-09 01:50:18 +01:00
|
|
|
|
|
|
|
func (a byXorDistance) Len() int { return len(a) }
|
|
|
|
func (a byXorDistance) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a byXorDistance) Less(i, j int) bool {
|
|
|
|
return a[i].xorDistanceToTarget.Less(a[j].xorDistanceToTarget)
|
|
|
|
}
|
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
// peer is a contact with extra freshness information
|
|
|
|
type peer struct {
|
2018-05-19 19:05:30 +02:00
|
|
|
Contact Contact
|
|
|
|
LastActivity time.Time
|
|
|
|
NumFailures int
|
2018-05-13 22:02:46 +02:00
|
|
|
//<lastPublished>,
|
|
|
|
//<originallyPublished>
|
|
|
|
// <originalPublisherID>
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
func (p *peer) Touch() {
|
2018-05-19 19:05:30 +02:00
|
|
|
p.LastActivity = time.Now()
|
|
|
|
p.NumFailures = 0
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
// ActiveSince returns whether a peer has responded in the last `d` duration
|
|
|
|
// this is used to check if the peer is "good", meaning that we believe the peer will respond to our requests
|
|
|
|
func (p *peer) ActiveInLast(d time.Duration) bool {
|
2018-05-30 03:38:55 +02:00
|
|
|
return time.Since(p.LastActivity) > d
|
2018-05-13 22:02:46 +02:00
|
|
|
}
|
2018-03-24 00:18:00 +01:00
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
// IsBad returns whether a peer is "bad", meaning that it has failed to respond to multiple pings in a row
|
|
|
|
func (p *peer) IsBad(maxFalures int) bool {
|
2018-05-19 19:05:30 +02:00
|
|
|
return p.NumFailures >= maxFalures
|
2018-05-13 22:02:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fail marks a peer as having failed to respond. It returns whether or not the peer should be removed from the routing table
|
|
|
|
func (p *peer) Fail() {
|
2018-05-19 19:05:30 +02:00
|
|
|
p.NumFailures++
|
2018-05-13 22:02:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type bucket struct {
|
|
|
|
lock *sync.RWMutex
|
2018-05-19 19:05:30 +02:00
|
|
|
peers []peer
|
2018-05-13 22:02:46 +02:00
|
|
|
lastUpdate time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of peers in the bucket
|
|
|
|
func (b bucket) Len() int {
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
2018-05-19 19:05:30 +02:00
|
|
|
return len(b.peers)
|
2018-05-13 22:02:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Contacts returns a slice of the bucket's contacts
|
|
|
|
func (b bucket) Contacts() []Contact {
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
2018-05-19 19:05:30 +02:00
|
|
|
contacts := make([]Contact, len(b.peers))
|
|
|
|
for i := range b.peers {
|
|
|
|
contacts[i] = b.peers[i].Contact
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
2018-05-13 22:02:46 +02:00
|
|
|
return contacts
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
// UpdateContact marks a contact as having been successfully contacted. if insertIfNew and the contact is does not exist yet, it is inserted
|
|
|
|
func (b *bucket) UpdateContact(c Contact, insertIfNew bool) {
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
peerIndex := find(c.ID, b.peers)
|
|
|
|
if peerIndex >= 0 {
|
2018-05-13 22:02:46 +02:00
|
|
|
b.lastUpdate = time.Now()
|
2018-05-19 19:05:30 +02:00
|
|
|
b.peers[peerIndex].Touch()
|
|
|
|
moveToBack(b.peers, peerIndex)
|
2018-05-13 22:02:46 +02:00
|
|
|
|
|
|
|
} else if insertIfNew {
|
|
|
|
hasRoom := true
|
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
if len(b.peers) >= bucketSize {
|
2018-05-13 22:02:46 +02:00
|
|
|
hasRoom = false
|
2018-05-19 19:05:30 +02:00
|
|
|
for i := range b.peers {
|
|
|
|
if b.peers[i].IsBad(maxPeerFails) {
|
2018-05-13 22:02:46 +02:00
|
|
|
// TODO: Ping contact first. Only remove if it does not respond
|
2018-05-19 19:05:30 +02:00
|
|
|
b.peers = append(b.peers[:i], b.peers[i+1:]...)
|
2018-05-13 22:02:46 +02:00
|
|
|
hasRoom = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasRoom {
|
|
|
|
b.lastUpdate = time.Now()
|
2018-05-19 19:05:30 +02:00
|
|
|
peer := peer{Contact: c}
|
2018-05-13 22:02:46 +02:00
|
|
|
peer.Touch()
|
2018-05-19 19:05:30 +02:00
|
|
|
b.peers = append(b.peers, peer)
|
2018-04-05 22:05:28 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-13 22:02:46 +02:00
|
|
|
}
|
2018-04-05 22:05:28 +02:00
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
// FailContact marks a contact as having failed, and removes it if it failed too many times
|
|
|
|
func (b *bucket) FailContact(id Bitmap) {
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
2018-05-19 19:05:30 +02:00
|
|
|
i := find(id, b.peers)
|
|
|
|
if i >= 0 {
|
2018-05-13 22:02:46 +02:00
|
|
|
// BEP5 says not to remove the contact until the bucket is full and you try to insert
|
2018-05-19 19:05:30 +02:00
|
|
|
b.peers[i].Fail()
|
2018-04-05 22:05:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
// find returns the contact in the bucket, or nil if the bucket does not contain the contact
|
2018-05-19 19:05:30 +02:00
|
|
|
func find(id Bitmap, peers []peer) int {
|
|
|
|
for i := range peers {
|
|
|
|
if peers[i].Contact.ID.Equals(id) {
|
|
|
|
return i
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
2018-04-25 00:12:17 +02:00
|
|
|
}
|
2018-05-19 19:05:30 +02:00
|
|
|
return -1
|
2018-04-25 00:12:17 +02:00
|
|
|
}
|
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
// NeedsRefresh returns true if bucket has not been updated in the last `refreshInterval`, false otherwise
|
|
|
|
func (b *bucket) NeedsRefresh(refreshInterval time.Duration) bool {
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
2018-05-30 03:38:55 +02:00
|
|
|
return time.Since(b.lastUpdate) > refreshInterval
|
2018-05-13 22:02:46 +02:00
|
|
|
}
|
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
type routingTable struct {
|
2018-05-13 22:02:46 +02:00
|
|
|
id Bitmap
|
2018-05-19 19:05:30 +02:00
|
|
|
buckets [nodeIDBits]bucket
|
2018-05-13 22:02:46 +02:00
|
|
|
}
|
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
func newRoutingTable(id Bitmap) *routingTable {
|
|
|
|
var rt routingTable
|
2018-05-13 22:02:46 +02:00
|
|
|
rt.id = id
|
|
|
|
for i := range rt.buckets {
|
|
|
|
rt.buckets[i] = bucket{
|
2018-05-19 19:05:30 +02:00
|
|
|
peers: make([]peer, 0, bucketSize),
|
2018-05-13 22:02:46 +02:00
|
|
|
lock: &sync.RWMutex{},
|
|
|
|
}
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
2018-05-13 22:02:46 +02:00
|
|
|
return &rt
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
func (rt *routingTable) BucketInfo() string {
|
2018-05-13 22:02:46 +02:00
|
|
|
var bucketInfo []string
|
|
|
|
for i, b := range rt.buckets {
|
|
|
|
if b.Len() > 0 {
|
|
|
|
contacts := b.Contacts()
|
|
|
|
s := make([]string, len(contacts))
|
|
|
|
for j, c := range contacts {
|
2018-05-19 19:05:30 +02:00
|
|
|
s[j] = c.ID.HexShort()
|
2018-05-13 22:02:46 +02:00
|
|
|
}
|
|
|
|
bucketInfo = append(bucketInfo, fmt.Sprintf("Bucket %d: (%d) %s", i, len(contacts), strings.Join(s, ", ")))
|
|
|
|
}
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
2018-05-13 22:02:46 +02:00
|
|
|
if len(bucketInfo) == 0 {
|
|
|
|
return "buckets are empty"
|
|
|
|
}
|
|
|
|
return strings.Join(bucketInfo, "\n")
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
// Update inserts or refreshes a contact
|
2018-05-19 19:05:30 +02:00
|
|
|
func (rt *routingTable) Update(c Contact) {
|
|
|
|
rt.bucketFor(c.ID).UpdateContact(c, true)
|
2018-05-13 22:02:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fresh refreshes a contact if its already in the routing table
|
2018-05-19 19:05:30 +02:00
|
|
|
func (rt *routingTable) Fresh(c Contact) {
|
|
|
|
rt.bucketFor(c.ID).UpdateContact(c, false)
|
2018-05-13 22:02:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FailContact marks a contact as having failed, and removes it if it failed too many times
|
2018-05-19 19:05:30 +02:00
|
|
|
func (rt *routingTable) Fail(c Contact) {
|
|
|
|
rt.bucketFor(c.ID).FailContact(c.ID)
|
2018-05-13 22:02:46 +02:00
|
|
|
}
|
2018-03-24 00:18:00 +01:00
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
// GetClosest returns the closest `limit` contacts from the routing table
|
|
|
|
// It marks each bucket it accesses as having been accessed
|
2018-05-19 19:05:30 +02:00
|
|
|
func (rt *routingTable) GetClosest(target Bitmap, limit int) []Contact {
|
2018-04-28 02:16:12 +02:00
|
|
|
var toSort []sortedContact
|
2018-03-29 03:05:27 +02:00
|
|
|
var bucketNum int
|
|
|
|
|
2018-04-28 02:16:12 +02:00
|
|
|
if rt.id.Equals(target) {
|
2018-03-29 03:05:27 +02:00
|
|
|
bucketNum = 0
|
|
|
|
} else {
|
2018-05-13 22:02:46 +02:00
|
|
|
bucketNum = rt.bucketNumFor(target)
|
2018-03-29 03:05:27 +02:00
|
|
|
}
|
2018-03-07 02:15:44 +01:00
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
toSort = appendContacts(toSort, rt.buckets[bucketNum], target)
|
2018-03-07 02:15:44 +01:00
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
for i := 1; (bucketNum-i >= 0 || bucketNum+i < nodeIDBits) && len(toSort) < limit; i++ {
|
2018-03-24 00:18:00 +01:00
|
|
|
if bucketNum-i >= 0 {
|
2018-05-13 22:02:46 +02:00
|
|
|
toSort = appendContacts(toSort, rt.buckets[bucketNum-i], target)
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
2018-05-19 19:05:30 +02:00
|
|
|
if bucketNum+i < nodeIDBits {
|
2018-05-13 22:02:46 +02:00
|
|
|
toSort = appendContacts(toSort, rt.buckets[bucketNum+i], target)
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(byXorDistance(toSort))
|
|
|
|
|
2018-04-28 02:16:12 +02:00
|
|
|
var contacts []Contact
|
|
|
|
for _, sorted := range toSort {
|
|
|
|
contacts = append(contacts, sorted.contact)
|
|
|
|
if len(contacts) >= limit {
|
2018-03-24 00:18:00 +01:00
|
|
|
break
|
|
|
|
}
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-04-28 02:16:12 +02:00
|
|
|
return contacts
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
func appendContacts(contacts []sortedContact, b bucket, target Bitmap) []sortedContact {
|
|
|
|
for _, contact := range b.Contacts() {
|
2018-05-19 19:05:30 +02:00
|
|
|
contacts = append(contacts, sortedContact{contact, contact.ID.Xor(target)})
|
2018-05-01 22:18:38 +02:00
|
|
|
}
|
|
|
|
return contacts
|
|
|
|
}
|
|
|
|
|
2018-04-28 02:16:12 +02:00
|
|
|
// Count returns the number of contacts in the routing table
|
2018-05-19 19:05:30 +02:00
|
|
|
func (rt *routingTable) Count() int {
|
2018-04-25 03:12:32 +02:00
|
|
|
count := 0
|
|
|
|
for _, bucket := range rt.buckets {
|
2018-05-13 22:02:46 +02:00
|
|
|
count = bucket.Len()
|
2018-04-25 03:12:32 +02:00
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Range is a structure that holds a min and max bitmaps. The range is used in bucket sizing.
|
2018-05-19 19:05:30 +02:00
|
|
|
type Range struct {
|
|
|
|
start Bitmap
|
|
|
|
end Bitmap
|
|
|
|
}
|
|
|
|
|
|
|
|
// BucketRanges returns a slice of ranges, where the `start` of each range is the smallest id that can
|
|
|
|
// go in that bucket, and the `end` is the largest id
|
|
|
|
func (rt *routingTable) BucketRanges() []Range {
|
|
|
|
ranges := make([]Range, len(rt.buckets))
|
|
|
|
for i := range rt.buckets {
|
|
|
|
ranges[i] = Range{
|
|
|
|
rt.id.Suffix(i, false).Set(nodeIDBits-1-i, !rt.id.Get(nodeIDBits-1-i)),
|
|
|
|
rt.id.Suffix(i, true).Set(nodeIDBits-1-i, !rt.id.Get(nodeIDBits-1-i)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ranges
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *routingTable) bucketNumFor(target Bitmap) int {
|
2018-05-01 22:18:38 +02:00
|
|
|
if rt.id.Equals(target) {
|
|
|
|
panic("routing table does not have a bucket for its own id")
|
|
|
|
}
|
2018-05-19 19:05:30 +02:00
|
|
|
return nodeIDBits - 1 - target.Xor(rt.id).PrefixLen()
|
2018-05-01 22:18:38 +02:00
|
|
|
}
|
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
func (rt *routingTable) bucketFor(target Bitmap) *bucket {
|
2018-05-13 22:02:46 +02:00
|
|
|
return &rt.buckets[rt.bucketNumFor(target)]
|
|
|
|
}
|
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
func (rt *routingTable) GetIDsForRefresh(refreshInterval time.Duration) []Bitmap {
|
2018-05-13 22:02:46 +02:00
|
|
|
var bitmaps []Bitmap
|
|
|
|
for i, bucket := range rt.buckets {
|
|
|
|
if bucket.NeedsRefresh(refreshInterval) {
|
2018-05-19 19:05:30 +02:00
|
|
|
bitmaps = append(bitmaps, RandomBitmapP().Prefix(i, false))
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-13 22:02:46 +02:00
|
|
|
return bitmaps
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
const rtContactSep = "-"
|
|
|
|
|
|
|
|
type rtSave struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Contacts []string `json:"contacts"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *routingTable) MarshalJSON() ([]byte, error) {
|
|
|
|
var data rtSave
|
|
|
|
data.ID = rt.id.Hex()
|
|
|
|
for _, b := range rt.buckets {
|
|
|
|
for _, c := range b.Contacts() {
|
|
|
|
data.Contacts = append(data.Contacts, strings.Join([]string{c.ID.Hex(), c.IP.String(), strconv.Itoa(c.Port)}, rtContactSep))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return json.Marshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *routingTable) UnmarshalJSON(b []byte) error {
|
|
|
|
var data rtSave
|
|
|
|
err := json.Unmarshal(b, &data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rt.id, err = BitmapFromHex(data.ID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Prefix("decoding ID", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range data.Contacts {
|
|
|
|
parts := strings.Split(s, rtContactSep)
|
|
|
|
if len(parts) != 3 {
|
|
|
|
return errors.Err("decoding contact %s: wrong number of parts", s)
|
|
|
|
}
|
|
|
|
var c Contact
|
|
|
|
c.ID, err = BitmapFromHex(parts[0])
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err("decoding contact %s: invalid ID: %s", s, err)
|
|
|
|
}
|
|
|
|
c.IP = net.ParseIP(parts[1])
|
|
|
|
if c.IP == nil {
|
|
|
|
return errors.Err("decoding contact %s: invalid IP", s)
|
|
|
|
}
|
|
|
|
c.Port, err = strconv.Atoi(parts[2])
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err("decoding contact %s: invalid port: %s", s, err)
|
|
|
|
}
|
|
|
|
rt.Update(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-13 22:02:46 +02:00
|
|
|
// RoutingTableRefresh refreshes any buckets that need to be refreshed
|
|
|
|
// It returns a channel that will be closed when the refresh is done
|
|
|
|
func RoutingTableRefresh(n *Node, refreshInterval time.Duration, cancel <-chan struct{}) <-chan struct{} {
|
|
|
|
var wg sync.WaitGroup
|
2018-05-24 23:49:43 +02:00
|
|
|
done := make(chan struct{})
|
2018-05-13 22:02:46 +02:00
|
|
|
|
|
|
|
for _, id := range n.rt.GetIDsForRefresh(refreshInterval) {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(id Bitmap) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
nf := newContactFinder(n, id, false)
|
|
|
|
|
|
|
|
if cancel != nil {
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-cancel:
|
|
|
|
nf.Cancel()
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
if _, err := nf.Find(); err != nil {
|
|
|
|
log.Error("error finding contact during routing table refresh - ", err)
|
|
|
|
}
|
2018-05-13 22:02:46 +02:00
|
|
|
}(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return done
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
2018-05-19 19:05:30 +02:00
|
|
|
|
|
|
|
func moveToBack(peers []peer, index int) {
|
|
|
|
if index < 0 || len(peers) <= index+1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := peers[index]
|
|
|
|
for i := index; i < len(peers)-1; i++ {
|
|
|
|
peers[i] = peers[i+1]
|
|
|
|
}
|
|
|
|
peers[len(peers)-1] = p
|
|
|
|
}
|