示例#1
0
    def on_message(self, msg):
        seq = tornado.escape.json_decode(msg)
        print(current_port, "on message from buddy connector", seq)
        if seq[0] == "DISCARDED_BRANCHES":
            for i in seq[1]:
                branch_host, branch_port, branch = i
                if tuple([branch_host, branch_port,
                          branch]) in available_branches:
                    available_branches.remove(
                        tuple([branch_host, branch_port, branch]))

        elif seq[0] == "AVAILABLE_BRANCHES":
            for i in seq[1]:
                branch_host, branch_port, branch = i
                available_branches.add(
                    tuple([branch_host, branch_port, branch]))
                available_children_buddies.setdefault(branch[:-1], set()).add(
                    (branch_host, branch_port))

        elif seq[0] == "NEW_BLOCK":
            miner.new_block(seq)

        elif seq[0] == "NEW_TX_BLOCK":
            miner.new_tx_block(seq)

        forward(seq)
示例#2
0
    def on_message(self, msg):
        global current_groupid
        global node_neighborhoods

        seq = tornado.escape.json_decode(msg)
        # print(current_port, "on message from child", seq)
        if seq[0] == "DISCARDED_BRANCHES":
            for i in seq[1]:
                branch_host, branch_port, branch = i
                if tuple([branch_host, branch_port,
                          branch]) in available_branches:
                    available_branches.remove(
                        tuple([branch_host, branch_port, branch]))

        elif seq[0] == "AVAILABLE_BRANCHES":
            for i in seq[1]:
                branch_host, branch_port, branch = i
                available_branches.add(
                    tuple([branch_host, branch_port, branch]))

        elif seq[0] == "NODE_NEIGHBOURHOODS":
            groupid = seq[1]
            if current_groupid is not None and group_distance(
                    groupid, current_groupid) > setting.NEIGHBOURHOODS_HOPS:
                return
            hosts = node_neighborhoods.get(groupid, [])
            node_neighborhoods[groupid] = [
                list(i) for i in set([tuple(i) for i in hosts + seq[2]])
            ]
            # print(current_port, "NODE_NEIGHBOURHOODS", current_groupid, groupid, node_neighborhoods)

        elif seq[0] == "NEW_BLOCK":
            miner.new_block(seq)

        elif seq[0] == "NEW_TX_BLOCK":
            miner.new_tx_block(seq)

        elif seq[0] == "NEW_TX":
            txid = seq[1]["transaction"]["txid"]
            if (
                    current_host, current_port
            ) in leader.current_leaders and txid not in processed_message_ids:
                processed_message_ids.add(txid)
                leader.transactions.append(seq)
                print(current_port, "tx msg", seq)

        elif seq[0] == "UPDATE_HOME":
            fs.transactions.append(seq)

        forward(seq)
示例#3
0
    def on_message(self, msg):
        global available_branches
        global available_buddies
        global available_children_buddies

        if msg is None:
            self.ws_uri = "ws://%s:%s/buddy?host=%s&port=%s" % (
                self.host, self.port, current_host, current_port)
            tornado.ioloop.IOLoop.instance().call_later(1.0, self.connect)
            return

        seq = tornado.escape.json_decode(msg)
        print(current_port, "on message from buddy", seq)
        if seq[0] == "DISCARDED_BRANCHES":
            for i in seq[1]:
                branch_host, branch_port, branch = i
                if tuple([branch_host, branch_port,
                          branch]) in available_branches:
                    available_branches.remove(
                        tuple([branch_host, branch_port, branch]))

            # for node in NodeHandler.child_nodes.values():
            #     node.write_message(msg)

        elif seq[0] == "AVAILABLE_BRANCHES":
            for i in seq[1]:
                branch_host, branch_port, branch = i
                available_branches.add(
                    tuple([branch_host, branch_port, branch]))
                available_children_buddies.setdefault(branch[:-1], set()).add(
                    (branch_host, branch_port))

            # for node in NodeHandler.child_nodes.values():
            #     node.write_message(msg)

        elif seq[0] == "GROUP_ID_FOR_BUDDY":
            current_groupid = self.branch = seq[1]
            available_branches.add(
                tuple([current_host, current_port, current_groupid + "0"]))
            available_branches.add(
                tuple([current_host, current_port, current_groupid + "1"]))

            available_buddies = available_buddies.union(
                set([tuple(i) for i in seq[2]]))
            buddies_left = available_buddies - set(
                [tuple([current_host, current_port])])
            buddies_left = buddies_left - set(
                [(i.host, i.port) for i in BuddyConnector.buddy_nodes])
            buddies_left = buddies_left - set(
                [(i.from_host, i.from_port) for i in BuddyHandler.buddy_nodes])
            for h, p in buddies_left:
                BuddyConnector(h, p)

            if self.conn is not None:
                message = [
                    "AVAILABLE_BRANCHES",
                    [[current_host, current_port, self.branch + "0"],
                     [current_host, current_port, self.branch + "1"]],
                    uuid.uuid4().hex
                ]
                self.conn.write_message(tornado.escape.json_encode(message))
            return

        elif seq[0] == "NEW_BLOCK":
            miner.new_block(seq)

        elif seq[0] == "NEW_TX_BLOCK":
            miner.new_tx_block(seq)

        # else:
        forward(seq)
示例#4
0
    def on_message(self, msg):
        global available_buddies
        global current_branch
        global current_groupid
        global node_parents
        global node_neighborhoods

        if msg is None:
            # print("reconnect2 ...")
            if current_branch in available_branches:
                available_branches.remove(current_branch)
            # available_branches = set([tuple(i) for i in branches])
            branches = list(available_branches)
            current_branch = tuple(branches[0])
            branch_host, branch_port, branch = current_branch
            self.ws_uri = "ws://%s:%s/node?branch=%s&host=%s&port=%s" % (
                branch_host, branch_port, branch, current_host, current_port)
            tornado.ioloop.IOLoop.instance().call_later(1.0, self.connect)
            return

        seq = tornado.escape.json_decode(msg)
        # print(current_port, "on message from parent", seq)
        if seq[0] == "DISCARDED_BRANCHES":
            for i in seq[1]:
                branch_host, branch_port, branch = i
                if tuple([branch_host, branch_port,
                          branch]) in available_branches:
                    available_branches.remove(
                        tuple([branch_host, branch_port, branch]))

            # for node in NodeHandler.child_nodes.values():
            #     node.write_message(msg)

        elif seq[0] == "AVAILABLE_BRANCHES":
            for i in seq[1]:
                branch_host, branch_port, branch = i
                available_branches.add(
                    tuple([branch_host, branch_port, branch]))

            # for node in NodeHandler.child_nodes.values():
            #     node.write_message(msg)

            message = [
                "NODE_NEIGHBOURHOODS", current_groupid,
                list(available_buddies),
                uuid.uuid4().hex
            ]
            forward(message)

        elif seq[0] == "GROUP_ID":
            current_groupid = seq[1]
            available_buddies = available_buddies.union(
                set([tuple(i) for i in seq[2]]))
            buddies_left = available_buddies - set(
                [tuple([current_host, current_port])])
            buddies_left = buddies_left - set(
                [(i.host, i.port) for i in BuddyConnector.buddy_nodes])
            buddies_left = buddies_left - set(
                [(i.from_host, i.from_port) for i in BuddyHandler.buddy_nodes])
            for h, p in buddies_left:
                BuddyConnector(h, p)

            control_node.write_message(
                tornado.escape.json_encode(
                    ["ADDRESS2", current_host, current_port,
                     current_groupid]))  #传branch!!!!!!!!!!!!!!!!!!

            available_children_buddies.setdefault(current_groupid, set()).add(
                (current_host, current_port))
            print(current_port, "GROUP_ID", current_groupid, seq[3])
            node_parents[current_groupid] = [
                list(i) for i in available_buddies
            ]
            # print(current_port, "NODE_PARENTS", node_parents[current_groupid])

            if self.conn and not self.conn.stream.closed:
                message = [
                    "NODE_NEIGHBOURHOODS", current_groupid,
                    list(available_buddies),
                    uuid.uuid4().hex
                ]
                self.conn.write_message(tornado.escape.json_encode(message))
            return

        elif seq[0] == "NODE_PARENTS":
            node_parents.update(seq[1])
            # print(current_port, "NODE_PARENTS", node_parents)

            for child_node in NodeHandler.child_nodes.values():
                child_node.write_message(msg)
            return

        elif seq[0] == "NODE_NEIGHBOURHOODS":
            groupid = seq[1]
            if current_groupid is not None and group_distance(
                    groupid, current_groupid) > setting.NEIGHBOURHOODS_HOPS:
                return
            hosts = node_neighborhoods.get(groupid, [])
            node_neighborhoods[groupid] = [
                list(i) for i in set([tuple(i) for i in hosts + seq[2]])
            ]
            # print(current_port, "NODE_NEIGHBOURHOODS", current_groupid, groupid, node_neighborhoods)

        elif seq[0] == "NEW_BLOCK":
            miner.new_block(seq)

        elif seq[0] == "NEW_TX_BLOCK":
            miner.new_tx_block(seq)

        elif seq[0] == "NEW_TX":
            txid = seq[1]["transaction"]["txid"]
            if (
                    current_host, current_port
            ) in leader.current_leaders and txid not in processed_message_ids:
                processed_message_ids.add(txid)
                leader.transactions.append(seq)
                print(current_port, "tx msg", seq)

        # else:
        forward(seq)