Skip to content

Commit

Permalink
Merge pull request #1398 from njgheorghita/ethpm-update-registry
Browse files Browse the repository at this point in the history
Update web3.pm to use simple solidity registry implementation
  • Loading branch information
njgheorghita authored Jul 24, 2019
2 parents de6a62f + e096f10 commit fdfe96f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 383 deletions.
1 change: 1 addition & 0 deletions ethpm/assets/registry/2.0.0a1.json

Large diffs are not rendered by default.

149 changes: 7 additions & 142 deletions tests/core/pm-module/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
PyEVMBackend,
)
from eth_utils import (
function_abi_to_4byte_selector,
to_bytes,
)

Expand All @@ -19,30 +18,19 @@
)
from web3 import Web3
from web3.pm import (
SolidityReferenceRegistry,
VyperReferenceRegistry,
)
from web3.tools import (
linker,
SimpleRegistry,
)
from web3.tools.pytest_ethereum.deployer import (
Deployer,
)

VY_PACKAGE_ID_1 = to_bytes(hexstr='0xd059e8a6ea5a8bbf8dd097a7b8922316dcb7f024e8220b56d3c9e7188a6a7640') # noqa: E501
VY_PACKAGE_ID_2 = to_bytes(hexstr='0x6df709a85698ad921462b8979547e3a873e22e1c73b1cb691f9376847fb2d402') # noqa: E501
VY_PACKAGE_ID_3 = to_bytes(hexstr='0x80e41a42e3b8c3af0ea51c3fd50d481eef1367dd9d3797ba93d35dcf60660882') # noqa: E501
VY_RELEASE_ID_1 = to_bytes(hexstr='0x595e26f1b2247bacc57e32807f8059e0b0837dd9c47e6946994196bd29c9ca97') # noqa: E501
VY_RELEASE_ID_2 = to_bytes(hexstr='0x04592cb9ced5413e1b09e87b089bf696a05efb76ee87c8c41259635fac6d938a') # noqa: E501
VY_RELEASE_ID_3 = to_bytes(hexstr='0xa06dbc51f8891894778ce03dc23d706d228c99789595758bd3dcac3a93f47f0a') # noqa: E501
VY_RELEASE_ID_4 = to_bytes(hexstr='0x9c6f87dda6435b2506e81206b39ed782a44b92d826c24ab02bbded321b86e2ad') # noqa: E501
SOL_PACKAGE_ID_1 = to_bytes(hexstr='0x60c5112b61159e6b42d54d945078394e9d5fc9c6ff0f3df78977006f8bbc06d4') # noqa: E501
SOL_PACKAGE_ID_2 = to_bytes(hexstr='0xdbcfb0bd7115bf659350d77bb22bb889ca8294f61b0ca480f8a47bb8fc904cc9') # noqa: E501
SOL_PACKAGE_ID_3 = to_bytes(hexstr='0xf3e4002c48a7f8f3485d62988317849c175340b66517c3b2993f725643eba84b') # noqa: E501
SOL_RELEASE_ID_1 = to_bytes(hexstr='0x73835668f71c7ae85cbdcdbb5a9905fa420ffe85a847d283fa9beefcd56cacc4') # noqa: E501
SOL_RELEASE_ID_2 = to_bytes(hexstr='0xe5ef0292a3b36b6ac2be07ee92df61be15e0b9f102df32cbe3f0c012ef69d462') # noqa: E501
SOL_RELEASE_ID_3 = to_bytes(hexstr='0x1280148e0af5c47e95b41df15734d0726c7320fc4cf21efe3923fb047b53899d') # noqa: E501
SOL_RELEASE_ID_4 = to_bytes(hexstr='0x7082e954e4fd6adf8a25c6cefe218f32ec66d8a197197f1d05aa67a65caf5111') # noqa: E501
SOL_RELEASE_ID_1 = to_bytes(hexstr='0x13414014c4f3c0ee41f1ede8e612e0377ae741f3abaa8d22e84e6b3759334fe9') # noqa: E501
SOL_RELEASE_ID_2 = to_bytes(hexstr='0x30cb63a88e721b461e294fa212af64f12e9500b3892e0e65fa70090ab63afb4d') # noqa: E501
SOL_RELEASE_ID_3 = to_bytes(hexstr='0x73f5fafa3d9bd5080d9b27c092cd65fdbf7c8f982df4d5d0de22eb2cd56f4fcb') # noqa: E501
SOL_RELEASE_ID_4 = to_bytes(hexstr='0x7fc4e4c04e1a4e5cba315f8fce216f8a77e1a1dd7c6539635555f95d1042667f') # noqa: E501


def setup_w3():
Expand All @@ -59,118 +47,14 @@ def setup_w3():
return w3


def solidity_registry_strategy():
def set_authority(package):
w3 = package.w3
authority = package.deployments.get_instance("WhitelistAuthority").address
package_registry = package.deployments.get_instance("PackageRegistry")
package_db = package.deployments.get_instance("PackageDB")
release_db = package.deployments.get_instance("ReleaseDB")
txh_1 = package_registry.functions.setAuthority(authority).transact()
w3.eth.waitForTransactionReceipt(txh_1)
txh_2 = package_db.functions.setAuthority(authority).transact()
w3.eth.waitForTransactionReceipt(txh_2)
txh_3 = release_db.functions.setAuthority(authority).transact()
w3.eth.waitForTransactionReceipt(txh_3)

def set_dependencies(package):
w3 = package.w3
package_db = package.deployments.get_instance("PackageDB").address
release_db = package.deployments.get_instance("ReleaseDB").address
release_validator = package.deployments.get_instance("ReleaseValidator").address
package_registry = package.deployments.get_instance("PackageRegistry")
txh_1 = package_registry.functions.setPackageDb(package_db).transact()
w3.eth.waitForTransactionReceipt(txh_1)
txh_2 = package_registry.functions.setReleaseDb(release_db).transact()
w3.eth.waitForTransactionReceipt(txh_2)
txh_3 = package_registry.functions.setReleaseValidator(
release_validator
).transact()
w3.eth.waitForTransactionReceipt(txh_3)

def get_selector(deployments, contract, fn):
function_abi = [
x for x in deployments.get_instance(contract).abi if x["name"] == fn
][0]
return function_abi_to_4byte_selector(function_abi)

def set_permissions(package):
w3 = package.w3
deployments = package.deployments
set_version = get_selector(deployments, "ReleaseDB", "setVersion")
set_release = get_selector(deployments, "ReleaseDB", "setRelease")
set_package = get_selector(deployments, "PackageDB", "setPackage")
set_package_owner = get_selector(deployments, "PackageDB", "setPackageOwner")
release = get_selector(deployments, "PackageRegistry", "release")
transfer_package_owner = get_selector(
deployments, "PackageRegistry", "transferPackageOwner"
)
package_db = package.deployments.get_instance("PackageDB").address
release_db = package.deployments.get_instance("ReleaseDB").address
package_registry = package.deployments.get_instance("PackageRegistry").address
authority = package.deployments.get_instance("WhitelistAuthority")
txh_1 = authority.functions.setCanCall(
package_registry, release_db, set_release, True
).transact()
w3.eth.waitForTransactionReceipt(txh_1)
txh_2 = authority.functions.setCanCall(
package_registry, package_db, set_package, True
).transact()
w3.eth.waitForTransactionReceipt(txh_2)
txh_3 = authority.functions.setCanCall(
package_registry, package_db, set_package_owner, True
).transact()
w3.eth.waitForTransactionReceipt(txh_3)
txh_4 = authority.functions.setAnyoneCanCall(
release_db, set_version, True
).transact()
w3.eth.waitForTransactionReceipt(txh_4)
txh_5 = authority.functions.setAnyoneCanCall(
package_registry, release, True
).transact()
w3.eth.waitForTransactionReceipt(txh_5)
txh_6 = authority.functions.setAnyoneCanCall(
package_registry, transfer_package_owner, True
).transact()
w3.eth.waitForTransactionReceipt(txh_6)

strategy = linker.linker(
linker.deploy("IndexedOrderedSetLib"),
linker.link("PackageDB", "IndexedOrderedSetLib"),
linker.link("ReleaseDB", "IndexedOrderedSetLib"),
linker.deploy("PackageRegistry"),
linker.deploy("WhitelistAuthority"),
linker.deploy("PackageDB"),
linker.deploy("ReleaseDB"),
linker.deploy("ReleaseValidator"),
linker.run_python(set_authority),
linker.run_python(set_dependencies),
linker.run_python(set_permissions),
)
return strategy


def sol_registry(w3):
manifest = json.loads((ASSETS_DIR / "registry" / "1.0.0.json").read_text())
strategy = solidity_registry_strategy()
manifest = json.loads((ASSETS_DIR / "registry" / "2.0.0a1.json").read_text())
registry_package = Package(manifest, w3)
registry_deployer = Deployer(registry_package)
registry_deployer.register_strategy("PackageRegistry", strategy)
deployed_registry_package = registry_deployer.deploy("PackageRegistry")
assert isinstance(registry_package, Package)
registry = deployed_registry_package.deployments.get_instance("PackageRegistry")
return SolidityReferenceRegistry(registry.address, w3)


def vy_registry(w3):
registry_path = ASSETS_DIR / "vyper_registry"
manifest = json.loads((registry_path / "0.1.0.json").read_text().rstrip('\n'))
registry_package = Package(manifest, w3)
registry_deployer = Deployer(registry_package)
deployed_registry_package = registry_deployer.deploy("registry")
registry_instance = deployed_registry_package.deployments.get_instance("registry")
assert registry_instance.functions.owner().call() == w3.eth.defaultAccount
return VyperReferenceRegistry(registry_instance.address, w3)
return SimpleRegistry(registry.address, w3)


def release_packages(registry):
Expand Down Expand Up @@ -214,13 +98,9 @@ def release_packages(registry):
# Tests are written against the sample packages released in `release_packages()` above, if more
# tests are needed, they should take into account the releases that exist on a "loaded registry".
W3 = setup_w3()
FRESH_VY_REGISTRY = vy_registry(W3)
FRESH_SOL_REGISTRY = sol_registry(W3)
LOADED_VY_REGISTRY = release_packages(vy_registry(W3))
LOADED_SOL_REGISTRY = release_packages(sol_registry(W3))
VY_PKG_IDS = (VY_PACKAGE_ID_1, VY_PACKAGE_ID_2, VY_PACKAGE_ID_3)
SOL_PKG_IDS = (SOL_PACKAGE_ID_1, SOL_PACKAGE_ID_2, SOL_PACKAGE_ID_3)
VY_RLS_IDS = (VY_RELEASE_ID_1, VY_RELEASE_ID_2, VY_RELEASE_ID_3, VY_RELEASE_ID_4)
SOL_RLS_IDS = (SOL_RELEASE_ID_1, SOL_RELEASE_ID_2, SOL_RELEASE_ID_3, SOL_RELEASE_ID_4)


Expand All @@ -229,26 +109,11 @@ def w3():
return W3


@pytest.fixture
def empty_vy_registry():
return FRESH_VY_REGISTRY


@pytest.fixture
def empty_sol_registry():
return FRESH_SOL_REGISTRY


@pytest.fixture
def loaded_vy_registry():
return LOADED_VY_REGISTRY, VY_PKG_IDS, VY_RLS_IDS


@pytest.fixture
def loaded_sol_registry():
return LOADED_SOL_REGISTRY, SOL_PKG_IDS, SOL_RLS_IDS


@pytest.fixture
def registry_getter(request):
return request.getfixturevalue(request.param)
4 changes: 2 additions & 2 deletions tests/core/pm-module/test_ens_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
InvalidAddress,
)
from web3.pm import (
VyperReferenceRegistry,
SimpleRegistry,
)


Expand Down Expand Up @@ -131,7 +131,7 @@ def test_web3_ens(ens):
w3 = ens.web3
ns = ENS.fromWeb3(w3, ens.ens.address)
w3.ens = ns
registry = VyperReferenceRegistry.deploy_new_instance(w3)
registry = SimpleRegistry.deploy_new_instance(w3)
w3.ens.setup_address('tester.eth', registry.address)
actual_addr = ens.address('tester.eth')
w3.pm.set_registry('tester.eth')
Expand Down
77 changes: 23 additions & 54 deletions tests/core/pm-module/test_registry.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
import pytest

from eth_utils import (
is_address,
)

from web3.pm import (
ERCRegistry,
VyperReferenceRegistry,
ERC1319Registry,
SimpleRegistry,
)


def test_vyper_registry_deploy_new_instance(w3):
registry = VyperReferenceRegistry.deploy_new_instance(w3)
assert isinstance(registry, ERCRegistry)
assert isinstance(registry, VyperReferenceRegistry)
def test_simple_registry_deploy_new_instance(w3):
registry = SimpleRegistry.deploy_new_instance(w3)
assert isinstance(registry, SimpleRegistry)
assert isinstance(registry, ERC1319Registry)
assert is_address(registry.address)


def test_vyper_registry_auth(w3):
registry = VyperReferenceRegistry.deploy_new_instance(w3)
assert registry.owner() == w3.eth.accounts[0]
registry.transfer_owner(w3.eth.accounts[1])
assert registry.owner() == w3.eth.accounts[1]


@pytest.mark.parametrize(
"registry_getter", ["empty_vy_registry", "empty_sol_registry"], indirect=True
)
def test_registry_releases_properly(registry_getter):
registry = registry_getter
release_id_1 = registry._release(
def test_registry_releases_properly(empty_sol_registry):
release_id_1 = empty_sol_registry._release(
"package", "1.0.0", "ipfs://Qme4otpS88NV8yQi8TfTP89EsQC5bko3F5N1yhRoi6cwGV"
)
release_id_2 = registry._release(
release_id_2 = empty_sol_registry._release(
"package1", "1.0.1", "ipfs://Qme4otpS88NV8yQi8TfTP89EsQC5bko3F5N1yhRoi6cwGZ"
)
release_data_1 = registry._get_release_data(release_id_1)
release_data_2 = registry._get_release_data(release_id_2)
release_data_1 = empty_sol_registry._get_release_data(release_id_1)
release_data_2 = empty_sol_registry._get_release_data(release_id_2)
assert release_data_1[0] == "package"
assert release_data_1[1] == "1.0.0"
assert release_data_1[2] == "ipfs://Qme4otpS88NV8yQi8TfTP89EsQC5bko3F5N1yhRoi6cwGV"
Expand All @@ -45,11 +32,8 @@ def test_registry_releases_properly(registry_getter):
assert release_data_2[2] == "ipfs://Qme4otpS88NV8yQi8TfTP89EsQC5bko3F5N1yhRoi6cwGZ"


@pytest.mark.parametrize(
"registry_getter", ["loaded_vy_registry", "loaded_sol_registry"], indirect=True
)
def test_registry_get_all_package_ids_and_get_package_name(registry_getter):
registry, expected_ids, _ = registry_getter
def test_registry_get_all_package_ids_and_get_package_name(loaded_sol_registry):
registry, expected_ids, _ = loaded_sol_registry
package_ids = registry._get_all_package_ids()
assert len(package_ids) == 6
assert package_ids[0] == expected_ids[0]
Expand All @@ -60,11 +44,8 @@ def test_registry_get_all_package_ids_and_get_package_name(registry_getter):
assert registry._get_package_name(package_ids[2]) == "package2"


@pytest.mark.parametrize(
"registry_getter", ["loaded_vy_registry", "loaded_sol_registry"], indirect=True
)
def test_registry_get_release_id_and_get_all_release_ids(registry_getter):
registry, _, expected_ids = registry_getter
def test_registry_get_release_id_and_get_all_release_ids(loaded_sol_registry):
registry, _, expected_ids = loaded_sol_registry
release_ids = registry._get_all_release_ids("package")
assert len(release_ids) == 6
assert release_ids[:3] == expected_ids[:3]
Expand All @@ -73,40 +54,28 @@ def test_registry_get_release_id_and_get_all_release_ids(registry_getter):
assert registry._get_release_id("package", "1.0.2") == expected_ids[2]


@pytest.mark.parametrize(
"registry_getter", ["loaded_vy_registry", "loaded_sol_registry"], indirect=True
)
def test_registry_num_package_ids(registry_getter):
registry, _, _ = registry_getter
def test_registry_num_package_ids(loaded_sol_registry):
registry, _, _ = loaded_sol_registry
assert registry._num_package_ids() == 6


@pytest.mark.parametrize(
"registry_getter", ["loaded_vy_registry", "loaded_sol_registry"], indirect=True
)
def test_registry_num_release_ids(registry_getter):
registry, _, _ = registry_getter
def test_registry_num_release_ids(loaded_sol_registry):
registry, _, _ = loaded_sol_registry
assert registry._num_release_ids("package") == 6
assert registry._num_release_ids("package1") == 1
assert registry._num_release_ids("package2") == 1


@pytest.mark.parametrize(
"registry_getter", ["loaded_vy_registry", "loaded_sol_registry"], indirect=True
)
def test_registry_generate_release_id(registry_getter):
registry, _, expected_ids = registry_getter
def test_registry_generate_release_id(loaded_sol_registry):
registry, _, expected_ids = loaded_sol_registry
assert registry._generate_release_id("package", "1.0.0") == expected_ids[0]
assert registry._generate_release_id("package", "1.0.1") == expected_ids[1]
assert registry._generate_release_id("package", "1.0.2") == expected_ids[2]
assert registry._generate_release_id("does-not-exist", "1.0.0") == expected_ids[3]


@pytest.mark.parametrize(
"registry_getter", ["loaded_vy_registry", "loaded_sol_registry"], indirect=True
)
def test_registry_get_release_data(registry_getter):
registry, _, release_ids = registry_getter
def test_registry_get_release_data(loaded_sol_registry):
registry, _, release_ids = loaded_sol_registry
release_data_1 = registry._get_release_data(release_ids[0])
release_data_2 = registry._get_release_data(release_ids[1])
release_data_3 = registry._get_release_data(release_ids[2])
Expand Down
Loading

0 comments on commit fdfe96f

Please sign in to comment.