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 13, 2018
1 parent 23ef954 commit c6717c1
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 107 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.
147 changes: 147 additions & 0 deletions tests/core/pm-module/test_ens_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import pytest

from ens import ENS
from ethpm import (
ASSETS_DIR,
)
from web3 import Web3
from web3.exceptions import (
BadFunctionCallOutput,
)
from web3.pm import (
PM,
)


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):
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


def test_web3_ens(ens):
w3 = ens.web3
PM.attach(w3, 'pm')
with pytest.raises(AttributeError):
w3.pm.tie_registry_to_ens('tester.eth')
w3.pm.deploy_and_set_registry()
assert w3.pm.registry
w3.pm.tie_registry_to_ens('tester.eth', ens_address=ens.ens.address)
actual_addr = ens.address('tester.eth')
assert w3.pm.registry.address == actual_addr
with pytest.raises(BadFunctionCallOutput):
# don't own this domain
w3.pm.tie_registry_to_ens('snakecharmers.eth')
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'


def test_registry_init_with_ens_name(ens):
w3 = ens.web3
PM.attach(w3, 'pm')
w3.pm.deploy_and_set_registry()
w3.pm.tie_registry_to_ens('tester.eth', ens_address=ens.ens.address)
w3.pm.set_registry('tester.eth', ens_address=ens.ens.address)
assert w3.pm.registry.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
115 changes: 60 additions & 55 deletions tests/core/pm-module/test_registry_integration.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import json
from pathlib import Path
import pytest

from eth_tester import EthereumTester
from eth_tester.backends import PyEVMBackend
from eth_utils import is_address, to_canonical_address
from ethpm import Package, ASSETS_DIR
from ethpm.contract import LinkableContract
from pytest_ethereum.plugins import twig_deployer
from eth_utils import (
to_canonical_address,
)

from ethpm import (
ASSETS_DIR,
)
from ethpm.contract import (
LinkableContract,
)
from web3 import Web3
from web3.pm import Registry
from ens import ENS
from web3.pm import (
Registry,
)


@pytest.fixture
Expand All @@ -26,9 +29,9 @@ def w3():
PM.attach(w3, 'pm')
return w3

# Solidity registry

def test_with_solidiy_registry_manifest(w3):
# Solidity registry
def test_with_solidity_registry_manifest(w3):
manifest_path = ASSETS_DIR / 'registry' / "1.0.2.json"
manifest = json.loads(manifest_path.read_text())
Registry = w3.pm.get_package_from_manifest(manifest)
Expand All @@ -40,8 +43,8 @@ def test_with_solidiy_registry_manifest(w3):
assert PackageDBFactory.needs_bytecode_linking
assert ReleaseDBFactory.needs_bytecode_linking

# Vyper registry

# Vyper registry
@pytest.fixture
def vy_registry(twig_deployer, w3):
registry_path = ASSETS_DIR / 'vyper_registry'
Expand All @@ -52,38 +55,39 @@ def vy_registry(twig_deployer, w3):
assert owner == w3.eth.accounts[0]
return Registry(registry_instance.address, w3)

# `Registry` class tests

# `Registry` class tests
def test_registry_init(vy_registry, w3):
# todo ens linking
# update to get_package_from_uri
assert vy_registry.registry.functions.owner().call() == w3.eth.accounts[0]


def test_registry_releases_properly(vy_registry):
vy_registry.release(b"package",b"1.0.0",b"google.com")
vy_registry.release(b"package", b"1.0.0", b"google.com")
release_data = vy_registry.get_release_data(b'package', b'1.0.0')
assert release_data[0][:7] == b'package'
assert release_data[1][:5] == b'1.0.0'
assert release_data[2][:10] == b'google.com'



def test_registry_get_all_packages(vy_registry):
vy_registry.release(b"package1",b"1.0.0",b"p1.v1.0.0.com")
vy_registry.release(b"package1",b"1.0.1",b"p1.v1.0.1.com")
vy_registry.release(b"package1", b"1.0.0", b"p1.v1.0.0.com")
vy_registry.release(b"package1", b"1.0.1", b"p1.v1.0.1.com")
all_pkgs_1 = vy_registry.get_all_package_ids()
assert all_pkgs_1 == ((b'package1', 2),)
vy_registry.release(b"package2",b"1.0.0",b"p2.v1.0.0.com")
vy_registry.release(b"package3",b"1.0.0",b"p3.v1.0.0.com")
vy_registry.release(b"package3",b"1.0.1",b"p3.v1.0.1.com")
vy_registry.release(b"package2", b"1.0.0", b"p2.v1.0.0.com")
vy_registry.release(b"package3", b"1.0.0", b"p3.v1.0.0.com")
vy_registry.release(b"package3", b"1.0.1", b"p3.v1.0.1.com")
all_pkgs_2 = vy_registry.get_all_package_ids()
assert all_pkgs_2 == (
(b'package1', 2),
(b'package2', 1),
(b'package3', 2),
)
vy_registry.release(b"package4",b"1.0.0",b"p2.v1.0.0.com")
vy_registry.release(b"package5",b"1.0.0",b"p2.v1.0.0.com")
vy_registry.release(b"package6",b"1.0.0",b"p2.v1.0.0.com")
vy_registry.release(b"package7",b"1.0.0",b"p2.v1.0.0.com")
vy_registry.release(b"package4", b"1.0.0", b"p2.v1.0.0.com")
vy_registry.release(b"package5", b"1.0.0", b"p2.v1.0.0.com")
vy_registry.release(b"package6", b"1.0.0", b"p2.v1.0.0.com")
vy_registry.release(b"package7", b"1.0.0", b"p2.v1.0.0.com")
all_pkgs_3 = vy_registry.get_all_package_ids()
assert all_pkgs_3 == (
(b'package1', 2),
Expand All @@ -95,33 +99,34 @@ def test_registry_get_all_packages(vy_registry):
(b'package7', 1),
)


def test_registry_get_all_package_versions(vy_registry):
vy_registry.release(b"package",b"1.0.0",b"p1.v1.0.0.com")
vy_registry.release(b"package",b"1.0.1",b"p1.v1.0.1.com")
vy_registry.release(b"package",b"1.0.2",b"p1.v1.0.2.com")
vy_registry.release(b"package",b"1.1.0",b"p1.v1.1.0.com")
vy_registry.release(b"package",b"2.0.0",b"p1.v2.0.0.com")
vy_registry.release(b"package", b"1.0.0", b"p1.v1.0.0.com")
vy_registry.release(b"package", b"1.0.1", b"p1.v1.0.1.com")
vy_registry.release(b"package", b"1.0.2", b"p1.v1.0.2.com")
vy_registry.release(b"package", b"1.1.0", b"p1.v1.1.0.com")
vy_registry.release(b"package", b"2.0.0", b"p1.v2.0.0.com")
all_rls_1 = vy_registry.get_all_package_versions(b"package")
assert all_rls_1 == (
(b'1.0.0',b'p1.v1.0.0.com'),
(b'1.0.1',b'p1.v1.0.1.com'),
(b'1.0.2',b'p1.v1.0.2.com'),
(b'1.1.0',b'p1.v1.1.0.com'),
(b'2.0.0',b'p1.v2.0.0.com'),
(b'1.0.0', b'p1.v1.0.0.com'),
(b'1.0.1', b'p1.v1.0.1.com'),
(b'1.0.2', b'p1.v1.0.2.com'),
(b'1.1.0', b'p1.v1.1.0.com'),
(b'2.0.0', b'p1.v2.0.0.com'),
)
vy_registry.release(b"package",b"3.0.0",b"p1.v3.0.0.com")
vy_registry.release(b"package",b"4.0.0",b"p1.v4.0.0.com")
vy_registry.release(b"package",b"5.0.0",b"p1.v5.0.0.com")
vy_registry.release(b"package", b"3.0.0", b"p1.v3.0.0.com")
vy_registry.release(b"package", b"4.0.0", b"p1.v4.0.0.com")
vy_registry.release(b"package", b"5.0.0", b"p1.v5.0.0.com")
all_rls_2 = vy_registry.get_all_package_versions(b'package')
assert all_rls_2 == (
(b'1.0.0',b'p1.v1.0.0.com'),
(b'1.0.1',b'p1.v1.0.1.com'),
(b'1.0.2',b'p1.v1.0.2.com'),
(b'1.1.0',b'p1.v1.1.0.com'),
(b'2.0.0',b'p1.v2.0.0.com'),
(b'3.0.0',b'p1.v3.0.0.com'),
(b'4.0.0',b'p1.v4.0.0.com'),
(b'5.0.0',b'p1.v5.0.0.com'),
(b'1.0.0', b'p1.v1.0.0.com'),
(b'1.0.1', b'p1.v1.0.1.com'),
(b'1.0.2', b'p1.v1.0.2.com'),
(b'1.1.0', b'p1.v1.1.0.com'),
(b'2.0.0', b'p1.v2.0.0.com'),
(b'3.0.0', b'p1.v3.0.0.com'),
(b'4.0.0', b'p1.v4.0.0.com'),
(b'5.0.0', b'p1.v5.0.0.com'),
)


Expand All @@ -131,35 +136,35 @@ def test_registry_transfer_owner(vy_registry, w3):


# web3.pm integration

def test_pm_set_registry_without_address(w3):
w3.pm.set_registry()
def test_pm_deploy_and_set_registry(w3):
w3.pm.deploy_and_set_registry()
assert isinstance(w3.pm.registry, Registry)


def test_pm_set_registry_with_address(vy_registry, w3):
w3.pm.set_registry(address=vy_registry.address)
def test_pm_set_registry(vy_registry, w3):
w3.pm.set_registry(address=to_canonical_address(vy_registry.address))
assert isinstance(w3.pm.registry, Registry)


def test_pm_must_set_registry_before_registry_interaction_functions(w3):
with pytest.raises(AttributeError):
w3.pm.release_package(b'package', b'1.0.0', b'google.com')
with pytest.raises(AttributeError):
w3.pm.get_release_data_from_registry(b'package', b'1.0.0')
w3.pm.get_release_data(b'package', b'1.0.0')


def test_pm_release_package(w3):
w3.pm.set_registry()
w3.pm.deploy_and_set_registry()
w3.pm.release_package(b'package', b'1.0.0', b'google.com')
package_data = w3.pm.get_release_data_from_registry(b'package', b'1.0.0')
package_data = w3.pm.get_release_data(b'package', b'1.0.0')
assert package_data[0][:7] == b'package'


def test_pm_release_package_raises_exception_with_invalid_from_address(vy_registry, w3):
w3.pm.set_registry()
w3.pm.deploy_and_set_registry()
w3.eth.defaultAccount = w3.eth.accounts[1]
with pytest.raises(AttributeError):
w3.pm.release_package(b'package', b'1.0.0', b'google.com')

# more integration tests
# w3.pm.get_package
Loading

0 comments on commit c6717c1

Please sign in to comment.