@@ -11,30 +11,35 @@ import (
1111 kbucket "github.com/libp2p/go-libp2p-kbucket"
1212)
1313
14+ // KeyKadID contains the Kademlia key in string and binary form.
1415type KeyKadID struct {
1516 Key string
1617 Kad kbucket.ID
1718}
1819
20+ // NewKeyKadID creates a KeyKadID from a string Kademlia ID.
1921func NewKeyKadID (k string ) * KeyKadID {
2022 return & KeyKadID {
2123 Key : k ,
2224 Kad : kbucket .ConvertKey (k ),
2325 }
2426}
2527
28+ // PeerKadID contains a libp2p Peer ID and a binary Kademlia ID.
2629type PeerKadID struct {
2730 Peer peer.ID
2831 Kad kbucket.ID
2932}
3033
34+ // NewPeerKadID creates a PeerKadID from a libp2p Peer ID.
3135func NewPeerKadID (p peer.ID ) * PeerKadID {
3236 return & PeerKadID {
3337 Peer : p ,
3438 Kad : kbucket .ConvertPeerID (p ),
3539 }
3640}
3741
42+ // NewPeerKadIDSlice creates a slice of PeerKadID from the passed slice of libp2p Peer IDs.
3843func NewPeerKadIDSlice (p []peer.ID ) []* PeerKadID {
3944 r := make ([]* PeerKadID , len (p ))
4045 for i := range p {
@@ -43,14 +48,16 @@ func NewPeerKadIDSlice(p []peer.ID) []*PeerKadID {
4348 return r
4449}
4550
51+ // OptPeerKadID returns a pointer to a PeerKadID or nil if the passed Peer ID is it's default value.
4652func OptPeerKadID (p peer.ID ) * PeerKadID {
4753 if p == "" {
4854 return nil
49- } else {
50- return NewPeerKadID (p )
5155 }
56+ return NewPeerKadID (p )
5257}
5358
59+ // NewLookupEvent creates a LookupEvent automatically converting the node
60+ // libp2p Peer ID to a PeerKadID and the string Kademlia key to a KeyKadID.
5461func NewLookupEvent (
5562 node peer.ID ,
5663 id uuid.UUID ,
@@ -86,6 +93,7 @@ type LookupEvent struct {
8693 Terminate * LookupTerminateEvent
8794}
8895
96+ // NewLookupUpdateEvent creates a new lookup update event, automatically converting the passed peer IDs to peer Kad IDs.
8997func NewLookupUpdateEvent (
9098 cause peer.ID ,
9199 source peer.ID ,
@@ -127,13 +135,15 @@ type LookupTerminateEvent struct {
127135 Reason LookupTerminationReason
128136}
129137
138+ // NewLookupTerminateEvent creates a new lookup termination event with a given reason.
130139func NewLookupTerminateEvent (reason LookupTerminationReason ) * LookupTerminateEvent {
131140 return & LookupTerminateEvent {Reason : reason }
132141}
133142
134143// LookupTerminationReason captures reasons for terminating a lookup.
135144type LookupTerminationReason int
136145
146+ // MarshalJSON returns the JSON encoding of the passed lookup termination reason.
137147func (r LookupTerminationReason ) MarshalJSON () ([]byte , error ) {
138148 return json .Marshal (r .String ())
139149}
@@ -220,7 +230,7 @@ func RegisterForLookupEvents(ctx context.Context) (context.Context, <-chan *Look
220230 return context .WithValue (ctx , routingLookupKey {}, ech ), ch
221231}
222232
223- // Number of events to buffer.
233+ // LookupEventBufferSize is the number of events to buffer.
224234var LookupEventBufferSize = 16
225235
226236// PublishLookupEvent publishes a query event to the query event channel
0 commit comments