Skip to content

Commit 1db60d4

Browse files
authored
validator: rm validator index alias (#217)
* validator: rm validator index * trigger ci
1 parent e62c4f0 commit 1db60d4

23 files changed

Lines changed: 217 additions & 234 deletions

File tree

packages/testing/src/consensus_testing/keys.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from lean_spec.subspecs.ssz.hash import hash_tree_root
3535
from lean_spec.subspecs.xmss.containers import PublicKey, SecretKey, Signature
3636
from lean_spec.subspecs.xmss.interface import TEST_SIGNATURE_SCHEME, GeneralizedXmssScheme
37-
from lean_spec.types import Uint64, ValidatorIndex
37+
from lean_spec.types import Uint64
3838

3939
if TYPE_CHECKING:
4040
from collections.abc import Mapping
@@ -87,7 +87,7 @@ def with_secret(self, secret: SecretKey) -> KeyPair:
8787

8888

8989
@cache
90-
def load_keys() -> dict[ValidatorIndex, KeyPair]:
90+
def load_keys() -> dict[Uint64, KeyPair]:
9191
"""
9292
Load pre-generated keys from disk (cached after first call).
9393
@@ -102,7 +102,7 @@ def load_keys() -> dict[ValidatorIndex, KeyPair]:
102102
f"Keys not found: {KEYS_FILE}\nRun: python -m consensus_testing.keys"
103103
)
104104
data = json.loads(KEYS_FILE.read_text())
105-
return {ValidatorIndex(i): KeyPair.from_dict(kp) for i, kp in enumerate(data)}
105+
return {Uint64(i): KeyPair.from_dict(kp) for i, kp in enumerate(data)}
106106

107107

108108
class XmssKeyManager:
@@ -119,8 +119,8 @@ class XmssKeyManager:
119119
120120
Examples:
121121
>>> mgr = XmssKeyManager()
122-
>>> mgr[ValidatorIndex(0)] # Get key pair
123-
>>> mgr.get_public_key(ValidatorIndex(1)) # Get public key only
122+
>>> mgr[Uint64(0)] # Get key pair
123+
>>> mgr.get_public_key(Uint64(1)) # Get public key only
124124
>>> mgr.sign_attestation(attestation) # Sign with auto-advancement
125125
"""
126126

@@ -132,38 +132,38 @@ def __init__(
132132
"""Initialize the manager with optional custom configuration."""
133133
self.max_slot = max_slot or DEFAULT_MAX_SLOT
134134
self.scheme = scheme
135-
self._state: dict[ValidatorIndex, KeyPair] = {}
135+
self._state: dict[Uint64, KeyPair] = {}
136136

137137
@property
138-
def keys(self) -> dict[ValidatorIndex, KeyPair]:
138+
def keys(self) -> dict[Uint64, KeyPair]:
139139
"""Lazy access to immutable base keys."""
140140
return load_keys()
141141

142-
def __getitem__(self, idx: ValidatorIndex) -> KeyPair:
142+
def __getitem__(self, idx: Uint64) -> KeyPair:
143143
"""Get key pair, returning advanced state if available."""
144144
if idx in self._state:
145145
return self._state[idx]
146146
if idx not in self.keys:
147147
raise KeyError(f"Validator {idx} not found (max: {len(self.keys) - 1})")
148148
return self.keys[idx]
149149

150-
def __contains__(self, idx: ValidatorIndex) -> bool:
150+
def __contains__(self, idx: Uint64) -> bool:
151151
"""Check if validator index exists."""
152152
return idx in self.keys
153153

154154
def __len__(self) -> int:
155155
"""Number of available validators."""
156156
return len(self.keys)
157157

158-
def __iter__(self) -> Iterator[ValidatorIndex]:
158+
def __iter__(self) -> Iterator[Uint64]:
159159
"""Iterate over validator indices."""
160160
return iter(self.keys)
161161

162-
def get_public_key(self, idx: ValidatorIndex) -> PublicKey:
162+
def get_public_key(self, idx: Uint64) -> PublicKey:
163163
"""Get a validator's public key."""
164164
return self[idx].public
165165

166-
def get_all_public_keys(self) -> dict[ValidatorIndex, PublicKey]:
166+
def get_all_public_keys(self) -> dict[Uint64, PublicKey]:
167167
"""Get all public keys (from base keys, not advanced state)."""
168168
return {idx: kp.public for idx, kp in self.keys.items()}
169169

packages/testing/src/consensus_testing/test_fixtures/fork_choice.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from lean_spec.subspecs.xmss.containers import Signature
3232
from lean_spec.subspecs.xmss.interface import TEST_SIGNATURE_SCHEME
3333
from lean_spec.subspecs.xmss.types import HashDigestList, HashTreeOpening, Randomness
34-
from lean_spec.types import Bytes32, Uint64, ValidatorIndex
34+
from lean_spec.types import Bytes32, Uint64
3535

3636
from ..keys import XmssKeyManager
3737
from ..test_types import (
@@ -194,9 +194,7 @@ def make_fixture(self) -> ForkChoiceTest:
194194

195195
# Update validator pubkeys to match key_manager's generated keys
196196
updated_validators = [
197-
validator.model_copy(
198-
update={"pubkey": key_manager[ValidatorIndex(i)].public.encode_bytes()}
199-
)
197+
validator.model_copy(update={"pubkey": key_manager[Uint64(i)].public.encode_bytes()})
200198
for i, validator in enumerate(self.anchor_state.validators)
201199
]
202200

@@ -318,7 +316,7 @@ def _build_block_from_spec(
318316
# Determine proposer
319317
if spec.proposer_index is None:
320318
validator_count = store.states[store.head].validators.count
321-
proposer_index = ValidatorIndex(int(spec.slot) % int(validator_count))
319+
proposer_index = Uint64(int(spec.slot) % int(validator_count))
322320
else:
323321
proposer_index = spec.proposer_index
324322

@@ -459,23 +457,16 @@ def _build_signed_attestation_from_spec(
459457
# Derive source from state's latest justified checkpoint
460458
source_checkpoint = state.latest_justified
461459

462-
# Convert validator_id to Uint64 if needed
463-
validator_id = (
464-
spec.validator_id
465-
if isinstance(spec.validator_id, Uint64)
466-
else Uint64(int(spec.validator_id))
467-
)
468-
469-
# Create attestation data
470-
attestation_data = AttestationData(
471-
slot=spec.slot,
472-
head=head_checkpoint,
473-
target=target_checkpoint,
474-
source=source_checkpoint,
475-
)
476-
477460
# Create attestation
478-
attestation = Attestation(validator_id=validator_id, data=attestation_data)
461+
attestation = Attestation(
462+
validator_id=spec.validator_id,
463+
data=AttestationData(
464+
slot=spec.slot,
465+
head=head_checkpoint,
466+
target=target_checkpoint,
467+
source=source_checkpoint,
468+
),
469+
)
479470

480471
# Create signed attestation
481472
return SignedAttestation(

packages/testing/src/consensus_testing/test_fixtures/state_transition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from lean_spec.subspecs.containers.block.types import Attestations
99
from lean_spec.subspecs.containers.state.state import State
1010
from lean_spec.subspecs.ssz.hash import hash_tree_root
11-
from lean_spec.types import Bytes32, ValidatorIndex
11+
from lean_spec.types import Bytes32, Uint64
1212

1313
from ..test_types import BlockSpec, StateExpectation
1414
from .base import BaseConsensusFixture
@@ -202,7 +202,7 @@ def _build_block_from_spec(self, spec: BlockSpec, state: State) -> tuple[Block,
202202
if spec.proposer_index is not None:
203203
proposer_index = spec.proposer_index
204204
else:
205-
proposer_index = ValidatorIndex(int(spec.slot) % int(state.validators.count))
205+
proposer_index = Uint64(int(spec.slot) % int(state.validators.count))
206206

207207
# Use provided parent_root or compute it
208208
if spec.parent_root is not None:

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from lean_spec.subspecs.containers.attestation import SignedAttestation
66
from lean_spec.subspecs.containers.block import BlockBody
77
from lean_spec.subspecs.containers.slot import Slot
8-
from lean_spec.types import Bytes32, CamelModel, ValidatorIndex
8+
from lean_spec.types import Bytes32, CamelModel, Uint64
99

1010
from .signed_attestation_spec import SignedAttestationSpec
1111

@@ -22,14 +22,14 @@ class BlockSpec(CamelModel):
2222
2323
Usage:
2424
- Simple: BlockSpec(slot=Slot(1)) - framework computes everything
25-
- Custom: BlockSpec(slot=Slot(1), proposer_index=ValidatorIndex(5)) - override specific fields
25+
- Custom: BlockSpec(slot=Slot(1), proposer_index=Uint64(5)) - override specific fields
2626
- Invalid: BlockSpec(slot=Slot(1), state_root=Bytes32.zero()) - test invalid blocks
2727
"""
2828

2929
slot: Slot
3030
"""The slot for this block (required)."""
3131

32-
proposer_index: ValidatorIndex | None = None
32+
proposer_index: Uint64 | None = None
3333
"""
3434
The proposer index for this block.
3535

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from lean_spec.subspecs.containers.state import State, Validators
66
from lean_spec.subspecs.containers.validator import Validator
7-
from lean_spec.types import Bytes52, Uint64, ValidatorIndex
7+
from lean_spec.types import Bytes52, Uint64
88

99

1010
def generate_pre_state(**kwargs: Any) -> State:
@@ -25,7 +25,7 @@ def generate_pre_state(**kwargs: Any) -> State:
2525
# TODO: Set an appropriate default here for test fixtures
2626
if "validators" not in kwargs:
2727
validators = Validators(
28-
data=[Validator(pubkey=Bytes52.zero(), index=ValidatorIndex(i)) for i in range(4)]
28+
data=[Validator(pubkey=Bytes52.zero(), index=Uint64(i)) for i in range(4)]
2929
)
3030
else:
3131
validators = kwargs["validators"]

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

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

33
from lean_spec.subspecs.containers.slot import Slot
44
from lean_spec.subspecs.xmss.containers import Signature
5-
from lean_spec.types import CamelModel, Uint64, ValidatorIndex
5+
from lean_spec.types import CamelModel, Uint64
66

77

88
class SignedAttestationSpec(CamelModel):
@@ -13,7 +13,7 @@ class SignedAttestationSpec(CamelModel):
1313
Head and source are automatically derived from target.
1414
"""
1515

16-
validator_id: ValidatorIndex | Uint64
16+
validator_id: Uint64
1717
"""The index of the validator making the attestation (required)."""
1818

1919
slot: Slot

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import TYPE_CHECKING, Literal
44

55
from lean_spec.subspecs.containers.slot import Slot
6-
from lean_spec.types import Bytes32, CamelModel, Uint64, ValidatorIndex
6+
from lean_spec.types import Bytes32, CamelModel, Uint64
77

88
if TYPE_CHECKING:
99
from lean_spec.subspecs.containers import SignedAttestation
@@ -19,7 +19,7 @@ class AttestationCheck(CamelModel):
1919
Used to validate attestation content beyond just counting.
2020
"""
2121

22-
validator: ValidatorIndex
22+
validator: Uint64
2323
"""Which validator's attestation to check."""
2424

2525
attestation_slot: Slot | None = None

src/lean_spec/subspecs/containers/state/state.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
Bytes32,
1414
Container,
1515
Uint64,
16-
ValidatorIndex,
1716
is_proposer,
1817
)
1918

@@ -94,7 +93,7 @@ def generate_genesis(cls, genesis_time: Uint64, validators: Validators) -> "Stat
9493
# Build the genesis block header for the state.
9594
genesis_header = BlockHeader(
9695
slot=Slot(0),
97-
proposer_index=ValidatorIndex(0),
96+
proposer_index=Uint64(0),
9897
parent_root=Bytes32.zero(),
9998
state_root=Bytes32.zero(),
10099
body_root=hash_tree_root(BlockBody(attestations=Attestations(data=[]))),

src/lean_spec/subspecs/containers/validator.py

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

33
from __future__ import annotations
44

5-
from lean_spec.types import Bytes52, Container, ValidatorIndex
5+
from lean_spec.types import Bytes52, Container, Uint64
66

77
from ..xmss.containers import PublicKey
88
from ..xmss.interface import TEST_SIGNATURE_SCHEME, GeneralizedXmssScheme
@@ -15,7 +15,7 @@ class Validator(Container):
1515
pubkey: Bytes52
1616
"""XMSS one-time signature public key."""
1717

18-
index: ValidatorIndex = ValidatorIndex(0)
18+
index: Uint64 = Uint64(0)
1919
"""Validator index in the registry."""
2020

2121
def get_pubkey(self, scheme: GeneralizedXmssScheme = TEST_SIGNATURE_SCHEME) -> PublicKey:

src/lean_spec/subspecs/forkchoice/store.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
ZERO_HASH,
4141
Bytes32,
4242
Uint64,
43-
ValidatorIndex,
4443
is_proposer,
4544
)
4645
from lean_spec.types.container import Container
@@ -122,15 +121,15 @@ class Store(Container):
122121
`Store`'s latest justified and latest finalized checkpoints.
123122
"""
124123

125-
latest_known_attestations: Dict[ValidatorIndex, SignedAttestation] = {}
124+
latest_known_attestations: Dict[Uint64, SignedAttestation] = {}
126125
"""
127126
Latest signed attestations by validator that have been processed.
128127
129128
- These attestations are "known" and contribute to fork choice weights.
130129
- Keyed by validator index to enforce one attestation per validator.
131130
"""
132131

133-
latest_new_attestations: Dict[ValidatorIndex, SignedAttestation] = {}
132+
latest_new_attestations: Dict[Uint64, SignedAttestation] = {}
134133
"""
135134
Latest signed attestations by validator that are pending processing.
136135
@@ -297,7 +296,7 @@ def on_attestation(
297296
self.validate_attestation(signed_attestation)
298297

299298
# Extract the validator index that produced this attestation.
300-
validator_id = ValidatorIndex(signed_attestation.message.validator_id)
299+
validator_id = Uint64(signed_attestation.message.validator_id)
301300

302301
# Extract the attestation's slot:
303302
# - used to decide if this attestation is "newer" than a previous one.
@@ -504,7 +503,7 @@ def on_block(self, signed_block_with_attestation: SignedBlockWithAttestation) ->
504503
def _compute_lmd_ghost_head(
505504
self,
506505
start_root: Bytes32,
507-
attestations: Dict[ValidatorIndex, SignedAttestation],
506+
attestations: Dict[Uint64, SignedAttestation],
508507
min_score: int = 0,
509508
) -> Bytes32:
510509
"""
@@ -915,7 +914,7 @@ def produce_attestation_data(self, slot: Slot) -> AttestationData:
915914
def produce_block_with_signatures(
916915
self,
917916
slot: Slot,
918-
validator_index: ValidatorIndex,
917+
validator_index: Uint64,
919918
) -> tuple["Store", Block, list[Signature]]:
920919
"""
921920
Produce a block and attestation signatures for the target slot.

0 commit comments

Comments
 (0)