forked from Team-DDev/bitsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_monitor.py
58 lines (41 loc) · 1.48 KB
/
main_monitor.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
import argparse
import time
import zmq
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
import secret
FLAGS = _ = None
DEBUG = False
def monitor():
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332" % (secret.rpcuser, secret.rpcpassword))
best_block_hash = rpc_connection.getbestblockhash()
return best_block_hash
def monitor_message(best_block_hash):
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332" % (secret.rpcuser, secret.rpcpassword))
block = rpc_connection.getblock(monitor())
message = {'hash': monitor(), 'height': block['height']}
return message
def main():
if DEBUG:
print(f'Parsed arguments {FLAGS}')
print(f'Unparsed arguments {_}')
try:
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.connect("tcp://127.0.0.1:5556")
while True:
value = monitor()
time.sleep(3)
best_block_hash = monitor()
if value != best_block_hash:
message = monitor_message(best_block_hash)
socket.send_json(message)
print('New Block : ', message)
except KeyboardInterrupt:
print("keyboradinterrupt")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--debug', action='store_true',
help='The present debug message')
FLAGS, _=parser.parse_known_args()
DEBUG = FLAGS.debug
main()