Skip to content

Commit

Permalink
geth --dev test fixture tweaks:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
fselmo committed Jan 25, 2024
1 parent 86c5c7c commit 8cc33b6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 31 deletions.
46 changes: 46 additions & 0 deletions tests/integration/test_ethereum_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
NetModuleTest,
Web3ModuleTest,
)
from web3._utils.module_testing.eth_module import (
UNKNOWN_ADDRESS,
)
from web3.exceptions import (
MethodUnavailable,
)
Expand Down Expand Up @@ -391,6 +394,49 @@ def test_eth_modify_transaction_legacy(self, eth_tester, w3, unlocked_account):
def test_eth_modify_transaction(self, eth_tester, w3, unlocked_account):
super().test_eth_modify_transaction(w3, unlocked_account)

@disable_auto_mine
def test_eth_get_logs_without_logs(
self, eth_tester, w3: "Web3", block_with_txn_with_log: BlockData
) -> None:
# Note: This was the old way the test was written before geth started returning
# an error when the `toBlock` was before the `fromBlock`

# Test with block range
filter_params = {
"fromBlock": 0,
"toBlock": block_with_txn_with_log["number"] - 1,
}
result = w3.eth.get_logs(filter_params)
assert len(result) == 0

# the range is wrong
filter_params = {
"fromBlock": block_with_txn_with_log["number"],
"toBlock": block_with_txn_with_log["number"] - 1,
}
result = w3.eth.get_logs(filter_params)
assert len(result) == 0

# Test with `address`

# filter with other address
filter_params = {
"fromBlock": 0,
"address": UNKNOWN_ADDRESS,
}
result = w3.eth.get_logs(filter_params)
assert len(result) == 0

# Test with multiple `address`

# filter with other address
filter_params = {
"fromBlock": 0,
"address": [UNKNOWN_ADDRESS, UNKNOWN_ADDRESS],
}
result = w3.eth.get_logs(filter_params)
assert len(result) == 0

@disable_auto_mine
def test_eth_call_old_contract_state(
self, eth_tester, w3, math_contract, unlocked_account
Expand Down
25 changes: 19 additions & 6 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
Union,
cast,
)
from flaky import flaky

import eth_abi as abi
from eth_typing import (
Expand All @@ -38,6 +37,9 @@
from eth_utils.toolz import (
assoc,
)
from flaky import (
flaky,
)
from hexbytes import (
HexBytes,
)
Expand Down Expand Up @@ -193,6 +195,7 @@ async def test_eth_send_transaction_legacy(
assert txn["gas"] == 21000
assert txn["gasPrice"] == txn_params["gasPrice"]

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

@flaky(max_runs=3)
@pytest.mark.asyncio
async def test_eth_modify_transaction(
self, async_w3: "AsyncWeb3", async_unlocked_account: ChecksumAddress
Expand Down Expand Up @@ -2071,6 +2075,7 @@ async def test_async_eth_sign_ens_names(
assert is_bytes(signature)
assert len(signature) == 32 + 32 + 1

@flaky(max_runs=3)
@pytest.mark.asyncio
async def test_async_eth_replace_transaction_legacy(
self, async_w3: "AsyncWeb3", async_unlocked_account_dual_type: ChecksumAddress
Expand All @@ -2080,9 +2085,7 @@ async def test_async_eth_replace_transaction_legacy(
"to": async_unlocked_account_dual_type,
"value": Wei(1),
"gas": 21000,
"gasPrice": async_w3.to_wei(
1, "gwei"
), # must be greater than base_fee post London
"gasPrice": async_w3.to_wei(1, "gwei"),
}
txn_hash = await async_w3.eth.send_transaction(txn_params)

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

@flaky(max_runs=3)
@pytest.mark.asyncio
async def test_async_eth_replace_transaction(
self, async_w3: "AsyncWeb3", async_unlocked_account_dual_type: ChecksumAddress
Expand Down Expand Up @@ -2134,6 +2138,7 @@ async def test_async_eth_replace_transaction(
assert replace_txn["maxFeePerGas"] == three_gwei_in_wei
assert replace_txn["maxPriorityFeePerGas"] == two_gwei_in_wei

@flaky(max_runs=3)
@pytest.mark.asyncio
async def test_async_eth_replace_transaction_underpriced(
self, async_w3: "AsyncWeb3", async_unlocked_account_dual_type: ChecksumAddress
Expand Down Expand Up @@ -2256,6 +2261,7 @@ async def test_async_eth_replace_transaction_gas_price_defaulting_minimum(
gas_price * 1.125
) # minimum gas price

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

@flaky(max_runs=3)
@pytest.mark.asyncio
async def test_async_eth_replace_transaction_gas_price_defaulting_strategy_lower(
self, async_w3: "AsyncWeb3", async_unlocked_account: ChecksumAddress
Expand Down Expand Up @@ -2333,7 +2340,6 @@ async def test_async_eth_new_block_filter(self, async_w3: "AsyncWeb3") -> None:

changes = await async_w3.eth.get_filter_changes(filter.filter_id)
assert is_list_like(changes)
assert not changes

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

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

@flaky(max_runs=3)
def test_eth_replace_transaction_legacy(
self, w3: "Web3", unlocked_account_dual_type: ChecksumAddress
) -> None:
Expand Down Expand Up @@ -3286,7 +3294,7 @@ def test_eth_replace_transaction_legacy(
assert replace_txn["gas"] == 21000
assert replace_txn["gasPrice"] == txn_params["gasPrice"]

@flaky(max_runs=5)
@flaky(max_runs=3)
def test_eth_replace_transaction(
self, w3: "Web3", unlocked_account_dual_type: ChecksumAddress
) -> None:
Expand Down Expand Up @@ -3414,6 +3422,7 @@ def test_eth_replace_transaction_gas_price_too_low(
with pytest.raises(ValueError):
w3.eth.replace_transaction(txn_hash, txn_params)

@flaky(max_runs=3)
def test_eth_replace_transaction_gas_price_defaulting_minimum(
self, w3: "Web3", unlocked_account: ChecksumAddress
) -> None:
Expand All @@ -3436,6 +3445,7 @@ def test_eth_replace_transaction_gas_price_defaulting_minimum(
gas_price * 1.125
) # minimum gas price

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

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

@flaky(max_runs=3)
def test_eth_modify_transaction_legacy(
self, w3: "Web3", unlocked_account: ChecksumAddress
) -> None:
Expand Down Expand Up @@ -3518,6 +3530,7 @@ def test_eth_modify_transaction_legacy(
assert modified_txn["gas"] == 21000
assert modified_txn["gasPrice"] == cast(int, txn_params["gasPrice"]) * 2

@flaky(max_runs=3)
def test_eth_modify_transaction(
self, w3: "Web3", unlocked_account: ChecksumAddress
) -> None:
Expand Down
32 changes: 10 additions & 22 deletions web3/tools/benchmark/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
Union,
)

from eth_typing import (
ChecksumAddress,
)

from web3 import (
AsyncHTTPProvider,
AsyncWeb3,
Expand Down Expand Up @@ -100,17 +96,6 @@ async def async_benchmark(func: Callable[..., Any], n: int) -> Union[float, str]
return "N/A"


def unlocked_account(w3: Web3) -> ChecksumAddress:
w3.geth.personal.unlock_account(w3.eth.coinbase, KEYFILE_PW)
return w3.eth.coinbase


async def async_unlocked_account(async_w3: AsyncWeb3) -> ChecksumAddress:
coinbase = await async_w3.eth.coinbase
await async_w3.geth.personal.unlock_account(coinbase, KEYFILE_PW)
return coinbase


def main(logger: logging.Logger, num_calls: int) -> None:
fixture = GethBenchmarkFixture()
for built_fixture in fixture.build():
Expand All @@ -122,12 +107,15 @@ def main(logger: logging.Logger, num_calls: int) -> None:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

# -- sync -- #
coinbase = w3_http.eth.coinbase

# -- async -- #
async_w3_http = loop.run_until_complete(
build_async_w3_http(fixture.endpoint_uri)
)
async_unlocked_acct = loop.run_until_complete(
async_unlocked_account(async_w3_http)
)
async_coinbase = loop.run_until_complete(async_w3_http.eth.coinbase)

methods = [
{
"name": "eth_gasPrice",
Expand All @@ -141,15 +129,15 @@ def main(logger: logging.Logger, num_calls: int) -> None:
"exec": lambda: w3_http.eth.send_transaction(
{
"to": "0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"from": unlocked_account(w3_http),
"value": Wei(12345),
"from": coinbase,
"value": Wei(1),
}
),
"async_exec": lambda: async_w3_http.eth.send_transaction(
{
"to": "0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"from": async_unlocked_acct,
"value": Wei(12345),
"from": async_coinbase,
"value": Wei(1),
}
),
},
Expand Down
9 changes: 6 additions & 3 deletions web3/tools/benchmark/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,23 @@ def _geth_binary(self) -> str:
def _geth_command_arguments(self, datadir: str) -> Sequence[str]:
return (
self.geth_binary,
"--dev",
"--dev.period",
"100",
"--datadir",
str(datadir),
"--nodiscover",
"--fakepow",
"--http",
"--http.port",
self.rpc_port,
"--http.api",
"admin,eth,net,web3,personal,miner",
"admin,eth,net,web3",
"--ipcdisable",
"--allow-insecure-unlock",
"--miner.etherbase",
COINBASE[2:],
"--rpc.enabledeprecatedpersonal",
"--password",
os.path.join(datadir, "keystore", "pw.txt"),
)

def _geth_process(
Expand Down

0 comments on commit 8cc33b6

Please sign in to comment.