gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] branch master updated: work on #6830: cost-based polic


From: gnunet
Subject: [taler-anastasis] branch master updated: work on #6830: cost-based policy generation
Date: Sun, 27 Jun 2021 00:06:38 +0200

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

grothoff pushed a commit to branch master
in repository anastasis.

The following commit(s) were added to refs/heads/master by this push:
     new 502b0f2  work on #6830: cost-based policy generation
502b0f2 is described below

commit 502b0f21b2f0ec11f80717af00c5366f9b8ba3a7
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Sun Jun 27 00:06:35 2021 +0200

    work on #6830: cost-based policy generation
---
 debian/changelog                         |   6 ++
 src/reducer/anastasis_api_backup_redux.c | 131 ++++++++++++++++++++++++++++++-
 2 files changed, 136 insertions(+), 1 deletion(-)

diff --git a/debian/changelog b/debian/changelog
index e02b669..fa7428b 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+anastasis (0.0.0-1) unstable; urgency=low
+
+  * Various fixes in packaging and code logic.
+
+ -- Christian Grothoff <grothoff@gnu.org>  Thu, 24 Jun 2021 00:00:00 +0000
+
 anastasis (0.0.0-0) unstable; urgency=medium
 
   * Initial Release.
diff --git a/src/reducer/anastasis_api_backup_redux.c 
b/src/reducer/anastasis_api_backup_redux.c
index aa5b595..f1f4a0d 100644
--- a/src/reducer/anastasis_api_backup_redux.c
+++ b/src/reducer/anastasis_api_backup_redux.c
@@ -373,6 +373,24 @@ del_authentication (json_t *state,
 
 /* ********************** done_authentication ******************** */
 
+/**
+ * Linked list of costs to associate with a policy.
+ */
+struct Costs
+{
+
+  /**
+   * Kept in a LL.
+   */
+  struct Costs *next;
+
+  /**
+   * Cost in one of the currencies.
+   */
+  struct TALER_Amount cost;
+};
+
+
 /**
  * Information for running done_authentication() logic.
  */
@@ -411,6 +429,12 @@ struct PolicyBuilder
    */
   const char *hint;
 
+  /**
+   * LL of costs associated with the currently preferred
+   * policy.
+   */
+  struct Costs *best_cost;
+
   /**
    * Overall number of challenges provided by the user.
    */
@@ -436,6 +460,21 @@ struct PolicyBuilder
 };
 
 
+/**
+ * Free @a costs LL.
+ *
+ * @param[in] costs linked list to free
+ */
+static void
+free_costs (struct Costs *costs)
+{
+  if (NULL == costs)
+    return;
+  free_costs (costs->next);
+  GNUNET_free (costs);
+}
+
+
 /**
  * Evaluate the cost/benefit of the provider selection in @a prov_sel
  * and if it is better then the best known one in @a pb, update @a pb.
@@ -448,6 +487,7 @@ eval_provider_selection (struct PolicyBuilder *pb,
                          const char *prov_sel[])
 {
   unsigned int curr_diversity;
+  struct Costs *my_cost = NULL;
 
   /* calculate cost */
   for (unsigned int i = 0; i < pb->req_methods; i++)
@@ -530,7 +570,28 @@ eval_provider_selection (struct PolicyBuilder *pb,
            (challenge_size_ok (size_limit_in_mb,
                                challenge_size) ) )
       {
+        struct Costs *pos = my_cost;
+
         found = true;
+        while ( (NULL != pos) &&
+                (GNUNET_OK !=
+                 TALER_amount_cmp_currency (&pos->cost,
+                                            &method_cost)) )
+          pos = pos->next;
+        if (NULL == pos)
+        {
+          pos = GNUNET_new (struct Costs);
+          pos->cost = method_cost;
+          pos->next = my_cost;
+          my_cost = pos;
+        }
+        else
+        {
+          GNUNET_assert (0 <=
+                         TALER_amount_add (&pos->cost,
+                                           &pos->cost,
+                                           &method_cost));
+        }
       }
     }
     if (! found)
@@ -539,6 +600,7 @@ eval_provider_selection (struct PolicyBuilder *pb,
          Cost is basically 'infinite', but we simply then skip this. */
       GNUNET_JSON_parse_free (pspec);
       GNUNET_JSON_parse_free (mspec);
+      free_costs (my_cost);
       return;
     }
     GNUNET_JSON_parse_free (mspec);
@@ -563,11 +625,78 @@ eval_provider_selection (struct PolicyBuilder *pb,
       curr_diversity++;
   }
   if (curr_diversity < pb->best_diversity)
+  {
+    free_costs (my_cost);
     return;
+  }
+  if (0 != pb->best_diversity)
+  {
+    int ranking = 0; /* lower is better for new cost */
+
+    for (struct Costs *cmp = pb->best_cost;
+         NULL != cmp;
+         cmp = cmp->next)
+    {
+      bool found = false;
+      for (struct Costs *pos = my_cost;
+           NULL != pos;
+           pos = pos->next)
+      {
+        if (GNUNET_OK !=
+            TALER_amount_cmp_currency (&cmp->cost,
+                                       &pos->cost))
+          continue;
+        found = true;
+      }
+      if (! found)
+        ranking--; /* new policy has no cost in this currency */
+    }
+
+    for (struct Costs *pos = my_cost;
+         NULL != pos;
+         pos = pos->next)
+    {
+      bool found = false;
+      for (struct Costs *cmp = pb->best_cost;
+           NULL != cmp;
+           cmp = cmp->next)
+      {
+        if (GNUNET_OK !=
+            TALER_amount_cmp_currency (&cmp->cost,
+                                       &pos->cost))
+          continue;
+        found = true;
+        switch (TALER_amount_cmp (&cmp->cost,
+                                  &pos->cost))
+        {
+        case -1: /* cmp < pos */
+          ranking--;
+          break;
+        case 0:
+          break;
+        case 1: /* cmp > pos */
+          ranking++;
+          break;
+        }
+        break;
+      }
+      if (! found)
+        ranking++; /* old policy has no cost in this currency */
+    }
+    if (ranking >= 0)
+    {
+      /* new method not clearly better, do not use it */
+      free_costs (my_cost);
+      return;
+    }
+  }
+
   memcpy (pb->best_sel,
           prov_sel,
           sizeof (const char *) * pb->req_methods);
   pb->best_diversity = curr_diversity;
+  free_costs (pb->best_cost);
+  pb->best_cost = my_cost;
 }
 
 
@@ -818,7 +947,7 @@ done_authentication (json_t *state,
     method_candidate (&pb,
                       0);
   }
-
+  free_costs (pb.best_cost);
   if (TALER_EC_NONE != pb.ec)
   {
     json_decref (pb.policies);

-- 
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]