diff --git a/gossipsub.go b/gossipsub.go index 7d6714b..3875392 100644 --- a/gossipsub.go +++ b/gossipsub.go @@ -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 { diff --git a/pubsub.go b/pubsub.go index 151ece8..285b1b7 100644 --- a/pubsub.go +++ b/pubsub.go @@ -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 } } diff --git a/score.go b/score.go new file mode 100644 index 0000000..36839ee --- /dev/null +++ b/score.go @@ -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) { + +}