glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / consensus / consensus-simulation.py.in
1 #!@PYTHON@
2 # This file is part of GNUnet
3 # (C) 2013, 2018 Christian Grothoff (and other contributing authors)
4 #
5 # GNUnet is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Affero General Public License as published
7 # by the Free Software Foundation, either version 3 of the License,
8 # or (at your option) any later version.
9 #
10 # GNUnet is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Affero General Public License for more details.
14
15 from __future__ import absolute_import
16 from __future__ import print_function
17 import argparse
18 import random
19 from math import ceil, log, floor
20 from past.builtins import xrange
21
22
23 def bsc(n):
24     """ count the bits set in n"""
25     l = n.bit_length()
26     c = 0
27     x = 1
28     for _ in range(0, l):
29         if n & x:
30             c = c + 1
31         x = x << 1
32     return c
33
34
35 def simulate(k, n, verbose):
36     assert k < n
37     largest_arc = int(2**ceil(log(n, 2))) / 2
38     num_ghosts = (2 * largest_arc) - n
39     if verbose:
40         print("we have", num_ghosts, "ghost peers")
41         # n.b. all peers with idx<k are evil
42     peers = range(n)
43     # py2-3 compatible, backwards.
44     # refer to http://python-future.org/compatible_idioms.html#xrange
45     info = [1 << x for x in xrange(n)]
46
47     def done_p():
48         for x in xrange(k, n):
49             if bsc(info[x]) < n-k:
50                 return False
51         return True
52     rounds = 0
53     while not done_p():
54         if verbose:
55             print("-- round --")
56         arc = 1
57         while arc <= largest_arc:
58             if verbose:
59                 print("-- subround --")
60             new_info = [x for x in info]
61             for peer_physical in xrange(n):
62                 peer_logical = peers[peer_physical]
63                 peer_type = None
64                 partner_logical = (peer_logical + arc) % n
65                 partner_physical = peers.index(partner_logical)
66                 if peer_physical < k or partner_physical < k:
67                     if verbose:
68                         print("bad peer in connection", peer_physical, "--", partner_physical)
69                     continue
70                 if peer_logical & arc == 0:
71                     # we are outgoing
72                     if verbose:
73                         print(peer_physical, "connects to", partner_physical)
74                     peer_type = "outgoing"
75                     if peer_logical < num_ghosts:
76                         # we have a ghost, check if the peer who connects
77                         # to our ghost is actually outgoing
78                         ghost_partner_logical = (peer_logical - arc) % n
79                         if ghost_partner_logical & arc == 0:
80                             peer_type = peer_type + ", ghost incoming"
81                     new_info[peer_physical] = new_info[peer_physical] | info[peer_physical] | info[partner_physical]
82                     new_info[partner_physical] = new_info[partner_physical] | info[peer_physical] | info[partner_physical]
83                 else:
84                     peer_type = "incoming"
85                 if verbose > 1:
86                     print("type of", str(peer_physical) + ":", peer_type)
87             info = new_info
88             arc = arc << 1
89         rounds = rounds + 1
90         random.shuffle(peers)
91     return rounds
92
93
94 if __name__ == "__main__":
95     parser = argparse.ArgumentParser()
96     parser.add_argument("k", metavar="k", type=int, help="#(bad peers)")
97     parser.add_argument("n", metavar="n", type=int, help="#(all peers)")
98     parser.add_argument("r", metavar="r", type=int, help="#(rounds)")
99     parser.add_argument('--verbose', '-v', action='count')
100
101     args = parser.parse_args()
102     sum = 0.0
103     for n in xrange(0, args.r):
104         sum += simulate(args.k, args.n, args.verbose)
105     print(sum / args.r)