Exemplo n.º 1
0
    def hasConsensus(self, reqId: int) -> Optional[str]:
        """
        Accepts a request ID and returns True if consensus was reached
        for the request or else False

        :param reqId: Request ID
        """
        replies = self.getRepliesFromAllNodes(reqId)
        if not replies:
            raise KeyError(reqId)  # NOT_FOUND
        # Check if at least f+1 replies are received or not.
        if self.f + 1 > len(replies):
            return False  # UNCONFIRMED
        else:
            onlyResults = {
                frm: reply["result"]
                for frm, reply in replies.items()
            }
            resultsList = list(onlyResults.values())
            # if all the elements in the resultList are equal - consensus
            # is reached.
            if all(result == resultsList[0] for result in resultsList):
                return resultsList[0]  # CONFIRMED
            else:
                logger.error(
                    "Received a different result from at least one of the nodes.."
                )
                return checkIfMoreThanFSameItems(resultsList, self.f)
Exemplo n.º 2
0
def checkSufficientRepliesReceived(receivedMsgs: Iterable, reqId: int,
                                   fValue: int):
    """
    Checks number of replies for request with specified id in given inbox and
    if this number is lower than number of malicious nodes (fValue) -
    raises exception

    If you do not need response ponder on using
    waitForSufficientRepliesForRequests instead

    :returns: response for request
    """

    receivedReplies = getRepliesFromClientInbox(inbox=receivedMsgs,
                                                reqId=reqId)
    logger.debug("received replies for reqId {}: {}".format(
        reqId, receivedReplies))
    assert len(receivedReplies) > fValue, "Received {} replies but expected " \
                                          "at-least {} for reqId {}". \
        format(len(receivedReplies), fValue + 1, reqId)
    result = checkIfMoreThanFSameItems(
        [reply[f.RESULT.nm] for reply in receivedReplies], fValue)
    assert result, "reqId {}: found less than in {} same replies".format(
        reqId, fValue)

    assert all([r[f.RESULT.nm][f.REQ_ID.nm] == reqId for r in receivedReplies
                ]), "not all replies have got reqId {}".format(reqId)

    return result
Exemplo n.º 3
0
 def isFinalised(self, f):
     if self.finalised is None:
         req = checkIfMoreThanFSameItems([v.__getstate__() for v in
                                          self.propagates.values()], f)
         if req:
             self.finalised = Request.fromState(req)
     return self.finalised
Exemplo n.º 4
0
 def postReplyRecvd(self, reqId, frm, result, numReplies):
     if not self.txnLog.hasTxnWithReqId(reqId) and numReplies > self.f:
         replies = self.reqRepStore.getReplies(reqId).values()
         reply = checkIfMoreThanFSameItems(replies, self.f)
         if reply:
             self.txnLog.append(reqId, reply)
             return reply
Exemplo n.º 5
0
 def isFinalised(self, f):
     if self.finalised is None:
         req = checkIfMoreThanFSameItems([v.__getstate__() for v in
                                          self.propagates.values()], f)
         if req:
             self.finalised = Request.fromState(req)
     return self.finalised
Exemplo n.º 6
0
    def hasConsensus(self, identifier: str, reqId: int) -> Optional[str]:
        """
        Accepts a request ID and returns True if consensus was reached
        for the request or else False

        :param identifier: identifier of the entity making the request
        :param reqId: Request ID
        """
        replies = self.getRepliesFromAllNodes(identifier, reqId)
        if not replies:
            raise KeyError('{}{}'.format(identifier, reqId))  # NOT_FOUND
        # Check if at least f+1 replies are received or not.
        if self.f + 1 > len(replies):
            return False  # UNCONFIRMED
        else:
            onlyResults = {frm: reply["result"] for frm, reply in
                           replies.items()}
            resultsList = list(onlyResults.values())
            # if all the elements in the resultList are equal - consensus
            # is reached.
            if all(result == resultsList[0] for result in resultsList):
                return resultsList[0]  # CONFIRMED
            else:
                logger.error(
                    "Received a different result from at least one of the nodes..")
                return checkIfMoreThanFSameItems(resultsList, self.f)
Exemplo n.º 7
0
 def postReplyRecvd(self, identifier, reqId, frm, result, numReplies):
     if not self.txnLog.hasTxn(identifier, reqId) and numReplies > self.f:
         replies = self.reqRepStore.getReplies(identifier, reqId).values()
         reply = checkIfMoreThanFSameItems(replies, self.f)
         if reply:
             self.txnLog.append(identifier, reqId, reply)
             return reply
Exemplo n.º 8
0
 def hasConsensus(self, reqId: int):
     result = self.store.client.command("select hasConsensus from {} where "
                                        "{} = {}".format(
                                            REQ_DATA, f.REQ_ID.nm, reqId))
     if result and result[0].oRecordData.get('hasConsensus'):
         replies = self.getReplies(reqId).values()
         fVal = getMaxFailures(len(list(replies)))
         return checkIfMoreThanFSameItems(replies, fVal)
     else:
         return False
Exemplo n.º 9
0
def checkSufficientRepliesRecvd(receivedMsgs: Iterable, reqId: int,
                                fValue: int):
    receivedReplies = getRepliesFromClientInbox(receivedMsgs, reqId)
    logger.debug("received replies for reqId {}: {}".
                 format(reqId, receivedReplies))
    assert len(receivedReplies) > fValue, "Received {} replies but expected " \
                                          "at-least {} for reqId {}".\
        format(len(receivedReplies), fValue+1, reqId)
    result = checkIfMoreThanFSameItems([reply[f.RESULT.nm] for reply in
                                        receivedReplies], fValue)
    assert result

    assert all([r[f.RESULT.nm][f.REQ_ID.nm] == reqId for r in receivedReplies])
    return result