def test_cmc(state, settle_timeout, netting_channel_abi, manager, events):  # pylint: disable=too-many-locals,too-many-statements
    address1 = sha3('address1')[:20]
    address3 = sha3('address3')[:20]
    inexisting_address = sha3('this_does_not_exist')[:20]

    netting_channel_translator = ContractTranslator(netting_channel_abi)

    assert len(manager.getChannelsParticipants()) == 0

    netting_channel_address1_hex = manager.newChannel(
        address1,
        settle_timeout,
    )

    # cannot have two channels at the same time
    with pytest.raises(TransactionFailed):
        manager.newChannel(address1, settle_timeout)

    # should trow if there is no channel for the given address
    with pytest.raises(TransactionFailed):
        manager.getChannelWith(inexisting_address)

    assert len(manager.getChannelsParticipants()) == 2

    netting_contract_proxy1 = ABIContract(
        state,
        netting_channel_translator,
        netting_channel_address1_hex,
    )

    assert netting_contract_proxy1.settleTimeout() == settle_timeout

    netting_channel_address2_hex = manager.newChannel(
        address3,
        settle_timeout,
    )

    assert manager.getChannelWith(address1) == netting_channel_address1_hex
    assert manager.getChannelWith(address3) == netting_channel_address2_hex

    msg_sender_channels = manager.nettingContractsByAddress(tester.DEFAULT_ACCOUNT)
    address1_channels = manager.nettingContractsByAddress(address1)
    inexisting_channels = manager.nettingContractsByAddress(inexisting_address)

    assert len(msg_sender_channels) == 2
    assert len(address1_channels) == 1
    assert len(inexisting_channels) == 0

    assert len(manager.getChannelsParticipants()) == 4

    assert len(events) == 2
    assert events[0]['_event_type'] == 'ChannelNew'
    assert events[0]['participant1'] == tester.a0.encode('hex')
    assert events[0]['participant2'] == address1.encode('hex')
    assert events[0]['nettingChannel'] == netting_channel_address1_hex
    assert events[0]['settleTimeout'] == 30
    assert events[1]['_event_type'] == 'ChannelNew'
    assert events[1]['participant1'] == tester.a0.encode('hex')
    assert events[1]['participant2'] == address3.encode('hex')
    assert events[1]['nettingChannel'] == netting_channel_address2_hex
    assert events[1]['settleTimeout'] == 30
def test_cmc(state, settle_timeout, netting_channel_abi, manager, events):  # pylint: disable=too-many-locals,too-many-statements
    address1 = sha3('address1')[:20]
    address3 = sha3('address3')[:20]
    inexisting_address = sha3('this_does_not_exist')[:20]

    netting_channel_translator = ContractTranslator(netting_channel_abi)

    assert len(manager.getChannelsParticipants()) == 0

    netting_channel_address1_hex = manager.newChannel(
        address1,
        settle_timeout,
    )

    # cannot have two channels at the same time
    with pytest.raises(TransactionFailed):
        manager.newChannel(address1, settle_timeout)

    # should trow if there is no channel for the given address
    with pytest.raises(TransactionFailed):
        manager.getChannelWith(inexisting_address)

    assert len(manager.getChannelsParticipants()) == 2

    netting_contract_proxy1 = ABIContract(
        state,
        netting_channel_translator,
        netting_channel_address1_hex,
    )

    assert netting_contract_proxy1.settleTimeout() == settle_timeout

    netting_channel_address2_hex = manager.newChannel(
        address3,
        settle_timeout,
    )

    assert manager.getChannelWith(address1) == netting_channel_address1_hex
    assert manager.getChannelWith(address3) == netting_channel_address2_hex

    msg_sender_channels = manager.nettingContractsByAddress(
        tester.DEFAULT_ACCOUNT)
    address1_channels = manager.nettingContractsByAddress(address1)
    inexisting_channels = manager.nettingContractsByAddress(inexisting_address)

    assert len(msg_sender_channels) == 2
    assert len(address1_channels) == 1
    assert len(inexisting_channels) == 0

    assert len(manager.getChannelsParticipants()) == 4

    assert len(events) == 2
    assert events[0]['_event_type'] == 'ChannelNew'
    assert events[0]['participant1'] == tester.a0.encode('hex')
    assert events[0]['participant2'] == address1.encode('hex')
    assert events[0]['nettingChannel'] == netting_channel_address1_hex
    assert events[0]['settleTimeout'] == 30
    assert events[1]['_event_type'] == 'ChannelNew'
    assert events[1]['participant1'] == tester.a0.encode('hex')
    assert events[1]['participant2'] == address3.encode('hex')
    assert events[1]['nettingChannel'] == netting_channel_address2_hex
    assert events[1]['settleTimeout'] == 30
def test_channelmanager(tester_state, tester_token, tester_events,
                        tester_channelmanager_library_address, settle_timeout,
                        netting_channel_abi):  # pylint: disable=too-many-locals,too-many-statements

    address0 = tester.DEFAULT_ACCOUNT
    address1 = tester.a1
    address2 = tester.a2
    nonexisting_address = sha3('this_does_not_exist')[:20]

    channelmanager_path = get_contract_path('ChannelManagerContract.sol')
    channel_manager = tester_state.abi_contract(
        None,
        path=channelmanager_path,
        language='solidity',
        constructor_parameters=[tester_token.address],
        contract_name='ChannelManagerContract',
        log_listener=tester_events.append,
        libraries={
            'ChannelManagerLibrary':
            tester_channelmanager_library_address.encode('hex'),
        })

    participants_count = len(channel_manager.getChannelsParticipants())
    assert participants_count == 0, 'newly deployed contract must be empty'

    netting_channel_translator = ContractTranslator(netting_channel_abi)

    previous_events = list(tester_events)
    netting_channel_address1_hex = channel_manager.newChannel(
        address1,
        settle_timeout,
    )
    assert len(previous_events) + 1 == len(
        tester_events), 'ChannelNew event must be fired.'

    channelnew_event = tester_events[-1]
    assert channelnew_event == {
        '_event_type': 'ChannelNew',
        'participant1': address0.encode('hex'),
        'participant2': address1.encode('hex'),
        'netting_channel': netting_channel_address1_hex,
        'settle_timeout': settle_timeout,
    }

    # should fail if settleTimeout is too low
    with pytest.raises(TransactionFailed):
        channel_manager.newChannel(address1, 5)

    # cannot have two channels at the same time
    with pytest.raises(TransactionFailed):
        channel_manager.newChannel(address1, settle_timeout)

    # should be zero address if there is no channel for the given address
    assert channel_manager.getChannelWith(nonexisting_address) == '0' * 40

    assert len(channel_manager.getChannelsParticipants()) == 2

    netting_contract_proxy1 = ABIContract(
        tester_state,
        netting_channel_translator,
        netting_channel_address1_hex,
    )

    assert netting_contract_proxy1.settleTimeout() == settle_timeout

    previous_events = list(tester_events)
    netting_channel_address2_hex = channel_manager.newChannel(
        address2,
        settle_timeout,
    )
    assert len(previous_events) + 1 == len(
        tester_events), 'ChannelNew event must be fired.'

    assert channel_manager.getChannelWith(
        address1) == netting_channel_address1_hex
    assert channel_manager.getChannelWith(
        address2) == netting_channel_address2_hex

    msg_sender_channels = channel_manager.nettingContractsByAddress(
        tester.DEFAULT_ACCOUNT)
    address1_channels = channel_manager.nettingContractsByAddress(address1)
    nonexisting_channels = channel_manager.nettingContractsByAddress(
        nonexisting_address)

    assert len(msg_sender_channels) == 2
    assert len(address1_channels) == 1
    assert len(nonexisting_channels) == 0

    assert len(channel_manager.getChannelsParticipants()) == 4

    channelnew_event = tester_events[-1]
    assert channelnew_event == {
        '_event_type': 'ChannelNew',
        'participant1': address0.encode('hex'),
        'participant2': address2.encode('hex'),
        'netting_channel': netting_channel_address2_hex,
        'settle_timeout': settle_timeout,
    }
def test_channelmanager(
    tester_state,
    tester_token,
    tester_events,
    tester_channelmanager_library_address,
    settle_timeout,
    netting_channel_abi,
):
    # pylint: disable=too-many-locals,too-many-statements

    address0 = tester.DEFAULT_ACCOUNT
    address1 = tester.a1
    address2 = tester.a2
    inexisting_address = sha3("this_does_not_exist")[:20]

    channelmanager_path = get_contract_path("ChannelManagerContract.sol")
    channel_manager = tester_state.abi_contract(
        None,
        path=channelmanager_path,
        language="solidity",
        constructor_parameters=[tester_token.address],
        contract_name="ChannelManagerContract",
        log_listener=tester_events.append,
        libraries={"ChannelManagerLibrary": tester_channelmanager_library_address.encode("hex")},
    )

    participants_count = len(channel_manager.getChannelsParticipants())
    assert participants_count == 0, "newly deployed contract must be empty"

    netting_channel_translator = ContractTranslator(netting_channel_abi)

    previous_events = list(tester_events)
    netting_channel_address1_hex = channel_manager.newChannel(address1, settle_timeout)
    assert len(previous_events) + 1 == len(tester_events), "ChannelNew event must be fired."

    channelnew_event = tester_events[-1]
    assert channelnew_event == {
        "_event_type": "ChannelNew",
        "participant1": address0.encode("hex"),
        "participant2": address1.encode("hex"),
        "netting_channel": netting_channel_address1_hex,
        "settle_timeout": settle_timeout,
    }

    # should fail if settleTimeout is too low
    with pytest.raises(TransactionFailed):
        channel_manager.newChannel(address1, 5)

    # cannot have two channels at the same time
    with pytest.raises(TransactionFailed):
        channel_manager.newChannel(address1, settle_timeout)

    # should trow if there is no channel for the given address
    with pytest.raises(TransactionFailed):
        channel_manager.getChannelWith(inexisting_address)

    assert len(channel_manager.getChannelsParticipants()) == 2

    netting_contract_proxy1 = ABIContract(tester_state, netting_channel_translator, netting_channel_address1_hex)

    assert netting_contract_proxy1.settleTimeout() == settle_timeout

    previous_events = list(tester_events)
    netting_channel_address2_hex = channel_manager.newChannel(address2, settle_timeout)
    assert len(previous_events) + 1 == len(tester_events), "ChannelNew event must be fired."

    assert channel_manager.getChannelWith(address1) == netting_channel_address1_hex
    assert channel_manager.getChannelWith(address2) == netting_channel_address2_hex

    msg_sender_channels = channel_manager.nettingContractsByAddress(tester.DEFAULT_ACCOUNT)
    address1_channels = channel_manager.nettingContractsByAddress(address1)
    inexisting_channels = channel_manager.nettingContractsByAddress(inexisting_address)

    assert len(msg_sender_channels) == 2
    assert len(address1_channels) == 1
    assert len(inexisting_channels) == 0

    assert len(channel_manager.getChannelsParticipants()) == 4

    channelnew_event = tester_events[-1]
    assert channelnew_event == {
        "_event_type": "ChannelNew",
        "participant1": address0.encode("hex"),
        "participant2": address2.encode("hex"),
        "netting_channel": netting_channel_address2_hex,
        "settle_timeout": settle_timeout,
    }