Exemplo n.º 1
0
def handle_tokennetwork_new(raiden: 'RaidenService', event: Event):
    """ Handles a `TokenNetworkCreated` event. """
    data = event.event_data
    args = data['args']
    block_number = data['block_number']
    token_network_address = args['token_network_address']
    token_address = typing.TokenAddress(args['token_address'])
    block_hash = data['block_hash']

    token_network_proxy = raiden.chain.token_network(token_network_address)
    raiden.blockchain_events.add_token_network_listener(
        token_network_proxy=token_network_proxy,
        contract_manager=raiden.contract_manager,
        from_block=block_number,
    )

    token_network_state = TokenNetworkState(
        token_network_address,
        token_address,
    )

    transaction_hash = event.event_data['transaction_hash']

    new_token_network = ContractReceiveNewTokenNetwork(
        transaction_hash=transaction_hash,
        payment_network_identifier=event.originating_contract,
        token_network=token_network_state,
        block_number=block_number,
        block_hash=block_hash,
    )
    raiden.handle_and_track_state_change(new_token_network)
Exemplo n.º 2
0
def wait_for_settle_all_channels(
    raiden: RaidenService,
    retry_timeout: float,
) -> None:
    """Wait until all channels are settled.

    Note:
        This does not time out, use gevent.Timeout.
    """
    chain_state = views.state_from_raiden(raiden)

    id_paymentnetworkstate = chain_state.identifiers_to_paymentnetworks.items()
    for payment_network_id, payment_network_state in id_paymentnetworkstate:

        id_tokennetworkstate = payment_network_state.tokenidentifiers_to_tokennetworks.items(
        )
        for token_network_id, token_network_state in id_tokennetworkstate:
            channel_ids = cast(
                List[typing.ChannelID],
                token_network_state.channelidentifiers_to_channels.keys(),
            )

            wait_for_settle(
                raiden=raiden,
                payment_network_id=payment_network_id,
                token_address=typing.TokenAddress(token_network_id),
                channel_ids=channel_ids,
                retry_timeout=retry_timeout,
            )
def handle_tokennetwork_new(raiden, event: Event):
    """ Handles a `TokenNetworkCreated` event. """
    data = event.event_data
    token_network_address = data['token_network_address']

    token_network_proxy = raiden.chain.token_network(token_network_address)
    raiden.blockchain_events.add_token_network_listener(
        token_network_proxy=token_network_proxy,
        contract_manager=raiden.contract_manager,
        from_block=data['blockNumber'],
    )

    token_address = typing.TokenAddress(
        data_decoder(event.event_data['args']['token_address']), )

    token_network_state = TokenNetworkState(
        token_network_address,
        token_address,
    )

    transaction_hash = event.event_data['transactionHash']
    assert transaction_hash, 'A mined transaction must have the hash field'

    new_token_network = ContractReceiveNewTokenNetwork(
        transaction_hash=transaction_hash,
        payment_network_identifier=event.originating_contract,
        token_network=token_network_state,
        block_number=data['block_number'],
    )
    raiden.handle_state_change(new_token_network)
Exemplo n.º 4
0
def handle_channel_new(raiden: 'RaidenService', event: Event):
    data = event.event_data
    block_number = data['block_number']
    block_hash = data['block_hash']
    args = data['args']
    token_network_identifier = event.originating_contract
    transaction_hash = event.event_data['transaction_hash']
    channel_identifier = args['channel_identifier']
    participant1 = args['participant1']
    participant2 = args['participant2']
    is_participant = raiden.address in (participant1, participant2)

    # Raiden node is participant
    if is_participant:
        channel_proxy = raiden.chain.payment_channel(
            token_network_identifier,
            channel_identifier,
        )
        token_address = channel_proxy.token_address()
        channel_state = get_channel_state(
            token_address=typing.TokenAddress(token_address),
            payment_network_identifier=raiden.default_registry.address,
            token_network_address=token_network_identifier,
            reveal_timeout=raiden.config['reveal_timeout'],
            payment_channel_proxy=channel_proxy,
            opened_block_number=block_number,
            opened_block_hash=block_hash,
        )

        new_channel = ContractReceiveChannelNew(
            transaction_hash=transaction_hash,
            token_network_identifier=token_network_identifier,
            channel_state=channel_state,
            block_number=block_number,
            block_hash=block_hash,
        )
        raiden.handle_and_track_state_change(new_channel)

        partner_address = channel_state.partner_state.address

        if ConnectionManager.BOOTSTRAP_ADDR != partner_address:
            raiden.start_health_check_for(partner_address)

    # Raiden node is not participant of channel
    else:
        new_route = ContractReceiveRouteNew(
            transaction_hash=transaction_hash,
            token_network_identifier=token_network_identifier,
            channel_identifier=channel_identifier,
            participant1=participant1,
            participant2=participant2,
            block_number=block_number,
            block_hash=block_hash,
        )
        raiden.handle_and_track_state_change(new_route)

    # A new channel is available, run the connection manager in case more
    # connections are needed
    connection_manager = raiden.connection_manager_for_token_network(
        token_network_identifier)
    retry_connect = gevent.spawn(connection_manager.retry_connect)
    raiden.add_pending_greenlet(retry_connect)
Exemplo n.º 5
0
def handle_channel_new(raiden: "RaidenService", event: Event):
    data = event.event_data
    block_number = data["block_number"]
    block_hash = data["block_hash"]
    args = data["args"]
    token_network_identifier = event.originating_contract
    transaction_hash = event.event_data["transaction_hash"]
    channel_identifier = args["channel_identifier"]
    participant1 = args["participant1"]
    participant2 = args["participant2"]
    is_participant = raiden.address in (participant1, participant2)

    # Check if at least one of the implied participants is a LC handled by the node
    is_participant1_handled_lc = LightClientService.is_handled_lc(
        to_checksum_address(encode_hex(participant1)), raiden.wal)
    is_participant2_handled_lc = LightClientService.is_handled_lc(
        to_checksum_address(encode_hex(participant2)), raiden.wal)

    if is_participant or is_participant1_handled_lc or is_participant2_handled_lc:
        channel_proxy = raiden.chain.payment_channel(
            canonical_identifier=CanonicalIdentifier(
                chain_identifier=views.state_from_raiden(raiden).chain_id,
                token_network_address=token_network_identifier,
                channel_identifier=channel_identifier,
            ))
        token_address = channel_proxy.token_address()
        channel_state = get_channel_state(
            token_address=typing.TokenAddress(token_address),
            payment_network_identifier=raiden.default_registry.address,
            token_network_address=token_network_identifier,
            reveal_timeout=raiden.config["reveal_timeout"],
            payment_channel_proxy=channel_proxy,
            opened_block_number=block_number,
        )

        # Swap our_state and partner_state in order to have the LC from our_side of the channel
        if is_participant1_handled_lc or is_participant2_handled_lc:
            if is_participant1_handled_lc:
                if participant1 != channel_state.our_state.address:
                    channel_state.our_state, channel_state.partner_state = channel_state.partner_state, channel_state.our_state
            else:
                if participant2 != channel_state.our_state.address:
                    channel_state.our_state, channel_state.partner_state = channel_state.partner_state, channel_state.our_state

        new_channel = ContractReceiveChannelNew(
            transaction_hash=transaction_hash,
            channel_state=channel_state,
            block_number=block_number,
            block_hash=block_hash,
        )
        raiden.handle_and_track_state_change(new_channel)

        partner_address = channel_state.partner_state.address

        light_client_address = None
        if is_participant1_handled_lc:
            light_client_address = participant1
        elif is_participant2_handled_lc:
            light_client_address = participant2

        if ConnectionManager.BOOTSTRAP_ADDR != partner_address:
            raiden.start_health_check_for(partner_address,
                                          light_client_address)

    # Raiden node is not participant of channel. Lc are not participants
    else:
        new_route = ContractReceiveRouteNew(
            transaction_hash=transaction_hash,
            canonical_identifier=CanonicalIdentifier(
                chain_identifier=raiden.chain.network_id,
                token_network_address=token_network_identifier,
                channel_identifier=channel_identifier,
            ),
            participant1=participant1,
            participant2=participant2,
            block_number=block_number,
            block_hash=block_hash,
        )
        raiden.handle_and_track_state_change(new_route)

    # A new channel is available, run the connection manager in case more
    # connections are needed
    connection_manager = raiden.connection_manager_for_token_network(
        token_network_identifier)
    retry_connect = gevent.spawn(connection_manager.retry_connect)
    raiden.add_pending_greenlet(retry_connect)