Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Add doc comments for validatorset package
Browse files Browse the repository at this point in the history
  • Loading branch information
albrow committed Jan 13, 2020
1 parent 47a24f5 commit 41169c4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions p2p/validatorset/set.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Package validatorset offers a way to combine a set of libp2p.Validators into
// a single validator. The combined validator set only passes if *all* of its
// constituent validators pass.
package validatorset

import (
Expand All @@ -9,6 +12,7 @@ import (
log "github.com/sirupsen/logrus"
)

// Set is a set of libp2p.Validators.
type Set struct {
mu sync.RWMutex
validators []*namedValidator
Expand All @@ -19,10 +23,13 @@ type namedValidator struct {
validator pubsub.Validator
}

// New creates a new validator set
func New() *Set {
return &Set{}
}

// Add adds a new validator to the set with the given name. The name is used
// in error messages.
func (s *Set) Add(name string, validator pubsub.Validator) {
s.mu.Lock()
defer s.mu.Unlock()
Expand All @@ -33,6 +40,9 @@ func (s *Set) Add(name string, validator pubsub.Validator) {
s.validators = append(s.validators, named)
}

// Validate validates the message. It returns true if all of the constituent
// validators in the set also return true. If one or more of them return false,
// Validate returns false.
func (s *Set) Validate(ctx context.Context, sender peer.ID, msg *pubsub.Message) bool {
s.mu.RLock()
defer s.mu.RUnlock()
Expand All @@ -47,10 +57,9 @@ func (s *Set) Validate(ctx context.Context, sender peer.ID, msg *pubsub.Message)
// Otherwise continue by running this validator.
isValid := validator.validator(ctx, sender, msg)
if !isValid {
// TODO(albrow): Change the verbosity of this log to Trace.
// TODO(albrow): Should we reduce a peer's score as a penalty for invalid
// messages?
log.WithField("validatorName", validator.name).Debug("pubsub message validation failed")
log.WithField("validatorName", validator.name).Trace("pubsub message validation failed")
return false
}
}
Expand Down

0 comments on commit 41169c4

Please sign in to comment.