Skip to content

Instantly share code, notes, and snippets.

@jhodges10
Last active July 3, 2018 19:11
Show Gist options
  • Save jhodges10/5db38d6fc1797fcee0b1605ef09a0f1d to your computer and use it in GitHub Desktop.
Save jhodges10/5db38d6fc1797fcee0b1605ef09a0f1d to your computer and use it in GitHub Desktop.
Dash ZeroMQ Test
#!/usr/bin/env python
import binascii
import zmq
import struct
import csv
import time
port = 28332
def write_csv(tstamp, type, value, sequence):
with open("../../../messages.csv", 'wb') as mlog_file:
msg_logger = csv.writer(mlog_file, delimter=',')
msg_logger.writerow([tstamp, type, value, sequence])
return True
def zmq_tx_consumer():
zmqContext = zmq.Context()
zmqSubSocket = zmqContext.socket(zmq.SUB)
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
# zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtx")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawgovernanceobject")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawgovernancevote")
zmqSubSocket.connect("tcp://127.0.0.1:%i" % port)
try:
while True:
msg = zmqSubSocket.recv_multipart()
topic = str(msg[0].decode("utf-8"))
body = msg[1]
sequence = "Unknown";
if len(msg[-1]) == 4:
msgSequence = struct.unpack('<I', msg[-1])[-1]
sequence = str(msgSequence)
if topic == "hashblock":
print('- HASH BLOCK ('+sequence+') -')
hashblock = binascii.hexlify(body).decode("utf-8")
write_csv(time.time(), 'hashblock', hashblock, sequence)
print(hashblock)
# initialstate.send_log({"block_count": "{}".format(sequence)})
elif topic == "rawgovernanceobject":
print('- RAW GOVERNANCE OBJECT ('+sequence+') -')
governance_object = binascii.hexlify(body).decode("utf-8")
write_csv(time.time(), 'rawgovernanceobject', governance_object, sequence)
print(governance_object)
# initialstate.send_log({"hash": governance_object, "tx_count": sequence})
elif topic == "rawgovernancevote":
print('- RAW GOVERNANCE VOTE ('+sequence+') -')
governance_vote = binascii.hexlify(body).decode("utf-8")
write_csv(time.time(), 'rawgovernancevote', governance_vote, sequence)
print(governance_votev)
# initialstate.send_log({"hash": governance_vote, "tx_count": sequence})
'''
elif topic == "hashtx":
print('- HASH TX ('+sequence+') -')
tx_hash = binascii.hexlify(body).decode("utf-8")
initialstate.send_log({"hash": tx_hash, "tx_count": sequence})
print(binascii.hexlify(body).decode("utf-8"))
elif topic == "hashtxlock":
print('- HASH TX LOCK ('+sequence+') -')
print(binascii.hexlify(body).decode("utf-8"))
elif topic == "rawblock":
print('- RAW BLOCK HEADER ('+sequence+') -')
print(binascii.hexlify(body[:80]).decode("utf-8"))
elif topic == "rawtx":
print('- RAW TX ('+sequence+') -')
print(binascii.hexlify(body).decode("utf-8"))
elif topic == "rawtxlock":
print('- RAW TX LOCK ('+sequence+') -')
print(binascii.hexlify(body).decode("utf-8"))
'''
except KeyboardInterrupt:
zmqContext.destroy()
if __name__ == '__main__':
print("Starting ZMQ Listener...")
zmq_tx_consumer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment