Skip to content

Commit

Permalink
Write pm.tie_registry_to_ens
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Oct 25, 2018
1 parent c1b122b commit 34c8830
Show file tree
Hide file tree
Showing 6 changed files with 469 additions and 142 deletions.
10 changes: 1 addition & 9 deletions docs/web3.pm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,4 @@ Methods

The following methods are available on the ``web3.pm`` namespace.

.. py:method:: PM.get_package_from_manifest(self, manifest)
* :manifest: must be a dict representing a valid manifest
* Returns a ``Package`` instance representing the Manifest

.. py:method:: PM.get_package_from_uri(self, uri)
* :uri: *must* be a valid content-addressed URI, as defined in the `Py-EthPM Documentation <https://py-ethpm.readthedocs.io/en/latest/uri_backends.html>`_.
* Returns a ``Package`` isntance representing the Manifest stored at the URI.
TODO: autoclass docstrings once ``web3.pm`` is stable.
167 changes: 167 additions & 0 deletions tests/core/pm-module/test_ens_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import pytest

from eth_utils import (
to_canonical_address,
)

from ens import ENS
from web3 import Web3
from web3.exceptions import (
InvalidAddress,
)

try:
from ethpm import (
ASSETS_DIR,
)
from web3.pm import (
PM,
)
except ImportError:
ethpm_installed = False
else:
ethpm_installed = True


def bytes32(val):
if isinstance(val, int):
result = Web3.toBytes(val)
else:
raise TypeError('val %r could not be converted to bytes')
if len(result) < 32:
return result.rjust(32, b'\0')
else:
return result


@pytest.fixture
def ens_setup(solc_deployer):
# todo: move to module level once ethpm alpha stable
ENS_MANIFEST = ASSETS_DIR / 'ens' / '1.0.1.json'
ens_deployer = solc_deployer(ENS_MANIFEST)
w3 = ens_deployer.package.w3

# ** Set up ENS contracts **

# remove account that creates ENS, so test transactions don't have write access
accounts = w3.eth.accounts
ens_key = accounts.pop()

# create ENS contract
eth_labelhash = w3.keccak(text='eth')
eth_namehash = bytes32(0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae)
resolver_namehash = bytes32(0xfdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5)
ens_package = ens_deployer.deploy("ENSRegistry", transaction={"from": ens_key})
ens_contract = ens_package.deployments.get_instance("ENSRegistry")

# create public resolver
public_resolver_package = ens_deployer.deploy(
"PublicResolver",
ens_contract.address,
transaction={"from": ens_key}
)
public_resolver = public_resolver_package.deployments.get_instance("PublicResolver")

# set 'resolver.eth' to resolve to public resolver
ens_contract.functions.setSubnodeOwner(
b'\0' * 32,
eth_labelhash,
ens_key
).transact({'from': ens_key})

ens_contract.functions.setSubnodeOwner(
eth_namehash,
w3.keccak(text='resolver'),
ens_key
).transact({'from': ens_key})

ens_contract.functions.setResolver(
resolver_namehash,
public_resolver.address
).transact({'from': ens_key})

public_resolver.functions.setAddr(
resolver_namehash,
public_resolver.address
).transact({'from': ens_key})

# create .eth auction registrar
eth_registrar_package = ens_deployer.deploy(
"FIFSRegistrar",
ens_contract.address,
eth_namehash,
transaction={"from": ens_key}
)
eth_registrar = eth_registrar_package.deployments.get_instance("FIFSRegistrar")

# set '.eth' to resolve to the registrar
ens_contract.functions.setResolver(
eth_namehash,
public_resolver.address
).transact({'from': ens_key})

public_resolver.functions.setAddr(
eth_namehash,
eth_registrar.address
).transact({'from': ens_key})

# set owner of tester.eth to an account controlled by tests
ens_contract.functions.setSubnodeOwner(
eth_namehash,
w3.keccak(text='tester'),
w3.eth.accounts[2] # note that this does not have to be the default, only in the list
).transact({'from': ens_key})

# make the registrar the owner of the 'eth' name
ens_contract.functions.setSubnodeOwner(
b'\0' * 32,
eth_labelhash,
eth_registrar.address
).transact({'from': ens_key})
return ENS.fromWeb3(w3, ens_contract.address)


@pytest.fixture
def ens(ens_setup, mocker):
mocker.patch('web3.middleware.stalecheck._isfresh', return_value=True)
ens_setup.web3.eth.defaultAccount = ens_setup.web3.eth.coinbase
return ens_setup


@pytest.mark.skipif(ethpm_installed is False, reason="ethpm is not installed")
def test_ens_must_be_set_before_ens_methods_can_be_used(ens):
w3 = ens.web3
PM.attach(w3, 'pm')
with pytest.raises(InvalidAddress):
w3.pm.set_registry("tester.eth")


@pytest.mark.skipif(ethpm_installed is False, reason="ethpm is not installed")
def test_web3_ens(ens):
w3 = ens.web3
PM.attach(w3, 'pm')
ns = ENS.fromWeb3(w3, ens.ens.address)
w3.ens = ns
w3.pm.deploy_and_set_registry()
assert w3.pm.registry
w3.ens.setup_address('tester.eth', w3.pm.registry.address)
actual_addr = ens.address('tester.eth')
assert w3.pm.registry.address == to_canonical_address(actual_addr)
w3.pm.release_package(b'pkg', b'v1', b'1.com')
pkg_name, version, manifest_uri = w3.pm.get_release_data(b'pkg', b'v1')
# trim trailing bytes returned from vyper bytes32 type
assert pkg_name.rstrip(b'\x00') == b'pkg'
assert version.rstrip(b'\x00') == b'v1'
assert manifest_uri.rstrip(b'\x00') == b'1.com'


@pytest.mark.skipif(ethpm_installed is False, reason="ethpm is not installed")
def test_registry_init_with_ens_name(ens):
w3 = ens.web3
PM.attach(w3, 'pm')
ns = ENS.fromWeb3(w3, ens.ens.address)
w3.ens = ns
w3.pm.deploy_and_set_registry()
w3.ens.setup_address('tester.eth', w3.pm.registry.address)
w3.pm.set_registry('tester.eth')
assert w3.pm.registry.address == to_canonical_address(ens.address('tester.eth'))
3 changes: 0 additions & 3 deletions tests/core/pm-module/test_pm_init.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from pathlib import (
Path,
)
import pytest

from eth_utils import (
Expand Down
Loading

0 comments on commit 34c8830

Please sign in to comment.