-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtestnet-gobject.py
More file actions
100 lines (80 loc) · 2.26 KB
/
testnet-gobject.py
File metadata and controls
100 lines (80 loc) · 2.26 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import io, os, sys
import simplejson as json
import datetime
import time
import zmq
import array
import binascii
import struct
import psutil
import re
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
from ISStreamer.Streamer import Streamer
# --- change
# rpc
rpcuser = 'rpc_user' # change
rpcpassword = 'rpc_paasword' # change
rpcbindip = '127.0.0.1'
rpcport = 19998
# zmq
zmqport = 28332
# initialstate
iss_bucket_name = 'bucket-name' # change
iss_bucket_key = 'bucket-key' # change
iss_access_key = 'access-key' # change
iss_prefix = 'prefix' # change to hostname or uniq name
# --- / change
def checksynced():
try:
synced = access.mnsync('status')['IsSynced']
return synced
except:
return False
def rpcgetinfo():
try:
now = time.time()
getinfo = access.gobject('list')
if len(getinfo) > 0:
gsec = time.time() - now
bucket = {}
bucket['gsec'] = gsec
bucket['len'] = len(getinfo)
streamer.log_object(bucket, key_prefix=iss_prefix + '_gobject')
streamer.flush()
except:
pass
# rpc
serverURL = 'http://' + rpcuser + ':' + rpcpassword + '@' + rpcbindip + ':' + str(rpcport)
access = AuthServiceProxy(serverURL)
while(not checksynced()):
time.sleep(30)
# zmq
zmqContext = zmq.Context()
zmqSubSocket = zmqContext.socket(zmq.SUB)
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
zmqSubSocket.connect("tcp://%s:%i" % (rpcbindip, zmqport))
# iss
streamer = Streamer(bucket_name=iss_bucket_name, bucket_key=iss_bucket_key, access_key=iss_access_key, buffer_size=100)
# main
try:
while True:
msg = zmqSubSocket.recv_multipart()
topic = str(msg[0].decode("utf-8"))
body = str(binascii.hexlify(msg[1]).decode("utf-8"))
sequence = "Unknown"
if len(msg[-1]) == 4:
msgSequence = struct.unpack('<I', msg[-1])[-1]
sequence = str(msgSequence)
if topic == "hashblock":
rpcgetinfo()
except Exception as e:
print(e.args[0])
streamer.close()
zmqContext.destroy()
sys.exit()
except KeyboardInterrupt:
streamer.close()
#zmqContext.destroy()
sys.exit()