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)
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)
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 = json.loads(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(json.dumps(message)) return elif seq[0] == "NEW_BLOCK": miner.new_block(seq) # else: forward(seq)
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 = json.loads(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) 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 is not None: message = [ "NODE_NEIGHBOURHOODS", current_groupid, list(available_buddies), uuid.uuid4().hex ] self.conn.write_message(json.dumps(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": if (current_host, current_port) in leader.current_leaders: leader.transactions.append(seq) print(current_port, "txid", seq[1]["transaction"]["txid"]) # else: forward(seq)
def on_message(self, message): global current_branch global current_nodeid global node_parents global node_neighborhoods global nodes_pool global parent_node_id_msg global control_node global control_node_msg_queue yield tornado.gen.sleep(random.random()) if message is None: print("NodeConnector reconnect2 ...") # retry before choose another parent # 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 # sig = node_sk.sign(b"%s%s%s%s" % (branch.encode("utf8"), current_host.encode("utf8"), current_port.encode("utf8"), self.pk)) # print(sig) # self.ws_uri = "ws://%s:%s/node?branch=%s&host=%s&port=%s&pk=%s&sig=%s" % (branch_host, branch_port, branch, current_host, current_port, base64.b32encode(self.pk).decode("utf8"), base64.b32encode(sig).decode("utf8")) tornado.ioloop.IOLoop.instance().call_later(1.0, self.connect) return seq = tornado.escape.json_decode(message) # 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(message) 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(message) message = ["NODE_NEIGHBOURHOODS", current_nodeid, [current_host, current_port], uuid.uuid4().hex] forward(message) elif seq[0] == "NODE_ID": pk = seq[1] nodeid = seq[2] parent_pk = seq[3] parent_nodeid = seq[4] timestamp = seq[5] if parent_nodeid == "": nodes_pool[parent_nodeid] = [parent_pk, timestamp] nodes_pool[nodeid] = [pk, timestamp] print(current_port, "NODE_ID", nodeid, self.branch, pk, parent_nodeid, parent_pk, seq[-1]) if self.branch == nodeid: current_nodeid = nodeid print(current_port, "control_node", control_node) if control_node: control_node.write_message(tornado.escape.json_encode(["ADDRESS2", current_host, current_port, current_nodeid])) else: print(current_port, "ADDRESS2 queue", current_host, current_port, current_nodeid) control_node_msg_queue.append(["ADDRESS2", current_host, current_port, current_nodeid]) # print(current_port, "NODE_PARENTS", node_parents[current_nodeid]) if self.conn and not self.conn.stream.closed: message = ["NODE_NEIGHBOURHOODS", current_nodeid, [current_host, current_port], uuid.uuid4().hex] self.conn.write_message(tornado.escape.json_encode(message)) parent_node_id_msg = seq # 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(message) return elif seq[0] == "NODE_NEIGHBOURHOODS": nodeid = seq[1] if current_nodeid is not None and node_distance(nodeid, current_nodeid) > setting.NEIGHBOURHOODS_HOPS: return node_neighborhoods[nodeid] = tuple(seq[2]) # print(current_port, "NODE_NEIGHBOURHOODS", current_nodeid, nodeid, node_neighborhoods) elif seq[0] == "NEW_CHAIN_BLOCK": miner.new_block(seq) if control_node: control_node.write_message(tornado.escape.json_encode(["BLOCK", current_nodeid, seq])) elif seq[0] == "NEW_TX_BLOCK": leader.new_tx_block(seq) msg.WaitMsgHandler.new_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: if txid not in processed_message_ids: processed_message_ids.add(txid) leader.message_queue.append(seq) # print(current_port, "tx msg", seq) elif seq[0] == "NEW_MSG_BLOCK": print(current_port, "NEW_MSG_BLOCK") leader.new_msg_block(seq) msg.WaitMsgHandler.new_block(seq) elif seq[0] == "NEW_MSG": msgid = seq[1]["message"]["msgid"] # if (current_host, current_port) in leader.current_leaders and msgid not in processed_message_ids: if msgid not in processed_message_ids: processed_message_ids.add(msgid) leader.message_queue.append(seq) # else: forward(seq)
def on_message(self, message): global current_nodeid global node_neighborhoods global nodes_pool yield tornado.gen.sleep(random.random()) seq = tornado.escape.json_decode(message) # 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_ID": pk = seq[1] nodeid = seq[2] parent_pk = seq[3] parent_nodeid = seq[4] timestamp = seq[5] if parent_nodeid == "": nodes_pool[parent_nodeid] = [parent_pk, timestamp] nodes_pool[nodeid] = [pk, timestamp] print(current_port, "NODE_ID", nodeid, pk, parent_nodeid, parent_pk, seq[-1]) elif seq[0] == "NODE_NEIGHBOURHOODS": nodeid = seq[1] if current_nodeid is not None and node_distance(nodeid, current_nodeid) > setting.NEIGHBOURHOODS_HOPS: return node_neighborhoods[nodeid] = tuple(seq[2]) # print(current_port, "NODE_NEIGHBOURHOODS", current_nodeid, nodeid, node_neighborhoods) elif seq[0] == "NEW_CHAIN_BLOCK": miner.new_block(seq) if control_node: control_node.write_message(tornado.escape.json_encode(["BLOCK", current_nodeid, seq])) elif seq[0] == "NEW_TX_BLOCK": leader.new_tx_block(seq) msg.WaitMsgHandler.new_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: if txid not in processed_message_ids: processed_message_ids.add(txid) leader.message_queue.append(seq) # print(current_port, "tx msg", seq) elif seq[0] == "NEW_MSG_BLOCK": print(current_port, "NEW_MSG_BLOCK") leader.new_msg_block(seq) msg.WaitMsgHandler.new_block(seq) elif seq[0] == "NEW_MSG": msgid = seq[1]["message"]["msgid"] if msgid not in processed_message_ids: processed_message_ids.add(msgid) leader.message_queue.append(seq) forward(seq)