From c4e93c2ed77a54e28c5f8ad0815db7ed1aa7bf25 Mon Sep 17 00:00:00 2001 From: Nick Gheorghita Date: Fri, 12 Jul 2019 12:06:43 -0500 Subject: [PATCH 1/3] Switch ethpm to use web3s infura strategy --- ethpm/backends/registry.py | 15 +++++++++------ ethpm/constants.py | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/ethpm/backends/registry.py b/ethpm/backends/registry.py index e815835b2b..9e6846227e 100644 --- a/ethpm/backends/registry.py +++ b/ethpm/backends/registry.py @@ -17,7 +17,7 @@ BaseURIBackend, ) from ethpm.constants import ( - INFURA_API_KEY, + ETHPM_INFURA_API_KEY, ) from ethpm.exceptions import ( CannotHandleURI, @@ -27,8 +27,8 @@ validate_registry_uri, ) from web3 import Web3 -from web3.providers.auto import ( - load_provider_from_uri, +from web3.exceptions import ( + InfuraKeyNotFound, ) # TODO: Update registry ABI once ERC is finalized. @@ -44,9 +44,12 @@ 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)) + try: + from web3.auto.infura.endpoints import load_api_key + except InfuraKeyNotFound: + os.environ['WEB3_INFURA_PROJECT_ID'] = ETHPM_INFURA_API_KEY + from web3.auto.infura import w3 + self.w3 = w3 def can_translate_uri(self, uri: str) -> bool: return is_valid_registry_uri(uri) diff --git a/ethpm/constants.py b/ethpm/constants.py index 5b862e9aea..42ed413d18 100644 --- a/ethpm/constants.py +++ b/ethpm/constants.py @@ -8,7 +8,7 @@ # 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" +ETHPM_INFURA_API_KEY = "4f1a358967c7474aae6f8f4a7698aefc" INFURA_GATEWAY_MULTIADDR = "/dns4/ipfs.infura.io/tcp/5001/https/" From 730a8f2bc2fdb2283afa424d85f167b28ab007ba Mon Sep 17 00:00:00 2001 From: Nick Gheorghita Date: Fri, 12 Jul 2019 12:25:07 -0500 Subject: [PATCH 2/3] Move infura key to monkeypatch env --- ethpm/backends/registry.py | 12 ------------ ethpm/constants.py | 4 ---- tests/ethpm/_utils/test_backend_utils.py | 4 ++-- tests/ethpm/backends/test_http_backends.py | 4 ++-- tests/ethpm/conftest.py | 6 ++++++ tests/ethpm/test_package_init_from_registry_uri.py | 2 +- tests/ethpm/test_package_init_from_uri.py | 2 +- 7 files changed, 12 insertions(+), 22 deletions(-) diff --git a/ethpm/backends/registry.py b/ethpm/backends/registry.py index 9e6846227e..8c26252bc5 100644 --- a/ethpm/backends/registry.py +++ b/ethpm/backends/registry.py @@ -1,7 +1,6 @@ from collections import ( namedtuple, ) -import os from urllib import ( parse, ) @@ -16,9 +15,6 @@ from ethpm.backends.base import ( BaseURIBackend, ) -from ethpm.constants import ( - ETHPM_INFURA_API_KEY, -) from ethpm.exceptions import ( CannotHandleURI, ValidationError, @@ -26,10 +22,6 @@ from ethpm.validation.uri import ( validate_registry_uri, ) -from web3 import Web3 -from web3.exceptions import ( - InfuraKeyNotFound, -) # TODO: Update registry ABI once ERC is finalized. REGISTRY_ABI = fetch_standard_registry_abi() @@ -44,10 +36,6 @@ class RegistryURIBackend(BaseURIBackend): """ def __init__(self) -> None: - try: - from web3.auto.infura.endpoints import load_api_key - except InfuraKeyNotFound: - os.environ['WEB3_INFURA_PROJECT_ID'] = ETHPM_INFURA_API_KEY from web3.auto.infura import w3 self.w3 = w3 diff --git a/ethpm/constants.py b/ethpm/constants.py index 42ed413d18..adad08914b 100644 --- a/ethpm/constants.py +++ b/ethpm/constants.py @@ -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! -ETHPM_INFURA_API_KEY = "4f1a358967c7474aae6f8f4a7698aefc" - INFURA_GATEWAY_MULTIADDR = "/dns4/ipfs.infura.io/tcp/5001/https/" GITHUB_API_AUTHORITY = "api.github.com" diff --git a/tests/ethpm/_utils/test_backend_utils.py b/tests/ethpm/_utils/test_backend_utils.py index 4cc03134a5..f6804f6059 100644 --- a/tests/ethpm/_utils/test_backend_utils.py +++ b/tests/ethpm/_utils/test_backend_utils.py @@ -42,7 +42,7 @@ def test_get_resolvable_backends_for_supported_uris(dummy_ipfs_backend, uri, bac ), ) def test_get_translatable_backends_for_supported_uris( - dummy_ipfs_backend, uri, backends + dummy_ipfs_backend, uri, backends, infura_env ): good_backends = get_translatable_backends_for_uri(uri) assert good_backends == backends @@ -65,6 +65,6 @@ def test_get_translatable_backends_for_supported_uris( "https://github.com/ethpm/ethpm-spec/examples/owned/1.0.0.json#content_hash", ), ) -def test_resolve_uri_contents_raises_exception_for_unsupported_schemes(uri): +def test_resolve_uri_contents_raises_exception_for_unsupported_schemes(uri, infura_env): with pytest.raises(CannotHandleURI): resolve_uri_contents(uri) diff --git a/tests/ethpm/backends/test_http_backends.py b/tests/ethpm/backends/test_http_backends.py index c0a3deddcf..616fa8265d 100644 --- a/tests/ethpm/backends/test_http_backends.py +++ b/tests/ethpm/backends/test_http_backends.py @@ -21,7 +21,7 @@ "https://api.github.com/repos/ethpm/py-ethpm/git/blobs/a7232a93f1e9e75d606f6c1da18aa16037e03480", # noqa: E501 ), ) -def test_github_over_https_backend_fetch_uri_contents(uri, owned_contract, w3): +def test_github_over_https_backend_fetch_uri_contents(uri, owned_contract, w3, infura_env): # these tests may occassionally fail CI as a result of their network requests backend = GithubOverHTTPSBackend() assert backend.base_uri == GITHUB_API_AUTHORITY @@ -30,7 +30,7 @@ def test_github_over_https_backend_fetch_uri_contents(uri, owned_contract, w3): assert owned_package.name == "owned" -def test_github_over_https_backend_raises_error_with_invalid_content_hash(w3): +def test_github_over_https_backend_raises_error_with_invalid_content_hash(w3, infura_env): invalid_uri = "https://api.github.com/repos/ethpm/py-ethpm/git/blobs/a7232a93f1e9e75d606f6c1da18aa16037e03123" # noqa: E501 with pytest.raises(HTTPError): Package.from_uri(invalid_uri, w3) diff --git a/tests/ethpm/conftest.py b/tests/ethpm/conftest.py index cd5323f539..7e0983bb56 100644 --- a/tests/ethpm/conftest.py +++ b/tests/ethpm/conftest.py @@ -40,6 +40,12 @@ def pytest_addoption(parser): parser.addoption("--integration", action="store_true", default=False) +@pytest.fixture +def infura_env(monkeypatch): + # Please play nice and don't use this key for any shenanigans, thanks! + monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", '4f1a358967c7474aae6f8f4a7698aefc') + + @pytest.fixture def package_names(): return PACKAGE_NAMES diff --git a/tests/ethpm/test_package_init_from_registry_uri.py b/tests/ethpm/test_package_init_from_registry_uri.py index 29491c67d2..a86813c39c 100644 --- a/tests/ethpm/test_package_init_from_registry_uri.py +++ b/tests/ethpm/test_package_init_from_registry_uri.py @@ -15,6 +15,6 @@ "bzz://da6adeeb4589d8652bbe5679aae6b6409ec85a20e92a8823c7c99e25dba9493d", ), ) -def test_package_init_with_unsupported_uris_raises_exception(uri, w3): +def test_package_init_with_unsupported_uris_raises_exception(uri, w3, infura_env): with pytest.raises(CannotHandleURI): Package.from_uri(uri, w3) diff --git a/tests/ethpm/test_package_init_from_uri.py b/tests/ethpm/test_package_init_from_uri.py index ce00f23cd6..77c071e358 100644 --- a/tests/ethpm/test_package_init_from_uri.py +++ b/tests/ethpm/test_package_init_from_uri.py @@ -26,6 +26,6 @@ def test_package_from_uri_with_valid_uri(dummy_ipfs_backend, w3): "ipfsQmTKB75Y73zhNbD3Y73xeXGjYrZHmaXXNxoZqGCagu7r8u/readme/", ), ) -def test_package_from_uri_rejects_invalid_ipfs_uri(invalid, w3): +def test_package_from_uri_rejects_invalid_ipfs_uri(invalid, w3, infura_env): with pytest.raises(CannotHandleURI): Package.from_uri(invalid, w3) From 4fed5557030779cca100b030524a982a7d949d92 Mon Sep 17 00:00:00 2001 From: Nick Gheorghita Date: Fri, 12 Jul 2019 17:00:43 -0500 Subject: [PATCH 3/3] Remove infura_env fixture, and use pytests skipif to detect env var --- .circleci/config.yml | 2 ++ tests/ethpm/_utils/test_backend_utils.py | 8 ++++++-- tests/ethpm/backends/test_http_backends.py | 7 +++++-- tests/ethpm/backends/test_registry_backend.py | 3 +++ tests/ethpm/conftest.py | 6 ------ tests/ethpm/test_package_init_from_registry_uri.py | 4 +++- tests/ethpm/test_package_init_from_uri.py | 4 +++- tox.ini | 1 + 8 files changed, 23 insertions(+), 12 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 26c6f4c2db..6984048577 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 @@ -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 diff --git a/tests/ethpm/_utils/test_backend_utils.py b/tests/ethpm/_utils/test_backend_utils.py index f6804f6059..c5909660b8 100644 --- a/tests/ethpm/_utils/test_backend_utils.py +++ b/tests/ethpm/_utils/test_backend_utils.py @@ -1,3 +1,4 @@ +import os import pytest from ethpm._utils.backend import ( @@ -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 @@ -41,8 +43,9 @@ 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, infura_env + dummy_ipfs_backend, uri, backends ): good_backends = get_translatable_backends_for_uri(uri) assert good_backends == backends @@ -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", ), ) -def test_resolve_uri_contents_raises_exception_for_unsupported_schemes(uri, infura_env): +@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) diff --git a/tests/ethpm/backends/test_http_backends.py b/tests/ethpm/backends/test_http_backends.py index 616fa8265d..eb4df32a82 100644 --- a/tests/ethpm/backends/test_http_backends.py +++ b/tests/ethpm/backends/test_http_backends.py @@ -1,3 +1,4 @@ +import os import pytest from requests.exceptions import ( @@ -21,7 +22,8 @@ "https://api.github.com/repos/ethpm/py-ethpm/git/blobs/a7232a93f1e9e75d606f6c1da18aa16037e03480", # noqa: E501 ), ) -def test_github_over_https_backend_fetch_uri_contents(uri, owned_contract, w3, infura_env): +@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() assert backend.base_uri == GITHUB_API_AUTHORITY @@ -30,7 +32,8 @@ def test_github_over_https_backend_fetch_uri_contents(uri, owned_contract, w3, i assert owned_package.name == "owned" -def test_github_over_https_backend_raises_error_with_invalid_content_hash(w3, infura_env): +@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): Package.from_uri(invalid_uri, w3) diff --git a/tests/ethpm/backends/test_registry_backend.py b/tests/ethpm/backends/test_registry_backend.py index 852012828f..872d542e9d 100644 --- a/tests/ethpm/backends/test_registry_backend.py +++ b/tests/ethpm/backends/test_registry_backend.py @@ -1,3 +1,4 @@ +import os import pytest from ethpm.backends.registry import ( @@ -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' @@ -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"): diff --git a/tests/ethpm/conftest.py b/tests/ethpm/conftest.py index 7e0983bb56..cd5323f539 100644 --- a/tests/ethpm/conftest.py +++ b/tests/ethpm/conftest.py @@ -40,12 +40,6 @@ def pytest_addoption(parser): parser.addoption("--integration", action="store_true", default=False) -@pytest.fixture -def infura_env(monkeypatch): - # Please play nice and don't use this key for any shenanigans, thanks! - monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", '4f1a358967c7474aae6f8f4a7698aefc') - - @pytest.fixture def package_names(): return PACKAGE_NAMES diff --git a/tests/ethpm/test_package_init_from_registry_uri.py b/tests/ethpm/test_package_init_from_registry_uri.py index a86813c39c..d1717925b0 100644 --- a/tests/ethpm/test_package_init_from_registry_uri.py +++ b/tests/ethpm/test_package_init_from_registry_uri.py @@ -1,3 +1,4 @@ +import os import pytest from ethpm import ( @@ -15,6 +16,7 @@ "bzz://da6adeeb4589d8652bbe5679aae6b6409ec85a20e92a8823c7c99e25dba9493d", ), ) -def test_package_init_with_unsupported_uris_raises_exception(uri, w3, infura_env): +@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) diff --git a/tests/ethpm/test_package_init_from_uri.py b/tests/ethpm/test_package_init_from_uri.py index 77c071e358..fd395aeaf8 100644 --- a/tests/ethpm/test_package_init_from_uri.py +++ b/tests/ethpm/test_package_init_from_uri.py @@ -1,3 +1,4 @@ +import os import pytest from ethpm import ( @@ -26,6 +27,7 @@ def test_package_from_uri_with_valid_uri(dummy_ipfs_backend, w3): "ipfsQmTKB75Y73zhNbD3Y73xeXGjYrZHmaXXNxoZqGCagu7r8u/readme/", ), ) -def test_package_from_uri_rejects_invalid_ipfs_uri(invalid, w3, infura_env): +@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) diff --git a/tox.ini b/tox.ini index 66c8a3a84e..9478fbafe5 100644 --- a/tox.ini +++ b/tox.ini @@ -50,6 +50,7 @@ passenv = PARITY_OS GOROOT GOPATH + WEB3_INFURA_PROJECT_ID basepython = doctest: python3.6 py36: python3.6