Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay committed Oct 28, 2022
1 parent 88ad12e commit e74e851
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
27 changes: 24 additions & 3 deletions nodebuilder/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package p2p

import (
"context"
"fmt"

"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
pubsub "github.com/libp2p/go-libp2p-pubsub"
rcmgr "github.com/libp2p/go-libp2p-resource-manager"
"github.com/libp2p/go-libp2p/p2p/host/autonat"
"github.com/libp2p/go-libp2p/p2p/net/conngater"
)
Expand Down Expand Up @@ -35,7 +38,8 @@ type API struct {
type Module interface {
// Info returns basic information about the node's p2p host/operations.
Info() Info
// Peers returns all peer IDs used across all inner stores. // TODO should we return the entire peerstore or just peers that are actively connected?
// Peers returns all peer IDs used across all inner stores. // TODO should we
// return the entire peerstore or just peers that are actively connected?
Peers() peer.IDSlice
// PeerInfo returns a small slice of information Peerstore has on the
// given peer.
Expand All @@ -51,7 +55,8 @@ type Module interface {
// NATStatus returns the current NAT status.
NATStatus() network.Reachability

// BlockPeer adds a peer to the set of blocked peers. // TODO should we wrap BlockPeer so that it 1. disconnects from peer, then 2. adds to blocklist?
// BlockPeer adds a peer to the set of blocked peers. // TODO should we wrap BlockPeer so
// that it 1. disconnects from peer, then 2. adds to blocklist?
BlockPeer(p peer.ID) error
// UnblockPeer removes a peer from the set of blocked peers.
UnblockPeer(p peer.ID) error
Expand All @@ -64,7 +69,8 @@ type Module interface {
// MutualRm removes a peer from the list of peers who have a bidirectional
// peering agreement that they are protected from being trimmed, dropped
// or negatively scored, returning a bool representing whether the given
// peer is protected or not. // TODO should we represent this bool as an error? For example, if it's still protected after the call, we can return error that it was unsuccessful.
// peer is protected or not. // TODO should we represent this bool as an error? For example, if
// it's still protected after the call, we can return error that it was unsuccessful.
MutualRm(id peer.ID, tag string) bool
// IsMutual returns whether the given peer is a mutual peer.
IsMutual(id peer.ID, tag string) bool
Expand All @@ -79,6 +85,9 @@ type Module interface {
// BandwidthForProtocol returns a Stats struct with bandwidth metrics associated with the given protocol.ID.
BandwidthForProtocol(proto protocol.ID) metrics.Stats

// ResourceState returns the state of the resource manager.
ResourceState() (rcmgr.ResourceManagerStat, error)

// PubSubPeers returns the peer IDs of the peers joined on
// the given topic.
PubSubPeers(topic string) []peer.ID
Expand All @@ -91,6 +100,7 @@ type manager struct { // TODO @renaynay: rename
nat autonat.AutoNAT
connGater *conngater.BasicConnectionGater
bw *metrics.BandwidthCounter
rm network.ResourceManager
}

func newManager(
Expand All @@ -99,13 +109,15 @@ func newManager(
nat autonat.AutoNAT,
cg *conngater.BasicConnectionGater,
bw *metrics.BandwidthCounter,
rm network.ResourceManager,
) Module {
return &manager{
host: host,
ps: ps,
nat: nat,
connGater: cg,
bw: bw,
rm: rm,
}
}

Expand Down Expand Up @@ -182,6 +194,15 @@ func (m *manager) BandwidthForProtocol(proto protocol.ID) metrics.Stats {
return m.bw.GetBandwidthForProtocol(proto)
}

func (m *manager) ResourceState() (rcmgr.ResourceManagerStat, error) {
rms, ok := m.rm.(rcmgr.ResourceManagerState)
if !ok {
return rcmgr.ResourceManagerStat{}, fmt.Errorf("network.ResourceManager does not implement " +
"rcmgr.ResourceManagerState") // TODO err msg?
}
return rms.Stat(), nil
}

func (m *manager) PubSubPeers(topic string) []peer.ID {
return m.ps.ListPeers(topic)
}
38 changes: 8 additions & 30 deletions nodebuilder/p2p/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package p2p

import (
"context"
"crypto/rand"
"github.com/ipfs/go-datastore"
"testing"
"time"

"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/crypto"
libhost "github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network"
Expand All @@ -17,7 +15,6 @@ import (
"github.com/libp2p/go-libp2p/p2p/host/autonat"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/require"
"go.uber.org/fx"
)

// TestP2PModule_methodsOnHost TODO
Expand All @@ -26,7 +23,7 @@ func TestP2PModule_methodsOnHost(t *testing.T) {
require.NoError(t, err)
host, peer := net.Hosts()[0], net.Hosts()[1]

mgr := newManager(host, nil, nil, nil, nil)
mgr := newManager(host, nil, nil, nil, nil, nil)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

Expand All @@ -43,7 +40,7 @@ func TestP2PModule_methodsOnHost(t *testing.T) {
require.NoError(t, mgr.Connect(ctx, *libhost.InfoFromHost(peer)))
mgr.MutualAdd(peer.ID(), "test")
isMutual := mgr.IsMutual(peer.ID(), "test") // TODO why fails?
//require.True(t, isMutual) // tODO why fails?
// require.True(t, isMutual) // tODO why fails?
require.Equal(t, host.ConnManager().IsProtected(peer.ID(), "test"), isMutual)
}

Expand All @@ -56,7 +53,7 @@ func TestP2PModule_methodsOnAutonat(t *testing.T) {
nat, err := autonat.New(host)
require.NoError(t, err)

mgr := newManager(host, nil, nat, nil, nil)
mgr := newManager(host, nil, nat, nil, nil, nil)

// test all methods on `manager.nat`
require.Equal(t, nat.Status(), mgr.NATStatus())
Expand Down Expand Up @@ -84,7 +81,7 @@ func TestP2PModule_methodsOnBandwidth(t *testing.T) {
t.Log("HITTING!") // TODO remove
})

mgr := newManager(host, nil, nil, nil, bw)
mgr := newManager(host, nil, nil, nil, bw, nil)

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
Expand Down Expand Up @@ -126,7 +123,7 @@ func TestP2PModule_methodsOnPubsub(t *testing.T) {
gs, err := pubsub.NewGossipSub(ctx, host)
require.NoError(t, err)

mgr := newManager(host, gs, nil, nil, nil)
mgr := newManager(host, gs, nil, nil, nil, nil)

topicStr := "test-topic"

Expand All @@ -150,37 +147,18 @@ func TestP2PModule_methodsOnPubsub(t *testing.T) {
// give for some peers to properly join the topic
time.Sleep(1 * time.Second)

require.Equal(t, topic.ListPeers(), mgr.PubSubPeers(topicStr))
require.Equal(t, len(topic.ListPeers()), len(mgr.PubSubPeers(topicStr)))
}

// TestP2PModule_methodsOnConnGater // TODO doc
func TestP2PModule_methodsOnConnGater(t *testing.T) {
gater, err := ConnectionGater(datastore.NewMapDatastore())
require.NoError(t, err)

mgr := newManager(nil, nil, nil, gater, nil)
mgr := newManager(nil, nil, nil, gater, nil, nil)

require.NoError(t, mgr.BlockPeer("badpeer"))
require.Len(t, mgr.ListBlockedPeers(), 1)
require.NoError(t, mgr.UnblockPeer("badpeer"))
require.Len(t, mgr.ListBlockedPeers(), 0)
}

// dummyLc is needed to mock out fx.Lifecycle
type dummyLc struct{}

func (dl *dummyLc) Append(fx.Hook) {}

func generatePrivKey(t *testing.T) crypto.PrivKey {
t.Helper()
priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
require.NoError(t, err)

bytes, err := crypto.MarshalPrivateKey(priv)
require.NoError(t, err)

key, err := crypto.UnmarshalPrivateKey(bytes)
require.NoError(t, err)

return key
}
1 change: 1 addition & 0 deletions nodebuilder/rpc/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rpc

import (
"context"

"go.uber.org/fx"

"github.com/celestiaorg/celestia-node/api/rpc"
Expand Down

0 comments on commit e74e851

Please sign in to comment.