Skip to content

Commit f19eb42

Browse files
committed
Give more stability to tests using either "pending" or "latest":
- Set a timeout and loop over the latest block a few times until desired tx is included. - Some processing, such as async IPC, goes faster than others. There'stability not a great way to ensure if we are in the "pending" or "latest" state for blocks using geth --dev. The important thing for testing on our end is that the JSON-RPC method works the way it's expected. Whether or not it returns the expected result based on block number, etc, is on Geth's side to make sure. Test the JSON-RPC method and give it flexibility in this commit.
1 parent aedff9f commit f19eb42

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

web3/_utils/module_testing/eth_module.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import json
23
import math
34
import pytest
@@ -992,9 +993,8 @@ async def test_eth_getBlockByNumber_not_found(
992993
async def test_eth_getBlockByNumber_pending(
993994
self, async_w3: "AsyncWeb3", async_empty_block: BlockData
994995
) -> None:
995-
current_block_number = await async_w3.eth.block_number
996996
block = await async_w3.eth.get_block("pending")
997-
assert block["number"] == current_block_number + 1
997+
assert block["hash"] is None
998998

999999
@pytest.mark.asyncio
10001000
async def test_eth_getBlockByNumber_earliest(
@@ -1086,16 +1086,23 @@ async def test_eth_get_raw_transaction_by_block(
10861086
async_keyfile_account_address_dual_type: ChecksumAddress,
10871087
) -> None:
10881088
# eth_getRawTransactionByBlockNumberAndIndex: block identifier
1089-
# send a tx and wait until the latest block includes it
1090-
tx_hash = await async_w3.eth.send_transaction(
1089+
await async_w3.eth.send_transaction(
10911090
{
10921091
"from": async_keyfile_account_address_dual_type,
10931092
"to": async_keyfile_account_address_dual_type,
10941093
"value": Wei(1),
10951094
}
10961095
)
1097-
await async_w3.eth.wait_for_transaction_receipt(tx_hash)
1098-
raw_txn = await async_w3.eth.get_raw_transaction_by_block("latest", 0)
1096+
1097+
async def wait_for_block_with_txn() -> HexBytes:
1098+
while True:
1099+
try:
1100+
return await async_w3.eth.get_raw_transaction_by_block("latest", 0)
1101+
except TransactionNotFound:
1102+
await asyncio.sleep(0.1)
1103+
continue
1104+
1105+
raw_txn = await asyncio.wait_for(wait_for_block_with_txn(), timeout=5)
10991106
assert is_bytes(raw_txn)
11001107

11011108
# eth_getRawTransactionByBlockNumberAndIndex: block number
@@ -4698,16 +4705,20 @@ def test_eth_get_raw_transaction_by_block(
46984705
block_with_txn: BlockData,
46994706
) -> None:
47004707
# eth_getRawTransactionByBlockNumberAndIndex: block identifier
4701-
# send a tx and wait until the latest block includes it
4702-
tx_hash = w3.eth.send_transaction(
4708+
w3.eth.send_transaction(
47034709
{
47044710
"from": keyfile_account_address_dual_type,
47054711
"to": keyfile_account_address_dual_type,
47064712
"value": Wei(1),
47074713
}
47084714
)
4709-
w3.eth.wait_for_transaction_receipt(tx_hash)
4710-
raw_transaction = w3.eth.get_raw_transaction_by_block("latest", 0)
4715+
raw_transaction = None
4716+
while not raw_transaction:
4717+
try:
4718+
raw_transaction = w3.eth.get_raw_transaction_by_block("latest", 0)
4719+
except TransactionNotFound:
4720+
continue
4721+
47114722
assert is_bytes(raw_transaction)
47124723

47134724
# eth_getRawTransactionByBlockNumberAndIndex: block number

0 commit comments

Comments
 (0)