def test_cancel_hold_with_block_from_blockchain(self):
        block_hash = Sha256Hash(
            helpers.generate_bytearray(crypto.SHA256_HASH_LEN))
        block_message = MockBlockMessage(block_hash)
        connection = MockBlockchainConnection(
            MockSocketConnection(node=self.node,
                                 ip_address=LOCALHOST,
                                 port=8000), self.node)

        self.sut.place_hold(block_hash, self.dummy_connection)
        self.sut.queue_block_for_processing(block_message, connection)
        self.sut.cancel_hold_timeout(block_hash, connection)

        self.assertEqual(0, len(self.sut._holds.contents))
    def test_queue_block_no_hold(self):
        block_hash = Sha256Hash(
            helpers.generate_bytearray(crypto.SHA256_HASH_LEN))
        block_message = MockBlockMessage(block_hash)
        connection = MockBlockchainConnection(
            MockSocketConnection(node=self.node,
                                 ip_address=LOCALHOST,
                                 port=8000), self.node)

        self.sut.queue_block_for_processing(block_message, connection)
        self._assert_block_propagated(block_hash)

        broadcasted_messages = self.node.broadcast_messages
        self.assertEqual(1, len(broadcasted_messages))
        self.assertEqual(BlockHoldingMessage(block_hash, network_num=1),
                         broadcasted_messages[0][0])
    def test_queue_block_holds_exists_until_timeout(self):
        block_hash = Sha256Hash(
            helpers.generate_bytearray(crypto.SHA256_HASH_LEN))
        block_message = MockBlockMessage(block_hash)
        connection = MockBlockchainConnection(
            MockSocketConnection(node=self.node,
                                 ip_address=LOCALHOST,
                                 port=8000), self.node)

        self.sut.place_hold(block_hash, self.dummy_connection)
        self.sut.queue_block_for_processing(block_message, connection)

        self.node.neutrality_service.propagate_block_to_network.assert_not_called(
        )
        time.time = MagicMock(return_value=time.time() +
                              self.sut._compute_hold_timeout(block_message))
        self.node.alarm_queue.fire_alarms()

        self._assert_block_propagated(block_hash)
    def test_queue_block_hold_exists_until_cancelled(self):
        block_hash = Sha256Hash(
            helpers.generate_bytearray(crypto.SHA256_HASH_LEN))
        block_message = MockBlockMessage(block_hash)
        connection = MockBlockchainConnection(
            MockSocketConnection(node=self.node,
                                 ip_address=LOCALHOST,
                                 port=8000), self.node)

        self.sut.place_hold(block_hash, self.dummy_connection)
        alarm_count = len(self.node.alarm_queue.alarms)
        self.sut.queue_block_for_processing(block_message, connection)

        self.node.neutrality_service.propagate_block_to_network.assert_not_called(
        )
        self.assertIn(block_hash, self.sut._holds.contents)
        self.assertEqual(alarm_count + 1, len(self.node.alarm_queue.alarms))

        hold = self.sut._holds.contents[block_hash]
        self.sut.cancel_hold_timeout(block_hash, self.dummy_connection)
        self.assertNotIn(block_hash, self.sut._holds.contents)
        self.assertFalse(hold.alarm.is_active)