Пример #1
0
    def test_receive_paxos_message_try_ok_3(self):
        # try_ok message with no prop/supp block stored locally and message does contain a propose block
        try_ok = PaxosMessage('TRY_OK', 1)

        b = Block(1, GENESIS.block_id, ['a'], 1)
        b.depth = 1

        try_ok.supp_block = b.block_id
        try_ok.prop_block = b.block_id

        self.node.c_request_seq = 1
        self.node.c_new_block = b
        self.node.c_votes = 5

        self.node.broadcast = MagicMock()
        self.node.blocktree.nodes.update({b.block_id: b})
        self.node.receive_paxos_message(try_ok, None)

        assert self.node.broadcast.called
        assert self.node.c_prop_block == b

        obj = self.node.broadcast.call_args[0][0]
        assert obj.com_block == b.block_id
Пример #2
0
    def receive_paxos_message(self, message, sender):
        """React on a received paxos `message`. This method implements the main functionality of the paxos algorithm.

        Args:
            message (PaxosMessage): Message received.
            sender (Connection): Connection instance of the sender (None if sender is this Node).
        """
        logger.debug('receive message type = %s', message.msg_type)
        if message.msg_type == 'TRY':
            # make sure last commited block of sender is also committed by this node
            if message.last_committed_block not in self.blocktree.committed_blocks:
                last_committed_block = self.get_block(
                    message.last_committed_block)
                if last_committed_block is None:
                    return
                self.commit(last_committed_block)

            # make sure that message.new_block is descendant of last committed block
            new_block = self.get_block(message.new_block)
            if new_block is None:
                return
            if not self.reach_genesis_block(new_block):
                # first need to request some missing blocks to be able to decide
                return

            if not self.blocktree.ancestor(self.blocktree.committed_block,
                                           new_block):
                # new_block is not a descendent of last committed block thus we reject it
                return

            if self.s_max_block_depth < new_block.depth:
                self.s_max_block_depth = new_block.depth

                # write changes to disk (add s_max_block_depth)
                self.blocktree.db.put(b's_max_block_depth',
                                      str(self.s_max_block_depth).encode())

                # create a TRY_OK message
                try_ok = PaxosMessage('TRY_OK', message.request_seq)
                if self.s_prop_block is not None:
                    try_ok.prop_block = self.s_prop_block.block_id
                if self.s_supp_block is not None:
                    try_ok.supp_block = self.s_supp_block.block_id
                if sender is not None:
                    self.respond(try_ok, sender)
                else:
                    self.receive_paxos_message(try_ok, None)

        elif message.msg_type == 'TRY_OK':
            # check if message is not outdated
            if message.request_seq != self.c_request_seq:
                # outdated message
                logger.debug('TRY_OK outdated')
                return

            # if TRY_OK message contains a propose block, we will support it if it is the first received
            # or if its support block is deeper than the one already stored.
            supp_block = self.get_block(message.supp_block)
            prop_block = self.get_block(message.prop_block)

            if supp_block and self.c_supp_block is None:
                self.c_supp_block = supp_block
                self.c_prop_block = prop_block
            elif supp_block and self.c_supp_block and self.c_supp_block < supp_block:
                self.c_supp_block = supp_block
                self.c_prop_block = prop_block

            self.c_votes += 1
            if self.c_votes > self.n / 2:

                # start new round
                self.c_votes = 0
                self.c_request_seq += 1

                # the compromise block will be the block we are going to propose in the end
                self.c_com_block = self.c_new_block

                # check if we need to support another block instead of the new block
                if self.c_prop_block:
                    self.c_com_block = self.c_prop_block

                # create PROPOSE message
                propose = PaxosMessage('PROPOSE', self.c_request_seq)
                propose.com_block = self.c_com_block.block_id
                propose.new_block = self.c_new_block.block_id

                self.broadcast(propose, 'PROPOSE')
                self.receive_paxos_message(propose, None)

        elif message.msg_type == 'PROPOSE':
            # if did not receive a try message with a deeper new block in mean time can store proposed block on server
            new_block = self.get_block(message.new_block)
            if new_block is None:
                return
            com_block = self.get_block(message.com_block)
            if com_block is None:
                return
            if new_block.depth == self.s_max_block_depth:
                self.s_prop_block = com_block
                self.s_supp_block = new_block

                # write changes to disk (add s_prop_block and s_supp_block)
                if self.s_prop_block is not None:
                    block_id_bytes = str(self.s_prop_block.block_id).encode()
                    self.blocktree.db.put(b's_prop_block', block_id_bytes)

                if self.s_supp_block is not None:
                    block_id_bytes = str(self.s_supp_block.block_id).encode()
                    self.blocktree.db.put(b's_supp_block', block_id_bytes)

                # create a PROPOSE_ACK message
                propose_ack = PaxosMessage('PROPOSE_ACK', message.request_seq)
                propose_ack.com_block = message.com_block

                if sender is not None:
                    self.respond(propose_ack, sender)
                else:
                    self.receive_paxos_message(propose_ack, None)

        elif message.msg_type == 'PROPOSE_ACK':
            # check if message is not outdated
            if message.request_seq != self.c_request_seq:
                # outdated message
                return

            self.c_votes += 1
            if self.c_votes > self.n / 2:
                # ignore further answers
                self.c_request_seq += 1

                # create commit message
                com_block = self.get_block(message.com_block)
                if com_block is None:
                    return
                commit = PaxosMessage('COMMIT', self.c_request_seq)
                commit.com_block = message.com_block
                self.broadcast(commit, 'COMMIT')
                self.commit(com_block)

                # allow new paxos instance
                self.c_commit_running = False
                self.c_quick_proposing = True

        elif message.msg_type == 'COMMIT':
            com_block = self.get_block(message.com_block)
            if com_block is None:
                return
            self.commit(com_block)