Skip to content

Commit bd38997

Browse files
committed
add comments and resolve nits
1 parent 859dc42 commit bd38997

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

Diff for: p2p/protocol/autonatv2/autonat.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ func (an *AutoNAT) CheckReachability(ctx context.Context, highPriorityAddrs []ma
142142
func (an *AutoNAT) validPeer() peer.ID {
143143
peers := an.host.Peerstore().Peers()
144144
idx := 0
145-
for _, p := range an.host.Peerstore().Peers() {
146-
if proto, err := an.host.Peerstore().SupportsProtocols(p, DialProtocol); len(proto) == 0 || err != nil {
145+
for i := 0; i < len(peers); i++ {
146+
if proto, err := an.host.Peerstore().SupportsProtocols(peers[i], DialProtocol); len(proto) == 0 || err != nil {
147147
continue
148148
}
149-
peers[idx] = p
149+
peers[idx] = peers[i]
150150
idx++
151151
}
152152
if idx == 0 {

Diff for: p2p/protocol/autonatv2/client.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@ import (
1818

1919
//go:generate protoc --go_out=. --go_opt=Mpbv2/autonat.proto=./pbv2 pbv2/autonat.proto
2020

21+
// Client implements the client for making dial requests for AutoNAT v2. It verifies successful
22+
// dials and provides an option to send data for amplification attack prevention.
2123
type Client struct {
2224
host host.Host
2325
dialCharge []byte
2426

25-
mu sync.Mutex
27+
mu sync.Mutex
28+
// attemptQueues maps nonce to the channel for providing the local multiaddr of the connection
29+
// the nonce was received on
2630
attemptQueues map[uint64]chan ma.Multiaddr
2731
}
2832

2933
func NewClient(h host.Host) *Client {
3034
return &Client{host: h, dialCharge: make([]byte, 4096), attemptQueues: make(map[uint64]chan ma.Multiaddr)}
3135
}
3236

37+
// CheckReachability verifies address reachability with a AutoNAT v2 server p. It'll provide data for amplification
38+
// attack prevention for high priority addresses and not for low priority addresses.
3339
func (ac *Client) CheckReachability(ctx context.Context, p peer.ID, highPriorityAddrs []ma.Multiaddr, lowPriorityAddrs []ma.Multiaddr) ([]Result, error) {
3440
ctx, cancel := context.WithTimeout(ctx, streamTimeout)
3541
defer cancel()

Diff for: p2p/protocol/autonatv2/server.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"golang.org/x/exp/rand"
1919
)
2020

21-
2221
type dataRequestPolicyFunc = func(s network.Stream, dialAddr ma.Multiaddr) bool
2322

2423
const (
@@ -268,7 +267,7 @@ func (r *rateLimiter) Accept(p peer.ID) bool {
268267
func (r *rateLimiter) cleanup(p peer.ID, now time.Time) {
269268
idx := len(r.reqs)
270269
for i, t := range r.reqs {
271-
if now.Sub(t).Minutes() <= 1 {
270+
if now.Sub(t) < time.Minute {
272271
idx = i
273272
break
274273
}
@@ -277,7 +276,7 @@ func (r *rateLimiter) cleanup(p peer.ID, now time.Time) {
277276

278277
idx = len(r.peerReqs[p])
279278
for i, t := range r.peerReqs[p] {
280-
if now.Sub(t).Minutes() <= 1 {
279+
if now.Sub(t) < time.Minute {
281280
idx = i
282281
break
283282
}

0 commit comments

Comments
 (0)