Documentation ¶
Index ¶
- func DHTJoin(params DHTJoinParams) int
- func Distance(a, b []byte) []byte
- func DistanceCmp(x []byte, a, b []byte) int
- func DistanceGt(x []byte, a, b []byte) bool
- func DistanceLt(x []byte, a, b []byte) bool
- func DistanceLz(a, b []byte) (ret int)
- func HasPrefix(x []byte, prefix []byte, nbits int) bool
- func LeadingZeros(x []byte) int
- func XORBytes(dst, a, b []byte) int
- type Cache
- func (kc *Cache[V]) AcceptingPrefixLen() int
- func (kc *Cache[V]) Closest(key []byte) (ret *Entry[V])
- func (kc *Cache[V]) Contains(key []byte, now time.Time) bool
- func (kc *Cache[V]) Count() int
- func (kc *Cache[V]) Delete(key []byte) *Entry[V]
- func (kc *Cache[V]) Expire(out []Entry[V], now time.Time) []Entry[V]
- func (kc *Cache[V]) ForEach(k []byte, fn func(e Entry[V]) bool)
- func (kc *Cache[V]) ForEachCloser(x []byte, fn func(Entry[V]) bool)
- func (kc *Cache[V]) ForEachMatching(prefix []byte, nbits int, fn func(Entry[V]) bool)
- func (kc *Cache[V]) Get(key []byte, now time.Time) (ret V, exists bool)
- func (kc *Cache[V]) IsFull() bool
- func (kc *Cache[V]) Locus() []byte
- func (kc *Cache[V]) Put(key []byte, v V, now, expiresAt time.Time) (evicted *Entry[V], affected bool)
- func (kc *Cache[V]) Update(key []byte, fn func(v Entry[V], exists bool) Entry[V]) (evicted *Entry[V], added bool)
- func (kc *Cache[V]) WouldAdd(key []byte, now time.Time) bool
- func (kc *Cache[V]) WouldPut(key []byte) bool
- type DHTFindNodeParams
- type DHTFindNodeResult
- type DHTGetParams
- type DHTGetResult
- type DHTJoinParams
- type DHTNode
- func (node *DHTNode) AddPeer(id p2p.PeerID, info []byte) bool
- func (node *DHTNode) Count() int
- func (node *DHTNode) Get(key []byte) []byte
- func (node *DHTNode) GetPeer(x p2p.PeerID) ([]byte, bool)
- func (n *DHTNode) HandleFindNode(from p2p.PeerID, req FindNodeReq) (FindNodeRes, error)
- func (n *DHTNode) HandleGet(from p2p.PeerID, req GetReq) (GetRes, error)
- func (node *DHTNode) HandlePut(from p2p.PeerID, req PutReq) (PutRes, error)
- func (node *DHTNode) HasPeer(x p2p.PeerID) bool
- func (node *DHTNode) ListNodeInfos(key []byte, n int) (ret []NodeInfo)
- func (node *DHTNode) ListPeers(limit int) (ret []p2p.PeerID)
- func (node *DHTNode) LocalID() p2p.PeerID
- func (node *DHTNode) Put(key, value []byte, ttl time.Duration) bool
- func (node *DHTNode) RemovePeer(localID p2p.PeerID) bool
- func (n *DHTNode) String() string
- func (node *DHTNode) WouldAdd(key []byte) bool
- type DHTNodeParams
- type DHTPutParams
- type DHTPutResult
- type Entry
- type ErrNotFound
- type FindNodeFunc
- type FindNodeReq
- type FindNodeRes
- type GetFunc
- type GetReq
- type GetRes
- type NodeInfo
- type PingReq
- type PingRes
- type PutFunc
- type PutReq
- type PutRes
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Distance ¶
Distance is the Kademlia distance metric. Distance(a, b) == Distance(b, a) len(Distance(a, b)) == min(len(a), len(b))
func DistanceCmp ¶
DistanceCmp is equivalent to bytes.Compare(Distance(x, a), Distance(x, b))
func DistanceGt ¶
DistanceGt returns true if Distance(x, a) > Distance(x, b)
func DistanceLt ¶
DistanceLt returns true if Distance(x, a) < Distance(x, b)
func DistanceLz ¶
DistanceLz returns the number of leading zeros in the distance between a and b DistanceLz == LeadingZeros(Distance(a, b))
func HasPrefix ¶
HasPrefix returns true if the first `nbits` bits of x match the first `nbits` bits of prefix.
func LeadingZeros ¶
LeadingZeros returns the number of 0s that occur before the first 1 when iterating bit by bit, most significant bit first, low indexed byte to high indexed byte.
Types ¶
type Cache ¶
type Cache[V any] struct { // contains filtered or unexported fields }
Cache stores data and evicts based on the Kademlia distance metric, and the CreatedAt time. Entries closer to the locus, that have existed for longer are more likely to remain in the cache.
func NewCache ¶
NewCache returns a Cache with the provided parameters NewCache will panic if max is < minPerBucket * 8 * len(locus) i.e. If the locus is 256 bits long, then you can have anywhere from 0 to 256 buckets, and max must be sufficiently large to respect each of those 256 buckets having a minimum number of keys.
func (*Cache[V]) AcceptingPrefixLen ¶
func (*Cache[V]) ForEach ¶
ForEach calls fn with entries. All of the entries with n bits in common with k will be emitted before any of the entries with n-1 bits in common with k.
func (*Cache[V]) ForEachCloser ¶
ForEachCloser calls fn with all the entries in the Cache which are closer to x than they are to the locus.
func (*Cache[V]) ForEachMatching ¶
ForEachMatching calls fn with every entry where the key matches prefix for the leading nbits. If nbits < len(prefix/8) it panics.
func (*Cache[V]) IsFull ¶
IsFull returns whether the cache is full further calls to Put will attempt an eviction.
func (*Cache[V]) Put ¶
func (kc *Cache[V]) Put(key []byte, v V, now, expiresAt time.Time) (evicted *Entry[V], affected bool)
Put puts an entry in the cache, replacing the entry at that key. Put returns the evicted entry if there was one, and whether or not the Put had an effect.
func (*Cache[V]) Update ¶
func (kc *Cache[V]) Update(key []byte, fn func(v Entry[V], exists bool) Entry[V]) (evicted *Entry[V], added bool)
Update calls fn with the entry in the cache at key if it exists.
type DHTFindNodeParams ¶
type DHTFindNodeParams struct { Initial []NodeInfo Target p2p.PeerID Ask FindNodeFunc Validate func(NodeInfo) bool }
type DHTFindNodeResult ¶
func DHTFindNode ¶
func DHTFindNode(params DHTFindNodeParams) (*DHTFindNodeResult, error)
type DHTGetParams ¶
type DHTGetResult ¶
type DHTGetResult struct { // Value is the value associated with the key Value []byte // From is the node that returned the value From p2p.PeerID // ExpiresAt is when the entry expires ExpiresAt time.Time // Closest is the peer closest to the key that we contacted. Closest p2p.PeerID NumContacted int NumResponded int }
func DHTGet ¶
func DHTGet(params DHTGetParams) (*DHTGetResult, error)
DHTPGet performs the Kademlia FIND_VALUE operation.
type DHTJoinParams ¶
type DHTJoinParams struct { Initial []NodeInfo Target p2p.PeerID Ask FindNodeFunc AddPeer func(p2p.PeerID, []byte) bool }
type DHTNode ¶
type DHTNode struct {
// contains filtered or unexported fields
}
DHTNode is the state of a single node in a DHT.
func NewDHTNode ¶
func NewDHTNode(params DHTNodeParams) *DHTNode
func (*DHTNode) HandleFindNode ¶
func (n *DHTNode) HandleFindNode(from p2p.PeerID, req FindNodeReq) (FindNodeRes, error)
func (*DHTNode) ListNodeInfos ¶
ListNodeInfos returns the n nodes closest to key.
func (*DHTNode) RemovePeer ¶
type DHTNodeParams ¶
type DHTPutParams ¶
type DHTPutResult ¶
func DHTPut ¶
func DHTPut(params DHTPutParams) (*DHTPutResult, error)
DHTPut performs the Kademlia STORE operation
type ErrNotFound ¶
type ErrNotFound struct {
Key []byte
}
func (ErrNotFound) Error ¶
func (e ErrNotFound) Error() string
type FindNodeFunc ¶
type FindNodeFunc = func(NodeInfo, FindNodeReq) (FindNodeRes, error)
FindNodeFunc is the type of functions implementing the FindNode rpc.
type FindNodeReq ¶
type FindNodeReq struct { Target p2p.PeerID `json:"target"` Limit int `json:"limit"` }
FindNodeReq is the request in the FindNode rpc
type FindNodeRes ¶
type FindNodeRes struct {
Nodes []NodeInfo `json:"nodes"`
}
FindNodeRes it the response in the FindNode rpc
type GetRes ¶
type GetRes struct { Value []byte `json:"value"` ExpiresAt tai64.TAI64N `json:"expires_at"` Closer []NodeInfo `json:"closer"` }
GetRes is the response in the Get rpc.
type NodeInfo ¶
type NodeInfo struct { ID p2p.PeerID `json:"id"` Info []byte `json:"info"` }
NodeInfo is information about a node, and its ID.