Skip to content

Commit

Permalink
created a new ens class for varyfying name_resolver middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitchiplunkar committed Sep 5, 2018
1 parent bd47cb9 commit 11798b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
26 changes: 21 additions & 5 deletions tests/core/middleware/test_name_to_address_middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest

from ens import ENS
from web3 import Web3
from web3.exceptions import (
InvalidAddress,
Expand All @@ -13,11 +12,25 @@
BaseProvider,
)

NAME = "dump.eth"
ADDRESS = "0x0000000000000000000000000000000000000000"
BALANCE = 0


class TempENS():
def __init__(self, name_addr_pairs):
self.registry = dict(name_addr_pairs)

def address(self, name, guess_tld=True):
# no automated web3 usages should be guessing the TLD
assert not guess_tld
return self.registry.get(name, None)


@pytest.fixture
def w3():
w3 = Web3(providers=[BaseProvider()], middlewares=[])
w3.ens = ENS.fromWeb3(w3)
w3.ens = TempENS({NAME: ADDRESS})
w3.middleware_stack.add(name_to_address_middleware(w3))
return w3

Expand All @@ -26,15 +39,18 @@ def test_pass_name_resolver(w3):
return_chain_on_mainnet = construct_fixture_middleware({
'net_version': '1',
})
return_balance = construct_fixture_middleware({
'eth_getBalance': BALANCE
})
w3.middleware_stack.inject(return_chain_on_mainnet, layer=0)
with pytest.raises(NotImplementedError):
w3.eth.getBalance('ethereum.eth')
w3.middleware_stack.inject(return_balance, layer=0)
assert w3.eth.getBalance(NAME) == BALANCE


def test_fail_name_resolver(w3):
return_chain_on_mainnet = construct_fixture_middleware({
'net_version': '2',
})
w3.middleware_stack.inject(return_chain_on_mainnet, layer=0)
with pytest.raises(InvalidAddress):
with pytest.raises(InvalidAddress, match='.*ethereum\.eth.*'):
w3.eth.getBalance("ethereum.eth")
18 changes: 12 additions & 6 deletions web3/utils/normalizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,20 @@ def abi_address_to_hex(abi_type, data):
def abi_ens_resolver(w3, abi_type, val):
if abi_type == 'address' and is_ens_name(val):
if w3 is None:
raise InvalidAddress("Could not look up name %r, \
because no web3 connection available" % val)
raise InvalidAddress(
"Could not look up name %r because no web3"
" connection available" % (val)
)
elif w3.ens is None:
raise InvalidAddress("Could not look up name %r, \
because ENS is set to None" % val)
raise InvalidAddress(
"Could not look up name %r because ENS is"
" set to None" % (val)
)
elif int(w3.net.version) is not 1 and not isinstance(w3.ens, StaticENS):
raise InvalidAddress("Could not look up name %r, \
because web3 is not connected to mainnet" % val)
raise InvalidAddress(
"Could not look up name %r because web3 is"
" not connected to mainnet" % (val)
)
else:
return (abi_type, validate_name_has_address(w3.ens, val))
else:
Expand Down

0 comments on commit 11798b5

Please sign in to comment.