diff --git a/benchmarks/ens/fake_rpc.py b/benchmarks/ens/fake_rpc.py index 74ef9efde9..4cb534f0b9 100644 --- a/benchmarks/ens/fake_rpc.py +++ b/benchmarks/ens/fake_rpc.py @@ -1,20 +1,24 @@ +# Ensures all names in NAMES_FULL_COVERAGE resolve successfully in benchmarks. +from benchmarks.web3._utils.params import BLOCK_DICT + FAKE_ENS_REGISTRY = "0x0000000000000000000000000000000000000002" FAKE_RESOLVER = "0x0000000000000000000000000000000000000001" FAKE_RESULT_ADDR = "0x314159265dD8dbb310642f98f50C066173C1259b" - def fake_json_rpc_response(request_data: dict): method = request_data.get("method") params = request_data.get("params", []) if method == "eth_call": call_data = params[0] to_addr = call_data.get("to", "").lower() + # Always return a valid resolver for any registry call if to_addr == FAKE_ENS_REGISTRY.lower(): return { "jsonrpc": "2.0", "id": request_data["id"], "result": "0x" + "0" * 24 + FAKE_RESOLVER[2:], } + # Always return a valid address for any resolver call, even for empty or unicode names elif to_addr == FAKE_RESOLVER.lower(): return { "jsonrpc": "2.0", @@ -22,6 +26,7 @@ def fake_json_rpc_response(request_data: dict): "result": "0x" + "0" * 24 + FAKE_RESULT_ADDR[2:], } else: + # For any other address, return zero address (should not be used in these benchmarks) return { "jsonrpc": "2.0", "id": request_data["id"], @@ -37,4 +42,12 @@ def fake_json_rpc_response(request_data: dict): } else: return {"jsonrpc": "2.0", "id": request_data["id"], "result": "0x"} - return {"jsonrpc": "2.0", "id": request_data["id"], "result": None} + elif method == "eth_getBlockByNumber": + # Return a realistic block dict for block queries (shared with tests) + return { + "jsonrpc": "2.0", + "id": request_data["id"], + "result": BLOCK_DICT, + } + # Default: always return a valid hex string for any other method + return {"jsonrpc": "2.0", "id": request_data["id"], "result": "0x"} diff --git a/benchmarks/ens/test_ens_benchmarks.py b/benchmarks/ens/test_ens_benchmarks.py index 6926699e46..622247b6cb 100644 --- a/benchmarks/ens/test_ens_benchmarks.py +++ b/benchmarks/ens/test_ens_benchmarks.py @@ -7,23 +7,26 @@ try: import ens.ens - import ens.exceptions import web3 except ImportError: pass import faster_ens.ens -import faster_ens.exceptions import faster_web3 from benchmarks.ens.params import parametrize_names_full_coverage from benchmarks.ens.fake_rpc import fake_json_rpc_response, FAKE_ENS_REGISTRY -def run_100(func, exc, *args, **kwargs): +def run_100(func, *args): + for _ in range(100): + func(*args) + + +def run_100_exc(exc, func, *args): for _ in range(100): try: - func(*args, **kwargs) + func(*args) except exc: pass @@ -33,14 +36,24 @@ def __init__(self, result): self.status_code = 200 self._result = result self.headers = {} - self.content = self.text = "" + # Ensure .text and .content are valid JSON strings + self.text = self.content = json.dumps(result) def json(self): return self._result + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + return False + + def raise_for_status(self) -> None: + pass + def fake_send(*args, **kwargs): - request_data = json.loads(args[1].body) + request_data = json.loads(args[0].body) return FakeResponse(fake_json_rpc_response(request_data)) @@ -51,7 +64,10 @@ def test_address(benchmark: BenchmarkFixture, name): provider = web3.HTTPProvider("http://localhost:8545") # Patch the ENS registry address to our fake one ns = ens.ens.ENS(provider=provider, addr=FAKE_ENS_REGISTRY) - benchmark(run_100, ens.exceptions.ENSException, ns.address, name) + if name == "": + benchmark(run_100_exc, UnboundLocalError, ns.address, name) + else: + benchmark(run_100, ns.address, name) @pytest.mark.benchmark(group="ENS.address") @@ -60,4 +76,7 @@ def test_faster_address(benchmark: BenchmarkFixture, name): with patch("requests.Session.send", side_effect=fake_send): provider = faster_web3.HTTPProvider("http://localhost:8545") ns = faster_ens.ens.ENS(provider=provider, addr=FAKE_ENS_REGISTRY) - benchmark(run_100, faster_ens.exceptions.ENSException, ns.address, name) + if name == "": + benchmark(run_100_exc, UnboundLocalError, ns.address, name) + else: + benchmark(run_100, ns.address, name) diff --git a/benchmarks/web3/_utils/params.py b/benchmarks/web3/_utils/params.py index 566036ea13..2d60bd06a4 100644 --- a/benchmarks/web3/_utils/params.py +++ b/benchmarks/web3/_utils/params.py @@ -4,11 +4,11 @@ TX_DICT = { "from": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e", "to": "0x53d284357ec70cE289D6D64134DfAc8E511c8a3D", - "value": 1000000000000000000, - "gas": 21000, + "value": hex(1000000000000000000), + "gas": hex(21000), "data": "0x", - "nonce": 12, - "gasPrice": 50000000000, + "nonce": hex(12), + "gasPrice": hex(50000000000), } # Log entry (mainnet-style) @@ -20,38 +20,38 @@ "0x000000000000000000000000742d35cc6634c0532925a3b844bc454e4438f44e", ], "data": "0x00000000000000000000000000000000000000000000000000000000000003e8", - "blockNumber": 12345678, + "blockNumber": hex(12345678), "transactionHash": "0x5e1d3a76fbf824220e1c5e0c2e5e7e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1", - "logIndex": 0, + "logIndex": hex(0), } # Block dict (mainnet-style, minimal for formatter) BLOCK_DICT = { - "number": 12345678, + "number": hex(12345678), "hash": "0x5e1d3a76fbf824220e1c5e0c2e5e7e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1", "transactions": [TX_DICT, TX_DICT], "logs": [LOG_ENTRY, LOG_ENTRY], "miner": "0x829BD824B016326A401d083B33D092293333A830", - "gasLimit": 15000000, - "gasUsed": 12000000, + "gasLimit": hex(15000000), + "gasUsed": hex(12000000), } # Receipt dict (minimal) RECEIPT_DICT = { "blockHash": BLOCK_DICT["hash"], "blockNumber": BLOCK_DICT["number"], - "transactionIndex": 0, + "transactionIndex": hex(64), "transactionHash": TX_DICT["from"], - "cumulativeGasUsed": 21000, - "status": 1, - "gasUsed": 21000, + "cumulativeGasUsed": hex(21000), + "status": hex(1), + "gasUsed": hex(21000), "contractAddress": None, "logs": [LOG_ENTRY], "logsBloom": "0x" + "0" * 512, "from": TX_DICT["from"], "to": TX_DICT["to"], "effectiveGasPrice": 50000000000, - "type": 2, + "type": hex(2), } # Fee history dict (minimal)