def handle(self, connection_id, message_content):
        obj, tag, ttl = message_content

        exclude = [connection_id]

        ttl -= 1

        if ttl <= 0:
            # Do not forward message if it has reached its time to live limit
            return HandlerResult(status=HandlerStatus.PASS)

        if tag == GossipMessage.BATCH:
            # If we already have this batch, don't forward it
            if not self._completer.get_batch(obj.header_signature):
                self._gossip.broadcast_batch(obj, exclude, time_to_live=ttl)
        elif tag == GossipMessage.BLOCK:
            # If we already have this block, don't forward it
            if not self._completer.get_block(obj.header_signature):
                self._gossip.broadcast_block(obj, exclude, time_to_live=ttl)
        elif tag == GossipMessage.CONSENSUS:
            peer_message = ConsensusPeerMessage()
            peer_message.ParseFromString(obj.message)

            self._notifier.notify_peer_message(
                message=peer_message,
                sender_id=bytes.fromhex(
                    self._gossip.peer_to_public_key(connection_id)))
        else:
            LOGGER.info("received %s, not BATCH or BLOCK or CONSENSUS", tag)
        return HandlerResult(status=HandlerStatus.PASS)
Ejemplo n.º 2
0
    def handle(self, connection_id, message_content):
        gossip_message = GossipConsensusMessage()
        gossip_message.ParseFromString(message_content)

        peer_message = ConsensusPeerMessage()
        peer_message.ParseFromString(gossip_message.message)

        self._notifier.notify_peer_message(message=peer_message,
                                           sender_id=gossip_message.sender_id)

        return HandlerResult(status=HandlerStatus.PASS)
Ejemplo n.º 3
0
    def broadcast2cluster(self, message):
        # ARBITER DONE = send to cluster
        peer_message = ConsensusPeerMessage()
        peer_message.ParseFromString(message)
        payload = PbftMessage()
        payload.ParseFromString(peer_message.content)
        block_id = payload.block.block_id.hex()
        LOGGER.debug(
            "broadcast2cluster '%s' ask %s from arbiters for block=%s",
            peer_message.name, peer_message.message_type, block_id[:8])
        if peer_message.message_type == 'ArbitrationDone':
            try:
                block = next(self._block_manager.get([block_id]))
                LOGGER.debug("ARBITRATION DONE: SEND BLOCK=%s TO OWN CLUSTER",
                             block.header_signature[:8])
                self._block_publisher.arbitrate_block(block, False)
            except StopIteration:
                LOGGER.debug("ARBITRATION: for UNDEFINED block=%s",
                             block_id[:8])

        self._gossip.broadcast_cluster_consensus_message(
            message=message, public_key=self._public_key)
Ejemplo n.º 4
0
    def send_to(self, peer_id, message):
        """
        send to peer consensus message 
        it could be Arbitration - in than case we can send block to peer too
        we can see on this code as expanded consensus API
        """
        #LOGGER.debug("ConsensusProxy:send_to peer=%s message=%s",peer_id.hex()[:8],message)
        peer_message = ConsensusPeerMessage()
        peer_message.ParseFromString(message)
        LOGGER.debug("ConsensusProxy:send_to peer=%s", peer_id.hex()[:8])
        if peer_message.message_type == 'ArbitrationDone':
            """
            inform peer about this block
            """
            payload = PbftMessage()
            payload.ParseFromString(peer_message.content)
            block_id = payload.block.block_id.hex()
            LOGGER.debug("Consensus '%s' ask %s from peer=%s for block=%s",
                         peer_message.name, peer_message.message_type,
                         peer_id.hex()[:8], block_id[:8])

        self._gossip.send_consensus_message(peer_id=peer_id.hex(),
                                            message=message,
                                            public_key=self._public_key)
Ejemplo n.º 5
0
    def broadcast2arbiter(self, message):
        peer_message = ConsensusPeerMessage()
        peer_message.ParseFromString(message)
        """
        inform peer about this block
        """
        payload = PbftMessage()
        payload.ParseFromString(peer_message.content)
        block_id = payload.block.block_id.hex()
        LOGGER.debug(
            "broadcast2arbiter '%s' ask %s from arbiters for block=%s",
            peer_message.name, peer_message.message_type, block_id[:8])
        if peer_message.message_type == 'Arbitration':
            try:
                block = next(self._block_manager.get([block_id]))
                LOGGER.debug("ARBITRATION:contains in block manager ID=%s",
                             block.header_signature[:8])
                self._block_publisher.arbitrate_block(block)
            except StopIteration:
                LOGGER.debug("ARBITRATION: for UNDEFINED block=%s",
                             block_id[:8])

        self._gossip.broadcast_arbiter_consensus_message(
            message=message, public_key=self._public_key)
Ejemplo n.º 6
0
    def _wrap_consensus_message(self, content, message_type, connection_id):
        _, name, version, _ = self._consensus_registry.get_active_engine_info()
        header = ConsensusPeerMessageHeader(
            signer_id=self._public_key,
            content_sha512=hashlib.sha512(content).digest(),
            message_type=message_type,
            name=name,
            version=version,
        ).SerializeToString()

        signature = bytes.fromhex(self._identity_signer.sign(header))
        message = ConsensusPeerMessage(header=header,
                                       content=content,
                                       header_signature=signature)

        return message
Ejemplo n.º 7
0
def gossip_message_preprocessor(message_content_bytes):
    gossip_message = GossipMessage()
    gossip_message.ParseFromString(message_content_bytes)

    tag = gossip_message.content_type

    if tag == GossipMessage.BLOCK:
        obj = Block()
        obj.ParseFromString(gossip_message.content)
    elif tag == GossipMessage.BATCH:
        obj = Batch()
        obj.ParseFromString(gossip_message.content)
    elif tag == GossipMessage.CONSENSUS:
        obj = ConsensusPeerMessage()
        obj.ParseFromString(gossip_message.content)

    content = obj, tag, gossip_message.time_to_live

    return PreprocessorResult(content=content)
Ejemplo n.º 8
0
    def _create_consensus_message(self, valid=True):
        name, version = "test", "1.0"
        content = b"123"
        message_type = "test"
        header_bytes = ConsensusPeerMessageHeader(
            signer_id=bytes.fromhex(self.public_key),
            content_sha512=hashlib.sha512(content).digest(),
            message_type=message_type,
            name=name,
            version=version,
        ).SerializeToString()

        if valid:
            signature = bytes.fromhex(self.signer.sign(header_bytes))
        else:
            signature = b"bad_signature"

        message = ConsensusPeerMessage(header=header_bytes,
                                       content=content,
                                       header_signature=signature)

        return message