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

Migrate tests over from pytest-ethereum #1385

Merged
merged 2 commits into from
Jul 17, 2019
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
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ include requirements.txt

recursive-exclude * __pycache__
recursive-exclude * *.py[co]

recursive-include ethpm/assets/ *
1 change: 0 additions & 1 deletion ethpm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
ETHPM_DIR = Path(__file__).parent
ASSETS_DIR = ETHPM_DIR / "assets"
SPEC_DIR: Path = ASSETS_DIR / "spec"
ETHPM_SPEC_DIR = ETHPM_DIR.parent / "ethpm-spec"

from .package import Package # noqa: F401
from .backends.registry import RegistryURI # noqa: F401
1 change: 1 addition & 0 deletions tests/core/tools/pytest_ethereum/assets/greeter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"contract_types":{"greeter":{"abi":[{"constant":false,"inputs":[],"name":"__init__","outputs":[],"payable":false,"type":"constructor"},{"constant":false,"gas":70954,"inputs":[{"name":"x","type":"bytes"}],"name":"setGreeting","outputs":[],"payable":false,"type":"function"},{"constant":false,"gas":4486,"inputs":[],"name":"greet","outputs":[{"name":"out","type":"bytes"}],"payable":false,"type":"function"}],"deployment_bytecode":{"bytecode":"0x600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a052341561009e57600080fd5b6005610140527f48656c6c6f0000000000000000000000000000000000000000000000000000006101605261014080600060c052602060c020602082510161012060006002818352015b826101205160200211156100fb5761011d565b61012051602002850151610120518501555b81516001018083528114156100e8575b50505050505061032f56600035601c52740100000000000000000000000000000000000000006020526f7fffffffffffffffffffffffffffffff6040527fffffffffffffffffffffffffffffffff8000000000000000000000000000000060605274012a05f1fffffffffffffffffffffffffdabf41c006080527ffffffffffffffffffffffffed5fa0e000000000000000000000000000000000060a05263b8e46d3a600051141561013057602060046101403734156100b457600080fd5b60346004356004016101603760146004356004013511156100d457600080fd5b61016080600060c052602060c020602082510161012060006002818352015b8261012051602002111561010657610128565b61012051602002850151610120518501555b81516001018083528114156100f3575b505050505050005b63cfae3217600051141561020157341561014957600080fd5b60008060c052602060c020610180602082540161012060006002818352015b8261012051602002111561017b5761019d565b61012051850154610120516020028501525b8151600101808352811415610168575b5050505050506101e0610180516014818352015b60146101e05111156101c2576101de565b60006101e0516101a001535b81516001018083528114156101b1575b50506020610160526040610180510160206001820306601f8201039050610160f3005b60006000fd5b61012861032f0361012860003961012861032f036000f3"}}},"deployments":{"blockchain://0817570684a8b349c2e15aa1a5ba48d269335d487bcf0d7a2f3ef1f9f764d5e4/block/03d7c71f9f8d8c29a17c00a0d5ce262ad243a575ce5023c430e58dbb02342901":{"greeter":{"address":"0xf2e246bb76df876cef8b38ae84130f4f55de395b","block":"0x03d7c71f9f8d8c29a17c00a0d5ce262ad243a575ce5023c430e58dbb02342901","contract_type":"greeter","transaction":"0x56e0b0d02c8f11e62937101ec419899b4f4a8be8ceec825eb9afb797f79e7262"}}},"manifest_version":"2","package_name":"greeter","sources":{"./contracts/greeter.vy":"# Vyper Greeter Contract\n\ngreeting: bytes[20]\n\n\n@public\ndef __init__():\n self.greeting = \"Hello\"\n\n\n@public\ndef setGreeting(x: bytes[20]):\n self.greeting = x\n\n\n@public\ndef greet() -> bytes[40]:\n return self.greeting\n"},"version":"1.0.0"}
27 changes: 27 additions & 0 deletions tests/core/tools/pytest_ethereum/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from pathlib import (
Path,
)
import pytest

from ethpm import (
ASSETS_DIR,
)
from web3 import Web3

PYTEST_ETH_TESTS_DIR = Path(__file__).parent


@pytest.fixture
def pte_assets_dir():
return PYTEST_ETH_TESTS_DIR / "assets"


@pytest.fixture
def w3():
return Web3(Web3.EthereumTesterProvider())


@pytest.fixture
def escrow_deployer(deployer):
escrow_manifest_path = ASSETS_DIR / "escrow" / "1.0.2.json"
return deployer(escrow_manifest_path)
83 changes: 83 additions & 0 deletions tests/core/tools/pytest_ethereum/test_deployer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import logging
import pytest

from eth_utils import (
is_address,
)

from ethpm import (
ASSETS_DIR,
)
import web3
from web3.tools.pytest_ethereum.exceptions import (
DeployerError,
)

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


#
# Vyper Contracts
#


# User Code
@pytest.fixture
def greeter(deployer, pte_assets_dir):
return deployer(pte_assets_dir / "greeter.json").deploy("greeter")


def test_user_code_with_fixture(greeter):
greeter_instance = greeter.deployments.get_instance("greeter")
assert isinstance(greeter_instance, web3.contract.Contract)
greeting = greeter_instance.functions.greet().call()
assert greeting == b"Hello"


#
# Solidity Compiler Output
#


# SIMPLE
@pytest.fixture
def owned(deployer):
owned_manifest_path = ASSETS_DIR / "owned" / "1.0.1.json"
owned_deployer = deployer(path=owned_manifest_path)
return owned_deployer.deploy("Owned")


def test_owned_deployer(owned):
owned_contract_instance = owned.deployments.get_instance("Owned")
assert is_address(owned_contract_instance.address)


# CONSTRUCTOR ARGS
@pytest.fixture
def standard_token(deployer):
standard_token_manifest_path = ASSETS_DIR / "standard-token" / "1.0.1.json"
standard_token_deployer = deployer(standard_token_manifest_path)
return standard_token_deployer.deploy("StandardToken", 100)


def test_standard_token_deployer(standard_token):
standard_token_instance = standard_token.deployments.get_instance("StandardToken")
assert standard_token_instance.functions.totalSupply().call() == 100


# LIBRARY
@pytest.fixture
def safe_math(deployer):
safe_math_manifest_path = ASSETS_DIR / "safe-math-lib" / "1.0.1.json"
safe_math_deployer = deployer(safe_math_manifest_path)
return safe_math_deployer.deploy("SafeMathLib")


def test_safe_math_deployer(safe_math):
safe_math_instance = safe_math.deployments.get_instance("SafeMathLib")
assert is_address(safe_math_instance.address)


def test_escrow_deployer_unlinked(escrow_deployer):
with pytest.raises(DeployerError):
escrow_deployer.deploy("Escrow", escrow_deployer.package.w3.eth.accounts[0])
82 changes: 82 additions & 0 deletions tests/core/tools/pytest_ethereum/test_linker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import pytest

from ethpm import (
ASSETS_DIR,
Package,
)
from web3.tools.pytest_ethereum.deployer import (
Deployer,
)
from web3.tools.pytest_ethereum.exceptions import (
DeployerError,
)
from web3.tools.pytest_ethereum.linker import (
deploy,
link,
linker,
run_python,
)


@pytest.fixture
def escrow_deployer(deployer):
escrow_manifest_path = ASSETS_DIR / "escrow" / "1.0.3.json"
return deployer(escrow_manifest_path)


def test_linker(escrow_deployer, w3):
# todo test multiple links in one type
assert isinstance(escrow_deployer, Deployer)
with pytest.raises(DeployerError):
escrow_deployer.deploy("Escrow")

escrow_strategy = linker(
deploy("SafeSendLib"),
link("Escrow", "SafeSendLib"),
deploy("Escrow", w3.eth.accounts[0]),
)
assert hasattr(escrow_strategy, "__call__")
escrow_deployer.register_strategy("Escrow", escrow_strategy)
linked_escrow_package = escrow_deployer.deploy("Escrow")
assert isinstance(linked_escrow_package, Package)
linked_escrow_factory = linked_escrow_package.get_contract_factory("Escrow")
assert linked_escrow_factory.needs_bytecode_linking is False


def test_linker_with_from(escrow_deployer, w3):
escrow_strategy = linker(
deploy("SafeSendLib"),
link("Escrow", "SafeSendLib"),
deploy("Escrow", w3.eth.accounts[0], transaction={"from": w3.eth.accounts[5]}),
)
escrow_deployer.register_strategy("Escrow", escrow_strategy)
linked_escrow_package = escrow_deployer.deploy("Escrow")
escrow_instance = linked_escrow_package.deployments.get_instance("Escrow")
assert escrow_instance.functions.sender().call() == w3.eth.accounts[5]


def test_linker_with_callback(escrow_deployer, w3):
sender = w3.eth.accounts[0]
recipient = w3.eth.accounts[5]

def callback_fn(package):
escrow_instance = package.deployments.get_instance("Escrow")
tx_hash = escrow_instance.functions.releaseFunds().transact({"from": sender})
w3.eth.waitForTransactionReceipt(tx_hash)

escrow_strategy = linker(
deploy("SafeSendLib", transaction={"from": sender}),
link("Escrow", "SafeSendLib"),
deploy(
"Escrow",
recipient,
transaction={"from": sender, "value": w3.toWei("1", "ether")},
),
run_python(callback_fn),
)
escrow_deployer.register_strategy("Escrow", escrow_strategy)
assert w3.eth.getBalance(recipient) == w3.toWei("1000000", "ether")
linked_escrow_package = escrow_deployer.deploy("Escrow")
escrow_instance = linked_escrow_package.deployments.get_instance("Escrow")
assert escrow_instance.functions.sender().call() == sender
assert w3.eth.getBalance(recipient) == w3.toWei("1000001", "ether")
90 changes: 90 additions & 0 deletions tests/core/tools/pytest_ethereum/test_linker_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import pytest

from eth_utils import (
remove_0x_prefix,
to_hex,
)
from eth_utils.toolz import (
assoc,
)

from ethpm.uri import (
create_latest_block_uri,
)
from web3.tools.pytest_ethereum._utils import (
contains_matching_uri,
insert_deployment,
pluck_matching_uri,
)
from web3.tools.pytest_ethereum.exceptions import (
LinkerError,
)


@pytest.fixture
def chain_setup(w3):
old_chain_id = remove_0x_prefix(to_hex(w3.eth.getBlock(0)["hash"]))
block_hash = remove_0x_prefix(to_hex(w3.eth.getBlock("earliest").hash))
old_chain_uri = f"blockchain://{old_chain_id}/block/{block_hash}"
match_data = {
old_chain_uri: {"x": "x"},
f"blockchain://1234/block/{block_hash}": {"x": "x"},
}
no_match_data = {
f"blockchain://56775ac59d0774e6b603a79c4218efeb5653b99ba0ff14db983bac2662251a8a/block/{block_hash}": { # noqa: E501
"x": "x"
}
}
return w3, match_data, no_match_data, old_chain_uri


def test_pluck_matching_uri(chain_setup):
w3, match_data, no_match_data, old_chain_uri = chain_setup

assert pluck_matching_uri(match_data, w3) == old_chain_uri
with pytest.raises(LinkerError):
assert pluck_matching_uri(no_match_data, w3)


def test_contains_matching_uri(chain_setup):
w3, match_data, no_match_data, _ = chain_setup

assert contains_matching_uri(match_data, w3) is True
assert contains_matching_uri(no_match_data, w3) is False


def test_insert_deployment(escrow_deployer):
w3 = escrow_deployer.package.w3
escrow_package = escrow_deployer.package
init_deployment_data = {
"contract_type": "Escrow",
"address": "0x",
"transaction": "0x",
"block": "0x",
}
new_deployment_data = {
"contract_type": "Escrow",
"address": "0x123",
"transaction": "0x123",
"block": "0x123",
}
w3.testing.mine(1)
init_block_uri = create_latest_block_uri(w3, 0)
alt_block_uri = init_block_uri[:15] + "yxz123" + init_block_uri[21:]
init_block_deployment_data = {
init_block_uri: {"Other": {"x": "x"}, "Escrow": init_deployment_data},
alt_block_uri: {"alt": {"x": "x"}},
}
w3.testing.mine(1)
new_block_uri = create_latest_block_uri(w3, 0)
escrow_package.manifest = assoc(
escrow_package.manifest, "deployments", init_block_deployment_data
)
updated_manifest = insert_deployment(
escrow_package, "Escrow", new_deployment_data, new_block_uri
)
expected_deployments_data = {
new_block_uri: {"Other": {"x": "x"}, "Escrow": new_deployment_data},
alt_block_uri: {"alt": {"x": "x"}},
}
assert updated_manifest["deployments"] == expected_deployments_data