Commit 4ff4d356 authored by Andreas Bigger's avatar Andreas Bigger

fix formatting and lints

parent 9cbd2055
......@@ -336,8 +336,6 @@ func parsePriv(data string) (*crypto.Secp256k1PrivateKey, error) {
return (p).(*crypto.Secp256k1PrivateKey), nil
}
func loadGossipOptions(conf *p2p.Config, ctx *cli.Context) error {
conf.MeshD = ctx.GlobalInt(flags.GossipMeshDFlag.Name)
conf.MeshDLo = ctx.GlobalInt(flags.GossipMeshDloFlag.Name)
......
......@@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode"
ds "github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core"
"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/crypto"
......@@ -19,7 +20,6 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/net/conngater"
cmgr "github.com/libp2p/go-libp2p/p2p/net/connmgr"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/ethereum-optimism/optimism/op-node/rollup"
)
......
......@@ -168,7 +168,7 @@ func TestP2PFull(t *testing.T) {
_, err = p2pClientA.DiscoveryTable(ctx)
// rpc does not preserve error type
require.Equal(t, err.Error(), DisabledDiscovery.Error(), "expecting discv5 to be disabled")
require.Equal(t, err.Error(), ErrDisabledDiscovery.Error(), "expecting discv5 to be disabled")
require.NoError(t, p2pClientA.BlockPeer(ctx, hostB.ID()))
blockedPeers, err := p2pClientA.ListBlockedPeers(ctx)
......
......@@ -76,7 +76,6 @@ func (n *NodeP2P) init(resourcesCtx context.Context, rollupCfg *rollup.Config, l
// notify of any new connections/streams/etc.
n.host.Network().Notify(NewNetworkNotifier(log, metrics))
// note: the IDDelta functionality was removed from libP2P, and no longer needs to be explicitly disabled.
fmt.Printf("Constructing gossip sub...\n")
n.gs, err = NewGossipSub(resourcesCtx, n.host, n.gater, rollupCfg, setup, metrics, log)
if err != nil {
return fmt.Errorf("failed to start gossipsub router: %w", err)
......
package p2p
import (
slices "golang.org/x/exp/slices"
log "github.com/ethereum/go-ethereum/log"
peer "github.com/libp2p/go-libp2p/core/peer"
slices "golang.org/x/exp/slices"
)
// ConnectionFactor is the factor by which we multiply the connection score.
......
......@@ -3,12 +3,12 @@ package p2p_test
import (
"testing"
peer "github.com/libp2p/go-libp2p/core/peer"
node "github.com/ethereum-optimism/optimism/op-node/node"
p2p "github.com/ethereum-optimism/optimism/op-node/p2p"
p2pMocks "github.com/ethereum-optimism/optimism/op-node/p2p/mocks"
log "github.com/ethereum/go-ethereum/log"
node "github.com/ethereum-optimism/optimism/op-node/node"
peer "github.com/libp2p/go-libp2p/core/peer"
suite "github.com/stretchr/testify/suite"
p2pMocks "github.com/ethereum-optimism/optimism/op-node/p2p/mocks"
)
// PeerGaterTestSuite tests peer parameterization.
......@@ -65,4 +65,3 @@ func (testSuite *PeerGaterTestSuite) TestPeerGaterUpdateNoBanning() {
// Notice: [BlockPeer] should not be called since banning is not enabled
gater.Update(peer.ID("peer1"), float64(-100000))
}
package p2p
import (
"sort"
"math"
"time"
"sort"
"testing"
"time"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/stretchr/testify/suite"
......@@ -55,13 +55,13 @@ func (testSuite *PeerParamsTestSuite) TestGetPeerScoreParams() {
testSuite.NoError(err)
expected := LightPeerScoreParams(1)
testSuite.Equal(expected.DecayInterval, params.DecayInterval)
testSuite.Equal(time.Duration(1) * time.Second, params.DecayInterval)
testSuite.Equal(time.Duration(1)*time.Second, params.DecayInterval)
params, err = GetPeerScoreParams("none", 1)
testSuite.NoError(err)
expected = DisabledPeerScoreParams(1)
testSuite.Equal(expected.DecayInterval, params.DecayInterval)
testSuite.Equal(time.Duration(1) * time.Second, params.DecayInterval)
testSuite.Equal(time.Duration(1)*time.Second, params.DecayInterval)
_, err = GetPeerScoreParams("invalid", 1)
testSuite.Error(err)
......@@ -76,7 +76,7 @@ func (testSuite *PeerParamsTestSuite) TestLightPeerScoreParams() {
// calculate the behavior penalty decay
duration := 10 * epoch
decay := math.Pow(DecayToZero, 1 / float64(duration / slot))
decay := math.Pow(DecayToZero, 1/float64(duration/slot))
testSuite.Equal(0.9261187281287935, decay)
// Test the params
......@@ -106,7 +106,7 @@ func (testSuite *PeerParamsTestSuite) TestDisabledPeerScoreParams() {
// calculate the behavior penalty decay
duration := 10 * epoch
decay := math.Pow(DecayToZero, 1 / float64(duration / slot))
decay := math.Pow(DecayToZero, 1/float64(duration/slot))
testSuite.Equal(0.9261187281287935, decay)
// Test the params
......
......@@ -2,8 +2,8 @@ package p2p
import (
log "github.com/ethereum/go-ethereum/log"
peer "github.com/libp2p/go-libp2p/core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
peer "github.com/libp2p/go-libp2p/core/peer"
)
type scorer struct {
......
......@@ -3,13 +3,13 @@ package p2p_test
import (
"testing"
peer "github.com/libp2p/go-libp2p/core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
node "github.com/ethereum-optimism/optimism/op-node/node"
p2p "github.com/ethereum-optimism/optimism/op-node/p2p"
p2pMocks "github.com/ethereum-optimism/optimism/op-node/p2p/mocks"
log "github.com/ethereum/go-ethereum/log"
node "github.com/ethereum-optimism/optimism/op-node/node"
pubsub "github.com/libp2p/go-libp2p-pubsub"
peer "github.com/libp2p/go-libp2p/core/peer"
suite "github.com/stretchr/testify/suite"
p2pMocks "github.com/ethereum-optimism/optimism/op-node/p2p/mocks"
)
// PeerScorerTestSuite tests peer parameterization.
......@@ -79,7 +79,7 @@ func (testSuite *PeerScorerTestSuite) TestSnapshotHook() {
// Apply the snapshot
snapshotMap := map[peer.ID]*pubsub.PeerScoreSnapshot{
peer.ID("peer1"): &pubsub.PeerScoreSnapshot{
peer.ID("peer1"): {
Score: -100,
},
}
......@@ -106,7 +106,7 @@ func (testSuite *PeerScorerTestSuite) TestSnapshotHookBlockPeer() {
// Apply the snapshot
snapshotMap := map[peer.ID]*pubsub.PeerScoreSnapshot{
peer.ID("peer1"): &pubsub.PeerScoreSnapshot{
peer.ID("peer1"): {
Score: -101,
},
}
......
package p2p_test
import (
"context"
"fmt"
"time"
"math/rand"
"context"
"testing"
"time"
p2p "github.com/ethereum-optimism/optimism/op-node/p2p"
node "github.com/ethereum-optimism/optimism/op-node/node"
testlog "github.com/ethereum-optimism/optimism/op-node/testlog"
p2p "github.com/ethereum-optimism/optimism/op-node/p2p"
p2pMocks "github.com/ethereum-optimism/optimism/op-node/p2p/mocks"
testlog "github.com/ethereum-optimism/optimism/op-node/testlog"
mock "github.com/stretchr/testify/mock"
suite "github.com/stretchr/testify/suite"
log "github.com/ethereum/go-ethereum/log"
peer "github.com/libp2p/go-libp2p/core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
host "github.com/libp2p/go-libp2p/core/host"
peer "github.com/libp2p/go-libp2p/core/peer"
bhost "github.com/libp2p/go-libp2p/p2p/host/blank"
tswarm "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
)
......@@ -139,7 +139,9 @@ func (testSuite *PeerScoresTestSuite) TestNegativeScores() {
// Create subscriptions
var subs []*pubsub.Subscription
for _, ps := range pubsubs {
sub, err := ps.Subscribe("test")
topic, err := ps.Join("test")
testSuite.NoError(err)
sub, err := topic.Subscribe()
testSuite.NoError(err)
subs = append(subs, sub)
}
......@@ -148,7 +150,10 @@ func (testSuite *PeerScoresTestSuite) TestNegativeScores() {
time.Sleep(3 * time.Second)
for i := 0; i < 20; i++ {
msg := []byte(fmt.Sprintf("message %d", i))
pubsubs[i%20].Publish("test", msg)
topic, err := pubsubs[i%20].Join("test")
testSuite.NoError(err)
err = topic.Publish(ctx, msg)
testSuite.NoError(err)
time.Sleep(20 * time.Millisecond)
}
......@@ -158,7 +163,7 @@ func (testSuite *PeerScoresTestSuite) TestNegativeScores() {
// Collects all messages from a subscription
collectAll := func(sub *pubsub.Subscription) []*pubsub.Message {
var res []*pubsub.Message
ctx, cancel := context.WithTimeout(context.Background(), 100 * time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
for {
msg, err := sub.Next(ctx)
......@@ -182,6 +187,4 @@ func (testSuite *PeerScoresTestSuite) TestNegativeScores() {
testSuite.NotEqual(hosts[0].ID(), m.ReceivedFrom)
}
}
}
......@@ -30,9 +30,9 @@ import (
// - banning peers based on score
var (
DisabledDiscovery = errors.New("discovery disabled")
NoConnectionManager = errors.New("no connection manager")
NoConnectionGater = errors.New("no connection gater")
ErrDisabledDiscovery = errors.New("discovery disabled")
ErrNoConnectionManager = errors.New("no connection manager")
ErrNoConnectionGater = errors.New("no connection gater")
)
type Node interface {
......@@ -236,7 +236,7 @@ func (s *APIBackend) DiscoveryTable(_ context.Context) ([]*enode.Node, error) {
if dv5 := s.node.Dv5Udp(); dv5 != nil {
return dv5.AllNodes(), nil
} else {
return nil, DisabledDiscovery
return nil, ErrDisabledDiscovery
}
}
......@@ -244,7 +244,7 @@ func (s *APIBackend) BlockPeer(_ context.Context, p peer.ID) error {
recordDur := s.m.RecordRPCServerRequest("opp2p_blockPeer")
defer recordDur()
if gater := s.node.ConnectionGater(); gater == nil {
return NoConnectionGater
return ErrNoConnectionGater
} else {
return gater.BlockPeer(p)
}
......@@ -254,7 +254,7 @@ func (s *APIBackend) UnblockPeer(_ context.Context, p peer.ID) error {
recordDur := s.m.RecordRPCServerRequest("opp2p_unblockPeer")
defer recordDur()
if gater := s.node.ConnectionGater(); gater == nil {
return NoConnectionGater
return ErrNoConnectionGater
} else {
return gater.UnblockPeer(p)
}
......@@ -264,7 +264,7 @@ func (s *APIBackend) ListBlockedPeers(_ context.Context) ([]peer.ID, error) {
recordDur := s.m.RecordRPCServerRequest("opp2p_listBlockedPeers")
defer recordDur()
if gater := s.node.ConnectionGater(); gater == nil {
return nil, NoConnectionGater
return nil, ErrNoConnectionGater
} else {
return gater.ListBlockedPeers(), nil
}
......@@ -276,7 +276,7 @@ func (s *APIBackend) BlockAddr(_ context.Context, ip net.IP) error {
recordDur := s.m.RecordRPCServerRequest("opp2p_blockAddr")
defer recordDur()
if gater := s.node.ConnectionGater(); gater == nil {
return NoConnectionGater
return ErrNoConnectionGater
} else {
return gater.BlockAddr(ip)
}
......@@ -286,7 +286,7 @@ func (s *APIBackend) UnblockAddr(_ context.Context, ip net.IP) error {
recordDur := s.m.RecordRPCServerRequest("opp2p_unblockAddr")
defer recordDur()
if gater := s.node.ConnectionGater(); gater == nil {
return NoConnectionGater
return ErrNoConnectionGater
} else {
return gater.UnblockAddr(ip)
}
......@@ -296,7 +296,7 @@ func (s *APIBackend) ListBlockedAddrs(_ context.Context) ([]net.IP, error) {
recordDur := s.m.RecordRPCServerRequest("opp2p_listBlockedAddrs")
defer recordDur()
if gater := s.node.ConnectionGater(); gater == nil {
return nil, NoConnectionGater
return nil, ErrNoConnectionGater
} else {
return gater.ListBlockedAddrs(), nil
}
......@@ -308,7 +308,7 @@ func (s *APIBackend) BlockSubnet(_ context.Context, ipnet *net.IPNet) error {
recordDur := s.m.RecordRPCServerRequest("opp2p_blockSubnet")
defer recordDur()
if gater := s.node.ConnectionGater(); gater == nil {
return NoConnectionGater
return ErrNoConnectionGater
} else {
return gater.BlockSubnet(ipnet)
}
......@@ -318,7 +318,7 @@ func (s *APIBackend) UnblockSubnet(_ context.Context, ipnet *net.IPNet) error {
recordDur := s.m.RecordRPCServerRequest("opp2p_unblockSubnet")
defer recordDur()
if gater := s.node.ConnectionGater(); gater == nil {
return NoConnectionGater
return ErrNoConnectionGater
} else {
return gater.UnblockSubnet(ipnet)
}
......@@ -328,7 +328,7 @@ func (s *APIBackend) ListBlockedSubnets(_ context.Context) ([]*net.IPNet, error)
recordDur := s.m.RecordRPCServerRequest("opp2p_listBlockedSubnets")
defer recordDur()
if gater := s.node.ConnectionGater(); gater == nil {
return nil, NoConnectionGater
return nil, ErrNoConnectionGater
} else {
return gater.ListBlockedSubnets(), nil
}
......@@ -338,7 +338,7 @@ func (s *APIBackend) ProtectPeer(_ context.Context, p peer.ID) error {
recordDur := s.m.RecordRPCServerRequest("opp2p_protectPeer")
defer recordDur()
if manager := s.node.ConnectionManager(); manager == nil {
return NoConnectionManager
return ErrNoConnectionManager
} else {
manager.Protect(p, "api-protected")
return nil
......@@ -349,7 +349,7 @@ func (s *APIBackend) UnprotectPeer(_ context.Context, p peer.ID) error {
recordDur := s.m.RecordRPCServerRequest("opp2p_unprotectPeer")
defer recordDur()
if manager := s.node.ConnectionManager(); manager == nil {
return NoConnectionManager
return ErrNoConnectionManager
} else {
manager.Unprotect(p, "api-protected")
return nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment