Skip to content

Commit

Permalink
Add minimal compatibility filter for accepting proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
soffokl committed Mar 22, 2024
1 parent 76e7789 commit be6f055
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func main() {
}

tagEnhancer := tags.NewEnhancer(tags.NewApi(cfg.BadgerAddress.String()))
proposalRepo := proposal.NewRepository([]proposal.Enhancer{tagEnhancer}, cfg.ProposalsHardLimitPerCountry, cfg.ProposalsSoftLimitPerCountry)
proposalRepo := proposal.NewRepository([]proposal.Enhancer{tagEnhancer}, cfg.ProposalsHardLimitPerCountry, cfg.ProposalsSoftLimitPerCountry, cfg.CompatibilityMin)
qualityOracleAPI := oracleapi.New(cfg.QualityOracleURL.String())
qualityService := quality.NewService(qualityOracleAPI, cfg.QualityCacheTTL)
proposalService := proposal.NewService(proposalRepo, qualityService)
Expand Down
8 changes: 8 additions & 0 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Options struct {
ProposalsHardLimitPerCountry int
ProposalsSoftLimitPerCountry int

CompatibilityMin int

MaxRequestsLimit int

ProposalsCacheTTL time.Duration
Expand Down Expand Up @@ -77,6 +79,11 @@ func ReadDiscovery() (*Options, error) {
return nil, err
}

compatibility, err := OptionalEnvInt("COMPATIBILITY_MIN", "0")
if err != nil {
return nil, err
}

locationUser := OptionalEnv("LOCATION_USER", "")
locationPass := OptionalEnv("LOCATION_PASS", "")
locationAddress, err := RequiredEnvURL("LOCATION_ADDRESS")
Expand Down Expand Up @@ -117,6 +124,7 @@ func ReadDiscovery() (*Options, error) {
ProposalsCacheTTL: *proposalsCacheTTL,
ProposalsCacheLimit: proposalsCacheLimit,
CountriesCacheLimit: countriesCacheLimit,
CompatibilityMin: compatibility,
LogLevel: logLevel,
ProposalsHardLimitPerCountry: proposalsHardLimitPerCountry,
ProposalsSoftLimitPerCountry: proposalsSoftLimitPerCountry,
Expand Down
11 changes: 10 additions & 1 deletion proposal/inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package proposal

import (
"errors"
"strings"
"sync"
"time"
Expand All @@ -13,6 +14,8 @@ import (
"github.com/mysteriumnetwork/discovery/quality/oracleapi"
)

var ErrProposalIncompatible = errors.New("compatibility too low")

type Enhancer interface {
Enhance(proposal *v3.Proposal)
}
Expand All @@ -25,6 +28,7 @@ type Repository struct {
enhancers []Enhancer
proposalsHardLimitPerCountry int
proposalsSoftLimitPerCountry int
compatibilityMin int
}

type repoListOpts struct {
Expand All @@ -48,14 +52,15 @@ type record struct {
expiresAt time.Time
}

func NewRepository(enhancers []Enhancer, proposalsHardLimitPerCountry, proposalsSoftLimitPerCountry int) *Repository {
func NewRepository(enhancers []Enhancer, proposalsHardLimitPerCountry, proposalsSoftLimitPerCountry, compatibilityMin int) *Repository {
return &Repository{
expirationDuration: 3*time.Minute + 10*time.Second,
expirationJobDelay: 20 * time.Second,
proposals: make(map[string]record),
enhancers: enhancers,
proposalsHardLimitPerCountry: proposalsHardLimitPerCountry,
proposalsSoftLimitPerCountry: proposalsSoftLimitPerCountry,
compatibilityMin: compatibilityMin,
}
}

Expand Down Expand Up @@ -154,6 +159,10 @@ func (r *Repository) Store(proposal v3.Proposal) error {
r.mu.Lock()
defer r.mu.Unlock()

if proposal.Compatibility < r.compatibilityMin {
return ErrProposalIncompatible
}

r.proposals[proposal.Key()] = record{
proposal: proposal,
expiresAt: time.Now().Add(r.expirationDuration),
Expand Down

0 comments on commit be6f055

Please sign in to comment.