Skip to content

Commit

Permalink
fixed error when accessing blocks by blocknumber using eth_tester (#1660
Browse files Browse the repository at this point in the history
)

* fixed error when accessing blocks by blocknumber using eth_tester

* added newsfragment

* Add (and trigger) block identifier test

* Reimplement formatter

* Test balance before and after mining

* Update newsfragments/1660.bugfix.rst

Co-authored-by: Marc Garreau <[email protected]>
Co-authored-by: Jason Carver <[email protected]>
  • Loading branch information
3 people authored Jun 9, 2020
1 parent d9cd290 commit 587d673
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions newsfragments/1660.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added formatter rules for eth_tester middleware to allow :meth:`~web3.eth.Eth.getBalance` by using integer block numbers
9 changes: 9 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ def test_eth_getBalance(self, web3: "Web3") -> None:
assert is_integer(balance)
assert balance >= 0

def test_eth_getBalance_with_block_identifier(self, web3: "Web3") -> None:
miner_address = web3.eth.getBlock(1)['miner']
genesis_balance = web3.eth.getBalance(miner_address, 0)
later_balance = web3.eth.getBalance(miner_address, 1)

assert is_integer(genesis_balance)
assert is_integer(later_balance)
assert later_balance > genesis_balance

def test_eth_getStorageAt(
self, web3: "Web3", emitter_contract_address: ChecksumAddress
) -> None:
Expand Down
4 changes: 4 additions & 0 deletions web3/providers/eth_tester/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ def is_hexstr(value: Any) -> bool:
identity,
apply_formatter_if(is_not_named_block, to_integer_if_hex),
),
RPCEndpoint('eth_getBalance'): apply_formatters_to_args(
identity,
apply_formatter_if(is_not_named_block, to_integer_if_hex),
),
# EVM
RPCEndpoint('evm_revert'): apply_formatters_to_args(hex_to_integer),
# Personal
Expand Down
2 changes: 1 addition & 1 deletion web3/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
TValue = TypeVar("TValue")

BlockParams = Literal["latest", "earliest", "pending"]
BlockIdentifier = Union[BlockParams, BlockNumber, Hash32, HexStr, HexBytes]
BlockIdentifier = Union[BlockParams, BlockNumber, Hash32, HexStr, HexBytes, int]
LatestBlockParam = Literal["latest"]

FunctionIdentifier = Union[str, Type[FallbackFn], Type[ReceiveFn]]
Expand Down

0 comments on commit 587d673

Please sign in to comment.