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

address lints and test failures #159

Merged
merged 4 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions addr/sorting.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ func (al AddrList) Less(i, j int) bool {
fda := isFDCostlyTransport(a)
fdb := isFDCostlyTransport(b)
if !fda {
if fdb {
return true
}

// if neither consume fd's, assume equal ordering
return false
return fdb
}

// if 'b' doesnt take a file descriptor
Expand Down
16 changes: 7 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ module github.com/libp2p/go-libp2p-peerstore
go 1.15

require (
github.com/gogo/protobuf v1.3.1
github.com/gogo/protobuf v1.3.2
github.com/hashicorp/golang-lru v0.5.4
github.com/ipfs/go-datastore v0.4.4
github.com/ipfs/go-ds-badger v0.2.3
github.com/ipfs/go-datastore v0.4.5
github.com/ipfs/go-ds-badger v0.2.7
github.com/ipfs/go-ds-leveldb v0.4.2
github.com/ipfs/go-log v1.0.3
github.com/ipfs/go-log v1.0.5
github.com/libp2p/go-buffer-pool v0.0.2
github.com/libp2p/go-libp2p-core v0.5.4
github.com/multiformats/go-base32 v0.0.3
github.com/multiformats/go-multiaddr v0.3.3
github.com/multiformats/go-multiaddr-fmt v0.1.0
github.com/multiformats/go-multihash v0.0.14
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.4.0
github.com/multiformats/go-multihash v0.0.15
github.com/stretchr/testify v1.7.0
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1
go.uber.org/goleak v1.0.0
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 // indirect
go.uber.org/goleak v1.1.10
)
127 changes: 83 additions & 44 deletions go.sum

Large diffs are not rendered by default.

14 changes: 5 additions & 9 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ func TestLatencyEWMAFun(t *testing.T) {
}

for {
select {
case <-time.After(200 * time.Millisecond):
m.RecordLatency(id, next())
print()
}
time.Sleep(200 * time.Millisecond)
m.RecordLatency(id, next())
print()
}
}

Expand All @@ -55,10 +53,8 @@ func TestLatencyEWMA(t *testing.T) {
}

for i := 0; i < 10; i++ {
select {
case <-time.After(200 * time.Millisecond):
m.RecordLatency(id, next())
}
time.Sleep(200 * time.Millisecond)
m.RecordLatency(id, next())
}

lat := m.LatencyEWMA(id)
Expand Down
15 changes: 3 additions & 12 deletions pb/pstore.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pstoreds/cyclic_batch.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package pstoreds

import (
"github.com/pkg/errors"
"errors"
"fmt"

ds "github.com/ipfs/go-datastore"
)
Expand Down Expand Up @@ -39,10 +40,10 @@ func (cb *cyclicBatch) cycle() (err error) {
}
// commit and renew the batch.
if err = cb.Batch.Commit(); err != nil {
return errors.Wrap(err, "failed while committing cyclic batch")
return fmt.Errorf("failed while committing cyclic batch: %w", err)
}
if cb.Batch, err = cb.ds.Batch(); err != nil {
return errors.Wrap(err, "failed while renewing cyclic batch")
return fmt.Errorf("failed while renewing cyclic batch: %w", err)
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions pstoreds/ds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func badgerStore(tb testing.TB) (ds.Batching, func()) {
return store, closer
}

//lint:ignore U1000 disabled for now
func leveldbStore(tb testing.TB) (ds.TxnDatastore, func()) {
dataPath, err := ioutil.TempDir(os.TempDir(), "leveldb")
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions pstoreds/peerstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func DefaultOpts() Options {
type pstoreds struct {
peerstore.Metrics

dsKeyBook
dsAddrBook
dsProtoBook
dsPeerMetadata
*dsKeyBook
*dsAddrBook
*dsProtoBook
*dsPeerMetadata
}

// NewPeerstore creates a peerstore backed by the provided persistent datastore.
Expand All @@ -79,10 +79,10 @@ func NewPeerstore(ctx context.Context, store ds.Batching, opts Options) (*pstore

ps := &pstoreds{
Metrics: pstore.NewMetrics(),
dsKeyBook: *keyBook,
dsAddrBook: *addrBook,
dsPeerMetadata: *peerMetadata,
dsProtoBook: *protoBook,
dsKeyBook: keyBook,
dsAddrBook: addrBook,
dsPeerMetadata: peerMetadata,
dsProtoBook: protoBook,
}
return ps, nil
}
Expand Down
30 changes: 12 additions & 18 deletions pstoremem/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewAddrBook() *memoryAddrBook {

ab := &memoryAddrBook{
segments: func() (ret addrSegments) {
for i, _ := range ret {
for i := range ret {
ret[i] = &addrSegment{
addrs: make(map[peer.ID]map[string]*expiringAddr),
signedPeerRecords: make(map[peer.ID]*peerRecordState)}
Expand Down Expand Up @@ -136,7 +136,7 @@ func (mab *memoryAddrBook) PeersWithAddrs() peer.IDSlice {
for _, s := range mab.segments {
s.RLock()
for pid, amap := range s.addrs {
if amap != nil && len(amap) > 0 {
if len(amap) > 0 {
pidSet.Add(pid)
}
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func (mab *memoryAddrBook) ConsumePeerRecord(recordEnvelope *record.Envelope, tt

func (mab *memoryAddrBook) addAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
if err := p.Validate(); err != nil {
log.Warningf("tried to set addrs for invalid peer ID %s: %s", p, err)
log.Warnw("tried to set addrs for invalid peer ID", "peer", p, "error", err)
return
}

Expand All @@ -223,7 +223,7 @@ func (mab *memoryAddrBook) addAddrsUnlocked(s *addrSegment, p peer.ID, addrs []m
addrSet := make(map[string]struct{}, len(addrs))
for _, addr := range addrs {
if addr == nil {
log.Warnf("was passed nil multiaddr for %s", p)
log.Warnw("was passed nil multiaddr", "peer", p)
continue
}
k := string(addr.Bytes())
Expand Down Expand Up @@ -253,7 +253,7 @@ func (mab *memoryAddrBook) addAddrsUnlocked(s *addrSegment, p peer.ID, addrs []m
// SetAddr calls mgr.SetAddrs(p, addr, ttl)
func (mab *memoryAddrBook) SetAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) {
if err := p.Validate(); err != nil {
log.Warningf("tried to set addrs for invalid peer ID %s: %s", p, err)
log.Warnw("tried to set addrs for invalid peer ID", "peer", p, "error", err)
return
}

Expand All @@ -264,7 +264,7 @@ func (mab *memoryAddrBook) SetAddr(p peer.ID, addr ma.Multiaddr, ttl time.Durati
// This is used when we receive the best estimate of the validity of an address.
func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
if err := p.Validate(); err != nil {
log.Warningf("tried to set addrs for invalid peer ID %s: %s", p, err)
log.Warnw("tried to set addrs for invalid peer ID", "peer", p, "error", err)
return
}

Expand All @@ -281,7 +281,7 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
exp := time.Now().Add(ttl)
for _, addr := range addrs {
if addr == nil {
log.Warnf("was passed nil multiaddr for %s", p)
log.Warnw("was passed nil multiaddr", "peer", p)
continue
}
aBytes := addr.Bytes()
Expand All @@ -306,7 +306,7 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
// the given oldTTL to have the given newTTL.
func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL time.Duration) {
if err := p.Validate(); err != nil {
log.Warningf("tried to set addrs for invalid peer ID %s: %s", p, err)
log.Warnw("tried to set addrs for invalid peer ID", "peer", p, "error", err)
return
}

Expand Down Expand Up @@ -412,7 +412,7 @@ func (mab *memoryAddrBook) ClearAddrs(p peer.ID) {
// given peer ID will be published.
func (mab *memoryAddrBook) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Multiaddr {
if err := p.Validate(); err != nil {
log.Warningf("tried to get addrs for invalid peer ID %s: %s", p, err)
log.Warnw("tried to set addrs for invalid peer ID", "peer", p, "error", err)
ch := make(chan ma.Multiaddr)
close(ch)
return ch
Expand All @@ -432,10 +432,8 @@ func (mab *memoryAddrBook) AddrStream(ctx context.Context, p peer.ID) <-chan ma.
}

type addrSub struct {
pubch chan ma.Multiaddr
lk sync.Mutex
buffer []ma.Multiaddr
ctx context.Context
pubch chan ma.Multiaddr
ctx context.Context
}

func (s *addrSub) pubAddr(a ma.Multiaddr) {
Expand Down Expand Up @@ -503,11 +501,7 @@ func (mgr *AddrSubManager) AddrStream(ctx context.Context, p peer.ID, initial []
out := make(chan ma.Multiaddr)

mgr.mu.Lock()
if _, ok := mgr.subs[p]; ok {
mgr.subs[p] = append(mgr.subs[p], sub)
} else {
mgr.subs[p] = []*addrSub{sub}
}
mgr.subs[p] = append(mgr.subs[p], sub)
mgr.mu.Unlock()

sort.Sort(addr.AddrList(initial))
Expand Down
2 changes: 1 addition & 1 deletion test/addr_book_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func testAddAddress(ab pstore.AddrBook) func(*testing.T) {
// 1 second left
ab.AddAddrs(id, addrs, 3*time.Second)
// 3 seconds left
time.Sleep(2)
time.Sleep(2 * time.Second)
// 1 seconds left.

// We still have the address.
Expand Down
2 changes: 1 addition & 1 deletion test/benchmarks_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func benchmarkAddGetAndClearAddrs(ps pstore.Peerstore, addrs chan *peerpair) fun
func benchmarkGet1000PeersWithAddrs(ps pstore.Peerstore, addrs chan *peerpair) func(*testing.B) {
return func(b *testing.B) {
var peers = make([]*peerpair, 1000)
for i, _ := range peers {
for i := range peers {
pp := <-addrs
ps.AddAddrs(pp.ID, pp.Addr, pstore.PermanentAddrTTL)
peers[i] = pp
Expand Down
3 changes: 3 additions & 0 deletions test/keybook_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ func testKeyBookPeers(kb pstore.KeyBook) func(t *testing.T) {

// Add a private key.
priv, _, err := pt.RandTestKeyPair(ic.RSA, 2048)
if err != nil {
t.Fatal(err)
}
p2, err := peer.IDFromPrivateKey(priv)
if err != nil {
t.Fatal(err)
Expand Down