コード例 #1
0
def handle_channel_new(raiden: RaidenService, event: Event):
    data = event.event_data
    block_number = data['block_number']
    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,
            raiden.default_registry.address,
            token_network_identifier,
            raiden.config['reveal_timeout'],
            channel_proxy,
            block_number,
        )

        new_channel = ContractReceiveChannelNew(
            transaction_hash=transaction_hash,
            token_network_identifier=token_network_identifier,
            channel_state=channel_state,
            block_number=block_number,
        )
        raiden.handle_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,
        )
        raiden.handle_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)