Skip to content

Commit 5f5e056

Browse files
committed
Use eth.default_account for tests, in the age of no eth_coinbase
This commit updates the tests to set ``w3.eth.default_account`` to the first account and use it in place of ``w3.eth.coinbase``, since ``eth_coinbase`` was removed in clients like geth. Using ``w3.eth.accounts[0]`` is not a very user-friendly API to keep calling across all tests.
1 parent f56b684 commit 5f5e056

17 files changed

+108
-91
lines changed

conftest.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,17 @@ def _wait_for_transaction(w3, txn_hash, timeout=120):
7070
return _wait_for_transaction
7171

7272

73-
@pytest.fixture()
73+
@pytest.fixture
7474
def w3():
75-
return Web3(EthereumTesterProvider())
75+
w3 = Web3(EthereumTesterProvider())
76+
w3.eth.default_account = w3.eth.accounts[0]
77+
return w3
7678

7779

7880
@pytest.fixture(scope="module")
7981
def w3_non_strict_abi():
8082
w3 = Web3(EthereumTesterProvider())
83+
w3.eth.default_account = w3.eth.accounts[0]
8184
w3.strict_bytes_type_checking = False
8285
return w3
8386

tests/core/contracts/conftest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,7 @@ def event_contract(
230230
wait_for_block(w3)
231231

232232
event_contract_factory = w3.eth.contract(**EVENT_CONTRACT_DATA)
233-
deploy_txn_hash = event_contract_factory.constructor().transact(
234-
{"from": w3.eth.accounts[0], "gas": 1000000}
235-
)
233+
deploy_txn_hash = event_contract_factory.constructor().transact({"gas": 1000000})
236234
deploy_receipt = wait_for_transaction(w3, deploy_txn_hash)
237235
contract_address = address_conversion_func(deploy_receipt["contractAddress"])
238236

@@ -251,7 +249,7 @@ def indexed_event_contract(
251249

252250
indexed_event_contract_factory = w3.eth.contract(**INDEXED_EVENT_CONTRACT_DATA)
253251
deploy_txn_hash = indexed_event_contract_factory.constructor().transact(
254-
{"from": w3.eth.accounts[0], "gas": 1000000}
252+
{"gas": 1000000}
255253
)
256254
deploy_receipt = wait_for_transaction(w3, deploy_txn_hash)
257255
contract_address = address_conversion_func(deploy_receipt["contractAddress"])

tests/core/contracts/test_contract_constructor.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ def test_contract_constructor_build_transaction_no_constructor(
218218
w3, math_contract_factory, address_conversion_func
219219
):
220220
txn_hash = math_contract_factory.constructor().transact(
221-
{"from": address_conversion_func(w3.eth.accounts[0])}
221+
{"from": address_conversion_func(w3.eth.default_account)}
222222
)
223223
txn = w3.eth.get_transaction(txn_hash)
224-
nonce = w3.eth.get_transaction_count(w3.eth.accounts[0])
224+
nonce = w3.eth.get_transaction_count(w3.eth.default_account)
225225
unsent_txn = math_contract_factory.constructor().build_transaction({"nonce": nonce})
226226
assert txn["input"] == HexBytes(unsent_txn["data"])
227227

@@ -235,10 +235,10 @@ def test_contract_constructor_build_transaction_without_arguments(
235235
w3, math_contract_factory, address_conversion_func
236236
):
237237
txn_hash = math_contract_factory.constructor().transact(
238-
{"from": address_conversion_func(w3.eth.accounts[0])}
238+
{"from": address_conversion_func(w3.eth.default_account)}
239239
)
240240
txn = w3.eth.get_transaction(txn_hash)
241-
nonce = w3.eth.get_transaction_count(w3.eth.accounts[0])
241+
nonce = w3.eth.get_transaction_count(w3.eth.default_account)
242242
unsent_txn = math_contract_factory.constructor().build_transaction({"nonce": nonce})
243243
assert txn["input"] == HexBytes(unsent_txn["data"])
244244

@@ -266,10 +266,10 @@ def test_contract_constructor_build_transaction_with_arguments(
266266
):
267267
txn_hash = non_strict_contract_with_constructor_args_factory.constructor(
268268
*constructor_args, **constructor_kwargs
269-
).transact({"from": address_conversion_func(w3_non_strict_abi.eth.accounts[0])})
269+
).transact({"from": address_conversion_func(w3_non_strict_abi.eth.default_account)})
270270
txn = w3_non_strict_abi.eth.get_transaction(txn_hash)
271271
nonce = w3_non_strict_abi.eth.get_transaction_count(
272-
w3_non_strict_abi.eth.accounts[0]
272+
w3_non_strict_abi.eth.default_account
273273
)
274274
unsent_txn = non_strict_contract_with_constructor_args_factory.constructor(
275275
*constructor_args, **constructor_kwargs
@@ -523,12 +523,11 @@ async def test_async_contract_constructor_build_transaction_to_field_error(
523523
async def test_async_contract_constructor_build_transaction_no_constructor(
524524
async_w3, async_math_contract_factory, address_conversion_func
525525
):
526-
async_w3_accounts = await async_w3.eth.accounts
527526
txn_hash = await async_math_contract_factory.constructor().transact(
528-
{"from": address_conversion_func(async_w3_accounts[0])}
527+
{"from": address_conversion_func(async_w3.eth.default_account)}
529528
)
530529
txn = await async_w3.eth.get_transaction(txn_hash)
531-
nonce = await async_w3.eth.get_transaction_count(async_w3_accounts[0])
530+
nonce = await async_w3.eth.get_transaction_count(async_w3.eth.default_account)
532531
unsent_txn = await async_math_contract_factory.constructor().build_transaction(
533532
{"nonce": nonce}
534533
)
@@ -544,12 +543,11 @@ async def test_async_contract_constructor_build_transaction_no_constructor(
544543
async def test_async_contract_constructor_build_transaction_without_arguments(
545544
async_w3, async_math_contract_factory, address_conversion_func
546545
):
547-
async_w3_accounts = await async_w3.eth.accounts
548546
txn_hash = await async_math_contract_factory.constructor().transact(
549-
{"from": address_conversion_func(async_w3_accounts[0])}
547+
{"from": address_conversion_func(async_w3.eth.default_account)}
550548
)
551549
txn = await async_w3.eth.get_transaction(txn_hash)
552-
nonce = await async_w3.eth.get_transaction_count(async_w3_accounts[0])
550+
nonce = await async_w3.eth.get_transaction_count(async_w3.eth.default_account)
553551
unsent_txn = await async_math_contract_factory.constructor().build_transaction(
554552
{"nonce": nonce}
555553
)
@@ -578,15 +576,20 @@ async def test_async_contract_constructor_build_transaction_with_arguments(
578576
constructor_kwargs,
579577
address_conversion_func,
580578
):
581-
async_w3_accounts = await async_w3_non_strict_abi.eth.accounts
582579
txn_hash = (
583580
await async_non_strict_constructor_with_args_contract_factory.constructor(
584581
*constructor_args, **constructor_kwargs
585-
).transact({"from": address_conversion_func(async_w3_accounts[0])})
582+
).transact(
583+
{
584+
"from": address_conversion_func(
585+
async_w3_non_strict_abi.eth.default_account
586+
)
587+
}
588+
)
586589
)
587590
txn = await async_w3_non_strict_abi.eth.get_transaction(txn_hash)
588591
nonce = await async_w3_non_strict_abi.eth.get_transaction_count(
589-
async_w3_accounts[0]
592+
async_w3_non_strict_abi.eth.default_account
590593
)
591594
unsent_txn = (
592595
await async_non_strict_constructor_with_args_contract_factory.constructor(

tests/core/contracts/test_contract_example.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,6 @@ async def async_foo_contract(async_w3):
149149
#
150150
# }
151151

152-
async_eth_tester_accounts = await async_w3.eth.accounts
153-
deploy_address = async_eth_tester_accounts[0]
154-
155152
abi = """[{"anonymous":false,"inputs":[{"indexed":false,"name":"_bar","type":"string"}],"name":"barred","type":"event"},{"constant":false,"inputs":[{"name":"_bar","type":"string"}],"name":"setBar","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"constant":true,"inputs":[],"name":"bar","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]""" # noqa: E501
156153
# This bytecode is the output of compiling with
157154
# solc version:0.5.3+commit.10d17f24.Emscripten.clang
@@ -162,13 +159,13 @@ async def async_foo_contract(async_w3):
162159
# issue a transaction to deploy the contract.
163160
tx_hash = await FooContract.constructor().transact(
164161
{
165-
"from": deploy_address,
162+
"from": async_w3.eth.default_account,
166163
}
167164
)
168165
# wait for the transaction to be mined
169166
tx_receipt = await async_w3.eth.wait_for_transaction_receipt(tx_hash, 180)
170167
# instantiate and return an instance of our contract.
171-
return FooContract(tx_receipt.contractAddress)
168+
return FooContract(tx_receipt["contractAddress"])
172169

173170

174171
@pytest.mark.asyncio
@@ -179,13 +176,11 @@ async def test_async_initial_greeting(async_foo_contract):
179176

180177
@pytest.mark.asyncio
181178
async def test_async_can_update_greeting(async_w3, async_foo_contract):
182-
async_eth_tester_accounts = await async_w3.eth.accounts
183-
# send transaction that updates the greeting
184179
tx_hash = await async_foo_contract.functions.setBar(
185180
"testing contracts is easy",
186181
).transact(
187182
{
188-
"from": async_eth_tester_accounts[1],
183+
"from": async_w3.eth.default_account,
189184
}
190185
)
191186
await async_w3.eth.wait_for_transaction_receipt(tx_hash, 180)
@@ -197,13 +192,12 @@ async def test_async_can_update_greeting(async_w3, async_foo_contract):
197192

198193
@pytest.mark.asyncio
199194
async def test_async_updating_greeting_emits_event(async_w3, async_foo_contract):
200-
async_eth_tester_accounts = await async_w3.eth.accounts
201195
# send transaction that updates the greeting
202196
tx_hash = await async_foo_contract.functions.setBar(
203197
"testing contracts is easy",
204198
).transact(
205199
{
206-
"from": async_eth_tester_accounts[1],
200+
"from": async_w3.eth.default_account,
207201
}
208202
)
209203
receipt = await async_w3.eth.wait_for_transaction_receipt(tx_hash, 180)

tests/core/contracts/test_contract_init.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
def math_addr(math_contract_factory, address_conversion_func):
1515
w3 = math_contract_factory.w3
1616
deploy_txn = math_contract_factory.constructor().transact(
17-
{"from": w3.eth.accounts[0]}
17+
{"from": w3.eth.default_account}
1818
)
1919
deploy_receipt = w3.eth.wait_for_transaction_receipt(deploy_txn)
2020
assert deploy_receipt is not None
@@ -30,7 +30,7 @@ def test_contract_with_unset_address(math_contract_factory):
3030
def test_contract_with_name_address(math_contract_factory, math_addr):
3131
with contract_ens_addresses(math_contract_factory, [("thedao.eth", math_addr)]):
3232
mc = math_contract_factory(address="thedao.eth")
33-
caller = mc.w3.eth.accounts[0]
33+
caller = mc.w3.eth.default_account
3434
assert mc.address == "thedao.eth"
3535
assert mc.functions.return13().call({"from": caller}) == 13
3636

@@ -50,17 +50,16 @@ def test_contract_with_name_address_from_eth_contract(
5050
bytecode_runtime=math_contract_runtime,
5151
)
5252

53-
caller = mc.w3.eth.accounts[0]
5453
assert mc.address == "thedao.eth"
55-
assert mc.functions.return13().call({"from": caller}) == 13
54+
assert mc.functions.return13().call({"from": mc.w3.eth.default_account}) == 13
5655

5756

5857
def test_contract_with_name_address_changing(math_contract_factory, math_addr):
5958
# Contract address is validated once on creation
6059
with contract_ens_addresses(math_contract_factory, [("thedao.eth", math_addr)]):
6160
mc = math_contract_factory(address="thedao.eth")
6261

63-
caller = mc.w3.eth.accounts[0]
62+
caller = mc.w3.eth.default_account
6463
assert mc.address == "thedao.eth"
6564

6665
# what happens when name returns no address at all

tests/core/contracts/test_contract_transact_interface.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_transacting_with_contract_with_arguments(
8989

9090

9191
def test_deploy_when_default_account_is_set(w3, string_contract_data):
92-
w3.eth.default_account = w3.eth.accounts[1]
92+
# default account for tests is set as the first account
9393
assert w3.eth.default_account is not empty
9494

9595
StringContract = w3.eth.contract(**string_contract_data)
@@ -101,7 +101,7 @@ def test_deploy_when_default_account_is_set(w3, string_contract_data):
101101

102102

103103
def test_transact_when_default_account_is_set(w3, math_contract, transact):
104-
w3.eth.default_account = w3.eth.accounts[1]
104+
# default account for tests is set as the first account
105105
assert w3.eth.default_account is not empty
106106

107107
txn_hash = transact(contract=math_contract, contract_function="incrementCounter")
@@ -392,8 +392,7 @@ async def test_async_transacting_with_contract_with_arguments(
392392

393393
@pytest.mark.asyncio
394394
async def test_async_deploy_when_default_account_is_set(async_w3, string_contract_data):
395-
async_w3_accounts = await async_w3.eth.accounts
396-
async_w3.eth.default_account = async_w3_accounts[1]
395+
# default account for tests is set as the first account
397396
assert async_w3.eth.default_account is not empty
398397

399398
StringContract = async_w3.eth.contract(**string_contract_data)
@@ -408,8 +407,7 @@ async def test_async_deploy_when_default_account_is_set(async_w3, string_contrac
408407
async def test_async_transact_when_default_account_is_set(
409408
async_w3, async_math_contract, async_transact
410409
):
411-
async_w3_accounts = await async_w3.eth.accounts
412-
async_w3.eth.default_account = async_w3_accounts[1]
410+
# default account for tests is set as the first account
413411
assert async_w3.eth.default_account is not empty
414412

415413
txn_hash = await async_transact(

tests/core/eth-module/test_accounts.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
AsyncWeb3,
2828
Web3,
2929
)
30+
from web3._utils.empty import (
31+
Empty,
32+
)
3033
from web3.eth import (
3134
BaseEth,
3235
)
@@ -100,6 +103,10 @@ def w3():
100103
return Web3(EthereumTesterProvider())
101104

102105

106+
def test_eth_default_account_is_empty_by_default(w3):
107+
assert isinstance(w3.eth.default_account, Empty)
108+
109+
103110
def test_eth_account_create_variation(acct):
104111
account1 = acct.create()
105112
account2 = acct.create()

tests/core/eth-module/test_default_account_api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ def wait_for_first_block(w3, wait_for_block):
77

88

99
def test_uses_default_account_when_set(w3, wait_for_transaction):
10+
current_default = w3.eth.default_account
11+
1012
w3.eth.default_account = None
1113
assert w3.eth.default_account is None
1214

@@ -25,6 +27,8 @@ def test_uses_default_account_when_set(w3, wait_for_transaction):
2527
txn = w3.eth.get_transaction(txn_hash)
2628
assert txn["from"] == w3.eth.accounts[3]
2729

30+
w3.eth.default_account = current_default
31+
2832

2933
def test_uses_given_from_address_when_provided(w3, wait_for_transaction):
3034
w3.eth.default_account = w3.eth.accounts[3]

tests/core/eth-module/test_transactions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def test_unmined_transaction_wait_for_receipt(w3, request_mocker):
184184

185185
txn_hash = w3.eth.send_transaction(
186186
{
187-
"from": w3.eth.accounts[0],
187+
"from": w3.eth.default_account,
188188
"to": "0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
189189
"value": 123457,
190190
}

tests/core/filtering/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ def create_filter(request):
6464
# --- async --- #
6565

6666

67-
@pytest.fixture(
67+
@pytest_asyncio.fixture(
6868
scope="function",
6969
params=[True, False],
7070
ids=["LocalFilterMiddleware", "node_based_filter"],
7171
)
72-
def async_w3(request):
73-
return _async_w3_fixture_logic(request)
72+
async def async_w3(request):
73+
return await _async_w3_fixture_logic(request)
7474

7575

7676
@pytest.fixture

tests/core/filtering/test_contract_data_filters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,13 @@ def event_loop():
279279
loop.close()
280280

281281

282-
@pytest.fixture(
282+
@pytest_asyncio.fixture(
283283
scope="module",
284284
params=[True, False],
285285
ids=["LocalFilterMiddleware", "node_based_filter"],
286286
)
287-
def async_w3(request):
288-
return _async_w3_fixture_logic(request)
287+
async def async_w3(request):
288+
return await _async_w3_fixture_logic(request)
289289

290290

291291
@pytest.fixture(scope="module")

tests/core/filtering/test_contract_topic_filters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,13 @@ def event_loop():
260260
loop.close()
261261

262262

263-
@pytest.fixture(
263+
@pytest_asyncio.fixture(
264264
scope="module",
265265
params=[True, False],
266266
ids=["LocalFilterMiddleware", "node_based_filter"],
267267
)
268-
def async_w3(request):
269-
return _async_w3_fixture_logic(request)
268+
async def async_w3(request):
269+
return await _async_w3_fixture_logic(request)
270270

271271

272272
@pytest_asyncio.fixture(scope="module")

0 commit comments

Comments
 (0)