Commit bf3945d6 authored by Andreas Bigger's avatar Andreas Bigger

place string parsing in band scorer constructor

parent 539b7504
......@@ -10,8 +10,8 @@ import (
// on the default band scores cli flag value.
func TestBandScorer_ParseDefault(t *testing.T) {
// Create a new band scorer.
bandScorer := NewBandScorer()
require.NoError(t, bandScorer.Parse("-40:graylist;-20:restricted;0:nopx;20:friend;"))
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{
......@@ -26,8 +26,8 @@ func TestBandScorer_ParseDefault(t *testing.T) {
// on a variety of scores.
func TestBandScorer_BucketCorrectly(t *testing.T) {
// Create a new band scorer.
bandScorer := NewBandScorer()
require.NoError(t, bandScorer.Parse("-40:graylist;-20:restricted;0:nopx;20:friend;"))
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")
......@@ -45,8 +45,8 @@ func TestBandScorer_BucketCorrectly(t *testing.T) {
// on a variety of scores, in descending order.
func TestBandScorer_BucketInverted(t *testing.T) {
// Create a new band scorer.
bandScorer := NewBandScorer()
require.NoError(t, bandScorer.Parse("20:friend;0:nopx;-20:restricted;-40:graylist;"))
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")
......@@ -64,8 +64,8 @@ func TestBandScorer_BucketInverted(t *testing.T) {
// on an empty string.
func TestBandScorer_ParseEmpty(t *testing.T) {
// Create a band scorer on an empty string.
bandScorer := NewBandScorer()
require.NoError(t, bandScorer.Parse(""))
bandScorer, err := NewBandScorer("")
require.NoError(t, err)
// Validate the [BandScorer] internals.
require.Len(t, bandScorer.bands, 0)
......@@ -75,8 +75,8 @@ func TestBandScorer_ParseEmpty(t *testing.T) {
// on a variety of whitespaced strings.
func TestBandScorer_ParseWhitespace(t *testing.T) {
// Create a band scorer on an empty string.
bandScorer := NewBandScorer()
require.NoError(t, bandScorer.Parse(" ; ; ; "))
bandScorer, err := NewBandScorer(" ; ; ; ")
require.NoError(t, err)
// Validate the [BandScorer] internals.
require.Len(t, bandScorer.bands, 0)
......
......@@ -128,8 +128,8 @@ func loadPeerScoringParams(conf *p2p.Config, ctx *cli.Context, blockTime uint64)
// loadPeerScoreBands loads [p2p.BandScorer] from the CLI context.
func loadPeerScoreBands(conf *p2p.Config, ctx *cli.Context) error {
scoreBands := ctx.GlobalString(flags.PeerScoreBands.Name)
bandScorer := p2p.NewBandScorer()
if err := bandScorer.Parse(scoreBands); err != nil {
bandScorer, err := p2p.NewBandScorer(scoreBands)
if err != nil {
return err
}
conf.BandScoreThresholds = bandScorer
......
......@@ -38,25 +38,16 @@ type BandScoreThresholds struct {
// [Parse] function and then expose the [Bucket] function for
// downstream [BandScorer] consumers.
type BandScorer interface {
Parse(str string) error
Bucket(score float64) string
Reset()
}
// NewBandScorer constructs a new [BandScorer] instance.
func NewBandScorer() *BandScoreThresholds {
return &BandScoreThresholds{
// NewBandScorer constructs a new [BandScoreThresholds] instance.
func NewBandScorer(str string) (*BandScoreThresholds, error) {
s := &BandScoreThresholds{
bands: make([]scorePair, 0),
}
}
// Reset wipes the internal state of the [BandScorer].
func (s *BandScoreThresholds) Reset() {
s.bands = s.bands[:0]
}
// Parse creates a [BandScorer] from a given string.
func (s *BandScoreThresholds) Parse(str string) error {
for _, band := range strings.Split(str, ";") {
// Skip empty band strings.
band := strings.TrimSpace(band)
......@@ -65,11 +56,11 @@ func (s *BandScoreThresholds) Parse(str string) error {
}
split := strings.Split(band, ":")
if len(split) != 2 {
return fmt.Errorf("invalid score band: %s", band)
return nil, fmt.Errorf("invalid score band: %s", band)
}
threshold, err := strconv.ParseFloat(split[0], 64)
if err != nil {
return err
return nil, err
}
s.bands = append(s.bands, scorePair{
band: split[1],
......@@ -82,7 +73,12 @@ func (s *BandScoreThresholds) Parse(str string) error {
return s.bands[i].threshold < s.bands[j].threshold
})
return nil
return s, nil
}
// Reset wipes the internal state of the [BandScorer].
func (s *BandScoreThresholds) Reset() {
s.bands = s.bands[:0]
}
// Bucket returns the appropriate band for a given score.
......
......@@ -29,8 +29,9 @@ func (testSuite *PeerScorerTestSuite) SetupTest() {
testSuite.mockGater = &p2pMocks.PeerGater{}
testSuite.mockStore = &p2pMocks.Peerstore{}
testSuite.mockMetricer = &p2pMocks.GossipMetricer{}
testSuite.bandScorer = &p2p.BandScoreThresholds{}
testSuite.NoError(testSuite.bandScorer.Parse("0:graylist;"))
bandScorer, err := p2p.NewBandScorer("0:graylist;")
testSuite.NoError(err)
testSuite.bandScorer = bandScorer
testSuite.logger = testlog.Logger(testSuite.T(), log.LvlError)
}
......
......@@ -39,8 +39,9 @@ func (testSuite *PeerScoresTestSuite) SetupTest() {
testSuite.mockGater = &p2pMocks.ConnectionGater{}
testSuite.mockStore = &p2pMocks.Peerstore{}
testSuite.mockMetricer = &p2pMocks.GossipMetricer{}
testSuite.bandScorer = &p2p.BandScoreThresholds{}
testSuite.NoError(testSuite.bandScorer.Parse("0:graylist;"))
bandScorer, err := p2p.NewBandScorer("0:graylist;")
testSuite.NoError(err)
testSuite.bandScorer = bandScorer
testSuite.logger = testlog.Logger(testSuite.T(), log.LvlError)
}
......
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