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

Change eth_getBalance and eth_getCode to use Method #1733

Merged
merged 3 commits into from
Sep 11, 2020
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
1 change: 1 addition & 0 deletions newsfragments/1733.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change eth_getBalance and eth_getCode to use Method class
43 changes: 22 additions & 21 deletions web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
LogFilter,
TransactionFilter,
)
from web3._utils.normalizers import (
abi_ens_resolver,
)
from web3._utils.rpc_abi import (
RPC,
)
Expand Down Expand Up @@ -202,17 +205,6 @@ def blockNumber(self) -> BlockNumber:
def chainId(self) -> int:
return self.web3.manager.request_blocking(RPC.eth_chainId, [])

def getBalance(
self, account: Union[Address, ChecksumAddress, ENS],
block_identifier: Optional[BlockIdentifier] = None
) -> Wei:
if block_identifier is None:
block_identifier = self.defaultBlock
return self.web3.manager.request_blocking(
RPC.eth_getBalance,
[account, block_identifier],
)

def block_identifier_munger(
self,
*args: Any,
Expand All @@ -222,6 +214,21 @@ def block_identifier_munger(
block_identifier = self.defaultBlock
return [*args, block_identifier]

def address_resolver_munger(
self,
account: Union[Address, ChecksumAddress, ENS],
block_identifier: Optional[BlockIdentifier]=None
) -> Tuple[Union[Address, ChecksumAddress, ENS], BlockIdentifier]:
resolved_addr = abi_ens_resolver(self.web3, 'address', account)[1]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly sure that having this web3 here is going to cause problems when we actually implement async, but I think it's the best solution for the moment. I believe the issue is that the middleware used to resolve an ENS address before it hit validate_address, but now validate_address is called before an ENS address is resolved. This results in an InvalidAddress error in the case where an ENS name is passed because validate_address expects a hexstring. For posterity's sake, the approach I took earlier was to add a conditional in validate_address to check that the domain was valid according to the idna spec, without actually resolving it, and then allowing the middleware to resolve it after the validation.

@marcgarreau let me know if you have a preference on approach!

Copy link
Member

@wolovim wolovim Sep 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too concerned about the added "cost" to the async work at this point, but this context may be long forgotten by the time its needed. Maybe a note on this issue is the way to address that?

Conceptually this is easy enough to reason about, though: if you pass an ENS name into one of these methods, the first thing that happens is a resolution check. 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine kicking the can down the road till async hits.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also fail as soon as the method is used on a ModuleV2 since the current architecture there removes self.web3 on the module class.

if block_identifier is None:
block_identifier = self.defaultBlock
return (resolved_addr, block_identifier)

getBalance: Method[Callable[..., Wei]] = Method(
RPC.eth_getBalance,
mungers=[address_resolver_munger],
)

getStorageAt: Method[
Callable[..., HexBytes]
] = Method(
Expand All @@ -239,16 +246,10 @@ def block_identifier_munger(
mungers=[block_identifier_munger],
)

def getCode(
self, account: Union[Address, ChecksumAddress, ENS],
block_identifier: Optional[BlockIdentifier] = None
) -> HexBytes:
if block_identifier is None:
block_identifier = self.defaultBlock
return self.web3.manager.request_blocking(
RPC.eth_getCode,
[account, block_identifier],
)
getCode: Method[Callable[..., HexBytes]] = Method(
RPC.eth_getCode,
mungers=[address_resolver_munger]
)

def getBlock(
self, block_identifier: BlockIdentifier, full_transactions: bool = False
Expand Down