Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ethPM] Update Infura strategy #1383

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ jobs:
- image: circleci/python:3.6
environment:
TOXENV: py36-ethpm
WEB3_INFURA_PROJECT_ID: 4f1a358967c7474aae6f8f4a7698aefc

py36-integration-goethereum-ipc-1.7.2:
<<: *geth_steps
Expand Down Expand Up @@ -285,6 +286,7 @@ jobs:
- image: circleci/python:3.7
environment:
TOXENV: py37-ethpm
WEB3_INFURA_PROJECT_ID: 4f1a358967c7474aae6f8f4a7698aefc

py37-integration-goethereum-ipc-1.7.2:
<<: *geth_steps
Expand Down
13 changes: 2 additions & 11 deletions ethpm/backends/registry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from collections import (
namedtuple,
)
import os
from urllib import (
parse,
)
Expand All @@ -16,20 +15,13 @@
from ethpm.backends.base import (
BaseURIBackend,
)
from ethpm.constants import (
INFURA_API_KEY,
)
from ethpm.exceptions import (
CannotHandleURI,
ValidationError,
)
from ethpm.validation.uri import (
validate_registry_uri,
)
from web3 import Web3
from web3.providers.auto import (
load_provider_from_uri,
)

# TODO: Update registry ABI once ERC is finalized.
REGISTRY_ABI = fetch_standard_registry_abi()
Expand All @@ -44,9 +36,8 @@ class RegistryURIBackend(BaseURIBackend):
"""

def __init__(self) -> None:
os.environ.setdefault("INFUFA_API_KEY", INFURA_API_KEY)
infura_url = f"wss://mainnet.infura.io/ws/v3/{INFURA_API_KEY}"
self.w3 = Web3(load_provider_from_uri(infura_url))
from web3.auto.infura import w3
self.w3 = w3

def can_translate_uri(self, uri: str) -> bool:
return is_valid_registry_uri(uri)
Expand Down
4 changes: 0 additions & 4 deletions ethpm/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

IPFS_GATEWAY_PREFIX = "https://ipfs.io/ipfs/"

# TODO Deprecate in favor of a better scheme for fetching registry URIs.
# Please play nice and don't use this key for any shenanigans, thanks!
INFURA_API_KEY = "4f1a358967c7474aae6f8f4a7698aefc"

INFURA_GATEWAY_MULTIADDR = "/dns4/ipfs.infura.io/tcp/5001/https/"

GITHUB_API_AUTHORITY = "api.github.com"
Expand Down
4 changes: 4 additions & 0 deletions tests/ethpm/_utils/test_backend_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pytest

from ethpm._utils.backend import (
Expand Down Expand Up @@ -29,6 +30,7 @@
("erc1319://packages.zeppelinos.eth:1/erc20?version=1.0.0", ()),
),
)
@pytest.mark.skipif('WEB3_INFURA_PROJECT_ID' not in os.environ, reason='Infura API key unavailable')
def test_get_resolvable_backends_for_supported_uris(dummy_ipfs_backend, uri, backends):
good_backends = get_resolvable_backends_for_uri(uri)
assert good_backends == backends
Expand All @@ -41,6 +43,7 @@ def test_get_resolvable_backends_for_supported_uris(dummy_ipfs_backend, uri, bac
("ipfs://QmTKB75Y73zhNbD3Y73xeXGjYrZHmaXXNxoZqGCagu7r8u/", ()),
),
)
@pytest.mark.skipif('WEB3_INFURA_PROJECT_ID' not in os.environ, reason='Infura API key unavailable')
def test_get_translatable_backends_for_supported_uris(
dummy_ipfs_backend, uri, backends
):
Expand All @@ -65,6 +68,7 @@ def test_get_translatable_backends_for_supported_uris(
"https://github.com/ethpm/ethpm-spec/examples/owned/1.0.0.json#content_hash",
),
)
@pytest.mark.skipif('WEB3_INFURA_PROJECT_ID' not in os.environ, reason='Infura API key unavailable')
def test_resolve_uri_contents_raises_exception_for_unsupported_schemes(uri):
with pytest.raises(CannotHandleURI):
resolve_uri_contents(uri)
3 changes: 3 additions & 0 deletions tests/ethpm/backends/test_http_backends.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pytest

from requests.exceptions import (
Expand All @@ -21,6 +22,7 @@
"https://api.github.com/repos/ethpm/py-ethpm/git/blobs/a7232a93f1e9e75d606f6c1da18aa16037e03480", # noqa: E501
),
)
@pytest.mark.skipif('WEB3_INFURA_PROJECT_ID' not in os.environ, reason='Infura API key unavailable')
def test_github_over_https_backend_fetch_uri_contents(uri, owned_contract, w3):
# these tests may occassionally fail CI as a result of their network requests
backend = GithubOverHTTPSBackend()
Expand All @@ -30,6 +32,7 @@ def test_github_over_https_backend_fetch_uri_contents(uri, owned_contract, w3):
assert owned_package.name == "owned"


@pytest.mark.skipif('WEB3_INFURA_PROJECT_ID' not in os.environ, reason='Infura API key unavailable')
def test_github_over_https_backend_raises_error_with_invalid_content_hash(w3):
invalid_uri = "https://api.github.com/repos/ethpm/py-ethpm/git/blobs/a7232a93f1e9e75d606f6c1da18aa16037e03123" # noqa: E501
with pytest.raises(HTTPError):
Expand Down
3 changes: 3 additions & 0 deletions tests/ethpm/backends/test_registry_backend.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pytest

from ethpm.backends.registry import (
Expand All @@ -13,6 +14,7 @@ def backend():
return RegistryURIBackend()


@pytest.mark.skipif('WEB3_INFURA_PROJECT_ID' not in os.environ, reason='Infura API key unavailable')
def test_registry_uri_backend(backend):
valid_uri = "erc1319://snakecharmers.eth:1/owned?version=1.0.0"
expected_uri = 'ipfs://QmbeVyFLSuEUxiXKwSsEjef6icpdTdA4kGG9BcrJXKNKUW'
Expand All @@ -21,6 +23,7 @@ def test_registry_uri_backend(backend):
assert backend.fetch_uri_contents(valid_uri) == expected_uri


@pytest.mark.skipif('WEB3_INFURA_PROJECT_ID' not in os.environ, reason='Infura API key unavailable')
def test_registry_uri_backend_raises_exception_for_non_mainnet_chains(backend):
ropsten_uri = "erc1319://snakecharmers.eth:3/owned?version=1.0.0"
with pytest.raises(CannotHandleURI, match="Currently only mainnet"):
Expand Down
2 changes: 2 additions & 0 deletions tests/ethpm/test_package_init_from_registry_uri.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pytest

from ethpm import (
Expand All @@ -15,6 +16,7 @@
"bzz://da6adeeb4589d8652bbe5679aae6b6409ec85a20e92a8823c7c99e25dba9493d",
),
)
@pytest.mark.skipif('WEB3_INFURA_PROJECT_ID' not in os.environ, reason='Infura API key unavailable')
def test_package_init_with_unsupported_uris_raises_exception(uri, w3):
with pytest.raises(CannotHandleURI):
Package.from_uri(uri, w3)
2 changes: 2 additions & 0 deletions tests/ethpm/test_package_init_from_uri.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pytest

from ethpm import (
Expand Down Expand Up @@ -26,6 +27,7 @@ def test_package_from_uri_with_valid_uri(dummy_ipfs_backend, w3):
"ipfsQmTKB75Y73zhNbD3Y73xeXGjYrZHmaXXNxoZqGCagu7r8u/readme/",
),
)
@pytest.mark.skipif('WEB3_INFURA_PROJECT_ID' not in os.environ, reason='Infura API key unavailable')
def test_package_from_uri_rejects_invalid_ipfs_uri(invalid, w3):
with pytest.raises(CannotHandleURI):
Package.from_uri(invalid, w3)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ passenv =
PARITY_OS
GOROOT
GOPATH
WEB3_INFURA_PROJECT_ID
basepython =
doctest: python3.6
py36: python3.6
Expand Down