Exemple #1
0
def blockchainevent_to_statechange(
    raiden: "RaidenService",
    event: DecodedEvent,
    current_confirmed_head: BlockNumber,
    pendingtokenregistration: Dict[TokenNetworkAddress,
                                   Tuple[TokenNetworkRegistryAddress,
                                         TokenAddress]],
) -> List[StateChange]:  # pragma: no unittest
    msg = "The state of the node has to be primed before blockchain events can be processed."
    assert raiden.wal, msg

    event_name = event.event_data["event"]
    chain_state = views.state_from_raiden(raiden)
    proxy_manager = raiden.proxy_manager

    state_changes: List[StateChange] = []

    if event_name == EVENT_TOKEN_NETWORK_CREATED:
        state_changes.append(
            contractreceivenewtokennetwork_from_event(
                event, pendingtokenregistration))

    elif event_name == ChannelEvent.OPENED:
        new_channel_details = get_contractreceivechannelnew_data_from_event(
            chain_state=chain_state,
            event=event,
            pendingtokenregistration=pendingtokenregistration)

        if new_channel_details is not None:
            fee_config: MediationFeeConfig = raiden.config.mediation_fees
            channel_config = ChannelConfig(
                reveal_timeout=raiden.config.reveal_timeout,
                fee_schedule=FeeScheduleState(
                    cap_fees=fee_config.cap_meditation_fees,
                    flat=fee_config.get_flat_fee(
                        new_channel_details.token_address),
                    proportional=fee_config.get_proportional_fee(
                        new_channel_details.token_address)
                    # no need to set the imbalance fee here, will be set during deposit
                ),
            )
            channel_new = contractreceivechannelnew_from_event(
                new_channel_details, channel_config, event)
            state_changes.append(channel_new)
        else:
            state_changes.append(contractreceiveroutenew_from_event(event))

    elif event_name == ChannelEvent.DEPOSIT:
        deposit = contractreceivechanneldeposit_from_event(
            event, raiden.config.mediation_fees)
        state_changes.append(deposit)

    elif event_name == ChannelEvent.WITHDRAW:
        withdraw = contractreceivechannelwithdraw_from_event(
            event, raiden.config.mediation_fees)
        state_changes.append(withdraw)

    elif event_name == ChannelEvent.BALANCE_PROOF_UPDATED:
        channel_state = get_contractreceiveupdatetransfer_data_from_event(
            chain_state, event)
        if channel_state:
            state_changes.append(
                contractreceiveupdatetransfer_from_event(channel_state, event))

    elif event_name == ChannelEvent.CLOSED:
        canonical_identifier = get_contractreceivechannelclosed_data_from_event(
            chain_state, event)

        if canonical_identifier is not None:
            state_changes.append(
                contractreceivechannelclosed_from_event(
                    canonical_identifier, event))
        else:
            state_changes.append(contractreceiverouteclosed_from_event(event))

    elif event_name == ChannelEvent.SETTLED:
        channel_settle_state = get_contractreceivechannelsettled_data_from_event(
            proxy_manager=proxy_manager,
            chain_state=chain_state,
            event=event,
            current_confirmed_head=current_confirmed_head,
        )

        if channel_settle_state:
            state_changes.append(
                contractreceivechannelsettled_from_event(
                    channel_settle_state, event))
        else:
            log.debug("Discarding settle event, we're not part of it",
                      raiden_event=event)

    elif event_name == EVENT_SECRET_REVEALED:
        state_changes.append(contractreceivesecretreveal_from_event(event))

    elif event_name == ChannelEvent.UNLOCKED:
        canonical_identifier = get_contractreceivechannelbatchunlock_data_from_event(
            chain_state, raiden.wal.storage, event)

        if canonical_identifier is not None:
            state_changes.append(
                contractreceivechannelbatchunlock_from_event(
                    canonical_identifier, event))
        else:
            log.debug("Discarding unlock event, we're not part of it",
                      raiden_event=event)

    else:
        log.error("Unknown event type", raiden_event=event)

    return state_changes
Exemple #2
0
def blockchainevent_to_statechange(
    raiden_config: RaidenConfig,
    proxy_manager: ProxyManager,
    raiden_storage: SerializedSQLiteStorage,
    chain_state: ChainState,
    event: DecodedEvent,
    current_confirmed_head: BlockNumber,
) -> Optional[StateChange]:  # pragma: no unittest
    event_name = event.event_data["event"]

    if event_name == EVENT_TOKEN_NETWORK_CREATED:
        return contractreceivenewtokennetwork_from_event(event)

    elif event_name == ChannelEvent.OPENED:
        new_channel_details = get_contractreceivechannelnew_data_from_event(
            chain_state, event)

        if new_channel_details is not None:
            fee_config = raiden_config.mediation_fees
            channel_config = ChannelConfig(
                reveal_timeout=raiden_config.reveal_timeout,
                fee_schedule=FeeScheduleState(
                    cap_fees=fee_config.cap_meditation_fees,
                    flat=fee_config.get_flat_fee(
                        new_channel_details.token_address),
                    proportional=fee_config.get_proportional_fee(
                        new_channel_details.token_address)
                    # no need to set the imbalance fee here, will be set during deposit
                ),
            )
            return contractreceivechannelnew_from_event(
                new_channel_details, channel_config, event)
        else:
            return contractreceiveroutenew_from_event(event)

    elif event_name == ChannelEvent.DEPOSIT:
        return contractreceivechanneldeposit_from_event(
            event, raiden_config.mediation_fees)

    elif event_name == ChannelEvent.WITHDRAW:
        return contractreceivechannelwithdraw_from_event(
            event, raiden_config.mediation_fees)

    elif event_name == ChannelEvent.BALANCE_PROOF_UPDATED:
        channel_state = get_contractreceiveupdatetransfer_data_from_event(
            chain_state, event)
        if channel_state:
            return contractreceiveupdatetransfer_from_event(
                channel_state, event)

    elif event_name == ChannelEvent.CLOSED:
        canonical_identifier = get_contractreceivechannelclosed_data_from_event(
            chain_state, event)

        if canonical_identifier is not None:
            return contractreceivechannelclosed_from_event(
                canonical_identifier, event)
        else:
            return contractreceiverouteclosed_from_event(event)

    elif event_name == ChannelEvent.SETTLED:
        channel_settle_state = get_contractreceivechannelsettled_data_from_event(
            proxy_manager=proxy_manager,
            chain_state=chain_state,
            event=event,
            current_confirmed_head=current_confirmed_head,
        )

        if channel_settle_state:
            return contractreceivechannelsettled_from_event(
                channel_settle_state, event)
        else:
            log.debug("Discarding settle event, we're not part of it",
                      raiden_event=event)

    elif event_name == EVENT_SECRET_REVEALED:
        return contractreceivesecretreveal_from_event(event)

    elif event_name == ChannelEvent.UNLOCKED:
        canonical_identifier = get_contractreceivechannelbatchunlock_data_from_event(
            chain_state, raiden_storage, event)

        if canonical_identifier is not None:
            return contractreceivechannelbatchunlock_from_event(
                canonical_identifier, event)
        else:
            log.debug("Discarding unlock event, we're not part of it",
                      raiden_event=event)

    else:
        log.error("Unknown event type", raiden_event=event)

    return None