22from math
import ceil, log, floor
26 """ count the bits set in n"""
39 largest_arc =
int(2**ceil(log(n, 2))) // 2
40 num_ghosts = (2 * largest_arc) - n
42 print(
"we have", num_ghosts,
"ghost peers")
44 peers =
list(range(n))
45 info = [1 << x
for x
in range(n)]
49 if bsc(info[x]) < n - k:
58 while arc <= largest_arc:
60 print(
"-- subround --")
61 new_info = [x
for x
in info]
62 for peer_physical
in range(n):
63 peer_logical = peers[peer_physical]
65 partner_logical = (peer_logical + arc) % n
66 partner_physical = peers.index(partner_logical)
67 if peer_physical < k
or partner_physical < k:
70 "bad peer in connection", peer_physical,
"--",
74 if peer_logical & arc == 0:
77 print(peer_physical,
"connects to", partner_physical)
78 peer_type =
"outgoing"
79 if peer_logical < num_ghosts:
82 ghost_partner_logical = (peer_logical - arc) % n
83 if ghost_partner_logical & arc == 0:
84 peer_type = peer_type +
", ghost incoming"
85 new_info[peer_physical] = new_info[peer_physical] | info[
86 peer_physical] | info[partner_physical]
87 new_info[partner_physical
88 ] = new_info[partner_physical] | info[
89 peer_physical] | info[partner_physical]
91 peer_type =
"incoming"
93 print(
"type of", str(peer_physical) +
":", peer_type)
101if __name__ ==
"__main__":
102 parser = argparse.ArgumentParser()
103 parser.add_argument(
"k", metavar=
"k", type=int, help=
"#(bad peers)")
104 parser.add_argument(
"n", metavar=
"n", type=int, help=
"#(all peers)")
105 parser.add_argument(
"r", metavar=
"r", type=int, help=
"#(rounds)")
106 parser.add_argument(
'--verbose',
'-v', action=
'count')
108 args = parser.parse_args()
110 for n
in range(0, args.r):
111 sum +=
simulate(args.k, args.n, args.verbose)
static int list
Set if we should print a list of currently running services.
def simulate(k, n, verbose)