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

3
package kbucket
4 5 6

import (
	"container/list"
7
	"sync"
8

9
	"github.com/libp2p/go-libp2p-core/peer"
10
)
Jeromy's avatar
Jeromy committed
11

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// PeerState is the state of the peer as seen by the Routing Table.
type PeerState int

const (
	// PeerStateActive indicates that we know the peer is active/alive.
	PeerStateActive PeerState = iota
	// PeerStateMissing indicates that we do not know the state of the peer.
	PeerStateMissing
)

// PeerInfo holds all related information for a peer in the K-Bucket.
type PeerInfo struct {
	Id    peer.ID
	State PeerState
}

// bucket holds a list of peers.
type bucket struct {
30 31 32 33
	lk   sync.RWMutex
	list *list.List
}

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

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

// return the Ids of all the peers in the bucket.
func (b *bucket) peerIds() []peer.ID {
55 56 57 58
	b.lk.RLock()
	defer b.lk.RUnlock()
	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
59
		p := e.Value.(*PeerInfo)
60
		ps = append(ps, p.Id)
61 62 63 64
	}
	return ps
}

Aarsh Shah's avatar
Aarsh Shah committed
65 66 67
// returns the peer with the given Id if it exists
// returns nil if the peerId does not exist
func (b *bucket) getPeer(p peer.ID) *PeerInfo {
68 69 70
	b.lk.RLock()
	defer b.lk.RUnlock()
	for e := b.list.Front(); e != nil; e = e.Next() {
Aarsh Shah's avatar
Aarsh Shah committed
71 72
		if e.Value.(*PeerInfo).Id == p {
			return e.Value.(*PeerInfo)
73 74
		}
	}
Aarsh Shah's avatar
Aarsh Shah committed
75
	return nil
76 77
}

78 79 80
// removes the peer with the given Id from the bucket.
// returns true if successful, false otherwise.
func (b *bucket) remove(id peer.ID) bool {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81 82
	b.lk.Lock()
	defer b.lk.Unlock()
83
	for e := b.list.Front(); e != nil; e = e.Next() {
Aarsh Shah's avatar
Aarsh Shah committed
84
		if e.Value.(*PeerInfo).Id == id {
85
			b.list.Remove(e)
86
			return true
87 88
		}
	}
89
	return false
90 91
}

92
func (b *bucket) moveToFront(id peer.ID) {
93
	b.lk.Lock()
94
	defer b.lk.Unlock()
95

96
	for e := b.list.Front(); e != nil; e = e.Next() {
Aarsh Shah's avatar
Aarsh Shah committed
97
		if e.Value.(*PeerInfo).Id == id {
98 99 100
			b.list.MoveToFront(e)
		}
	}
101 102
}

Aarsh Shah's avatar
Aarsh Shah committed
103
func (b *bucket) pushFront(p *PeerInfo) {
104 105 106
	b.lk.Lock()
	b.list.PushFront(p)
	b.lk.Unlock()
107 108
}

109
func (b *bucket) len() int {
110 111 112
	b.lk.RLock()
	defer b.lk.RUnlock()
	return b.list.Len()
113 114
}

115
// splits a buckets peers into two buckets, the methods receiver will have
116 117
// peers with CPL equal to cpl, the returned bucket will have peers with CPL
// greater than cpl (returned bucket has closer peers)
118
func (b *bucket) split(cpl int, target ID) *bucket {
119 120 121
	b.lk.Lock()
	defer b.lk.Unlock()

122
	out := list.New()
123
	newbuck := newBucket()
124 125
	newbuck.list = out
	e := b.list.Front()
126
	for e != nil {
Aarsh Shah's avatar
Aarsh Shah committed
127
		peerID := ConvertPeerID(e.Value.(*PeerInfo).Id)
Matt Joiner's avatar
Matt Joiner committed
128
		peerCPL := CommonPrefixLen(peerID, target)
129
		if peerCPL > cpl {
130 131 132
			cur := e
			out.PushBack(e.Value)
			e = e.Next()
133
			b.list.Remove(cur)
134 135 136 137
			continue
		}
		e = e.Next()
	}
138
	return newbuck
139
}