gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-taldir] branch master updated (02ffc8a -> bc7c627)


From: gnunet
Subject: [taler-taldir] branch master updated (02ffc8a -> bc7c627)
Date: Sun, 17 Jul 2022 17:37:25 +0200

This is an automated email from the git hooks/post-receive script.

martin-schanzenbach pushed a change to branch master
in repository taldir.

    from 02ffc8a  use taler-go amount
     new 2394164  simplify limitations for registrations
     new bc7c627  cleanup

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 pkg/rest/taldir.go | 77 +++++++-----------------------------------------------
 1 file changed, 10 insertions(+), 67 deletions(-)

diff --git a/pkg/rest/taldir.go b/pkg/rest/taldir.go
index 028fe3b..9f18f34 100644
--- a/pkg/rest/taldir.go
+++ b/pkg/rest/taldir.go
@@ -22,8 +22,6 @@ package taldir
  - ToS API (terms, privacy) with localizions
  - ToS compression
  - ToS etag
- - Base32: Use gnunet-go module? (currently copied)
- - OrderId processing (WIP)
  - Maintenance of database: When to delete expired validations?
    Currently, we expire on startup 1 day old validations
 */
@@ -80,7 +78,7 @@ type Taldir struct {
   ValidationTimeframe time.Duration
 
   // How often may a challenge be requested
-  ValidationInitiationMax int
+  ValidationInitiationMax int64
 
   // How often may a solution be attempted (in the given timeframe)
   SolutionAttemptsMax int
@@ -157,13 +155,6 @@ type RegisterMessage struct {
   Duration int64 `json:"duration"`
 }
 
-// Order is part of the RegisterMessage payload but optional and as such
-// processed separately
-type Order struct {
-  // Order ID, if the client recently paid for this registration
-  ID string `json:"order_id"`
-}
-
 // Entry is a mapping from the identity key hash to a wallet key
 // The identity key hash is sha512(sha512(address)|salt) where identity is
 // one of the identity key types supported (e.g. an email address)
@@ -220,20 +211,6 @@ type validation struct {
 }
 
 
-type validationMetadata struct {
-  // ORM
-  gorm.Model `json:"-"`
-
-  // The hash (SHA512) of the address
-  HAddress string `json:"h_address"`
-
-  // When does this validation timeframe begin (for retry calculation)
-  TimeframeStart time.Time
-
-  // How often was this validation re-initiated for this address
-  InitiationCount int
-}
-
 // ErrorDetail is the detailed error payload returned from Taldir endpoints
 type ErrorDetail struct {
 
@@ -360,7 +337,6 @@ func (t *Taldir) validationRequest(w http.ResponseWriter, r 
*http.Request){
     w.WriteHeader(http.StatusForbidden)
     return
   }
-  // FIXME: Expire validations somewhere?
   err = t.Db.Delete(&validation).Error
   if err != nil {
     log.Fatalf("Error deleting validation")
@@ -386,31 +362,13 @@ func (t *Taldir) validationRequest(w http.ResponseWriter, 
r *http.Request){
 }
 
 func (t *Taldir) isRateLimited(hAddress string) (bool, error) {
-  var validationMetadata validationMetadata
-  err := t.Db.First(&validationMetadata, "h_address = ?", hAddress).Error
+  var validations []validation
+  res := t.Db.Where("h_address = ?", hAddress).Find(&validations)
   // NOTE: Check rate limit
-  if err == nil {
-    // Limit re-initiation attempts
-    // FIXME: Do not limit tries. Very unlikely.
-    validationMetadata.InitiationCount++
-    if 
time.Now().Before(validationMetadata.TimeframeStart.Add(t.ValidationTimeframe)) 
{
-      if validationMetadata.InitiationCount > t.ValidationInitiationMax {
-        return true, nil
-      }
-    } else {
-      log.Println("Validation stale, resetting retry counter")
-      validationMetadata.TimeframeStart = time.Now()
-      validationMetadata.InitiationCount = 1
-    }
-    err = t.Db.Save(&validationMetadata).Error
-  } else  {
-    validationMetadata.HAddress = hAddress
-    validationMetadata.InitiationCount = 1
-    validationMetadata.TimeframeStart = time.Now()
-    err = t.Db.Create(&validationMetadata).Error
-  }
-  if err != nil {
-    return false, err
+  if res.Error == nil {
+    // Limit re-initiation attempts to ValidationInitiationMax times
+    // within the expiration timeframe of a validation.
+    return res.RowsAffected >= t.ValidationInitiationMax, nil
   }
   return false, nil
 }
@@ -421,7 +379,6 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r 
*http.Request){
   var errDetail ErrorDetail
   var validation validation
   var entry entry
-  var order Order
   // Check if this validation method is supported or not.
   if !t.Validators[vars["method"]] {
     errDetail.Code = gana.TALDIR_METHOD_NOT_SUPPORTED
@@ -445,7 +402,6 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r 
*http.Request){
     w.Write(resp)
     return
   }
-  json.NewDecoder(r.Body).Decode(&order)
 
   // Setup validation object. Retrieve object from DB if it already
   // exists.
@@ -481,7 +437,7 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r 
*http.Request){
     w.WriteHeader(http.StatusTooManyRequests)
     rlResponse := RateLimitedResponse{
       Code: gana.TALDIR_REGISTER_RATE_LIMITED,
-      RequestFrequency: t.ValidationTimeframe.Microseconds() / 
int64(t.ValidationInitiationMax),
+      RequestFrequency: t.ValidationTimeframe.Microseconds() / 
t.ValidationInitiationMax,
       Hint: "Registration rate limit reached",
     }
     jsonResp, _ := json.Marshal(rlResponse)
@@ -489,7 +445,6 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r 
*http.Request){
     t.Db.Delete(&validation)
     return
   }
-  // FIXME try to avoid validationMetadata
   err = t.Db.First(&validation, "h_address = ? AND public_key = ? AND inbox = 
? AND duration = ?",
                                 hAddress, req.PublicKey, req.Inbox, 
reqDuration).Error
   validationExists := (nil == err)
@@ -516,12 +471,6 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r 
*http.Request){
     return
   }
   if !cost.IsZero() {
-    if validationExists {
-      if order.ID != validation.OrderID {
-        w.WriteHeader(http.StatusConflict)
-        return
-      }
-    }
     if len(validation.OrderID) == 0 {
       // Add new order for new validations
       orderID, newOrderErr := t.Merchant.AddNewOrder(*cost)
@@ -533,8 +482,7 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r 
*http.Request){
       validation.OrderID = orderID
     }
 
-    // FIXME what if provided order ID and validation order ID differ???
-    // Check if order paid. FIXME: How to check if this the a correct order??
+    // Check if order paid.
     // FIXME: Remember that it was activated and paid
     payto, paytoErr := t.Merchant.IsOrderPaid(validation.OrderID)
     if paytoErr != nil {
@@ -641,7 +589,6 @@ func (t *Taldir) validationPage(w http.ResponseWriter, r 
*http.Request) {
 func (t *Taldir) ClearDatabase() {
   t.Db.Where("1 = 1").Delete(&entry{})
   t.Db.Where("1 = 1").Delete(&validation{})
-  t.Db.Where("1 = 1").Delete(&validationMetadata{})
 }
 
 func (t *Taldir) termsResponse(w http.ResponseWriter, r *http.Request) {
@@ -772,7 +719,7 @@ func (t *Taldir) Initialize(cfgfile string) {
     t.Validators[a] = true
   }
   t.ChallengeBytes = t.Cfg.Section("taldir").Key("challenge_bytes").MustInt(16)
-  t.ValidationInitiationMax = 
t.Cfg.Section("taldir").Key("validation_initiation_max").MustInt(3)
+  t.ValidationInitiationMax = 
t.Cfg.Section("taldir").Key("validation_initiation_max").MustInt64(3)
   t.SolutionAttemptsMax = 
t.Cfg.Section("taldir").Key("solution_attempt_max").MustInt(3)
 
   validationTTLStr := 
t.Cfg.Section("taldir").Key("validation_timeframe").MustString("5m")
@@ -807,10 +754,6 @@ func (t *Taldir) Initialize(cfgfile string) {
   if err := t.Db.AutoMigrate(&validation{}); err != nil {
     panic(err)
   }
-  if err := t.Db.AutoMigrate(&validationMetadata{}); err != nil {
-    panic(err)
-  }
-
 
   // Clean up validations
   validationExpStr := 
t.Cfg.Section("taldir").Key("validation_expiration").MustString("24h")

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]