Skip to content

Commit 558a3cf

Browse files
authored
tests: simplify forkchoice api tests (leanEthereum#440)
1 parent 919e805 commit 558a3cf

1 file changed

Lines changed: 25 additions & 89 deletions

File tree

Lines changed: 25 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
"""Tests for the fork choice endpoint."""
22

3+
from __future__ import annotations
4+
35
import httpx
46

7+
ZERO_ROOT = "0x" + "00" * 32
8+
59

610
def get_fork_choice(server_url: str) -> httpx.Response:
711
"""Fetch fork choice tree from the server."""
@@ -25,92 +29,24 @@ def test_content_type_is_json(self, server_url: str) -> None:
2529
content_type = response.headers.get("content-type", "")
2630
assert "application/json" in content_type
2731

28-
def test_has_nodes(self, server_url: str) -> None:
29-
"""Fork choice response has a nodes array."""
30-
data = get_fork_choice(server_url).json()
31-
32-
assert "nodes" in data
33-
assert isinstance(data["nodes"], list)
34-
assert len(data["nodes"]) > 0
35-
36-
def test_node_structure(self, server_url: str) -> None:
37-
"""Each node has root, slot, parent_root, proposer_index, and weight."""
38-
data = get_fork_choice(server_url).json()
39-
node = data["nodes"][0]
40-
41-
assert isinstance(node["root"], str)
42-
assert node["root"].startswith("0x")
43-
assert len(node["root"]) == 66
44-
45-
assert isinstance(node["slot"], int)
46-
assert node["slot"] >= 0
47-
48-
assert isinstance(node["parent_root"], str)
49-
assert node["parent_root"].startswith("0x")
50-
assert len(node["parent_root"]) == 66
51-
52-
assert isinstance(node["proposer_index"], int)
53-
assert node["proposer_index"] >= 0
54-
55-
assert isinstance(node["weight"], int)
56-
assert node["weight"] >= 0
57-
58-
def test_has_head(self, server_url: str) -> None:
59-
"""Fork choice response has a valid head root."""
60-
data = get_fork_choice(server_url).json()
61-
62-
assert "head" in data
63-
assert isinstance(data["head"], str)
64-
assert data["head"].startswith("0x")
65-
assert len(data["head"]) == 66
66-
67-
def test_has_justified_checkpoint(self, server_url: str) -> None:
68-
"""Fork choice response has a justified checkpoint with slot and root."""
69-
data = get_fork_choice(server_url).json()
70-
71-
assert "justified" in data
72-
justified = data["justified"]
73-
74-
assert isinstance(justified["slot"], int)
75-
assert justified["slot"] >= 0
76-
77-
assert isinstance(justified["root"], str)
78-
assert justified["root"].startswith("0x")
79-
assert len(justified["root"]) == 66
80-
81-
def test_has_finalized_checkpoint(self, server_url: str) -> None:
82-
"""Fork choice response has a finalized checkpoint with slot and root."""
83-
data = get_fork_choice(server_url).json()
84-
85-
assert "finalized" in data
86-
finalized = data["finalized"]
87-
88-
assert isinstance(finalized["slot"], int)
89-
assert finalized["slot"] >= 0
90-
91-
assert isinstance(finalized["root"], str)
92-
assert finalized["root"].startswith("0x")
93-
assert len(finalized["root"]) == 66
94-
95-
def test_has_safe_target(self, server_url: str) -> None:
96-
"""Fork choice response has a valid safe_target root."""
97-
data = get_fork_choice(server_url).json()
98-
99-
assert "safe_target" in data
100-
assert isinstance(data["safe_target"], str)
101-
assert data["safe_target"].startswith("0x")
102-
assert len(data["safe_target"]) == 66
103-
104-
def test_has_validator_count(self, server_url: str) -> None:
105-
"""Fork choice response has a non-negative validator count."""
106-
data = get_fork_choice(server_url).json()
107-
108-
assert "validator_count" in data
109-
assert isinstance(data["validator_count"], int)
110-
assert data["validator_count"] >= 0
111-
112-
def test_head_is_in_nodes(self, server_url: str) -> None:
113-
"""The head root appears in the nodes list."""
114-
data = get_fork_choice(server_url).json()
115-
node_roots = {node["root"] for node in data["nodes"]}
116-
assert data["head"] in node_roots
32+
def test_fork_choice_response(self, server_url: str) -> None:
33+
"""Fork choice response matches expected genesis-only tree structure."""
34+
data = get_fork_choice(server_url).json()
35+
genesis_root = data["head"]
36+
37+
assert data == {
38+
"nodes": [
39+
{
40+
"root": genesis_root,
41+
"slot": 0,
42+
"parent_root": ZERO_ROOT,
43+
"proposer_index": 0,
44+
"weight": 0,
45+
},
46+
],
47+
"head": genesis_root,
48+
"justified": {"slot": 0, "root": genesis_root},
49+
"finalized": {"slot": 0, "root": genesis_root},
50+
"safe_target": genesis_root,
51+
"validator_count": 3,
52+
}

0 commit comments

Comments
 (0)