Skip to content

Commit 1177800

Browse files
tcoratgerclaude
andauthored
refactor: move generate_pre_state to consensus_testing package root (#550)
The function was misplaced in test_types/, which is otherwise exclusively Pydantic model definitions. Move it to consensus_testing/genesis.py, alongside keys.py which it directly depends on. Also improves the function: - Replace assert with ValueError for input validation - Clean up docstring (remove default value restating) - Rename validator_list to validators Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 142a359 commit 1177800

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

packages/testing/src/consensus_testing/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Type
44

55
from . import forks
6+
from .genesis import generate_pre_state
67
from .test_fixtures import (
78
BaseConsensusFixture,
89
ForkChoiceTest,
@@ -25,7 +26,6 @@
2526
StateExpectation,
2627
StoreChecks,
2728
TickStep,
28-
generate_pre_state,
2929
)
3030

3131
StateTransitionTestFiller = Type[StateTransitionTest]

packages/testing/src/consensus_testing/test_types/genesis.py renamed to packages/testing/src/consensus_testing/genesis.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from lean_spec.subspecs.containers.validator import Validator, ValidatorIndex
55
from lean_spec.types import Bytes52, Uint64
66

7-
from ..keys import XmssKeyManager
7+
from .keys import XmssKeyManager
88

99
_DEFAULT_GENESIS_TIME = Uint64(0)
1010

@@ -13,37 +13,33 @@ def generate_pre_state(
1313
genesis_time: Uint64 = _DEFAULT_GENESIS_TIME,
1414
num_validators: int = 4,
1515
) -> State:
16-
"""
17-
Generate a default pre-state for consensus tests.
16+
"""Generate a default pre-state for consensus tests.
1817
1918
Args:
20-
genesis_time: The genesis timestamp (defaults to Uint64(0)).
21-
num_validators: Number of validators (defaults to 4).
19+
genesis_time: The genesis timestamp.
20+
num_validators: Number of validators to include.
2221
2322
Returns:
2423
A properly initialized consensus state.
2524
"""
2625
key_manager = XmssKeyManager.shared()
27-
available_keys = len(key_manager)
2826

29-
assert num_validators <= available_keys, (
30-
f"Not enough keys to generate state. "
31-
f"Expecting a minimum of {num_validators} validators "
32-
f"but the key manager has only {available_keys} keys"
33-
)
27+
if num_validators > len(key_manager):
28+
raise ValueError(
29+
f"Not enough keys: need {num_validators} validators "
30+
f"but the key manager has only {len(key_manager)} keys"
31+
)
3432

35-
validator_list = []
33+
validators = []
3634
for i in range(num_validators):
3735
idx = ValidatorIndex(i)
3836
attestation_pubkey, proposal_pubkey = key_manager.get_public_keys(idx)
39-
validator_list.append(
37+
validators.append(
4038
Validator(
4139
attestation_pubkey=Bytes52(attestation_pubkey.encode_bytes()),
4240
proposal_pubkey=Bytes52(proposal_pubkey.encode_bytes()),
4341
index=idx,
4442
)
4543
)
4644

47-
return State.generate_genesis(
48-
genesis_time=genesis_time, validators=Validators(data=validator_list)
49-
)
45+
return State.generate_genesis(genesis_time=genesis_time, validators=Validators(data=validators))

packages/testing/src/consensus_testing/test_types/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from .aggregated_attestation_spec import AggregatedAttestationSpec
44
from .block_spec import BlockSpec
5-
from .genesis import generate_pre_state
65
from .gossip_aggregated_attestation_spec import GossipAggregatedAttestationSpec
76
from .gossip_attestation_spec import GossipAttestationSpec
87
from .state_expectation import StateExpectation
@@ -31,5 +30,4 @@
3130
"AttestationStep",
3231
"ForkChoiceStep",
3332
"GossipAggregatedAttestationStep",
34-
"generate_pre_state",
3533
]

0 commit comments

Comments
 (0)