import csv import importlib.util import json import os import re import shutil import subprocess import threading from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[2] spec = importlib.util.spec_from_file_location("tierb", ROOT / "scripts" / "d1_tierb_funnel.py") tierb = importlib.util.module_from_spec(spec) spec.loader.exec_module(tierb) def test_selection_real_data_and_rejects_other_sources(): with (ROOT / "data" / "candidates.csv").open(newline="") as f: rows = list(csv.DictReader(f)) selected = tierb.select_candidates(rows) assert len(selected) == 253 assert all(row["verified_source"] in tierb.VERIFIED for row in selected) assert tierb.select_candidates([{"verified_source": "sourcify:partial_match"}, {"verified_source": "blockscout:verified"}]) == [{"verified_source": "blockscout:verified"}] def test_reason_classification(): assert tierb.classify_reason("missing trie node") == "rpc_state_unavailable" assert tierb.classify_reason("execution reverted") == "revert" assert tierb.classify_reason("HTTP 429") == "rpc_error" def test_abi_view_requires_exact_signature_and_mutability(): view = {"type": "function", "name": "ownerOf", "stateMutability": "view", "inputs": [{"type": "uint256"}], "outputs": [{"type": "address"}]} assert tierb.abi_view([view], "ownerOf", ["uint256"], ["address"]) == view assert tierb.abi_view([view], "ownerOf", ["uint256"], ["bool"]) is None assert tierb.abi_view([{**view, "stateMutability": "nonpayable"}], "ownerOf", ["uint256"], ["address"]) is None assert tierb.abi_view([view, view], "ownerOf", ["uint256"], ["address"]) is None def test_summary_cumulative_counts_by_chain_and_standard(): rows = [ {"chain": "base", "standards_claimed": "721;4907", "last_rung_passed": "read", "fail_reason": "revert"}, {"chain": "base", "standards_claimed": "721", "last_rung_passed": "flip", "fail_reason": ""}, {"chain": "bsc", "standards_claimed": "1155", "last_rung_passed": "", "fail_reason": "abi_unavailable"}, ] summary = tierb.summarize(rows) assert summary["overall"]["passed"] == {"abi": 2, "fork": 2, "token": 2, "read": 2, "write": 1, "flip": 1} assert summary["by_chain"]["bsc"]["total"] == 1 assert summary["by_standard"]["4907"]["passed"]["write"] == 0 assert summary["overall"]["fail_reasons"] == {"abi_unavailable": 1, "revert": 1} with pytest.raises(ValueError): tierb.summarize([{**rows[0], "last_rung_passed": "unknown"}]) def test_abi_encoding_and_decoding(): # ownerOf(uint256) data = tierb.encode_abi_call("ownerOf(uint256)", ["uint256"], [42]) assert data.startswith("0x6352211e") assert len(data) == 10 + 64 assert int(data[10:], 16) == 42 # decode address, uint256, bool addr = "0x" + "1" * 40 addr_padded = "0x" + "0" * 24 + "1" * 40 decoded = tierb.decode_abi_output(["address"], addr_padded) assert decoded == [addr.lower()] decoded_multi = tierb.decode_abi_output(["address", "uint256", "bool"], addr_padded + "0" * 63 + "1" + "0" * 63 + "1") assert decoded_multi == [addr.lower(), 1, True] # Too short output raises ValueError with pytest.raises(ValueError): tierb.decode_abi_output(["address"], "0x1234") def test_parse_transfer_logs(): # ERC-721 Transfer log t721 = { "topics": [ tierb.TRANSFER_721, "0x" + "0" * 24 + "a" * 40, "0x" + "0" * 24 + "b" * 40, "0x" + hex(101)[2:].rjust(64, "0"), ], "data": "0x", } parsed = tierb.parse_transfer_log(t721) assert parsed == (101, "0x" + "b" * 40) # Burn log (to zero address) returns None t_burn = { "topics": [ tierb.TRANSFER_721, "0x" + "0" * 24 + "a" * 40, "0x" + "0" * 64, "0x" + hex(101)[2:].rjust(64, "0"), ], "data": "0x", } assert tierb.parse_transfer_log(t_burn) is None # ERC-1155 TransferSingle log t1155 = { "topics": [ tierb.TRANSFER_SINGLE_1155, "0x" + "0" * 24 + "a" * 40, "0x" + "0" * 24 + "b" * 40, "0x" + "0" * 24 + "c" * 40, ], "data": "0x" + hex(202)[2:].rjust(64, "0") + hex(1)[2:].rjust(64, "0"), } parsed1155 = tierb.parse_transfer_log(t1155) assert parsed1155 == (202, "0x" + "c" * 40) # Non-transfer log returns None assert tierb.parse_transfer_log({"topics": ["0x12345678"]}) is None def test_fetch_abi_cache(tmp_path, monkeypatch): monkeypatch.setattr(tierb, "ABI_CACHE_DIR", tmp_path) chain = "ethereum" addr = "0x1234567890123456789012345678901234567890" mock_abi = [{"type": "function", "name": "foo", "inputs": [], "outputs": []}] chain_dir = tmp_path / chain chain_dir.mkdir(parents=True) (chain_dir / f"{addr.lower()}.json").write_text(json.dumps(mock_abi)) got = tierb.fetch_abi(chain, addr, "sourcify:exact_match", False, None) assert got == mock_abi def test_check_fork_code(): class DummyAnvil: def __init__(self, code=None, err=None): self._code = code self._err = err def rpc(self, method, *params): if self._err: raise RuntimeError(self._err) return self._code ok, why = tierb.check_fork_code(DummyAnvil(code="0x60806040"), "0x123") assert ok is True and why == "" ok, why = tierb.check_fork_code(DummyAnvil(code="0x"), "0x123") assert ok is False and why == "no_code" ok, why = tierb.check_fork_code(DummyAnvil(err="missing trie node 1234"), "0x123") assert ok is False and why == "rpc_state_unavailable" def test_rungs_4_to_6_local_anvil(): foundry_dir = Path(os.environ.get("FOUNDRY_BIN", Path.home() / ".foundry" / "bin")) anvil_bin = shutil.which("anvil") or str(foundry_dir / "anvil") proc = subprocess.Popen( [anvil_bin, "--port", "0", "--threads", "1", "--base-fee", "0", "--gas-price", "0"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, ) tierb.register_anvil(proc.pid) try: port = None for line in proc.stdout: m = re.search(r"Listening on 127\.0\.0\.1:(\d+)", line) if m: port = int(m.group(1)) break assert port is not None, "local anvil did not start" threading.Thread(target=proc.stdout.read, daemon=True).start() anvil = tierb.AnvilFork("local", 1, "http://127.0.0.1:8545") anvil.port = port anvil.proc = proc # Deploy CollectionV1 from contracts/out artifact_path = ROOT / "contracts" / "out" / "CollectionV1.sol" / "CollectionV1.json" artifact = json.loads(artifact_path.read_text()) bytecode = artifact["bytecode"]["object"] abi = artifact["abi"] deployer = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266" deploy_tx = anvil.rpc("eth_sendTransaction", {"from": deployer, "data": bytecode, "gas": "0x1000000"}) anvil.rpc("evm_mine") rec = anvil.rpc("eth_getTransactionReceipt", deploy_tx) contract = rec["contractAddress"] # Mint token 1 to Alice alice = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" anvil.rpc("anvil_setBalance", alice, hex(10**18)) mint_call = tierb.encode_abi_call("mint(address,uint256)", ["address", "uint256"], [alice, 1]) assert tierb.send_tx(anvil, deployer, contract, mint_call) # Test Rung 4: Read standards = ["721", "4907", "5192"] evidence = ["census_6551_tokenContract"] read_ok, read_ctx, read_why = tierb.run_read_views(anvil, contract, abi, standards, evidence, 1, alice) assert read_ok is True, f"Rung 4 failed: {read_why}" assert read_ctx["owner"] == alice.lower() assert read_ctx["is_locked"] is False assert read_ctx["userOf"] == "0x" + "0" * 40 # Test Rung 5: Write write_ok, write_why = tierb.run_write(anvil, contract, standards, 1, alice, read_ctx) assert write_ok is True, f"Rung 5 failed: {write_why}" # Verify state back call_u = tierb.encode_abi_call("userOf(uint256)", ["uint256"], [1]) res_u = anvil.rpc("eth_call", {"to": contract, "data": call_u}, "latest") assert tierb.decode_address(res_u) == "0x000000000000000000000000000000000000dead" # Test Rung 6: Flip flip_ok, flip_why = tierb.run_flip(anvil, contract, standards, 1, alice, read_ctx) assert flip_ok is True, f"Rung 6 failed: {flip_why}" finally: tierb.unregister_anvil(proc.pid) proc.terminate() try: proc.wait(timeout=5) except Exception: proc.kill() proc.wait(timeout=2) def test_resolve_token_and_holder_source_order(): class DummyAnvil: def rpc(self, method, *params): return None anvil = DummyAnvil() addr = "0x1234567890123456789012345678901234567890" holder_ok = "0x" + "a" * 40 abi_with_tbi = [{"type": "function", "name": "tokenByIndex", "stateMutability": "view", "inputs": [{"type": "uint256"}], "outputs": [{"type": "uint256"}]}] abi_no_tbi = [] # Confirmer: confirms any token > 0 def mock_confirmer(anv, a, is_1155, tid, cand_holder=""): if tid == 999: # fail token return False, "", "revert" return True, holder_ok, "" # Case A: Source 1 (census_6551) wins when census_6551_tokenContract in evidence tid, holder, src, why = tierb.resolve_token_and_holder( anvil, "ethereum", addr, ["721"], ["census_6551_tokenContract"], abi_with_tbi, 100, census_tokens_getter=lambda c, a: [(101, "0x111")], window_logs_getter=lambda c, a: [(102, "0x222")], token_by_index_caller=lambda anv, a, idx: 103, get_logs_getter=lambda anv, a, pin: [(105, "0x555")], holder_confirmer=mock_confirmer, ) assert (tid, holder, src, why) == (101, holder_ok, "census_6551", "") # Case B: Source 1 candidates fail or empty -> Source 2 (window_logs) wins tid, holder, src, why = tierb.resolve_token_and_holder( anvil, "ethereum", addr, ["721"], ["census_6551_tokenContract"], abi_with_tbi, 100, census_tokens_getter=lambda c, a: [(999, "0x111")], window_logs_getter=lambda c, a: [(102, "0x222")], token_by_index_caller=lambda anv, a, idx: 103, get_logs_getter=lambda anv, a, pin: [(105, "0x555")], holder_confirmer=mock_confirmer, ) assert (tid, holder, src, why) == (102, holder_ok, "window_logs", "") # Case C: Source 1 and 2 fail/empty, ABI has tokenByIndex -> Source 3 (token_by_index) wins tid, holder, src, why = tierb.resolve_token_and_holder( anvil, "ethereum", addr, ["721"], ["sampled_windows"], abi_with_tbi, 100, census_tokens_getter=lambda c, a: [(101, "0x111")], # not 6551 tokenContract row, skipped! window_logs_getter=lambda c, a: [], token_by_index_caller=lambda anv, a, idx: 103 if idx == 0 else None, get_logs_getter=lambda anv, a, pin: [(105, "0x555")], holder_confirmer=mock_confirmer, ) assert (tid, holder, src, why) == (103, holder_ok, "token_by_index", "") # Case D: Source 1, 2, 3 fail/absent -> Source 4 (owner_of 0..64) wins # Confirmer succeeds on tid == 5 def mock_confirmer_for_d(anv, a, is_1155, tid, cand_holder=""): if tid == 5: return True, holder_ok, "" return False, "", "revert" tid, holder, src, why = tierb.resolve_token_and_holder( anvil, "ethereum", addr, ["721"], ["sampled_windows"], abi_no_tbi, 100, census_tokens_getter=lambda c, a: [], window_logs_getter=lambda c, a: [], token_by_index_caller=lambda anv, a, idx: None, get_logs_getter=lambda anv, a, pin: [(105, "0x555")], holder_confirmer=mock_confirmer_for_d, ) assert (tid, holder, src, why) == (5, holder_ok, "owner_of", "") # Case E: Source 1..4 fail -> Source 5 (get_logs) wins def mock_confirmer_for_e(anv, a, is_1155, tid, cand_holder=""): if tid == 105: return True, holder_ok, "" return False, "", "revert" tid, holder, src, why = tierb.resolve_token_and_holder( anvil, "ethereum", addr, ["721"], ["sampled_windows"], abi_no_tbi, 100, census_tokens_getter=lambda c, a: [], window_logs_getter=lambda c, a: [], token_by_index_caller=lambda anv, a, idx: None, get_logs_getter=lambda anv, a, pin: [(105, "0x555")], holder_confirmer=mock_confirmer_for_e, ) assert (tid, holder, src, why) == (105, holder_ok, "get_logs", "") def test_resolve_token_and_holder_all_fail_reason_token_not_found(): class DummyAnvil: def rpc(self, method, *params): return None anvil = DummyAnvil() addr = "0x1234567890123456789012345678901234567890" def always_fail(anv, a, is_1155, tid, cand_holder=""): return False, "", "revert" tid, holder, src, why = tierb.resolve_token_and_holder( anvil, "ethereum", addr, ["721"], ["sampled_windows"], [], 100, census_tokens_getter=lambda c, a: [], window_logs_getter=lambda c, a: [], token_by_index_caller=lambda anv, a, idx: None, get_logs_getter=lambda anv, a, pin: [], holder_confirmer=always_fail, ) assert tid is None assert holder == "" assert src == "" assert why == "token_not_found" def test_resolve_token_and_holder_implementation_only(): class DummyAnvil: def rpc(self, method, *params): raise AssertionError("should not call anvil") anvil = DummyAnvil() addr = "0x1234567890123456789012345678901234567890" tid, holder, src, why = tierb.resolve_token_and_holder( anvil, "ethereum", addr, [], ["census_6551_implementation"], [], 100, census_tokens_getter=lambda c, a: (_ for _ in ()).throw(AssertionError("should not call getter")), holder_confirmer=lambda *a: (_ for _ in ()).throw(AssertionError("should not call confirmer")), ) assert tid is None assert why == "implementation_only" def test_resolve_token_and_holder_erc1155_behavior(): class DummyAnvil: def rpc(self, method, *params): return None anvil = DummyAnvil() addr = "0x1234567890123456789012345678901234567890" holder_ok = "0x" + "b" * 40 abi_with_tbi = [{"type": "function", "name": "tokenByIndex", "stateMutability": "view", "inputs": [{"type": "uint256"}], "outputs": [{"type": "uint256"}]}] def mock_confirmer(anv, a, is_1155, tid, cand_holder=""): assert is_1155 is True if cand_holder == holder_ok: return True, holder_ok, "" return False, "", "unconfirmed_holder" # Even with tokenByIndex in ABI and owner_of available, 1155 skips them and uses get_logs with holder tbi_called = [] tid, holder, src, why = tierb.resolve_token_and_holder( anvil, "ethereum", addr, ["1155"], ["sampled_windows"], abi_with_tbi, 100, census_tokens_getter=lambda c, a: [], window_logs_getter=lambda c, a: [], token_by_index_caller=lambda anv, a, idx: tbi_called.append(idx), get_logs_getter=lambda anv, a, pin: [(888, holder_ok)], holder_confirmer=mock_confirmer, ) assert len(tbi_called) == 0 # tokenByIndex was never called for 1155 assert (tid, holder, src, why) == (888, holder_ok, "get_logs", "") def test_parse_window_log(): # Locked event (ERC-5192) locked_log = { "topics": [tierb.LOCKED_5192], "data": "0x" + hex(2852)[2:].rjust(64, "0"), } assert tierb.parse_window_log(locked_log) == (2852, "") # Unlocked event (ERC-5192) unlocked_log = { "topics": [tierb.UNLOCKED_5192, "0x" + hex(123)[2:].rjust(64, "0")], "data": "0x", } assert tierb.parse_window_log(unlocked_log) == (123, "") # UpdateUser event (ERC-4907) update_user_log = { "topics": [tierb.UPDATE_USER_4907, "0x" + hex(456)[2:].rjust(64, "0")], "data": "0x", } assert tierb.parse_window_log(update_user_log) == (456, "") # Transfer event (ERC-721) t721 = { "topics": [ tierb.TRANSFER_721, "0x" + "0" * 24 + "a" * 40, "0x" + "0" * 24 + "b" * 40, "0x" + hex(789)[2:].rjust(64, "0"), ], "data": "0x", } assert tierb.parse_window_log(t721) == (789, "0x" + "b" * 40) # --- Tests for Review Fixes Items 1-12 --- def test_item1_rpc_failure_not_locked_write_pass(): class FailingAnvil: def rpc(self, method, *params): if method == "eth_sendTransaction": raise RuntimeError("missing trie node 1234") return None anvil = FailingAnvil() addr = "0x1111111111111111111111111111111111111111" holder = "0x2222222222222222222222222222222222222222" # 1. Locked write when RPC fails with missing trie node ok, why = tierb.run_write(anvil, addr, ["721", "5192"], 1, holder, {"is_locked": True}) assert ok is False assert why == "rpc_state_unavailable" # 2. Ordinary write when RPC fails with missing trie node reports rpc_state_unavailable, not revert ok_ord, why_ord = tierb.run_write(anvil, addr, ["721"], 1, holder, {}) assert ok_ord is False assert why_ord == "rpc_state_unavailable" def test_item2_token_discovery_retains_tooling_failures(): class DummyAnvil: def rpc(self, method, *params): return None anvil = DummyAnvil() addr = "0x1234567890123456789012345678901234567890" def failing_confirmer(anv, a, is_1155, tid, cand_holder=""): return False, "", "rpc_state_unavailable" tid, holder, src, why = tierb.resolve_token_and_holder( anvil, "ethereum", addr, ["721"], ["sampled_windows"], [], 100, census_tokens_getter=lambda c, a: [], window_logs_getter=lambda c, a: [(1, "0xabc")], token_by_index_caller=lambda anv, a, idx: None, get_logs_getter=lambda anv, a, pin: [], holder_confirmer=failing_confirmer, ) assert tid is None assert why == "rpc_state_unavailable" def test_item3_later_exceptions_preserve_earlier_passes(): class AnvilWithFlipException: def __init__(self): self.closed = False self.proc = 123 self.written = False def rpc(self, method, *params): if method == "eth_getCode": return "0x60806040" if method == "evm_snapshot": return "0x1" if method == "evm_revert": return True if method == "eth_sendTransaction": self.written = True return "0xtx" if method == "eth_getTransactionReceipt": return {"status": "0x1"} if method == "evm_mine": return None if method == "eth_call": if self.written: return "0x" + "0" * 60 + "dead" return "0x" + "0" * 24 + "a" * 40 if method in ("anvil_impersonateAccount", "anvil_setBalance"): if method == "anvil_impersonateAccount" and params[0] == "0x2222222222222222222222222222222222222222": raise RuntimeError("connection reset by peer") return None return None def close(self): self.closed = True anvil = AnvilWithFlipException() row = { "chain": "ethereum", "address": "0x" + "1" * 40, "pinned_block": "100", "standards_claimed": "721", "evidence_source": "sampled_windows", "verified_source": "sourcify:exact_match", "license": "MIT", "is_proxy": "False", } abi = [ {"type": "function", "name": "ownerOf", "stateMutability": "view", "inputs": [{"type": "uint256"}], "outputs": [{"type": "address"}]}, {"type": "function", "name": "transferFrom", "stateMutability": "nonpayable", "inputs": [{"type": "address"}, {"type": "address"}, {"type": "uint256"}], "outputs": []}, {"type": "function", "name": "approve", "stateMutability": "nonpayable", "inputs": [{"type": "address"}, {"type": "uint256"}], "outputs": []}, ] import httpx cache_path = tierb.ABI_CACHE_DIR / "ethereum" / f"{row['address'].lower()}.json" cache_path.parent.mkdir(parents=True, exist_ok=True) cache_path.write_text(json.dumps(abi)) client = httpx.Client() try: res = tierb.evaluate_contract(anvil, row, None, client) assert res["last_rung_passed"] == "write" assert res["fail_reason"] == "rpc_error" finally: client.close() def test_item4_resume_rejects_fingerprint_mismatch_and_ineligible_contracts(tmp_path, monkeypatch): monkeypatch.setattr(tierb, "OUT_CSV", tmp_path / "tier_b_funnel.csv") monkeypatch.setattr(tierb, "OUT_SUMMARY", tmp_path / "tier_b_funnel_summary.json") eligible_cohort = [ {"chain": "ethereum", "address": "0x" + "1" * 40, "pinned_block": "100", "standards_claimed": "721", "verified_source": "sourcify:exact_match"}, {"chain": "ethereum", "address": "0x" + "2" * 40, "pinned_block": "100", "standards_claimed": "721", "verified_source": "sourcify:exact_match"}, ] current_fp = tierb.compute_run_fingerprint(eligible_cohort) # 1. Reject on fingerprint mismatch tierb.OUT_CSV.write_text("chain,address,standards_claimed,license,is_proxy,last_rung_passed,fail_reason,token_id,holder,token_source\nethereum,0x" + "1" * 40 + ",721,MIT,False,flip,,1,0xaaa,census_6551\n") tierb.OUT_SUMMARY.write_text(json.dumps({"run_fingerprint": "stale_fingerprint_hash"})) with pytest.raises(RuntimeError, match="Run fingerprint mismatch"): tierb.load_existing_results(eligible_cohort, current_fp) # 2. Reject when CSV contains an ineligible contract not in eligible cohort tierb.OUT_SUMMARY.write_text(json.dumps({"run_fingerprint": current_fp})) ineligible_addr = "0x" + "9" * 40 tierb.OUT_CSV.write_text("chain,address,standards_claimed,license,is_proxy,last_rung_passed,fail_reason,token_id,holder,token_source\nethereum," + ineligible_addr + ",721,MIT,False,flip,,1,0xaaa,census_6551\n") with pytest.raises(RuntimeError, match="not in eligible cohort"): tierb.load_existing_results(eligible_cohort, current_fp) def test_item5_read_requires_view_and_infers_721(): class DummyAnvil: def rpc(self, method, *params): if method == "eth_call": return "0x" + "0" * 24 + "1" * 40 return None anvil = DummyAnvil() addr = "0x1111111111111111111111111111111111111111" holder = "0x2222222222222222222222222222222222222222" standards = [] evidence = ["sampled_windows"] # ABI missing ownerOf must fail with view_missing:ownerOf ok, ctx, why = tierb.run_read_views(anvil, addr, [], standards, evidence, 1, holder) assert ok is False assert why == "view_missing:ownerOf" # ABI with ownerOf must execute view and pass abi_721 = [{"type": "function", "name": "ownerOf", "stateMutability": "view", "inputs": [{"type": "uint256"}], "outputs": [{"type": "address"}]}] ok_good, ctx_good, why_good = tierb.run_read_views(anvil, addr, abi_721, standards, evidence, 1, holder) assert ok_good is True assert ctx_good["owner"] == "0x" + "1" * 40 def test_item6_canonical_abi_decoding_and_invalid_contract_return(): # 1. Non-canonical boolean word 2 must raise AbiDecodingError with pytest.raises(tierb.AbiDecodingError): tierb.decode_bool("0x" + "0" * 63 + "2") # 2. Non-zero address padding must raise AbiDecodingError dirty_padding = "0x" + "0" * 23 + "1" + "a" * 40 with pytest.raises(tierb.AbiDecodingError): tierb.decode_address(dirty_padding) with pytest.raises(tierb.AbiDecodingError): tierb.decode_abi_output(["address"], dirty_padding) # 3. In run_read_views, non-canonical return reports invalid_contract_return instead of rpc_error class BadAnvil: def rpc(self, method, *params): return "0x" + "0" * 63 + "2" abi_5192 = [{"type": "function", "name": "locked", "stateMutability": "view", "inputs": [{"type": "uint256"}], "outputs": [{"type": "bool"}]}] ok, _, why = tierb.run_read_views(BadAnvil(), "0x123", abi_5192, ["5192"], [], 1, "0xaaa") assert ok is False assert why == "invalid_contract_return" def test_item7_opposite_lock_state_requires_live_token(): class AnvilUnmintedOpposite: def rpc(self, method, *params): data = params[0].get("data", "") # locked(2) returns 0 (false) if data.startswith("0xb45a3c0e"): return "0x" + "0" * 64 # ownerOf(2) reverts (unminted token) if data.startswith("0x6352211e"): raise RuntimeError("ERC721: invalid token ID") return None anvil = AnvilUnmintedOpposite() addr = "0x" + "1" * 40 holder = "0x" + "2" * 40 read_context = {"is_locked": True} ok, why = tierb.run_flip(anvil, addr, ["5192"], 1, holder, read_context) assert ok is False assert why == "flip_failed" def test_item8_erc1155_flip_uses_1155_operations(): calls = [] class Anvil1155: def __init__(self): self.h_bal = 5 self.r_bal = 0 self.approved = False def rpc(self, method, *params): if method in ("anvil_impersonateAccount", "anvil_setBalance", "evm_mine"): return None if method == "eth_call": data = params[0].get("data", "") calls.append(("eth_call", data[:10])) if "00fdd58e" in data or "00fdd343" in data: addr = data[10 + 24:10 + 64].lower() if addr == "2" * 40: return "0x" + hex(self.h_bal)[2:].rjust(64, "0") elif addr == "3" * 40: return "0x" + hex(self.r_bal)[2:].rjust(64, "0") return "0x" + "0" * 64 if method == "eth_sendTransaction": tx = params[0] data = tx.get("data", "") from_addr = tx.get("from", "").lower() calls.append(("eth_sendTransaction", from_addr, data[:10])) # safeTransferFrom selector if "f242432a" in data: if not self.approved: raise RuntimeError("ERC1155: caller is not token owner or approved") self.h_bal -= 1 self.r_bal += 1 return "0xtx" # setApprovalForAll selector if "a22cb465" in data: self.approved = True return "0xtx" return "0xtx" if method == "eth_getTransactionReceipt": return {"status": "0x1"} return None anvil = Anvil1155() addr = "0x" + "1" * 40 holder = "0x" + "2" * 40 read_context = {} ok, why = tierb.run_flip(anvil, addr, ["1155"], 1, holder, read_context) assert ok is True assert why == "" for call in calls: assert "23b872dd" not in str(call) # transferFrom assert "095ea7b3" not in str(call) # approve assert "6352211e" not in str(call) # ownerOf def test_item9_erc1155_write_verifies_balance_delta(): class Anvil1155NoOpWrite: def rpc(self, method, *params): if method in ("anvil_impersonateAccount", "anvil_setBalance", "evm_mine"): return None if method == "eth_call": data = params[0].get("data", "") if "00fdd58e" in data or "00fdd343" in data: # balanceOf addr = data[10 + 24:10 + 64].lower() if "dead" in addr: return "0x" + hex(1)[2:].rjust(64, "0") return "0x" + hex(10)[2:].rjust(64, "0") if method == "eth_sendTransaction": return "0xtx" if method == "eth_getTransactionReceipt": return {"status": "0x1"} return None anvil = Anvil1155NoOpWrite() addr = "0x" + "1" * 40 holder = "0x" + "2" * 40 ok, why = tierb.run_write(anvil, addr, ["1155"], 1, holder, {}) assert ok is False assert why == "write_state_mismatch" def test_item10_failed_restoration_discards_and_restarts_fork(): class AnvilFailedRevert: def __init__(self): self.closed = False self.proc = 123 self.written = False def rpc(self, method, *params): if method == "eth_getCode": return "0x60806040" if method == "evm_snapshot": return "0x1" if method == "evm_revert": return False if method in ("anvil_impersonateAccount", "anvil_setBalance", "evm_mine"): return None if method == "eth_sendTransaction": self.written = True return "0xtx" if method == "eth_getTransactionReceipt": return {"status": "0x1"} if method == "eth_call": if self.written: return "0x" + "0" * 60 + "dead" return "0x" + "0" * 24 + "d" * 40 return None def close(self): self.closed = True self.proc = None anvil = AnvilFailedRevert() row = { "chain": "ethereum", "address": "0x" + "1" * 40, "pinned_block": "100", "standards_claimed": "721", "evidence_source": "sampled_windows", "verified_source": "sourcify:exact_match", "license": "MIT", "is_proxy": "False", } abi = [ {"type": "function", "name": "ownerOf", "stateMutability": "view", "inputs": [{"type": "uint256"}], "outputs": [{"type": "address"}]}, {"type": "function", "name": "transferFrom", "stateMutability": "nonpayable", "inputs": [{"type": "address"}, {"type": "address"}, {"type": "uint256"}], "outputs": []}, ] import httpx client = httpx.Client() try: res = tierb.evaluate_contract(anvil, row, None, client) assert res["last_rung_passed"] == "write" assert res["fail_reason"] == "rpc_state_unavailable" assert anvil.closed is True assert anvil.proc is None finally: client.close() def test_item11_proxy_fallback_does_not_poison_cache(tmp_path, monkeypatch): monkeypatch.setattr(tierb, "ABI_CACHE_DIR", tmp_path) chain = "ethereum" proxy_addr = "0x" + "1" * 40 impl_addr = "0x" + "9" * 40 proxy_shell_abi = [{"type": "function", "name": "upgradeTo", "inputs": [], "outputs": []}] import httpx class MockTransport(httpx.BaseTransport): def handle_request(self, request): url_str = str(request.url) if impl_addr.lower() in url_str.lower(): return httpx.Response(404) if proxy_addr.lower() in url_str.lower(): return httpx.Response(200, json={"metadata": {"output": {"abi": proxy_shell_abi}}}) return httpx.Response(404) client = httpx.Client(transport=MockTransport()) try: abi = tierb.fetch_abi(chain, proxy_addr, "sourcify:exact_match", is_proxy=True, implementation=impl_addr, client=client) assert abi is None impl_cache = tmp_path / chain / f"{impl_addr.lower()}.json" assert not impl_cache.exists() finally: client.close() def test_item12_sigterm_handler_terminates_and_reaps_children(): import sys proc = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"]) tierb.register_anvil(proc.pid) assert proc.pid in tierb.ACTIVE_ANVILS assert proc.poll() is None try: tierb.cleanup_all_anvils() assert proc.pid not in tierb.ACTIVE_ANVILS assert proc.poll() is not None finally: if proc.poll() is None: proc.kill()