Exemplo n.º 1
0
def test_set_secret():
    amount = 10
    block_number = 7
    routes = []
    routes_state = RoutesState(routes)

    state = MediatorState(
        factories.ADDR,
        routes_state,
        block_number,
        factories.UNIT_HASHLOCK,
    )

    state.transfers_pair = make_transfers_pair(
        [factories.HOP1, factories.HOP2, factories.HOP3],
        factories.HOP6,
        amount,
    )

    mediator.set_secret(state, factories.UNIT_SECRET)
    assert state.secret == factories.UNIT_SECRET

    for pair in state.transfers_pair:
        assert pair.payer_transfer.secret == factories.UNIT_SECRET
        assert pair.payee_transfer.secret == factories.UNIT_SECRET
def test_set_secret():
    amount = 10
    block_number = 7
    routes = []
    routes_state = RoutesState(routes)

    state = MediatorState(
        factories.ADDR,
        routes_state,
        block_number,
        factories.UNIT_HASHLOCK,
    )

    state.transfers_pair = make_transfers_pair(
        factories.HOP1,
        [factories.HOP2, factories.HOP3, factories.HOP4],
        factories.HOP6,
        amount,
    )

    mediator.set_secret(state, factories.UNIT_SECRET)
    assert state.secret == factories.UNIT_SECRET

    for pair in state.transfers_pair:
        assert pair.payer_transfer.secret == factories.UNIT_SECRET
        assert pair.payee_transfer.secret == factories.UNIT_SECRET
Exemplo n.º 3
0
def test_mediate_transfer():
    amount = 10
    block_number = 5
    expiration = 30

    routes = [
        factories.make_route(factories.HOP2, available_balance=factories.UNIT_TRANSFER_AMOUNT),
    ]

    routes_state = RoutesState(routes)
    state = MediatorState(
        factories.ADDR,
        routes_state,
        block_number,
        factories.UNIT_HASHLOCK,
    )

    payer_route, payer_transfer = make_from(amount, factories.HOP6, expiration)

    iteration = mediator.mediate_transfer(
        state,
        payer_route,
        payer_transfer,
    )

    events_mediated = [
        e
        for e in iteration.events
        if isinstance(e, SendMediatedTransfer)
    ]

    assert len(events_mediated) == 1
    transfer = events_mediated[0]
    assert transfer.identifier == payer_transfer.identifier
    assert transfer.token == payer_transfer.token
    assert transfer.amount == payer_transfer.amount
    assert transfer.hashlock == payer_transfer.hashlock
    assert transfer.target == payer_transfer.target
    assert payer_transfer.expiration > transfer.expiration
    assert transfer.node_address == payer_route.node_address
Exemplo n.º 4
0
def state_transition(state, state_change):
    """ State machine for a node mediating a transfer. """
    # pylint: disable=too-many-branches
    # Notes:
    # - A user cannot cancel a mediated transfer after it was initiated, she
    #   may only reject to mediate before hand. This is because the mediator
    #   doesn't control the secret reveal and needs to wait for the lock
    #   expiration before safely discarding the transfer.

    iteration = TransitionResult(state, list())

    if state is None:
        if isinstance(state_change, ActionInitMediator):
            routes = state_change.routes

            from_route = state_change.from_route
            from_transfer = state_change.from_transfer

            state = MediatorState(
                state_change.our_address,
                routes,
                state_change.block_number,
                from_transfer.hashlock,
            )

            iteration = mediate_transfer(state, from_route, from_transfer)

    elif state.secret is None:
        if isinstance(state_change, Block):
            iteration = handle_block(state, state_change)

        elif isinstance(state_change, ActionRouteChange):
            iteration = handle_routechange(state, state_change)

        elif isinstance(state_change, ReceiveTransferRefund):
            iteration = handle_refundtransfer(state, state_change)

        elif isinstance(state_change, ReceiveSecretReveal):
            iteration = handle_secretreveal(state, state_change)

        elif isinstance(state_change, ContractReceiveWithdraw):
            iteration = handle_contractwithdraw(state, state_change)

    else:
        if isinstance(state_change, Block):
            iteration = handle_block(state, state_change)

        elif isinstance(state_change, ActionRouteChange):
            iteration = handle_routechange(state, state_change)

        if isinstance(state_change, ReceiveSecretReveal):
            iteration = handle_secretreveal(state, state_change)

        elif isinstance(state_change, ReceiveBalanceProof):
            iteration = handle_balanceproof(state, state_change)

        elif isinstance(state_change, ContractReceiveWithdraw):
            iteration = handle_contractwithdraw(state, state_change)

    # this is the place for paranoia
    if iteration.new_state is not None:
        sanity_check(iteration.new_state)

    return clear_if_finalized(iteration)