-
Notifications
You must be signed in to change notification settings - Fork 19
/
CONNECT_WEBSOCKET.py
101 lines (74 loc) · 2.48 KB
/
CONNECT_WEBSOCKET.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import json
import logging
import rel
import websocket
from CONFIG import KV_STORE, RPC_WEBSOCKET
SUBSCRIBE_MSG = '{"jsonrpc": "2.0", "method": "subscribe", "params": ["tm.event=\'NewBlock\'"], "id": 1}'
logger = logging.getLogger(__name__)
CONNECTED = False
# on a new block message, we will clear in the KV Store of any values which the config set to -2
# Use this for an indexer in the future?? :D
def on_message(ws, message):
msg = json.loads(message)
if msg.get("result") == {}:
logger.info("Subscribed to New Block with TendermintRPC...")
return
# block_height = msg["result"]["data"]["value"]["block"]["header"]["height"]
block_height = (
msg.get("result", {})
.get("data", {})
.get("value", {})
.get("block", {})
.get("header", {})
.get("height", -1)
)
if block_height == -1:
logger.error("Error: block height not found")
return
logger.debug(f"""New Block: {block_height}""")
del_keys = KV_STORE.get_keys("*;IsBlockOnly;*")
if len(del_keys) > 0:
res: bool = KV_STORE.delete(del_keys)
if res:
logger.debug(f"Deleting {len(del_keys)} keys...")
# KV_STORE.dump()
def on_error(ws, error):
logger.error(error)
def on_close(ws, close_status_code, close_msg):
logger.info("Closed connection")
def on_open(ws):
logger.info("Opened connection")
ws.send(SUBSCRIBE_MSG)
logger.info("Sent subscribe request")
class TendermintRPCWebSocket:
def __init__(
self,
enableSignal: bool = False,
enableTrace: bool = False,
logLevel: int = logging.DEBUG,
):
self.enableSignal = enableSignal
websocket.enableTrace(enableTrace) # toggle to show or hide output
self.ws = websocket.WebSocketApp(
f"{RPC_WEBSOCKET}",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close,
)
logger.setLevel(logLevel)
logger.addHandler(logging.StreamHandler())
def start(self):
if self.enableSignal:
self.ws.run_forever(dispatcher=rel, reconnect=5)
self.signal(2, rel.abort)
self.dispatch()
else:
self.run_forever()
def signal(self, sig, func):
rel.signal(sig, func)
def dispatch(self):
rel.dispatch()
if __name__ == "__main__":
tmrpc = TendermintRPCWebSocket(enableSignal=True) # so we can ctrl+c
tmrpc.start()