Skip to content

Commit

Permalink
Merge pull request #46 from lidofinance/feat/bump-web3py-version
Browse files Browse the repository at this point in the history
Bump web3py version to latest
  • Loading branch information
F4ever authored Oct 30, 2024
2 parents b9f6c8e + 5a47e16 commit 6d1c266
Show file tree
Hide file tree
Showing 8 changed files with 1,822 additions and 1,925 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ name: Linters
on: pull_request

jobs:
python:
uses: lidofinance/linters/.github/workflows/python.yml@master
with:
dirs: web3_multi_provider
security:
uses: lidofinance/linters/.github/workflows/security.yml@master
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Set up Python 3.11
- name: Set up Python 3.12
uses: actions/setup-python@v4
with:
python-version: '3.11'
python-version: '3.12'

- name: Setup poetry
run: |
Expand Down
4 changes: 2 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -499,5 +499,5 @@ preferred-modules=

# Exceptions that will emit a warning when being caught. Defaults to
# "BaseException, Exception".
overgeneral-exceptions=BaseException,
Exception
overgeneral-exceptions=builtins.BaseException,
builtins.Exception
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,22 @@ from web3_multi_provider import FallbackProvider
w3 = Web3(MultiProvider([ # RPC endpoints list
'http://127.0.0.1:8000/',
'https://mainnet.infura.io/v3/...',
'wss://mainnet.infura.io/ws/v3/...',
]))

# or

w3 = Web3(FallbackProvider([ # RPC endpoints list
'http://127.0.0.1:8000/',
'https://mainnet.infura.io/v3/...',
'wss://mainnet.infura.io/ws/v3/...',
]))

last_block = w3.eth.get_block('latest')
```

### `MultiProvider`

This provider tracks currently used endpoint internally and switch to the next one on error or fails if no more
endpoints to switch to.
This provider keeps track of the current endpoint and switches to the next one if an error occurs.
It fails if no endpoints are available.

### `FallbackProvider`

Expand Down
3,542 changes: 1,716 additions & 1,826 deletions poetry.lock

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "web3-multi-provider"
version = "1.0.0"
version = "2.0.0"
description = "Web3py provider that makes it easy to switch between different blockchain nodes to make sure application will be be online if main blockchain node will be unavailable."
authors = ["Raman <[email protected]>"]
license = "MIT License"
Expand All @@ -10,18 +10,18 @@ include = [
]

[tool.poetry.dependencies]
python = ">=3.7.10,<4"
web3 = ">=5.22.0,<7"
python = ">=3.12,<4"
web3 = ">=7,<8"

[tool.poetry.dev-dependencies]
pytest = "7.1.1"
pytest-cov = "^3.0.0"
pre-commit = "2.17.0"
isort = "^5.10.1"
mypy = "^0.942"
pylint = "^2.13.4"
black = "^22.3.0"
pytest-mock = "^3.7.0"
[tool.poetry.group.dev.dependencies]
pytest = "^8.3.3"
pytest-cov = "^5.0.0"
pre-commit = "^4.0.1"
isort = "^5.13.2"
mypy = "^1.13.0"
pylint = "^3.3.1"
black = "^24.10.0"
pytest-mock = "^3.14.0"

[tool.isort]
profile = "black"
42 changes: 26 additions & 16 deletions tests/test_http_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ class HttpProviderTestCase(TestCase):
def __inject_fixtures(self, caplog):
self._caplog = caplog

@patch("web3.providers.rpc.make_post_request", side_effect=mocked_requests_get)
@patch("web3._utils.http_session_manager.HTTPSessionManager.make_post_request", side_effect=mocked_requests_get)
def test_one_provider_works(self, make_post_request):
self.one_provider_works(MultiProvider)
self.one_provider_works(FallbackProvider)

@patch("web3.providers.rpc.make_post_request", side_effect=mocked_requests_get)
@patch("web3._utils.http_session_manager.HTTPSessionManager.make_post_request", side_effect=mocked_requests_get)
def test_nothing_works(self, make_post_request):
self._caplog.set_level(logging.WARNING)

Expand Down Expand Up @@ -105,13 +106,17 @@ def one_provider_works(self, provider_class):
def test_protocols_support(self):
MultiProvider(["http://127.0.0.1:9001"])
MultiProvider(["https://127.0.0.1:9001"])
MultiProvider(["ws://127.0.0.1:9001"])
MultiProvider(["wss://127.0.0.1:9001"])

with self.assertRaises(ProtocolNotSupported):
MultiProvider(["ipc://127.0.0.1:9001"])

@patch("web3.providers.rpc.make_post_request", side_effect=mocked_request_poa)
with self.assertRaises(ProtocolNotSupported):
MultiProvider(["ws://127.0.0.1:9001"])

with self.assertRaises(ProtocolNotSupported):
MultiProvider(["wss://127.0.0.1:9001"])

@patch("web3._utils.http_session_manager.HTTPSessionManager.make_post_request", side_effect=mocked_request_poa)
def test_poa_blockchain(self, make_post_request):
provider = MultiProvider(["http://127.0.0.1:9000"])

Expand All @@ -127,7 +132,7 @@ def test_poa_blockchain(self, make_post_request):

self.assertIsNotNone(block.get("proofOfAuthorityData", None))

@patch("web3.providers.rpc.make_post_request", side_effect=mocked_requests_get)
@patch("web3._utils.http_session_manager.HTTPSessionManager.make_post_request", side_effect=mocked_requests_get)
def test_pos_blockchain(self, make_post_request):
provider = MultiProvider(["http://127.0.0.1:9000"])

Expand All @@ -151,41 +156,44 @@ def test_no_endpoints(self):
with pytest.raises(NoActiveProviderError):
w3.eth.get_block("latest")

@patch("web3.providers.rpc.make_post_request", side_effect=mocked_requests_get)
@patch("web3._utils.http_session_manager.HTTPSessionManager.make_post_request", side_effect=mocked_requests_get)
def test_one_endpoint(self, make_post_request: Mock):
w3 = Web3(
FallbackProvider(
[
"http://127.0.0.1:9000",
]
],
exception_retry_configuration=None,
)
)
w3.eth.get_block("latest")
make_post_request.assert_called_once()

@patch("web3.providers.rpc.make_post_request", side_effect=mocked_requests_get)
@patch("web3._utils.http_session_manager.HTTPSessionManager.make_post_request", side_effect=mocked_requests_get)
def test_first_working(self, make_post_request: Mock):
w3 = Web3(
FallbackProvider(
[
"http://127.0.0.1:9000",
"http://127.0.0.1:9001",
]
],
exception_retry_configuration=None,
)
)
w3.eth.get_block("latest")
make_post_request.assert_called_once()
assert make_post_request.call_args.args[0] == "http://127.0.0.1:9000"

@patch("web3.providers.rpc.make_post_request", side_effect=mocked_requests_get)
@patch("web3._utils.http_session_manager.HTTPSessionManager.make_post_request", side_effect=mocked_requests_get)
def test_all_endpoints_fail(self, make_post_request: Mock):
w3 = Web3(
FallbackProvider(
[
"http://127.0.0.1:9001",
"http://127.0.0.1:9002",
"http://127.0.0.1:9003",
]
],
exception_retry_configuration=None,
)
)

Expand All @@ -195,29 +203,31 @@ def test_all_endpoints_fail(self, make_post_request: Mock):
assert make_post_request.call_count == 3
assert make_post_request.call_args.args[0] == "http://127.0.0.1:9003"

@patch("web3.providers.rpc.make_post_request", side_effect=mocked_requests_get)
@patch("web3._utils.http_session_manager.HTTPSessionManager.make_post_request", side_effect=mocked_requests_get)
def test_one_endpoint_works(self, make_post_request: Mock):
w3 = Web3(
FallbackProvider(
[
"http://127.0.0.1:9001",
"http://127.0.0.1:9000",
]
],
exception_retry_configuration=None,
)
)

w3.eth.get_block("latest")
assert make_post_request.call_count == 2
assert make_post_request.call_args.args[0] == "http://127.0.0.1:9000"

@patch("web3.providers.rpc.make_post_request", side_effect=mocked_requests_get)
@patch("web3._utils.http_session_manager.HTTPSessionManager.make_post_request", side_effect=mocked_requests_get)
def test_starts_from_the_first(self, make_post_request: Mock):
w3 = Web3(
FallbackProvider(
[
"http://127.0.0.1:9001",
"http://127.0.0.1:9000",
]
],
exception_retry_configuration=None,
)
)

Expand Down
Loading

0 comments on commit 6d1c266

Please sign in to comment.