Esempio n. 1
0
    def chainsync(self):
        redis_client = redis.Redis(host='localhost', port=6379, db=0)
        ip_list = []
        nodes_map = utils.decode_redis(redis_client.hgetall("nodes_map"))

        for ip_addr, raw_data in nodes_map.items():
            if ip_addr in settings.EXPLORER_IP:
                continue
            ip_list.append(ip_addr)

        # IP chosing method is under development!
        ip = random.choice(ip_list)

        udp = UDPHandler()

        context = zmq.Context()
        socket = context.socket(zmq.REP)
        udp.getchainlength({"ip_addr": ip})
        socket.connect("tcp://127.0.0.1:%s" % settings.SYNC_ZMQ_PORT)
        res = json.loads(socket.recv_string())
        length = res["body"]
        socket.send_string("Recieved Chain Length")
        socket.close()

        mylen = redis_client.llen("chain")

        if mylen == 0:
            for i in range(0, length):
                udp.getblockbyheight({"height": i, "ip_addr": ip})
                socket.connect("tcp://127.0.0.1:%s" % settings.SYNC_ZMQ_PORT)
                res = json.loads(socket.recv_string())
                socket.send_string("Recieved a block of the chain!!!")
                socket.close()
                blchain = Blockchain()
                blchain.add_block(res["body"])
            # chain verification
            verf = Verification()
            msg = verf.full_chain_verify()
            if msg != "verified":
                return self.chainsync()
        elif mylen > length:
            return self.chainsync()
        elif mylen == length:
            return
        elif mylen < length:
            for i in range(mylen, length):
                udp.getblockbyheight({"height": i, "ip_addr": ip})
                socket.connect("tcp://127.0.0.1:%s" % settings.SYNC_ZMQ_PORT)
                res = json.loads(socket.recv_string())
                socket.send_string("Recieved a block of the chain!!!")
                socket.close()
                blchain = BlockChain()
                blchain.add_block(res["body"])
            # chain verification
            verf = Verification()
            msg = verf.full_chain_verify()
            if msg != "verified":
                return self.chainsync()
Esempio n. 2
0
 def get_ip_from_addr(addr):
     redis_client = redis.Redis(host='localhost', port=6379, db=0)
     nodes_data = decode_redis(redis_client.hgetall("nodes_map"))
     for ip_addr, raw in nodes_data.keys():
         data = json.loads(raw)
         if data["node_addr"] == addr:
             redis_client.close()
             return ip_addr
     redis_client.close()
     return "Unknown"
Esempio n. 3
0
 def broadcastmessage(message):
     host = '0.0.0.0'
     port = settings.UDP_BROADCAST_PORT
     udpsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     udpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     udpsock.bind((host, port))
     redis_client = redis.Redis(host='localhost', port=6379, db=0)
     redis_data = decode_redis(redis_client.hgetall("nodes_map"))
     for ip_addr, raw_data in redis_data.items():
         data = json.loads(raw_data)
         udpsock.sendto(message.encode('utf-8'),
                        (ip_addr, data["receiver_port"]))
     udpsock.close()
Esempio n. 4
0
    def memsync(self):
        ip_address = utils.get_own_ip()
        print("Mempool sync started ...")
        redis_client = redis.Redis(host='localhost', port=6379, db=0)
        ip_list = []
        ip_list.append(ip_address)

        nodes_map = utils.decode_redis(redis_client.hgetall("nodes_map"))

        for ip_addr, raw_data in nodes_map.items():
            if ip_addr == ip_address:
                continue
            if ip_addr in settings.EXPLORER_IP:
                continue
            else:
                ip_list.append(ip_addr)

        print("Nodes list : " + str(ip_list))

        ip_list.sort()
        length = len(ip_list)
        send = ""

        if length == 0:
            print("NO NODES ACTIVE TO SYNC WITH!!!")
            return

        for i in range(0, length):
            if ip_list[i] == ip_address:
                if i == 0:
                    send = ip_list[-1]
                else:
                    send = ip_list[i - 1]

        print("Starting mempool transaction sync ...")
        # UDP command to send txs to prev addr
        i = 0
        while True:
            tx = redis_client.lindex("mempool", i)
            if tx == None:
                break
            udp = UDPHandler()
            print("Sending transaction for sync " + str(i) + "....")
            udp.synctx({"body": tx.decode(), "ip_addr": send})
            i = i + 1

        time.sleep(1)
        print("Mempool sync finished!!!")
Esempio n. 5
0
    def broadcastmessage(message):
        """ This method is to broadcast message to all IPs """

        own_ip = get_own_ip()
        host = '0.0.0.0'
        port = settings.UDP_BROADCAST_PORT
        udpsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        udpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        udpsock.bind((host, port))
        redis_client = redis.Redis(host='localhost', port=6379, db=0)
        nodes_map = decode_redis(redis_client.hgetall("nodes_map"))
        # print(nodes_map)
        for ip_addr, raw_data in nodes_map.items():
            if ip_addr == own_ip:
                continue
            data = json.loads(raw_data)
            # print(data)
            udpsock.sendto(message.encode('utf-8'),
                           (ip_addr, int(data["receiver_port"])))
        udpsock.close()
Esempio n. 6
0
 def get_stakes(self):
     # Get stakes from redis
     self.stakes_map = decode_redis(self.redis_client.hgetall("stakes_map"))
     print("Scanned stakes ...!")
Esempio n. 7
0
 def get_stakes(self):
     self.stakes_map = decode_redis(redis_client.hgetall("stakes_map"))
Esempio n. 8
0
 def synctime(self):
     redis_client = redis.Redis(host='localhost', port=6379, db=0)
     nodes_data = decode_redis(redis_client.hgetall("nodes_map"))
     redis_client.close()