Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
chore: cleanup addr book patch
Browse files Browse the repository at this point in the history
1. Remove duplicate import.
2. Lift empty check to simplify code.
3. Use a more efficient map key (go will optimize this to not allocate).
  • Loading branch information
Stebalien committed Jul 20, 2021
1 parent c678fe3 commit 25f38ee
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions pstoreds/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"sync"
"time"

"github.com/libp2p/go-libp2p-core/record"

ds "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
logging "github.com/ipfs/go-log"
Expand Down Expand Up @@ -454,6 +452,10 @@ func (ab *dsAddrBook) ClearAddrs(p peer.ID) {
}

func (ab *dsAddrBook) setAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration, mode ttlWriteMode, signed bool) (err error) {
if len(addrs) == 0 {
return nil
}

pr, err := ab.loadRecord(p, true, false)
if err != nil {
return fmt.Errorf("failed to load peerstore entry for peer %v while setting addrs, err: %v", p, err)
Expand All @@ -468,16 +470,13 @@ func (ab *dsAddrBook) setAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duratio
// }

newExp := time.Now().Add(ttl).Unix()
var addrsMap map[string]*pb.AddrBookRecord_AddrEntry
if len(addrs) > 0 {
addrsMap = make(map[string]*pb.AddrBookRecord_AddrEntry, len(pr.Addrs))
for _, addr := range pr.Addrs {
addrsMap[addr.Addr.String()] = addr
}
addrsMap := make(map[string]*pb.AddrBookRecord_AddrEntry, len(pr.Addrs))
for _, addr := range pr.Addrs {
addrsMap[string(addr.Addr.Bytes())] = addr
}

updateExisting := func(incoming ma.Multiaddr) *pb.AddrBookRecord_AddrEntry {
existingEntry := addrsMap[incoming.String()]
existingEntry := addrsMap[string(incoming.Bytes())]
if existingEntry == nil {
return nil
}
Expand Down

0 comments on commit 25f38ee

Please sign in to comment.