<commit_before># Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.

from operator import itemgetter

import configuration as config

import db, logger, transaction
from merkle import sha256


class State:
    """
        state functions
        first iteration - state data stored in leveldb file
        state holds address balances, the transaction nonce and a list of pubhash keys used for each tx - to prevent key reuse.
    """

    def __init__(self):
        self.db = db.DB()  # generate db object here

    def stake_list_get(self):
        try:
            return self.db.get('stake_list')
        except Exception as e:
            logger.exception(e)
            return []

    def stake_list_put(self, sl):
        try:
            self.db.put('stake_list', sl)
        except Exception as e:
            logger.warning("stake_list_put: %s %s", type(e), e.message)
            return False

    def next_stake_list_get(self):
        try:
            return self.db.get('next_stake_list')
        except Exception as e:
            logger.exception(e)
            return []

    def next_stake_list_put(self, next_sl):
        try:
            self.db.put('next_stake_list', next_sl)
        except Exception as e:
            logger.warning("next_stake_list_put: %s %s", type(e), e.message)
            return False

    def put_epoch_seed(self, epoch_seed):
        try:
            self.db.put('epoch_seed', epoch_seed)
        except Exception as e:
            logger.exception(e)
            return False

    def get_epoch_seed(self):
        try:
            return self.db.get('epoch_seed')
        except Exception as e:
            logger.warning("get_epoch_seed: %s %s", type(e), e.message)
            return False

    def state_uptodate(self, height):  # check state db marker to current blockheight.
        if height == self.db.get('blockheight'):
            return True
        return False

    def state_blockheight(self):
        return self.db.get('blockheight')

    def state_get_txn_count(self, addr):
        try:
            return self.db.get('txn_count_' + addr)
        except Exception as e:
            logger.exception(e)
            return 0

    def state_get_address(self, addr):
        try:
            return self.db.get(addr)
        except Exception as e:
            logger.warning("state_get_address: %s %s", type(e), e.message)
            return [0, 0, []]

    def state_address_used(self, addr):  # if excepts then address does not exist..
        try:
            return self.db.get(addr)
        except Exception as e:
            logger.exception(e)
            return False

    def state_balance(self, addr):
        try:
            return self.db.get(addr)[1]
        except Exception as e:
            logger.warning("state_balance: %s %s", type(e), e.message)
            return 0

    def state_nonce(self, addr):
        try:
            return self.db.get(addr)[0]
        except Exception as e:
            logger.exception(e)
            return 0

    def state_pubhash(self, addr):
        try:
            return self.db.get(addr)[2]
        except Exception as e:
            logger.warning("state_pubhash: %s %s", type(e), e.message)
            return []

    def state_hrs(self, hrs):
        try:
            return self.db.get('hrs' + hrs)
        except Exception as e:
            logger.exception(e)
            return False

    def state_validate_tx_pool(self, chain):
        result = True

        for tx in chain.transaction_pool:
            block_chain_buffer = chain.block_chain_buffer
            tx_state = block_chain_buffer.get_stxn_state(blocknumber=block_chain_buffer.height()+1,
                                                         addr=tx.txfrom)
            if tx.state_validate_tx(tx_state=tx_state) is False:
                result = False
                logger.warning('tx %s failed', tx.txhash)
                chain.remove_tx_from_pool(tx)

        return result

    def state_add_block(self, chain, block):
        address_txn = dict()<commit_msg>Removed logger.exception<commit_after># Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.

from operator import itemgetter

import configuration as config

import db, logger, transaction
from merkle import sha256


class State:
    """
        state functions
        first iteration - state data stored in leveldb file
        state holds address balances, the transaction nonce and a list of pubhash keys used for each tx - to prevent key reuse.
    """

    def __init__(self):
        self.db = db.DB()  # generate db object here

    def stake_list_get(self):
        try:
            return self.db.get('stake_list')
        except Exception:
            logger.warning('stake_list empty returning empty list')
            return []

    def stake_list_put(self, sl):
        try:
            self.db.put('stake_list', sl)
        except Exception as e:
            logger.warning("stake_list_put: %s %s", type(e), e.message)
            return False

    def next_stake_list_get(self):
        try:
            return self.db.get('next_stake_list')
        except Exception:
            logger.warning('next_stake_list empty returning empty list')
            return []

    def next_stake_list_put(self, next_sl):
        try:
            self.db.put('next_stake_list', next_sl)
        except Exception as e:
            logger.warning("next_stake_list_put: %s %s", type(e), e.message)
            return False

    def put_epoch_seed(self, epoch_seed):
        try:
            self.db.put('epoch_seed', epoch_seed)
        except Exception as e:
            logger.exception(e)
            return False

    def get_epoch_seed(self):
        try:
            return self.db.get('epoch_seed')
        except Exception as e:
            logger.warning("get_epoch_seed: %s %s", type(e), e.message)
            return False

    def state_uptodate(self, height):  # check state db marker to current blockheight.
        if height == self.db.get('blockheight'):
            return True
        return False

    def state_blockheight(self):
        return self.db.get('blockheight')

    def state_get_txn_count(self, addr):
        try:
            return self.db.get('txn_count_' + addr)
        except Exception:
            logger.warning('No txn count for %s', addr)
            return 0

    def state_get_address(self, addr):
        try:
            return self.db.get(addr)
        except Exception:
            logger.warning('state_get_address: No state found for %s', addr)
            return [0, 0, []]

    def state_address_used(self, addr):  # if excepts then address does not exist..
        try:
            return self.db.get(addr)
        except Exception:
            logger.warning('state_address_used: address not found %s', addr)
            return False

    def state_balance(self, addr):
        try:
            return self.db.get(addr)[1]
        except Exception:
            logger.warning("state_balance: state not found for %s", addr)
            return 0

    def state_nonce(self, addr):
        try:
            return self.db.get(addr)[0]
        except Exception:
            logger.warning("state_nonce: state not found for %s", addr)
            return 0

    def state_pubhash(self, addr):
        try:
            return self.db.get(addr)[2]
        except Exception:
            logger.warning("state_pubhash: state not found for %s", addr)
            return []

    def state_hrs(self, hrs):
        try:
            return self.db.get('hrs' + hrs)
        except Exception:
            logger.warning("state_hrs: state not found for %s", hrs)
            return False

    def state_validate_tx_pool(self, chain):
        result = True

        for tx in chain.transaction_pool:
            block_chain_buffer = chain.block_chain_buffer
            tx_state = block_chain_buffer.get_stxn_state(blocknumber=block_chain_buffer.height()+1,
                                                         addr=tx.txfrom)
            if tx.state_validate_tx(tx_state=tx_state) is False:
                result = False
                logger.warning('tx %s failed', tx.txhash)
                chain.remove_tx_from_pool(tx)

        return result

    def state_add_block(self, chain, block):
        address_txn = dict()
{'fraction_non_alphanumeric': 0.0896243080909198, 'fraction_numerical': 0.0028265221999764457, 'mean_word_length': 1.9262577532736045, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6391487', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2380, 'n_words': 662}