Skip to content

Commit 6bfdbfc

Browse files
authored
test: simplify validators initialization (#219)
* test: simplify validators init * fix: linting * fix: linting
1 parent 1db60d4 commit 6bfdbfc

3 files changed

Lines changed: 34 additions & 32 deletions

File tree

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
"""Consensus layer pre-state generation."""
22

3+
from functools import lru_cache
34
from typing import Any
45

6+
from lean_spec.subspecs.containers.slot import Slot
57
from lean_spec.subspecs.containers.state import State, Validators
68
from lean_spec.subspecs.containers.validator import Validator
7-
from lean_spec.types import Bytes52, Uint64
9+
from lean_spec.types import Uint64
10+
11+
from ..keys import XmssKeyManager
12+
13+
14+
@lru_cache(maxsize=1)
15+
def _get_shared_key_manager() -> XmssKeyManager:
16+
"""
17+
Get or create the shared XMSS key manager for reusing keys across tests.
18+
19+
Uses functools.lru_cache to create a singleton instance that's shared
20+
across all test fixture generations within a session. This optimizes
21+
performance by reusing keys when possible.
22+
23+
Returns:
24+
Shared XmssKeyManager instance with max_slot=10.
25+
"""
26+
return XmssKeyManager(max_slot=Slot(10))
827

928

1029
def generate_pre_state(**kwargs: Any) -> State:
@@ -14,20 +33,20 @@ def generate_pre_state(**kwargs: Any) -> State:
1433
Args:
1534
**kwargs: Optional keyword arguments:
1635
- genesis_time: The genesis timestamp (defaults to Uint64(0)).
17-
- validators: Validators list (defaults to 4 validators with dummy pubkeys).
36+
- num_validators: Number of validators (defaults to 4 validators).
1837
1938
Returns:
2039
State: A properly initialized consensus state.
2140
"""
2241
genesis_time = kwargs.get("genesis_time", Uint64(0))
23-
24-
# If validators not provided, create a default set of 4 validators with dummy pubkeys
25-
# TODO: Set an appropriate default here for test fixtures
26-
if "validators" not in kwargs:
27-
validators = Validators(
28-
data=[Validator(pubkey=Bytes52.zero(), index=Uint64(i)) for i in range(4)]
29-
)
30-
else:
31-
validators = kwargs["validators"]
42+
num_validators = kwargs.get("num_validators", 4)
43+
key_manager = _get_shared_key_manager()
44+
45+
validators = Validators(
46+
data=[
47+
Validator(pubkey=key_manager[Uint64(i)].public.encode_bytes(), index=Uint64(i))
48+
for i in range(num_validators)
49+
]
50+
)
3251

3352
return State.generate_genesis(genesis_time=genesis_time, validators=validators)

tests/consensus/devnet/fc/test_fork_choice_reorgs.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,7 @@ def test_reorg_with_slot_gaps(
361361
conditions where perfect block production is impossible.
362362
"""
363363
fork_choice_test(
364-
anchor_state=generate_pre_state(
365-
validators=Validators(
366-
data=[Validator(pubkey=Bytes52.zero(), index=Uint64(i)) for i in range(10)]
367-
),
368-
),
364+
anchor_state=generate_pre_state(num_validators=10),
369365
steps=[
370366
# Base at slot 1
371367
BlockStep(
@@ -577,11 +573,7 @@ def test_reorg_prevention_heavy_fork_resists_light_competition(
577573
- Network naturally converges on heaviest fork
578574
"""
579575
fork_choice_test(
580-
anchor_state=generate_pre_state(
581-
validators=Validators(
582-
data=[Validator(pubkey=Bytes52.zero(), index=Uint64(i)) for i in range(12)]
583-
)
584-
),
576+
anchor_state=generate_pre_state(num_validators=12),
585577
steps=[
586578
# Common base
587579
BlockStep(
@@ -814,11 +806,7 @@ def test_reorg_on_newly_justified_slot(
814806
"""
815807
fork_choice_test(
816808
# Using 9 validators: 3 for Fork A and 6 for Fork B to achieve 2/3rd for Fork B
817-
anchor_state=generate_pre_state(
818-
validators=Validators(
819-
data=[Validator(pubkey=Bytes52.zero(), index=Uint64(i)) for i in range(9)]
820-
)
821-
),
809+
anchor_state=generate_pre_state(num_validators=9),
822810
steps=[
823811
# Common base at slot 1
824812
BlockStep(

tests/consensus/devnet/state_transition/test_genesis.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,8 @@ def test_genesis_custom_validator_set(
127127
Genesis state should contain exactly 8 validators while
128128
maintaining all other genesis properties.
129129
"""
130-
# Create 8 validators with unique pubkeys
131-
validators = Validators(
132-
data=[Validator(pubkey=Bytes52(bytes([i] * 52)), index=Uint64(i)) for i in range(8)]
133-
)
134-
135130
state_transition_test(
136-
pre=generate_pre_state(validators=validators),
131+
pre=generate_pre_state(num_validators=8),
137132
blocks=[],
138133
post=StateExpectation(
139134
slot=Slot(0),

0 commit comments

Comments
 (0)