Пример #1
0
def test_handle_offchain_secretreveal():
    """ The target node needs to inform the secret to the previous node to
    receive an updated balance proof.
    """
    setup = make_target_state()
    state_change = ReceiveSecretReveal(UNIT_SECRET, setup.initiator)
    iteration = target.handle_offchain_secretreveal(
        target_state=setup.new_state,
        state_change=state_change,
        channel_state=setup.channel,
        pseudo_random_generator=setup.pseudo_random_generator,
        block_number=setup.block_number,
    )
    assert len(iteration.events) == 1

    reveal = iteration.events[0]
    assert isinstance(reveal, SendSecretReveal)

    assert iteration.new_state.state == 'reveal_secret'
    assert reveal.secret == UNIT_SECRET
    assert reveal.recipient == setup.new_state.route.node_address

    # if we get an empty hash secret make sure it's rejected
    secret = EMPTY_HASH
    state_change = ReceiveSecretReveal(secret, setup.initiator)
    iteration = target.handle_offchain_secretreveal(
        target_state=setup.new_state,
        state_change=state_change,
        channel_state=setup.channel,
        pseudo_random_generator=setup.pseudo_random_generator,
        block_number=setup.block_number,
    )
    assert len(iteration.events) == 0
Пример #2
0
    def wrong_address_receive_secret_reveal(self, previous_action_with_address, invalid_sender):
        previous_action, client, our_address = self._unwrap(previous_action_with_address)
        secret = self.secrethash_to_secret[previous_action.from_transfer.lock.secrethash]
        invalid_action = ReceiveSecretReveal(secret, invalid_sender)
        result = node.state_transition(client.chain_state, invalid_action)
        assert not result.events

        valid_sender = previous_action.from_transfer.target
        valid_action = ReceiveSecretReveal(secret, valid_sender)
        return WithOurAddress(our_address=our_address, data=valid_action)
Пример #3
0
def make_mediated_transfer(registry_address,
                           from_channel,
                           partner_channel,
                           initiator,
                           target,
                           lock,
                           pkey,
                           secret=None):
    """ Helper to create and register a mediated transfer from `from_channel` to
    `partner_channel`."""
    payment_identifier = channel.get_next_nonce(from_channel.our_state)
    message_identifier = random.randint(0, UINT64_MAX)

    lockedtransfer = channel.send_lockedtransfer(
        registry_address,
        from_channel,
        initiator,
        target,
        lock.amount,
        message_identifier,
        payment_identifier,
        lock.expiration,
        lock.secrethash,
    )
    mediated_transfer_msg = LockedTransfer.from_event(lockedtransfer)

    address = privatekey_to_address(pkey)
    sign_key = PrivateKey(pkey)
    mediated_transfer_msg.sign(sign_key, address)

    # compute the signature
    balance_proof = balanceproof_from_envelope(mediated_transfer_msg)
    lockedtransfer.balance_proof = balance_proof

    # if this fails it's not the right key for the current `from_channel`
    assert mediated_transfer_msg.sender == from_channel.our_state.address
    receive_lockedtransfer = lockedtransfersigned_from_message(
        mediated_transfer_msg)

    channel.handle_receive_lockedtransfer(
        partner_channel,
        receive_lockedtransfer,
    )

    if secret is not None:
        random_sender = make_address()

        from_secretreveal = ReceiveSecretReveal(secret, random_sender)
        channel.handle_receive_secretreveal(from_channel, from_secretreveal)

        partner_secretreveal = ReceiveSecretReveal(secret, random_sender)
        channel.handle_receive_secretreveal(partner_channel,
                                            partner_secretreveal)

    return mediated_transfer_msg
Пример #4
0
    def wrong_address_receive_secret_reveal(self, previous_action,
                                            invalid_sender):
        secret = self.secrethash_to_secret[
            previous_action.from_transfer.lock.secrethash]
        invalid_action = ReceiveSecretReveal(secret, invalid_sender)
        result = node.state_transition(self.chain_state, invalid_action)
        assert not result.events

        valid_sender = previous_action.from_transfer.target
        valid_action = ReceiveSecretReveal(secret, valid_sender)
        return valid_action
Пример #5
0
def test_state_wait_unlock_valid():
    setup = setup_initiator_tests()

    # setup the state for the wait unlock
    setup.current_state.initiator.revealsecret = SendSecretReveal(
        recipient=UNIT_TRANSFER_TARGET,
        channel_identifier=CHANNEL_IDENTIFIER_GLOBAL_QUEUE,
        message_identifier=UNIT_TRANSFER_IDENTIFIER,
        secret=UNIT_SECRET,
    )

    state_change = ReceiveSecretReveal(
        secret=UNIT_SECRET,
        sender=setup.channel.partner_state.address,
    )
    iteration = initiator_manager.state_transition(
        setup.current_state,
        state_change,
        setup.channel_map,
        setup.prng,
        setup.block_number,
    )

    assert len(iteration.events) == 3
    assert any(isinstance(e, SendBalanceProof) for e in iteration.events)
    assert any(isinstance(e, EventPaymentSentSuccess) for e in iteration.events)
    assert any(isinstance(e, EventUnlockSuccess) for e in iteration.events)

    balance_proof = next(e for e in iteration.events if isinstance(e, SendBalanceProof))
    complete = next(e for e in iteration.events if isinstance(e, EventPaymentSentSuccess))

    assert balance_proof.recipient == setup.channel.partner_state.address
    assert complete.identifier == UNIT_TRANSFER_IDENTIFIER
    assert iteration.new_state is None, 'state must be cleaned'
Пример #6
0
    def message_secret(self, message):
        self.balance_proof(message)
        hashlock = message.hashlock
        identifier = message.identifier
        secret = message.secret

        self.raiden.register_secret(secret)

        channel = None
        try:
            channel = self.raiden.find_channel_by_address(message.channel)
        except ValueError:
            log.info('Message for unknown channel: {}'.format(
                pex(message.channel)))
        else:
            self.raiden.handle_secret(
                identifier,
                channel.token_address,
                secret,
                message,
                hashlock,
            )

        self.raiden.greenlet_task_dispatcher.dispatch_message(
            message, hashlock)

        state_change = ReceiveSecretReveal(
            secret,
            message.sender,
        )

        self.raiden.state_machine_event_handler.log_and_dispatch_by_identifier(
            identifier,
            state_change,
        )
Пример #7
0
def test_target_lock_is_expired_if_secret_is_not_registered_onchain():
    lock_amount = 7
    block_number = 1
    initiator = factories.HOP6
    pseudo_random_generator = random.Random()

    our_balance = 100
    our_address = factories.make_address()
    partner_balance = 130

    from_channel = factories.make_channel(
        our_address=our_address,
        our_balance=our_balance,
        partner_address=UNIT_TRANSFER_SENDER,
        partner_balance=partner_balance,
    )
    from_route = factories.route_from_channel(from_channel)
    expiration = block_number + from_channel.settle_timeout - from_channel.reveal_timeout

    from_transfer = factories.make_signed_transfer_for(
        from_channel,
        lock_amount,
        initiator,
        our_address,
        expiration,
        UNIT_SECRET,
    )

    init = ActionInitTarget(
        from_route,
        from_transfer,
    )

    init_transition = target.state_transition(
        None,
        init,
        from_channel,
        pseudo_random_generator,
        block_number,
    )
    assert init_transition.new_state is not None

    secret_reveal_iteration = target.state_transition(
        target_state=init_transition.new_state,
        state_change=ReceiveSecretReveal(UNIT_SECRET,
                                         from_channel.partner_state.address),
        channel_state=from_channel,
        pseudo_random_generator=pseudo_random_generator,
        block_number=block_number,
    )

    expired_block_number = from_transfer.lock.expiration + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS
    iteration = target.state_transition(
        target_state=secret_reveal_iteration.new_state,
        state_change=Block(expired_block_number, None, None),
        channel_state=from_channel,
        pseudo_random_generator=pseudo_random_generator,
        block_number=expired_block_number,
    )
    assert must_contain_entry(iteration.events, EventUnlockClaimFailed, {})
def test_state_wait_unlock_invalid():
    setup = setup_initiator_tests()
    identifier = setup.channel.identifier
    target_address = factories.HOP2

    # setup the state for the wait unlock
    setup.current_state.initiator.revealsecret = SendSecretReveal(
        recipient=target_address,
        channel_identifier=CHANNEL_IDENTIFIER_GLOBAL_QUEUE,
        message_identifier=identifier,
        secret=UNIT_SECRET,
    )

    before_state = deepcopy(setup.current_state)

    state_change = ReceiveSecretReveal(
        secret=UNIT_SECRET,
        sender=factories.ADDR,
    )
    iteration = initiator_manager.state_transition(
        setup.current_state,
        state_change,
        setup.channel_map,
        setup.prng,
        setup.block_number,
    )

    assert not iteration.events
    assert iteration.new_state == before_state
Пример #9
0
def test_handle_secretreveal():
    """ The target node needs to inform the secret to the previous node to
    receive an updated balance proof.
    """
    amount = 3
    block_number = 1
    expiration = block_number + factories.UNIT_REVEAL_TIMEOUT
    initiator = factories.HOP1
    our_address = factories.ADDR
    secret = factories.UNIT_SECRET
    pseudo_random_generator = random.Random()

    channel_state, state = make_target_state(
        our_address,
        amount,
        block_number,
        initiator,
        expiration,
    )
    state_change = ReceiveSecretReveal(secret, initiator)

    iteration = target.handle_secretreveal(
        state,
        state_change,
        channel_state,
        pseudo_random_generator,
    )
    assert len(iteration.events) == 1

    reveal = iteration.events[0]
    assert isinstance(reveal, SendRevealSecret)

    assert iteration.new_state.state == 'reveal_secret'
    assert reveal.secret == secret
    assert reveal.recipient == state.route.node_address
Пример #10
0
def handle_message_revealsecret(raiden: 'RaidenService',
                                message: RevealSecret):
    state_change = ReceiveSecretReveal(
        message.secret,
        message.sender,
    )
    raiden.handle_state_change(state_change)
Пример #11
0
def handle_channel_unlock(node_state, state_change):
    token_address = state_change.token_address
    payment_network_state, token_network_state = get_networks(
        node_state,
        state_change.payment_network_identifier,
        state_change.token_address,
    )

    # first dispatch the unlock claim to update the channel
    events = []
    if token_network_state:
        pseudo_random_generator = node_state.pseudo_random_generator
        sub_iteration = token_network.subdispatch_to_channel_by_id(
            token_network_state,
            state_change,
            pseudo_random_generator,
            node_state.block_number,
        )
        events.extend(sub_iteration.events)

        if sub_iteration.new_state is None:
            del payment_network_state.tokenaddresses_to_tokennetworks[token_address]

    # second emulate a secret reveal, to register the secret with all the other
    # channels and proceed with the protocol
    state_change = ReceiveSecretReveal(state_change.secret, None)
    sub_iteration_secret_reveal = handle_secret_reveal(
        node_state,
        state_change,
    )
    events.extend(sub_iteration_secret_reveal.events)

    return TransitionResult(node_state, events)
Пример #12
0
 def wrong_secret_receive_secret_reveal(self, previous_action_with_address,
                                        secret):
     previous_action, client, _ = self._unwrap(previous_action_with_address)
     sender = previous_action.from_transfer.target
     action = ReceiveSecretReveal(secret, sender)
     result = node.state_transition(client.chain_state, action)
     assert not result.events
Пример #13
0
def test_handle_onchain_secretreveal():
    """ The target node must update the lock state when the secret is
    registered in the blockchain.
    """
    setup = make_target_state(
        block_number=1,
        expiration=1 + factories.UNIT_REVEAL_TIMEOUT,
    )
    assert factories.UNIT_SECRETHASH in setup.channel.partner_state.secrethashes_to_lockedlocks

    offchain_secret_reveal_iteration = target.state_transition(
        target_state=setup.new_state,
        state_change=ReceiveSecretReveal(UNIT_SECRET, setup.initiator),
        channel_state=setup.channel,
        pseudo_random_generator=setup.pseudo_random_generator,
        block_number=setup.block_number,
    )
    assert UNIT_SECRETHASH in setup.channel.partner_state.secrethashes_to_unlockedlocks
    assert UNIT_SECRETHASH not in setup.channel.partner_state.secrethashes_to_lockedlocks

    # Make sure that an emptyhash on chain reveal is rejected.
    block_number_prior_the_expiration = setup.expiration - 2
    onchain_reveal = ContractReceiveSecretReveal(
        transaction_hash=factories.make_address(),
        secret_registry_address=factories.make_address(),
        secrethash=EMPTY_HASH_KECCAK,
        secret=EMPTY_HASH,
        block_number=block_number_prior_the_expiration,
    )
    onchain_secret_reveal_iteration = target.state_transition(
        target_state=offchain_secret_reveal_iteration.new_state,
        state_change=onchain_reveal,
        channel_state=setup.channel,
        pseudo_random_generator=setup.pseudo_random_generator,
        block_number=block_number_prior_the_expiration,
    )
    unlocked_onchain = setup.channel.partner_state.secrethashes_to_onchain_unlockedlocks
    assert EMPTY_HASH_KECCAK not in unlocked_onchain

    # now let's go for the actual secret
    onchain_reveal.secret = UNIT_SECRET
    onchain_reveal.secrethash = UNIT_SECRETHASH
    onchain_secret_reveal_iteration = target.state_transition(
        target_state=offchain_secret_reveal_iteration.new_state,
        state_change=onchain_reveal,
        channel_state=setup.channel,
        pseudo_random_generator=setup.pseudo_random_generator,
        block_number=block_number_prior_the_expiration,
    )
    unlocked_onchain = setup.channel.partner_state.secrethashes_to_onchain_unlockedlocks
    assert UNIT_SECRETHASH in unlocked_onchain

    # Check that after we register a lock on-chain handling the block again will
    # not cause us to attempt an onchain re-register
    extra_block_handle_transition = target.handle_block(
        target_state=onchain_secret_reveal_iteration.new_state,
        channel_state=setup.channel,
        block_number=block_number_prior_the_expiration + 1,
    )
    assert len(extra_block_handle_transition.events) == 0
Пример #14
0
def test_handle_secretreveal():
    """ The target node needs to inform the secret to the previous node to
    receive an updated balance proof.
    """
    amount = 3
    block_number = 1
    expire = block_number + factories.UNIT_REVEAL_TIMEOUT
    initiator = factories.HOP1
    our_address = factories.ADDR
    secret = factories.UNIT_SECRET

    state = make_target_state(
        our_address,
        amount,
        block_number,
        initiator,
        expire,
    )
    state_change = ReceiveSecretReveal(secret, initiator)

    iteration = target.handle_secretreveal(state, state_change)
    reveal = [e for e in iteration.events if isinstance(e, SendRevealSecret)]

    assert iteration.new_state.state == 'reveal_secret'
    assert reveal[0].identifier == state.from_transfer.identifier
    assert reveal[0].secret == secret
    assert reveal[0].receiver == state.from_route.node_address
    assert reveal[0].sender == our_address
Пример #15
0
    def valid_receive_secret_reveal(self, previous_action):
        secret = self.secrethash_to_secret[
            previous_action.from_transfer.lock.secrethash]
        sender = previous_action.from_transfer.target
        recipient = previous_action.from_transfer.initiator

        action = ReceiveSecretReveal(secret=secret, sender=sender)
        result = node.state_transition(self.chain_state, action)

        expiration = previous_action.from_transfer.lock.expiration
        in_time = self.block_number < expiration - DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS
        still_waiting = self.block_number < expiration + DEFAULT_WAIT_BEFORE_LOCK_REMOVAL

        if in_time and self.channel_opened(sender) and self.channel_opened(
                recipient):
            assert event_types_match(result.events, SendSecretReveal,
                                     SendBalanceProof, EventUnlockSuccess)
            self.event("Unlock successful.")
            self.waiting_for_unlock[secret] = recipient
        elif still_waiting and self.channel_opened(recipient):
            assert event_types_match(result.events, SendSecretReveal)
            self.event("Unlock failed, secret revealed too late.")
        else:
            assert not result.events
            self.event(
                "ReceiveSecretRevealed after removal of lock - dropped.")
        return action
Пример #16
0
 def replay_receive_secret_reveal_scrambled_sender(
     self, previous_action_with_address, invalid_sender
 ):
     previous_action, client, _ = self._unwrap(previous_action_with_address)
     action = ReceiveSecretReveal(previous_action.secret, invalid_sender)
     result = node.state_transition(client.chain_state, action)
     assert not result.events
Пример #17
0
 def handle_message_revealsecret(
     raiden: "RaidenService",
     message: RevealSecret  # pylint: disable=unused-argument
 ) -> List[StateChange]:
     assert message.sender, "message must be signed"
     secret_reveal = ReceiveSecretReveal(secret=message.secret,
                                         sender=message.sender)
     return [secret_reveal]
Пример #18
0
    def message_revealsecret(self, message):
        secret = message.secret
        sender = message.sender

        self.raiden.register_secret(secret)

        state_change = ReceiveSecretReveal(secret, sender)
        self.raiden.state_machine_event_handler.log_and_dispatch_to_all_tasks(
            state_change)
Пример #19
0
def test_state_wait_unlock_valid():
    identifier = 1
    amount = factories.UNIT_TRANSFER_AMOUNT
    block_number = 1
    mediator_address = factories.HOP1
    target_address = factories.HOP2
    our_address = factories.ADDR
    secret_generator = SequenceGenerator()
    token = factories.UNIT_TOKEN_ADDRESS

    routes = [factories.make_route(mediator_address, available_balance=amount)]
    current_state = make_initiator_state(
        routes,
        target_address,
        block_number=block_number,
        our_address=our_address,
        secret_generator=secret_generator,
        identifier=identifier,
        token=token,
    )

    secret = secret_generator.secrets[0]
    assert secret is not None

    # setup the state for the wait unlock
    current_state.revealsecret = SendRevealSecret(
        identifier,
        secret,
        token,
        target_address,
        our_address,
    )

    initiator_state_machine = StateManager(
        initiator.state_transition,
        current_state,
    )

    state_change = ReceiveSecretReveal(
        secret=secret,
        sender=mediator_address,
    )
    events = initiator_state_machine.dispatch(state_change)
    assert len(events) == 3

    assert any(isinstance(e, SendBalanceProof) for e in events)
    assert any(isinstance(e, EventTransferSentSuccess) for e in events)
    assert any(isinstance(e, EventUnlockSuccess) for e in events)

    balance_proof = next(e for e in events if isinstance(e, SendBalanceProof))
    complete = next(e for e in events
                    if isinstance(e, EventTransferSentSuccess))

    assert balance_proof.receiver == mediator_address
    assert complete.identifier == identifier
    assert initiator_state_machine.current_state is None, 'state must be cleaned'
Пример #20
0
def test_handle_onchain_secretreveal():
    """ The target node must update the lock state when the secret is
    registered in the blockchain.
    """
    amount = 3
    block_number = 1
    expiration = block_number + factories.UNIT_REVEAL_TIMEOUT
    initiator = factories.HOP1
    our_address = factories.ADDR
    secret = factories.UNIT_SECRET
    pseudo_random_generator = random.Random()

    channel_state, state = make_target_state(
        our_address,
        amount,
        block_number,
        initiator,
        expiration,
    )
    assert factories.UNIT_SECRETHASH in channel_state.partner_state.secrethashes_to_lockedlocks

    offchain_secret_reveal_iteration = target.state_transition(
        state,
        ReceiveSecretReveal(secret, initiator),
        channel_state,
        pseudo_random_generator,
        block_number,
    )
    assert factories.UNIT_SECRETHASH in channel_state.partner_state.secrethashes_to_unlockedlocks
    assert factories.UNIT_SECRETHASH not in channel_state.partner_state.secrethashes_to_lockedlocks

    block_number_prior_the_expiration = expiration - 2
    onchain_secret_reveal_iteration = target.state_transition(
        offchain_secret_reveal_iteration.new_state,
        ContractReceiveSecretReveal(
            transaction_hash=factories.make_address(),
            secret_registry_address=factories.make_address(),
            secrethash=UNIT_SECRETHASH,
            secret=UNIT_SECRET,
            block_number=block_number_prior_the_expiration,
        ),
        channel_state,
        pseudo_random_generator,
        block_number_prior_the_expiration,
    )
    unlocked_onchain = channel_state.partner_state.secrethashes_to_onchain_unlockedlocks
    assert factories.UNIT_SECRETHASH in unlocked_onchain

    # Check that after we register a lock on-chain handling the block again will
    # not cause us to attempt an onchain re-register
    extra_block_handle_transition = target.handle_block(
        onchain_secret_reveal_iteration.new_state,
        channel_state,
        block_number_prior_the_expiration + 1,
    )
    assert len(extra_block_handle_transition.events) == 0
Пример #21
0
def test_state_transition():
    """ Happy case testing. """
    amount = 7
    block_number = 1
    initiator = factories.HOP6
    expire = block_number + factories.UNIT_REVEAL_TIMEOUT

    from_route, from_transfer = factories.make_from(
        amount,
        factories.ADDR,
        expire,
        initiator,
    )
    init = ActionInitTarget(
        factories.ADDR,
        from_route,
        from_transfer,
        block_number,
    )

    init_transition = target.state_transition(None, init)
    assert init_transition.new_state is not None
    assert init_transition.new_state.from_route == from_route
    assert init_transition.new_state.from_transfer == from_transfer

    first_new_block = Block(block_number + 1)
    first_block_iteration = target.state_transition(init_transition.new_state,
                                                    first_new_block)
    assert first_block_iteration.new_state.block_number == block_number + 1

    secret_reveal = ReceiveSecretReveal(factories.UNIT_SECRET, initiator)
    reveal_iteration = target.state_transition(first_block_iteration.new_state,
                                               secret_reveal)
    assert reveal_iteration.new_state.from_transfer.secret == factories.UNIT_SECRET

    second_new_block = Block(block_number + 2)
    second_block_iteration = target.state_transition(init_transition.new_state,
                                                     second_new_block)
    assert second_block_iteration.new_state.block_number == block_number + 2

    nonce = 11
    transferred_amount = 13
    locksroot = ''
    message_hash = ''
    balance_proof = ReceiveBalanceProof(
        from_transfer.identifier,
        from_route.node_address,
        nonce,
        transferred_amount,
        locksroot,
        from_route.channel_address,
        message_hash,
    )
    proof_iteration = target.state_transition(init_transition.new_state,
                                              balance_proof)
    assert proof_iteration.new_state is None
Пример #22
0
 def handle_message_revealsecret(raiden: RaidenService,
                                 message: RevealSecret,
                                 is_light_client=False) -> None:
     if is_light_client:
         state_change = ReceiveSecretRevealLight(message.secret,
                                                 message.sender, message)
         raiden.handle_and_track_state_change(state_change)
     else:
         state_change = ReceiveSecretReveal(message.secret, message.sender)
         raiden.handle_and_track_state_change(state_change)
Пример #23
0
def test_state_wait_unlock_valid():
    block_number = 1
    pseudo_random_generator = random.Random()

    channel1 = factories.make_channel(
        our_balance=UNIT_TRANSFER_AMOUNT,
        token_address=UNIT_TOKEN_ADDRESS,
        token_network_identifier=UNIT_TOKEN_NETWORK_ADDRESS,
    )
    channelmap = {channel1.identifier: channel1}
    available_routes = [factories.route_from_channel(channel1)]

    current_state = make_initiator_state(
        available_routes,
        factories.UNIT_TRANSFER_DESCRIPTION,
        channelmap,
        pseudo_random_generator,
        block_number,
    )

    # setup the state for the wait unlock
    current_state.initiator.revealsecret = SendRevealSecret(
        UNIT_TRANSFER_TARGET,
        'global',
        UNIT_TRANSFER_IDENTIFIER,
        UNIT_SECRET,
    )

    state_change = ReceiveSecretReveal(
        secret=UNIT_SECRET,
        sender=channel1.partner_state.address,
    )
    iteration = initiator_manager.state_transition(
        current_state,
        state_change,
        channelmap,
        pseudo_random_generator,
        block_number,
    )

    assert len(iteration.events) == 3
    assert any(isinstance(e, SendBalanceProof) for e in iteration.events)
    assert any(
        isinstance(e, EventTransferSentSuccess) for e in iteration.events)
    assert any(isinstance(e, EventUnlockSuccess) for e in iteration.events)

    balance_proof = next(e for e in iteration.events
                         if isinstance(e, SendBalanceProof))
    complete = next(e for e in iteration.events
                    if isinstance(e, EventTransferSentSuccess))

    assert balance_proof.recipient == channel1.partner_state.address
    assert complete.identifier == UNIT_TRANSFER_IDENTIFIER
    assert iteration.new_state is None, 'state must be cleaned'
Пример #24
0
    def message_revealsecret(self, message):
        secret = message.secret
        sender = message.sender

        self.raiden.greenlet_task_dispatcher.dispatch_message(
            message,
            message.hashlock,
        )
        self.raiden.register_secret(secret)

        state_change = ReceiveSecretReveal(secret, sender)
        self.raiden.state_machine_event_handler.dispatch_to_all_tasks(state_change)
Пример #25
0
def test_handle_offchain_secretreveal_after_lock_expired():
    """Test that getting the secret revealed after lock expiration for the
    target does not end up continuously emitting EventUnlockClaimFailed

    Target part for https://github.com/raiden-network/raiden/issues/3086
    """
    setup = make_target_state()

    lock_expiration = setup.new_state.transfer.lock.expiration
    lock_expiration_block_number = lock_expiration + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS * 2
    lock_expiration_block = Block(
        block_number=lock_expiration_block_number,
        gas_limit=1,
        block_hash=factories.make_transaction_hash(),
    )
    iteration = target.state_transition(
        target_state=setup.new_state,
        state_change=lock_expiration_block,
        channel_state=setup.channel,
        pseudo_random_generator=setup.pseudo_random_generator,
        block_number=lock_expiration_block_number,
    )
    state = iteration.new_state

    msg = 'At the expiration block we should get an EventUnlockClaimFailed'
    assert must_contain_entry(iteration.events, EventUnlockClaimFailed,
                              {}), msg

    iteration = target.state_transition(
        target_state=state,
        state_change=ReceiveSecretReveal(UNIT_SECRET, setup.initiator),
        channel_state=setup.channel,
        pseudo_random_generator=setup.pseudo_random_generator,
        block_number=lock_expiration_block_number + 1,
    )
    state = iteration.new_state

    next_block = Block(
        block_number=lock_expiration_block_number + 1,
        gas_limit=1,
        block_hash=factories.make_transaction_hash(),
    )
    iteration = target.state_transition(
        target_state=state,
        state_change=next_block,
        channel_state=setup.channel,
        pseudo_random_generator=setup.pseudo_random_generator,
        block_number=lock_expiration_block_number + 1,
    )
    msg = 'At the next block we should not get the same event'
    assert not must_contain_entry(iteration.events, EventUnlockClaimFailed,
                                  {}), msg
Пример #26
0
def test_handle_offchain_secretreveal_after_lock_expired():
    """Test that getting the secret revealed after lock expiration for the
    target does not end up continuously emitting EventUnlockClaimFailed

    Target part for https://github.com/raiden-network/raiden/issues/3086
    """
    setup = make_target_state()

    lock_expiration_block_number = channel.get_sender_expiration_threshold(
        setup.new_state.transfer.lock)
    lock_expiration_block = Block(
        block_number=lock_expiration_block_number,
        gas_limit=1,
        block_hash=factories.make_transaction_hash(),
    )
    iteration = target.state_transition(
        target_state=setup.new_state,
        state_change=lock_expiration_block,
        channel_state=setup.channel,
        pseudo_random_generator=setup.pseudo_random_generator,
        block_number=lock_expiration_block_number,
        storage=None)
    state = iteration.new_state

    msg = "At the expiration block we should get an EventUnlockClaimFailed"
    assert search_for_item(iteration.events, EventUnlockClaimFailed, {}), msg

    iteration = target.state_transition(
        target_state=state,
        state_change=ReceiveSecretReveal(UNIT_SECRET, setup.initiator),
        channel_state=setup.channel,
        pseudo_random_generator=setup.pseudo_random_generator,
        block_number=lock_expiration_block_number + 1,
        storage=None)
    state = iteration.new_state

    next_block = Block(
        block_number=lock_expiration_block_number + 1,
        gas_limit=1,
        block_hash=factories.make_transaction_hash(),
    )
    iteration = target.state_transition(
        target_state=state,
        state_change=next_block,
        channel_state=setup.channel,
        pseudo_random_generator=setup.pseudo_random_generator,
        block_number=lock_expiration_block_number + 1,
        storage=None)
    msg = "At the next block we should not get the same event"
    assert not search_for_item(iteration.events, EventUnlockClaimFailed,
                               {}), msg
Пример #27
0
def test_state_wait_unlock_invalid():
    identifier = identifier = 1
    amount = factories.UNIT_TRANSFER_AMOUNT
    block_number = 1
    mediator_address = factories.HOP1
    target_address = factories.HOP2
    our_address = factories.ADDR
    secret_generator = SequenceGenerator()
    token = factories.UNIT_TOKEN_ADDRESS

    routes = [factories.make_route(mediator_address, available_balance=amount)]
    current_state = make_initiator_state(
        routes,
        target_address,
        block_number=block_number,
        our_address=our_address,
        secret_generator=secret_generator,
        token=token,
    )

    secret = secret_generator.secrets[0]
    assert secret is not None

    current_state.revealsecret = SendRevealSecret(
        identifier,
        secret,
        token,
        target_address,
        our_address,
    )

    before_state = deepcopy(current_state)

    initiator_state_machine = StateManager(
        initiator.state_transition,
        current_state,
    )

    state_change = ReceiveSecretReveal(
        secret=secret,
        sender=factories.ADDR,
    )
    events = initiator_state_machine.dispatch(state_change)
    assert len(events) == 0
    assert initiator_state_machine.current_state.revealsecret is not None
    assert_state_equal(initiator_state_machine.current_state, current_state)
    assert_state_equal(current_state, before_state)
def test_handle_offchain_emptyhash_secret():
    setup = setup_initiator_tests(block_number=10)

    secret_reveal = ReceiveSecretReveal(
        secret=EMPTY_HASH,
        sender=setup.channel.partner_state.address,
    )
    iteration = initiator.handle_offchain_secretreveal(
        initiator_state=setup.current_state.initiator,
        state_change=secret_reveal,
        channel_state=setup.channel,
        pseudo_random_generator=setup.prng,
    )
    secrethash = factories.UNIT_TRANSFER_DESCRIPTION.secrethash
    assert len(iteration.events) == 0
    # make sure the lock has not moved
    assert secrethash in setup.channel.our_state.secrethashes_to_lockedlocks
Пример #29
0
def test_state_wait_unlock_invalid():
    identifier = 1
    block_number = 1
    target_address = factories.HOP2
    token = factories.UNIT_TOKEN_ADDRESS
    pseudo_random_generator = random.Random()

    channel1 = factories.make_channel(
        our_balance=UNIT_TRANSFER_AMOUNT,
        token_address=UNIT_TOKEN_ADDRESS,
    )
    channelmap = {channel1.identifier: channel1}
    available_routes = [factories.route_from_channel(channel1)]
    current_state = make_initiator_state(
        available_routes,
        factories.UNIT_TRANSFER_DESCRIPTION,
        channelmap,
        pseudo_random_generator,
        block_number,
    )

    # setup the state for the wait unlock
    current_state.initiator.revealsecret = SendRevealSecret(
        target_address,
        'global',
        identifier,
        UNIT_SECRET,
        token,
    )

    before_state = deepcopy(current_state)

    state_change = ReceiveSecretReveal(
        secret=UNIT_SECRET,
        sender=factories.ADDR,
    )
    iteration = initiator_manager.state_transition(
        current_state,
        state_change,
        channelmap,
        pseudo_random_generator,
        block_number,
    )

    assert not iteration.events
    assert iteration.new_state == before_state
Пример #30
0
def test_state_wait_unlock_invalid():
    identifier = 1
    block_number = 1
    target_address = factories.HOP2
    pseudo_random_generator = random.Random()

    channel1 = factories.make_channel(
        our_balance=UNIT_TRANSFER_AMOUNT,
        token_address=UNIT_TOKEN_ADDRESS,
        token_network_identifier=UNIT_TOKEN_NETWORK_ADDRESS,
    )
    channel_map = {channel1.identifier: channel1}
    available_routes = [factories.route_from_channel(channel1)]
    current_state = make_initiator_manager_state(
        available_routes,
        factories.UNIT_TRANSFER_DESCRIPTION,
        channel_map,
        pseudo_random_generator,
        block_number,
    )

    # setup the state for the wait unlock
    current_state.initiator.revealsecret = SendSecretReveal(
        recipient=target_address,
        channel_identifier=CHANNEL_IDENTIFIER_GLOBAL_QUEUE,
        message_identifier=identifier,
        secret=UNIT_SECRET,
    )

    before_state = deepcopy(current_state)

    state_change = ReceiveSecretReveal(
        secret=UNIT_SECRET,
        sender=factories.ADDR,
    )
    iteration = initiator_manager.state_transition(
        current_state,
        state_change,
        channel_map,
        pseudo_random_generator,
        block_number,
    )

    assert not iteration.events
    assert iteration.new_state == before_state