def mediate_transfer(
        state: MediatorTransferState,
        possible_routes: List['RouteState'],
        payer_channel: NettingChannelState,
        channelidentifiers_to_channels: ChannelMap,
        nodeaddresses_to_networkstates: NodeNetworkStateMap,
        pseudo_random_generator: random.Random,
        payer_transfer: LockedTransferSignedState,
        block_number: BlockNumber,
) -> TransitionResult[MediatorTransferState]:
    """ Try a new route or fail back to a refund.

    The mediator can safely try a new route knowing that the tokens from
    payer_transfer will cover the expenses of the mediation. If there is no
    route available that may be used at the moment of the call the mediator may
    send a refund back to the payer, allowing the payer to try a different
    route.
    """
    reachable_routes = filter_reachable_routes(
        possible_routes,
        nodeaddresses_to_networkstates,
    )
    available_routes = filter_used_routes(
        state.transfers_pair,
        reachable_routes,
    )

    assert payer_channel.partner_state.address == payer_transfer.balance_proof.sender

    transfer_pair, mediated_events = forward_transfer_pair(
        payer_transfer,
        available_routes,
        channelidentifiers_to_channels,
        pseudo_random_generator,
        block_number,
    )

    if transfer_pair is None:
        assert not mediated_events

        if state.transfers_pair:
            original_pair = state.transfers_pair[0]
            original_channel = get_payer_channel(
                channelidentifiers_to_channels,
                original_pair,
            )
        else:
            original_channel = payer_channel

        if original_channel:
            transfer_pair, mediated_events = backward_transfer_pair(
                original_channel,
                payer_transfer,
                pseudo_random_generator,
                block_number,
            )
        else:
            transfer_pair = None
            mediated_events = list()

    if transfer_pair is None:
        assert not mediated_events
        mediated_events = list()
        state.waiting_transfer = WaitingTransferState(payer_transfer)

    else:
        # the list must be ordered from high to low expiration, expiration
        # handling depends on it
        state.transfers_pair.append(transfer_pair)

    return TransitionResult(state, mediated_events)
def mediate_transfer ( state : MediatorTransferState , possible_routes : List [ 'RouteState' ] , payer_channel : NettingChannelState , channelidentifiers_to_channels : ChannelMap , nodeaddresses_to_networkstates : NodeNetworkStateMap , pseudo_random_generator : random . Random , payer_transfer : LockedTransferSignedState , block_number : BlockNumber , ) -> TransitionResult [ MediatorTransferState ] : reachable_routes = filter_reachable_routes ( possible_routes , nodeaddresses_to_networkstates , ) available_routes = filter_used_routes ( state . transfers_pair , reachable_routes , ) assert payer_channel . partner_state . address == payer_transfer . balance_proof . sender transfer_pair , mediated_events = forward_transfer_pair ( payer_transfer , available_routes , channelidentifiers_to_channels , pseudo_random_generator , block_number , ) if transfer_pair is None : assert not mediated_events if state . transfers_pair : original_pair = state . transfers_pair [ 0 ] original_channel = get_payer_channel ( channelidentifiers_to_channels , original_pair , ) else : original_channel = payer_channel if original_channel : transfer_pair , mediated_events = backward_transfer_pair ( original_channel , payer_transfer , pseudo_random_generator , block_number , ) else : transfer_pair = None mediated_events = list ( ) if transfer_pair is None : assert not mediated_events mediated_events = list ( ) state . waiting_transfer = WaitingTransferState ( payer_transfer ) else : state . transfers_pair . append ( transfer_pair ) return TransitionResult ( state , mediated_events )
Try a new route or fail back to a refund.

    The mediator can safely try a new route knowing that the tokens from
    payer_transfer will cover the expenses of the mediation. If there is no
    route available that may be used at the moment of the call the mediator may
    send a refund back to the payer, allowing the payer to try a different
    route.