Skip to content

Commit

Permalink
Rename transaction_dict to transaction.
Browse files Browse the repository at this point in the history
Make caller tests better
  • Loading branch information
kclowes committed Feb 11, 2019
1 parent 6edf8a8 commit 6844214
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 67 deletions.
4 changes: 2 additions & 2 deletions docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -783,15 +783,15 @@ It can also be invoked using parentheses:
>>> twentyone
21

And a transaction dictionary, with or without the ``transaction_dict`` keyword. For example:
And a transaction dictionary, with or without the ``transaction`` keyword. For example:

.. doctest::

>>> from_address = w3.eth.accounts[1]
>>> twentyone = myContract.caller({'from': from_address}).multiply7(3)
>>> twentyone
21
>>> twentyone = myContract.caller(transaction_dict={'from': from_address}).multiply7(3)
>>> twentyone = myContract.caller(transaction={'from': from_address}).multiply7(3)
>>> twentyone
21

Expand Down
55 changes: 34 additions & 21 deletions tests/core/contracts/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,54 +527,67 @@ def FallballFunctionContract(web3, FALLBACK_FUNCTION_CONTRACT):
return web3.eth.contract(**FALLBACK_FUNCTION_CONTRACT)


CONTRACT_RETURN_ARGS_SOURCE = """
CONTRACT_CALLER_TESTER_SOURCE = """
contract CallerTester {
function add(int256 a, int256 b) public payable returns (int256) {
return a + b;
int public count;
function add(int256 a, int256 b) public payable returns (int256) {
return a + b;
}
function increment() public returns (int256) {
return count += 1;
}
function counter() public payable returns (int256) {
return count;
}
function returnMeta() public payable returns (address, bytes memory, uint256, uint, uint) {
return (msg.sender, msg.data, gasleft(), msg.value, block.number);
return (msg.sender, msg.data, gasleft(), msg.value, block.number);
}
}
"""

CONTRACT_RETURN_ARGS_CODE = "608060405234801561001057600080fd5b506101d4806100206000396000f3fe608060405260043610610045577c01000000000000000000000000000000000000000000000000000000006000350463a5f3c23b811461004a578063c7fa7d661461007f575b600080fd5b61006d6004803603604081101561006057600080fd5b5080359060200135610147565b60408051918252519081900360200190f35b61008761014b565b604051808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200180602001858152602001848152602001838152602001828103825286818151815260200191508051906020019080838360005b838110156101085781810151838201526020016100f0565b50505050905090810190601f1680156101355780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390f35b0190565b600060606000806000336000365a344385955084848080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250989e929d50949b509299509097509550505050505056fea165627a7a7230582009a1e09c8cb9406971ab18e5c44bbc09e03adbae9b66c9d6b68a9fa503d495c90029" # noqa: E501

CONTRACT_RETURN_ARGS_RUNTIME = "608060405260043610610045577c01000000000000000000000000000000000000000000000000000000006000350463a5f3c23b811461004a578063c7fa7d661461007f575b600080fd5b61006d6004803603604081101561006057600080fd5b5080359060200135610147565b60408051918252519081900360200190f35b61008761014b565b604051808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200180602001858152602001848152602001838152602001828103825286818151815260200191508051906020019080838360005b838110156101085781810151838201526020016100f0565b50505050905090810190601f1680156101355780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390f35b0190565b600060606000806000336000365a344385955084848080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250989e929d50949b509299509097509550505050505056fea165627a7a7230582009a1e09c8cb9406971ab18e5c44bbc09e03adbae9b66c9d6b68a9fa503d495c90029" # noqa: E501
CONTRACT_CALLER_TESTER_CODE = "608060405234801561001057600080fd5b50610241806100206000396000f3fe608060405260043610610066577c0100000000000000000000000000000000000000000000000000000000600035046306661abd811461006b57806361bc221a14610092578063a5f3c23b1461009a578063c7fa7d66146100bd578063d09de08a14610185575b600080fd5b34801561007757600080fd5b5061008061019a565b60408051918252519081900360200190f35b6100806101a0565b610080600480360360408110156100b057600080fd5b50803590602001356101a6565b6100c56101aa565b604051808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200180602001858152602001848152602001838152602001828103825286818151815260200191508051906020019080838360005b8381101561014657818101518382015260200161012e565b50505050905090810190601f1680156101735780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390f35b34801561019157600080fd5b50610080610207565b60005481565b60005490565b0190565b600060606000806000336000365a344385955084848080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250989e929d50949b5092995090975095505050505050565b60008054600101908190559056fea165627a7a72305820ffe1620e420efa326b9c5e4ef9f93cac71cf986196246c7966d71a39259899b10029" # noqa: E501


CONTRACT_CALLER_TESTER_RUNTIME = "608060405260043610610066577c0100000000000000000000000000000000000000000000000000000000600035046306661abd811461006b57806361bc221a14610092578063a5f3c23b1461009a578063c7fa7d66146100bd578063d09de08a14610185575b600080fd5b34801561007757600080fd5b5061008061019a565b60408051918252519081900360200190f35b6100806101a0565b610080600480360360408110156100b057600080fd5b50803590602001356101a6565b6100c56101aa565b604051808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200180602001858152602001848152602001838152602001828103825286818151815260200191508051906020019080838360005b8381101561014657818101518382015260200161012e565b50505050905090810190601f1680156101735780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390f35b34801561019157600080fd5b50610080610207565b60005481565b60005490565b0190565b600060606000806000336000365a344385955084848080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250989e929d50949b5092995090975095505050505050565b60008054600101908190559056fea165627a7a72305820ffe1620e420efa326b9c5e4ef9f93cac71cf986196246c7966d71a39259899b10029" # noqa: E501


CONTRACT_RETURN_ARGS_ABI = json.loads('[ { "constant": false, "inputs": [ { "name": "a", "type": "int256" }, { "name": "b", "type": "int256" } ], "name": "add", "outputs": [ { "name": "", "type": "int256" } ], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [], "name": "returnMeta", "outputs": [ { "name": "", "type": "address" }, { "name": "", "type": "bytes" }, { "name": "", "type": "uint256" }, { "name": "", "type": "uint256" }, { "name": "", "type": "uint256" } ], "payable": true, "stateMutability": "payable", "type": "function" } ]') # noqa: E501
CONTRACT_CALLER_TESTER_ABI = json.loads('[ { "constant": true, "inputs": [], "name": "count", "outputs": [ { "name": "", "type": "int256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [], "name": "counter", "outputs": [ { "name": "", "type": "int256" } ], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [ { "name": "a", "type": "int256" }, { "name": "b", "type": "int256" } ], "name": "add", "outputs": [ { "name": "", "type": "int256" } ], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [], "name": "returnMeta", "outputs": [ { "name": "", "type": "address" }, { "name": "", "type": "bytes" }, { "name": "", "type": "uint256" }, { "name": "", "type": "uint256" }, { "name": "", "type": "uint256" } ], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [], "name": "increment", "outputs": [ { "name": "", "type": "int256" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" } ]') # noqa: E501


@pytest.fixture()
def RETURN_ARGS_CODE():
return CONTRACT_RETURN_ARGS_CODE
def CALLER_TESTER_CODE():
return CONTRACT_CALLER_TESTER_CODE


@pytest.fixture()
def RETURN_ARGS_RUNTIME():
return CONTRACT_RETURN_ARGS_RUNTIME
def CALLER_TESTER_RUNTIME():
return CONTRACT_CALLER_TESTER_RUNTIME


@pytest.fixture()
def RETURN_ARGS_ABI():
return CONTRACT_RETURN_ARGS_ABI
def CALLER_TESTER_ABI():
return CONTRACT_CALLER_TESTER_ABI


@pytest.fixture()
def RETURN_ARGS_CONTRACT(RETURN_ARGS_CODE,
RETURN_ARGS_RUNTIME,
RETURN_ARGS_ABI):
def CALLER_TESTER_CONTRACT(CALLER_TESTER_CODE,
CALLER_TESTER_RUNTIME,
CALLER_TESTER_ABI):
return {
'bytecode': RETURN_ARGS_CODE,
'bytecode_runtime': RETURN_ARGS_RUNTIME,
'abi': RETURN_ARGS_ABI,
'bytecode': CALLER_TESTER_CODE,
'bytecode_runtime': CALLER_TESTER_RUNTIME,
'abi': CALLER_TESTER_ABI,
}


@pytest.fixture()
def ReturnArgsContract(web3, RETURN_ARGS_CONTRACT):
return web3.eth.contract(**RETURN_ARGS_CONTRACT)
def CallerTesterContract(web3, CALLER_TESTER_CONTRACT):
return web3.eth.contract(**CALLER_TESTER_CONTRACT)


class LogFunctions:
Expand Down
89 changes: 58 additions & 31 deletions tests/core/contracts/test_contract_caller_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,29 @@ def deploy(web3, Contract, apply_func=identity, args=None):
return contract


@pytest.fixture()
def address(web3):
return web3.eth.accounts[1]


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


@pytest.fixture()
def return_args_contract(web3, ReturnArgsContract, address_conversion_func):
return deploy(web3, ReturnArgsContract, address_conversion_func)
def caller_tester_contract(web3, CallerTesterContract, address_conversion_func):
return deploy(web3, CallerTesterContract, address_conversion_func)


@pytest.fixture()
def transaction_dict(web3, address):
return {
'from': address,
'gas': 210000,
'gasPrice': web3.toWei(.001, 'ether'),
'value': 12345,
}


def test_caller_default(math_contract):
Expand Down Expand Up @@ -87,15 +102,37 @@ def test_caller_with_block_identifier(web3, math_contract):
assert output2 == 2


def test_caller_with_transaction_keyword(web3, return_args_contract):
address = web3.eth.accounts[1]
transaction_dict = {
'from': address,
'gas': 210000,
'gasPrice': web3.toWei(.001, 'ether'),
'value': 12345,
}
contract = return_args_contract.caller(transaction_dict=transaction_dict)
def test_caller_with_block_identifier_and_transaction_dict(web3,
caller_tester_contract,
transaction_dict,
address):
start_num = web3.eth.getBlock('latest').number
assert caller_tester_contract.caller.counter() == 0

web3.provider.make_request(method='evm_mine', params=[5])
caller_tester_contract.functions.increment().transact()

block_id = start_num + 6
contract = caller_tester_contract.caller(
transaction=transaction_dict,
block_identifier=block_id
)

sender, _, gasLeft, value, block_num = contract.returnMeta()
counter = contract.counter()

assert sender == address
assert gasLeft <= transaction_dict['gas']
assert value == transaction_dict['value']
assert block_num == block_id
assert counter == 1


def test_caller_with_transaction_keyword(web3,
caller_tester_contract,
transaction_dict,
address):
contract = caller_tester_contract.caller(transaction=transaction_dict)

sender, _, gasLeft, value, _ = contract.returnMeta()

Expand All @@ -104,16 +141,11 @@ def test_caller_with_transaction_keyword(web3, return_args_contract):
assert value == transaction_dict['value']


def test_caller_with_dict_but_no_transaction_keyword(web3, return_args_contract):
address = web3.eth.accounts[1]
transaction_dict = {
'from': address,
'gas': 210000,
'gasPrice': web3.toWei(.001, 'ether'),
'value': 12345,
}

contract = return_args_contract.caller(transaction_dict)
def test_caller_with_dict_but_no_transaction_keyword(web3,
caller_tester_contract,
transaction_dict,
address):
contract = caller_tester_contract.caller(transaction_dict)

sender, _, gasLeft, value, _ = contract.returnMeta()

Expand All @@ -122,16 +154,11 @@ def test_caller_with_dict_but_no_transaction_keyword(web3, return_args_contract)
assert value == transaction_dict['value']


def test_caller_with_args_and_no_transaction_keyword(web3, return_args_contract):
address = web3.eth.accounts[1]
transaction_dict = {
'from': address,
'gas': 210000,
'gasPrice': web3.toWei(.001, 'ether'),
'value': 12345,
}

contract = return_args_contract.caller(transaction_dict)
def test_caller_with_args_and_no_transaction_keyword(web3,
caller_tester_contract,
transaction_dict,
address):
contract = caller_tester_contract.caller(transaction_dict)

sender, _, gasLeft, value, _ = contract.returnMeta()

Expand Down
26 changes: 13 additions & 13 deletions web3/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,22 +1470,22 @@ class ContractCaller:
or
> contract.caller(transaction_dict={'from': eth.accounts[1], 'gas': 100000, ...}).add(2, 3)
> contract.caller(transaction={'from': eth.accounts[1], 'gas': 100000, ...}).add(2, 3)
"""
def __init__(self,
abi,
web3,
address,
transaction_dict=None,
transaction=None,
block_identifier='latest'):
self.web3 = web3
self.address = address
self.abi = abi
self._functions = None

if abi:
if transaction_dict is None:
transaction_dict = {}
if transaction is None:
transaction = {}

self._functions = filter_by_type('function', self.abi)
for func in self._functions:
Expand All @@ -1499,7 +1499,7 @@ def __init__(self,
block_id = parse_block_identifier(self.web3, block_identifier)
caller_method = partial(self.call_function,
fn,
transaction_dict=transaction_dict,
transaction=transaction,
block_identifier=block_id)

setattr(self, func['name'], caller_method)
Expand All @@ -1525,20 +1525,20 @@ def __getattr__(self, function_name):
else:
return super().__getattribute__(function_name)

def __call__(self, transaction_dict=None, block_identifier='latest'):
if transaction_dict is None:
transaction_dict = {}
def __call__(self, transaction=None, block_identifier='latest'):
if transaction is None:
transaction = {}
return type(self)(self.abi,
self.web3,
self.address,
transaction_dict=transaction_dict,
transaction=transaction,
block_identifier=block_identifier)

@staticmethod
def call_function(fn, *args, transaction_dict=None, block_identifier='latest', **kwargs):
if transaction_dict is None:
transaction_dict = {}
return fn(*args, **kwargs).call(transaction_dict, block_identifier)
def call_function(fn, *args, transaction=None, block_identifier='latest', **kwargs):
if transaction is None:
transaction = {}
return fn(*args, **kwargs).call(transaction, block_identifier)


def check_for_forbidden_api_filter_arguments(event_abi, _filters):
Expand Down

0 comments on commit 6844214

Please sign in to comment.