add threshold validation

This commit is contained in:
nisdas 2021-03-31 21:10:55 +08:00 committed by vyzo
parent 5cd1316efc
commit 3ea6751619
2 changed files with 25 additions and 10 deletions

View File

@ -32,20 +32,20 @@ type PeerScoreThresholds struct {
}
func (p *PeerScoreThresholds) validate() error {
if p.GossipThreshold > 0 {
return fmt.Errorf("invalid gossip threshold; it must be <= 0")
if p.GossipThreshold > 0 || isInvalidNumber(p.GossipThreshold) {
return fmt.Errorf("invalid gossip threshold; it must be <= 0 and a valid number")
}
if p.PublishThreshold > 0 || p.PublishThreshold > p.GossipThreshold {
return fmt.Errorf("invalid publish threshold; it must be <= 0 and <= gossip threshold")
if p.PublishThreshold > 0 || p.PublishThreshold > p.GossipThreshold || isInvalidNumber(p.PublishThreshold) {
return fmt.Errorf("invalid publish threshold; it must be <= 0 and <= gossip threshold and a valid number")
}
if p.GraylistThreshold > 0 || p.GraylistThreshold > p.PublishThreshold {
return fmt.Errorf("invalid graylist threshold; it must be <= 0 and <= publish threshold")
if p.GraylistThreshold > 0 || p.GraylistThreshold > p.PublishThreshold || isInvalidNumber(p.GraylistThreshold) {
return fmt.Errorf("invalid graylist threshold; it must be <= 0 and <= publish threshold and a valid number")
}
if p.AcceptPXThreshold < 0 {
return fmt.Errorf("invalid accept PX threshold; it must be >= 0")
if p.AcceptPXThreshold < 0 || isInvalidNumber(p.AcceptPXThreshold) {
return fmt.Errorf("invalid accept PX threshold; it must be >= 0 and a valid number")
}
if p.OpportunisticGraftThreshold < 0 {
return fmt.Errorf("invalid opportunistic grafting threshold; it must be >= 0")
if p.OpportunisticGraftThreshold < 0 || isInvalidNumber(p.OpportunisticGraftThreshold) {
return fmt.Errorf("invalid opportunistic grafting threshold; it must be >= 0 and a valid number")
}
return nil
}

View File

@ -30,6 +30,21 @@ func TestPeerScoreThresholdsValidation(t *testing.T) {
if (&PeerScoreThresholds{GossipThreshold: -1, PublishThreshold: -2, GraylistThreshold: -3, AcceptPXThreshold: 1, OpportunisticGraftThreshold: 2}).validate() != nil {
t.Fatal("expected validation success")
}
if (&PeerScoreThresholds{GossipThreshold: math.Inf(-1), PublishThreshold: -2, GraylistThreshold: -3, AcceptPXThreshold: 1, OpportunisticGraftThreshold: 2}).validate() == nil {
t.Fatal("expected validation error")
}
if (&PeerScoreThresholds{GossipThreshold: -1, PublishThreshold: math.Inf(-1), GraylistThreshold: -3, AcceptPXThreshold: 1, OpportunisticGraftThreshold: 2}).validate() == nil {
t.Fatal("expected validation error")
}
if (&PeerScoreThresholds{GossipThreshold: -1, PublishThreshold: -2, GraylistThreshold: math.Inf(-1), AcceptPXThreshold: 1, OpportunisticGraftThreshold: 2}).validate() == nil {
t.Fatal("expected validation error")
}
if (&PeerScoreThresholds{GossipThreshold: -1, PublishThreshold: -2, GraylistThreshold: -3, AcceptPXThreshold: math.NaN(), OpportunisticGraftThreshold: 2}).validate() == nil {
t.Fatal("expected validation error")
}
if (&PeerScoreThresholds{GossipThreshold: -1, PublishThreshold: -2, GraylistThreshold: -3, AcceptPXThreshold: 1, OpportunisticGraftThreshold: math.Inf(0)}).validate() == nil {
t.Fatal("expected validation error")
}
}
func TestTopicScoreParamsValidation(t *testing.T) {