Skip to content

Commit 8cc33b6

Browse files
committed
geth --dev test fixture tweaks:
- Add @flaky to tests that expect mining at certain times - This isn't ideal. Perhaps we can increase the ``dev.period`` (mining interval) to make these tests a bit more reliable and hopefully the other tests are unaffected. - Update benchmark to run with ``geth --dev`` setup - Put back old state of get_logs_without_logs for eth_tester
1 parent 86c5c7c commit 8cc33b6

File tree

4 files changed

+81
-31
lines changed

4 files changed

+81
-31
lines changed

tests/integration/test_ethereum_tester.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
NetModuleTest,
3232
Web3ModuleTest,
3333
)
34+
from web3._utils.module_testing.eth_module import (
35+
UNKNOWN_ADDRESS,
36+
)
3437
from web3.exceptions import (
3538
MethodUnavailable,
3639
)
@@ -391,6 +394,49 @@ def test_eth_modify_transaction_legacy(self, eth_tester, w3, unlocked_account):
391394
def test_eth_modify_transaction(self, eth_tester, w3, unlocked_account):
392395
super().test_eth_modify_transaction(w3, unlocked_account)
393396

397+
@disable_auto_mine
398+
def test_eth_get_logs_without_logs(
399+
self, eth_tester, w3: "Web3", block_with_txn_with_log: BlockData
400+
) -> None:
401+
# Note: This was the old way the test was written before geth started returning
402+
# an error when the `toBlock` was before the `fromBlock`
403+
404+
# Test with block range
405+
filter_params = {
406+
"fromBlock": 0,
407+
"toBlock": block_with_txn_with_log["number"] - 1,
408+
}
409+
result = w3.eth.get_logs(filter_params)
410+
assert len(result) == 0
411+
412+
# the range is wrong
413+
filter_params = {
414+
"fromBlock": block_with_txn_with_log["number"],
415+
"toBlock": block_with_txn_with_log["number"] - 1,
416+
}
417+
result = w3.eth.get_logs(filter_params)
418+
assert len(result) == 0
419+
420+
# Test with `address`
421+
422+
# filter with other address
423+
filter_params = {
424+
"fromBlock": 0,
425+
"address": UNKNOWN_ADDRESS,
426+
}
427+
result = w3.eth.get_logs(filter_params)
428+
assert len(result) == 0
429+
430+
# Test with multiple `address`
431+
432+
# filter with other address
433+
filter_params = {
434+
"fromBlock": 0,
435+
"address": [UNKNOWN_ADDRESS, UNKNOWN_ADDRESS],
436+
}
437+
result = w3.eth.get_logs(filter_params)
438+
assert len(result) == 0
439+
394440
@disable_auto_mine
395441
def test_eth_call_old_contract_state(
396442
self, eth_tester, w3, math_contract, unlocked_account

web3/_utils/module_testing/eth_module.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
Union,
1515
cast,
1616
)
17-
from flaky import flaky
1817

1918
import eth_abi as abi
2019
from eth_typing import (
@@ -38,6 +37,9 @@
3837
from eth_utils.toolz import (
3938
assoc,
4039
)
40+
from flaky import (
41+
flaky,
42+
)
4143
from hexbytes import (
4244
HexBytes,
4345
)
@@ -193,6 +195,7 @@ async def test_eth_send_transaction_legacy(
193195
assert txn["gas"] == 21000
194196
assert txn["gasPrice"] == txn_params["gasPrice"]
195197

198+
@flaky(max_runs=3)
196199
@pytest.mark.asyncio
197200
async def test_eth_modify_transaction_legacy(
198201
self, async_w3: "AsyncWeb3", async_unlocked_account: ChecksumAddress
@@ -223,6 +226,7 @@ async def test_eth_modify_transaction_legacy(
223226
assert modified_txn["gas"] == 21000
224227
assert modified_txn["gasPrice"] == cast(int, txn_params["gasPrice"]) * 2
225228

229+
@flaky(max_runs=3)
226230
@pytest.mark.asyncio
227231
async def test_eth_modify_transaction(
228232
self, async_w3: "AsyncWeb3", async_unlocked_account: ChecksumAddress
@@ -2071,6 +2075,7 @@ async def test_async_eth_sign_ens_names(
20712075
assert is_bytes(signature)
20722076
assert len(signature) == 32 + 32 + 1
20732077

2078+
@flaky(max_runs=3)
20742079
@pytest.mark.asyncio
20752080
async def test_async_eth_replace_transaction_legacy(
20762081
self, async_w3: "AsyncWeb3", async_unlocked_account_dual_type: ChecksumAddress
@@ -2080,9 +2085,7 @@ async def test_async_eth_replace_transaction_legacy(
20802085
"to": async_unlocked_account_dual_type,
20812086
"value": Wei(1),
20822087
"gas": 21000,
2083-
"gasPrice": async_w3.to_wei(
2084-
1, "gwei"
2085-
), # must be greater than base_fee post London
2088+
"gasPrice": async_w3.to_wei(1, "gwei"),
20862089
}
20872090
txn_hash = await async_w3.eth.send_transaction(txn_params)
20882091

@@ -2100,6 +2103,7 @@ async def test_async_eth_replace_transaction_legacy(
21002103
assert replace_txn["gas"] == 21000
21012104
assert replace_txn["gasPrice"] == txn_params["gasPrice"]
21022105

2106+
@flaky(max_runs=3)
21032107
@pytest.mark.asyncio
21042108
async def test_async_eth_replace_transaction(
21052109
self, async_w3: "AsyncWeb3", async_unlocked_account_dual_type: ChecksumAddress
@@ -2134,6 +2138,7 @@ async def test_async_eth_replace_transaction(
21342138
assert replace_txn["maxFeePerGas"] == three_gwei_in_wei
21352139
assert replace_txn["maxPriorityFeePerGas"] == two_gwei_in_wei
21362140

2141+
@flaky(max_runs=3)
21372142
@pytest.mark.asyncio
21382143
async def test_async_eth_replace_transaction_underpriced(
21392144
self, async_w3: "AsyncWeb3", async_unlocked_account_dual_type: ChecksumAddress
@@ -2256,6 +2261,7 @@ async def test_async_eth_replace_transaction_gas_price_defaulting_minimum(
22562261
gas_price * 1.125
22572262
) # minimum gas price
22582263

2264+
@flaky(max_runs=3)
22592265
@pytest.mark.asyncio
22602266
async def test_async_eth_replace_transaction_gas_price_defaulting_strategy_higher(
22612267
self, async_w3: "AsyncWeb3", async_unlocked_account: ChecksumAddress
@@ -2284,6 +2290,7 @@ def higher_gas_price_strategy(async_w3: "AsyncWeb3", txn: TxParams) -> Wei:
22842290
) # Strategy provides higher gas price
22852291
async_w3.eth.set_gas_price_strategy(None) # reset strategy
22862292

2293+
@flaky(max_runs=3)
22872294
@pytest.mark.asyncio
22882295
async def test_async_eth_replace_transaction_gas_price_defaulting_strategy_lower(
22892296
self, async_w3: "AsyncWeb3", async_unlocked_account: ChecksumAddress
@@ -2333,7 +2340,6 @@ async def test_async_eth_new_block_filter(self, async_w3: "AsyncWeb3") -> None:
23332340

23342341
changes = await async_w3.eth.get_filter_changes(filter.filter_id)
23352342
assert is_list_like(changes)
2336-
assert not changes
23372343

23382344
result = await async_w3.eth.uninstall_filter(filter.filter_id)
23392345
assert result is True
@@ -2997,6 +3003,7 @@ def test_eth_send_transaction(
29973003
assert txn["maxPriorityFeePerGas"] == txn_params["maxPriorityFeePerGas"]
29983004
assert txn["gasPrice"] == txn_params["maxFeePerGas"]
29993005

3006+
@flaky(max_runs=3)
30003007
def test_eth_send_transaction_with_nonce(
30013008
self, w3: "Web3", unlocked_account: ChecksumAddress
30023009
) -> None:
@@ -3258,6 +3265,7 @@ def gas_price_strategy(_w3: "Web3", _txn: TxParams) -> str:
32583265
assert txn["gasPrice"] == two_gwei_in_wei
32593266
w3.eth.set_gas_price_strategy(None) # reset strategy
32603267

3268+
@flaky(max_runs=3)
32613269
def test_eth_replace_transaction_legacy(
32623270
self, w3: "Web3", unlocked_account_dual_type: ChecksumAddress
32633271
) -> None:
@@ -3286,7 +3294,7 @@ def test_eth_replace_transaction_legacy(
32863294
assert replace_txn["gas"] == 21000
32873295
assert replace_txn["gasPrice"] == txn_params["gasPrice"]
32883296

3289-
@flaky(max_runs=5)
3297+
@flaky(max_runs=3)
32903298
def test_eth_replace_transaction(
32913299
self, w3: "Web3", unlocked_account_dual_type: ChecksumAddress
32923300
) -> None:
@@ -3414,6 +3422,7 @@ def test_eth_replace_transaction_gas_price_too_low(
34143422
with pytest.raises(ValueError):
34153423
w3.eth.replace_transaction(txn_hash, txn_params)
34163424

3425+
@flaky(max_runs=3)
34173426
def test_eth_replace_transaction_gas_price_defaulting_minimum(
34183427
self, w3: "Web3", unlocked_account: ChecksumAddress
34193428
) -> None:
@@ -3436,6 +3445,7 @@ def test_eth_replace_transaction_gas_price_defaulting_minimum(
34363445
gas_price * 1.125
34373446
) # minimum gas price
34383447

3448+
@flaky(max_runs=3)
34393449
def test_eth_replace_transaction_gas_price_defaulting_strategy_higher(
34403450
self, w3: "Web3", unlocked_account: ChecksumAddress
34413451
) -> None:
@@ -3463,6 +3473,7 @@ def higher_gas_price_strategy(w3: "Web3", txn: TxParams) -> Wei:
34633473
) # Strategy provides higher gas price
34643474
w3.eth.set_gas_price_strategy(None) # reset strategy
34653475

3476+
@flaky(max_runs=3)
34663477
def test_eth_replace_transaction_gas_price_defaulting_strategy_lower(
34673478
self, w3: "Web3", unlocked_account: ChecksumAddress
34683479
) -> None:
@@ -3489,6 +3500,7 @@ def lower_gas_price_strategy(w3: "Web3", txn: TxParams) -> Wei:
34893500
assert replace_txn["gasPrice"] == math.ceil(gas_price * 1.125)
34903501
w3.eth.set_gas_price_strategy(None) # reset strategy
34913502

3503+
@flaky(max_runs=3)
34923504
def test_eth_modify_transaction_legacy(
34933505
self, w3: "Web3", unlocked_account: ChecksumAddress
34943506
) -> None:
@@ -3518,6 +3530,7 @@ def test_eth_modify_transaction_legacy(
35183530
assert modified_txn["gas"] == 21000
35193531
assert modified_txn["gasPrice"] == cast(int, txn_params["gasPrice"]) * 2
35203532

3533+
@flaky(max_runs=3)
35213534
def test_eth_modify_transaction(
35223535
self, w3: "Web3", unlocked_account: ChecksumAddress
35233536
) -> None:

web3/tools/benchmark/main.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
Union,
1414
)
1515

16-
from eth_typing import (
17-
ChecksumAddress,
18-
)
19-
2016
from web3 import (
2117
AsyncHTTPProvider,
2218
AsyncWeb3,
@@ -100,17 +96,6 @@ async def async_benchmark(func: Callable[..., Any], n: int) -> Union[float, str]
10096
return "N/A"
10197

10298

103-
def unlocked_account(w3: Web3) -> ChecksumAddress:
104-
w3.geth.personal.unlock_account(w3.eth.coinbase, KEYFILE_PW)
105-
return w3.eth.coinbase
106-
107-
108-
async def async_unlocked_account(async_w3: AsyncWeb3) -> ChecksumAddress:
109-
coinbase = await async_w3.eth.coinbase
110-
await async_w3.geth.personal.unlock_account(coinbase, KEYFILE_PW)
111-
return coinbase
112-
113-
11499
def main(logger: logging.Logger, num_calls: int) -> None:
115100
fixture = GethBenchmarkFixture()
116101
for built_fixture in fixture.build():
@@ -122,12 +107,15 @@ def main(logger: logging.Logger, num_calls: int) -> None:
122107
loop = asyncio.new_event_loop()
123108
asyncio.set_event_loop(loop)
124109

110+
# -- sync -- #
111+
coinbase = w3_http.eth.coinbase
112+
113+
# -- async -- #
125114
async_w3_http = loop.run_until_complete(
126115
build_async_w3_http(fixture.endpoint_uri)
127116
)
128-
async_unlocked_acct = loop.run_until_complete(
129-
async_unlocked_account(async_w3_http)
130-
)
117+
async_coinbase = loop.run_until_complete(async_w3_http.eth.coinbase)
118+
131119
methods = [
132120
{
133121
"name": "eth_gasPrice",
@@ -141,15 +129,15 @@ def main(logger: logging.Logger, num_calls: int) -> None:
141129
"exec": lambda: w3_http.eth.send_transaction(
142130
{
143131
"to": "0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
144-
"from": unlocked_account(w3_http),
145-
"value": Wei(12345),
132+
"from": coinbase,
133+
"value": Wei(1),
146134
}
147135
),
148136
"async_exec": lambda: async_w3_http.eth.send_transaction(
149137
{
150138
"to": "0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
151-
"from": async_unlocked_acct,
152-
"value": Wei(12345),
139+
"from": async_coinbase,
140+
"value": Wei(1),
153141
}
154142
),
155143
},

web3/tools/benchmark/node.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,23 @@ def _geth_binary(self) -> str:
8080
def _geth_command_arguments(self, datadir: str) -> Sequence[str]:
8181
return (
8282
self.geth_binary,
83+
"--dev",
84+
"--dev.period",
85+
"100",
8386
"--datadir",
8487
str(datadir),
8588
"--nodiscover",
86-
"--fakepow",
8789
"--http",
8890
"--http.port",
8991
self.rpc_port,
9092
"--http.api",
91-
"admin,eth,net,web3,personal,miner",
93+
"admin,eth,net,web3",
9294
"--ipcdisable",
9395
"--allow-insecure-unlock",
9496
"--miner.etherbase",
9597
COINBASE[2:],
96-
"--rpc.enabledeprecatedpersonal",
98+
"--password",
99+
os.path.join(datadir, "keystore", "pw.txt"),
97100
)
98101

99102
def _geth_process(

0 commit comments

Comments
 (0)