Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions packages/testing/src/consensus_testing/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@
from typing import ClassVar, Literal

from lean_spec.config import LEAN_ENV
from lean_spec.forks.lstar.containers import (
AttestationData,
ValidatorIndices,
)
from lean_spec.forks.lstar.containers import AttestationData
from lean_spec.forks.lstar.containers.block.types import (
AggregatedAttestations,
AttestationSignatures,
Expand All @@ -64,7 +61,7 @@
HashTreeOpening,
Randomness,
)
from lean_spec.types import Bytes32, Slot, Uint64, ValidatorIndex
from lean_spec.types import Bytes32, Slot, Uint64, ValidatorIndex, ValidatorIndices

SecretField = Literal["attestation_secret", "proposal_secret"]
"""Discriminator for which secret key to load from a validator key pair."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
from lean_spec.forks.lstar.containers.block.block import Block, BlockBody
from lean_spec.forks.lstar.containers.block.types import AggregatedAttestations
from lean_spec.forks.lstar.containers.state import State
from lean_spec.forks.lstar.containers.validator import ValidatorIndices
from lean_spec.subspecs.ssz.hash import hash_tree_root
from lean_spec.subspecs.xmss.aggregation import AggregatedSignatureProof
from lean_spec.types import Bytes32
from lean_spec.types import Bytes32, ValidatorIndices

from ..keys import XmssKeyManager
from ..test_types import AggregatedAttestationSpec, BlockSpec, StateExpectation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from pydantic import Field

from lean_spec.forks.lstar.containers.attestation import AggregatedAttestation
from lean_spec.forks.lstar.containers.attestation.aggregation_bits import AggregationBits
from lean_spec.forks.lstar.containers.block import (
SignedBlock,
)
Expand All @@ -16,7 +15,7 @@
AttestationSignatures,
)
from lean_spec.forks.lstar.containers.state import State
from lean_spec.types import Boolean, ValidatorIndex
from lean_spec.types import AggregationBits, Boolean, ValidatorIndex

from ..keys import XmssKeyManager
from ..test_types import BlockSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
from lean_spec.forks.lstar.containers.block.block import Block
from lean_spec.forks.lstar.containers.block.types import AggregatedAttestations
from lean_spec.forks.lstar.containers.state import State
from lean_spec.forks.lstar.containers.validator import ValidatorIndices
from lean_spec.subspecs.xmss.aggregation import AggregatedSignatureProof
from lean_spec.types import ByteListMiB, Bytes32, CamelModel, Checkpoint, Slot, ValidatorIndex
from lean_spec.types import (
ByteListMiB,
Bytes32,
CamelModel,
Checkpoint,
Slot,
ValidatorIndex,
ValidatorIndices,
)

from ..keys import XmssKeyManager
from .utils import resolve_checkpoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
AttestationSignatures,
)
from lean_spec.forks.lstar.containers.state import State
from lean_spec.forks.lstar.containers.validator import ValidatorIndices
from lean_spec.forks.lstar.store import Store
from lean_spec.subspecs.chain.clock import Interval
from lean_spec.subspecs.ssz.hash import hash_tree_root
from lean_spec.subspecs.xmss.aggregation import AggregatedSignatureProof
from lean_spec.subspecs.xmss.containers import Signature
from lean_spec.types import Bytes32, CamelModel, Slot, ValidatorIndex
from lean_spec.types import Bytes32, CamelModel, Slot, ValidatorIndex, ValidatorIndices

from ..keys import LEAN_ENV_TO_SCHEMES, XmssKeyManager, create_dummy_signature
from .aggregated_attestation_spec import AggregatedAttestationSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
from lean_spec.forks.lstar.containers.attestation.attestation import SignedAggregatedAttestation
from lean_spec.forks.lstar.containers.block.block import Block
from lean_spec.forks.lstar.containers.state import State
from lean_spec.forks.lstar.containers.validator import ValidatorIndices
from lean_spec.subspecs.xmss.aggregation import AggregatedSignatureProof
from lean_spec.types import ByteListMiB, Bytes32, CamelModel, Checkpoint, Slot, ValidatorIndex
from lean_spec.types import (
ByteListMiB,
Bytes32,
CamelModel,
Checkpoint,
Slot,
ValidatorIndex,
ValidatorIndices,
)

from ..keys import XmssKeyManager
from .utils import resolve_checkpoint
Expand Down
33 changes: 32 additions & 1 deletion src/lean_spec/forks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
"""Multi-fork dispatch layer for leanSpec consensus specification."""

from .lstar.containers.state import State
from .lstar.containers import (
AggregatedAttestation,
Attestation,
AttestationData,
Block,
BlockBody,
BlockHeader,
Config,
SignedAggregatedAttestation,
SignedAttestation,
SignedBlock,
Validator,
)
from .lstar.containers.block import BlockLookup, BlockSignatures
from .lstar.containers.block.types import AggregatedAttestations, AttestationSignatures
from .lstar.containers.state import State, Validators
from .lstar.spec import LstarSpec
from .lstar.store import AttestationSignatureEntry, Store
from .protocol import ForkProtocol, SpecStateType, SpecStoreType
Expand All @@ -13,14 +28,30 @@
"""Shared registry over the registered forks. Convenient for top-level callers."""

__all__ = [
"AggregatedAttestation",
"AggregatedAttestations",
"Attestation",
"AttestationData",
"AttestationSignatureEntry",
"AttestationSignatures",
"Block",
"BlockBody",
"BlockHeader",
"BlockLookup",
"BlockSignatures",
"Config",
"DEFAULT_REGISTRY",
"FORK_SEQUENCE",
"ForkProtocol",
"ForkRegistry",
"LstarSpec",
"SignedAggregatedAttestation",
"SignedAttestation",
"SignedBlock",
"SpecStateType",
"SpecStoreType",
"State",
"Store",
"Validator",
"Validators",
]
5 changes: 1 addition & 4 deletions src/lean_spec/forks/lstar/containers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from .attestation import (
AggregatedAttestation,
AggregationBits,
Attestation,
AttestationData,
SignedAggregatedAttestation,
Expand All @@ -23,11 +22,10 @@
SignedBlock,
)
from .config import Config
from .validator import Validator, ValidatorIndices
from .validator import Validator

__all__ = [
"AggregatedAttestation",
"AggregationBits",
"Attestation",
"AttestationData",
"Block",
Expand All @@ -38,5 +36,4 @@
"SignedAttestation",
"SignedBlock",
"Validator",
"ValidatorIndices",
]
2 changes: 0 additions & 2 deletions src/lean_spec/forks/lstar/containers/attestation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Attestation containers and related types for the Lean spec."""

from .aggregation_bits import AggregationBits
from .attestation import (
AggregatedAttestation,
Attestation,
Expand All @@ -11,7 +10,6 @@

__all__ = [
"AggregatedAttestation",
"AggregationBits",
"Attestation",
"AttestationData",
"SignedAggregatedAttestation",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

from lean_spec.subspecs.xmss.aggregation import AggregatedSignatureProof
from lean_spec.subspecs.xmss.containers import Signature
from lean_spec.types import Checkpoint, Container, Slot, ValidatorIndex

from .aggregation_bits import AggregationBits
from lean_spec.types import AggregationBits, Checkpoint, Container, Slot, ValidatorIndex


class AttestationData(Container):
Expand Down
42 changes: 1 addition & 41 deletions src/lean_spec/forks/lstar/containers/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,9 @@

from __future__ import annotations

import lean_spec.forks.lstar.containers.attestation.aggregation_bits as _aggregation_bits
from lean_spec.subspecs.chain.config import VALIDATOR_REGISTRY_LIMIT
from lean_spec.subspecs.xmss.containers import PublicKey
from lean_spec.types import Boolean, Bytes52, Container, SSZList, ValidatorIndex


class ValidatorIndices(SSZList[ValidatorIndex]):
"""List of validator indices up to registry limit."""

LIMIT = int(VALIDATOR_REGISTRY_LIMIT)

def to_aggregation_bits(self) -> _aggregation_bits.AggregationBits:
"""
Convert to aggregation bits marking which validators are present.

Returns:
AggregationBits with the corresponding indices set to True.

Raises:
AssertionError: If no indices are provided.
AssertionError: If any index is outside the supported LIMIT.
"""
index_list = self.data

# Require at least one validator for a valid aggregation.
if not index_list:
raise AssertionError("Aggregated attestation must reference at least one validator")

# Convert to a set of native ints.
#
# This combines int conversion and deduplication in a single O(N) pass.
ids = {int(i) for i in index_list}

# Validate bounds: max index must be within registry limit.
if (max_id := max(ids)) >= _aggregation_bits.AggregationBits.LIMIT:
raise AssertionError("Validator index out of range for aggregation bits")

# Build bit list:
# - True at positions present in indices,
# - False elsewhere.
return _aggregation_bits.AggregationBits(
data=[Boolean(i in ids) for i in range(max_id + 1)]
)
from lean_spec.types import Bytes52, Container, SSZList, ValidatorIndex


class Validator(Container):
Expand Down
31 changes: 29 additions & 2 deletions src/lean_spec/forks/lstar/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

from typing import ClassVar

from lean_spec.forks.lstar.containers.block import Block
from lean_spec.forks.lstar.containers import (
AggregatedAttestation,
Attestation,
AttestationData,
Block,
Config,
SignedAggregatedAttestation,
SignedAttestation,
SignedBlock,
Validator,
)
from lean_spec.forks.lstar.containers.block.block import BlockSignatures
from lean_spec.forks.lstar.containers.state import State
from lean_spec.forks.lstar.containers.validator import Validators

from ..protocol import ForkProtocol, SpecStateType
from .containers.state import State
from .store import Store


Expand All @@ -20,8 +32,23 @@ class LstarSpec(ForkProtocol):

state_class: type[State] = State
block_class: type[Block] = Block
signed_block_class: type[SignedBlock] = SignedBlock
block_signatures_class: type[BlockSignatures] = BlockSignatures
store_class: type[Store] = Store

attestation_data_class: type[AttestationData] = AttestationData
attestation_class: type[Attestation] = Attestation
signed_attestation_class: type[SignedAttestation] = SignedAttestation
aggregated_attestation_class: type[AggregatedAttestation] = AggregatedAttestation
signed_aggregated_attestation_class: type[SignedAggregatedAttestation] = (
SignedAggregatedAttestation
)

validator_class: type[Validator] = Validator
validators_class: type[Validators] = Validators

config_class: type[Config] = Config

def upgrade_state(self, state: SpecStateType) -> State:
"""
Lstar is the root fork: there is no predecessor, so no migration.
Expand Down
2 changes: 1 addition & 1 deletion src/lean_spec/forks/lstar/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
)
from lean_spec.forks.lstar.containers.attestation.attestation import SignedAggregatedAttestation
from lean_spec.forks.lstar.containers.block import BlockLookup
from lean_spec.forks.lstar.containers.validator import ValidatorIndices
from lean_spec.subspecs.chain.clock import Interval
from lean_spec.subspecs.chain.config import (
GOSSIP_DISPARITY_INTERVALS,
Expand All @@ -42,6 +41,7 @@
Uint8,
Uint64,
ValidatorIndex,
ValidatorIndices,
)
from lean_spec.types.base import StrictBaseModel

Expand Down
Loading
Loading