Skip to content

Commit

Permalink
test contracts with binary addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
voith committed Jun 22, 2018
1 parent 5f0e61f commit ddc8a81
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 96 deletions.
22 changes: 17 additions & 5 deletions tests/core/contracts/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

from eth_utils import (
event_signature_to_log_topic,
to_bytes,
)

from web3.utils.toolz import (
identity,
)

CONTRACT_CODE = "0x606060405261022e806100126000396000f360606040523615610074576000357c01000000000000000000000000000000000000000000000000000000009004806316216f391461007657806361bc221a146100995780637cf5dab0146100bc578063a5f3c23b146100e8578063d09de08a1461011d578063dcf537b11461014057610074565b005b610083600480505061016c565b6040518082815260200191505060405180910390f35b6100a6600480505061017f565b6040518082815260200191505060405180910390f35b6100d26004808035906020019091905050610188565b6040518082815260200191505060405180910390f35b61010760048080359060200190919080359060200190919050506101ea565b6040518082815260200191505060405180910390f35b61012a6004805050610201565b6040518082815260200191505060405180910390f35b6101566004808035906020019091905050610217565b6040518082815260200191505060405180910390f35b6000600d9050805080905061017c565b90565b60006000505481565b6000816000600082828250540192505081905550600060005054905080507f3496c3ede4ec3ab3686712aa1c238593ea6a42df83f98a5ec7df9834cfa577c5816040518082815260200191505060405180910390a18090506101e5565b919050565b6000818301905080508090506101fb565b92915050565b600061020d6001610188565b9050610214565b90565b60006007820290508050809050610229565b91905056" # noqa: E501
Expand All @@ -15,6 +20,11 @@
CONTRACT_ABI = json.loads('[{"constant":false,"inputs":[],"name":"return13","outputs":[{"name":"result","type":"int256"}],"type":"function"},{"constant":true,"inputs":[],"name":"counter","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"amt","type":"uint256"}],"name":"increment","outputs":[{"name":"result","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"a","type":"int256"},{"name":"b","type":"int256"}],"name":"add","outputs":[{"name":"result","type":"int256"}],"type":"function"},{"constant":false,"inputs":[],"name":"increment","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"a","type":"int256"}],"name":"multiply7","outputs":[{"name":"result","type":"int256"}],"type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"value","type":"uint256"}],"name":"Increased","type":"event"}]') # noqa: E501


@pytest.fixture(params=[lambda x: to_bytes(hexstr=x), identity])
def address_conversion_func(request):
return request.param


@pytest.fixture(scope="session")
def MATH_CODE():
return CONTRACT_CODE
Expand Down Expand Up @@ -292,17 +302,19 @@ def Emitter(web3_empty, EMITTER):


@pytest.fixture()
def emitter(web3_empty, Emitter, wait_for_transaction, wait_for_block):
def emitter(web3_empty, Emitter, wait_for_transaction, wait_for_block, address_conversion_func):
web3 = web3_empty

wait_for_block(web3)
deploy_txn_hash = Emitter.constructor().transact({'from': web3.eth.coinbase, 'gas': 1000000})
deploy_receipt = wait_for_transaction(web3, deploy_txn_hash)
contract_address = deploy_receipt['contractAddress']
contract_address = address_conversion_func(deploy_receipt['contractAddress'])

bytecode = web3.eth.getCode(contract_address)
assert bytecode == Emitter.bytecode_runtime
return Emitter(address=contract_address)
emitter_contract = Emitter(address=contract_address)
assert emitter_contract.address == contract_address
return emitter_contract


CONTRACT_ARRAYS_SOURCE = """
Expand Down Expand Up @@ -527,8 +539,8 @@ def emitter_log_topics():


@pytest.fixture()
def some_address():
return '0x5B2063246F2191f18F2675ceDB8b28102e957458'
def some_address(address_conversion_func):
return address_conversion_func('0x5B2063246F2191f18F2675ceDB8b28102e957458')


def invoke_contract(api_style=None,
Expand Down
5 changes: 3 additions & 2 deletions tests/core/contracts/test_concise_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ def deploy(web3, Contract, args=None):


@pytest.fixture()
def EMPTY_ADDR():
return '0x' + '00' * 20
def EMPTY_ADDR(address_conversion_func):
addr = '0x' + '00' * 20
return address_conversion_func(addr)


@pytest.fixture()
Expand Down
6 changes: 4 additions & 2 deletions tests/core/contracts/test_contract_ambiguous_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@


@pytest.fixture()
def string_contract(web3, StringContract):
def string_contract(web3, StringContract, address_conversion_func):
deploy_txn = StringContract.constructor("Caqalai").transact()
deploy_receipt = web3.eth.waitForTransactionReceipt(deploy_txn)
assert deploy_receipt is not None
contract = StringContract(address=deploy_receipt['contractAddress'])
contract_address = address_conversion_func(deploy_receipt['contractAddress'])
contract = StringContract(address=contract_address)
assert contract.address == contract_address
assert len(web3.eth.getCode(contract.address)) > 0
return contract

Expand Down
12 changes: 8 additions & 4 deletions tests/core/contracts/test_contract_buildTransaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@


@pytest.fixture()
def math_contract(web3, MathContract):
def math_contract(web3, MathContract, address_conversion_func):
deploy_txn = MathContract.constructor().transact()
deploy_receipt = web3.eth.waitForTransactionReceipt(deploy_txn)
assert deploy_receipt is not None
_math_contract = MathContract(address=deploy_receipt['contractAddress'])
math_contract_address = address_conversion_func(deploy_receipt['contractAddress'])
_math_contract = MathContract(address=math_contract_address)
assert _math_contract.address == math_contract_address
return _math_contract


@pytest.fixture()
def fallback_function_contract(web3, FallballFunctionContract):
def fallback_function_contract(web3, FallballFunctionContract, address_conversion_func):
deploy_txn = FallballFunctionContract.constructor().transact()
deploy_receipt = web3.eth.waitForTransactionReceipt(deploy_txn)
assert deploy_receipt is not None
_fallback_contract = FallballFunctionContract(address=deploy_receipt['contractAddress'])
fallback_contract_address = address_conversion_func(deploy_receipt['contractAddress'])
_fallback_contract = FallballFunctionContract(address=fallback_contract_address)
assert _fallback_contract.address == fallback_contract_address
return _fallback_contract


Expand Down
62 changes: 35 additions & 27 deletions tests/core/contracts/test_contract_call_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,70 @@
from web3.utils.ens import (
contract_ens_addresses,
)
from web3.utils.toolz import (
identity,
)

# Ignore warning in pyethereum 1.6 - will go away with the upgrade
pytestmark = pytest.mark.filterwarnings("ignore:implicit cast from 'char *'")


def deploy(web3, Contract, args=None):
def deploy(web3, Contract, apply_func=identity, args=None):
args = args or []
deploy_txn = Contract.constructor(*args).transact()
deploy_receipt = web3.eth.waitForTransactionReceipt(deploy_txn)
assert deploy_receipt is not None
contract = Contract(address=deploy_receipt['contractAddress'])
address = apply_func(deploy_receipt['contractAddress'])
contract = Contract(address=address)
assert contract.address == address
assert len(web3.eth.getCode(contract.address)) > 0
return contract


@pytest.fixture()
def address_reflector_contract(web3, AddressReflectorContract):
return deploy(web3, AddressReflectorContract)
def address_reflector_contract(web3, AddressReflectorContract, address_conversion_func):
return deploy(web3, AddressReflectorContract, address_conversion_func)


@pytest.fixture()
def math_contract(web3, MathContract):
return deploy(web3, MathContract)
def math_contract(web3, MathContract, address_conversion_func):
return deploy(web3, MathContract, address_conversion_func)


@pytest.fixture()
def string_contract(web3, StringContract):
return deploy(web3, StringContract, args=["Caqalai"])
def string_contract(web3, StringContract, address_conversion_func):
return deploy(web3, StringContract, address_conversion_func, args=["Caqalai"])


@pytest.fixture()
def arrays_contract(web3, ArraysContract):
def arrays_contract(web3, ArraysContract, address_conversion_func):
# bytes_32 = [keccak('0'), keccak('1')]
bytes32_array = [
b'\x04HR\xb2\xa6p\xad\xe5@~x\xfb(c\xc5\x1d\xe9\xfc\xb9eB\xa0q\x86\xfe:\xed\xa6\xbb\x8a\x11m', # noqa: E501
b'\xc8\x9e\xfd\xaaT\xc0\xf2\x0cz\xdfa(\x82\xdf\tP\xf5\xa9Qc~\x03\x07\xcd\xcbLg/)\x8b\x8b\xc6', # noqa: E501
]
byte_arr = [b'\xff', b'\xff', b'\xff', b'\xff']
return deploy(web3, ArraysContract, args=[bytes32_array, byte_arr])
return deploy(web3, ArraysContract, address_conversion_func, args=[bytes32_array, byte_arr])


@pytest.fixture()
def address_contract(web3, WithConstructorAddressArgumentsContract):
return deploy(web3, WithConstructorAddressArgumentsContract, args=[
"0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
])
def address_contract(web3, WithConstructorAddressArgumentsContract, address_conversion_func):
return deploy(
web3,
WithConstructorAddressArgumentsContract,
address_conversion_func,
args=["0xd3CdA913deB6f67967B99D67aCDFa1712C293601"]
)


@pytest.fixture(params=[b'\x04\x06', '0x0406', '0406'])
def bytes_contract(web3, BytesContract, request):
return deploy(web3, BytesContract, args=[request.param])
def bytes_contract(web3, BytesContract, request, address_conversion_func):
return deploy(web3, BytesContract, address_conversion_func, args=[request.param])


@pytest.fixture()
def fixed_reflection_contract(web3, FixedReflectionContract):
return deploy(web3, FixedReflectionContract)
def fixed_reflection_contract(web3, FixedReflectionContract, address_conversion_func):
return deploy(web3, FixedReflectionContract, address_conversion_func)


@pytest.fixture()
Expand All @@ -91,30 +99,30 @@ def call_transaction():
'0406040604060406040604060406040604060406040604060406040604060406',
HexBytes('0406040604060406040604060406040604060406040604060406040604060406'),
])
def bytes32_contract(web3, Bytes32Contract, request):
return deploy(web3, Bytes32Contract, args=[request.param])
def bytes32_contract(web3, Bytes32Contract, request, address_conversion_func):
return deploy(web3, Bytes32Contract, address_conversion_func, args=[request.param])


@pytest.fixture()
def undeployed_math_contract(web3, MathContract):
empty_address = "0x000000000000000000000000000000000000dEaD"
def undeployed_math_contract(web3, MathContract, address_conversion_func):
empty_address = address_conversion_func("0x000000000000000000000000000000000000dEaD")
_undeployed_math_contract = MathContract(address=empty_address)
return _undeployed_math_contract


@pytest.fixture()
def mismatched_math_contract(web3, StringContract, MathContract):
def mismatched_math_contract(web3, StringContract, MathContract, address_conversion_func):
deploy_txn = StringContract.constructor("Caqalai").transact()
deploy_receipt = web3.eth.waitForTransactionReceipt(deploy_txn)
assert deploy_receipt is not None

_mismatched_math_contract = MathContract(address=deploy_receipt['contractAddress'])
address = address_conversion_func(deploy_receipt['contractAddress'])
_mismatched_math_contract = MathContract(address=address)
return _mismatched_math_contract


@pytest.fixture()
def fallback_function_contract(web3, FallballFunctionContract):
return deploy(web3, FallballFunctionContract)
def fallback_function_contract(web3, FallballFunctionContract, address_conversion_func):
return deploy(web3, FallballFunctionContract, address_conversion_func)


def test_invalid_address_in_deploy_arg(web3, WithConstructorAddressArgumentsContract):
Expand Down
60 changes: 42 additions & 18 deletions tests/core/contracts/test_contract_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,40 +64,48 @@ def test_contract_constructor_gas_estimate_with_constructor_with_arguments(

def test_contract_constructor_gas_estimate_with_constructor_with_address_argument(
web3,
WithConstructorAddressArgumentsContract):
WithConstructorAddressArgumentsContract,
address_conversion_func):
gas_estimate = WithConstructorAddressArgumentsContract.constructor(
"0x16D9983245De15E7A9A73bC586E01FF6E08dE737").estimateGas()
address_conversion_func("0x16D9983245De15E7A9A73bC586E01FF6E08dE737")).estimateGas()

deploy_txn = WithConstructorAddressArgumentsContract.constructor(
"0x16D9983245De15E7A9A73bC586E01FF6E08dE737").transact()
address_conversion_func("0x16D9983245De15E7A9A73bC586E01FF6E08dE737")).transact()
txn_receipt = web3.eth.waitForTransactionReceipt(deploy_txn)
gas_used = txn_receipt.get('gasUsed')

assert abs(gas_estimate - gas_used) < 21000


def test_contract_constructor_transact_no_constructor(web3, MathContract, MATH_RUNTIME):
def test_contract_constructor_transact_no_constructor(
web3,
MathContract,
MATH_RUNTIME,
address_conversion_func):
deploy_txn = MathContract.constructor().transact()

txn_receipt = web3.eth.waitForTransactionReceipt(deploy_txn)
assert txn_receipt is not None

assert txn_receipt['contractAddress']
contract_address = txn_receipt['contractAddress']
contract_address = address_conversion_func(txn_receipt['contractAddress'])

blockchain_code = web3.eth.getCode(contract_address)
assert blockchain_code == decode_hex(MATH_RUNTIME)


def test_contract_constructor_transact_with_constructor_without_arguments(
web3, SimpleConstructorContract, SIMPLE_CONSTRUCTOR_RUNTIME):
web3,
SimpleConstructorContract,
SIMPLE_CONSTRUCTOR_RUNTIME,
address_conversion_func):
deploy_txn = SimpleConstructorContract.constructor().transact()

txn_receipt = web3.eth.waitForTransactionReceipt(deploy_txn)
assert txn_receipt is not None

assert txn_receipt['contractAddress']
contract_address = txn_receipt['contractAddress']
contract_address = address_conversion_func(txn_receipt['contractAddress'])

blockchain_code = web3.eth.getCode(contract_address)
assert blockchain_code == decode_hex(SIMPLE_CONSTRUCTOR_RUNTIME)
Expand All @@ -119,15 +127,16 @@ def test_contract_constructor_transact_with_constructor_with_arguments(
constructor_args,
constructor_kwargs,
expected_a,
expected_b):
expected_b,
address_conversion_func):
deploy_txn = WithConstructorArgumentsContract.constructor(
*constructor_args, **constructor_kwargs).transact()

txn_receipt = web3.eth.waitForTransactionReceipt(deploy_txn)
assert txn_receipt is not None

assert txn_receipt['contractAddress']
contract_address = txn_receipt['contractAddress']
contract_address = address_conversion_func(txn_receipt['contractAddress'])

blockchain_code = web3.eth.getCode(contract_address)
assert blockchain_code == decode_hex(WITH_CONSTRUCTOR_ARGUMENTS_RUNTIME)
Expand All @@ -138,12 +147,15 @@ def test_contract_constructor_transact_with_constructor_with_arguments(


def test_contract_constructor_transact_with_constructor_with_address_arguments(
web3, WithConstructorAddressArgumentsContract, WITH_CONSTRUCTOR_ADDRESS_RUNTIME):
web3,
WithConstructorAddressArgumentsContract,
WITH_CONSTRUCTOR_ADDRESS_RUNTIME,
address_conversion_func):
deploy_txn = WithConstructorAddressArgumentsContract.constructor(TEST_ADDRESS).transact()
txn_receipt = web3.eth.waitForTransactionReceipt(deploy_txn)
assert txn_receipt is not None
assert txn_receipt['contractAddress']
contract_address = txn_receipt['contractAddress']
contract_address = address_conversion_func(txn_receipt['contractAddress'])
blockchain_code = web3.eth.getCode(contract_address)
assert blockchain_code == decode_hex(WITH_CONSTRUCTOR_ADDRESS_RUNTIME)
assert TEST_ADDRESS == WithConstructorAddressArgumentsContract(
Expand All @@ -155,8 +167,13 @@ def test_contract_constructor_build_transaction_to_field_error(MathContract):
MathContract.constructor().buildTransaction({'to': '123'})


def test_contract_constructor_build_transaction_no_constructor(web3, MathContract):
txn_hash = MathContract.constructor().transact({'from': web3.eth.accounts[0]})
def test_contract_constructor_build_transaction_no_constructor(
web3,
MathContract,
address_conversion_func):
txn_hash = MathContract.constructor().transact(
{'from': address_conversion_func(web3.eth.accounts[0])}
)
txn = web3.eth.getTransaction(txn_hash)
nonce = web3.eth.getTransactionCount(web3.eth.coinbase)
unsent_txn = MathContract.constructor().buildTransaction({'nonce': nonce})
Expand All @@ -168,9 +185,13 @@ def test_contract_constructor_build_transaction_no_constructor(web3, MathContrac
assert new_txn['nonce'] == nonce


def test_contract_constructor_build_transaction_with_constructor_without_argument(web3,
MathContract):
txn_hash = MathContract.constructor().transact({'from': web3.eth.accounts[0]})
def test_contract_constructor_build_transaction_with_constructor_without_argument(
web3,
MathContract,
address_conversion_func):
txn_hash = MathContract.constructor().transact(
{'from': address_conversion_func(web3.eth.accounts[0])}
)
txn = web3.eth.getTransaction(txn_hash)
nonce = web3.eth.getTransactionCount(web3.eth.coinbase)
unsent_txn = MathContract.constructor().buildTransaction({'nonce': nonce})
Expand All @@ -195,9 +216,12 @@ def test_contract_constructor_build_transaction_with_constructor_with_argument(
web3,
WithConstructorArgumentsContract,
constructor_args,
constructor_kwargs):
constructor_kwargs,
address_conversion_func):
txn_hash = WithConstructorArgumentsContract.constructor(
*constructor_args, **constructor_kwargs).transact({'from': web3.eth.accounts[0]})
*constructor_args, **constructor_kwargs).transact(
{'from': address_conversion_func(web3.eth.accounts[0])}
)
txn = web3.eth.getTransaction(txn_hash)
nonce = web3.eth.getTransactionCount(web3.eth.coinbase)
unsent_txn = WithConstructorArgumentsContract.constructor(
Expand Down
Loading

0 comments on commit ddc8a81

Please sign in to comment.