Exemplo n.º 1
0
def test_inplace_delete_message_queue(chain_state):
    sender = factories.make_address()
    canonical_identifier = factories.make_canonical_identifier()
    message_id = factories.make_message_identifier()
    delivered_state_change = ReceiveDelivered(sender=sender,
                                              message_identifier=message_id)
    processed_state_change = ReceiveProcessed(sender=sender,
                                              message_identifier=message_id)

    global_identifier = QueueIdentifier(
        recipient=sender,
        canonical_identifier=CANONICAL_IDENTIFIER_GLOBAL_QUEUE)

    chain_state.queueids_to_queues[global_identifier] = None
    assert global_identifier in chain_state.queueids_to_queues, "queue mapping insertion failed"
    inplace_delete_message_queue(chain_state=chain_state,
                                 state_change=delivered_state_change,
                                 queueid=global_identifier)
    assert global_identifier not in chain_state.queueids_to_queues, "did not clear queue"

    chain_state.queueids_to_queues[global_identifier] = [
        SendMessageEvent(
            recipient=sender,
            canonical_identifier=canonical_identifier,
            message_identifier=message_id,
        )
    ]
    assert global_identifier in chain_state.queueids_to_queues, "queue mapping insertion failed"
    handle_receive_delivered(chain_state=chain_state,
                             state_change=delivered_state_change)
    assert global_identifier not in chain_state.queueids_to_queues, "did not clear queue"

    queue_identifier = QueueIdentifier(
        recipient=sender, canonical_identifier=canonical_identifier)
    assert queue_identifier not in chain_state.queueids_to_queues, "queue not empty"
    chain_state.queueids_to_queues[queue_identifier] = [
        SendMessageEvent(
            recipient=sender,
            canonical_identifier=canonical_identifier,
            message_identifier=message_id,
        )
    ]
    assert queue_identifier in chain_state.queueids_to_queues, "queue mapping not mutable"
    handle_receive_processed(chain_state=chain_state,
                             state_change=processed_state_change)
    assert queue_identifier not in chain_state.queueids_to_queues, "queue did not clear"
Exemplo n.º 2
0
def test_delivered_message_must_clean_unordered_messages(chain_id):
    pseudo_random_generator = random.Random()
    block_number = 10
    our_address = factories.make_address()
    recipient = factories.make_address()
    canonical_identifier = factories.make_canonical_identifier()
    message_identifier = random.randint(0, 2**16)
    secret = factories.random_secret()

    chain_state = state.ChainState(
        pseudo_random_generator=pseudo_random_generator,
        block_number=block_number,
        block_hash=factories.make_block_hash(),
        our_address=our_address,
        chain_id=chain_id,
    )
    queue_identifier = QueueIdentifier(
        recipient=recipient,
        canonical_identifier=CANONICAL_IDENTIFIER_GLOBAL_QUEUE)

    # Regression test:
    # The code delivered_message handler worked only with a queue of one
    # element
    first_message = SendSecretReveal(
        recipient=recipient,
        message_identifier=message_identifier,
        secret=secret,
        canonical_identifier=canonical_identifier,
    )

    second_message = SendSecretReveal(
        recipient=recipient,
        message_identifier=random.randint(0, 2**16),
        secret=secret,
        canonical_identifier=canonical_identifier,
    )

    chain_state.queueids_to_queues[queue_identifier] = [
        first_message, second_message
    ]

    delivered_message = state_change.ReceiveDelivered(recipient,
                                                      message_identifier)

    iteration = node.handle_receive_delivered(chain_state, delivered_message)
    new_queue = iteration.new_state.queueids_to_queues.get(
        queue_identifier, [])

    assert first_message not in new_queue