def get_netting_channel_deposit_events(
        chain: BlockChainService,
        token_network_address: Address,
        netting_channel_identifier: ChannelID,
        contract_manager: ContractManager,
        from_block: BlockSpecification = GENESIS_BLOCK_NUMBER,
        to_block: BlockSpecification = 'latest',
) -> List[Dict]:
    deposit_event_abi = contract_manager.get_event_abi(
        CONTRACT_TOKEN_NETWORK,
        ChannelEvent.DEPOSIT,
    )
    topic_set = construct_event_topic_set(
        event_abi=deposit_event_abi,
        arguments={'channel_identifier': netting_channel_identifier},
    )

    if len(topic_set) == 1 and is_list_like(topic_set[0]):
        topics = topic_set[0]
    else:
        topics = topic_set

    return get_contract_events(
        chain,
        contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK),
        token_network_address,
        topics,
        from_block,
        to_block,
    )
def get_netting_channel_closed_events(
    chain: BlockChainService,
    token_network_address: Address,
    netting_channel_identifier: ChannelID,
    contract_manager: ContractManager,
    from_block: BlockSpecification = 0,
    to_block: BlockSpecification = 'latest',
) -> List[Dict]:
    closed_event_abi = contract_manager.get_event_abi(
        CONTRACT_TOKEN_NETWORK,
        ChannelEvent.CLOSED,
    )

    topic_set = construct_event_topic_set(
        event_abi=closed_event_abi,
        arguments={'channel_identifier': netting_channel_identifier},
    )

    if len(topic_set) == 1 and is_list_like(topic_set[0]):
        topics = topic_set[0]
    else:
        topics = topic_set

    return get_contract_events(
        chain=chain,
        abi=contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK),
        contract_address=token_network_address,
        topics=topics,
        from_block=from_block,
        to_block=to_block,
    )
def get_netting_channel_deposit_events(
        chain: BlockChainService,
        token_network_address: Address,
        netting_channel_identifier: ChannelID,
        contract_manager: ContractManager,
        from_block: BlockSpecification = GENESIS_BLOCK_NUMBER,
        to_block: BlockSpecification = 'latest',
) -> List[Dict]:
    deposit_event_abi = contract_manager.get_event_abi(
        CONTRACT_TOKEN_NETWORK,
        ChannelEvent.DEPOSIT,
    )
    topic_set = construct_event_topic_set(
        event_abi=deposit_event_abi,
        arguments={'channel_identifier': netting_channel_identifier},
    )

    if len(topic_set) == 1 and is_list_like(topic_set[0]):
        topics = topic_set[0]
    else:
        topics = topic_set

    return get_contract_events(
        chain,
        contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK),
        token_network_address,
        topics,
        from_block,
        to_block,
    )
Exemple #4
0
def get_netting_channel_closed_events(
    proxy_manager: ProxyManager,
    token_network_address: TokenNetworkAddress,
    netting_channel_identifier: ChannelID,
    contract_manager: ContractManager,
    from_block: BlockIdentifier = GENESIS_BLOCK_NUMBER,
    to_block: BlockIdentifier = BLOCK_ID_LATEST,
) -> List[Dict]:
    closed_event_abi = contract_manager.get_event_abi(CONTRACT_TOKEN_NETWORK,
                                                      ChannelEvent.CLOSED)

    topic_set = construct_event_topic_set(
        event_abi=closed_event_abi,
        abi_codec=proxy_manager.client.web3.codec,
        arguments={"channel_identifier": netting_channel_identifier},
    )

    return get_contract_events(
        proxy_manager=proxy_manager,
        abi=contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK),
        contract_address=Address(token_network_address),
        topics=topic_set,  # type: ignore
        from_block=from_block,
        to_block=to_block,
    )
Exemple #5
0
def secret_registry_batch_happy_path(
    proxy_manager: ProxyManager, secret_registry_proxy: SecretRegistry
) -> None:
    secrets = [make_secret() for i in range(4)]
    secrethashes = [sha256_secrethash(secret) for secret in secrets]

    transaction_hashes = secret_registry_proxy.register_secret_batch(secrets=secrets)
    for tx_hash in transaction_hashes:
        assert is_tx_hash_bytes(tx_hash)

    logs = get_contract_events(
        proxy_manager=proxy_manager,
        abi=proxy_manager.contract_manager.get_contract_abi(CONTRACT_SECRET_REGISTRY),
        contract_address=Address(secret_registry_proxy.address),
    )

    for secrethash in secrethashes:
        secret_registered = must_have_event(
            logs, {"event": "SecretRevealed", "args": {"secrethash": secrethash}}
        )
        assert secret_registered, "All secrets from the batch must be registered"

        block = secret_registry_proxy.get_secret_registration_block_by_secrethash(
            secrethash=secrethash, block_identifier=BLOCK_ID_LATEST
        )
        msg = "Block number reported by the proxy and the event must match"
        assert block == secret_registered["block_number"], msg
Exemple #6
0
def get_netting_channel_settled_events(
        chain: BlockChainService,
        token_network_address: Address,
        netting_channel_identifier: ChannelID,
        from_block: BlockSpecification = 0,
        to_block: BlockSpecification = 'latest',
) -> List[Dict]:
    settled_event_abi = CONTRACT_MANAGER.get_event_abi(
        CONTRACT_TOKEN_NETWORK,
        ChannelEvent.SETTLED,
    )
    topic_set = construct_event_topic_set(
        event_abi=settled_event_abi,
        arguments={'channel_identifier': netting_channel_identifier},
    )

    if len(topic_set) == 1 and is_list_like(topic_set[0]):
        topics = topic_set[0]
    else:
        topics = topic_set

    return get_contract_events(
        chain,
        CONTRACT_MANAGER.get_contract_abi(CONTRACT_TOKEN_NETWORK),
        token_network_address,
        topics,
        from_block,
        to_block,
    )
def get_netting_channel_deposit_events(
    proxy_manager: ProxyManager,
    token_network_address: TokenNetworkAddress,
    netting_channel_identifier: ChannelID,
    contract_manager: ContractManager,
    from_block: BlockSpecification = GENESIS_BLOCK_NUMBER,
    to_block: BlockSpecification = "latest",
) -> List[Dict]:
    deposit_event_abi = contract_manager.get_event_abi(CONTRACT_TOKEN_NETWORK,
                                                       ChannelEvent.DEPOSIT)
    topic_set = construct_event_topic_set(
        event_abi=deposit_event_abi,
        arguments={"channel_identifier": netting_channel_identifier})

    if len(topic_set) == 1 and is_list_like(topic_set[0]):
        topics = topic_set[0]
    else:
        topics = topic_set

    return get_contract_events(
        proxy_manager,
        contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK),
        Address(token_network_address),
        topics,
        from_block,
        to_block,
    )
Exemple #8
0
def test_get_contract_events_invalid_blocknumber():
    with pytest.raises(InvalidBlockNumberInput):
        get_contract_events(None, {}, ADDR, [], -1, 0)

    with pytest.raises(InvalidBlockNumberInput):
        get_contract_events(None, {}, ADDR, [], 999999999999999999999999, 0)

    with pytest.raises(InvalidBlockNumberInput):
        get_contract_events(None, {}, ADDR, [], 1, -1)

    with pytest.raises(InvalidBlockNumberInput):
        get_contract_events(None, {}, ADDR, [], 1, 999999999999999999999999)
Exemple #9
0
def test_register_secret_happy_path(
    secret_registry_proxy: SecretRegistry, contract_manager: ContractManager
) -> None:
    """Test happy path of SecretRegistry with a single secret.

    Test that `register_secret` changes the smart contract state by registering
    the secret, this can be verified by the block height and the existence of
    the SecretRegistered event.
    """
    secret = make_secret()
    secrethash = sha256_secrethash(secret)
    secret_unregistered = make_secret()
    secrethash_unregistered = sha256_secrethash(secret_unregistered)

    assert not secret_registry_proxy.is_secret_registered(
        secrethash=secrethash, block_identifier=BLOCK_ID_LATEST
    ), "Test setup is invalid, secret must be unknown"
    assert not secret_registry_proxy.is_secret_registered(
        secrethash=secrethash_unregistered, block_identifier=BLOCK_ID_LATEST
    ), "Test setup is invalid, secret must be unknown"

    secret_registry_proxy.client.wait_until_block(BlockNumber(STATE_PRUNING_AFTER_BLOCKS + 1))

    with pytest.raises(NoStateForBlockIdentifier):
        secret_registry_proxy.is_secret_registered(
            secrethash=secrethash_unregistered, block_identifier=BlockNumber(0)
        )

    secret_registry_proxy.register_secret(secret=secret)

    proxy_manager = ProxyManager(
        rpc_client=secret_registry_proxy.client,
        contract_manager=contract_manager,
        metadata=ProxyManagerMetadata(
            token_network_registry_deployed_at=GENESIS_BLOCK_NUMBER,
            filters_start_at=GENESIS_BLOCK_NUMBER,
        ),
    )
    logs = get_contract_events(
        proxy_manager=proxy_manager,
        abi=proxy_manager.contract_manager.get_contract_abi(CONTRACT_SECRET_REGISTRY),
        contract_address=Address(secret_registry_proxy.address),
    )
    secret_registered = must_have_event(
        logs, {"event": "SecretRevealed", "args": {"secrethash": secrethash}}
    )

    msg = "SecretRegistry.register_secret returned but the SecretRevealed event was not emitted."
    assert secret_registered, msg

    registered_block = secret_registry_proxy.get_secret_registration_block_by_secrethash(
        secrethash=secrethash, block_identifier=BLOCK_ID_LATEST
    )
    msg = (
        "Block height returned by the SecretRegistry.get_secret_registration_block_by_secrethash "
        "does not match the block from the SecretRevealed event."
    )
    assert secret_registered["block_number"] == registered_block, msg

    block = secret_registry_proxy.get_secret_registration_block_by_secrethash(
        secrethash=secrethash_unregistered, block_identifier=BLOCK_ID_LATEST
    )
    assert block is None, "The secret that was not registered must not change block height!"
def test_query_events(
    raiden_chain: List[RaidenService],
    token_addresses,
    deposit,
    settle_timeout,
    retry_timeout,
    contract_manager,
    blockchain_type,
):
    app0, app1 = raiden_chain  # pylint: disable=unbalanced-tuple-unpacking
    registry_address = app0.default_registry.address
    token_address = token_addresses[0]

    token_network_address = app0.default_registry.get_token_network(
        token_address, BLOCK_ID_LATEST)

    assert token_network_address
    manager0 = app0.proxy_manager.token_network(token_network_address,
                                                BLOCK_ID_LATEST)

    channelcount0 = views.total_token_network_channels(
        views.state_from_raiden(app0), registry_address, token_address)

    events = get_contract_events(
        proxy_manager=app0.proxy_manager,
        abi=contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK_REGISTRY),
        contract_address=Address(registry_address),
    )

    assert must_have_event(
        events,
        {
            "event": EVENT_TOKEN_NETWORK_CREATED,
            "args": {
                "token_network_address": to_checksum_address(manager0.address),
                "token_address": to_checksum_address(token_address),
            },
        },
    )

    if blockchain_type == "geth":
        # FIXME: This is apparently meant to verify that querying nonexisting blocks
        # returns an empty list, which is not true for parity.
        events = get_contract_events(
            proxy_manager=app0.proxy_manager,
            abi=contract_manager.get_contract_abi(
                CONTRACT_TOKEN_NETWORK_REGISTRY),
            contract_address=Address(app0.default_registry.address),
            from_block=BlockNumber(999999998),
            to_block=BlockNumber(999999999),
        )
        assert not events

    RaidenAPI(app0).channel_open(registry_address, token_address, app1.address)

    wait_both_channel_open(app0, app1, registry_address, token_address,
                           retry_timeout)

    events = get_contract_events(
        proxy_manager=app0.proxy_manager,
        abi=contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK),
        contract_address=Address(manager0.address),
    )

    _event = must_have_event(
        events,
        {
            "event": ChannelEvent.OPENED,
            "args": {
                "participant1": to_checksum_address(app0.address),
                "participant2": to_checksum_address(app1.address),
                "settle_timeout": settle_timeout,
            },
        },
    )
    assert _event
    channel_id = _event["args"]["channel_identifier"]

    if blockchain_type == "geth":
        # see above
        events = get_contract_events(
            proxy_manager=app0.proxy_manager,
            abi=contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK),
            contract_address=Address(manager0.address),
            from_block=BlockNumber(999999998),
            to_block=BlockNumber(999999999),
        )
        assert not events

    # channel is created but not opened and without funds
    channelcount1 = views.total_token_network_channels(
        views.state_from_raiden(app0), registry_address, token_address)
    assert channelcount0 + 1 == channelcount1

    assert_synced_channel_state(token_network_address, app0, Balance(0), [],
                                app1, Balance(0), [])

    RaidenAPI(app0).set_total_channel_deposit(registry_address, token_address,
                                              app1.address, deposit)

    all_netting_channel_events = get_all_netting_channel_events(
        proxy_manager=app0.proxy_manager,
        token_network_address=token_network_address,
        netting_channel_identifier=channel_id,
        contract_manager=app0.contract_manager,
    )

    deposit_events = get_netting_channel_deposit_events(
        proxy_manager=app0.proxy_manager,
        token_network_address=token_network_address,
        netting_channel_identifier=channel_id,
        contract_manager=contract_manager,
    )

    total_deposit_event = {
        "event": ChannelEvent.DEPOSIT,
        "args": {
            "participant": to_checksum_address(app0.address),
            "total_deposit": deposit,
            "channel_identifier": channel_id,
        },
    }
    assert must_have_event(deposit_events, total_deposit_event)
    assert must_have_event(all_netting_channel_events, total_deposit_event)

    RaidenAPI(app0).channel_close(registry_address, token_address,
                                  app1.address)

    all_netting_channel_events = get_all_netting_channel_events(
        proxy_manager=app0.proxy_manager,
        token_network_address=token_network_address,
        netting_channel_identifier=channel_id,
        contract_manager=app0.contract_manager,
    )

    closed_events = get_netting_channel_closed_events(
        proxy_manager=app0.proxy_manager,
        token_network_address=token_network_address,
        netting_channel_identifier=channel_id,
        contract_manager=contract_manager,
    )

    closed_event = {
        "event": ChannelEvent.CLOSED,
        "args": {
            "channel_identifier": channel_id,
            "closing_participant": to_checksum_address(app0.address),
        },
    }
    assert must_have_event(closed_events, closed_event)
    assert must_have_event(all_netting_channel_events, closed_event)

    settle_expiration = app0.rpc_client.block_number() + settle_timeout + 5
    app0.proxy_manager.client.wait_until_block(
        target_block_number=settle_expiration)

    all_netting_channel_events = get_all_netting_channel_events(
        proxy_manager=app0.proxy_manager,
        token_network_address=token_network_address,
        netting_channel_identifier=channel_id,
        contract_manager=app0.contract_manager,
    )

    settled_events = get_netting_channel_settled_events(
        proxy_manager=app0.proxy_manager,
        token_network_address=token_network_address,
        netting_channel_identifier=channel_id,
        contract_manager=contract_manager,
    )

    settled_event = {
        "event": ChannelEvent.SETTLED,
        "args": {
            "channel_identifier": channel_id
        }
    }
    assert must_have_event(settled_events, settled_event)
    assert must_have_event(all_netting_channel_events, settled_event)