Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
bf3945d6
Commit
bf3945d6
authored
Mar 22, 2023
by
Andreas Bigger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
place string parsing in band scorer constructor
parent
539b7504
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
29 additions
and
31 deletions
+29
-31
band_scorer_test.go
op-node/p2p/band_scorer_test.go
+10
-10
load_config.go
op-node/p2p/cli/load_config.go
+2
-2
peer_scorer.go
op-node/p2p/peer_scorer.go
+11
-15
peer_scorer_test.go
op-node/p2p/peer_scorer_test.go
+3
-2
peer_scores_test.go
op-node/p2p/peer_scores_test.go
+3
-2
No files found.
op-node/p2p/band_scorer_test.go
View file @
bf3945d6
...
...
@@ -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
)
...
...
op-node/p2p/cli/load_config.go
View file @
bf3945d6
...
...
@@ -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
...
...
op-node/p2p/peer_scorer.go
View file @
bf3945d6
...
...
@@ -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 [BandScore
r
] instance.
func
NewBandScorer
(
)
*
BandScoreThresholds
{
return
&
BandScoreThresholds
{
// NewBandScorer constructs a new [BandScore
Thresholds
] 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.
...
...
op-node/p2p/peer_scorer_test.go
View file @
bf3945d6
...
...
@@ -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
)
}
...
...
op-node/p2p/peer_scores_test.go
View file @
bf3945d6
...
...
@@ -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
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment