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

Swap getBlock for get_block #1829

Merged
merged 1 commit into from
Jan 13, 2021
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
10 changes: 5 additions & 5 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ Looking up blocks
-----------------

Blocks can be looked up by either their number or hash using the
``web3.eth.getBlock`` API. Block hashes should be in their hexadecimal
``web3.eth.get_block`` API. Block hashes should be in their hexadecimal
representation. Block numbers

.. code-block:: python

# get a block by number
>>> web3.eth.getBlock(12345)
>>> web3.eth.get_block(12345)
{
'author': '0xad5C1768e5974C231b2148169da064e61910f31a',
'difficulty': 735512610763,
Expand Down Expand Up @@ -45,19 +45,19 @@ representation. Block numbers
}

# get a block by it's hash
>>> web3.eth.getBlock('0x767c2bfb3bdee3f78676c1285cd757bcd5d8c272cef2eb30d9733800a78c0b6d')
>>> web3.eth.get_block('0x767c2bfb3bdee3f78676c1285cd757bcd5d8c272cef2eb30d9733800a78c0b6d')
{...}


Getting the latest block
------------------------

You can also retrieve the latest block using the string ``'latest'`` in the
``web3.eth.getBlock`` API.
``web3.eth.get_block`` API.

.. code-block:: python

>>> web3.eth.getBlock('latest')
>>> web3.eth.get_block('latest')
{...}


Expand Down
6 changes: 3 additions & 3 deletions docs/middleware.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ AttributeDict
.. py:method:: web3.middleware.attrdict_middleware
This middleware converts the output of a function from a dictionary to an ``AttributeDict``
which enables dot-syntax access, like ``eth.getBlock('latest').number``
in addition to ``eth.getBlock('latest')['number']``.
which enables dot-syntax access, like ``eth.get_block('latest').number``
in addition to ``eth.get_block('latest')['number']``.

.eth Name Resolution
~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -272,7 +272,7 @@ 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
``web3.eth.getBlock()``.
``web3.eth.get_block()``.


Cache
Expand Down
4 changes: 2 additions & 2 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ Fetching Data

Viewing account balances (:meth:`get_balance <web3.eth.Eth.get_balance>`), transactions
(:meth:`getTransaction <web3.eth.Eth.getTransaction>`), and block data
(:meth:`getBlock <web3.eth.Eth.getBlock>`) are some of the most common starting
(:meth:`get_block <web3.eth.Eth.get_block>`) are some of the most common starting
points in Web3.py.


API
^^^

- :meth:`web3.eth.get_balance() <web3.eth.Eth.get_balance>`
- :meth:`web3.eth.getBlock() <web3.eth.Eth.getBlock>`
- :meth:`web3.eth.get_block() <web3.eth.Eth.get_block>`
- :meth:`web3.eth.getBlockTransactionCount() <web3.eth.Eth.getBlockTransactionCount>`
- :meth:`web3.eth.getCode() <web3.eth.Eth.getCode>`
- :meth:`web3.eth.getProof() <web3.eth.Eth.getProof>`
Expand Down
2 changes: 1 addition & 1 deletion docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ to interact with the Ethereum blockchain. Try getting all the information about

.. code-block:: python
>>> w3.eth.getBlock('latest')
>>> w3.eth.get_block('latest')
{'difficulty': 1,
'gasLimit': 6283185,
'gasUsed': 0,
Expand Down
12 changes: 8 additions & 4 deletions docs/web3.eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ you can find the latest block number in these two ways:

.. code-block:: python

>>> block = web3.eth.getBlock('latest')
>>> block = web3.eth.get_block('latest')
AttributeDict({
'hash': '0xe8ad537a261e6fff80d551d8d087ee0f2202da9b09b64d172a5f45e818eb472a',
'number': 4022281,
Expand Down Expand Up @@ -309,7 +309,7 @@ The following methods are available on the ``web3.eth`` namespace.

return True

block = w3.eth.getBlock(3391)
block = w3.eth.get_block(3391)
proof = w3.eth.getProof('0x6C8f2A135f6ed072DE4503Bd7C4999a1a17F824B', [0, 1], 3391)
assert verify_eth_getProof(proof, block.stateRoot)

Expand All @@ -333,7 +333,7 @@ The following methods are available on the ``web3.eth`` namespace.
'0x'


.. py:method:: Eth.getBlock(block_identifier=eth.defaultBlock, full_transactions=False)
.. py:method:: Eth.get_block(block_identifier=eth.defaultBlock, full_transactions=False)

* Delegates to ``eth_getBlockByNumber`` or ``eth_getBlockByHash`` RPC Methods

Expand All @@ -348,7 +348,7 @@ The following methods are available on the ``web3.eth`` namespace.

.. code-block:: python

>>> web3.eth.getBlock(2000000)
>>> web3.eth.get_block(2000000)
AttributeDict({
'difficulty': 49824742724615,
'extraData': '0xe4b883e5bda9e7a59ee4bb99e9b1bc',
Expand All @@ -371,6 +371,10 @@ The following methods are available on the ``web3.eth`` namespace.
'uncles': [],
})

.. py:method:: Eth.getBlock(block_identifier=eth.defaultBlock, full_transactions=False)

.. warning:: Deprecated: This method is deprecated in favor of
:meth:`~web3.eth.Eth.get_block`

.. py:method:: Eth.getBlockTransactionCount(block_identifier)

Expand Down
2 changes: 1 addition & 1 deletion ethpm/_utils/chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


def get_genesis_block_hash(web3: "Web3") -> HexBytes:
return web3.eth.getBlock(BlockNumber(0))["hash"]
return web3.eth.get_block(BlockNumber(0))["hash"]


BLOCK = "block"
Expand Down
8 changes: 4 additions & 4 deletions ethpm/uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,27 @@ def create_latest_block_uri(w3: "Web3", from_blocks_ago: int = 3) -> URI:
If using a testnet with less than 3 mined blocks, adjust :from_blocks_ago:.
"""
chain_id = to_hex(get_genesis_block_hash(w3))
latest_block_tx_receipt = w3.eth.getBlock("latest")
latest_block_tx_receipt = w3.eth.get_block("latest")
target_block_number = BlockNumber(latest_block_tx_receipt["number"] - from_blocks_ago)
if target_block_number < 0:
raise Exception(
f"Only {latest_block_tx_receipt['number']} blocks avaible on provided w3, "
f"cannot create latest block uri for {from_blocks_ago} blocks ago."
)
recent_block = to_hex(w3.eth.getBlock(target_block_number)["hash"])
recent_block = to_hex(w3.eth.get_block(target_block_number)["hash"])
return create_block_uri(chain_id, recent_block)


@curry
def check_if_chain_matches_chain_uri(web3: "Web3", blockchain_uri: URI) -> bool:
chain_id, resource_type, resource_hash = parse_BIP122_uri(blockchain_uri)
genesis_block = web3.eth.getBlock("earliest")
genesis_block = web3.eth.get_block("earliest")

if encode_hex(genesis_block["hash"]) != chain_id:
return False

if resource_type == BLOCK:
resource = web3.eth.getBlock(resource_hash)
resource = web3.eth.get_block(resource_hash)
else:
raise ValueError(f"Unsupported resource type: {resource_type}")

Expand Down
1 change: 1 addition & 0 deletions newsfragments/1829.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``eth.get_block`` method and deprecate ``eth.getBlock``.
2 changes: 1 addition & 1 deletion tests/core/contracts/test_contract_call_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ def test_neg_block_indexes_from_the_end(web3, math_contract):


def test_returns_data_from_specified_block(web3, math_contract):
start_num = web3.eth.getBlock('latest').number
start_num = web3.eth.get_block('latest').number
web3.provider.make_request(method='evm_mine', params=[5])
math_contract.functions.increment().transact()
math_contract.functions.increment().transact()
Expand Down
4 changes: 2 additions & 2 deletions tests/core/contracts/test_contract_caller_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_caller_with_a_nonexistent_function(math_contract):


def test_caller_with_block_identifier(web3, math_contract):
start_num = web3.eth.getBlock('latest').number
start_num = web3.eth.get_block('latest').number
assert math_contract.caller.counter() == 0

web3.provider.make_request(method='evm_mine', params=[5])
Expand All @@ -117,7 +117,7 @@ def test_caller_with_block_identifier_and_transaction_dict(web3,
caller_tester_contract,
transaction_dict,
address):
start_num = web3.eth.getBlock('latest').number
start_num = web3.eth.get_block('latest').number
assert caller_tester_contract.caller.counter() == 0

web3.provider.make_request(method='evm_mine', params=[5])
Expand Down
4 changes: 2 additions & 2 deletions tests/core/contracts/test_contract_util_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
# This tests negative block number identifiers, which behave like python
# list slices, with -1 being the latest block and -2 being the block before that.
# This test is necessary because transaction calls allow negative block indexes, although
# getBlock() does not allow negative block identifiers. Support for negative block identifier
# get_block() does not allow negative block identifiers. Support for negative block identifier
# will likely be removed in v5.
def test_parse_block_identifier_int(web3):
last_num = web3.eth.getBlock('latest').number
last_num = web3.eth.get_block('latest').number
assert 0 == parse_block_identifier_int(web3, -1 - last_num)
2 changes: 1 addition & 1 deletion tests/core/contracts/test_implicit_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def math_contract(web3, MATH_ABI, MATH_CODE, MATH_RUNTIME, address_conversion_fu
@pytest.fixture()
def get_transaction_count(web3):
def get_transaction_count(blocknum_or_label):
block = web3.eth.getBlock(blocknum_or_label)
block = web3.eth.get_block(blocknum_or_label)
# Return the blocknum if we requested this via labels
# so we can directly query the block next time (using the same API call)
# Either way, return the number of transactions in the given block
Expand Down
6 changes: 3 additions & 3 deletions tests/core/eth-module/test_poa.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def test_long_extra_data(web3):
})
web3.middleware_onion.inject(return_block_with_long_extra_data, layer=0)
with pytest.raises(ExtraDataLengthError):
web3.eth.getBlock('latest')
web3.eth.get_block('latest')


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


Expand All @@ -34,6 +34,6 @@ def test_geth_proof_of_authority(web3):
})
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')
block = web3.eth.get_block('latest')
assert 'extraData' not in block
assert block.proofOfAuthorityData == b'\xff' * 33
2 changes: 1 addition & 1 deletion tests/core/filtering/test_existing_filter_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ def test_instantiate_existing_filter(web3, sleep_interval, wait_for_block, filte
assert len(found_block_hashes) == 3

expected_block_hashes = [
web3.eth.getBlock(n + 1).hash for n in range(current_block, current_block + 3)
web3.eth.get_block(n + 1).hash for n in range(current_block, current_block + 3)
]
assert found_block_hashes == expected_block_hashes
2 changes: 1 addition & 1 deletion tests/core/filtering/test_filter_against_latest_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ def test_sync_filter_against_latest_blocks(web3, sleep_interval, wait_for_block)
assert len(found_block_hashes) == 3

expected_block_hashes = [
web3.eth.getBlock(n + 1).hash for n in range(current_block, current_block + 3)
web3.eth.get_block(n + 1).hash for n in range(current_block, current_block + 3)
]
assert found_block_hashes == expected_block_hashes
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_latest_block_based_cache_middleware_pulls_from_cache(
w3.middleware_onion.add(block_data_middleware)
w3.middleware_onion.add(result_generator_middleware)

current_block_hash = w3.eth.getBlock('latest')['hash']
current_block_hash = w3.eth.get_block('latest')['hash']

def cache_class():
return {
Expand Down
10 changes: 5 additions & 5 deletions tests/core/middleware/test_stalecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_stalecheck_pass(request_middleware):

def test_stalecheck_fail(request_middleware, now):
with patch('web3.middleware.stalecheck._isfresh', return_value=False):
request_middleware.web3.eth.getBlock.return_value = stub_block(now)
request_middleware.web3.eth.get_block.return_value = stub_block(now)
with pytest.raises(StaleBlockchain):
request_middleware('', [])

Expand All @@ -84,16 +84,16 @@ def test_stalecheck_fail(request_middleware, now):
]
)
def test_stalecheck_ignores_get_by_block_methods(request_middleware, rpc_method):
# This is especially critical for getBlock('latest') which would cause infinite recursion
# This is especially critical for get_block('latest') which would cause infinite recursion
with patch('web3.middleware.stalecheck._isfresh', side_effect=[False, True]):
request_middleware(rpc_method, [])
assert not request_middleware.web3.eth.getBlock.called
assert not request_middleware.web3.eth.get_block.called


def test_stalecheck_calls_isfresh_with_empty_cache(request_middleware, allowable_delay):
with patch('web3.middleware.stalecheck._isfresh', side_effect=[False, True]) as freshspy:
block = object()
request_middleware.web3.eth.getBlock.return_value = block
request_middleware.web3.eth.get_block.return_value = block
request_middleware('', [])
cache_call, live_call = freshspy.call_args_list
assert cache_call[0] == (None, allowable_delay)
Expand All @@ -103,7 +103,7 @@ def test_stalecheck_calls_isfresh_with_empty_cache(request_middleware, allowable
def test_stalecheck_adds_block_to_cache(request_middleware, allowable_delay):
with patch('web3.middleware.stalecheck._isfresh', side_effect=[False, True, True]) as freshspy:
block = object()
request_middleware.web3.eth.getBlock.return_value = block
request_middleware.web3.eth.get_block.return_value = block

# cache miss
request_middleware('', [])
Expand Down
12 changes: 6 additions & 6 deletions tests/core/mining-module/test_miner_setExtra.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
def test_miner_set_extra(web3_empty, wait_for_block):
web3 = web3_empty

initial_extra = decode_hex(web3.eth.getBlock(web3.eth.blockNumber)['extraData'])
initial_extra = decode_hex(web3.eth.get_block(web3.eth.blockNumber)['extraData'])

new_extra_data = b'-this-is-32-bytes-of-extra-data-'

Expand All @@ -28,12 +28,12 @@ def test_miner_set_extra(web3_empty, wait_for_block):

with Timeout(60) as timeout:
while True:
extra_data = decode_hex(web3.eth.getBlock(web3.eth.blockNumber)['extraData'])
extra_data = decode_hex(web3.eth.get_block(web3.eth.blockNumber)['extraData'])
if extra_data == new_extra_data:
break
timeout.sleep(random.random())

after_extra = decode_hex(web3.eth.getBlock(web3.eth.blockNumber)['extraData'])
after_extra = decode_hex(web3.eth.get_block(web3.eth.blockNumber)['extraData'])

assert after_extra == new_extra_data

Expand All @@ -42,7 +42,7 @@ def test_miner_set_extra(web3_empty, wait_for_block):
def test_miner_setExtra(web3_empty, wait_for_block):
web3 = web3_empty

initial_extra = decode_hex(web3.eth.getBlock(web3.eth.blockNumber)['extraData'])
initial_extra = decode_hex(web3.eth.get_block(web3.eth.blockNumber)['extraData'])

new_extra_data = b'-this-is-32-bytes-of-extra-data-'

Expand All @@ -53,10 +53,10 @@ def test_miner_setExtra(web3_empty, wait_for_block):
web3.geth.miner.setExtra(new_extra_data)
with Timeout(60) as timeout:
while True:
extra_data = decode_hex(web3.eth.getBlock(web3.eth.blockNumber)['extraData'])
extra_data = decode_hex(web3.eth.get_block(web3.eth.blockNumber)['extraData'])
if extra_data == new_extra_data:
break
timeout.sleep(random.random())

after_extra = decode_hex(web3.eth.getBlock(web3.eth.blockNumber)['extraData'])
after_extra = decode_hex(web3.eth.get_block(web3.eth.blockNumber)['extraData'])
assert after_extra == new_extra_data
2 changes: 1 addition & 1 deletion tests/core/providers/test_ipc_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ def test_web3_auto_gethdev():
'eth_getBlockByNumber': {'extraData': '0x' + 'ff' * 33},
})
w3.middleware_onion.inject(return_block_with_long_extra_data, layer=0)
block = w3.eth.getBlock('latest')
block = w3.eth.get_block('latest')
assert 'extraData' not in block
assert block.proofOfAuthorityData == b'\xff' * 33
8 changes: 4 additions & 4 deletions tests/core/testing-module/test_testing_mine.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
def test_testing_mine_single_block(web3):
web3.testing.mine()

before_mining_block = web3.eth.getBlock("latest")
before_mining_block = web3.eth.get_block("latest")

web3.testing.mine()

after_mining_block = web3.eth.getBlock("latest")
after_mining_block = web3.eth.get_block("latest")

assert after_mining_block['number'] - before_mining_block['number'] == 1


def test_testing_mine_multiple_blocks(web3):
web3.testing.mine()

before_mining_block = web3.eth.getBlock("latest")
before_mining_block = web3.eth.get_block("latest")

web3.testing.mine(5)

after_mining_block = web3.eth.getBlock("latest")
after_mining_block = web3.eth.get_block("latest")

assert after_mining_block['number'] - before_mining_block['number'] == 5
Loading