import socket
import funcs
import user
import threading
import json
import redis
import time
import sys

HOST = '127.0.0.1'
PORT = 9997
TRANSACTION_QUEUE_KEY = 'transactions.queue'
BLOCK_KEY_PREFIX = 'chain.block.'
PREV_HASH_KEY = 'prev_hash'
BLOCK_USED_KEY_PREFIX = 'chain.block.used.'

prev_hash = 'block hash of genesis'


def miner_thread(sock, User):
	miner = Miner()

	while True:
		block = miner.mine()
		funcs.send_message(sock,json.dumps(block))


def send_transaction(sock,User):
	pass


def handle_receive(sock, User):
    ''' Thread receives broadcasted data'''
    while True:
        message = funcs.receive_message(clientSock)
        data = json.loads(message)
        if message['type'] == 'transaction':
            payload = message['payload']
            # load transaction into a transaction object
            transaction = Transaction.from_json(payload)
            # verify transaction and if it is valid, put it into the
            # redis queue of transactions that need to be mined
            if transaction.verify():
                self.redis.rpush(TRANSACTION_QUEUE_KEY, json.dumps(payload))
            else:
                print("Invalid transaction received from tracker", file=sys.stderr)
                print("json of transaction: ", file=sys.stderr)
                print(json.dumps(payload, indent=4), file=sys.stderr)
        elif message['type'] == 'block':
            payload = message['payload']
            # load block into a block object and verify if it is valid
            # if it is valid, put it into redis and update the prev_hash key
            # and remove the transactions done from the pending transactions queue
            block = Block.from_json(payload)
            if block.verify():
                # add block to redis
                key = "{}{}".format(BLOCK_KEY_PREFIX, block.hash)
                self.redis.set(key, json.dumps(payload))

                # store in redis that this block hasn't been used yet
                self.redis.set("{}{}".format(BLOCK_USED_KEY_PREFIX, block.hash), '0')

                # make the prev_hash field used by local miner to be the hash of the block
                # we just added
                self.redis.set(PREV_HASH_KEY, block.hash)

                # TODO: remove pending transactions


if __name__ == '__main__':

	# the user managing this miner
	User = LazyUser()

	# boradcasting connection to csec
	clientSock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
	clientSock.connect((HOST,PORT))

	# The receiving thread for this miner
	th = threading.Thread(target = handle_receive, args = [clientSock,User], daemon = True)
	th.start()

	#The Broadcasting thread for this miner
	th = threading.Thread(target = miner_thread, args = [clientSock,User], daemon = True)
	th.start()

	th = threading.Thread(target = send_transaction, args = [clientSock,User], daemon = True)
	th.start()

	clientSock.close()

@@ -1056,26 +1056,32 @@
         
-self.
 redis
+_connection
 .rpush(T
@@ -1863,34 +1863,41 @@
             
-self.
 redis
+_connnection
 .set(key, js
@@ -1998,34 +1998,41 @@
             
-self.
 redis
+_connnection
 .set(%22%7B%7D%7B%7D%22.
@@ -2223,18 +2223,24 @@
     
-self.
 redis
+_connection
 .set

Fix redis connection