gnunet-svn
[Top][All Lists]
Advanced

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

[gnunet-scheme] 41/49: dht/client: Impose some bounds on the replication


From: gnunet
Subject: [gnunet-scheme] 41/49: dht/client: Impose some bounds on the replication level.
Date: Sat, 25 Dec 2021 23:00:18 +0100

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

maxime-devos pushed a commit to branch master
in repository gnunet-scheme.

commit 316558b05a49536abf1a980df8a6f332ed412d42
Author: Maxime Devos <maximedevos@telenet.be>
AuthorDate: Wed Oct 6 22:54:50 2021 +0200

    dht/client: Impose some bounds on the replication level.
    
    Fixes the Scheme-GNUnet part of 007029 (Mantis).
    
    * gnu/gnunet/dht/client.scm
      
(%effective-minimum-replication-level,%effective-maximum-replication-level)
      (%minimum-replication-level,%maximum-replication-level): New constants.
      (bound-replication-level): New procedure.
      (send-get!): Use new procedure.
    * tests/distibuted-hash-table.scm: Test the new procedure.
    * Makefile.am (SCM_TESTS): Add the test.
---
 Makefile.am                      |  1 +
 gnu/gnunet/dht/client.scm        | 45 ++++++++++++++++++++++--
 tests/distributed-hash-table.scm | 76 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 119 insertions(+), 3 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index adf964e..a400c10 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -178,6 +178,7 @@ SCM_TESTS = \
   tests/config-db.scm \
   tests/config-fs.scm \
   tests/crypto.scm \
+  tests/distributed-hash-table.scm \
   tests/form.scm \
   tests/netstruct.scm \
   tests/time.scm \
diff --git a/gnu/gnunet/dht/client.scm b/gnu/gnunet/dht/client.scm
index f70dd2b..dc00f09 100644
--- a/gnu/gnunet/dht/client.scm
+++ b/gnu/gnunet/dht/client.scm
@@ -21,7 +21,12 @@
 ;; ^^ TODO: not visible yet, but once more parts are ported ...
 ;; Author: Maxime Devos (Scheme port)
 (define-library (gnu gnunet dht client)
-  (export connect
+  (export %effective-minimum-replication-level
+         %effective-maximum-replication-level
+         %minimum-replication-level
+         %maximum-replication-level
+         bound-replication-level
+         connect
          disconnect!
          put!
          cancel-put!
@@ -69,12 +74,46 @@
                and >= = quote * + - define begin ... let*
                quote case else values apply let cond if >
                <= expt assert integer? lambda for-each
-               not)
+               not expt min max)
          (only (rnrs control)
                unless when)
          (only (rnrs records syntactic)
                define-record-type))
   (begin
+    ;; The minimal and maximal replication levels the DHT service allows.
+    ;; While the service won't reject replication levels outside this range,
+    ;; it will clip them to within this range, so choosing replication levels
+    ;; outside this range is useless.
+    ;;
+    ;; Also, GNUnet v0.15.3 and earlier has a bug where the DHT service can 
crash
+    ;; if the replication level 0 is passed, see 
https://bugs.gnunet.org/view.php?id=7029.
+    ;;
+    ;; These values are based on the MINIMUM_REPLICATION_LEVEL and
+    ;; MAXIMUM_REPLICATION_LEVEL values in 
src/dht/gnunet-service-dht_neighbours.c
+    ;; of the C implementation.
+    (define %effective-minimum-replication-level 1)
+    (define %effective-maximum-replication-level 16)
+    (define %minimum-replication-level 0)
+    (define %maximum-replication-level (- (expt 2 32) 1))
+
+    ;; Called by 'send-get!'.
+    (define (bound-replication-level replication-level)
+      "Bound the replication level @var{replication-level}, which must be a
+valid replication to the level, to the range the DHT service likes."
+      (unless (<= %minimum-replication-level replication-level
+                 %maximum-replication-level)
+       (error "replication level is out of bounds"))
+      ;; OOPS swap them
+      (max %effective-minimum-replication-level
+          (min %effective-maximum-replication-level replication-level)))
+
+    ;; New get or put operations are initially in new-get-operations or
+    ;; new-put-operation, and not in id->operation-map.  They are moved
+    ;; in the background by 'process-new-get-operations' and
+    ;; 'process-new-put-operations'.
+    ;;
+    ;; Operations must be put in id->operation-map before sending them
+    ;; to the service!
     (define-record-type (<server> %make-server server?)
       (fields (immutable request-close?/box server-request-close?/box)
              (immutable request-close-condition
@@ -127,7 +166,7 @@
             (value->index (symbol-value message-type msg:dht:client:get)))
       (set%! /:msg:dht:client:get '(options) s (get:options get))
       (set%! /:msg:dht:client:get '(desired-replication-level) s
-            (get:desired-replication-level get))
+            (bound-replication-level (get:desired-replication-level get)))
       (set%! /:msg:dht:client:get '(type) s (get:type get))
       (slice-copy! (get:key get) (select /:msg:dht:client:get '(key) s))
       (set%! /:msg:dht:client:get '(unique-id) s (get:unique-id get))
diff --git a/tests/distributed-hash-table.scm b/tests/distributed-hash-table.scm
new file mode 100644
index 0000000..1cb134b
--- /dev/null
+++ b/tests/distributed-hash-table.scm
@@ -0,0 +1,76 @@
+;; This file is part of scheme-GNUnet, a partial Scheme port of GNUnet.
+;; Copyright (C) 2021 GNUnet e.V.
+;;
+;; scheme-GNUnet is free software: you can redistribute it and/or modify it
+;; under the terms of the GNU Affero General Public License as published
+;; by the Free Software Foundation, either version 3 of the License,
+;; or (at your option) any later version.
+;;
+;; scheme-GNUnet is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; Affero General Public License for more details.
+;;
+;; You should have received a copy of the GNU Affero General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+;;
+;; SPDX-License-Identifier: AGPL-3.0-or-later
+(define-module (test-distributed-hash-table))
+(import (gnu gnunet dht client)
+       (rnrs base)
+       (srfi srfi-26)
+       (srfi srfi-64))
+
+;; It's easy to accidentally swap the min and the max,
+;; or use theoretical bounds instead of effective bounds.
+(test-begin "bound-replication-level")
+
+(define-syntax test-bound-equals
+  (syntax-rules (->)
+    ((_ (name argument -> expected) ...)
+     (begin
+       (test-equal name (list expected)
+                  (call-with-values
+                      (lambda ()
+                        (bound-replication-level argument))
+                    list))
+       ...))))
+
+(test-bound-equals
+ ;; Boundaries of set of fixed points
+ ("effective minimum" %effective-minimum-replication-level
+  -> %effective-minimum-replication-level)
+ ("effective maximum" %effective-maximum-replication-level
+  -> %effective-maximum-replication-level)
+ ;; off by one
+ ("zero" ; remove this test if %effective-minimum-replication-level becomes 
zero
+  (begin (assert (> %effective-minimum-replication-level 
%minimum-replication-level))
+        %effective-minimum-replication-level)
+  -> %effective-minimum-replication-level)
+ ("effective maximum + 1"
+  (begin (assert (< %effective-maximum-replication-level 
%maximum-replication-level))
+        (+ 1 %effective-maximum-replication-level))
+  -> %effective-maximum-replication-level)
+ ;; Extreme values
+ ("theoretical minimum" %minimum-replication-level
+  -> %effective-minimum-replication-level)
+ ("theoretical maximum" %maximum-replication-level
+  -> %effective-maximum-replication-level))
+
+(define between
+  (map (cut + %effective-minimum-replication-level <>)
+       (iota (- %effective-maximum-replication-level
+               %effective-minimum-replication-level))))
+
+;; Inner fixed points
+(test-equal "between effective extrema"
+  between
+  (map bound-replication-level between))
+
+(test-error "too large" (bound-replication-level (+ 1 
%maximum-replication-level)))
+(test-error "way too large" (bound-replication-level (* #e1e20 
%maximum-replication-level)))
+(test-error "too small" (bound-replication-level (- %minimum-replication-level 
1)))
+(test-error "way too small" (bound-replication-level (- 
%minimum-replication-level #e1e20)))
+(test-error "non-numeric" (bound-replication-level 'what))
+
+(test-end)

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