Skip to content

Commit

Permalink
fix: block validation issues (ApeWorX#2125)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jun 10, 2024
1 parent 73bc44b commit 1937752
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,17 @@ def validate_size(cls, values, handler):
gets returned in computed field "size".
"""

if not hasattr(values, "pop"):
# Handle weird AttributeDict missing pop method.
# https://github.com/ethereum/web3.py/issues/3326
values = {**values}
if isinstance(values, BlockAPI):
size = values.size

else:
if not hasattr(values, "pop"):
# Handle weird AttributeDict missing pop method.
# https://github.com/ethereum/web3.py/issues/3326
values = {**values}

size = values.pop("size", None)

size = values.pop("size", None)
model = handler(values)
if size is not None:
model._size = size
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/test_block.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from eth_pydantic_types import HexBytes
from web3.types import BlockData

from ape_ethereum.ecosystem import Block

Expand Down Expand Up @@ -73,3 +74,21 @@ def test_block_uncles(block):
data["uncles"] = uncles
actual = Block.model_validate(data)
assert actual.uncles == uncles


def test_model_dump_and_validate(block):
model_dump = block.model_dump(by_alias=True)
model_validate = Block.model_validate(model_dump)
assert model_validate == block
# Validate existing model.
model_validate_from_model = Block.model_validate(block)
assert model_validate_from_model == block


def test_model_validate_web3_block():
"""
Show we have good compatability with web3.py native types.
"""
data = BlockData(number=123, timestamp=123, gasLimit=123, gasUsed=100) # type: ignore
actual = Block.model_validate(data)
assert actual.number == 123

0 comments on commit 1937752

Please sign in to comment.