-
Notifications
You must be signed in to change notification settings - Fork 2
/
DrawCluster.py
68 lines (55 loc) · 2.24 KB
/
DrawCluster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import blockbuilder
import networkx
from networkx.drawing.nx_agraph import write_dot, graphviz_layout
import matplotlib.pyplot as plt
def readCluster(fileLocation):
try:
clusterFile = open(fileLocation, 'r')
line = clusterFile.readline().strip()
cluster = blockbuilder.Cluster(mempool.txs[line], 40000)
line = clusterFile.readline().strip()
while line:
print(line)
cluster.addTx(mempool.txs[line])
line = clusterFile.readline().strip()
clusterFile.close()
return cluster
except FileNotFoundError:
print("file not found: "+fileLocation)
def readClusterFromMempool():
return list(mempool.cluster(4000000).values())[0]
def drawClusterGraph(cluster):
G = networkx.DiGraph()
for tx in cluster.txs:
G.add_node(mempool.txs[tx].txid)
for parent in mempool.txs[tx].parents:
G.add_edge(mempool.txs[tx].txid, mempool.txs[parent].txid)
# write_dot(G, 'test.dot')
plt.title("graph")
# pos = graphviz_layout(G, prog='dot')
# networkx.draw(G, pos, with_labels=False, arrows=True)
nodeClr = [mempool.txs[tx].fee/mempool.txs[tx].weight for tx in G.nodes()]
nodeSize = [mempool.txs[tx].weight*10 for tx in G.nodes()]
print(nodeClr)
networkx.draw(G, with_labels=False, node_color=nodeClr)
plt.show()
def drawHierarchicalGraph(cluster):
G = networkx.DiGraph()
for tx in cluster.txs:
G.add_node(mempool.txs[tx].txid, label='')
for parent in mempool.txs[tx].parents:
G.add_edge(mempool.txs[tx].txid, mempool.txs[parent].txid)
p = networkx.drawing.nx_pydot.to_pydot(G)
p.write_png('plot-219.png')
if __name__== "__main__":
mempool = blockbuilder.Mempool()
# mempool.fromTXT(r"./data/data example/000000000000000000067df78658a05f17aea0844d11c1854a740abf8b6b70cb.mempool")
mempool.fromJSON(r"./problemclusters/219-010adae49f37806edb85e8a3faedaff4b9b0ee509d581b622c1354aa235d7fe3")
cluster = readClusterFromMempool()
print(type(cluster))
# drawClusterGraph(cluster)
drawHierarchicalGraph(cluster)
'''
cluster_to_check = readCluster(r"./data/data example/123_cluster_example")
print(cluster_to_check)
drawClusterGraph(cluster_to_check)'''