Skip to content

Commit 07285de

Browse files
committed
Merge branch 'master' of github.com:libp2p/go-libp2p-kad-dht into feat/dual
2 parents 0d90799 + 969e03e commit 07285de

File tree

11 files changed

+77
-54
lines changed

11 files changed

+77
-54
lines changed

dht.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636

3737
var logger = logging.Logger("dht")
3838

39+
// BaseConnMgrScore is the base of the score set on the connection manager "kbucket" tag.
40+
// It is added with the common prefix length between two peer IDs.
3941
const BaseConnMgrScore = 5
4042

4143
type mode int
@@ -607,22 +609,22 @@ func (dht *IpfsDHT) getMode() mode {
607609
return dht.mode
608610
}
609611

610-
// Context return dht's context
612+
// Context returns the DHT's context.
611613
func (dht *IpfsDHT) Context() context.Context {
612614
return dht.ctx
613615
}
614616

615-
// Process return dht's process
617+
// Process returns the DHT's process.
616618
func (dht *IpfsDHT) Process() goprocess.Process {
617619
return dht.proc
618620
}
619621

620-
// RoutingTable return dht's routingTable
622+
// RoutingTable returns the DHT's routingTable.
621623
func (dht *IpfsDHT) RoutingTable() *kb.RoutingTable {
622624
return dht.routingTable
623625
}
624626

625-
// Close calls Process Close
627+
// Close calls Process Close.
626628
func (dht *IpfsDHT) Close() error {
627629
return dht.proc.Close()
628630
}
@@ -631,18 +633,22 @@ func mkDsKey(s string) ds.Key {
631633
return ds.NewKey(base32.RawStdEncoding.EncodeToString([]byte(s)))
632634
}
633635

636+
// PeerID returns the DHT node's Peer ID.
634637
func (dht *IpfsDHT) PeerID() peer.ID {
635638
return dht.self
636639
}
637640

641+
// PeerKey returns a DHT key, converted from the DHT node's Peer ID.
638642
func (dht *IpfsDHT) PeerKey() []byte {
639643
return kb.ConvertPeerID(dht.self)
640644
}
641645

646+
// Host returns the libp2p host this DHT is operating with.
642647
func (dht *IpfsDHT) Host() host.Host {
643648
return dht.host
644649
}
645650

651+
// Ping sends a ping message to the passed peer and waits for a response.
646652
func (dht *IpfsDHT) Ping(ctx context.Context, p peer.ID) error {
647653
req := pb.NewMessage(pb.Message_PING, nil, 0)
648654
resp, err := dht.sendRequest(ctx, p, req)

dht_bootstrap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package dht
33
import (
44
"context"
55
"fmt"
6-
"github.com/libp2p/go-libp2p-core/peer"
76
"time"
87

98
multierror "github.com/hashicorp/go-multierror"
109
process "github.com/jbenet/goprocess"
1110
processctx "github.com/jbenet/goprocess/context"
11+
"github.com/libp2p/go-libp2p-core/peer"
1212
kbucket "github.com/libp2p/go-libp2p-kbucket"
1313
"github.com/multiformats/go-multiaddr"
14-
_ "github.com/multiformats/go-multiaddr-dns"
1514
)
1615

16+
// DefaultBootstrapPeers is a set of public DHT bootstrap peers provided by libp2p.
1717
var DefaultBootstrapPeers []multiaddr.Multiaddr
1818

1919
// Minimum number of peers in the routing table. If we drop below this and we

dht_net.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424

2525
var dhtReadMessageTimeout = 10 * time.Second
2626
var dhtStreamIdleTimeout = 1 * time.Minute
27+
28+
// ErrReadTimeout is an error that occurs when no message is read within the timeout period.
2729
var ErrReadTimeout = fmt.Errorf("timed out reading response")
2830

2931
// The Protobuf writer performs multiple small writes when writing a message.
@@ -111,7 +113,7 @@ func (dht *IpfsDHT) handleNewMessage(s network.Stream) bool {
111113
err = req.Unmarshal(msgbytes)
112114
r.ReleaseMsg(msgbytes)
113115
if err != nil {
114-
logger.Debugf("error unmarshalling message: %#v", err)
116+
logger.Debugf("error unmarshaling message: %#v", err)
115117
_ = stats.RecordWithTags(ctx,
116118
[]tag.Mutator{tag.Upsert(metrics.KeyMessageType, "UNKNOWN")},
117119
metrics.ReceivedMessages.M(1),

dht_options.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
ds "github.com/ipfs/go-datastore"
1010
dssync "github.com/ipfs/go-datastore/sync"
11+
"github.com/ipfs/go-ipns"
1112
"github.com/libp2p/go-libp2p-core/host"
1213
"github.com/libp2p/go-libp2p-core/network"
1314
"github.com/libp2p/go-libp2p-core/peer"
@@ -30,6 +31,7 @@ const (
3031
ModeAutoServer
3132
)
3233

34+
// DefaultPrefix is the application specific prefix attached to all DHT protocols by default.
3335
const DefaultPrefix protocol.ID = "/ipfs"
3436

3537
// Options is a structure containing all the options that can be used when constructing a DHT.
@@ -334,7 +336,7 @@ func DisableProviders() Option {
334336
}
335337
}
336338

337-
// DisableProviders disables storing and retrieving value records (including
339+
// DisableValues disables storing and retrieving value records (including
338340
// public keys).
339341
//
340342
// Defaults to enabled.

dht_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,9 +1332,8 @@ func TestClientModeFindPeer(t *testing.T) {
13321332
func minInt(a, b int) int {
13331333
if a < b {
13341334
return a
1335-
} else {
1336-
return b
13371335
}
1336+
return b
13381337
}
13391338

13401339
func TestFindPeerQueryMinimal(t *testing.T) {

events.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
1415
type KeyKadID struct {
1516
Key string
1617
Kad kbucket.ID
1718
}
1819

20+
// NewKeyKadID creates a KeyKadID from a string Kademlia ID.
1921
func 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.
2629
type PeerKadID struct {
2730
Peer peer.ID
2831
Kad kbucket.ID
2932
}
3033

34+
// NewPeerKadID creates a PeerKadID from a libp2p Peer ID.
3135
func 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.
3843
func 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.
4652
func 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.
5461
func 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.
8997
func 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.
130139
func NewLookupTerminateEvent(reason LookupTerminationReason) *LookupTerminateEvent {
131140
return &LookupTerminateEvent{Reason: reason}
132141
}
133142

134143
// LookupTerminationReason captures reasons for terminating a lookup.
135144
type LookupTerminationReason int
136145

146+
// MarshalJSON returns the JSON encoding of the passed lookup termination reason.
137147
func (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.
224234
var LookupEventBufferSize = 16
225235

226236
// PublishLookupEvent publishes a query event to the query event channel

lookup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func (lk loggableKeyBytes) String() string {
6666
return k
6767
}
6868

69-
// Kademlia 'node lookup' operation. Returns a channel of the K closest peers
70-
// to the given key
69+
// GetClosestPeers is a Kademlia 'node lookup' operation. Returns a channel of
70+
// the K closest peers to the given key.
7171
//
7272
// If the context is canceled, this function will return the context error along
7373
// with the closest K peers it has found so far.

protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ import (
77
var (
88
// ProtocolDHT is the default DHT protocol.
99
ProtocolDHT protocol.ID = "/ipfs/kad/1.0.0"
10-
// DefualtProtocols spoken by the DHT.
10+
// DefaultProtocols spoken by the DHT.
1111
DefaultProtocols = []protocol.ID{ProtocolDHT}
1212
)

query.go

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -294,30 +294,31 @@ func (q *query) run() {
294294

295295
// spawnQuery starts one query, if an available heard peer is found
296296
func (q *query) spawnQuery(ctx context.Context, cause peer.ID, ch chan<- *queryUpdate) {
297-
if peers := q.queryPeers.GetSortedHeard(); len(peers) == 0 {
297+
peers := q.queryPeers.GetSortedHeard()
298+
if len(peers) == 0 {
298299
return
299-
} else {
300-
PublishLookupEvent(ctx,
301-
NewLookupEvent(
302-
q.dht.self,
303-
q.id,
304-
q.key,
305-
NewLookupUpdateEvent(
306-
cause,
307-
q.queryPeers.GetReferrer(peers[0]),
308-
nil, // heard
309-
[]peer.ID{peers[0]}, // waiting
310-
nil, // queried
311-
nil, // unreachable
312-
),
313-
nil,
314-
nil,
315-
),
316-
)
317-
q.queryPeers.SetState(peers[0], qpeerset.PeerWaiting)
318-
q.waitGroup.Add(1)
319-
go q.queryPeer(ctx, ch, peers[0])
320300
}
301+
302+
PublishLookupEvent(ctx,
303+
NewLookupEvent(
304+
q.dht.self,
305+
q.id,
306+
q.key,
307+
NewLookupUpdateEvent(
308+
cause,
309+
q.queryPeers.GetReferrer(peers[0]),
310+
nil, // heard
311+
[]peer.ID{peers[0]}, // waiting
312+
nil, // queried
313+
nil, // unreachable
314+
),
315+
nil,
316+
nil,
317+
),
318+
)
319+
q.queryPeers.SetState(peers[0], qpeerset.PeerWaiting)
320+
q.waitGroup.Add(1)
321+
go q.queryPeer(ctx, ch, peers[0])
321322
}
322323

323324
func (q *query) isReadyToTerminate() (bool, LookupTerminationReason) {
@@ -353,20 +354,20 @@ func (q *query) isStarvationTermination() bool {
353354
func (q *query) terminate(ctx context.Context, cancel context.CancelFunc, reason LookupTerminationReason) {
354355
if q.terminated {
355356
return
356-
} else {
357-
PublishLookupEvent(ctx,
358-
NewLookupEvent(
359-
q.dht.self,
360-
q.id,
361-
q.key,
362-
nil,
363-
nil,
364-
NewLookupTerminateEvent(reason),
365-
),
366-
)
367-
cancel() // abort outstanding queries
368-
q.terminated = true
369357
}
358+
359+
PublishLookupEvent(ctx,
360+
NewLookupEvent(
361+
q.dht.self,
362+
q.id,
363+
q.key,
364+
nil,
365+
nil,
366+
NewLookupTerminateEvent(reason),
367+
),
368+
)
369+
cancel() // abort outstanding queries
370+
q.terminated = true
370371
}
371372

372373
// queryPeer queries a single peer and reports its findings on the channel.

records.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type pubkrs struct {
1515
err error
1616
}
1717

18+
// GetPublicKey gets the public key when given a Peer ID. It will extract from
19+
// the Peer ID if inlined or ask the node it belongs to or ask the DHT.
1820
func (dht *IpfsDHT) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, error) {
1921
logger.Debugf("getPublicKey for: %s", p)
2022

@@ -77,7 +79,7 @@ func (dht *IpfsDHT) getPublicKeyFromDHT(ctx context.Context, p peer.ID) (ci.PubK
7779

7880
pubk, err := ci.UnmarshalPublicKey(val)
7981
if err != nil {
80-
logger.Errorf("Could not unmarshall public key retrieved from DHT for %v", p)
82+
logger.Errorf("Could not unmarshal public key retrieved from DHT for %v", p)
8183
return nil, err
8284
}
8385

@@ -109,7 +111,7 @@ func (dht *IpfsDHT) getPublicKeyFromNode(ctx context.Context, p peer.ID) (ci.Pub
109111

110112
pubk, err := ci.UnmarshalPublicKey(record.GetValue())
111113
if err != nil {
112-
logger.Errorf("Could not unmarshall public key for %v", p)
114+
logger.Errorf("Could not unmarshal public key for %v", p)
113115
return nil, err
114116
}
115117

0 commit comments

Comments
 (0)