2018-05-23 00:22:10 +00:00
|
|
|
from networksim import NetworkSimulator
|
2018-07-23 21:05:30 +00:00
|
|
|
from ghost_node import Node, NOTARIES, Block, genesis
|
2018-05-23 00:22:10 +00:00
|
|
|
from distributions import normal_distribution
|
|
|
|
|
2018-07-31 23:15:02 +00:00
|
|
|
net = NetworkSimulator(latency=22)
|
|
|
|
notaries = [Node(i, net, ts=max(normal_distribution(300, 300)(), 0) * 0.1, sleepy=i%4==0) for i in range(NOTARIES)]
|
2018-05-23 00:22:10 +00:00
|
|
|
net.agents = notaries
|
|
|
|
net.generate_peers()
|
2018-06-09 10:04:04 +00:00
|
|
|
for i in range(100000):
|
2018-05-23 00:22:10 +00:00
|
|
|
net.tick()
|
|
|
|
for n in notaries:
|
2018-06-09 10:04:04 +00:00
|
|
|
print("Local timestamp: %.1f, timequeue len %d" % (n.ts, len(n.timequeue)))
|
2018-05-23 00:22:10 +00:00
|
|
|
print("Main chain head: %d" % n.blocks[n.main_chain[-1]].number)
|
2018-06-09 10:04:04 +00:00
|
|
|
print("Total main chain blocks received: %d" % (len([b for b in n.blocks.values() if isinstance(b, Block)]) - 1))
|
|
|
|
print("Notarized main chain blocks received: %d" % (len([b for b in n.blocks.values() if isinstance(b, Block) and n.is_notarized(b)]) - 1))
|
2018-05-23 00:22:10 +00:00
|
|
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import networkx as nx
|
|
|
|
import random
|
|
|
|
|
|
|
|
G=nx.Graph()
|
|
|
|
|
2018-06-09 10:04:04 +00:00
|
|
|
#positions = {genesis.hash: 0, beacon_genesis.hash: 0}
|
2018-05-23 00:22:10 +00:00
|
|
|
#queue = [
|
|
|
|
|
|
|
|
for b in n.blocks.values():
|
2018-06-09 10:04:04 +00:00
|
|
|
for en in notaries:
|
|
|
|
if isinstance(b, Block) and b.hash in en.processed and b.hash not in en.blocks:
|
|
|
|
assert (not en.have_ancestry(b.hash)) or b.ts > en.ts
|
2018-05-23 00:22:10 +00:00
|
|
|
if b.number > 0:
|
2018-06-09 10:04:04 +00:00
|
|
|
if isinstance(b, Block):
|
|
|
|
if n.is_notarized(b):
|
|
|
|
G.add_edge(b.hash, b.parent_hash, color='b')
|
|
|
|
else:
|
|
|
|
G.add_edge(b.hash, b.parent_hash, color='#dddddd')
|
2018-05-23 00:22:10 +00:00
|
|
|
|
2018-06-09 10:04:04 +00:00
|
|
|
|
|
|
|
cache = {genesis.hash: 0}
|
2018-05-23 00:22:10 +00:00
|
|
|
|
|
|
|
def mkoffset(b):
|
|
|
|
if b.hash not in cache:
|
|
|
|
cache[b.hash] = cache[b.parent_hash] + random.randrange(35)
|
|
|
|
return cache[b.hash]
|
|
|
|
|
|
|
|
pos={b.hash: (b.ts + mkoffset(b), b.ts) for b in n.blocks.values()}
|
|
|
|
edges = G.edges()
|
|
|
|
colors = [G[u][v]['color'] for u,v in edges]
|
|
|
|
nx.draw_networkx_nodes(G,pos,node_size=10,node_shape='o',node_color='0.75')
|
|
|
|
|
|
|
|
nx.draw_networkx_edges(G,pos,
|
|
|
|
width=2,edge_color=colors)
|
|
|
|
|
|
|
|
plt.axis('off')
|
|
|
|
# plt.savefig("degree.png", bbox_inches="tight")
|
|
|
|
plt.show()
|