consensus-simulation.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/python
  2. # This file is part of GNUnet
  3. # (C) 2013 Christian Grothoff (and other contributing authors)
  4. #
  5. # GNUnet is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published
  7. # by the Free Software Foundation; either version 2, or (at your
  8. # 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. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with GNUnet; see the file COPYING. If not, write to the
  17. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. # Boston, MA 02111-1307, USA.
  19. import argparse
  20. import random
  21. from math import ceil,log,floor
  22. def bsc(n):
  23. """ count the bits set in n"""
  24. l = n.bit_length()
  25. c = 0
  26. x = 1
  27. for _ in range(0, l):
  28. if n & x:
  29. c = c + 1
  30. x = x << 1
  31. return c
  32. def simulate(k, n, verbose):
  33. assert k < n
  34. largest_arc = int(2**ceil(log(n, 2))) / 2
  35. num_ghosts = (2 * largest_arc) - n
  36. if verbose:
  37. print "we have", num_ghosts, "ghost peers"
  38. # n.b. all peers with idx<k are evil
  39. peers = range(n)
  40. info = [1 << x for x in xrange(n)]
  41. def done_p():
  42. for x in xrange(k, n):
  43. if bsc(info[x]) < n-k:
  44. return False
  45. return True
  46. rounds = 0
  47. while not done_p():
  48. if verbose:
  49. print "-- round --"
  50. arc = 1
  51. while arc <= largest_arc:
  52. if verbose:
  53. print "-- subround --"
  54. new_info = [x for x in info]
  55. for peer_physical in xrange(n):
  56. peer_logical = peers[peer_physical]
  57. peer_type = None
  58. partner_logical = (peer_logical + arc) % n
  59. partner_physical = peers.index(partner_logical)
  60. if peer_physical < k or partner_physical < k:
  61. if verbose:
  62. print "bad peer in connection", peer_physical, "--", partner_physical
  63. continue
  64. if peer_logical & arc == 0:
  65. # we are outgoing
  66. if verbose:
  67. print peer_physical, "connects to", partner_physical
  68. peer_type = "outgoing"
  69. if peer_logical < num_ghosts:
  70. # we have a ghost, check if the peer who connects
  71. # to our ghost is actually outgoing
  72. ghost_partner_logical = (peer_logical - arc) % n
  73. if ghost_partner_logical & arc == 0:
  74. peer_type = peer_type + ", ghost incoming"
  75. new_info[peer_physical] = new_info[peer_physical] | info[peer_physical] | info[partner_physical]
  76. new_info[partner_physical] = new_info[partner_physical] | info[peer_physical] | info[partner_physical]
  77. else:
  78. peer_type = "incoming"
  79. if verbose > 1:
  80. print "type of", str(peer_physical) + ":", peer_type
  81. info = new_info
  82. arc = arc << 1;
  83. rounds = rounds + 1
  84. random.shuffle(peers)
  85. return rounds
  86. if __name__ == "__main__":
  87. parser = argparse.ArgumentParser()
  88. parser.add_argument("k", metavar="k", type=int, help="#(bad peers)")
  89. parser.add_argument("n", metavar="n", type=int, help="#(all peers)")
  90. parser.add_argument("r", metavar="r", type=int, help="#(rounds)")
  91. parser.add_argument('--verbose', '-v', action='count')
  92. args = parser.parse_args()
  93. sum = 0.0;
  94. for n in xrange (0, args.r):
  95. sum += simulate(args.k, args.n, args.verbose)
  96. print sum / args.r;