mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-23 19:37:46 +08:00
this is a major refactor of the entire codebase it changes the monolithic peer.Peer into using a peer.ID and a peer.Peerstore. Other changes: - removed handshake3. - testutil vastly simplified peer - secio bugfix + debugging logs - testutil: RandKeyPair - backpressure bugfix: w.o.w. - peer: added hex enc/dec - peer: added a PeerInfo struct PeerInfo is a small struct used to pass around a peer with a set of addresses and keys. This is not meant to be a complete view of the system, but rather to model updates to the peerstore. It is used by things like the routing system. - updated peer/queue + peerset - latency metrics - testutil: use crand for PeerID gen RandPeerID generates random "valid" peer IDs. it does not NEED to generate keys because it is as if we lost the key right away. fine to read some randomness and hash it. to generate proper keys and an ID, use: sk, pk, _ := testutil.RandKeyPair() id, _ := peer.IDFromPublicKey(pk) Also added RandPeerIDFatal helper - removed old spipe - updated seccat - core: cleanup initIdentity - removed old getFromPeerList
102 lines
2.0 KiB
Go
102 lines
2.0 KiB
Go
package queue
|
|
|
|
import (
|
|
"container/heap"
|
|
"math/big"
|
|
"sync"
|
|
|
|
peer "github.com/jbenet/go-ipfs/peer"
|
|
ks "github.com/jbenet/go-ipfs/routing/keyspace"
|
|
u "github.com/jbenet/go-ipfs/util"
|
|
)
|
|
|
|
// peerMetric tracks a peer and its distance to something else.
|
|
type peerMetric struct {
|
|
// the peer
|
|
peer peer.ID
|
|
|
|
// big.Int for XOR metric
|
|
metric *big.Int
|
|
}
|
|
|
|
// peerMetricHeap implements a heap of peerDistances
|
|
type peerMetricHeap []*peerMetric
|
|
|
|
func (ph peerMetricHeap) Len() int {
|
|
return len(ph)
|
|
}
|
|
|
|
func (ph peerMetricHeap) Less(i, j int) bool {
|
|
return -1 == ph[i].metric.Cmp(ph[j].metric)
|
|
}
|
|
|
|
func (ph peerMetricHeap) Swap(i, j int) {
|
|
ph[i], ph[j] = ph[j], ph[i]
|
|
}
|
|
|
|
func (ph *peerMetricHeap) Push(x interface{}) {
|
|
item := x.(*peerMetric)
|
|
*ph = append(*ph, item)
|
|
}
|
|
|
|
func (ph *peerMetricHeap) Pop() interface{} {
|
|
old := *ph
|
|
n := len(old)
|
|
item := old[n-1]
|
|
*ph = old[0 : n-1]
|
|
return item
|
|
}
|
|
|
|
// distancePQ implements heap.Interface and PeerQueue
|
|
type distancePQ struct {
|
|
// from is the Key this PQ measures against
|
|
from ks.Key
|
|
|
|
// heap is a heap of peerDistance items
|
|
heap peerMetricHeap
|
|
|
|
sync.RWMutex
|
|
}
|
|
|
|
func (pq *distancePQ) Len() int {
|
|
pq.Lock()
|
|
defer pq.Unlock()
|
|
return len(pq.heap)
|
|
}
|
|
|
|
func (pq *distancePQ) Enqueue(p peer.ID) {
|
|
pq.Lock()
|
|
defer pq.Unlock()
|
|
|
|
distance := ks.XORKeySpace.Key([]byte(p)).Distance(pq.from)
|
|
|
|
heap.Push(&pq.heap, &peerMetric{
|
|
peer: p,
|
|
metric: distance,
|
|
})
|
|
}
|
|
|
|
func (pq *distancePQ) Dequeue() peer.ID {
|
|
pq.Lock()
|
|
defer pq.Unlock()
|
|
|
|
if len(pq.heap) < 1 {
|
|
panic("called Dequeue on an empty PeerQueue")
|
|
// will panic internally anyway, but we can help debug here
|
|
}
|
|
|
|
o := heap.Pop(&pq.heap)
|
|
p := o.(*peerMetric)
|
|
return p.peer
|
|
}
|
|
|
|
// NewXORDistancePQ returns a PeerQueue which maintains its peers sorted
|
|
// in terms of their distances to each other in an XORKeySpace (i.e. using
|
|
// XOR as a metric of distance).
|
|
func NewXORDistancePQ(fromKey u.Key) PeerQueue {
|
|
return &distancePQ{
|
|
from: ks.XORKeySpace.Key([]byte(fromKey)),
|
|
heap: peerMetricHeap{},
|
|
}
|
|
}
|