<commit_before># coding=utf-8
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
import copy
import threading
from typing import Optional

from pyqrllib.pyqrllib import bin2hstr, hstr2bin
from pyqryptonight.pyqryptonight import Qryptominer, UInt256ToString, SOLUTION

from qrl.core import config
from qrl.core.AddressState import AddressState
from qrl.core.Block import Block
from qrl.core.DifficultyTracker import DifficultyTracker
from qrl.core.TransactionPool import TransactionPool
from qrl.core.misc import logger
from qrl.core.txs.Transaction import Transaction


class Miner(Qryptominer):
    def __init__(self,
                 pre_block_logic,
                 mining_address: bytes,
                 chain_manager,
                 mining_thread_count,
                 add_unprocessed_txn_fn):
        super().__init__()
        self.pre_block_logic = pre_block_logic  # FIXME: Circular dependency with node.py

        self._mining_block = None
        self._current_difficulty = None
        self._current_target = None
        self._measurement = None  # Required only for logging

        self._mining_address = mining_address
        self._reward_address = None
        self._chain_manager = chain_manager
        self._add_unprocessed_txn_fn = add_unprocessed_txn_fn
        self._mining_thread_count = mining_thread_count
        self._dummy_xmss = None
        self.setForcedSleep(config.user.mining_pause)

        self.lock = threading.RLock()

    def prepare_next_unmined_block_template(self, mining_address, tx_pool, parent_block: Block, parent_difficulty):
        try:
            with self.lock:
                logger.debug('Miner-Locked - prepare_next_unmined_block_template')

                logger.debug('Miner-TryCancel - prepare_next_unmined_block_template')
                self.cancel()
                logger.debug('Miner-Cancel - prepare_next_unmined_block_template')

                self._mining_block = self.create_block(last_block=parent_block,
                                                       mining_nonce=0,
                                                       tx_pool=tx_pool,
                                                       miner_address=mining_address)

                parent_metadata = self._chain_manager.get_block_metadata(parent_block.headerhash)
                self._measurement = self._chain_manager.get_measurement(self._mining_block.timestamp,
                                                                        self._mining_block.prev_headerhash,
                                                                        parent_metadata)<commit_msg>Added debug log in prepare_next_unmined_block_template<commit_after># coding=utf-8
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
import copy
import threading
from typing import Optional

from pyqrllib.pyqrllib import bin2hstr, hstr2bin
from pyqryptonight.pyqryptonight import Qryptominer, UInt256ToString, SOLUTION

from qrl.core import config
from qrl.core.AddressState import AddressState
from qrl.core.Block import Block
from qrl.core.DifficultyTracker import DifficultyTracker
from qrl.core.TransactionPool import TransactionPool
from qrl.core.misc import logger
from qrl.core.txs.Transaction import Transaction


class Miner(Qryptominer):
    def __init__(self,
                 pre_block_logic,
                 mining_address: bytes,
                 chain_manager,
                 mining_thread_count,
                 add_unprocessed_txn_fn):
        super().__init__()
        self.pre_block_logic = pre_block_logic  # FIXME: Circular dependency with node.py

        self._mining_block = None
        self._current_difficulty = None
        self._current_target = None
        self._measurement = None  # Required only for logging

        self._mining_address = mining_address
        self._reward_address = None
        self._chain_manager = chain_manager
        self._add_unprocessed_txn_fn = add_unprocessed_txn_fn
        self._mining_thread_count = mining_thread_count
        self._dummy_xmss = None
        self.setForcedSleep(config.user.mining_pause)

        self.lock = threading.RLock()

    def prepare_next_unmined_block_template(self, mining_address, tx_pool, parent_block: Block, parent_difficulty):
        try:
            logger.debug('Miner-Try - prepare_next_unmined_block_template')
            with self.lock:
                logger.debug('Miner-Locked - prepare_next_unmined_block_template')

                logger.debug('Miner-TryCancel - prepare_next_unmined_block_template')
                self.cancel()
                logger.debug('Miner-Cancel - prepare_next_unmined_block_template')

                self._mining_block = self.create_block(last_block=parent_block,
                                                       mining_nonce=0,
                                                       tx_pool=tx_pool,
                                                       miner_address=mining_address)

                parent_metadata = self._chain_manager.get_block_metadata(parent_block.headerhash)
                self._measurement = self._chain_manager.get_measurement(self._mining_block.timestamp,
                                                                        self._mining_block.prev_headerhash,
                                                                        parent_metadata)
{'fraction_non_alphanumeric': 0.057612425501173924, 'fraction_numerical': 0.0025284450063211127, 'mean_word_length': 2.099048684946838, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2092569', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1394, 'n_words': 275}