web3 7.0.0b6__py3-none-any.whl → 7.0.0b8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. ens/__init__.py +13 -2
  2. web3/__init__.py +21 -5
  3. web3/_utils/contract_sources/contract_data/arrays_contract.py +3 -3
  4. web3/_utils/contract_sources/contract_data/bytes_contracts.py +5 -5
  5. web3/_utils/contract_sources/contract_data/constructor_contracts.py +7 -7
  6. web3/_utils/contract_sources/contract_data/contract_caller_tester.py +3 -3
  7. web3/_utils/contract_sources/contract_data/emitter_contract.py +3 -3
  8. web3/_utils/contract_sources/contract_data/event_contracts.py +5 -5
  9. web3/_utils/contract_sources/contract_data/extended_resolver.py +3 -3
  10. web3/_utils/contract_sources/contract_data/fallback_function_contract.py +3 -3
  11. web3/_utils/contract_sources/contract_data/function_name_tester_contract.py +3 -3
  12. web3/_utils/contract_sources/contract_data/math_contract.py +3 -3
  13. web3/_utils/contract_sources/contract_data/offchain_lookup.py +3 -3
  14. web3/_utils/contract_sources/contract_data/offchain_resolver.py +3 -3
  15. web3/_utils/contract_sources/contract_data/panic_errors_contract.py +3 -3
  16. web3/_utils/contract_sources/contract_data/payable_tester.py +3 -3
  17. web3/_utils/contract_sources/contract_data/receive_function_contracts.py +5 -5
  18. web3/_utils/contract_sources/contract_data/reflector_contracts.py +3 -3
  19. web3/_utils/contract_sources/contract_data/revert_contract.py +3 -3
  20. web3/_utils/contract_sources/contract_data/simple_resolver.py +3 -3
  21. web3/_utils/contract_sources/contract_data/storage_contract.py +3 -3
  22. web3/_utils/contract_sources/contract_data/string_contract.py +3 -3
  23. web3/_utils/contract_sources/contract_data/tuple_contracts.py +5 -5
  24. web3/_utils/http.py +3 -0
  25. web3/_utils/http_session_manager.py +280 -0
  26. web3/_utils/method_formatters.py +19 -3
  27. web3/_utils/module_testing/eth_module.py +84 -111
  28. web3/_utils/module_testing/module_testing_utils.py +22 -18
  29. web3/_utils/module_testing/persistent_connection_provider.py +45 -16
  30. web3/_utils/rpc_abi.py +0 -3
  31. web3/beacon/__init__.py +5 -0
  32. web3/beacon/async_beacon.py +9 -5
  33. web3/beacon/beacon.py +7 -5
  34. web3/contract/__init__.py +7 -0
  35. web3/contract/base_contract.py +10 -1
  36. web3/eth/__init__.py +7 -0
  37. web3/eth/async_eth.py +0 -33
  38. web3/eth/eth.py +2 -53
  39. web3/exceptions.py +6 -0
  40. web3/manager.py +34 -11
  41. web3/middleware/__init__.py +17 -0
  42. web3/module.py +1 -1
  43. web3/providers/__init__.py +21 -0
  44. web3/providers/eth_tester/__init__.py +5 -0
  45. web3/providers/eth_tester/defaults.py +0 -6
  46. web3/providers/eth_tester/middleware.py +3 -8
  47. web3/providers/persistent/__init__.py +7 -0
  48. web3/providers/persistent/async_ipc.py +34 -79
  49. web3/providers/persistent/persistent.py +76 -7
  50. web3/providers/persistent/persistent_connection.py +47 -5
  51. web3/providers/persistent/websocket.py +19 -59
  52. web3/providers/rpc/__init__.py +5 -0
  53. web3/providers/rpc/async_rpc.py +16 -12
  54. web3/providers/rpc/rpc.py +16 -12
  55. web3/providers/rpc/utils.py +0 -3
  56. web3/tools/benchmark/main.py +7 -6
  57. web3/tools/benchmark/node.py +1 -1
  58. web3/utils/__init__.py +14 -5
  59. web3/utils/async_exception_handling.py +19 -7
  60. web3/utils/caching.py +24 -0
  61. web3/utils/exception_handling.py +7 -5
  62. {web3-7.0.0b6.dist-info → web3-7.0.0b8.dist-info}/METADATA +14 -8
  63. {web3-7.0.0b6.dist-info → web3-7.0.0b8.dist-info}/RECORD +66 -67
  64. {web3-7.0.0b6.dist-info → web3-7.0.0b8.dist-info}/WHEEL +1 -1
  65. web3/_utils/contract_sources/contract_data/address_reflector.py +0 -29
  66. web3/_utils/request.py +0 -265
  67. {web3-7.0.0b6.dist-info → web3-7.0.0b8.dist-info}/LICENSE +0 -0
  68. {web3-7.0.0b6.dist-info → web3-7.0.0b8.dist-info}/top_level.txt +0 -0
@@ -1,3 +1,4 @@
1
+ import asyncio
1
2
  import json
2
3
  import math
3
4
  import pytest
@@ -5,7 +6,6 @@ from random import (
5
6
  randint,
6
7
  )
7
8
  import re
8
- import time
9
9
  from typing import (
10
10
  TYPE_CHECKING,
11
11
  Any,
@@ -42,9 +42,6 @@ from hexbytes import (
42
42
  HexBytes,
43
43
  )
44
44
 
45
- from web3._utils.empty import (
46
- empty,
47
- )
48
45
  from web3._utils.ens import (
49
46
  ens_addresses,
50
47
  )
@@ -882,7 +879,8 @@ class AsyncEthModuleTest:
882
879
  async_math_contract: "AsyncContract",
883
880
  params: StateOverrideParams,
884
881
  ) -> None:
885
- txn_params: TxParams = {"from": await async_w3.eth.coinbase}
882
+ accounts = await async_w3.eth.accounts
883
+ txn_params: TxParams = {"from": accounts[0]}
886
884
 
887
885
  # assert does not raise
888
886
  await async_w3.eth.estimate_gas(
@@ -978,9 +976,8 @@ class AsyncEthModuleTest:
978
976
  async def test_eth_getBlockByNumber_latest(
979
977
  self, async_w3: "AsyncWeb3", async_empty_block: BlockData
980
978
  ) -> None:
981
- current_block_number = await async_w3.eth.block_number
982
979
  block = await async_w3.eth.get_block("latest")
983
- assert block["number"] == current_block_number
980
+ assert block["hash"] is not None
984
981
 
985
982
  @pytest.mark.asyncio
986
983
  async def test_eth_getBlockByNumber_not_found(
@@ -993,9 +990,8 @@ class AsyncEthModuleTest:
993
990
  async def test_eth_getBlockByNumber_pending(
994
991
  self, async_w3: "AsyncWeb3", async_empty_block: BlockData
995
992
  ) -> None:
996
- current_block_number = await async_w3.eth.block_number
997
993
  block = await async_w3.eth.get_block("pending")
998
- assert block["number"] == current_block_number + 1
994
+ assert block["hash"] is None
999
995
 
1000
996
  @pytest.mark.asyncio
1001
997
  async def test_eth_getBlockByNumber_earliest(
@@ -1087,7 +1083,6 @@ class AsyncEthModuleTest:
1087
1083
  async_keyfile_account_address_dual_type: ChecksumAddress,
1088
1084
  ) -> None:
1089
1085
  # eth_getRawTransactionByBlockNumberAndIndex: block identifier
1090
- # send a txn to make sure pending block has at least one txn
1091
1086
  await async_w3.eth.send_transaction(
1092
1087
  {
1093
1088
  "from": async_keyfile_account_address_dual_type,
@@ -1095,11 +1090,16 @@ class AsyncEthModuleTest:
1095
1090
  "value": Wei(1),
1096
1091
  }
1097
1092
  )
1098
- pending_block = await async_w3.eth.get_block("pending")
1099
- last_pending_txn_index = len(pending_block["transactions"]) - 1
1100
- raw_txn = await async_w3.eth.get_raw_transaction_by_block(
1101
- "pending", last_pending_txn_index
1102
- )
1093
+
1094
+ async def wait_for_block_with_txn() -> HexBytes:
1095
+ while True:
1096
+ try:
1097
+ return await async_w3.eth.get_raw_transaction_by_block("latest", 0)
1098
+ except TransactionNotFound:
1099
+ await asyncio.sleep(0.1)
1100
+ continue
1101
+
1102
+ raw_txn = await asyncio.wait_for(wait_for_block_with_txn(), timeout=5)
1103
1103
  assert is_bytes(raw_txn)
1104
1104
 
1105
1105
  # eth_getRawTransactionByBlockNumberAndIndex: block number
@@ -1149,14 +1149,15 @@ class AsyncEthModuleTest:
1149
1149
 
1150
1150
  @pytest.mark.asyncio
1151
1151
  async def test_eth_get_balance(self, async_w3: "AsyncWeb3") -> None:
1152
- coinbase = await async_w3.eth.coinbase
1152
+ accounts = await async_w3.eth.accounts
1153
+ account = accounts[0]
1153
1154
 
1154
1155
  with pytest.raises(InvalidAddress):
1155
1156
  await async_w3.eth.get_balance(
1156
- ChecksumAddress(HexAddress(HexStr(coinbase.lower())))
1157
+ ChecksumAddress(HexAddress(HexStr(account.lower())))
1157
1158
  )
1158
1159
 
1159
- balance = await async_w3.eth.get_balance(coinbase)
1160
+ balance = await async_w3.eth.get_balance(account)
1160
1161
 
1161
1162
  assert is_integer(balance)
1162
1163
  assert balance >= 0
@@ -1209,7 +1210,7 @@ class AsyncEthModuleTest:
1209
1210
  assert len(access_list) > 0
1210
1211
  assert access_list[0]["address"] is not None
1211
1212
  assert is_checksum_address(access_list[0]["address"])
1212
- assert len(access_list[0]["storageKeys"][0]) == 32
1213
+ assert len(access_list[0]["storageKeys"][0]) == 66
1213
1214
  assert int(response["gasUsed"]) >= 0
1214
1215
 
1215
1216
  # assert the result can be used directly in a transaction dict
@@ -1235,11 +1236,13 @@ class AsyncEthModuleTest:
1235
1236
  async def test_eth_call(
1236
1237
  self, async_w3: "AsyncWeb3", async_math_contract: "AsyncContract"
1237
1238
  ) -> None:
1238
- coinbase = await async_w3.eth.coinbase
1239
+ accounts = await async_w3.eth.accounts
1240
+ account = accounts[0]
1241
+
1239
1242
  txn_params = async_math_contract._prepare_transaction(
1240
1243
  fn_name="add",
1241
1244
  fn_args=(7, 11),
1242
- transaction={"from": coinbase, "to": async_math_contract.address},
1245
+ transaction={"from": account, "to": async_math_contract.address},
1243
1246
  )
1244
1247
  call_result = await async_w3.eth.call(txn_params)
1245
1248
  assert is_string(call_result)
@@ -1252,10 +1255,11 @@ class AsyncEthModuleTest:
1252
1255
  async_w3: "AsyncWeb3",
1253
1256
  async_revert_contract: "AsyncContract",
1254
1257
  ) -> None:
1255
- coinbase = await async_w3.eth.coinbase
1258
+ accounts = await async_w3.eth.accounts
1259
+ account = accounts[0]
1256
1260
  txn_params = async_revert_contract._prepare_transaction(
1257
1261
  fn_name="normalFunction",
1258
- transaction={"from": coinbase, "to": async_revert_contract.address},
1262
+ transaction={"from": account, "to": async_revert_contract.address},
1259
1263
  )
1260
1264
  call_result = await async_w3.eth.call(txn_params)
1261
1265
  (result,) = async_w3.codec.decode(["bool"], call_result)
@@ -1309,8 +1313,8 @@ class AsyncEthModuleTest:
1309
1313
  async_math_contract: "AsyncContract",
1310
1314
  params: StateOverrideParams,
1311
1315
  ) -> None:
1312
- coinbase = await async_w3.eth.coinbase
1313
- txn_params: TxParams = {"from": coinbase}
1316
+ accounts = await async_w3.eth.accounts
1317
+ txn_params: TxParams = {"from": accounts[0]}
1314
1318
 
1315
1319
  # assert does not raise
1316
1320
  await async_w3.eth.call(
@@ -1321,11 +1325,11 @@ class AsyncEthModuleTest:
1321
1325
  async def test_eth_call_with_0_result(
1322
1326
  self, async_w3: "AsyncWeb3", async_math_contract: "AsyncContract"
1323
1327
  ) -> None:
1324
- coinbase = await async_w3.eth.coinbase
1328
+ accounts = await async_w3.eth.accounts
1325
1329
  txn_params = async_math_contract._prepare_transaction(
1326
1330
  fn_name="add",
1327
1331
  fn_args=(0, 0),
1328
- transaction={"from": coinbase, "to": async_math_contract.address},
1332
+ transaction={"from": accounts[0], "to": async_math_contract.address},
1329
1333
  )
1330
1334
  call_result = await async_w3.eth.call(txn_params)
1331
1335
  assert is_string(call_result)
@@ -1661,23 +1665,12 @@ class AsyncEthModuleTest:
1661
1665
  with pytest.raises(TooManyRequests, match="Too many CCIP read redirects"):
1662
1666
  await async_offchain_lookup_contract.caller().continuousOffchainLookup() # noqa: E501 type: ignore
1663
1667
 
1664
- @pytest.mark.asyncio
1665
- async def test_async_eth_hashrate(self, async_w3: "AsyncWeb3") -> None:
1666
- hashrate = await async_w3.eth.hashrate
1667
- assert is_integer(hashrate)
1668
- assert hashrate >= 0
1669
-
1670
1668
  @pytest.mark.asyncio
1671
1669
  async def test_async_eth_chain_id(self, async_w3: "AsyncWeb3") -> None:
1672
1670
  chain_id = await async_w3.eth.chain_id
1673
1671
  # chain id value from geth fixture genesis file
1674
1672
  assert chain_id == 131277322940537
1675
1673
 
1676
- @pytest.mark.asyncio
1677
- async def test_async_eth_mining(self, async_w3: "AsyncWeb3") -> None:
1678
- mining = await async_w3.eth.mining
1679
- assert is_boolean(mining)
1680
-
1681
1674
  @pytest.mark.asyncio
1682
1675
  async def test_async_eth_get_transaction_receipt_mined(
1683
1676
  self,
@@ -1818,7 +1811,6 @@ class AsyncEthModuleTest:
1818
1811
  assert is_list_like(accounts)
1819
1812
  assert len(accounts) != 0
1820
1813
  assert all(is_checksum_address(account) for account in accounts)
1821
- assert await async_w3.eth.coinbase in accounts
1822
1814
 
1823
1815
  @pytest.mark.asyncio
1824
1816
  async def test_async_eth_get_logs_without_logs(
@@ -2028,10 +2020,10 @@ class AsyncEthModuleTest:
2028
2020
  async def test_async_eth_get_storage_at_invalid_address(
2029
2021
  self, async_w3: "AsyncWeb3"
2030
2022
  ) -> None:
2031
- coinbase = await async_w3.eth.coinbase
2023
+ accounts = await async_w3.eth.accounts
2032
2024
  with pytest.raises(InvalidAddress):
2033
2025
  await async_w3.eth.get_storage_at(
2034
- ChecksumAddress(HexAddress(HexStr(coinbase.lower()))), 0
2026
+ ChecksumAddress(HexAddress(HexStr(accounts[0].lower()))), 0
2035
2027
  )
2036
2028
 
2037
2029
  def test_async_provider_default_account(
@@ -2039,9 +2031,7 @@ class AsyncEthModuleTest:
2039
2031
  async_w3: "AsyncWeb3",
2040
2032
  async_keyfile_account_address_dual_type: ChecksumAddress,
2041
2033
  ) -> None:
2042
- # check defaults to empty
2043
- default_account = async_w3.eth.default_account
2044
- assert default_account is empty
2034
+ current_default_account = async_w3.eth.default_account
2045
2035
 
2046
2036
  # check setter
2047
2037
  async_w3.eth.default_account = async_keyfile_account_address_dual_type
@@ -2049,7 +2039,7 @@ class AsyncEthModuleTest:
2049
2039
  assert default_account == async_keyfile_account_address_dual_type
2050
2040
 
2051
2041
  # reset to default
2052
- async_w3.eth.default_account = empty
2042
+ async_w3.eth.default_account = current_default_account
2053
2043
 
2054
2044
  def test_async_provider_default_block(
2055
2045
  self,
@@ -2517,19 +2507,6 @@ class EthModuleTest:
2517
2507
  assert is_integer(sync_dict["currentBlock"])
2518
2508
  assert is_integer(sync_dict["highestBlock"])
2519
2509
 
2520
- def test_eth_coinbase(self, w3: "Web3") -> None:
2521
- coinbase = w3.eth.coinbase
2522
- assert is_checksum_address(coinbase)
2523
-
2524
- def test_eth_mining(self, w3: "Web3") -> None:
2525
- mining = w3.eth.mining
2526
- assert is_boolean(mining)
2527
-
2528
- def test_eth_hashrate(self, w3: "Web3") -> None:
2529
- hashrate = w3.eth.hashrate
2530
- assert is_integer(hashrate)
2531
- assert hashrate >= 0
2532
-
2533
2510
  def test_eth_chain_id(self, w3: "Web3") -> None:
2534
2511
  chain_id = w3.eth.chain_id
2535
2512
  # chain id value from geth fixture genesis file
@@ -2597,7 +2574,6 @@ class EthModuleTest:
2597
2574
  assert is_list_like(accounts)
2598
2575
  assert len(accounts) != 0
2599
2576
  assert all(is_checksum_address(account) for account in accounts)
2600
- assert w3.eth.coinbase in accounts
2601
2577
 
2602
2578
  def test_eth_block_number(self, w3: "Web3") -> None:
2603
2579
  block_number = w3.eth.block_number
@@ -2610,12 +2586,12 @@ class EthModuleTest:
2610
2586
  assert block_number >= 0
2611
2587
 
2612
2588
  def test_eth_get_balance(self, w3: "Web3") -> None:
2613
- coinbase = w3.eth.coinbase
2589
+ account = w3.eth.accounts[0]
2614
2590
 
2615
2591
  with pytest.raises(InvalidAddress):
2616
- w3.eth.get_balance(ChecksumAddress(HexAddress(HexStr(coinbase.lower()))))
2592
+ w3.eth.get_balance(ChecksumAddress(HexAddress(HexStr(account.lower()))))
2617
2593
 
2618
- balance = w3.eth.get_balance(coinbase)
2594
+ balance = w3.eth.get_balance(account)
2619
2595
 
2620
2596
  assert is_integer(balance)
2621
2597
  assert balance >= 0
@@ -2677,10 +2653,10 @@ class EthModuleTest:
2677
2653
  assert storage == HexBytes(f"0x{'00' * 31}01")
2678
2654
 
2679
2655
  def test_eth_get_storage_at_invalid_address(self, w3: "Web3") -> None:
2680
- coinbase = w3.eth.coinbase
2656
+ account = w3.eth.accounts[0]
2681
2657
  with pytest.raises(InvalidAddress):
2682
2658
  w3.eth.get_storage_at(
2683
- ChecksumAddress(HexAddress(HexStr(coinbase.lower()))), 0
2659
+ ChecksumAddress(HexAddress(HexStr(account.lower()))), 0
2684
2660
  )
2685
2661
 
2686
2662
  def test_eth_get_transaction_count(
@@ -2705,10 +2681,10 @@ class EthModuleTest:
2705
2681
  assert transaction_count >= 0
2706
2682
 
2707
2683
  def test_eth_get_transaction_count_invalid_address(self, w3: "Web3") -> None:
2708
- coinbase = w3.eth.coinbase
2684
+ account = w3.eth.accounts[0]
2709
2685
  with pytest.raises(InvalidAddress):
2710
2686
  w3.eth.get_transaction_count(
2711
- ChecksumAddress(HexAddress(HexStr(coinbase.lower())))
2687
+ ChecksumAddress(HexAddress(HexStr(account.lower())))
2712
2688
  )
2713
2689
 
2714
2690
  def test_eth_getBlockTransactionCountByHash_empty_block(
@@ -2810,7 +2786,7 @@ class EthModuleTest:
2810
2786
  assert len(access_list) > 0
2811
2787
  assert access_list[0]["address"] is not None
2812
2788
  assert is_checksum_address(access_list[0]["address"])
2813
- assert len(access_list[0]["storageKeys"][0]) == 32
2789
+ assert len(access_list[0]["storageKeys"][0]) == 66
2814
2790
  assert int(response["gasUsed"]) >= 0
2815
2791
 
2816
2792
  # assert the result can be used directly in a transaction dict
@@ -3724,11 +3700,10 @@ class EthModuleTest:
3724
3700
  assert txn_hash == HexBytes(signed.hash)
3725
3701
 
3726
3702
  def test_eth_call(self, w3: "Web3", math_contract: "Contract") -> None:
3727
- coinbase = w3.eth.coinbase
3728
3703
  txn_params = math_contract._prepare_transaction(
3729
3704
  fn_name="add",
3730
3705
  fn_args=(7, 11),
3731
- transaction={"from": coinbase, "to": math_contract.address},
3706
+ transaction={"from": w3.eth.accounts[0], "to": math_contract.address},
3732
3707
  )
3733
3708
  call_result = w3.eth.call(txn_params)
3734
3709
  assert is_string(call_result)
@@ -3738,10 +3713,9 @@ class EthModuleTest:
3738
3713
  def test_eth_call_with_override_code(
3739
3714
  self, w3: "Web3", revert_contract: "Contract"
3740
3715
  ) -> None:
3741
- coinbase = w3.eth.coinbase
3742
3716
  txn_params = revert_contract._prepare_transaction(
3743
3717
  fn_name="normalFunction",
3744
- transaction={"from": coinbase, "to": revert_contract.address},
3718
+ transaction={"from": w3.eth.accounts[0], "to": revert_contract.address},
3745
3719
  )
3746
3720
  call_result = w3.eth.call(txn_params)
3747
3721
  (result,) = w3.codec.decode(["bool"], call_result)
@@ -3792,7 +3766,7 @@ class EthModuleTest:
3792
3766
  math_contract: "Contract",
3793
3767
  params: StateOverrideParams,
3794
3768
  ) -> None:
3795
- txn_params: TxParams = {"from": w3.eth.coinbase}
3769
+ txn_params: TxParams = {"from": w3.eth.accounts[0]}
3796
3770
 
3797
3771
  # assert does not raise
3798
3772
  w3.eth.call(txn_params, "latest", {math_contract.address: params})
@@ -3800,11 +3774,10 @@ class EthModuleTest:
3800
3774
  def test_eth_call_with_0_result(
3801
3775
  self, w3: "Web3", math_contract: "Contract"
3802
3776
  ) -> None:
3803
- coinbase = w3.eth.coinbase
3804
3777
  txn_params = math_contract._prepare_transaction(
3805
3778
  fn_name="add",
3806
3779
  fn_args=(0, 0),
3807
- transaction={"from": coinbase, "to": math_contract.address},
3780
+ transaction={"from": w3.eth.accounts[0], "to": math_contract.address},
3808
3781
  )
3809
3782
  call_result = w3.eth.call(txn_params)
3810
3783
  assert is_string(call_result)
@@ -4243,7 +4216,7 @@ class EthModuleTest:
4243
4216
  math_contract: "Contract",
4244
4217
  params: StateOverrideParams,
4245
4218
  ) -> None:
4246
- txn_params: TxParams = {"from": w3.eth.coinbase}
4219
+ txn_params: TxParams = {"from": w3.eth.accounts[0]}
4247
4220
 
4248
4221
  # assert does not raise
4249
4222
  w3.eth.estimate_gas(txn_params, None, {math_contract.address: params})
@@ -4270,12 +4243,9 @@ class EthModuleTest:
4270
4243
  block = w3.eth.get_block(empty_block["number"])
4271
4244
  assert block["number"] == empty_block["number"]
4272
4245
 
4273
- def test_eth_getBlockByNumber_latest(
4274
- self, w3: "Web3", empty_block: BlockData
4275
- ) -> None:
4276
- current_block_number = w3.eth.block_number
4246
+ def test_eth_getBlockByNumber_latest(self, w3: "Web3") -> None:
4277
4247
  block = w3.eth.get_block("latest")
4278
- assert block["number"] == current_block_number
4248
+ assert block["hash"] is not None
4279
4249
 
4280
4250
  def test_eth_getBlockByNumber_not_found(
4281
4251
  self, w3: "Web3", empty_block: BlockData
@@ -4665,39 +4635,42 @@ class EthModuleTest:
4665
4635
  math_contract: "Contract",
4666
4636
  keyfile_account_address: ChecksumAddress,
4667
4637
  ) -> None:
4668
- latest_block = w3.eth.get_block("latest")
4669
- block_num = latest_block["number"]
4670
- block_hash = latest_block["hash"]
4638
+ current_block = w3.eth.get_block("latest")
4639
+ block_num = current_block["number"]
4640
+ block_hash = current_block["hash"]
4671
4641
 
4642
+ default_call_result = math_contract.functions.counter().call()
4672
4643
  latest_call_result = math_contract.functions.counter().call(
4673
4644
  block_identifier="latest"
4674
4645
  )
4646
+
4647
+ # increment counter and get tx receipt
4648
+ tx_hash = math_contract.functions.incrementCounter().transact(
4649
+ {"from": keyfile_account_address}
4650
+ )
4651
+ tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
4652
+
4653
+ # get new state value
4654
+ post_state_block_num_call_result = math_contract.functions.counter().call(
4655
+ block_identifier=tx_receipt["blockNumber"]
4656
+ )
4657
+
4658
+ # call old state values with different block identifiers
4675
4659
  block_hash_call_result = math_contract.functions.counter().call(
4676
4660
  block_identifier=block_hash
4677
4661
  )
4678
- block_num_call_result = math_contract.functions.counter().call(
4662
+ pre_state_block_num_call_result = math_contract.functions.counter().call(
4679
4663
  block_identifier=block_num
4680
4664
  )
4681
- default_call_result = math_contract.functions.counter().call()
4682
-
4683
- # send and wait 1 second to mine
4684
- math_contract.functions.incrementCounter().transact(
4685
- {"from": keyfile_account_address}
4686
- )
4687
- time.sleep(1)
4688
4665
 
4689
- pending_call_result = math_contract.functions.counter().call(
4690
- block_identifier="pending"
4666
+ # assert old state values before incrementing counter
4667
+ assert pre_state_block_num_call_result == post_state_block_num_call_result - 1
4668
+ assert (
4669
+ pre_state_block_num_call_result
4670
+ == block_hash_call_result
4671
+ == default_call_result
4672
+ == latest_call_result
4691
4673
  )
4692
- assert block_hash_call_result == 0
4693
- assert block_num_call_result == 0
4694
- assert latest_call_result == 0
4695
- assert default_call_result == 0
4696
-
4697
- if pending_call_result != 1:
4698
- raise AssertionError(
4699
- f"pending call result was {pending_call_result} instead of 1"
4700
- )
4701
4674
 
4702
4675
  def test_eth_uninstall_filter(self, w3: "Web3") -> None:
4703
4676
  filter = w3.eth.filter({})
@@ -4726,7 +4699,6 @@ class EthModuleTest:
4726
4699
  block_with_txn: BlockData,
4727
4700
  ) -> None:
4728
4701
  # eth_getRawTransactionByBlockNumberAndIndex: block identifier
4729
- # send a txn to make sure pending block has at least one txn
4730
4702
  w3.eth.send_transaction(
4731
4703
  {
4732
4704
  "from": keyfile_account_address_dual_type,
@@ -4734,10 +4706,13 @@ class EthModuleTest:
4734
4706
  "value": Wei(1),
4735
4707
  }
4736
4708
  )
4737
- last_pending_txn_index = len(w3.eth.get_block("pending")["transactions"]) - 1
4738
- raw_transaction = w3.eth.get_raw_transaction_by_block(
4739
- "pending", last_pending_txn_index
4740
- )
4709
+ raw_transaction = None
4710
+ while not raw_transaction:
4711
+ try:
4712
+ raw_transaction = w3.eth.get_raw_transaction_by_block("latest", 0)
4713
+ except TransactionNotFound:
4714
+ continue
4715
+
4741
4716
  assert is_bytes(raw_transaction)
4742
4717
 
4743
4718
  # eth_getRawTransactionByBlockNumberAndIndex: block number
@@ -4781,9 +4756,7 @@ class EthModuleTest:
4781
4756
  def test_default_account(
4782
4757
  self, w3: "Web3", keyfile_account_address_dual_type: ChecksumAddress
4783
4758
  ) -> None:
4784
- # check defaults to empty
4785
- default_account = w3.eth.default_account
4786
- assert default_account is empty
4759
+ current_default = w3.eth.default_account
4787
4760
 
4788
4761
  # check setter
4789
4762
  w3.eth.default_account = keyfile_account_address_dual_type
@@ -4791,7 +4764,7 @@ class EthModuleTest:
4791
4764
  assert default_account == keyfile_account_address_dual_type
4792
4765
 
4793
4766
  # reset to default
4794
- w3.eth.default_account = empty
4767
+ w3.eth.default_account = current_default
4795
4768
 
4796
4769
  def test_default_block(
4797
4770
  self,
@@ -1,6 +1,4 @@
1
- from collections import (
2
- deque,
3
- )
1
+ import asyncio
4
2
  from typing import (
5
3
  TYPE_CHECKING,
6
4
  Any,
@@ -13,6 +11,7 @@ from typing import (
13
11
  )
14
12
 
15
13
  from aiohttp import (
14
+ ClientSession,
16
15
  ClientTimeout,
17
16
  )
18
17
  from eth_typing import (
@@ -28,10 +27,10 @@ from flaky import (
28
27
  from hexbytes import (
29
28
  HexBytes,
30
29
  )
30
+ import requests
31
31
 
32
- from web3._utils.request import (
33
- async_cache_and_return_session,
34
- cache_and_return_session,
32
+ from web3._utils.http import (
33
+ DEFAULT_HTTP_TIMEOUT,
35
34
  )
36
35
  from web3.types import (
37
36
  BlockData,
@@ -40,7 +39,9 @@ from web3.types import (
40
39
 
41
40
  if TYPE_CHECKING:
42
41
  from _pytest.monkeypatch import MonkeyPatch # noqa: F401
43
- from aiohttp import ClientResponse # noqa: F401
42
+ from aiohttp import ( # noqa: F401
43
+ ClientResponse,
44
+ )
44
45
  from requests import Response # noqa: F401
45
46
 
46
47
  from web3 import Web3 # noqa: F401
@@ -102,13 +103,13 @@ def mock_offchain_lookup_request_response(
102
103
 
103
104
  # mock response only to specified url while validating appropriate fields
104
105
  if url_from_args == mocked_request_url:
105
- assert kwargs["timeout"] == 30
106
+ assert kwargs["timeout"] == DEFAULT_HTTP_TIMEOUT
106
107
  if http_method.upper() == "POST":
107
108
  assert kwargs["data"] == {"data": calldata, "sender": sender}
108
109
  return MockedResponse()
109
110
 
110
111
  # else, make a normal request (no mocking)
111
- session = cache_and_return_session(url_from_args)
112
+ session = requests.Session()
112
113
  return session.request(method=http_method.upper(), url=url_from_args, **kwargs)
113
114
 
114
115
  monkeypatch.setattr(
@@ -152,16 +153,18 @@ def async_mock_offchain_lookup_request_response(
152
153
 
153
154
  # mock response only to specified url while validating appropriate fields
154
155
  if url_from_args == mocked_request_url:
155
- assert kwargs["timeout"] == ClientTimeout(30)
156
+ assert kwargs["timeout"] == ClientTimeout(DEFAULT_HTTP_TIMEOUT)
156
157
  if http_method.upper() == "post":
157
158
  assert kwargs["data"] == {"data": calldata, "sender": sender}
158
159
  return AsyncMockedResponse()
159
160
 
160
161
  # else, make a normal request (no mocking)
161
- session = await async_cache_and_return_session(url_from_args)
162
- return await session.request(
162
+ session = ClientSession()
163
+ response = await session.request(
163
164
  method=http_method.upper(), url=url_from_args, **kwargs
164
165
  )
166
+ await session.close()
167
+ return response
165
168
 
166
169
  monkeypatch.setattr(
167
170
  f"aiohttp.ClientSession.{http_method.lower()}", _mock_specific_request
@@ -174,7 +177,9 @@ class WebSocketMessageStreamMock:
174
177
  def __init__(
175
178
  self, messages: Collection[bytes] = None, raise_exception: Exception = None
176
179
  ) -> None:
177
- self.messages = deque(messages) if messages else deque()
180
+ self.queue = asyncio.Queue() # type: ignore # py38 issue
181
+ for msg in messages or []:
182
+ self.queue.put_nowait(msg)
178
183
  self.raise_exception = raise_exception
179
184
 
180
185
  def __await__(self) -> Generator[Any, Any, "Self"]:
@@ -187,13 +192,12 @@ class WebSocketMessageStreamMock:
187
192
  return self
188
193
 
189
194
  async def __anext__(self) -> bytes:
195
+ return await self.queue.get()
196
+
197
+ async def recv(self) -> bytes:
190
198
  if self.raise_exception:
191
199
  raise self.raise_exception
192
-
193
- elif len(self.messages) == 0:
194
- raise StopAsyncIteration
195
-
196
- return self.messages.popleft()
200
+ return await self.queue.get()
197
201
 
198
202
  @staticmethod
199
203
  async def pong() -> Literal[False]:
@@ -23,6 +23,7 @@ from web3.middleware import (
23
23
  )
24
24
  from web3.types import (
25
25
  FormattedEthSubscriptionResponse,
26
+ RPCEndpoint,
26
27
  )
27
28
 
28
29
  if TYPE_CHECKING:
@@ -31,6 +32,22 @@ if TYPE_CHECKING:
31
32
  )
32
33
 
33
34
 
35
+ SOME_BLOCK_KEYS = [
36
+ "number",
37
+ "hash",
38
+ "parentHash",
39
+ "transactionsRoot",
40
+ "stateRoot",
41
+ "receiptsRoot",
42
+ "size",
43
+ "gasLimit",
44
+ "gasUsed",
45
+ "timestamp",
46
+ "transactions",
47
+ "baseFeePerGas",
48
+ ]
49
+
50
+
34
51
  class PersistentConnectionProviderTest:
35
52
  @pytest.mark.asyncio
36
53
  @pytest.mark.parametrize(
@@ -301,6 +318,7 @@ class PersistentConnectionProviderTest:
301
318
  assert response["result"] == expected_formatted_result
302
319
 
303
320
  # only testing one message, so break here
321
+ await async_w3.eth.unsubscribe(sub_id)
304
322
  break
305
323
 
306
324
  @pytest.mark.asyncio
@@ -371,22 +389,8 @@ class PersistentConnectionProviderTest:
371
389
  assert isinstance(pending, AttributeDict)
372
390
 
373
391
  # assert block values
374
- some_block_keys = [
375
- "number",
376
- "hash",
377
- "parentHash",
378
- "transactionsRoot",
379
- "stateRoot",
380
- "receiptsRoot",
381
- "size",
382
- "gasLimit",
383
- "gasUsed",
384
- "timestamp",
385
- "transactions",
386
- "baseFeePerGas",
387
- ]
388
- assert all(k in latest.keys() for k in some_block_keys)
389
- assert all(k in pending.keys() for k in some_block_keys)
392
+ assert all(k in latest.keys() for k in SOME_BLOCK_KEYS)
393
+ assert all(k in pending.keys() for k in SOME_BLOCK_KEYS)
390
394
 
391
395
  assert isinstance(block_num, int)
392
396
  assert latest["number"] == block_num
@@ -394,3 +398,28 @@ class PersistentConnectionProviderTest:
394
398
  assert isinstance(chain_id, int)
395
399
  assert isinstance(chain_id2, int)
396
400
  assert isinstance(chain_id3, int)
401
+
402
+ @pytest.mark.asyncio
403
+ async def test_public_socket_api(self, async_w3: "AsyncWeb3") -> None:
404
+ # send a request over the socket
405
+ await async_w3.socket.send(
406
+ RPCEndpoint("eth_getBlockByNumber"), ["latest", True]
407
+ )
408
+
409
+ # recv and validate the unprocessed response
410
+ response = await async_w3.socket.recv()
411
+ assert "id" in response, "Expected 'id' key in response."
412
+ assert "jsonrpc" in response, "Expected 'jsonrpc' key in response."
413
+ assert "result" in response, "Expected 'result' key in response."
414
+ assert all(k in response["result"].keys() for k in SOME_BLOCK_KEYS)
415
+ assert not isinstance(response["result"]["number"], int) # assert not processed
416
+
417
+ # make a request over the socket
418
+ response = await async_w3.socket.make_request(
419
+ RPCEndpoint("eth_getBlockByNumber"), ["latest", True]
420
+ )
421
+ assert "id" in response, "Expected 'id' key in response."
422
+ assert "jsonrpc" in response, "Expected 'jsonrpc' key in response."
423
+ assert "result" in response, "Expected 'result' key in response."
424
+ assert all(k in response["result"].keys() for k in SOME_BLOCK_KEYS)
425
+ assert not isinstance(response["result"]["number"], int) # assert not processed