From d549d5fabda3eaa87e2843540a28871474a31a7c Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Tue, 26 Feb 2019 12:11:43 +0200 Subject: [PATCH 1/9] Remove old patch fixes. --- tests/conftest.py | 58 ----------------------------------------------- 1 file changed, 58 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index eb88f01466..33b9eeaa19 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,6 @@ import eth_tester import logging import pytest -import web3 from functools import wraps @@ -79,63 +78,6 @@ def set_evm_verbose_logging(): # vdb.set_evm_opcode_debugger() -@pytest.fixture(autouse=True) -def patch_log_filter_remove(monkeypatch): - - def Filter_remove(self, *values): - - def get_key(v): - return v.get('transaction_hash'), v.get('log_index'), v.get('transaction_index') - - values_to_remove = set([ - get_key(value) - for value in values - ]) - - 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) - - -@pytest.fixture(autouse=True) -def patch_is_encodeable_for_fixed(monkeypatch): - original_is_encodable = web3.utils.abi.is_encodable - - 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) - - 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 - - # default behaviour - return original_is_encodable(_type, value) - - monkeypatch.setattr(web3.utils.abi, 'is_encodable', utils_abi_is_encodable) - - @pytest.fixture(scope="module") def tester(): t = EthereumTester() From ea94b8273d5c8b5d22007fc2f8051bff9378fbba Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Tue, 26 Feb 2019 15:03:23 +0200 Subject: [PATCH 2/9] Fix on chain market tests: - Improve balances tests, to use before balance into account, which is easier to read and supports all pytest scoping. - Switch large values to use w3.toWei() as this makes logic much clearer. --- .../test_on_chain_market_maker.py | 66 ++++++++++--------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/tests/examples/market_maker/test_on_chain_market_maker.py b/tests/examples/market_maker/test_on_chain_market_maker.py index b91bf33544..6971cf2a45 100644 --- a/tests/examples/market_maker/test_on_chain_market_maker.py +++ b/tests/examples/market_maker/test_on_chain_market_maker.py @@ -31,27 +31,27 @@ def test_initial_statet(market_maker): def test_initiate(w3, market_maker, erc20, assert_tx_failed): a0 = w3.eth.accounts[0] - erc20.approve(market_maker.address, 2 * 10**18, transact={}) - market_maker.initiate(erc20.address, 1 * 10**18, transact={'value': 2 * 10**18}) - assert market_maker.totalEthQty() == 2 * 10**18 - assert market_maker.totalTokenQty() == 1 * 10**18 + erc20.approve(market_maker.address, w3.toWei(2, "ether"), transact={}) + market_maker.initiate(erc20.address, w3.toWei(1, "ether"), transact={'value': w3.toWei(2, "ether")}) + assert market_maker.totalEthQty() == w3.toWei(2, "ether") + assert market_maker.totalTokenQty() == w3.toWei(1, "ether") assert market_maker.invariant() == 2 * 10**36 assert market_maker.owner() == a0 assert erc20.name() == TOKEN_NAME assert erc20.decimals() == TOKEN_DECIMALS # Initiate cannot be called twice - assert_tx_failed(lambda: market_maker.initiate(erc20.address, 1 * 10**18, transact={'value': 2 * 10**18})) + assert_tx_failed(lambda: market_maker.initiate(erc20.address, w3.toWei(1, "ether"), transact={'value': w3.toWei(2, "ether")})) def test_eth_to_tokens(w3, market_maker, erc20): a1 = w3.eth.accounts[1] - erc20.approve(market_maker.address, 2 * 10**18, transact={}) - market_maker.initiate(erc20.address, 1 * 10**18, transact={'value': 2 * 10**18}) - assert erc20.balanceOf(market_maker.address) == 1000000000000000000 + erc20.approve(market_maker.address, w3.toWei(2, "ether"), transact={}) + market_maker.initiate(erc20.address, w3.toWei(1, "ether"), transact={'value': w3.toWei(2, "ether")}) + assert erc20.balanceOf(market_maker.address) == w3.toWei(1, "ether") assert erc20.balanceOf(a1) == 0 - assert market_maker.totalTokenQty() == 1000000000000000000 - assert market_maker.totalEthQty() == 2000000000000000000 + assert market_maker.totalTokenQty() == w3.toWei(1, "ether") + assert market_maker.totalEthQty() == w3.toWei(2, "ether") market_maker.ethToTokens(transact={'value': 100, 'from': a1}) assert erc20.balanceOf(market_maker.address) == 999999999999999950 @@ -62,30 +62,34 @@ def test_eth_to_tokens(w3, market_maker, erc20): def test_tokens_to_eth(w3, tester, market_maker, erc20): a1 = w3.eth.accounts[1] - erc20.transfer(a1, 2 * 10**18, transact={}) - erc20.approve(market_maker.address, 2 * 10**18, transact={'from': a1}) - market_maker.initiate(erc20.address, 1 * 10**18, transact={'value': 2 * 10**18, 'from': a1}) - assert w3.eth.getBalance(market_maker.address) == 2000000000000000000 - assert w3.eth.getBalance(a1) == 999997999999999999999900 - assert market_maker.totalTokenQty() == 1000000000000000000 + a1_balance_before = w3.eth.getBalance(a1) - erc20.approve(market_maker.address, 1 * 10**18, transact={'from': a1}) - market_maker.tokensToEth(1 * 10**18, transact={'from': a1}) - assert w3.eth.getBalance(market_maker.address) == 1000000000000000000 - assert w3.eth.getBalance(a1) == 999998999999999999999900 - assert market_maker.totalTokenQty() == 2000000000000000000 - assert market_maker.totalEthQty() == 1000000000000000000 + erc20.transfer(a1, w3.toWei(2, "ether"), transact={}) + erc20.approve(market_maker.address, w3.toWei(2, "ether"), transact={'from': a1}) + market_maker.initiate(erc20.address, w3.toWei(1, "ether"), transact={'value': w3.toWei(2, "ether"), 'from': a1}) + assert w3.eth.getBalance(market_maker.address) == w3.toWei(2, "ether") + assert w3.eth.getBalance(a1) == a1_balance_before - w3.toWei(2, "ether") # sent 2 eth, with initiate. + assert market_maker.totalTokenQty() == w3.toWei(1, "ether") + + erc20.approve(market_maker.address, w3.toWei(1, "ether"), transact={'from': a1}) + market_maker.tokensToEth(w3.toWei(1, "ether"), transact={'from': a1}) + assert w3.eth.getBalance(market_maker.address) == w3.toWei(1, "ether") # 1 eth less in market. + assert w3.eth.getBalance(a1) == a1_balance_before - w3.toWei(1, "ether") # got 1 eth back, for trade. + assert market_maker.totalTokenQty() == w3.toWei(2, "ether") # Tokens increased by 1 + assert market_maker.totalEthQty() == w3.toWei(1, "ether") def test_owner_withdraw(w3, tester, market_maker, erc20, assert_tx_failed): a0, a1 = w3.eth.accounts[:2] - erc20.approve(market_maker.address, 2 * 10**18, transact={}) - market_maker.initiate(erc20.address, 1 * 10**18, transact={'value': 2 * 10**18}) - assert w3.eth.getBalance(a0) == 999994000000000000000000 - assert erc20.balanceOf(a0) == 20999999000000000000000000 - - # Only owner can call ownerWithdraw - assert_tx_failed(lambda: market_maker.ownerWithdraw(transact={'from': a1})) + a0_balance_before = w3.eth.getBalance(a0) + # Approve 2 eth transfers. + erc20.approve(market_maker.address, w3.toWei(2, "ether"), transact={}) + # Initiate market with 2 eth value. + market_maker.initiate(erc20.address, w3.toWei(1, "ether"), transact={'value': w3.toWei(2, "ether")}) + assert w3.eth.getBalance(a0) == a0_balance_before - w3.toWei(2, "ether") # 2 eth was sent to market_maker contract. + assert erc20.balanceOf(a0) == TOKEN_TOTAL_SUPPLY - w3.toWei(1, "ether") # a0's balance is locked up in market_maker contract. + + assert_tx_failed(lambda: market_maker.ownerWithdraw(transact={'from': a1})) # Only owner can call ownerWithdraw market_maker.ownerWithdraw(transact={}) - assert w3.eth.getBalance(a0) == 999996000000000000000000 - assert erc20.balanceOf(a0) == 21000000000000000000000000 + assert w3.eth.getBalance(a0) == a0_balance_before # Eth balance restored. + assert erc20.balanceOf(a0) == TOKEN_TOTAL_SUPPLY # Tokens returned to a0. From bb145d74d8beca70062b1a4e22164291331f65ff Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Tue, 26 Feb 2019 15:28:49 +0200 Subject: [PATCH 3/9] Fix safe remote tests. - Use constants depending on sync test run. Uses pre-balance variables now instead. --- .../test_safe_remote_purchase.py | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/tests/examples/safe_remote_purchase/test_safe_remote_purchase.py b/tests/examples/safe_remote_purchase/test_safe_remote_purchase.py index fbcb92f7b4..8e029507e0 100644 --- a/tests/examples/safe_remote_purchase/test_safe_remote_purchase.py +++ b/tests/examples/safe_remote_purchase/test_safe_remote_purchase.py @@ -9,11 +9,6 @@ import pytest -# Inital balance of accounts -INIT_BAL_a0 = 1000000000000000000000000 -INIT_BAL_a1 = 1000000000000000000000000 - - @pytest.fixture def contract_code(get_contract): with open("examples/safe_remote_purchase/safe_remote_purchase.vy") as f: @@ -22,20 +17,19 @@ def contract_code(get_contract): @pytest.fixture -def check_balance(w3, tester): - def check_balance(): +def get_balance(w3, tester): + def get_balance(): a0, a1 = w3.eth.accounts[:2] # balance of a1 = seller, a2 = buyer return w3.eth.getBalance(a0), w3.eth.getBalance(a1) - return check_balance + return get_balance -def test_initial_state(w3, assert_tx_failed, get_contract, check_balance, contract_code): - assert check_balance() == (INIT_BAL_a0, INIT_BAL_a1) +def test_initial_state(w3, assert_tx_failed, get_contract, get_balance, contract_code): # Inital deposit has to be divisible by two assert_tx_failed(lambda: get_contract(contract_code, value=13)) # Seller puts item up for sale - a0_pre_bal, a1_pre_bal = check_balance() + a0_pre_bal, a1_pre_bal = get_balance() c = get_contract(contract_code, value_in_eth=2) # Check that the seller is set correctly assert c.seller() == w3.eth.accounts[0] @@ -44,26 +38,29 @@ def test_initial_state(w3, assert_tx_failed, get_contract, check_balance, contra # Check if unlocked() works correctly after initialization assert c.unlocked() is True # Check that sellers (and buyers) balance is correct - assert check_balance() == ((INIT_BAL_a0 - w3.toWei(2, 'ether')), INIT_BAL_a1) + assert get_balance() == ((a1_pre_bal - w3.toWei(2, 'ether')), a1_pre_bal) -def test_abort(w3, assert_tx_failed, check_balance, get_contract, contract_code): +def test_abort(w3, assert_tx_failed, get_balance, get_contract, contract_code): a0, a1, a2 = w3.eth.accounts[:3] - c = get_contract(contract_code, value=2) + + a0_pre_bal, a1_pre_bal = get_balance() + c = get_contract(contract_code, value=w3.toWei(2, 'ether')) + assert c.value() == w3.toWei(1, 'ether') # Only sender can trigger refund assert_tx_failed(lambda: c.abort(transact={'from': a2})) # Refund works correctly c.abort(transact={'from': a0, 'gasPrice': 0}) - assert check_balance() == (INIT_BAL_a0 - w3.toWei(2, 'ether'), INIT_BAL_a1) + assert get_balance() == (a0_pre_bal, a1_pre_bal) # Purchase in process, no refund possible c = get_contract(contract_code, value=2) c.purchase(transact={'value': 2, 'from': a1, 'gasPrice': 0}) assert_tx_failed(lambda: c.abort(transact={'from': a0})) -def test_purchase(w3, get_contract, assert_tx_failed, check_balance, contract_code): +def test_purchase(w3, get_contract, assert_tx_failed, get_balance, contract_code): a0, a1, a2, a3 = w3.eth.accounts[:4] - init_bal_a0, init_bal_a1 = check_balance() + init_bal_a0, init_bal_a1 = get_balance() c = get_contract(contract_code, value=2) # Purchase for too low/high price assert_tx_failed(lambda: c.purchase(transact={'value': 1, 'from': a1})) @@ -75,14 +72,14 @@ def test_purchase(w3, get_contract, assert_tx_failed, check_balance, contract_co # Check if contract is locked correctly assert c.unlocked() is False # Check balances, both deposits should have been deducted - assert check_balance() == (init_bal_a0 - 2, init_bal_a1 - 2) + assert get_balance() == (init_bal_a0 - 2, init_bal_a1 - 2) # Allow nobody else to purchase assert_tx_failed(lambda: c.purchase(transact={'value': 2, 'from': a3})) -def test_received(w3, get_contract, assert_tx_failed, check_balance, contract_code): +def test_received(w3, get_contract, assert_tx_failed, get_balance, contract_code): a0, a1 = w3.eth.accounts[:2] - init_bal_a0, init_bal_a1 = check_balance() + init_bal_a0, init_bal_a1 = get_balance() c = get_contract(contract_code, value=2) # Can only be called after purchase assert_tx_failed(lambda: c.received(transact={'from': a1, 'gasPrice': 0})) @@ -93,10 +90,10 @@ def test_received(w3, get_contract, assert_tx_failed, check_balance, contract_co # Check if buyer can call receive c.received(transact={'from': a1, 'gasPrice': 0}) # Final check if everything worked. 1 value has been transferred - assert check_balance() == (init_bal_a0 + 1, init_bal_a1 - 1) + assert get_balance() == (init_bal_a0 + 1, init_bal_a1 - 1) -def test_received_reentrancy(w3, get_contract, assert_tx_failed, check_balance, contract_code): +def test_received_reentrancy(w3, get_contract, assert_tx_failed, get_balance, contract_code): buyer_contract_code = """ contract PurchaseContract: From 4e12c8b581132f9c7a9e1c24c0c9f1d1dd62d048 Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Tue, 26 Feb 2019 15:39:22 +0200 Subject: [PATCH 4/9] Remove old patches. Remove module pytest fixture scoping. --- tests/conftest.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 33b9eeaa19..78affbdd39 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,3 @@ -import eth_tester import logging import pytest @@ -63,8 +62,8 @@ def __init__(self, classic_contract, method_class=VyperMethod): # PATCHING # ############ -setattr(eth_tester.backends.pyevm.main, 'GENESIS_GAS_LIMIT', 10**9) -setattr(eth_tester.backends.pyevm.main, 'GENESIS_DIFFICULTY', 1) +# 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(): @@ -73,12 +72,12 @@ def set_evm_verbose_logging(): # Useful options to comment out whilst working: -set_evm_verbose_logging() +# 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 @@ -88,7 +87,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) From fdb9bb169c9bee84cee34c834ebe55848221b95d Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Tue, 26 Feb 2019 16:14:31 +0200 Subject: [PATCH 5/9] Bump py-evm / eth-tester versions. --- setup.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index f36fd5a9a8..72b678d094 100644 --- a/setup.py +++ b/setup.py @@ -6,9 +6,10 @@ test_deps = [ 'pytest', 'pytest-cov', - 'py-evm==0.2.0a34', - 'eth-tester==0.1.0b33', - 'web3==4.8.2', + 'pytest-xdist==1.26.1', + 'py-evm==0.2.0a39', + 'eth-tester==0.1.0b37', + 'web3==5.0.0a6' ] From 22e746625da56e1bbcee0f837c04fec9cd2047ef Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Tue, 26 Feb 2019 16:15:23 +0200 Subject: [PATCH 6/9] Use xdist for make test. --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 78affbdd39..aa185668cc 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -96,7 +96,7 @@ def w3(tester): @pytest.fixture def keccak(): - return Web3.sha3 + return Web3.keccak @pytest.fixture From 325a8bea9b67365c28f791db84f7f494967fe1ff Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Tue, 26 Feb 2019 16:15:32 +0200 Subject: [PATCH 7/9] Use web3.keccak instead. --- Makefile | 2 +- tests/examples/auctions/test_blind_auction.py | 22 +++++++++---------- tests/examples/wallet/test_wallet.py | 4 ++-- tests/parser/features/test_assert.py | 6 ++--- .../functions/test_default_parameters.py | 5 ----- 5 files changed, 17 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index cef5327578..18d082117d 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ init: python setup.py install test: - python setup.py test + python setup.py test --addopts "-n auto" lint: flake8 vyper tests --ignore=E122,E124,E127,E128,E501,E731,W504 diff --git a/tests/examples/auctions/test_blind_auction.py b/tests/examples/auctions/test_blind_auction.py index a8e40fcdfb..7e67daf2d6 100644 --- a/tests/examples/auctions/test_blind_auction.py +++ b/tests/examples/auctions/test_blind_auction.py @@ -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') @@ -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') @@ -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') @@ -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') @@ -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') @@ -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') @@ -194,7 +194,7 @@ 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') @@ -202,7 +202,7 @@ def test_blind_auction(w3, auction_contract): 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') @@ -210,7 +210,7 @@ def test_blind_auction(w3, auction_contract): 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') @@ -220,7 +220,7 @@ 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') @@ -228,7 +228,7 @@ def test_blind_auction(w3, auction_contract): 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') diff --git a/tests/examples/wallet/test_wallet.py b/tests/examples/wallet/test_wallet.py index bf09527423..fe3b46df2e 100644 --- a/tests/examples/wallet/test_wallet.py +++ b/tests/examples/wallet/test_wallet.py @@ -89,8 +89,8 @@ def test_javascript_signatures(w3, get_contract): ) for x in map(lambda z: w3.toBytes(hexstr=z[2:]), raw_sigs) ] - h = w3.sha3((0).to_bytes(32, "big") + b'\x00' * 12 + w3.toBytes(hexstr=recipient[2:]) + (25).to_bytes(32, "big") + b'') - h2 = w3.sha3(b"\x19Ethereum Signed Message:\n32" + h) + h = w3.keccak((0).to_bytes(32, "big") + b'\x00' * 12 + w3.toBytes(hexstr=recipient[2:]) + (25).to_bytes(32, "big") + b'') + h2 = w3.keccak(b"\x19Ethereum Signed Message:\n32" + h) # Check to make sure the signatures are valid assert is_same_address(Account.recoverHash(h2, sigs[0]), accounts[0]) diff --git a/tests/parser/features/test_assert.py b/tests/parser/features/test_assert.py index b8e593aeec..3a04ee5aa6 100644 --- a/tests/parser/features/test_assert.py +++ b/tests/parser/features/test_assert.py @@ -48,15 +48,15 @@ def test2(a: int128, b: int128) -> int128: with pytest.raises(TransactionFailed) as e_info: c.test(0) - assert e_info.value.args[0] == b'larger than one please' + assert e_info.value.args[0] == 'larger than one please' # a = 0, b = 1 with pytest.raises(TransactionFailed) as e_info: c.test2(0, 1) - assert e_info.value.args[0] == b'a is not large enough' + assert e_info.value.args[0] == 'a is not large enough' # a = 1, b = 0 with pytest.raises(TransactionFailed) as e_info: c.test2(2, 2) - assert e_info.value.args[0] == b'b may only be 1' + assert e_info.value.args[0] == 'b may only be 1' # return correct value assert c.test2(5, 1) == 17 diff --git a/tests/parser/functions/test_default_parameters.py b/tests/parser/functions/test_default_parameters.py index 35abe193fe..4ad6bf4eb8 100644 --- a/tests/parser/functions/test_default_parameters.py +++ b/tests/parser/functions/test_default_parameters.py @@ -110,15 +110,10 @@ def bar(a: int128, b: int128 = -1) -> (int128, int128): assert c.bar(-123) == [-123, -1] assert c.bar(100, 100) == [100, 100] - # bypass abi encoding checking: - def utils_abi_is_encodable(_type, value): - return True - def validate_value(cls, value): pass monkeypatch.setattr('eth_abi.encoding.NumberEncoder.validate_value', validate_value) - monkeypatch.setattr('web3.utils.abi.is_encodable', utils_abi_is_encodable) assert c.bar(200, 2**127 - 1) == [200, 2**127 - 1] assert_tx_failed(lambda: c.bar(200, 2**127)) From 56f24ee39e765c083a26d0668d2cc4458dbc240b Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Tue, 26 Feb 2019 23:12:23 +0200 Subject: [PATCH 8/9] Move depracted ConciseContract code into VyperContract. --- tests/conftest.py | 67 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index aa185668cc..392941886b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -16,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 @@ -30,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) @@ -52,11 +59,61 @@ def __prepared_function(self, *args, **kwargs): return getattr(self._function(*args), modifier)(modifier_dict) -class VyperContract(ConciseContract): +class VyperContract: + + """ + 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 + + > contract.withdraw(amount, transact={'from': eth.accounts[1], 'gas': 100000, ...}) + 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): - super().__init__(classic_contract, method_class) + 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('__')] + + 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) + + else: + _classic_method = getattr( + self._classic_contract.functions, + fn_name) + + _concise_method = method_class( + _classic_method, + self._classic_contract._return_data_normalizers + ) + + setattr(self, fn_name, _concise_method) + + @classmethod + def factory(cls, *args, **kwargs): + return compose(cls, Contract.factory(*args, **kwargs)) + + +def _none_addr(datatype, data): + if datatype == 'address' and int(data, base=16) == 0: + return (datatype, None) + else: + return (datatype, data) + + +CONCISE_NORMALIZERS = ( + _none_addr, +) ############ # PATCHING # From e937d14a6b88c917ea042452dada0c896cd4700a Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Wed, 27 Feb 2019 01:15:45 +0200 Subject: [PATCH 9/9] Move xdist parameter to setup.cfg. --- Makefile | 2 +- setup.cfg | 2 +- setup.py | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 18d082117d..cef5327578 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ init: python setup.py install test: - python setup.py test --addopts "-n auto" + python setup.py test lint: flake8 vyper tests --ignore=E122,E124,E127,E128,E501,E731,W504 diff --git a/setup.cfg b/setup.cfg index 3c90a124ba..68987778a0 100644 --- a/setup.cfg +++ b/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 diff --git a/setup.py b/setup.py index 72b678d094..b262b41d3c 100644 --- a/setup.py +++ b/setup.py @@ -4,9 +4,9 @@ test_deps = [ - 'pytest', - 'pytest-cov', - 'pytest-xdist==1.26.1', + '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'