Skip to content

Commit

Permalink
add comments and resolve nits
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Aug 14, 2023
1 parent 859dc42 commit bd38997
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
6 changes: 3 additions & 3 deletions p2p/protocol/autonatv2/autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ func (an *AutoNAT) CheckReachability(ctx context.Context, highPriorityAddrs []ma
func (an *AutoNAT) validPeer() peer.ID {
peers := an.host.Peerstore().Peers()
idx := 0
for _, p := range an.host.Peerstore().Peers() {
if proto, err := an.host.Peerstore().SupportsProtocols(p, DialProtocol); len(proto) == 0 || err != nil {
for i := 0; i < len(peers); i++ {
if proto, err := an.host.Peerstore().SupportsProtocols(peers[i], DialProtocol); len(proto) == 0 || err != nil {
continue
}
peers[idx] = p
peers[idx] = peers[i]
idx++
}
if idx == 0 {
Expand Down
8 changes: 7 additions & 1 deletion p2p/protocol/autonatv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@ import (

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

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

mu sync.Mutex
mu sync.Mutex
// attemptQueues maps nonce to the channel for providing the local multiaddr of the connection
// the nonce was received on
attemptQueues map[uint64]chan ma.Multiaddr
}

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

// CheckReachability verifies address reachability with a AutoNAT v2 server p. It'll provide data for amplification
// attack prevention for high priority addresses and not for low priority addresses.
func (ac *Client) CheckReachability(ctx context.Context, p peer.ID, highPriorityAddrs []ma.Multiaddr, lowPriorityAddrs []ma.Multiaddr) ([]Result, error) {
ctx, cancel := context.WithTimeout(ctx, streamTimeout)
defer cancel()
Expand Down
5 changes: 2 additions & 3 deletions p2p/protocol/autonatv2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"golang.org/x/exp/rand"
)


type dataRequestPolicyFunc = func(s network.Stream, dialAddr ma.Multiaddr) bool

const (
Expand Down Expand Up @@ -268,7 +267,7 @@ func (r *rateLimiter) Accept(p peer.ID) bool {
func (r *rateLimiter) cleanup(p peer.ID, now time.Time) {
idx := len(r.reqs)
for i, t := range r.reqs {
if now.Sub(t).Minutes() <= 1 {
if now.Sub(t) < time.Minute {
idx = i
break
}
Expand All @@ -277,7 +276,7 @@ func (r *rateLimiter) cleanup(p peer.ID, now time.Time) {

idx = len(r.peerReqs[p])
for i, t := range r.peerReqs[p] {
if now.Sub(t).Minutes() <= 1 {
if now.Sub(t) < time.Minute {
idx = i
break
}
Expand Down

0 comments on commit bd38997

Please sign in to comment.