def validateNodeMsg(self, wrappedMsg): """ Validate another node's message sent to this node. :param wrappedMsg: Tuple of message and the name of the node that sent the message :return: Tuple of message from node and name of the node """ msg, frm = wrappedMsg if self.isNodeBlacklisted(frm): self.discard(msg, "received from blacklisted node {}" .format(frm), logger.info) return None op = msg.pop(OP_FIELD_NAME, None) if not op: raise MissingNodeOp cls = TaggedTuples.get(op, None) if not cls: raise InvalidNodeOp(op) try: cMsg = cls(**msg) except Exception as ex: raise InvalidNodeMsg from ex try: self.verifySignature(cMsg) except Exception as ex: raise SuspiciousNode from ex logger.debug("{} received node message from {}: {}". format(self, frm, cMsg), extra={"cli": False}) return cMsg, frm
def validateClientMsg(self, wrappedMsg): """ Validate a message sent by a client. :param wrappedMsg: a message from a client :return: Tuple of clientMessage and client address """ msg, frm = wrappedMsg if self.isClientBlacklisted(frm): self.discard(msg, "received from blacklisted client {}" .format(frm), logger.info) return None if all(attr in msg.keys() for attr in [OPERATION, 'clientId', 'reqId']): cls = Request elif OP_FIELD_NAME in msg: op = msg.pop(OP_FIELD_NAME) cls = TaggedTuples.get(op, None) if not cls: raise InvalidClientOp(op) if cls is not Batch: raise InvalidClientMsgType(cls) else: raise InvalidClientRequest # don't check for signature on Batches from clients, signatures will # be checked on the individual messages when they are unpacked try: cMsg = cls(**msg) except Exception as ex: raise InvalidClientRequest from ex try: self.verifySignature(cMsg) except Exception as ex: raise SuspiciousClient from ex logger.trace("{} received CLIENT message: {}". format(self.clientstack.name, cMsg)) return cMsg, frm