Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3695.internal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Address some flaky tests due to a geth bug in state synchronization.
65 changes: 47 additions & 18 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,8 @@ async def test_async_sign_authorization_and_send_raw_set_code_transaction(
keyfile_account_pkey: HexStr,
async_math_contract: "AsyncContract",
) -> None:
# TODO: remove blockNumber block_id from eth_call and eth_getCode calls once
# geth behavior for "latest" seems stable again.
keyfile_account = async_w3.eth.account.from_key(keyfile_account_pkey)

chain_id = await async_w3.eth.chain_id
Expand All @@ -737,9 +739,7 @@ async def test_async_sign_authorization_and_send_raw_set_code_transaction(

# get current math counter and increase it only in the delegation by n
math_counter = await async_math_contract.functions.counter().call()
built_tx = await async_math_contract.functions.incrementCounter(
math_counter + 1337
).build_transaction({})
data = async_math_contract.encode_abi("incrementCounter", [math_counter + 1337])
txn: TxParams = {
"chainId": chain_id,
"to": keyfile_account.address,
Expand All @@ -748,23 +748,30 @@ async def test_async_sign_authorization_and_send_raw_set_code_transaction(
"nonce": nonce,
"maxPriorityFeePerGas": Wei(10**9),
"maxFeePerGas": Wei(10**9),
"data": built_tx["data"],
"data": data,
"authorizationList": [signed_auth],
}

signed = keyfile_account.sign_transaction(txn)
tx_hash = await async_w3.eth.send_raw_transaction(signed.raw_transaction)
get_tx = await async_w3.eth.get_transaction(tx_hash)
await async_w3.eth.wait_for_transaction_receipt(tx_hash, timeout=10)
tx_receipt = await async_w3.eth.wait_for_transaction_receipt(
tx_hash, timeout=10
)

code = await async_w3.eth.get_code(keyfile_account.address)
code = await async_w3.eth.get_code(
keyfile_account.address, block_identifier=tx_receipt["blockNumber"]
)
assert code.to_0x_hex() == f"0xef0100{async_math_contract.address[2:].lower()}"
delegated = async_w3.eth.contract(
address=keyfile_account.address, abi=async_math_contract.abi
)

# assert the math counter is increased by 1337 only in delegated acct
assert await async_math_contract.functions.counter().call() == math_counter
delegated_call = await delegated.functions.counter().call()
delegated_call = await delegated.functions.counter().call(
block_identifier=tx_receipt["blockNumber"]
)
assert delegated_call == math_counter + 1337

assert len(get_tx["authorizationList"]) == 1
Expand All @@ -791,9 +798,13 @@ async def test_async_sign_authorization_and_send_raw_set_code_transaction(
reset_tx_hash = await async_w3.eth.send_raw_transaction(
signed_reset.raw_transaction
)
await async_w3.eth.wait_for_transaction_receipt(reset_tx_hash, timeout=10)
reset_tx_receipt = await async_w3.eth.wait_for_transaction_receipt(
reset_tx_hash, timeout=10
)

reset_code = await async_w3.eth.get_code(keyfile_account.address)
reset_code = await async_w3.eth.get_code(
keyfile_account.address, reset_tx_receipt["blockNumber"]
)
assert reset_code == HexBytes("0x")

@pytest.mark.asyncio
Expand Down Expand Up @@ -1881,6 +1892,10 @@ async def test_async_eth_wait_for_transaction_receipt_mined(
assert effective_gas_price > 0

@pytest.mark.asyncio
# TODO: Remove xfail when issue has been identified
@pytest.mark.xfail(
reason="latest geth seems to cause this to be flaky", strict=False
)
async def test_async_eth_wait_for_transaction_receipt_unmined(
self,
async_w3: "AsyncWeb3",
Expand Down Expand Up @@ -3855,6 +3870,8 @@ def test_sign_and_send_raw_middleware(
def test_sign_authorization_and_send_raw_set_code_transaction(
self, w3: "Web3", keyfile_account_pkey: HexStr, math_contract: "Contract"
) -> None:
# TODO: remove blockNumber block_id from eth_call and eth_getCode calls once
# geth behavior for "latest" seems stable again.
keyfile_account = w3.eth.account.from_key(keyfile_account_pkey)

chain_id = w3.eth.chain_id
Expand All @@ -3869,9 +3886,7 @@ def test_sign_authorization_and_send_raw_set_code_transaction(

# get current math counter and increase it only in the delegation by n
math_counter = math_contract.functions.counter().call()
data = math_contract.functions.incrementCounter(
math_counter + 1337
).build_transaction({})["data"]
data = math_contract.encode_abi("incrementCounter", [math_counter + 1337])
txn: TxParams = {
"chainId": chain_id,
"to": keyfile_account.address,
Expand All @@ -3887,16 +3902,26 @@ def test_sign_authorization_and_send_raw_set_code_transaction(
signed = keyfile_account.sign_transaction(txn)
tx_hash = w3.eth.send_raw_transaction(signed.raw_transaction)
get_tx = w3.eth.get_transaction(tx_hash)
w3.eth.wait_for_transaction_receipt(tx_hash, timeout=10)
receipt = w3.eth.wait_for_transaction_receipt(tx_hash, timeout=10)

code = w3.eth.get_code(keyfile_account.address)
code = w3.eth.get_code(
keyfile_account.address, block_identifier=receipt["blockNumber"]
)
assert code.to_0x_hex() == f"0xef0100{math_contract.address[2:].lower()}"
delegated = w3.eth.contract(
address=keyfile_account.address, abi=math_contract.abi
)
# assert the math counter is increased by 1337 only in delegated acct
assert math_contract.functions.counter().call() == math_counter
assert delegated.functions.counter().call() == math_counter + 1337
assert (
math_contract.functions.counter().call(
block_identifier=receipt["blockNumber"]
)
== math_counter
)
assert (
delegated.functions.counter().call(block_identifier=receipt["blockNumber"])
== math_counter + 1337
)

assert len(get_tx["authorizationList"]) == 1
get_auth = get_tx["authorizationList"][0]
Expand All @@ -3920,9 +3945,13 @@ def test_sign_authorization_and_send_raw_set_code_transaction(

signed_reset = keyfile_account.sign_transaction(new_txn)
reset_tx_hash = w3.eth.send_raw_transaction(signed_reset.raw_transaction)
w3.eth.wait_for_transaction_receipt(reset_tx_hash, timeout=10)
reset_tx_receipt = w3.eth.wait_for_transaction_receipt(
reset_tx_hash, timeout=10
)

reset_code = w3.eth.get_code(keyfile_account.address)
reset_code = w3.eth.get_code(
keyfile_account.address, block_identifier=reset_tx_receipt["blockNumber"]
)
assert reset_code == HexBytes("0x")

def test_eth_call(self, w3: "Web3", math_contract: "Contract") -> None:
Expand Down