<commit_before>from typing import Optional

from eth_utils import decode_hex
from web3.utils.filters import Filter

from raiden.constants import UINT256_MAX
from raiden.network.proxies.token_network import ChannelDetails, TokenNetwork
from raiden.utils.filters import decode_event, get_filter_args_for_specific_event_from_channel
from raiden.utils.typing import (
    AdditionalHash,
    Address,
    BalanceHash,
    BlockSpecification,
    ChannelID,
    Locksroot,
    MerkleTreeLeaves,
    Nonce,
    Signature,
    TokenAmount,
)
from raiden_contracts.constants import CONTRACT_TOKEN_NETWORK, ChannelEvent
from raiden_contracts.contract_manager import ContractManager


class PaymentChannel:
    def __init__(
            self,
            token_network: TokenNetwork,
            channel_identifier: ChannelID,
            contract_manager: ContractManager,
    ):

        self.contract_manager = contract_manager
        if channel_identifier < 0 or channel_identifier > UINT256_MAX:
            raise ValueError('channel_identifier {} is not a uint256'.format(channel_identifier))

        filter_args = get_filter_args_for_specific_event_from_channel(
            token_network_address=token_network.address,
            channel_identifier=channel_identifier,
            event_name=ChannelEvent.OPENED,
            contract_manager=self.contract_manager,
        )

        events = token_network.proxy.contract.web3.eth.getLogs(filter_args)
        if not len(events) > 0:
            raise ValueError('Channel is non-existing.')

        event = decode_event(
            self.contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK),
            events[-1],
        )
        participant1 = Address(decode_hex(event['args']['participant1']))
        participant2 = Address(decode_hex(event['args']['participant2']))

        if token_network.node_address not in (participant1, participant2):
            raise ValueError('One participant must be the node address')

        if token_network.node_address == participant2:
            participant1, participant2 = participant2, participant1

        self.channel_identifier = channel_identifier
        self.participant1 = participant1
        self.participant2 = participant2
        self.token_network = token_network
        self.client = self.token_network.client

    def token_address(self) -> Address:
        """ Returns the address of the token for the channel. """
        return self.token_network.token_address()
<commit_msg>cosmetic changes to payment channel<commit_after>from typing import Optional

from eth_utils import decode_hex
from web3.utils.filters import Filter

from raiden.constants import GENESIS_BLOCK_NUMBER, UINT256_MAX
from raiden.network.proxies.token_network import ChannelDetails, TokenNetwork
from raiden.utils.filters import decode_event, get_filter_args_for_specific_event_from_channel
from raiden.utils.typing import (
    AdditionalHash,
    Address,
    BalanceHash,
    BlockSpecification,
    ChannelID,
    Locksroot,
    MerkleTreeLeaves,
    Nonce,
    Signature,
    TokenAmount,
)
from raiden_contracts.constants import CONTRACT_TOKEN_NETWORK, ChannelEvent
from raiden_contracts.contract_manager import ContractManager


class PaymentChannel:
    def __init__(
            self,
            token_network: TokenNetwork,
            channel_identifier: ChannelID,
            contract_manager: ContractManager,
    ):
        if channel_identifier <= 0 or channel_identifier > UINT256_MAX:
            raise ValueError(f'channel_identifier {channel_identifier} is not a uint256')

        # FIXME: Issue #3958
        from_block = GENESIS_BLOCK_NUMBER

        # For this check it is perfectly fine to use a `latest` block number.
        # If the channel happened to be closed, even if the chanel is already
        # closed/settled.
        to_block = 'latest'

        filter_args = get_filter_args_for_specific_event_from_channel(
            token_network_address=token_network.address,
            channel_identifier=channel_identifier,
            event_name=ChannelEvent.OPENED,
            contract_manager=contract_manager,
            from_block=from_block,
            to_block=to_block,
        )

        events = token_network.proxy.contract.web3.eth.getLogs(filter_args)
        if not len(events) > 0:
            raise ValueError('Channel is non-existing.')

        event = decode_event(
            contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK),
            events[-1],
        )
        participant1 = Address(decode_hex(event['args']['participant1']))
        participant2 = Address(decode_hex(event['args']['participant2']))

        if token_network.node_address not in (participant1, participant2):
            raise ValueError('One participant must be the node address')

        if token_network.node_address == participant2:
            participant1, participant2 = participant2, participant1

        self.channel_identifier = channel_identifier
        self.participant1 = participant1
        self.participant2 = participant2
        self.token_network = token_network
        self.client = token_network.client
        self.contract_manager = contract_manager

    def token_address(self) -> Address:
        """ Returns the address of the token for the channel. """
        return self.token_network.token_address()

{'fraction_non_alphanumeric': 0.0659217877094972, 'fraction_numerical': 0.01154562383612663, 'mean_word_length': 3.1251920122887866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '4811614', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1409, 'n_words': 326}