Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
Use SSZ Vector for HistoricalBatch` fields
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed Apr 28, 2019
1 parent af397e2 commit c51e86a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,6 @@ def _update_historical_roots(state: BeaconState,
historical_batch = HistoricalBatch(
block_roots=state.latest_block_roots,
state_roots=state.latest_state_roots,
slots_per_historical_root=config.SLOTS_PER_HISTORICAL_ROOT,
)
updated_historical_roots += (historical_batch.root,)

Expand Down
12 changes: 4 additions & 8 deletions eth2/beacon/types/historical_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,23 @@
import ssz
from ssz.sedes import (
bytes32,
List,
Vector,
)


class HistoricalBatch(ssz.Serializable):

fields = [
# Block roots
('block_roots', List(bytes32)),
('block_roots', Vector(bytes32, 1)),
# State roots
('state_roots', List(bytes32)),
('state_roots', Vector(bytes32, 1)),
]

def __init__(self,
*,
block_roots: Sequence[Hash32],
state_roots: Sequence[Hash32],
slots_per_historical_root: int) -> None:
assert len(block_roots) == slots_per_historical_root
assert len(state_roots) == slots_per_historical_root

state_roots: Sequence[Hash32]) -> None:
super().__init__(
block_roots=block_roots,
state_roots=state_roots,
Expand Down
12 changes: 10 additions & 2 deletions tests/eth2/beacon/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from eth2.beacon.types.deposit_data import DepositData
from eth2.beacon.types.deposit_input import DepositInput
from eth2.beacon.types.eth1_data import Eth1Data
from eth2.beacon.types.historical_batch import HistoricalBatch
from eth2.beacon.types.slashable_attestations import SlashableAttestation
from eth2.beacon.types.states import BeaconState

Expand Down Expand Up @@ -57,17 +58,24 @@
# SSZ
@pytest.fixture(scope="function", autouse=True)
def override_length(config):
vector_dict = {
state_vector_dict = {
"latest_randao_mixes": config.LATEST_RANDAO_MIXES_LENGTH,
"latest_crosslinks": config.SHARD_COUNT,
"latest_block_roots": config.SLOTS_PER_HISTORICAL_ROOT,
"latest_state_roots": config.SLOTS_PER_HISTORICAL_ROOT,
"latest_active_index_roots": config.LATEST_ACTIVE_INDEX_ROOTS_LENGTH,
"latest_slashed_balances": config.LATEST_SLASHED_EXIT_LENGTH,
}
for key, value in vector_dict.items():
for key, value in state_vector_dict.items():
BeaconState._meta.container_sedes.field_name_to_sedes[key].length = value

historical_batch_vector_dict = {
"block_roots": config.SLOTS_PER_HISTORICAL_ROOT,
"state_roots": config.SLOTS_PER_HISTORICAL_ROOT,
}
for key, value in historical_batch_vector_dict.items():
HistoricalBatch._meta.container_sedes.field_name_to_sedes[key].length = value


@pytest.fixture(scope="session")
def privkeys():
Expand Down
1 change: 0 additions & 1 deletion tests/eth2/beacon/state_machines/test_state_transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def test_per_slot_transition(base_db,
historical_batch = HistoricalBatch(
block_roots=state.latest_block_roots,
state_roots=state.latest_state_roots,
slots_per_historical_root=config.SLOTS_PER_HISTORICAL_ROOT,
)
assert updated_state.historical_roots[-1] == historical_batch.root
else:
Expand Down

0 comments on commit c51e86a

Please sign in to comment.