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
c4f8e11e
Unverified
Commit
c4f8e11e
authored
May 22, 2023
by
Adrian Sutton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
op-node: Switch peer score setter to use an enum for score type.
parent
a163d0cc
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
30 additions
and
15 deletions
+30
-15
go.mod
go.mod
+1
-1
iface.go
op-node/p2p/store/iface.go
+8
-2
scorebook.go
op-node/p2p/store/scorebook.go
+7
-4
scorebook_test.go
op-node/p2p/store/scorebook_test.go
+12
-6
serialize_test.go
op-node/p2p/store/serialize_test.go
+2
-2
No files found.
go.mod
View file @
c4f8e11e
...
...
@@ -31,7 +31,6 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/prometheus/client_golang v1.14.0
github.com/schollz/progressbar/v3 v3.13.0
github.com/status-im/keycard-go v0.2.0
github.com/stretchr/testify v1.8.1
github.com/urfave/cli v1.22.9
github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa
...
...
@@ -163,6 +162,7 @@ require (
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/status-im/keycard-go v0.2.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
...
...
op-node/p2p/store/iface.go
View file @
c4f8e11e
...
...
@@ -9,13 +9,19 @@ type PeerScores struct {
Gossip
float64
}
type
ScoreType
int
const
(
TypeGossip
ScoreType
=
iota
)
// ScoreDatastore defines a type-safe API for getting and setting libp2p peer score information
type
ScoreDatastore
interface
{
// GetPeerScores returns the current scores for the specified peer
GetPeerScores
(
id
peer
.
ID
)
(
PeerScores
,
error
)
// Set
GossipScore stores the latest gossip score for a peer
Set
GossipScore
(
id
peer
.
ID
,
score
float64
)
error
// Set
Score stores the latest score for the specified peer and score type
Set
Score
(
id
peer
.
ID
,
scoreType
ScoreType
,
score
float64
)
error
}
// ExtendedPeerstore defines a type-safe API to work with additional peer metadata based on a libp2p peerstore.Peerstore
...
...
op-node/p2p/store/scorebook.go
View file @
c4f8e11e
...
...
@@ -21,8 +21,6 @@ type scoreBook struct {
var
scoresBase
=
ds
.
NewKey
(
"/peers/scores"
)
type
ScoreType
string
const
(
scoreDataV0
=
"0"
scoreCacheSize
=
100
...
...
@@ -65,14 +63,19 @@ func (d *scoreBook) getPeerScoresNoLock(id peer.ID) (PeerScores, error) {
return
scores
,
nil
}
func
(
d
*
scoreBook
)
Set
GossipScore
(
id
peer
.
ID
,
score
float64
)
error
{
func
(
d
*
scoreBook
)
Set
Score
(
id
peer
.
ID
,
scoreType
ScoreType
,
score
float64
)
error
{
d
.
Lock
()
defer
d
.
Unlock
()
scores
,
err
:=
d
.
getPeerScoresNoLock
(
id
)
if
err
!=
nil
{
return
err
}
scores
.
Gossip
=
score
switch
scoreType
{
case
TypeGossip
:
scores
.
Gossip
=
score
default
:
return
fmt
.
Errorf
(
"unknown score type: %v"
,
scoreType
)
}
data
,
err
:=
serializeScoresV0
(
scores
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"encode scores for peer %v: %w"
,
id
,
err
)
...
...
op-node/p2p/store/scorebook_test.go
View file @
c4f8e11e
...
...
@@ -21,7 +21,7 @@ func TestRoundTripGossipScore(t *testing.T) {
id
:=
peer
.
ID
(
"aaaa"
)
store
:=
createMemoryStore
(
t
)
score
:=
123.45
err
:=
store
.
Set
GossipScore
(
id
,
score
)
err
:=
store
.
Set
Score
(
id
,
TypeGossip
,
score
)
require
.
NoError
(
t
,
err
)
assertPeerScores
(
t
,
store
,
id
,
PeerScores
{
Gossip
:
score
})
...
...
@@ -31,8 +31,8 @@ func TestUpdateGossipScore(t *testing.T) {
id
:=
peer
.
ID
(
"aaaa"
)
store
:=
createMemoryStore
(
t
)
score
:=
123.45
require
.
NoError
(
t
,
store
.
Set
GossipScore
(
id
,
444.223
))
require
.
NoError
(
t
,
store
.
Set
GossipScore
(
id
,
score
))
require
.
NoError
(
t
,
store
.
Set
Score
(
id
,
TypeGossip
,
444.223
))
require
.
NoError
(
t
,
store
.
Set
Score
(
id
,
TypeGossip
,
score
))
assertPeerScores
(
t
,
store
,
id
,
PeerScores
{
Gossip
:
score
})
}
...
...
@@ -43,8 +43,8 @@ func TestStoreScoresForMultiplePeers(t *testing.T) {
store
:=
createMemoryStore
(
t
)
score1
:=
123.45
score2
:=
453.22
require
.
NoError
(
t
,
store
.
Set
GossipScore
(
id1
,
score1
))
require
.
NoError
(
t
,
store
.
Set
GossipScore
(
id2
,
score2
))
require
.
NoError
(
t
,
store
.
Set
Score
(
id1
,
TypeGossip
,
score1
))
require
.
NoError
(
t
,
store
.
Set
Score
(
id2
,
TypeGossip
,
score2
))
assertPeerScores
(
t
,
store
,
id1
,
PeerScores
{
Gossip
:
score1
})
assertPeerScores
(
t
,
store
,
id2
,
PeerScores
{
Gossip
:
score2
})
...
...
@@ -56,7 +56,7 @@ func TestPersistData(t *testing.T) {
backingStore
:=
sync
.
MutexWrap
(
ds
.
NewMapDatastore
())
store
:=
createPeerstoreWithBacking
(
t
,
backingStore
)
require
.
NoError
(
t
,
store
.
Set
GossipScore
(
id
,
score
))
require
.
NoError
(
t
,
store
.
Set
Score
(
id
,
TypeGossip
,
score
))
// Close and recreate a new store from the same backing
require
.
NoError
(
t
,
store
.
Close
())
...
...
@@ -65,6 +65,12 @@ func TestPersistData(t *testing.T) {
assertPeerScores
(
t
,
store
,
id
,
PeerScores
{
Gossip
:
score
})
}
func
TestUnknownScoreType
(
t
*
testing
.
T
)
{
store
:=
createMemoryStore
(
t
)
err
:=
store
.
SetScore
(
"aaaa"
,
92832
,
244.24
)
require
.
ErrorContains
(
t
,
err
,
"unknown score type"
)
}
func
assertPeerScores
(
t
*
testing
.
T
,
store
ExtendedPeerstore
,
id
peer
.
ID
,
expected
PeerScores
)
{
result
,
err
:=
store
.
GetPeerScores
(
id
)
require
.
NoError
(
t
,
err
)
...
...
op-node/p2p/store/serialize_test.go
View file @
c4f8e11e
...
...
@@ -3,7 +3,7 @@ package store
import
(
"testing"
"github.com/
status-im/keycard-go/hexutils
"
"github.com/
ethereum/go-ethereum/common
"
"github.com/stretchr/testify/require"
)
...
...
@@ -30,7 +30,7 @@ func TestParseHistoricSerializationsV0(t *testing.T) {
}{
{
name
:
"GossipOnly"
,
data
:
hexutils
.
HexTo
Bytes
(
"40934A18644523F6"
),
data
:
common
.
Hex2
Bytes
(
"40934A18644523F6"
),
expected
:
PeerScores
{
Gossip
:
1234.52382
},
},
}
...
...
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