Commit f2e442b9 authored by Adrian Sutton's avatar Adrian Sutton

op-node: Remove old band score metric.

parent e399e495
...@@ -36,12 +36,10 @@ var ( ...@@ -36,12 +36,10 @@ var (
} }
PeerScoreBands = cli.StringFlag{ PeerScoreBands = cli.StringFlag{
Name: "p2p.score.bands", Name: "p2p.score.bands",
Usage: "Sets the peer score bands used primarily for peer score metrics. " + Usage: "Deprecated. This option is ignored and is only present for backwards compatibility.",
"Should be provided in following format: <threshold>:<label>;<threshold>:<label>;..." +
"For example: -40:graylist;-20:restricted;0:nopx;20:friend;",
Required: false, Required: false,
Value: "-40:<=-40;-10:<=-10;-5:<=-05;-0.05:<=-00.05;0:<=0;0.05:<=00.05;5:<=05;10:<=10;20:<=20;100:>20;", Value: "",
EnvVar: p2pEnv("SCORE_BANDS"), Hidden: true,
} }
// Banning Flag - whether or not we want to act on the scoring // Banning Flag - whether or not we want to act on the scoring
......
...@@ -67,7 +67,7 @@ type Metricer interface { ...@@ -67,7 +67,7 @@ type Metricer interface {
Document() []metrics.DocumentedMetric Document() []metrics.DocumentedMetric
RecordChannelInputBytes(num int) RecordChannelInputBytes(num int)
// P2P Metrics // P2P Metrics
SetPeerScores(scores map[string]float64, allScores []store.PeerScores) SetPeerScores(allScores []store.PeerScores)
ClientPayloadByNumberEvent(num uint64, resultCode byte, duration time.Duration) ClientPayloadByNumberEvent(num uint64, resultCode byte, duration time.Duration)
ServerPayloadByNumberEvent(num uint64, resultCode byte, duration time.Duration) ServerPayloadByNumberEvent(num uint64, resultCode byte, duration time.Duration)
PayloadsQuarantineSize(n int) PayloadsQuarantineSize(n int)
...@@ -135,14 +135,13 @@ type Metrics struct { ...@@ -135,14 +135,13 @@ type Metrics struct {
// P2P Metrics // P2P Metrics
PeerCount prometheus.Gauge PeerCount prometheus.Gauge
StreamCount prometheus.Gauge StreamCount prometheus.Gauge
PeerScores *prometheus.GaugeVec
GossipEventsTotal *prometheus.CounterVec GossipEventsTotal *prometheus.CounterVec
BandwidthTotal *prometheus.GaugeVec BandwidthTotal *prometheus.GaugeVec
PeerUnbans prometheus.Counter PeerUnbans prometheus.Counter
IPUnbans prometheus.Counter IPUnbans prometheus.Counter
Dials *prometheus.CounterVec Dials *prometheus.CounterVec
Accepts *prometheus.CounterVec Accepts *prometheus.CounterVec
PeerScoresHistogram *prometheus.HistogramVec PeerScores *prometheus.HistogramVec
ChannelInputBytes prometheus.Counter ChannelInputBytes prometheus.Counter
...@@ -311,22 +310,9 @@ func NewMetrics(procName string) *Metrics { ...@@ -311,22 +310,9 @@ func NewMetrics(procName string) *Metrics {
Name: "peer_count", Name: "peer_count",
Help: "Count of currently connected p2p peers", Help: "Count of currently connected p2p peers",
}), }),
// Notice: We cannot use peer ids as [Labels] in the GaugeVec PeerScores: factory.NewHistogramVec(prometheus.HistogramOpts{
// since peer ids would open a service attack vector.
// Each peer id would be a separate metric, flooding prometheus.
//
// [Labels]: https://prometheus.io/docs/practices/naming/#labels
PeerScores: factory.NewGaugeVec(prometheus.GaugeOpts{
Namespace: ns, Namespace: ns,
Subsystem: "p2p",
Name: "peer_scores", Name: "peer_scores",
Help: "Count of peer scores grouped by score",
}, []string{
"band",
}),
PeerScoresHistogram: factory.NewHistogramVec(prometheus.HistogramOpts{
Namespace: ns,
Name: "peer_scores_histogram",
Help: "Histogram of currrently connected peer scores", Help: "Histogram of currrently connected peer scores",
Buckets: []float64{-100, -40, -20, -10, -5, -2, -1, -0.5, -0.05, 0, 0.05, 0.5, 1, 2, 5, 10, 20, 40}, Buckets: []float64{-100, -40, -20, -10, -5, -2, -1, -0.5, -0.05, 0, 0.05, 0.5, 1, 2, 5, 10, 20, 40},
}, []string{"type"}), }, []string{"type"}),
...@@ -459,20 +445,17 @@ func NewMetrics(procName string) *Metrics { ...@@ -459,20 +445,17 @@ func NewMetrics(procName string) *Metrics {
} }
} }
// SetPeerScores updates the peer score [prometheus.GaugeVec]. // SetPeerScores updates the peer score metrics.
// This takes a map of labels to scores. // Accepts a slice of peer scores in any order.
func (m *Metrics) SetPeerScores(scores map[string]float64, allScores []store.PeerScores) { func (m *Metrics) SetPeerScores(allScores []store.PeerScores) {
for _, scores := range allScores { for _, scores := range allScores {
m.PeerScoresHistogram.WithLabelValues("total").Observe(scores.Gossip.Total) m.PeerScores.WithLabelValues("total").Observe(scores.Gossip.Total)
m.PeerScoresHistogram.WithLabelValues("ipColocation").Observe(scores.Gossip.IPColocationFactor) m.PeerScores.WithLabelValues("ipColocation").Observe(scores.Gossip.IPColocationFactor)
m.PeerScoresHistogram.WithLabelValues("behavioralPenalty").Observe(scores.Gossip.BehavioralPenalty) m.PeerScores.WithLabelValues("behavioralPenalty").Observe(scores.Gossip.BehavioralPenalty)
m.PeerScoresHistogram.WithLabelValues("blocksFirstMessage").Observe(scores.Gossip.Blocks.FirstMessageDeliveries) m.PeerScores.WithLabelValues("blocksFirstMessage").Observe(scores.Gossip.Blocks.FirstMessageDeliveries)
m.PeerScoresHistogram.WithLabelValues("blocksTimeInMesh").Observe(scores.Gossip.Blocks.TimeInMesh) m.PeerScores.WithLabelValues("blocksTimeInMesh").Observe(scores.Gossip.Blocks.TimeInMesh)
m.PeerScoresHistogram.WithLabelValues("blocksMessageDeliveries").Observe(scores.Gossip.Blocks.MeshMessageDeliveries) m.PeerScores.WithLabelValues("blocksMessageDeliveries").Observe(scores.Gossip.Blocks.MeshMessageDeliveries)
m.PeerScoresHistogram.WithLabelValues("blocksInvalidMessageDeliveries").Observe(scores.Gossip.Blocks.InvalidMessageDeliveries) m.PeerScores.WithLabelValues("blocksInvalidMessageDeliveries").Observe(scores.Gossip.Blocks.InvalidMessageDeliveries)
}
for label, score := range scores {
m.PeerScores.WithLabelValues(label).Set(score)
} }
} }
...@@ -803,7 +786,7 @@ func (n *noopMetricer) RecordSequencerReset() { ...@@ -803,7 +786,7 @@ func (n *noopMetricer) RecordSequencerReset() {
func (n *noopMetricer) RecordGossipEvent(evType int32) { func (n *noopMetricer) RecordGossipEvent(evType int32) {
} }
func (n *noopMetricer) SetPeerScores(scores map[string]float64, allScores []store.PeerScores) { func (n *noopMetricer) SetPeerScores(allScores []store.PeerScores) {
} }
func (n *noopMetricer) IncPeerCount() { func (n *noopMetricer) IncPeerCount() {
......
package p2p
import (
"testing"
"github.com/stretchr/testify/require"
)
// TestBandScorer_ParseDefault tests the [BandScorer.Parse] function
// on the default band scores cli flag value.
func TestBandScorer_ParseDefault(t *testing.T) {
// Create a new band scorer.
bandScorer, err := NewBandScorer("-40:graylist;-20:restricted;0:nopx;20:friend;")
require.NoError(t, err)
// Validate the [BandScorer] internals.
require.ElementsMatch(t, bandScorer.bands, []scorePair{
{band: "graylist", threshold: -40},
{band: "restricted", threshold: -20},
{band: "nopx", threshold: 0},
{band: "friend", threshold: 20},
})
}
// TestBandScorer_BucketCorrectly tests the [BandScorer.Bucket] function
// on a variety of scores.
func TestBandScorer_BucketCorrectly(t *testing.T) {
// Create a new band scorer.
bandScorer, err := NewBandScorer("-40:graylist;-20:restricted;0:nopx;20:friend;")
require.NoError(t, err)
// Validate the [BandScorer] internals.
require.Equal(t, bandScorer.Bucket(-100), "graylist")
require.Equal(t, bandScorer.Bucket(-40), "graylist")
require.Equal(t, bandScorer.Bucket(-39), "restricted")
require.Equal(t, bandScorer.Bucket(-20), "restricted")
require.Equal(t, bandScorer.Bucket(-19), "nopx")
require.Equal(t, bandScorer.Bucket(0), "nopx")
require.Equal(t, bandScorer.Bucket(1), "friend")
require.Equal(t, bandScorer.Bucket(20), "friend")
require.Equal(t, bandScorer.Bucket(21), "friend")
}
// TestBandScorer_BucketInverted tests the [BandScorer.Bucket] function
// on a variety of scores, in descending order.
func TestBandScorer_BucketInverted(t *testing.T) {
// Create a new band scorer.
bandScorer, err := NewBandScorer("20:friend;0:nopx;-20:restricted;-40:graylist;")
require.NoError(t, err)
// Validate the [BandScorer] internals.
require.Equal(t, bandScorer.Bucket(-100), "graylist")
require.Equal(t, bandScorer.Bucket(-40), "graylist")
require.Equal(t, bandScorer.Bucket(-39), "restricted")
require.Equal(t, bandScorer.Bucket(-20), "restricted")
require.Equal(t, bandScorer.Bucket(-19), "nopx")
require.Equal(t, bandScorer.Bucket(0), "nopx")
require.Equal(t, bandScorer.Bucket(1), "friend")
require.Equal(t, bandScorer.Bucket(20), "friend")
require.Equal(t, bandScorer.Bucket(21), "friend")
}
// TestBandScorer_ParseEmpty tests the [BandScorer.Parse] function
// on an empty string.
func TestBandScorer_ParseEmpty(t *testing.T) {
// Create a band scorer on an empty string.
bandScorer, err := NewBandScorer("")
require.NoError(t, err)
// Validate the [BandScorer] internals.
require.Len(t, bandScorer.bands, 0)
}
// TestBandScorer_ParseWhitespace tests the [BandScorer.Parse] function
// on a variety of whitespaced strings.
func TestBandScorer_ParseWhitespace(t *testing.T) {
// Create a band scorer on an empty string.
bandScorer, err := NewBandScorer(" ; ; ; ")
require.NoError(t, err)
// Validate the [BandScorer] internals.
require.Len(t, bandScorer.bands, 0)
}
...@@ -58,10 +58,6 @@ func NewConfig(ctx *cli.Context, blockTime uint64) (*p2p.Config, error) { ...@@ -58,10 +58,6 @@ func NewConfig(ctx *cli.Context, blockTime uint64) (*p2p.Config, error) {
return nil, fmt.Errorf("failed to load p2p peer scoring options: %w", err) return nil, fmt.Errorf("failed to load p2p peer scoring options: %w", err)
} }
if err := loadPeerScoreBands(conf, ctx); err != nil {
return nil, fmt.Errorf("failed to load p2p peer score bands: %w", err)
}
if err := loadBanningOptions(conf, ctx); err != nil { if err := loadBanningOptions(conf, ctx); err != nil {
return nil, fmt.Errorf("failed to load banning option: %w", err) return nil, fmt.Errorf("failed to load banning option: %w", err)
} }
...@@ -124,17 +120,6 @@ func loadPeerScoringParams(conf *p2p.Config, ctx *cli.Context, blockTime uint64) ...@@ -124,17 +120,6 @@ func loadPeerScoringParams(conf *p2p.Config, ctx *cli.Context, blockTime uint64)
return nil return nil
} }
// loadPeerScoreBands loads [p2p.BandScorer] from the CLI context.
func loadPeerScoreBands(conf *p2p.Config, ctx *cli.Context) error {
scoreBands := ctx.GlobalString(flags.PeerScoreBands.Name)
bandScorer, err := p2p.NewBandScorer(scoreBands)
if err != nil {
return err
}
conf.BandScoreThresholds = *bandScorer
return nil
}
// loadBanningOptions loads whether or not to ban peers from the CLI context. // loadBanningOptions loads whether or not to ban peers from the CLI context.
func loadBanningOptions(conf *p2p.Config, ctx *cli.Context) error { func loadBanningOptions(conf *p2p.Config, ctx *cli.Context) error {
conf.BanningEnabled = ctx.GlobalBool(flags.Banning.Name) conf.BanningEnabled = ctx.GlobalBool(flags.Banning.Name)
......
...@@ -47,7 +47,6 @@ type SetupP2P interface { ...@@ -47,7 +47,6 @@ type SetupP2P interface {
BanPeers() bool BanPeers() bool
BanThreshold() float64 BanThreshold() float64
BanDuration() time.Duration BanDuration() time.Duration
PeerBandScorer() *BandScoreThresholds
GossipSetupConfigurables GossipSetupConfigurables
ReqRespSyncEnabled() bool ReqRespSyncEnabled() bool
} }
...@@ -67,9 +66,6 @@ type Config struct { ...@@ -67,9 +66,6 @@ type Config struct {
PeerScoring pubsub.PeerScoreParams PeerScoring pubsub.PeerScoreParams
TopicScoring pubsub.TopicScoreParams TopicScoring pubsub.TopicScoreParams
// Peer Score Band Thresholds
BandScoreThresholds BandScoreThresholds
// Whether to ban peers based on their [PeerScoring] score. Should be negative. // Whether to ban peers based on their [PeerScoring] score. Should be negative.
BanningEnabled bool BanningEnabled bool
// Minimum score before peers are disconnected and banned // Minimum score before peers are disconnected and banned
...@@ -142,10 +138,6 @@ func (conf *Config) PeerScoringParams() *pubsub.PeerScoreParams { ...@@ -142,10 +138,6 @@ func (conf *Config) PeerScoringParams() *pubsub.PeerScoreParams {
return &conf.PeerScoring return &conf.PeerScoring
} }
func (conf *Config) PeerBandScorer() *BandScoreThresholds {
return &conf.BandScoreThresholds
}
func (conf *Config) BanPeers() bool { func (conf *Config) BanPeers() bool {
return conf.BanningEnabled return conf.BanningEnabled
} }
......
...@@ -13,9 +13,9 @@ type ScoreMetrics struct { ...@@ -13,9 +13,9 @@ type ScoreMetrics struct {
mock.Mock mock.Mock
} }
// SetPeerScores provides a mock function with given fields: _a0, _a1 // SetPeerScores provides a mock function with given fields: _a0
func (_m *ScoreMetrics) SetPeerScores(_a0 map[string]float64, _a1 []store.PeerScores) { func (_m *ScoreMetrics) SetPeerScores(_a0 []store.PeerScores) {
_m.Called(_a0, _a1) _m.Called(_a0)
} }
type mockConstructorTestingTNewScoreMetrics interface { type mockConstructorTestingTNewScoreMetrics interface {
......
...@@ -118,7 +118,7 @@ func (_c *PeerManager_GetPeerScore_Call) RunAndReturn(run func(peer.ID) (float64 ...@@ -118,7 +118,7 @@ func (_c *PeerManager_GetPeerScore_Call) RunAndReturn(run func(peer.ID) (float64
return _c return _c
} }
// IsProtected provides a mock function with given fields: _a0 // IsStatic provides a mock function with given fields: _a0
func (_m *PeerManager) IsStatic(_a0 peer.ID) bool { func (_m *PeerManager) IsStatic(_a0 peer.ID) bool {
ret := _m.Called(_a0) ret := _m.Called(_a0)
...@@ -132,30 +132,30 @@ func (_m *PeerManager) IsStatic(_a0 peer.ID) bool { ...@@ -132,30 +132,30 @@ func (_m *PeerManager) IsStatic(_a0 peer.ID) bool {
return r0 return r0
} }
// PeerManager_IsProtected_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsStatic' // PeerManager_IsStatic_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsStatic'
type PeerManager_IsProtected_Call struct { type PeerManager_IsStatic_Call struct {
*mock.Call *mock.Call
} }
// IsProtected is a helper method to define mock.On call // IsStatic is a helper method to define mock.On call
// - _a0 peer.ID // - _a0 peer.ID
func (_e *PeerManager_Expecter) IsProtected(_a0 interface{}) *PeerManager_IsProtected_Call { func (_e *PeerManager_Expecter) IsStatic(_a0 interface{}) *PeerManager_IsStatic_Call {
return &PeerManager_IsProtected_Call{Call: _e.mock.On("IsStatic", _a0)} return &PeerManager_IsStatic_Call{Call: _e.mock.On("IsStatic", _a0)}
} }
func (_c *PeerManager_IsProtected_Call) Run(run func(_a0 peer.ID)) *PeerManager_IsProtected_Call { func (_c *PeerManager_IsStatic_Call) Run(run func(_a0 peer.ID)) *PeerManager_IsStatic_Call {
_c.Call.Run(func(args mock.Arguments) { _c.Call.Run(func(args mock.Arguments) {
run(args[0].(peer.ID)) run(args[0].(peer.ID))
}) })
return _c return _c
} }
func (_c *PeerManager_IsProtected_Call) Return(_a0 bool) *PeerManager_IsProtected_Call { func (_c *PeerManager_IsStatic_Call) Return(_a0 bool) *PeerManager_IsStatic_Call {
_c.Call.Return(_a0) _c.Call.Return(_a0)
return _c return _c
} }
func (_c *PeerManager_IsProtected_Call) RunAndReturn(run func(peer.ID) bool) *PeerManager_IsProtected_Call { func (_c *PeerManager_IsStatic_Call) RunAndReturn(run func(peer.ID) bool) *PeerManager_IsStatic_Call {
_c.Call.Return(run) _c.Call.Return(run)
return _c return _c
} }
......
...@@ -96,7 +96,7 @@ func TestCheckNextPeer(t *testing.T) { ...@@ -96,7 +96,7 @@ func TestCheckNextPeer(t *testing.T) {
id := peerIDs[0] id := peerIDs[0]
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().IsStatic(id).Return(false).Once()
manager.EXPECT().BanPeer(id, clock.Now().Add(testBanDuration)).Return(nil).Once() manager.EXPECT().BanPeer(id, clock.Now().Add(testBanDuration)).Return(nil).Once()
require.NoError(t, monitor.checkNextPeer()) require.NoError(t, monitor.checkNextPeer())
...@@ -107,7 +107,7 @@ func TestCheckNextPeer(t *testing.T) { ...@@ -107,7 +107,7 @@ func TestCheckNextPeer(t *testing.T) {
id := peerIDs[0] id := peerIDs[0]
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(true) manager.EXPECT().IsStatic(id).Return(true)
require.NoError(t, monitor.checkNextPeer()) require.NoError(t, monitor.checkNextPeer())
}) })
......
...@@ -117,7 +117,7 @@ func (n *NodeP2P) init(resourcesCtx context.Context, rollupCfg *rollup.Config, l ...@@ -117,7 +117,7 @@ func (n *NodeP2P) init(resourcesCtx context.Context, rollupCfg *rollup.Config, l
return fmt.Errorf("cannot init without extended peerstore: %w", err) return fmt.Errorf("cannot init without extended peerstore: %w", err)
} }
n.store = eps n.store = eps
n.scorer = NewScorer(rollupCfg, eps, metrics, setup.PeerBandScorer(), log) n.scorer = NewScorer(rollupCfg, eps, metrics, log)
n.host.Network().Notify(&network.NotifyBundle{ n.host.Network().Notify(&network.NotifyBundle{
ConnectedF: func(_ network.Network, conn network.Conn) { ConnectedF: func(_ network.Network, conn network.Conn) {
n.scorer.OnConnect(conn.RemotePeer()) n.scorer.OnConnect(conn.RemotePeer())
......
package p2p package p2p
import ( import (
"fmt"
"sort"
"strconv"
"strings"
"time" "time"
"github.com/ethereum-optimism/optimism/op-node/rollup" "github.com/ethereum-optimism/optimism/op-node/rollup"
...@@ -19,71 +15,9 @@ type scorer struct { ...@@ -19,71 +15,9 @@ type scorer struct {
peerStore Peerstore peerStore Peerstore
metricer ScoreMetrics metricer ScoreMetrics
log log.Logger log log.Logger
bandScoreThresholds *BandScoreThresholds
cfg *rollup.Config cfg *rollup.Config
} }
// scorePair holds a band and its corresponding threshold.
type scorePair struct {
band string
threshold float64
}
// BandScoreThresholds holds the thresholds for classifying peers
// into different score bands.
type BandScoreThresholds struct {
bands []scorePair
}
// NewBandScorer constructs a new [BandScoreThresholds] instance.
func NewBandScorer(str string) (*BandScoreThresholds, error) {
s := &BandScoreThresholds{
bands: make([]scorePair, 0),
}
for _, band := range strings.Split(str, ";") {
// Skip empty band strings.
band := strings.TrimSpace(band)
if band == "" {
continue
}
split := strings.Split(band, ":")
if len(split) != 2 {
return nil, fmt.Errorf("invalid score band: %s", band)
}
threshold, err := strconv.ParseFloat(split[0], 64)
if err != nil {
return nil, err
}
s.bands = append(s.bands, scorePair{
band: split[1],
threshold: threshold,
})
}
// Order the bands by threshold in ascending order.
sort.Slice(s.bands, func(i, j int) bool {
return s.bands[i].threshold < s.bands[j].threshold
})
return s, nil
}
// Bucket returns the appropriate band for a given score.
func (s *BandScoreThresholds) Bucket(score float64) string {
for _, pair := range s.bands {
if score <= pair.threshold {
return pair.band
}
}
// If there is no band threshold higher than the score,
// the peer must be placed in the highest bucket.
if len(s.bands) > 0 {
return s.bands[len(s.bands)-1].band
}
return ""
}
// Peerstore is a subset of the libp2p peerstore.Peerstore interface. // Peerstore is a subset of the libp2p peerstore.Peerstore interface.
// //
//go:generate mockery --name Peerstore --output mocks/ //go:generate mockery --name Peerstore --output mocks/
...@@ -108,16 +42,15 @@ type Scorer interface { ...@@ -108,16 +42,15 @@ type Scorer interface {
//go:generate mockery --name ScoreMetrics --output mocks/ //go:generate mockery --name ScoreMetrics --output mocks/
type ScoreMetrics interface { type ScoreMetrics interface {
SetPeerScores(map[string]float64, []store.PeerScores) SetPeerScores([]store.PeerScores)
} }
// NewScorer returns a new peer scorer. // NewScorer returns a new peer scorer.
func NewScorer(cfg *rollup.Config, peerStore Peerstore, metricer ScoreMetrics, bandScoreThresholds *BandScoreThresholds, log log.Logger) Scorer { func NewScorer(cfg *rollup.Config, peerStore Peerstore, metricer ScoreMetrics, log log.Logger) Scorer {
return &scorer{ return &scorer{
peerStore: peerStore, peerStore: peerStore,
metricer: metricer, metricer: metricer,
log: log, log: log,
bandScoreThresholds: bandScoreThresholds,
cfg: cfg, cfg: cfg,
} }
} }
...@@ -129,12 +62,7 @@ func NewScorer(cfg *rollup.Config, peerStore Peerstore, metricer ScoreMetrics, b ...@@ -129,12 +62,7 @@ func NewScorer(cfg *rollup.Config, peerStore Peerstore, metricer ScoreMetrics, b
func (s *scorer) SnapshotHook() pubsub.ExtendedPeerScoreInspectFn { func (s *scorer) SnapshotHook() pubsub.ExtendedPeerScoreInspectFn {
blocksTopicName := blocksTopicV1(s.cfg) blocksTopicName := blocksTopicV1(s.cfg)
return func(m map[peer.ID]*pubsub.PeerScoreSnapshot) { return func(m map[peer.ID]*pubsub.PeerScoreSnapshot) {
scoreMap := make(map[string]float64)
allScores := make([]store.PeerScores, 0, len(m)) allScores := make([]store.PeerScores, 0, len(m))
// Zero out all bands.
for _, b := range s.bandScoreThresholds.bands {
scoreMap[b.band] = 0
}
// Now set the new scores. // Now set the new scores.
for id, snap := range m { for id, snap := range m {
diff := store.GossipScores{ diff := store.GossipScores{
...@@ -155,11 +83,7 @@ func (s *scorer) SnapshotHook() pubsub.ExtendedPeerScoreInspectFn { ...@@ -155,11 +83,7 @@ func (s *scorer) SnapshotHook() pubsub.ExtendedPeerScoreInspectFn {
allScores = append(allScores, peerScores) allScores = append(allScores, peerScores)
} }
} }
for _, snap := range m { s.metricer.SetPeerScores(allScores)
band := s.bandScoreThresholds.Bucket(snap.Score)
scoreMap[band] += 1
}
s.metricer.SetPeerScores(scoreMap, allScores)
} }
} }
......
...@@ -23,7 +23,6 @@ type PeerScorerTestSuite struct { ...@@ -23,7 +23,6 @@ type PeerScorerTestSuite struct {
mockStore *p2pMocks.Peerstore mockStore *p2pMocks.Peerstore
mockMetricer *p2pMocks.ScoreMetrics mockMetricer *p2pMocks.ScoreMetrics
bandScorer *p2p.BandScoreThresholds
logger log.Logger logger log.Logger
} }
...@@ -31,9 +30,6 @@ type PeerScorerTestSuite struct { ...@@ -31,9 +30,6 @@ type PeerScorerTestSuite struct {
func (testSuite *PeerScorerTestSuite) SetupTest() { func (testSuite *PeerScorerTestSuite) SetupTest() {
testSuite.mockStore = &p2pMocks.Peerstore{} testSuite.mockStore = &p2pMocks.Peerstore{}
testSuite.mockMetricer = &p2pMocks.ScoreMetrics{} testSuite.mockMetricer = &p2pMocks.ScoreMetrics{}
bandScorer, err := p2p.NewBandScorer("-40:graylist;0:friend;")
testSuite.NoError(err)
testSuite.bandScorer = bandScorer
testSuite.logger = testlog.Logger(testSuite.T(), log.LvlError) testSuite.logger = testlog.Logger(testSuite.T(), log.LvlError)
} }
...@@ -48,7 +44,6 @@ func (testSuite *PeerScorerTestSuite) TestScorer_OnConnect() { ...@@ -48,7 +44,6 @@ func (testSuite *PeerScorerTestSuite) TestScorer_OnConnect() {
&rollup.Config{L2ChainID: big.NewInt(123)}, &rollup.Config{L2ChainID: big.NewInt(123)},
testSuite.mockStore, testSuite.mockStore,
testSuite.mockMetricer, testSuite.mockMetricer,
testSuite.bandScorer,
testSuite.logger, testSuite.logger,
) )
scorer.OnConnect(peer.ID("alice")) scorer.OnConnect(peer.ID("alice"))
...@@ -60,7 +55,6 @@ func (testSuite *PeerScorerTestSuite) TestScorer_OnDisconnect() { ...@@ -60,7 +55,6 @@ func (testSuite *PeerScorerTestSuite) TestScorer_OnDisconnect() {
&rollup.Config{L2ChainID: big.NewInt(123)}, &rollup.Config{L2ChainID: big.NewInt(123)},
testSuite.mockStore, testSuite.mockStore,
testSuite.mockMetricer, testSuite.mockMetricer,
testSuite.bandScorer,
testSuite.logger, testSuite.logger,
) )
scorer.OnDisconnect(peer.ID("alice")) scorer.OnDisconnect(peer.ID("alice"))
...@@ -72,20 +66,16 @@ func (testSuite *PeerScorerTestSuite) TestScorer_SnapshotHook() { ...@@ -72,20 +66,16 @@ func (testSuite *PeerScorerTestSuite) TestScorer_SnapshotHook() {
&rollup.Config{L2ChainID: big.NewInt(123)}, &rollup.Config{L2ChainID: big.NewInt(123)},
testSuite.mockStore, testSuite.mockStore,
testSuite.mockMetricer, testSuite.mockMetricer,
testSuite.bandScorer,
testSuite.logger, testSuite.logger,
) )
inspectFn := scorer.SnapshotHook() inspectFn := scorer.SnapshotHook()
scores := store.PeerScores{} scores := store.PeerScores{Gossip: store.GossipScores{Total: 3}}
// Expect updating the peer store // Expect updating the peer store
testSuite.mockStore.On("SetScore", peer.ID("peer1"), &store.GossipScores{Total: float64(-100)}).Return(scores, nil).Once() testSuite.mockStore.On("SetScore", peer.ID("peer1"), &store.GossipScores{Total: float64(-100)}).Return(scores, nil).Once()
// The metricer should then be called with the peer score band map // The metricer should then be called with the peer score band map
testSuite.mockMetricer.On("SetPeerScores", map[string]float64{ testSuite.mockMetricer.On("SetPeerScores", []store.PeerScores{scores}).Return(nil).Once()
"friend": 0,
"graylist": 1,
}, []store.PeerScores{scores}).Return(nil).Once()
// Apply the snapshot // Apply the snapshot
snapshotMap := map[peer.ID]*pubsub.PeerScoreSnapshot{ snapshotMap := map[peer.ID]*pubsub.PeerScoreSnapshot{
...@@ -99,10 +89,7 @@ func (testSuite *PeerScorerTestSuite) TestScorer_SnapshotHook() { ...@@ -99,10 +89,7 @@ func (testSuite *PeerScorerTestSuite) TestScorer_SnapshotHook() {
testSuite.mockStore.On("SetScore", peer.ID("peer1"), &store.GossipScores{Total: 0}).Return(scores, nil).Once() testSuite.mockStore.On("SetScore", peer.ID("peer1"), &store.GossipScores{Total: 0}).Return(scores, nil).Once()
// The metricer should then be called with the peer score band map // The metricer should then be called with the peer score band map
testSuite.mockMetricer.On("SetPeerScores", map[string]float64{ testSuite.mockMetricer.On("SetPeerScores", []store.PeerScores{scores}).Return(nil).Once()
"friend": 1,
"graylist": 0,
}, []store.PeerScores{scores}).Return(nil).Once()
// Apply the snapshot // Apply the snapshot
snapshotMap = map[peer.ID]*pubsub.PeerScoreSnapshot{ snapshotMap = map[peer.ID]*pubsub.PeerScoreSnapshot{
......
...@@ -37,7 +37,6 @@ type PeerScoresTestSuite struct { ...@@ -37,7 +37,6 @@ type PeerScoresTestSuite struct {
mockStore *p2pMocks.Peerstore mockStore *p2pMocks.Peerstore
mockMetricer *p2pMocks.ScoreMetrics mockMetricer *p2pMocks.ScoreMetrics
bandScorer BandScoreThresholds
logger log.Logger logger log.Logger
} }
...@@ -45,9 +44,6 @@ type PeerScoresTestSuite struct { ...@@ -45,9 +44,6 @@ type PeerScoresTestSuite struct {
func (testSuite *PeerScoresTestSuite) SetupTest() { func (testSuite *PeerScoresTestSuite) SetupTest() {
testSuite.mockStore = &p2pMocks.Peerstore{} testSuite.mockStore = &p2pMocks.Peerstore{}
testSuite.mockMetricer = &p2pMocks.ScoreMetrics{} testSuite.mockMetricer = &p2pMocks.ScoreMetrics{}
bandScorer, err := NewBandScorer("0:graylist;")
testSuite.NoError(err)
testSuite.bandScorer = *bandScorer
testSuite.logger = testlog.Logger(testSuite.T(), log.LvlError) testSuite.logger = testlog.Logger(testSuite.T(), log.LvlError)
} }
...@@ -104,9 +100,8 @@ func newGossipSubs(testSuite *PeerScoresTestSuite, ctx context.Context, hosts [] ...@@ -104,9 +100,8 @@ func newGossipSubs(testSuite *PeerScoresTestSuite, ctx context.Context, hosts []
scorer := NewScorer( scorer := NewScorer(
&rollup.Config{L2ChainID: big.NewInt(123)}, &rollup.Config{L2ChainID: big.NewInt(123)},
extPeerStore, testSuite.mockMetricer, &testSuite.bandScorer, logger) extPeerStore, testSuite.mockMetricer, logger)
opts = append(opts, ConfigurePeerScoring(&Config{ opts = append(opts, ConfigurePeerScoring(&Config{
BandScoreThresholds: testSuite.bandScorer,
PeerScoring: pubsub.PeerScoreParams{ PeerScoring: pubsub.PeerScoreParams{
AppSpecificScore: func(p peer.ID) float64 { AppSpecificScore: func(p peer.ID) float64 {
if p == hosts[0].ID() { if p == hosts[0].ID() {
......
...@@ -73,10 +73,6 @@ func (p *Prepared) PeerScoringParams() *pubsub.PeerScoreParams { ...@@ -73,10 +73,6 @@ func (p *Prepared) PeerScoringParams() *pubsub.PeerScoreParams {
return nil return nil
} }
func (p *Prepared) PeerBandScorer() *BandScoreThresholds {
return nil
}
func (p *Prepared) BanPeers() bool { func (p *Prepared) BanPeers() bool {
return false return false
} }
......
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