Skip to content

Commit 1d53bad

Browse files
committed
chore: linting fixes
1 parent 6c82b40 commit 1d53bad

File tree

11 files changed

+73
-49
lines changed

11 files changed

+73
-49
lines changed

dht.go

Lines changed: 6 additions & 0 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
@@ -630,18 +632,22 @@ func mkDsKey(s string) ds.Key {
630632
return ds.NewKey(base32.RawStdEncoding.EncodeToString([]byte(s)))
631633
}
632634

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

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

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

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

dht_bootstrap.go

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

8+
"github.com/libp2p/go-libp2p-core/peer"
9+
910
multierror "github.com/hashicorp/go-multierror"
1011
process "github.com/jbenet/goprocess"
1112
processctx "github.com/jbenet/goprocess/context"
1213
kbucket "github.com/libp2p/go-libp2p-kbucket"
1314
"github.com/multiformats/go-multiaddr"
14-
_ "github.com/multiformats/go-multiaddr-dns"
1515
)
1616

17+
// DefaultBootstrapPeers is a set of public DHT bootstrap peers provided by libp2p.
1718
var DefaultBootstrapPeers []multiaddr.Multiaddr
1819

1920
// 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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package dht
22

33
import (
44
"fmt"
5-
"github.com/ipfs/go-ipns"
65
"time"
76

7+
"github.com/ipfs/go-ipns"
8+
89
ds "github.com/ipfs/go-datastore"
910
dssync "github.com/ipfs/go-datastore/sync"
1011
"github.com/libp2p/go-libp2p-core/host"
@@ -27,6 +28,7 @@ const (
2728
ModeServer
2829
)
2930

31+
// DefaultPrefix is the application specific prefix attached to all DHT protocols by default.
3032
const DefaultPrefix protocol.ID = "/ipfs"
3133

3234
// Options is a structure containing all the options that can be used when constructing a DHT.
@@ -322,7 +324,7 @@ func DisableProviders() Option {
322324
}
323325
}
324326

325-
// DisableProviders disables storing and retrieving value records (including
327+
// DisableValues disables storing and retrieving value records (including
326328
// public keys).
327329
//
328330
// Defaults to enabled.

dht_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,9 +1356,8 @@ func TestClientModeFindPeer(t *testing.T) {
13561356
func minInt(a, b int) int {
13571357
if a < b {
13581358
return a
1359-
} else {
1360-
return b
13611359
}
1360+
return b
13621361
}
13631362

13641363
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: 2 additions & 0 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

0 commit comments

Comments
 (0)