-
Notifications
You must be signed in to change notification settings - Fork 23
/
loganimation.py
48 lines (37 loc) · 1.56 KB
/
loganimation.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
import os
from hathor import graphviz
from hathor.manager import HathorEvents, HathorManager
class GraphvizLogAnimation:
""" Create a Graphviz SVG after every arrival of new blocks and transactions.
It was created for debugging purposes.
In the future, it can be expanded to generate a video or a webpage to visualize the images.
"""
def __init__(self, manager: HathorManager, dirname: str):
self.manager: HathorManager = manager
self.dirname: str = dirname
self.sequence: int = 0
self.is_running: bool = False
self.format: str = 'png'
def start(self) -> None:
""" Start recording.
"""
os.makedirs(self.dirname, exist_ok=True)
self.manager.pubsub.subscribe(HathorEvents.NETWORK_NEW_TX_ACCEPTED, self.on_new_tx)
self.is_running = True
def stop(self):
""" Stop recording.
"""
self.manager.pubsub.unsubscribe(HathorEvents.NETWORK_NEW_TX_ACCEPTED, self.on_new_tx)
self.is_running = False
def on_new_tx(self) -> None:
""" This method is called every change in the DAG. It saves a new snapshot in disk.
"""
if not self.is_running:
return
n = self.sequence
tx_storage = self.manager.tx_storage
dot1 = graphviz.verifications(tx_storage, format=self.format)
dot1.render(os.path.join(self.dirname, 'seq_v_{:010d}'.format(n)))
dot2 = graphviz.funds(tx_storage, format='png')
dot2.render(os.path.join(self.dirname, 'seq_f_{:010d}'.format(n)))
self.sequence += 1