From 06d55fa4e977c35a5c50343dcf8d2e2eecc32a45 Mon Sep 17 00:00:00 2001 From: Piper Merriam Date: Fri, 1 Jul 2016 13:58:12 -0600 Subject: [PATCH] getCode and sendTransacion tests --- conftest.py | 2 +- requirements-dev.txt | 2 +- tests/eth-module/conftest.py | 65 ++++++++++++++++++++ tests/eth-module/test_eth_getCode.py | 22 +++++++ tests/eth-module/test_eth_sendTransaction.py | 17 +++++ 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 tests/eth-module/test_eth_getCode.py diff --git a/conftest.py b/conftest.py index a9f9963..835427d 100644 --- a/conftest.py +++ b/conftest.py @@ -5,7 +5,7 @@ import shutil # This has to go here so that the `gevent.monkey.patch_all()` happens in the # main thread. -from pygeth.geth import ( +from pygeth import ( LoggingMixin, DevGethProcess, ) diff --git a/requirements-dev.txt b/requirements-dev.txt index 1d16eb8..fbac5ec 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,6 +2,6 @@ pytest>=2.8.2 pytest-pythonpath>=0.3 tox>=1.8.0 eth-testrpc==0.3.1 -py-geth>=0.1.0 +py-geth>=0.4.0 # Until pyethereum > 1.3.6 is released. https://github.com/ethereum/pyethereum/tarball/b06829e56e2a3de5276077a611ed8813b6cf5e74 diff --git a/tests/eth-module/conftest.py b/tests/eth-module/conftest.py index 5ef66ca..dc35344 100644 --- a/tests/eth-module/conftest.py +++ b/tests/eth-module/conftest.py @@ -1,4 +1,6 @@ import pytest +import json +import textwrap @pytest.fixture @@ -14,3 +16,66 @@ def extra_accounts(web3, account_password): web3.personal.newAccount(account_password) return web3.eth.accounts + + +CONTRACT_CODE = b"606060405261022e806100126000396000f360606040523615610074576000357c01000000000000000000000000000000000000000000000000000000009004806316216f391461007657806361bc221a146100995780637cf5dab0146100bc578063a5f3c23b146100e8578063d09de08a1461011d578063dcf537b11461014057610074565b005b610083600480505061016c565b6040518082815260200191505060405180910390f35b6100a6600480505061017f565b6040518082815260200191505060405180910390f35b6100d26004808035906020019091905050610188565b6040518082815260200191505060405180910390f35b61010760048080359060200190919080359060200190919050506101ea565b6040518082815260200191505060405180910390f35b61012a6004805050610201565b6040518082815260200191505060405180910390f35b6101566004808035906020019091905050610217565b6040518082815260200191505060405180910390f35b6000600d9050805080905061017c565b90565b60006000505481565b6000816000600082828250540192505081905550600060005054905080507f3496c3ede4ec3ab3686712aa1c238593ea6a42df83f98a5ec7df9834cfa577c5816040518082815260200191505060405180910390a18090506101e5565b919050565b6000818301905080508090506101fb565b92915050565b600061020d6001610188565b9050610214565b90565b60006007820290508050809050610229565b91905056" + + +CONTRACT_RUNTIME = b"0x60606040523615610074576000357c01000000000000000000000000000000000000000000000000000000009004806316216f391461007657806361bc221a146100995780637cf5dab0146100bc578063a5f3c23b146100e8578063d09de08a1461011d578063dcf537b11461014057610074565b005b610083600480505061016c565b6040518082815260200191505060405180910390f35b6100a6600480505061017f565b6040518082815260200191505060405180910390f35b6100d26004808035906020019091905050610188565b6040518082815260200191505060405180910390f35b61010760048080359060200190919080359060200190919050506101ea565b6040518082815260200191505060405180910390f35b61012a6004805050610201565b6040518082815260200191505060405180910390f35b6101566004808035906020019091905050610217565b6040518082815260200191505060405180910390f35b6000600d9050805080905061017c565b90565b60006000505481565b6000816000600082828250540192505081905550600060005054905080507f3496c3ede4ec3ab3686712aa1c238593ea6a42df83f98a5ec7df9834cfa577c5816040518082815260200191505060405180910390a18090506101e5565b919050565b6000818301905080508090506101fb565b92915050565b600061020d6001610188565b9050610214565b90565b60006007820290508050809050610229565b91905056" + + +CONTRACT_SOURCE = textwrap.dedent((""" + contract Math { + uint public counter; + + event Increased(uint value); + + function increment() public returns (uint) { + return increment(1); + } + + function increment(uint amt) public returns (uint result) { + counter += amt; + result = counter; + Increased(result); + return result; + } + + function add(int a, int b) public returns (int result) { + result = a + b; + return result; + } + + function multiply7(int a) public returns (int result) { + result = a * 7; + return result; + } + + function return13() public returns (int result) { + result = 13; + return result; + } + } +""")).strip() + +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 + + +@pytest.fixture(scope="session") +def MATH_CODE(): + return CONTRACT_CODE + + +@pytest.fixture(scope="session") +def MATH_RUNTIME(): + return CONTRACT_RUNTIME + + +@pytest.fixture(scope="session") +def MATH_SOURCE(): + return CONTRACT_SOURCE + + +@pytest.fixture(scope="session") +def MATH_ABI(): + return CONTRACT_ABI diff --git a/tests/eth-module/test_eth_getCode.py b/tests/eth-module/test_eth_getCode.py new file mode 100644 index 0000000..338d73a --- /dev/null +++ b/tests/eth-module/test_eth_getCode.py @@ -0,0 +1,22 @@ +import pytest + +from web3.utils.encoding import force_bytes + + +@pytest.fixture(autouse=True) +def wait_for_first_block(web3, wait_for_block): + wait_for_block(web3) + + +def test_eth_getCode(web3, wait_for_transaction, MATH_CODE, MATH_RUNTIME): + txn_hash = web3.eth.sendTransaction({ + "from": web3.eth.coinbase, + "data": MATH_CODE, + }) + + wait_for_transaction(txn_hash) + + txn_receipt = web3.eth.getTransactionReciept(txn_hash) + contract_address = txn_receipt['contractAddress'] + + assert force_bytes(web3.eth.getCode(contract_address)) == MATH_RUNTIME diff --git a/tests/eth-module/test_eth_sendTransaction.py b/tests/eth-module/test_eth_sendTransaction.py index 0ec222e..6355d31 100644 --- a/tests/eth-module/test_eth_sendTransaction.py +++ b/tests/eth-module/test_eth_sendTransaction.py @@ -1,5 +1,7 @@ import pytest +from web3.utils.encoding import force_bytes + @pytest.fixture(autouse=True) def wait_for_first_block(web3, wait_for_block): @@ -21,3 +23,18 @@ def test_eth_sendTransaction_with_value_only_transaction(web3, extra_accounts, after_balance = int(web3.eth.getBalance(extra_accounts[1]), 16) assert after_balance - initial_balance == 1234 + + +def test_eth_sendTransaction_with_data(web3, wait_for_transaction, MATH_CODE, MATH_RUNTIME): + txn_hash = web3.eth.sendTransaction({ + "from": web3.eth.coinbase, + "data": MATH_CODE, + "gas": 3000000, + }) + + wait_for_transaction(txn_hash) + + txn_receipt = web3.eth.getTransactionReciept(txn_hash) + contract_address = txn_receipt['contractAddress'] + + assert force_bytes(web3.eth.getCode(contract_address)) == MATH_RUNTIME