Commit a5ea097f authored by Adrian Sutton's avatar Adrian Sutton

op-node: Introduce suitable constants to avoid magic numbers in PeerMonitor

parent 60b81b39
...@@ -32,6 +32,11 @@ import ( ...@@ -32,6 +32,11 @@ import (
"github.com/ethereum-optimism/optimism/op-service/clock" "github.com/ethereum-optimism/optimism/op-service/clock"
) )
const (
staticPeerTag = "static"
minAcceptedPeerScore = -100
)
type ExtraHostFeatures interface { type ExtraHostFeatures interface {
host.Host host.Host
ConnectionGater() gating.BlockingConnectionGater ConnectionGater() gating.BlockingConnectionGater
...@@ -67,7 +72,7 @@ func (e *extraHost) initStaticPeers() { ...@@ -67,7 +72,7 @@ func (e *extraHost) initStaticPeers() {
e.Peerstore().AddAddrs(addr.ID, addr.Addrs, time.Hour*24*7) e.Peerstore().AddAddrs(addr.ID, addr.Addrs, time.Hour*24*7)
// We protect the peer, so the connection manager doesn't decide to prune it. // We protect the peer, so the connection manager doesn't decide to prune it.
// We tag it with "static" so other protects/unprotects with different tags don't affect this protection. // We tag it with "static" so other protects/unprotects with different tags don't affect this protection.
e.connMgr.Protect(addr.ID, "static") e.connMgr.Protect(addr.ID, staticPeerTag)
// Try to dial the node in the background // Try to dial the node in the background
go func(addr *peer.AddrInfo) { go func(addr *peer.AddrInfo) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
......
...@@ -12,8 +12,9 @@ import ( ...@@ -12,8 +12,9 @@ import (
) )
const ( const (
// Time delay between checking the score of each peer to avoid // Time delay between checking the score of each peer to avoid activity spikes
checkInterval = 1 * time.Second checkInterval = 1 * time.Second
banDuration = 1 * time.Hour
) )
//go:generate mockery --name PeerManager --output mocks/ --with-expecter=true //go:generate mockery --name PeerManager --output mocks/ --with-expecter=true
...@@ -35,7 +36,6 @@ type PeerMonitor struct { ...@@ -35,7 +36,6 @@ type PeerMonitor struct {
clock clock.Clock clock clock.Clock
manager PeerManager manager PeerManager
minScore float64 minScore float64
banDuration time.Duration
bgTasks sync.WaitGroup bgTasks sync.WaitGroup
...@@ -44,7 +44,7 @@ type PeerMonitor struct { ...@@ -44,7 +44,7 @@ type PeerMonitor struct {
nextPeerIdx int nextPeerIdx int
} }
func NewPeerMonitor(ctx context.Context, l log.Logger, clock clock.Clock, manager PeerManager, minScore float64, banDuration time.Duration) *PeerMonitor { func NewPeerMonitor(ctx context.Context, l log.Logger, clock clock.Clock, manager PeerManager, minScore float64) *PeerMonitor {
ctx, cancelFn := context.WithCancel(ctx) ctx, cancelFn := context.WithCancel(ctx)
return &PeerMonitor{ return &PeerMonitor{
ctx: ctx, ctx: ctx,
...@@ -53,7 +53,6 @@ func NewPeerMonitor(ctx context.Context, l log.Logger, clock clock.Clock, manage ...@@ -53,7 +53,6 @@ func NewPeerMonitor(ctx context.Context, l log.Logger, clock clock.Clock, manage
clock: clock, clock: clock,
manager: manager, manager: manager,
minScore: minScore, minScore: minScore,
banDuration: banDuration,
} }
} }
func (k *PeerMonitor) Start() { func (k *PeerMonitor) Start() {
...@@ -92,7 +91,7 @@ func (k *PeerMonitor) checkNextPeer() error { ...@@ -92,7 +91,7 @@ func (k *PeerMonitor) checkNextPeer() error {
if k.manager.IsStatic(id) { if k.manager.IsStatic(id) {
return nil return nil
} }
if err := k.manager.BanPeer(id, k.clock.Now().Add(k.banDuration)); err != nil { if err := k.manager.BanPeer(id, k.clock.Now().Add(banDuration)); err != nil {
return fmt.Errorf("banning peer %v: %w", id, err) return fmt.Errorf("banning peer %v: %w", id, err)
} }
......
...@@ -15,13 +15,11 @@ import ( ...@@ -15,13 +15,11 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
const monitorBanDuration = 10 * time.Minute
func peerMonitorSetup(t *testing.T) (*PeerMonitor, *clock2.DeterministicClock, *mocks.PeerManager) { func peerMonitorSetup(t *testing.T) (*PeerMonitor, *clock2.DeterministicClock, *mocks.PeerManager) {
l := testlog.Logger(t, log.LvlInfo) l := testlog.Logger(t, log.LvlInfo)
clock := clock2.NewDeterministicClock(time.UnixMilli(10000)) clock := clock2.NewDeterministicClock(time.UnixMilli(10000))
manager := mocks.NewPeerManager(t) manager := mocks.NewPeerManager(t)
monitor := NewPeerMonitor(context.Background(), l, clock, manager, -100, monitorBanDuration) monitor := NewPeerMonitor(context.Background(), l, clock, manager, -100)
return monitor, clock, manager return monitor, clock, manager
} }
...@@ -97,7 +95,7 @@ func TestCheckNextPeer(t *testing.T) { ...@@ -97,7 +95,7 @@ func TestCheckNextPeer(t *testing.T) {
manager.EXPECT().Peers().Return(peerIDs).Once() manager.EXPECT().Peers().Return(peerIDs).Once()
manager.EXPECT().GetPeerScore(id).Return(-101, nil).Once() manager.EXPECT().GetPeerScore(id).Return(-101, nil).Once()
manager.EXPECT().IsProtected(id).Return(false).Once() manager.EXPECT().IsProtected(id).Return(false).Once()
manager.EXPECT().BanPeer(id, clock.Now().Add(monitorBanDuration)).Return(nil).Once() manager.EXPECT().BanPeer(id, clock.Now().Add(banDuration)).Return(nil).Once()
require.NoError(t, monitor.checkNextPeer()) require.NoError(t, monitor.checkNextPeer())
}) })
......
...@@ -126,9 +126,7 @@ func (n *NodeP2P) init(resourcesCtx context.Context, rollupCfg *rollup.Config, l ...@@ -126,9 +126,7 @@ func (n *NodeP2P) init(resourcesCtx context.Context, rollupCfg *rollup.Config, l
n.scorer.OnDisconnect(conn.RemotePeer()) n.scorer.OnDisconnect(conn.RemotePeer())
}, },
}) })
// TODO: minScore shouldn't just be hard coded here n.peerMonitor = monitor.NewPeerMonitor(resourcesCtx, log, clock.SystemClock, n, minAcceptedPeerScore)
// TODO: Ban duration needs to be set sensibly and probably shouldn't be a magic number here
n.peerMonitor = monitor.NewPeerMonitor(resourcesCtx, log, clock.SystemClock, n, -100, 1*time.Hour)
// notify of any new connections/streams/etc. // notify of any new connections/streams/etc.
n.host.Network().Notify(NewNetworkNotifier(log, metrics)) n.host.Network().Notify(NewNetworkNotifier(log, metrics))
// note: the IDDelta functionality was removed from libP2P, and no longer needs to be explicitly disabled. // note: the IDDelta functionality was removed from libP2P, and no longer needs to be explicitly disabled.
...@@ -214,8 +212,7 @@ func (n *NodeP2P) GetPeerScore(id peer.ID) (float64, error) { ...@@ -214,8 +212,7 @@ func (n *NodeP2P) GetPeerScore(id peer.ID) (float64, error) {
} }
func (n *NodeP2P) IsStatic(id peer.ID) bool { func (n *NodeP2P) IsStatic(id peer.ID) bool {
// TODO: "static" constant should be shared with host layer rather than hard-coded here return n.connMgr != nil && n.connMgr.IsProtected(id, staticPeerTag)
return n.connMgr != nil && n.connMgr.IsProtected(id, "static")
} }
func (n *NodeP2P) BanPeer(id peer.ID, expiration time.Time) error { func (n *NodeP2P) BanPeer(id peer.ID, expiration time.Time) error {
......
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