From a7e1a5e814a626318e2320054079b6b58309fbdf Mon Sep 17 00:00:00 2001 From: violog <51th.apprent1ce.f0rce@gmail.com> Date: Mon, 1 Jul 2024 16:56:42 +0300 Subject: [PATCH 1/3] Delete also not started events, forbid fulfill/claim for inactive events in nooneisforgotten --- .../service/workers/expirywatch/watcher.go | 8 +++--- .../service/workers/nooneisforgotten/main.go | 27 +++---------------- requests_test.go | 2 +- 3 files changed, 8 insertions(+), 29 deletions(-) diff --git a/internal/service/workers/expirywatch/watcher.go b/internal/service/workers/expirywatch/watcher.go index 23d99ea..11e5873 100644 --- a/internal/service/workers/expirywatch/watcher.go +++ b/internal/service/workers/expirywatch/watcher.go @@ -27,12 +27,10 @@ func newWatcher(cfg config.Config) *watcher { } func (w *watcher) initialRun() error { - expired := w.types.Names(func(ev evtypes.EventConfig) bool { - return !ev.Disabled && !evtypes.FilterExpired(ev) - }) + expired := w.types.Names(func(ev evtypes.EventConfig) bool { return !evtypes.FilterInactive(ev) }) if len(expired) == 0 { - w.log.Debug("No events were disabled or have expired") + w.log.Debug("No inactive events were found") return nil } @@ -45,7 +43,7 @@ func (w *watcher) cleanOpen(types ...string) error { return fmt.Errorf("clean open events [types=%v]: %w", types, err) } - w.log.Infof("Deleted %d expired and disabled open events, types: %v", deleted, types) + w.log.Infof("Deleted %d deactivated open events, types: %v", deleted, types) return nil } diff --git a/internal/service/workers/nooneisforgotten/main.go b/internal/service/workers/nooneisforgotten/main.go index 67e744d..ad9c704 100644 --- a/internal/service/workers/nooneisforgotten/main.go +++ b/internal/service/workers/nooneisforgotten/main.go @@ -43,18 +43,12 @@ func Run(cfg config.Config, sig chan struct{}) { // First, there is an attempt to claim as many events as // possible and to fulfill the rest of the events. // -// Events may not be fulfilled if the event is not active. -// Event may not be claimed if AutoClaim is disabled. -// If the event is not active, only fulfilled events will be claimed. +// Event will not be claimed if AutoClaim is disabled. func updatePassportScanEvents(db *pgdb.DB, types evtypes.Types, levels config.Levels) error { - // if event inactive - fulfilled event can be claimed - evType := types.Get(evtypes.TypePassportScan) + evType := types.Get(evtypes.TypePassportScan, evtypes.FilterInactive) if evType == nil { return nil } - if evType.Disabled { - return nil - } if evtypes.FilterInactive(*evType) { return nil @@ -154,10 +148,6 @@ func updatePassportScanEvents(db *pgdb.DB, types evtypes.Types, levels config.Le } } - if evtypes.FilterInactive(*evType) { - return nil - } - toFulfill = make([]string, 0, len(balances)) for _, balances := range countriesBalancesMap { for _, balance := range balances { @@ -183,8 +173,6 @@ func updatePassportScanEvents(db *pgdb.DB, types evtypes.Types, levels config.Le // updateReferralUserEvents is used to add events for referrers // for friends who have scanned the passport, if they have not been added. -// -// Events are not added if the event is inactive or disabled func updateReferralUserEvents(db *pgdb.DB, types evtypes.Types) error { evTypeRef := types.Get(evtypes.TypeReferralSpecific, evtypes.FilterInactive) if evTypeRef == nil { @@ -220,14 +208,8 @@ func updateReferralUserEvents(db *pgdb.DB, types evtypes.Types) error { // claimReferralSpecificEvents claim fulfilled events for invited // friends which have passport scanned, if it possible func claimReferralSpecificEvents(db *pgdb.DB, types evtypes.Types, levels config.Levels) error { - evType := types.Get(evtypes.TypeReferralSpecific) - if evType == nil { - return nil - } - if evType.Disabled { - return nil - } - if !evType.AutoClaim { + evType := types.Get(evtypes.TypeReferralSpecific, evtypes.FilterInactive) + if evType == nil || !evType.AutoClaim { return nil } @@ -267,7 +249,6 @@ func claimReferralSpecificEvents(db *pgdb.DB, types evtypes.Types, levels config if !balance.ReferredBy.Valid || balance.Country == nil { continue } - // country can't be nil because of db query logic if _, ok := countriesBalancesMap[*balance.Country]; !ok { countriesBalancesMap[*balance.Country] = make([]data.Balance, 0, len(balances)) } diff --git a/requests_test.go b/requests_test.go index 05fa3d5..57a6e32 100644 --- a/requests_test.go +++ b/requests_test.go @@ -903,7 +903,7 @@ func verifyPassportBody(nullifier string, proof zkptypes.ZKProof) resources.Veri Type: resources.VERIFY_PASSPORT, }, Attributes: resources.VerifyPassportAttributes{ - Proof: proof, + Proof: &proof, }, }, } From 346c8f943228218edc25433545f6201f24accb88 Mon Sep 17 00:00:00 2001 From: violog <51th.apprent1ce.f0rce@gmail.com> Date: Mon, 1 Jul 2024 17:01:37 +0300 Subject: [PATCH 2/3] Update anonymous ID on difference --- internal/service/handlers/verify_passport.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/service/handlers/verify_passport.go b/internal/service/handlers/verify_passport.go index e1cb777..e8ad31f 100644 --- a/internal/service/handlers/verify_passport.go +++ b/internal/service/handlers/verify_passport.go @@ -93,9 +93,18 @@ func VerifyPassport(w http.ResponseWriter, r *http.Request) { return } - var balAID string - if balance.AnonymousID != nil { - balAID = *balance.AnonymousID + if balance.AnonymousID != nil && *balance.AnonymousID != anonymousID { + log.Infof("Balance %s is updating anonymous ID", balance.Nullifier) + err = BalancesQ(r).FilterByNullifier(balance.Nullifier).Update(map[string]any{ + data.ColAnonymousID: anonymousID, + }) + + if err != nil { + log.WithError(err).Error("Failed to update balance") + ape.RenderErr(w, problems.InternalError()) + return + } + balance.AnonymousID = &anonymousID } proofCountry, err := requests.ExtractCountry(*proof) @@ -107,7 +116,6 @@ func VerifyPassport(w http.ResponseWriter, r *http.Request) { err = validation.Errors{ "data/attributes/country": validation.Validate(*balance.Country, validation.Required, validation.In(country)), - "data/attributes/anonymous_id": validation.Validate(anonymousID, validation.Required, validation.In(balAID)), "data/attributes/proof/pub_signals/country": validation.Validate(proofCountry, validation.Required, validation.In(*balance.Country)), }.Filter() if err != nil { From 8abe44c4594a7e6e6d1b67211eaaa06904909d2f Mon Sep 17 00:00:00 2001 From: violog <51th.apprent1ce.f0rce@gmail.com> Date: Mon, 1 Jul 2024 17:09:29 +0300 Subject: [PATCH 3/3] Update anonymous ID in one query --- internal/service/handlers/verify_passport.go | 25 +++++++------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/internal/service/handlers/verify_passport.go b/internal/service/handlers/verify_passport.go index e8ad31f..0513396 100644 --- a/internal/service/handlers/verify_passport.go +++ b/internal/service/handlers/verify_passport.go @@ -93,20 +93,6 @@ func VerifyPassport(w http.ResponseWriter, r *http.Request) { return } - if balance.AnonymousID != nil && *balance.AnonymousID != anonymousID { - log.Infof("Balance %s is updating anonymous ID", balance.Nullifier) - err = BalancesQ(r).FilterByNullifier(balance.Nullifier).Update(map[string]any{ - data.ColAnonymousID: anonymousID, - }) - - if err != nil { - log.WithError(err).Error("Failed to update balance") - ape.RenderErr(w, problems.InternalError()) - return - } - balance.AnonymousID = &anonymousID - } - proofCountry, err := requests.ExtractCountry(*proof) if err != nil { log.WithError(err).Error("failed to extract country while proof was successfully verified") @@ -123,9 +109,16 @@ func VerifyPassport(w http.ResponseWriter, r *http.Request) { return } - err = BalancesQ(r).FilterByNullifier(balance.Nullifier).Update(map[string]any{ + toUpd := map[string]any{ data.ColIsPassport: true, - }) + } + if balance.AnonymousID != nil && *balance.AnonymousID != anonymousID { + log.Infof("Balance %s is updating anonymous ID", balance.Nullifier) + toUpd[data.ColAnonymousID] = anonymousID + balance.AnonymousID = &anonymousID + } + + err = BalancesQ(r).FilterByNullifier(balance.Nullifier).Update(toUpd) if err != nil { log.WithError(err).Error("Failed to update balance") ape.RenderErr(w, problems.InternalError())