Skip to content

Commit

Permalink
fixes peer score helper
Browse files Browse the repository at this point in the history
  • Loading branch information
thep2p committed Nov 8, 2022
1 parent 1c1c946 commit ab8365c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gossipsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -1936,10 +1936,10 @@ func (gs *GossipSubRouter) GetPeerScore() *PeerScore {
}

func (gs *GossipSubRouter) SetPeerScoreThresholds(thresholds *PeerScoreThresholds) {
gs.gossipThreshold = thresholds.GossipThreshold
gs.publishThreshold = thresholds.PublishThreshold
gs.graylistThreshold = thresholds.GraylistThreshold
gs.acceptPXThreshold = thresholds.AcceptPXThreshold
gs.gossipThreshold = thresholds.GossipThreshold
gs.opportunisticGraftThreshold = thresholds.OpportunisticGraftThreshold
}

Expand Down
4 changes: 2 additions & 2 deletions gossipsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2115,8 +2115,7 @@ func TestGossipsubPeerScoreInspect(t *testing.T) {

hosts := getNetHosts(t, ctx, 2)
inspector := &mockPeerScoreInspector{}
router1 := getGossipSubRouter(hosts[0], WithPeerScoreInspect(inspector.inspect, time.Second))
psub1 := getGossipSubWithRouter(ctx, hosts[0], router1,
psub1 := getGossipSub(ctx, hosts[0],
WithPeerScore(
&PeerScoreParams{
Topics: map[string]*TopicScoreParams{
Expand All @@ -2139,6 +2138,7 @@ func TestGossipsubPeerScoreInspect(t *testing.T) {
PublishThreshold: -10,
GraylistThreshold: -1000,
}),
WithPeerScoreInspect(inspector.inspect, time.Second),
)
psub2 := getGossipSub(ctx, hosts[1])
psubs := []*PubSub{psub1, psub2}
Expand Down
19 changes: 12 additions & 7 deletions score.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,31 @@ type TopicScoreSnapshot struct {
// components for debugging peer scoring.
//
// This option must be passed _after_ the WithPeerScore option.
func WithPeerScoreInspect(inspect interface{}, period time.Duration) GossipSubRouterOption {
return func(gs *GossipSubRouter) error {
if gs.score == nil {
func WithPeerScoreInspect(inspect interface{}, period time.Duration) Option {
return func(ps *PubSub) error {
gs, ok := ps.rt.(GossipPubSubRouter)
if !ok {
return fmt.Errorf("pubsub router is not gossipsub")
}

if gs.GetPeerScore() == nil {
return fmt.Errorf("peer scoring is not enabled")
}

if gs.score.inspect != nil || gs.score.inspectEx != nil {
if gs.GetPeerScore().inspect != nil || gs.GetPeerScore().inspectEx != nil {
return fmt.Errorf("duplicate peer score inspector")
}

switch i := inspect.(type) {
case PeerScoreInspectFn:
gs.score.inspect = i
gs.GetPeerScore().inspect = i
case ExtendedPeerScoreInspectFn:
gs.score.inspectEx = i
gs.GetPeerScore().inspectEx = i
default:
return fmt.Errorf("unknown peer score insector type: %v", inspect)
}

gs.score.inspectPeriod = period
gs.GetPeerScore().inspectPeriod = period

return nil
}
Expand Down

0 comments on commit ab8365c

Please sign in to comment.