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

Rename middleware_stack to middleware_onion #1210

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
6 changes: 3 additions & 3 deletions docs/gas_price.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ Available gas price strategies
w3 = Web3()
w3.eth.setGasPriceStrategy(medium_gas_price_strategy)

w3.middleware_stack.add(middleware.time_based_cache_middleware)
w3.middleware_stack.add(middleware.latest_block_based_cache_middleware)
w3.middleware_stack.add(middleware.simple_cache_middleware)
w3.middleware_onion.add(middleware.time_based_cache_middleware)
w3.middleware_onion.add(middleware.latest_block_based_cache_middleware)
w3.middleware_onion.add(middleware.simple_cache_middleware)
2 changes: 1 addition & 1 deletion docs/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ middleware performs the following translations for requests and responses.
* Numeric responses will be converted from their hexadecimal representations to
their integer representations.

The ``RequestManager`` object exposes the ``middleware_stack`` object to manage middlewares. It
The ``RequestManager`` object exposes the ``middleware_onion`` object to manage middlewares. It
is also exposed on the ``Web3`` object for convenience. That API is detailed in
:ref:`Modifying_Middleware`.

Expand Down
50 changes: 25 additions & 25 deletions docs/middleware.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ to the request inside the innermost layer of the onion. Here is a (simplified) d
Returned value in Web3.py


The middlewares are maintained in ``Web3.middleware_stack``. See
The middlewares are maintained in ``Web3.middleware_onion``. See
below for the API.

When specifying middlewares in a list, or retrieving the list of middlewares, they will
be returned in the order of outermost layer first and innermost layer last. In the above
example, that means that ``list(w3.middleware_stack)`` would return the middlewares in
example, that means that ``list(w3.middleware_onion)`` would return the middlewares in
the order of: ``[2, 1, 0]``.

See "Internals: :ref:`internals__middlewares`" for a deeper dive to how middlewares work.
Expand All @@ -157,7 +157,7 @@ Middleware Stack API

To add or remove items in different layers, use the following API:

.. py:method:: Web3.middleware_stack.add(middleware, name=None)
.. py:method:: Web3.middleware_onion.add(middleware, name=None)

Middleware will be added to the outermost layer. That means the new middleware will modify the
request first, and the response last. You can optionally name it with any hashable object,
Expand All @@ -166,27 +166,27 @@ To add or remove items in different layers, use the following API:
.. code-block:: python

>>> w3 = Web3(...)
>>> w3.middleware_stack.add(web3.middleware.pythonic_middleware)
>>> w3.middleware_onion.add(web3.middleware.pythonic_middleware)
# or
>>> w3.middleware_stack.add(web3.middleware.pythonic_middleware, 'pythonic')
>>> w3.middleware_onion.add(web3.middleware.pythonic_middleware, 'pythonic')

.. py:method:: Web3.middleware_stack.inject(middleware, name=None, layer=None)
.. py:method:: Web3.middleware_onion.inject(middleware, name=None, layer=None)

Inject a named middleware to an arbitrary layer.

The current implementation only supports injection at the innermost or
outermost layers. Note that injecting to the outermost layer is equivalent to calling
:meth:`Web3.middleware_stack.add` .
:meth:`Web3.middleware_onion.add` .

.. code-block:: python

# Either of these will put the pythonic middleware at the innermost layer
>>> w3 = Web3(...)
>>> w3.middleware_stack.inject(web3.middleware.pythonic_middleware, layer=0)
>>> w3.middleware_onion.inject(web3.middleware.pythonic_middleware, layer=0)
# or
>>> w3.middleware_stack.inject(web3.middleware.pythonic_middleware, 'pythonic', layer=0)
>>> w3.middleware_onion.inject(web3.middleware.pythonic_middleware, 'pythonic', layer=0)

.. py:method:: Web3.middleware_stack.remove(middleware)
.. py:method:: Web3.middleware_onion.remove(middleware)

Middleware will be removed from whatever layer it was in. If you added the middleware with
a name, use the name to remove it. If you added the middleware as an object, use the object
Expand All @@ -195,11 +195,11 @@ To add or remove items in different layers, use the following API:
.. code-block:: python

>>> w3 = Web3(...)
>>> w3.middleware_stack.remove(web3.middleware.pythonic_middleware)
>>> w3.middleware_onion.remove(web3.middleware.pythonic_middleware)
# or
>>> w3.middleware_stack.remove('pythonic')
>>> w3.middleware_onion.remove('pythonic')

.. py:method:: Web3.middleware_stack.replace(old_middleware, new_middleware)
.. py:method:: Web3.middleware_onion.replace(old_middleware, new_middleware)

Middleware will be replaced from whatever layer it was in. If the middleware was named, it will
continue to have the same name. If it was un-named, then you will now reference it with the new
Expand All @@ -210,25 +210,25 @@ To add or remove items in different layers, use the following API:
>>> from web3.middleware import pythonic_middleware, attrdict_middleware
>>> w3 = Web3(...)

>>> w3.middleware_stack.replace(pythonic_middleware, attrdict_middleware)
>>> w3.middleware_onion.replace(pythonic_middleware, attrdict_middleware)
# this is now referenced by the new middleware object, so to remove it:
>>> w3.middleware_stack.remove(attrdict_middleware)
>>> w3.middleware_onion.remove(attrdict_middleware)

# or, if it was named

>>> w3.middleware_stack.replace('pythonic', attrdict_middleware)
>>> w3.middleware_onion.replace('pythonic', attrdict_middleware)
# this is still referenced by the original name, so to remove it:
>>> w3.middleware_stack.remove('pythonic')
>>> w3.middleware_onion.remove('pythonic')

.. py:method:: Web3.middleware_stack.clear()
.. py:method:: Web3.middleware_onion.clear()

Empty all the middlewares, including the default ones.

.. code-block:: python

>>> w3 = Web3(...)
>>> w3.middleware_stack.clear()
>>> assert len(w3.middleware_stack) == 0
>>> w3.middleware_onion.clear()
>>> assert len(w3.middleware_onion) == 0


Optional Middleware
Expand All @@ -244,7 +244,7 @@ Web3 ships with non-default middleware, for your custom use. In addition to the
.. warning::
This will
*replace* the default middlewares. To keep the default functionality,
either use ``middleware_stack.add()`` from above, or add the default middlewares to your list of
either use ``middleware_onion.add()`` from above, or add the default middlewares to your list of
new middlewares.

Below is a list of built-in middleware, which is not enabled by default.
Expand All @@ -266,7 +266,7 @@ Stalecheck
.. code-block:: python

two_day_stalecheck = make_stalecheck_middleware(60 * 60 * 24 * 2)
web3.middleware_stack.add(two_day_stalecheck)
web3.middleware_onion.add(two_day_stalecheck)

If the latest block in the blockchain is older than 2 days in this example, then the
middleware will raise a ``StaleBlockchain`` exception on every call except
Expand Down Expand Up @@ -352,7 +352,7 @@ unique IPC location and loads the middleware:
>>> from web3.middleware import geth_poa_middleware

# inject the poa compatibility middleware to the innermost layer
>>> w3.middleware_stack.inject(geth_poa_middleware, layer=0)
>>> w3.middleware_onion.inject(geth_poa_middleware, layer=0)

# confirm that the connection succeeded
>>> w3.version.node
Expand Down Expand Up @@ -383,7 +383,7 @@ retrieved using JSON-RPC endpoints that don't rely on server state.
>>> from web3 import Web3, EthereumTesterProvider
>>> w3 = Web3(EthereumTesterProvider)
>>> from web3.middleware import local_filter_middleware
>>> w3.middleware_stack.add(local_filter_middleware())
>>> w3.middleware_onion.add(local_filter_middleware())

# Normal block and log filter apis behave as before.
>>> block_filter = w3.eth.filter("latest")
Expand Down Expand Up @@ -412,6 +412,6 @@ This middleware automatically captures transactions, signs them, and sends them
>>> from web3.middleware import construct_sign_and_send_raw_middleware
>>> from eth_account import Account
>>> acct = Account.create('KEYSMASH FJAFJKLDSKF7JKFDJ 1530')
>>> w3.middleware_stack.add(construct_sign_and_send_raw_middleware(acct))
>>> w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acct))
>>> w3.eth.defaultAccount = acct.address
# Now you can send a tx from acct.address without having to build and sign each raw transaction
4 changes: 2 additions & 2 deletions ens/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def customize_web3(w3):
from web3.contract import ConciseContract
from web3.middleware import make_stalecheck_middleware

w3.middleware_stack.remove('name_to_address')
w3.middleware_stack.add(
w3.middleware_onion.remove('name_to_address')
w3.middleware_onion.add(
make_stalecheck_middleware(ACCEPTABLE_STALE_HOURS * 3600),
name='stalecheck',
)
Expand Down
8 changes: 4 additions & 4 deletions tests/core/eth-module/test_poa.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_long_extra_data(web3):
return_block_with_long_extra_data = construct_fixture_middleware({
'eth_getBlockByNumber': {'extraData': '0x' + 'ff' * 33},
})
web3.middleware_stack.inject(return_block_with_long_extra_data, layer=0)
web3.middleware_onion.inject(return_block_with_long_extra_data, layer=0)
with pytest.raises(ValidationError):
web3.eth.getBlock('latest')

Expand All @@ -23,7 +23,7 @@ def test_full_extra_data(web3):
return_block_with_long_extra_data = construct_fixture_middleware({
'eth_getBlockByNumber': {'extraData': '0x' + 'ff' * 32},
})
web3.middleware_stack.inject(return_block_with_long_extra_data, layer=0)
web3.middleware_onion.inject(return_block_with_long_extra_data, layer=0)
block = web3.eth.getBlock('latest')
assert block.extraData == b'\xff' * 32

Expand All @@ -32,8 +32,8 @@ def test_geth_proof_of_authority(web3):
return_block_with_long_extra_data = construct_fixture_middleware({
'eth_getBlockByNumber': {'extraData': '0x' + 'ff' * 33},
})
web3.middleware_stack.inject(geth_poa_middleware, layer=0)
web3.middleware_stack.inject(return_block_with_long_extra_data, layer=0)
web3.middleware_onion.inject(geth_poa_middleware, layer=0)
web3.middleware_onion.inject(return_block_with_long_extra_data, layer=0)
block = web3.eth.getBlock('latest')
assert 'extraData' not in block
assert block.proofOfAuthorityData == b'\xff' * 33
2 changes: 1 addition & 1 deletion tests/core/eth-module/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_wait_for_missing_receipt(web3):


def test_unmined_transaction_wait_for_receipt(web3):
web3.middleware_stack.add(unmined_receipt_simulator_middleware)
web3.middleware_onion.add(unmined_receipt_simulator_middleware)
txn_hash = web3.eth.sendTransaction({
'from': web3.eth.coinbase,
'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
Expand Down
2 changes: 1 addition & 1 deletion tests/core/filtering/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def web3(request):
provider = EthereumTesterProvider()
w3 = Web3(provider)
if use_filter_middleware:
w3.middleware_stack.add(local_filter_middleware)
w3.middleware_onion.add(local_filter_middleware)
return w3


Expand Down
Loading