11"""Consensus layer pre-state generation."""
22
3+ from functools import lru_cache
34from typing import Any
45
6+ from lean_spec .subspecs .containers .slot import Slot
57from lean_spec .subspecs .containers .state import State , Validators
68from 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
1029def 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 )
0 commit comments