bucket.go 3.16 KB
Newer Older
1 2
//go:generate go run ./generate

3
package kbucket
4 5 6

import (
	"container/list"
7
	"time"
Jeromy's avatar
Jeromy committed
8

9
	"github.com/libp2p/go-libp2p-core/peer"
10 11
)

Aarsh Shah's avatar
Aarsh Shah committed
12 13
// PeerInfo holds all related information for a peer in the K-Bucket.
type PeerInfo struct {
14
	Id peer.ID
Aarsh Shah's avatar
Aarsh Shah committed
15
	// LastSuccessfulOutboundQuery is the time instant when we last made a successful
16
	// outbound query to this peer
Aarsh Shah's avatar
Aarsh Shah committed
17
	LastSuccessfulOutboundQuery time.Time
18 19 20

	// Id of the peer in the DHT XOR keyspace
	dhtId ID
21 22 23
}

// bucket holds a list of peers.
Aarsh Shah's avatar
Aarsh Shah committed
24 25 26 27
// we synchronize on the Routing Table lock for all access to the bucket
// and so do not need any locks in the bucket.
// if we want/need to avoid locking the table for accessing a bucket in the future,
// it WILL be the caller's responsibility to synchronize all access to a bucket.
28
type bucket struct {
29 30 31
	list *list.List
}

32 33
func newBucket() *bucket {
	b := new(bucket)
34 35 36
	b.list = list.New()
	return b
}
37

38
// returns all peers in the bucket
Aarsh Shah's avatar
Aarsh Shah committed
39
// it is safe for the caller to modify the returned objects as it is a defensive copy
Aarsh Shah's avatar
Aarsh Shah committed
40 41
func (b *bucket) peers() []PeerInfo {
	var ps []PeerInfo
42
	for e := b.list.Front(); e != nil; e = e.Next() {
Aarsh Shah's avatar
Aarsh Shah committed
43
		p := e.Value.(*PeerInfo)
Aarsh Shah's avatar
Aarsh Shah committed
44
		ps = append(ps, *p)
45 46 47 48 49 50
	}
	return ps
}

// return the Ids of all the peers in the bucket.
func (b *bucket) peerIds() []peer.ID {
51 52
	ps := make([]peer.ID, 0, b.list.Len())
	for e := b.list.Front(); e != nil; e = e.Next() {
Aarsh Shah's avatar
Aarsh Shah committed
53
		p := e.Value.(*PeerInfo)
54
		ps = append(ps, p.Id)
55 56 57 58
	}
	return ps
}

Aarsh Shah's avatar
Aarsh Shah committed
59 60
// returns the peer with the given Id if it exists
// returns nil if the peerId does not exist
Aarsh Shah's avatar
Aarsh Shah committed
61
func (b *bucket) getPeer(p peer.ID) *PeerInfo {
62
	for e := b.list.Front(); e != nil; e = e.Next() {
Aarsh Shah's avatar
Aarsh Shah committed
63 64
		if e.Value.(*PeerInfo).Id == p {
			return e.Value.(*PeerInfo)
65 66
		}
	}
Aarsh Shah's avatar
Aarsh Shah committed
67
	return nil
68 69
}

70 71 72
// removes the peer with the given Id from the bucket.
// returns true if successful, false otherwise.
func (b *bucket) remove(id peer.ID) bool {
73
	for e := b.list.Front(); e != nil; e = e.Next() {
Aarsh Shah's avatar
Aarsh Shah committed
74
		if e.Value.(*PeerInfo).Id == id {
75
			b.list.Remove(e)
76
			return true
77 78
		}
	}
79
	return false
80 81
}

82 83
func (b *bucket) moveToFront(id peer.ID) {

84
	for e := b.list.Front(); e != nil; e = e.Next() {
Aarsh Shah's avatar
Aarsh Shah committed
85
		if e.Value.(*PeerInfo).Id == id {
86 87 88
			b.list.MoveToFront(e)
		}
	}
89 90
}

Aarsh Shah's avatar
Aarsh Shah committed
91
func (b *bucket) pushFront(p *PeerInfo) {
92
	b.list.PushFront(p)
93 94
}

95
func (b *bucket) len() int {
96
	return b.list.Len()
97 98
}

99
// splits a buckets peers into two buckets, the methods receiver will have
100 101
// peers with CPL equal to cpl, the returned bucket will have peers with CPL
// greater than cpl (returned bucket has closer peers)
102
func (b *bucket) split(cpl int, target ID) *bucket {
103
	out := list.New()
104
	newbuck := newBucket()
105 106
	newbuck.list = out
	e := b.list.Front()
107
	for e != nil {
Aarsh Shah's avatar
Aarsh Shah committed
108
		pDhtId := e.Value.(*PeerInfo).dhtId
109
		peerCPL := CommonPrefixLen(pDhtId, target)
110
		if peerCPL > cpl {
111 112 113
			cur := e
			out.PushBack(e.Value)
			e = e.Next()
114
			b.list.Remove(cur)
115 116 117 118
			continue
		}
		e = e.Next()
	}
119
	return newbuck
120
}
121 122 123 124 125 126 127 128 129 130 131 132 133

// maxCommonPrefix returns the maximum common prefix length between any peer in
// the bucket with the target ID.
func (b *bucket) maxCommonPrefix(target ID) uint {
	maxCpl := uint(0)
	for e := b.list.Front(); e != nil; e = e.Next() {
		cpl := uint(CommonPrefixLen(e.Value.(*PeerInfo).dhtId, target))
		if cpl > maxCpl {
			maxCpl = cpl
		}
	}
	return maxCpl
}