peer score scaffolding

This commit is contained in:
vyzo 2020-03-01 14:29:24 +02:00
parent 455a836d7d
commit 7d928697a2
3 changed files with 88 additions and 1 deletions

View File

@ -71,6 +71,27 @@ func NewGossipSub(ctx context.Context, h host.Host, opts ...Option) (*PubSub, er
return NewPubSub(ctx, h, rt, opts...)
}
// WithPeerScore is a gossipsub router option that enables peer scoring.
func WithPeerScore(params *PeerScoreParams) Option {
return func(ps *PubSub) error {
gs, ok := ps.rt.(*GossipSubRouter)
if !ok {
return fmt.Errorf("pubsub router is not gossipsub")
}
gs.score = newPeerScore(gs, params)
// hook the tracer
if ps.tracer != nil {
ps.tracer.score = gs.score
} else {
ps.tracer = &pubsubTracer{score: gs.score, pid: ps.host.ID(), msgID: ps.msgID}
}
return nil
}
}
// GossipSubRouter is a router that implements the gossipsub protocol.
// For each topic we have joined, we maintain an overlay through which
// messages flow; this is the mesh map.
@ -90,6 +111,7 @@ type GossipSubRouter struct {
connect chan connectInfo // px connection requests
mcache *MessageCache
tracer *pubsubTracer
score *peerScore
}
type connectInfo struct {

View File

@ -356,7 +356,11 @@ func WithDiscovery(d discovery.Discovery, opts ...DiscoverOpt) Option {
// WithEventTracer provides a tracer for the pubsub system
func WithEventTracer(tracer EventTracer) Option {
return func(p *PubSub) error {
p.tracer = &pubsubTracer{tracer: tracer, pid: p.host.ID(), msgID: p.msgID}
if tracer != nil {
p.tracer.tracer = tracer
} else {
p.tracer = &pubsubTracer{tracer: tracer, pid: p.host.ID(), msgID: p.msgID}
}
return nil
}
}

61
score.go Normal file
View File

@ -0,0 +1,61 @@
package pubsub
import (
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
)
type peerScore struct {
}
type PeerScoreParams struct {
}
type TopicScoreParams struct {
}
func newPeerScore(gs *GossipSubRouter, params *PeerScoreParams) *peerScore {
return nil
}
// router interface
func (ps *peerScore) Score(p peer.ID, topic string) float64 {
return 0
}
// tracer interface
func (ps *peerScore) AddPeer(p peer.ID, proto protocol.ID) {
}
func (ps *peerScore) RemovePeer(p peer.ID) {
}
func (ps *peerScore) Join(topic string) {
}
func (ps *peerScore) Leave(topic string) {
}
func (ps *peerScore) Graft(p peer.ID, topic string) {
}
func (ps *peerScore) Prune(p peer.ID, topic string) {
}
func (ps *peerScore) DeliverMessage(msg *Message) {
}
func (ps *peerScore) RejectMessage(msg *Message, reason string) {
}
func (ps *peerScore) DuplicateMessage(msg *Message) {
}