<commit_before>from raiden.storage import wal
from raiden.transfer import node, views
from raiden.transfer.state import NettingChannelState
from raiden.transfer.utils import hash_balance_data
from raiden.utils import typing


def channel_state_until_balance_hash(
        raiden: 'RaidenService',
        token_address: typing.TokenAddress,
        channel_identifier: typing.ChannelID,
        target_balance_hash: bytes,
) -> typing.Optional[NettingChannelState]:
    """ Go through WAL state changes until a certain hash balance is found. """

    # Restore state from the latest snapshot
    snapshot = raiden.wal.storage.get_latest_state_snapshot()
    last_applied_state_change_id, chain_state = snapshot
    unapplied_state_changes = raiden.wal.storage.get_statechanges_by_identifier(
        from_identifier=last_applied_state_change_id,
        to_identifier='latest',
    )
    # Create a copy WAL
    log = wal.wal_from_snapshot(node.state_transition, raiden.wal.storage, chain_state)
    for state_change in unapplied_state_changes:
        log.state_manager.dispatch(state_change)
        channel_state = views.get_channelstate_by_id(
            chain_state=chain_state,
            payment_network_id=raiden.default_registry,
            token_address=token_address,
            channel_id=channel_identifier,
        )
        if not channel_state:
            continue

        partner_latest_balance_proof = channel_state.partner_state.balance_proof
        balance_hash = hash_balance_data(
            transferred_amount=partner_latest_balance_proof.transferred_amount,
            locked_amount=partner_latest_balance_proof.locked_amount,
            locksroot=partner_latest_balance_proof.locksroot,
        )
        if target_balance_hash == balance_hash:
            return channel_state
    return None
<commit_msg>Use current channel state if no snapshots were found<commit_after>from copy import deepcopy

from raiden.storage import wal
from raiden.transfer import node, views
from raiden.transfer.state import NettingChannelState
from raiden.transfer.utils import hash_balance_data
from raiden.utils import typing


def channel_state_until_balance_hash(
        raiden: 'RaidenService',
        token_address: typing.TokenAddress,
        channel_identifier: typing.ChannelID,
        target_balance_hash: bytes,
) -> typing.Optional[NettingChannelState]:
    """ Go through WAL state changes until a certain hash balance is found. """

    # Restore state from the latest snapshot
    snapshot = raiden.wal.storage.get_latest_state_snapshot()
    if not snapshot:
        # No snapshots were taken before this taking place.
        # Therefore, we return a copy of the current channel state
        channel_state = deepcopy(views.get_channelstate_by_id(
            chain_state=views.state_from_raiden(raiden),
            payment_network_id=raiden.default_registry.address,
            token_address=token_address,
            channel_id=channel_identifier,
        ))
        return channel_state

    last_applied_state_change_id, chain_state = snapshot
    unapplied_state_changes = raiden.wal.storage.get_statechanges_by_identifier(
        from_identifier=last_applied_state_change_id,
        to_identifier='latest',
    )

    # Create a copy WAL
    log = wal.wal_from_snapshot(node.state_transition, raiden.wal.storage, chain_state)
    for state_change in unapplied_state_changes:
        log.state_manager.dispatch(state_change)
        channel_state = views.get_channelstate_by_id(
            chain_state=chain_state,
            payment_network_id=raiden.default_registry,
            token_address=token_address,
            channel_id=channel_identifier,
        )
        if not channel_state:
            continue

        partner_latest_balance_proof = channel_state.partner_state.balance_proof
        balance_hash = hash_balance_data(
            transferred_amount=partner_latest_balance_proof.transferred_amount,
            locked_amount=partner_latest_balance_proof.locked_amount,
            locksroot=partner_latest_balance_proof.locksroot,
        )
        if target_balance_hash == balance_hash:
            return channel_state

    return None

{'fraction_non_alphanumeric': 0.056096981221773236, 'fraction_numerical': 0.0, 'mean_word_length': 3.3696780893042577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6686749', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1145, 'n_words': 233}