Call contract method with blockhash requires fetching block number
- Dominant language
- Python
- Stars
- 5.5k
- Forks
- 1.7k
- Avg merge
- 3d 10h
- Merged PRs (30d)
- 2
Description
### What feature should we add?
Hi everyone!
For security reasons, we're calling contracts using block hashes and avoiding block numbers to ensure that we don't receive responses from a fork.
However, the contract method makes an additional request to the node to retrieve the block number with the block hash, increasing node load and making an unnecessary request. See here:
https://github.com/ethereum/web3.py/blob/main/web3/contract/contract.py#L287
Would it be possible to modify the `parse_block_identifier` function to avoid this additional request?
https://github.com/ethereum/web3.py/blob/main/web3/_utils/contracts.py#L349
e.g.
```python
def parse_block_identifier(
w3: "Web3", block_identifier: Optional[BlockIdentifier]
) -> BlockIdentifier:
if block_identifier is None:
return w3.eth.default_block
if isinstance(block_identifier, int):
return parse_block_identifier_int(w3, block_identifier)
elif block_identifier in ["latest", "earliest", "pending", "safe", "finalized"]:
return block_identifier
elif isinstance(block_identifier, bytes) or is_hex_encoded_block_hash(
block_identifier
):
return to_hex_if_bytes(block_identifier)
else:
raise BlockNumberOutOfRange
```
Related to:
https://github.com/ethereum/web3.py/issues/2816
Contributor guide
Assessment
This issue has not been assessed yet.