Ejemplo n.º 1
0
def test_api_channel_events(raiden_chain, token_addresses, deposit, events_poll_timeout):
    app0, app1 = raiden_chain
    token_address = token_addresses[0]
    registry_address = app0.raiden.default_registry.address

    # Without blockchain_cache we need to wait for the events to be processed
    waiting.wait_for_newbalance(
        app0.raiden,
        registry_address,
        token_address,
        app1.raiden.address,
        deposit,
        events_poll_timeout,
    )
    waiting.wait_for_newbalance(
        app1.raiden,
        registry_address,
        token_address,
        app0.raiden.address,
        deposit,
        events_poll_timeout,
    )

    amount = 30
    direct_transfer(
        app0,
        app1,
        token_address,
        amount,
        identifier=1,
    )

    channel_0_1 = get_channelstate(app0, app1, token_address)
    app0_events = RaidenAPI(app0.raiden).get_channel_events(channel_0_1.identifier, 0)

    assert must_have_event(app0_events, {'_event_type': b'ChannelNewBalance'})

    # This event was temporarily removed. Confirmation from the protocol layer
    # as a state change is necessary to properly fire this event.
    # assert must_have_event(results, {'_event_type': b'EventTransferSentSuccess'})

    app0_events = app0.raiden.wal.storage.get_events_by_identifier(0, 'latest')
    max_block = max(event[0] for event in app0_events)
    results = RaidenAPI(app0.raiden).get_channel_events(
        channel_0_1.identifier,
        max_block + 1,
        max_block + 100,
    )
    assert not results

    app1_events = RaidenAPI(app1.raiden).get_channel_events(channel_0_1.identifier, 0)
    assert must_have_event(app1_events, {'_event_type': b'ChannelNewBalance'})
    assert must_have_event(app1_events, {'_event_type': b'EventTransferReceivedSuccess'})
Ejemplo n.º 2
0
    def channel_deposit(self,
                        token_address,
                        partner_address,
                        amount,
                        poll_timeout=DEFAULT_POLL_TIMEOUT):
        """ Deposit `amount` in the channel with the peer at `partner_address` and the
        given `token_address` in order to be able to do transfers.

        Raises:
            InvalidAddress: If either token_address or partner_address is not
            20 bytes long.
            TransactionThrew: May happen for multiple reasons:
                - If the token approval fails, e.g. the token may validate if
                  account has enough balance for the allowance.
                - The deposit failed, e.g. the allowance did not set the token
                  aside for use and the user spent it before deposit was called.
                - The channel was closed/settled between the allowance call and
                  the deposit call.
            AddressWithoutCode: The channel was settled during the deposit
            execution.
        """
        node_state = views.state_from_raiden(self.raiden)
        registry_address = self.raiden.default_registry.address

        token_networks = views.get_token_network_addresses_for(
            node_state,
            registry_address,
        )
        channel_state = views.get_channelstate_for(
            node_state,
            registry_address,
            token_address,
            partner_address,
        )

        if not isaddress(token_address):
            raise InvalidAddress(
                'Expected binary address format for token in channel deposit')

        if not isaddress(partner_address):
            raise InvalidAddress(
                'Expected binary address format for partner in channel deposit'
            )

        if token_address not in token_networks:
            raise UnknownTokenAddress('Unknown token address')

        if channel_state is None:
            raise InvalidAddress(
                'No channel with partner_address for the given token')

        token = self.raiden.chain.token(token_address)
        balance = token.balance_of(hexlify(self.raiden.address))

        # If this check succeeds it does not imply the the `deposit` will
        # succeed, since the `deposit` transaction may race with another
        # transaction.
        if not balance >= amount:
            msg = 'Not enough balance to deposit. {} Available={} Tried={}'.format(
                pex(token_address),
                balance,
                amount,
            )
            raise InsufficientFunds(msg)

        netcontract_address = channel_state.identifier
        channel_proxy = self.raiden.chain.netting_channel(netcontract_address)

        # If concurrent operations are happening on the channel, fail the request
        if not channel_proxy.channel_operations_lock.acquire(blocking=False):
            raise ChannelBusyError(
                f'Channel with id {channel_state.identifier} is '
                f'busy with another ongoing operation')

        with releasing(channel_proxy.channel_operations_lock):
            token.approve(netcontract_address, amount)
            channel_proxy.deposit(amount)

            old_balance = channel_state.our_state.contract_balance
            target_balance = old_balance + amount

            msg = 'After {} seconds the deposit was not properly processed.'.format(
                poll_timeout)

            # Wait until the `ChannelNewBalance` event is processed.
            with gevent.Timeout(poll_timeout, EthNodeCommunicationError(msg)):
                waiting.wait_for_newbalance(
                    self.raiden,
                    registry_address,
                    token_address,
                    partner_address,
                    target_balance,
                    self.raiden.alarm.wait_time,
                )