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

Write registry contract #3

Merged
merged 3 commits into from
Oct 16, 2018
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
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
include_package_data=True,
install_requires=[
"eth-utils>=1,<2",
"ethpm==0.1.3a6",
"pytest-ethereum>=0.1.2,<2",
"ethpm==0.1.3a7",
"pytest-ethereum==0.1.2-alpha.3",
"web3[tester]==4.4.1",
],
setup_requires=['setuptools-markdown'],
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from twig import CONTRACTS_DIR
from web3 import Web3

TEST_CONTRACTS_DIR = Path(__file__).parent / "contracts"
Expand All @@ -22,3 +23,8 @@ def tmp_contracts(tmpdir):
tmp = p.join(contract.name)
tmp.write(contract.read_text())
return p


@pytest.fixture
def deployer(twig_deployer):
return twig_deployer(CONTRACTS_DIR)
13 changes: 6 additions & 7 deletions tests/core/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,21 @@ def test_compiler(compiler):
assert isinstance(compiler.backend, VyperBackend)


def test_compiler_creates_valid_registry_package_and_deployment(twig_deployer):
registry_package, address = twig_deployer.deploy("registry")
w3 = registry_package.w3
registry_contract = registry_package.get_contract_instance("registry", address)
def test_compiler_creates_valid_registry_package_and_deployment(twig_deployer, w3):
registry_package = twig_deployer.deploy("registry")
registry_contract = registry_package.deployments.get_contract_instance("registry")
registry_contract.functions.register(b"xxx", w3.eth.accounts[0]).transact()
actual = registry_contract.functions.lookup(b"xxx").call()
assert actual == w3.eth.accounts[0]


def test_compiler_creates_valid_auction_package_and_deployment(twig_deployer, w3):
auction_package, address = twig_deployer.deploy(
auction_package = twig_deployer.deploy(
"simple_open_auction", w3.eth.accounts[0], 100
)
w3 = auction_package.w3
auction_contract = auction_package.get_contract_instance(
"simple_open_auction", address
auction_contract = auction_package.deployments.get_contract_instance(
"simple_open_auction"
)
auction_start = auction_contract.functions.auction_start().call()
auction_end = auction_contract.functions.auction_end().call()
Expand Down
200 changes: 200 additions & 0 deletions tests/core/test_registry_contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import logging

import pytest

import pytest_ethereum as pte
from pytest_ethereum.testing import Log

PACKAGE_ID = b'\xd0Y\xe8\xa6\xeaZ\x8b\xbf\x8d\xd0\x97\xa7\xb8\x92#\x16\xdc\xb7\xf0$\xe8"\x0bV\xd3\xc9\xe7\x18\x8ajv@' # noqa: E501
V1_RELEASE_ID = b"Y^&\xf1\xb2${\xac\xc5~2\x80\x7f\x80Y\xe0\xb0\x83}\xd9\xc4~iF\x99A\x96\xbd)\xc9\xca\x97" # noqa: E501
V2_RELEASE_ID = b"\x04Y,\xb9\xce\xd5A>\x1b\t\xe8{\x08\x9b\xf6\x96\xa0^\xfbv\xee\x87\xc8\xc4\x12Yc_\xacm\x93\x8a" # noqa: E501

logging.getLogger("evm").setLevel(logging.INFO)


@pytest.fixture
def registry(deployer):
pkg = deployer.deploy("registry")
return pkg.deployments.get_contract_instance("registry")


def test_registry_init(registry, w3):
assert registry.functions.owner().call() == w3.eth.accounts[0]


def test_registry_get_release_id(registry):
# requires release to exist
with pte.tx_fail():
registry.functions.getReleaseId(b"package", b"1.0.0").call()
with pte.tx_fail():
registry.functions.getReleaseId(b"package", b"1.0.1").call()
# cut releases
registry.functions.release(b"package", b"1.0.0", b"google.com").transact()
registry.functions.release(b"package", b"1.0.1", b"google.com").transact()
v1_release_id = registry.functions.getReleaseId(b"package", b"1.0.0").call()
v2_release_id = registry.functions.getReleaseId(b"package", b"1.0.1").call()
assert v1_release_id == V1_RELEASE_ID
assert v2_release_id == V2_RELEASE_ID


def test_registry_generate_release_id(registry):
# doesn't require release to exist
v1_release_id = registry.functions.generateReleaseId(b"package", b"1.0.0").call()
v2_release_id = registry.functions.generateReleaseId(b"package", b"1.0.1").call()
assert v1_release_id == V1_RELEASE_ID
assert v2_release_id == V2_RELEASE_ID


def test_registry_get_package_name(registry):
registry.functions.release(b"package", b"1.0.0", b"google.com").transact()
package_name = registry.functions.getPackageName(PACKAGE_ID).call()
assert package_name.rstrip(b"\x00") == b"package"


def test_registry_get_package_data(registry):
registry.functions.release(b"package", b"1.0.0", b"google.com").transact()
registry.functions.release(b"package", b"1.0.1", b"google1.com").transact()
package_data = registry.functions.getPackageData(b"package").call()
assert package_data[0].rstrip(b"\x00") == b"package"
assert package_data[1] == PACKAGE_ID
assert package_data[2] == 2


def test_registry_get_package_name_raises_exception_if_package_doesnt_exist(registry):
with pte.tx_fail():
registry.functions.getPackageName(PACKAGE_ID).call()


def test_registry_release(registry):
release_id = registry.functions.release(b"package", b"1.0.0", b"google.com").call()
assert release_id == V1_RELEASE_ID


def test_registry_get_all_package_ids(registry):
registry.functions.release(b"package", b"1.0.0", b"google.com").transact()
registry.functions.release(b"package_2", b"1.0.0", b"cnn.com").transact()
all_package_ids = registry.functions.getAllPackageIds(0, 5).call()
assert all_package_ids[0] == PACKAGE_ID
assert all_package_ids[1] != b"\x00" * 32
assert all_package_ids[2] == b"\x00" * 32
assert all_package_ids[3] == b"\x00" * 32
assert all_package_ids[4] == b"\x00" * 32
# test with different offset
all_offset_ids = registry.functions.getAllPackageIds(1, 5).call()
assert all_offset_ids[0] != b"\x00" * 32
assert all_offset_ids[1] == b"\x00" * 32
assert all_offset_ids[2] == b"\x00" * 32
assert all_offset_ids[3] == b"\x00" * 32
assert all_offset_ids[4] == b"\x00" * 32
# offset must be less than total package count
with pte.tx_fail():
registry.functions.getAllPackageIds(3, 5).call()
# length variable must be 5
with pte.tx_fail():
registry.functions.getAllPackageIds(1, 6).call()


def test_registry_get_all_release_ids(registry):
registry.functions.release(b"package", b"1.0.0", b"google.com").transact()
registry.functions.release(b"package", b"1.0.1", b"google1.com").transact()
registry.functions.release(b"package", b"1.1.0", b"google2.com").transact()
all_release_ids = registry.functions.getAllReleaseIds(b"package", 0, 5).call()
assert all_release_ids[0] == V1_RELEASE_ID
assert all_release_ids[1] == V2_RELEASE_ID
assert all_release_ids[2] != b"\x00" * 32
assert all_release_ids[3] == b"\x00" * 32
assert all_release_ids[4] == b"\x00" * 32
# test with offset
all_release_ids = registry.functions.getAllReleaseIds(b"package", 2, 5).call()
assert all_release_ids[0] != b"\x00" * 32
assert all_release_ids[1] == b"\x00" * 32
assert all_release_ids[2] == b"\x00" * 32
assert all_release_ids[3] == b"\x00" * 32
assert all_release_ids[4] == b"\x00" * 32
# length input arg must be 5
with pte.tx_fail():
registry.functions.getAllReleaseIds(b"package", 0, 4).call()
# package must exist
with pte.tx_fail():
registry.functions.getAllReleaseIds(b"invalid", 0, 5).call()
# offset must be below package release count
with pte.tx_fail():
registry.functions.getAllReleaseIds(b"package", 4, 5).call()


def test_registry_get_release_data(registry):
registry.functions.release(b"package", b"1.0.0", b"google.com").transact()
registry.functions.release(b"package", b"1.0.1", b"google1.com").transact()
v1_data = registry.functions.getReleaseData(V1_RELEASE_ID).call()
v2_data = registry.functions.getReleaseData(V2_RELEASE_ID).call()
assert v1_data[0].rstrip(b"\x00") == b"package"
assert v1_data[1].rstrip(b"\x00") == b"1.0.0"
assert v1_data[2].rstrip(b"\x00") == b"google.com"
assert v2_data[0].rstrip(b"\x00") == b"package"
assert v2_data[1].rstrip(b"\x00") == b"1.0.1"
assert v2_data[2].rstrip(b"\x00") == b"google1.com"


def test_registry_logs_release_event(registry, w3):
tx_hash = registry.functions.release(b"package", b"1.0.0", b"google.com").transact()
receipt = w3.eth.waitForTransactionReceipt(tx_hash)
assert Log(
registry.events.Release,
_package=b"package",
_version=b"1.0.0",
_uri=b"google.com",
).exact_match(receipt)


def test_registry_release_auth(registry, w3):
registry.functions.release(b"package", b"1.0.0", b"google.com").transact()
with pte.tx_fail():
registry.functions.release(b"package", b"1.0.1", b"googlex.com").transact(
{"from": w3.eth.accounts[1]}
)


def test_cannot_release_different_uri_for_same_version(registry):
registry.functions.release(b"package", b"1.0.0", b"google.com").transact()
with pte.tx_fail():
registry.functions.release(b"package", b"1.0.0", b"googlex.com").transact()


def test_registry_update_release(registry):
registry.functions.release(b"package", b"1.0.0", b"google.com").transact()
registry.functions.release(b"package", b"1.0.1", b"yahoo.com").transact()
release_data = registry.functions.getReleaseData(V2_RELEASE_ID).call()
assert release_data[0][:7] == b"package"
assert release_data[1][:5] == b"1.0.1"
assert release_data[2][:9] == b"yahoo.com"


@pytest.mark.parametrize(
"args",
(
(b"package", b"", b"google.com"),
(b"", b"1.0.0", b"google.com"),
(b"package", b"1.0.0", b""),
),
)
def test_release_with_empty_values_raises_exception(registry, args):
with pte.tx_fail():
registry.functions.release(*args).transact()


def test_registry_transfer_owner(registry, w3):
registry.functions.release(b"package", b"1.0.0", b"google.com").transact()
release_count = registry.functions.releaseCount().call()
assert release_count == 1
registry.functions.transferOwner(w3.eth.accounts[1]).transact()
w3.testing.mine(1)
owner = registry.functions.owner().call()
assert owner == w3.eth.accounts[1]
# test you cannot release unless from owner
with pte.tx_fail():
registry.functions.release(b"package", b"1.0.1", b"yahoo.com").transact()
registry.functions.release(b"package", b"1.0.1", b"yahoo.com").transact(
{"from": w3.eth.accounts[1]}
)
release_count = registry.functions.releaseCount().call()
assert release_count == 2
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ whitelist_externals=make
basepython=python
extras=lint
commands=
flake8 {toxinidir}/twig {toxinidir}/tests
flake8 {toxinidir}/twig {toxinidir}/tests --exclude twig/contracts/
black --check --diff {toxinidir}/twig/ --check --diff {toxinidir}/tests/
isort --recursive {toxinidir}/twig {toxinidir}/tests
Loading