|
2 | 2 | import collections
|
3 | 3 | import itertools
|
4 | 4 |
|
| 5 | +from eth_account import ( |
| 6 | + Account, |
| 7 | +) |
5 | 8 | from eth_utils import (
|
6 | 9 | to_checksum_address,
|
7 | 10 | to_int,
|
8 | 11 | )
|
9 | 12 | from hexbytes import (
|
10 | 13 | HexBytes,
|
11 | 14 | )
|
| 15 | +import pytest_asyncio |
12 | 16 |
|
| 17 | +from tests.core.contracts.utils import ( |
| 18 | + async_deploy, |
| 19 | + deploy, |
| 20 | +) |
| 21 | +from web3._utils.contract_sources.contract_data.math_contract import ( |
| 22 | + MATH_CONTRACT_DATA, |
| 23 | +) |
13 | 24 | from web3._utils.ens import (
|
14 | 25 | ens_addresses,
|
15 | 26 | )
|
|
29 | 40 | RECEIPT_TIMEOUT = 0.2
|
30 | 41 |
|
31 | 42 |
|
| 43 | +@pytest.fixture |
| 44 | +def math_contract(w3): |
| 45 | + factory = w3.eth.contract(**MATH_CONTRACT_DATA) |
| 46 | + return deploy(w3, factory) |
| 47 | + |
| 48 | + |
32 | 49 | def _tx_indexing_response_iterator():
|
33 | 50 | while True:
|
34 | 51 | yield {"error": {"message": "transaction indexing in progress"}}
|
@@ -371,9 +388,86 @@ def test_eth_send_raw_blob_transaction(w3):
|
371 | 388 | )
|
372 | 389 |
|
373 | 390 |
|
| 391 | +def test_send_set_code_transaction(w3, math_contract): |
| 392 | + pkey = w3.provider.ethereum_tester.backend.account_keys[0] |
| 393 | + acct = Account.from_key(pkey) |
| 394 | + |
| 395 | + nonce = w3.eth.get_transaction_count(acct.address) |
| 396 | + chain_id = w3.eth.chain_id |
| 397 | + |
| 398 | + auth = { |
| 399 | + "chainId": chain_id, |
| 400 | + "address": math_contract.address, |
| 401 | + "nonce": nonce + 1, |
| 402 | + } |
| 403 | + signed_auth = acct.sign_authorization(auth) |
| 404 | + |
| 405 | + # get current math counter and increase it only in the delegation by n |
| 406 | + math_counter = math_contract.functions.counter().call() |
| 407 | + built_tx = math_contract.functions.incrementCounter( |
| 408 | + math_counter + 1337 |
| 409 | + ).build_transaction({}) |
| 410 | + txn = { |
| 411 | + "chainId": chain_id, |
| 412 | + "to": acct.address, |
| 413 | + "value": 0, |
| 414 | + "gas": 200_000, |
| 415 | + "nonce": nonce, |
| 416 | + "maxPriorityFeePerGas": 10**9, |
| 417 | + "maxFeePerGas": 10**9, |
| 418 | + "data": built_tx["data"], |
| 419 | + "authorizationList": [signed_auth], |
| 420 | + } |
| 421 | + |
| 422 | + tx_hash = w3.eth.send_transaction(txn) |
| 423 | + get_tx = w3.eth.get_transaction(tx_hash) |
| 424 | + w3.eth.wait_for_transaction_receipt(tx_hash, timeout=10) |
| 425 | + |
| 426 | + code = w3.eth.get_code(acct.address) |
| 427 | + |
| 428 | + assert code.to_0x_hex().lower() == f"0xef0100{math_contract.address[2:].lower()}" |
| 429 | + delegated = w3.eth.contract(address=acct.address, abi=math_contract.abi) |
| 430 | + # assert the math counter is increased by 1337 only in delegated acct |
| 431 | + assert math_contract.functions.counter().call() == math_counter |
| 432 | + delegated_call = delegated.functions.counter().call() |
| 433 | + assert delegated_call == math_counter + 1337 |
| 434 | + |
| 435 | + assert len(get_tx["authorizationList"]) == 1 |
| 436 | + get_auth = get_tx["authorizationList"][0] |
| 437 | + assert get_auth["chainId"] == chain_id |
| 438 | + assert get_auth["address"].lower() == math_contract.address.lower() |
| 439 | + assert get_auth["nonce"] == nonce + 1 |
| 440 | + assert isinstance(get_auth["yParity"], int) |
| 441 | + assert isinstance(get_auth["r"], HexBytes) |
| 442 | + assert isinstance(get_auth["s"], HexBytes) |
| 443 | + |
| 444 | + # reset code |
| 445 | + reset_auth = { |
| 446 | + "chainId": chain_id, |
| 447 | + "address": "0x" + ("00" * 20), |
| 448 | + "nonce": nonce + 3, |
| 449 | + } |
| 450 | + signed_reset_auth = acct.sign_authorization(reset_auth) |
| 451 | + new_txn = dict(txn) |
| 452 | + new_txn["authorizationList"] = [signed_reset_auth] |
| 453 | + new_txn["nonce"] = nonce + 2 |
| 454 | + |
| 455 | + reset_tx_hash = w3.eth.send_transaction(new_txn) |
| 456 | + w3.eth.wait_for_transaction_receipt(reset_tx_hash, timeout=10) |
| 457 | + |
| 458 | + reset_code = w3.eth.get_code(acct.address) |
| 459 | + assert reset_code == HexBytes("0x") |
| 460 | + |
| 461 | + |
374 | 462 | # --- async --- #
|
375 | 463 |
|
376 | 464 |
|
| 465 | +@pytest_asyncio.fixture |
| 466 | +async def async_math_contract(async_w3): |
| 467 | + factory = async_w3.eth.contract(**MATH_CONTRACT_DATA) |
| 468 | + return await async_deploy(async_w3, factory) |
| 469 | + |
| 470 | + |
377 | 471 | @pytest.mark.asyncio
|
378 | 472 | async def test_async_wait_for_transaction_receipt_transaction_indexing_in_progress(
|
379 | 473 | async_w3, request_mocker
|
@@ -420,3 +514,77 @@ async def test_async_send_raw_blob_transaction(async_w3):
|
420 | 514 | assert transaction["blobVersionedHashes"][0] == HexBytes(
|
421 | 515 | "0x0127c38bcad458d932e828b580b9ad97310be01407dfa0ed88118735980a3e9a"
|
422 | 516 | )
|
| 517 | + |
| 518 | + |
| 519 | +@pytest.mark.asyncio |
| 520 | +async def test_async_send_set_code_transaction(async_w3, async_math_contract): |
| 521 | + pkey = async_w3.provider.ethereum_tester.backend.account_keys[0] |
| 522 | + acct = Account.from_key(pkey) |
| 523 | + |
| 524 | + nonce = await async_w3.eth.get_transaction_count(acct.address) |
| 525 | + chain_id = await async_w3.eth.chain_id |
| 526 | + |
| 527 | + auth = { |
| 528 | + "chainId": chain_id, |
| 529 | + "address": async_math_contract.address, |
| 530 | + "nonce": nonce + 1, |
| 531 | + } |
| 532 | + signed_auth = acct.sign_authorization(auth) |
| 533 | + |
| 534 | + # get current math counter and increase it only in the delegation by n |
| 535 | + math_counter = await async_math_contract.functions.counter().call() |
| 536 | + built_tx = await async_math_contract.functions.incrementCounter( |
| 537 | + math_counter + 1337 |
| 538 | + ).build_transaction({}) |
| 539 | + txn = { |
| 540 | + "chainId": chain_id, |
| 541 | + "to": acct.address, |
| 542 | + "value": 0, |
| 543 | + "gas": 200_000, |
| 544 | + "nonce": nonce, |
| 545 | + "maxPriorityFeePerGas": 10**9, |
| 546 | + "maxFeePerGas": 10**9, |
| 547 | + "data": built_tx["data"], |
| 548 | + "authorizationList": [signed_auth], |
| 549 | + } |
| 550 | + |
| 551 | + tx_hash = await async_w3.eth.send_transaction(txn) |
| 552 | + get_tx = await async_w3.eth.get_transaction(tx_hash) |
| 553 | + await async_w3.eth.wait_for_transaction_receipt(tx_hash, timeout=10) |
| 554 | + |
| 555 | + code = await async_w3.eth.get_code(acct.address) |
| 556 | + |
| 557 | + assert ( |
| 558 | + code.to_0x_hex().lower() == f"0xef0100{async_math_contract.address[2:].lower()}" |
| 559 | + ) |
| 560 | + delegated = async_w3.eth.contract(address=acct.address, abi=async_math_contract.abi) |
| 561 | + # assert the math counter is increased by 1337 only in delegated acct |
| 562 | + assert await async_math_contract.functions.counter().call() == math_counter |
| 563 | + delegated_call = await delegated.functions.counter().call() |
| 564 | + assert delegated_call == math_counter + 1337 |
| 565 | + |
| 566 | + assert len(get_tx["authorizationList"]) == 1 |
| 567 | + get_auth = get_tx["authorizationList"][0] |
| 568 | + assert get_auth["chainId"] == chain_id |
| 569 | + assert get_auth["address"].lower() == async_math_contract.address.lower() |
| 570 | + assert get_auth["nonce"] == nonce + 1 |
| 571 | + assert isinstance(get_auth["yParity"], int) |
| 572 | + assert isinstance(get_auth["r"], HexBytes) |
| 573 | + assert isinstance(get_auth["s"], HexBytes) |
| 574 | + |
| 575 | + # reset code |
| 576 | + reset_auth = { |
| 577 | + "chainId": chain_id, |
| 578 | + "address": "0x" + ("00" * 20), |
| 579 | + "nonce": nonce + 3, |
| 580 | + } |
| 581 | + signed_reset_auth = acct.sign_authorization(reset_auth) |
| 582 | + new_txn = dict(txn) |
| 583 | + new_txn["authorizationList"] = [signed_reset_auth] |
| 584 | + new_txn["nonce"] = nonce + 2 |
| 585 | + |
| 586 | + reset_tx_hash = await async_w3.eth.send_transaction(new_txn) |
| 587 | + await async_w3.eth.wait_for_transaction_receipt(reset_tx_hash, timeout=10) |
| 588 | + |
| 589 | + reset_code = await async_w3.eth.get_code(acct.address) |
| 590 | + assert reset_code == HexBytes("0x") |
0 commit comments