Skip to content

Commit

Permalink
Merge pull request #1275 from jacqueswww/bump_pyevm_versions
Browse files Browse the repository at this point in the history
Bump pyevm versions for tests
  • Loading branch information
jacqueswww committed Feb 27, 2019
2 parents 028b8ec + e937d14 commit c02c1dc
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 147 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
@@ -1,5 +1,5 @@
[tool:pytest]
addopts = --cov-report term --cov-report html --cov=vyper
addopts = -n auto --cov-report term --cov-report html --cov=vyper
python_files = test_*.py
testpaths = tests

Expand Down
11 changes: 6 additions & 5 deletions setup.py
Expand Up @@ -4,11 +4,12 @@


test_deps = [
'pytest',
'pytest-cov',
'py-evm==0.2.0a34',
'eth-tester==0.1.0b33',
'web3==4.8.2',
'pytest>=3.6',
'pytest-cov==2.4.0',
'pytest-xdist==1.18.1',
'py-evm==0.2.0a39',
'eth-tester==0.1.0b37',
'web3==5.0.0a6'
]


Expand Down
132 changes: 65 additions & 67 deletions tests/conftest.py
@@ -1,7 +1,5 @@
import eth_tester
import logging
import pytest
import web3

from functools import wraps

Expand All @@ -18,9 +16,12 @@
from web3 import (
Web3,
)
from web3._utils.toolz import (
compose,
)
from web3.contract import (
ConciseContract,
ConciseMethod
Contract,
mk_collision_prop,
)
from vyper.parser.parser_utils import (
LLLnode
Expand All @@ -32,9 +33,13 @@
)


class VyperMethod(ConciseMethod):
class VyperMethod:
ALLOWED_MODIFIERS = {'call', 'estimateGas', 'transact', 'buildTransaction'}

def __init__(self, function, normalizers=None):
self._function = function
self._function._return_data_normalizers = normalizers

def __call__(self, *args, **kwargs):
return self.__prepared_function(*args, **kwargs)

Expand All @@ -54,89 +59,82 @@ def __prepared_function(self, *args, **kwargs):
return getattr(self._function(*args), modifier)(modifier_dict)


class VyperContract(ConciseContract):
class VyperContract:

def __init__(self, classic_contract, method_class=VyperMethod):
super().__init__(classic_contract, method_class)
"""
An alternative Contract Factory which invokes all methods as `call()`,
unless you add a keyword argument. The keyword argument assigns the prep method.
This call
############
# PATCHING #
############
> contract.withdraw(amount, transact={'from': eth.accounts[1], 'gas': 100000, ...})
setattr(eth_tester.backends.pyevm.main, 'GENESIS_GAS_LIMIT', 10**9)
setattr(eth_tester.backends.pyevm.main, 'GENESIS_DIFFICULTY', 1)
is equivalent to this call in the classic contract:
> contract.functions.withdraw(amount).transact({'from': eth.accounts[1], 'gas': 100000, ...})
"""
def __init__(self, classic_contract, method_class=VyperMethod):

def set_evm_verbose_logging():
logger = logging.getLogger('evm')
logger.setLevel('TRACE')
classic_contract._return_data_normalizers += CONCISE_NORMALIZERS
self._classic_contract = classic_contract
self.address = self._classic_contract.address

protected_fn_names = [fn for fn in dir(self) if not fn.endswith('__')]

# Useful options to comment out whilst working:
set_evm_verbose_logging()
# from vdb import vdb
# vdb.set_evm_opcode_debugger()
for fn_name in self._classic_contract.functions:

# Override namespace collisions
if fn_name in protected_fn_names:
_concise_method = mk_collision_prop(fn_name)

@pytest.fixture(autouse=True)
def patch_log_filter_remove(monkeypatch):
else:
_classic_method = getattr(
self._classic_contract.functions,
fn_name)

def Filter_remove(self, *values):
_concise_method = method_class(
_classic_method,
self._classic_contract._return_data_normalizers
)

def get_key(v):
return v.get('transaction_hash'), v.get('log_index'), v.get('transaction_index')
setattr(self, fn_name, _concise_method)

values_to_remove = set([
get_key(value)
for value in values
])
@classmethod
def factory(cls, *args, **kwargs):
return compose(cls, Contract.factory(*args, **kwargs))

queued_values = self.get_changes()
self.values = [
value
for value
in self.get_all()
if get_key(value) not in values_to_remove
]
for value in queued_values:
if get_key(value) in values_to_remove:
continue
self.queue.put_nowait(value)

monkeypatch.setattr(eth_tester.utils.filters.Filter, 'remove', Filter_remove)
def _none_addr(datatype, data):
if datatype == 'address' and int(data, base=16) == 0:
return (datatype, None)
else:
return (datatype, data)


@pytest.fixture(autouse=True)
def patch_is_encodeable_for_fixed(monkeypatch):
original_is_encodable = web3.utils.abi.is_encodable
CONCISE_NORMALIZERS = (
_none_addr,
)

def utils_abi_is_encodable(_type, value):
from eth_utils import is_integer
from eth_abi.abi import process_type
try:
base, sub, arrlist = _type
except ValueError:
base, sub, arrlist = process_type(_type)
############
# PATCHING #
############

if not arrlist:
if base == 'fixed' and not arrlist:
return True
elif base == 'int':
if not is_integer(value):
return False
exp = int(sub)
if value < -1 * 2**(exp - 1) or value > 2**(exp - 1) + 1:
return False
return True
# setattr(eth_tester.backends.pyevm.main, 'GENESIS_GAS_LIMIT', 10**9)
# setattr(eth_tester.backends.pyevm.main, 'GENESIS_DIFFICULTY', 1)


def set_evm_verbose_logging():
logger = logging.getLogger('evm')
logger.setLevel('TRACE')

# default behaviour
return original_is_encodable(_type, value)

monkeypatch.setattr(web3.utils.abi, 'is_encodable', utils_abi_is_encodable)
# Useful options to comment out whilst working:
# set_evm_verbose_logging()
# from vdb import vdb
# vdb.set_evm_opcode_debugger()


@pytest.fixture(scope="module")
@pytest.fixture
def tester():
t = EthereumTester()
return t
Expand All @@ -146,7 +144,7 @@ def zero_gas_price_strategy(web3, transaction_params=None):
return 0 # zero gas price makes testing simpler.


@pytest.fixture(scope="module")
@pytest.fixture
def w3(tester):
w3 = Web3(EthereumTesterProvider(tester))
w3.eth.setGasPriceStrategy(zero_gas_price_strategy)
Expand All @@ -155,7 +153,7 @@ def w3(tester):

@pytest.fixture
def keccak():
return Web3.sha3
return Web3.keccak


@pytest.fixture
Expand Down
22 changes: 11 additions & 11 deletions tests/examples/auctions/test_blind_auction.py
Expand Up @@ -39,7 +39,7 @@ def test_late_bid(w3, auction_contract, assert_tx_failed):

# Try to bid after bidding has ended
assert_tx_failed(lambda: auction_contract.bid(
w3.sha3(b''.join([
w3.keccak(b''.join([
(200).to_bytes(32, byteorder='big'),
(0).to_bytes(32, byteorder='big'),
(8675309).to_bytes(32, byteorder='big')
Expand All @@ -54,7 +54,7 @@ def test_too_many_bids(w3, auction_contract, assert_tx_failed):
# First 128 bids should be able to be placed successfully
for i in range(MAX_BIDS):
auction_contract.bid(
w3.sha3(b''.join([
w3.keccak(b''.join([
(i).to_bytes(32, byteorder='big'),
(1).to_bytes(32, byteorder='big'),
(8675309).to_bytes(32, byteorder='big')
Expand All @@ -64,7 +64,7 @@ def test_too_many_bids(w3, auction_contract, assert_tx_failed):

# 129th bid should fail
assert_tx_failed(lambda: auction_contract.bid(
w3.sha3(b''.join([
w3.keccak(b''.join([
(128).to_bytes(32, byteorder='big'),
(0).to_bytes(32, byteorder='big'),
(8675309).to_bytes(32, byteorder='big')
Expand All @@ -78,7 +78,7 @@ def test_early_reval(w3, auction_contract, assert_tx_failed):

# k1 places 1 real bid
auction_contract.bid(
w3.sha3(b''.join([
w3.keccak(b''.join([
(100).to_bytes(32, byteorder='big'),
(0).to_bytes(32, byteorder='big'),
(8675309).to_bytes(32, byteorder='big')
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_late_reveal(w3, auction_contract, assert_tx_failed):

# k1 places 1 real bid
auction_contract.bid(
w3.sha3(b''.join([
w3.keccak(b''.join([
(100).to_bytes(32, byteorder='big'),
(0).to_bytes(32, byteorder='big'),
(8675309).to_bytes(32, byteorder='big')
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_blind_auction(w3, auction_contract):

# k1 places 1 real bid
auction_contract.bid(
w3.sha3(b''.join([
w3.keccak(b''.join([
(100).to_bytes(32, byteorder='big'),
(0).to_bytes(32, byteorder='big'),
(8675309).to_bytes(32, byteorder='big')
Expand All @@ -194,23 +194,23 @@ def test_blind_auction(w3, auction_contract):

# k2 places 1 real bid (highest) and 2 fake
auction_contract.bid(
w3.sha3(b''.join([
w3.keccak(b''.join([
(150).to_bytes(32, byteorder='big'),
(1).to_bytes(32, byteorder='big'),
(1234567).to_bytes(32, byteorder='big')
])),
transact={'value': 150, 'from': k2}
)
auction_contract.bid(
w3.sha3(b''.join([
w3.keccak(b''.join([
(200).to_bytes(32, byteorder='big'),
(0).to_bytes(32, byteorder='big'),
(1234567).to_bytes(32, byteorder='big')
])),
transact={'value': 250, 'from': k2}
)
auction_contract.bid(
w3.sha3(b''.join([
w3.keccak(b''.join([
(300).to_bytes(32, byteorder='big'),
(1).to_bytes(32, byteorder='big'),
(1234567).to_bytes(32, byteorder='big')
Expand All @@ -220,15 +220,15 @@ def test_blind_auction(w3, auction_contract):

# k3 places 2 fake bids
auction_contract.bid(
w3.sha3(b''.join([
w3.keccak(b''.join([
(175).to_bytes(32, byteorder='big'),
(1).to_bytes(32, byteorder='big'),
(9876543).to_bytes(32, byteorder='big')
])),
transact={'value': 175, 'from': k3}
)
auction_contract.bid(
w3.sha3(b''.join([
w3.keccak(b''.join([
(275).to_bytes(32, byteorder='big') +
(1).to_bytes(32, byteorder='big') +
(9876543).to_bytes(32, byteorder='big')
Expand Down

0 comments on commit c02c1dc

Please sign in to comment.