From 67ed06eae597c2a91e045e73dda85e0c3ef62b20 Mon Sep 17 00:00:00 2001 From: violog <51th.apprent1ce.f0rce@gmail.com> Date: Wed, 8 May 2024 21:06:52 +0300 Subject: [PATCH 1/7] Use rand v1 package for Go 1.21 --- internal/service/api/handlers/create_identity.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/api/handlers/create_identity.go b/internal/service/api/handlers/create_identity.go index df4732e..1e497e8 100644 --- a/internal/service/api/handlers/create_identity.go +++ b/internal/service/api/handlers/create_identity.go @@ -12,7 +12,7 @@ import ( "encoding/pem" "fmt" "math/big" - "math/rand/v2" + "math/rand" "net/http" "strconv" "strings" @@ -233,7 +233,7 @@ func CreateIdentity(w http.ResponseWriter, r *http.Request) { } if count > 0 { - allowed := rand.IntN(cfg.MultiAccMaxLimit-cfg.MultiAccMinLimit+1) + cfg.MultiAccMinLimit + allowed := rand.Intn(cfg.MultiAccMaxLimit-cfg.MultiAccMinLimit+1) + cfg.MultiAccMinLimit if count >= allowed { err = masterQ.Claim().FilterBy("document_hash", documentHash).Update(data.Claim{IsBanned: true}) From 8ed14353e60905a27e29ab7f6101e28578ad2ced Mon Sep 17 00:00:00 2001 From: Maksym Hrynenko <108219165+mhrynenko@users.noreply.github.com> Date: Thu, 9 May 2024 13:10:23 +0300 Subject: [PATCH 2/7] fix: migrate down syntax --- internal/assets/migrations/003_banned.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/assets/migrations/003_banned.sql b/internal/assets/migrations/003_banned.sql index de2565a..3e55251 100644 --- a/internal/assets/migrations/003_banned.sql +++ b/internal/assets/migrations/003_banned.sql @@ -1,5 +1,5 @@ -- +migrate Up ALTER TABLE claims ADD COLUMN is_banned BOOLEAN NOT NULL DEFAULT FALSE; --- -migrate Down +-- +migrate Down ALTER TABLE claims DROP COLUMN is_banned; From 004800ac72f5f49d92f395d56721e50a2952ba12 Mon Sep 17 00:00:00 2001 From: Maksym Hrynenko Date: Thu, 9 May 2024 13:38:01 +0300 Subject: [PATCH 3/7] fix: filter by document_hash, use string, not *big.Int --- internal/service/api/handlers/create_identity.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/api/handlers/create_identity.go b/internal/service/api/handlers/create_identity.go index 1e497e8..196eaef 100644 --- a/internal/service/api/handlers/create_identity.go +++ b/internal/service/api/handlers/create_identity.go @@ -211,7 +211,7 @@ func CreateIdentity(w http.ResponseWriter, r *http.Request) { return } - existing, err := masterQ.Claim().FilterBy("document_hash", documentHash).Get() + existing, err := masterQ.Claim().FilterBy("document_hash", documentHash.String()).Get() if err != nil { Log(r).WithError(err).Error("failed to get claim by document hash") ape.RenderErr(w, problems.InternalError()) From 3931b2e405b487f2beb6e15b7972736a09e4f21c Mon Sep 17 00:00:00 2001 From: violog <51th.apprent1ce.f0rce@gmail.com> Date: Thu, 9 May 2024 13:58:55 +0300 Subject: [PATCH 4/7] Fix filter by document hash again --- internal/service/api/handlers/create_identity.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/service/api/handlers/create_identity.go b/internal/service/api/handlers/create_identity.go index 196eaef..86cadde 100644 --- a/internal/service/api/handlers/create_identity.go +++ b/internal/service/api/handlers/create_identity.go @@ -218,14 +218,14 @@ func CreateIdentity(w http.ResponseWriter, r *http.Request) { return } if existing != nil { - log := Log(r).WithField("document_hash", documentHash) + log := Log(r).WithField("document_hash", documentHash.String()) if existing.IsBanned { log.Info("user of the provided document is banned") ape.RenderErr(w, problems.InternalError()) return } - count, err := masterQ.Claim().FilterBy("document_hash", documentHash).Count() + count, err := masterQ.Claim().FilterBy("document_hash", documentHash.String()).Count() if err != nil { log.WithError(err).Error("failed to count claims by document hash") ape.RenderErr(w, problems.InternalError()) @@ -235,7 +235,7 @@ func CreateIdentity(w http.ResponseWriter, r *http.Request) { if count > 0 { allowed := rand.Intn(cfg.MultiAccMaxLimit-cfg.MultiAccMinLimit+1) + cfg.MultiAccMinLimit if count >= allowed { - err = masterQ.Claim().FilterBy("document_hash", documentHash).Update(data.Claim{IsBanned: true}) + err = masterQ.Claim().FilterBy("document_hash", documentHash.String()).Update(data.Claim{IsBanned: true}) if err != nil { log.WithError(err).Error("failed to ban user") From 368860b68156b14efeafb376b2123a8f88841788 Mon Sep 17 00:00:00 2001 From: violog <51th.apprent1ce.f0rce@gmail.com> Date: Thu, 9 May 2024 14:19:19 +0300 Subject: [PATCH 5/7] Fix counter in DB --- internal/data/pg/claims.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/data/pg/claims.go b/internal/data/pg/claims.go index 4c4345a..9bdaa24 100644 --- a/internal/data/pg/claims.go +++ b/internal/data/pg/claims.go @@ -80,7 +80,7 @@ func (q *claimsQ) Count() (int, error) { var result struct { Count int `db:"count"` } - err := q.db.Select(&result, q.count) + err := q.db.Get(&result, q.count) return result.Count, err } From 5338a414b738d7c1d77336a31c3eab80802bf555 Mon Sep 17 00:00:00 2001 From: violog <51th.apprent1ce.f0rce@gmail.com> Date: Thu, 9 May 2024 14:48:55 +0300 Subject: [PATCH 6/7] Fix updating handler --- internal/data/claims.go | 2 +- internal/data/pg/claims.go | 5 ++--- internal/service/api/handlers/create_identity.go | 5 ++++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/data/claims.go b/internal/data/claims.go index 9515839..be8fe38 100644 --- a/internal/data/claims.go +++ b/internal/data/claims.go @@ -9,7 +9,7 @@ import ( type ClaimQ interface { New() ClaimQ Insert(value Claim) error - Update(value Claim) error + Update(fields map[string]any) error FilterBy(column string, value any) ClaimQ Get() (*Claim, error) Select() ([]Claim, error) diff --git a/internal/data/pg/claims.go b/internal/data/pg/claims.go index 9bdaa24..7ed3f07 100644 --- a/internal/data/pg/claims.go +++ b/internal/data/pg/claims.go @@ -46,9 +46,8 @@ func (q *claimsQ) Insert(value data.Claim) error { return err } -func (q *claimsQ) Update(value data.Claim) error { - clauses := structs.Map(value) - stmt := q.upd.SetMap(clauses) +func (q *claimsQ) Update(fields map[string]any) error { + stmt := q.upd.SetMap(fields) err := q.db.Exec(stmt) return err } diff --git a/internal/service/api/handlers/create_identity.go b/internal/service/api/handlers/create_identity.go index 86cadde..fbe5aca 100644 --- a/internal/service/api/handlers/create_identity.go +++ b/internal/service/api/handlers/create_identity.go @@ -235,7 +235,10 @@ func CreateIdentity(w http.ResponseWriter, r *http.Request) { if count > 0 { allowed := rand.Intn(cfg.MultiAccMaxLimit-cfg.MultiAccMinLimit+1) + cfg.MultiAccMinLimit if count >= allowed { - err = masterQ.Claim().FilterBy("document_hash", documentHash.String()).Update(data.Claim{IsBanned: true}) + claim.IsBanned = true + err = masterQ.Claim().FilterBy("document_hash", documentHash.String()).Update(map[string]any{ + "is_banned": true, + }) if err != nil { log.WithError(err).Error("failed to ban user") From 936598331a2aa3bfc419d97c20e6c822c995a021 Mon Sep 17 00:00:00 2001 From: violog <51th.apprent1ce.f0rce@gmail.com> Date: Thu, 9 May 2024 15:11:11 +0300 Subject: [PATCH 7/7] Drop the line that caused nil dereference --- internal/service/api/handlers/create_identity.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/service/api/handlers/create_identity.go b/internal/service/api/handlers/create_identity.go index fbe5aca..def174f 100644 --- a/internal/service/api/handlers/create_identity.go +++ b/internal/service/api/handlers/create_identity.go @@ -235,7 +235,6 @@ func CreateIdentity(w http.ResponseWriter, r *http.Request) { if count > 0 { allowed := rand.Intn(cfg.MultiAccMaxLimit-cfg.MultiAccMinLimit+1) + cfg.MultiAccMinLimit if count >= allowed { - claim.IsBanned = true err = masterQ.Claim().FilterBy("document_hash", documentHash.String()).Update(map[string]any{ "is_banned": true, })