From be6f0553b7143051988303570163f50ddece1fec Mon Sep 17 00:00:00 2001 From: soffokl Date: Fri, 22 Mar 2024 09:37:15 +0600 Subject: [PATCH] Add minimal compatibility filter for accepting proposals --- cmd/main.go | 2 +- config/options.go | 8 ++++++++ proposal/inmemory.go | 11 ++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index ba91f87..f369cd3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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) diff --git a/config/options.go b/config/options.go index 7f39c88..6c021e6 100644 --- a/config/options.go +++ b/config/options.go @@ -40,6 +40,8 @@ type Options struct { ProposalsHardLimitPerCountry int ProposalsSoftLimitPerCountry int + CompatibilityMin int + MaxRequestsLimit int ProposalsCacheTTL time.Duration @@ -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") @@ -117,6 +124,7 @@ func ReadDiscovery() (*Options, error) { ProposalsCacheTTL: *proposalsCacheTTL, ProposalsCacheLimit: proposalsCacheLimit, CountriesCacheLimit: countriesCacheLimit, + CompatibilityMin: compatibility, LogLevel: logLevel, ProposalsHardLimitPerCountry: proposalsHardLimitPerCountry, ProposalsSoftLimitPerCountry: proposalsSoftLimitPerCountry, diff --git a/proposal/inmemory.go b/proposal/inmemory.go index 52ecdcb..53fcae5 100644 --- a/proposal/inmemory.go +++ b/proposal/inmemory.go @@ -5,6 +5,7 @@ package proposal import ( + "errors" "strings" "sync" "time" @@ -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) } @@ -25,6 +28,7 @@ type Repository struct { enhancers []Enhancer proposalsHardLimitPerCountry int proposalsSoftLimitPerCountry int + compatibilityMin int } type repoListOpts struct { @@ -48,7 +52,7 @@ 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, @@ -56,6 +60,7 @@ func NewRepository(enhancers []Enhancer, proposalsHardLimitPerCountry, proposals enhancers: enhancers, proposalsHardLimitPerCountry: proposalsHardLimitPerCountry, proposalsSoftLimitPerCountry: proposalsSoftLimitPerCountry, + compatibilityMin: compatibilityMin, } } @@ -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),