simplify index

This commit is contained in:
Danny Ryan 2019-10-16 18:47:19 +09:00
parent 6208e74d3f
commit bd1c71b82e
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
6 changed files with 8 additions and 70 deletions

View File

@ -490,7 +490,6 @@ class BeaconState(Container):
validators: List[Validator, VALIDATOR_REGISTRY_LIMIT]
balances: List[Gwei, VALIDATOR_REGISTRY_LIMIT]
# Shuffling
start_index: uint64
randao_mixes: Vector[Hash, EPOCHS_PER_HISTORICAL_VECTOR]
# Slashings
slashings: Vector[Gwei, EPOCHS_PER_SLASHINGS_VECTOR] # Per-epoch sums of slashed effective balances
@ -881,9 +880,7 @@ def get_crosslink_committee(state: BeaconState, slot: Slot, index: uint64) -> Se
"""
epoch = compute_epoch_of_slot(slot)
committees_per_slot = get_committees_per_slot(state, slot)
slot_start_index = get_slot_start_index(state, slot)
slot_offset = (index + MAX_COMMITTEES_PER_SLOT - slot_start_index) % MAX_COMMITTEES_PER_SLOT
epoch_offset = slot_offset + (slot % SLOTS_PER_EPOCH) * committees_per_slot
epoch_offset = index + (slot % SLOTS_PER_EPOCH) * committees_per_slot
return compute_committee(
indices=get_active_validator_indices(state, epoch),
@ -893,47 +890,6 @@ def get_crosslink_committee(state: BeaconState, slot: Slot, index: uint64) -> Se
)
```
#### `get_slot_start_index`
```python
def get_slot_start_index(state: BeaconState, slot: Slot) -> uint64:
"""
Return the start index of the 0th committee at ``slot``.
"""
epoch = compute_epoch_of_slot(slot)
committees_per_slot = get_committees_per_slot(state, slot)
start_index = get_start_index(state, epoch)
slot_start_index = ((slot % SLOTS_PER_EPOCH) * committees_per_slot + start_index) % MAX_COMMITTEES_PER_SLOT
return slot_start_index
```
#### `get_start_index`
```python
def get_start_index(state: BeaconState, epoch: Epoch) -> uint64:
"""
Return the start index of the 0th committee at ``epoch``.
"""
assert epoch <= get_current_epoch(state) + 1
check_epoch = Epoch(get_current_epoch(state) + 1)
index = (state.start_index + get_index_delta(state, get_current_epoch(state))) % MAX_COMMITTEES_PER_SLOT
MAX_COMMITTEES_PER_EPOCH = MAX_COMMITTEES_PER_SLOT * SLOTS_PER_EPOCH
while check_epoch > epoch:
check_epoch -= Epoch(1)
index = (index + MAX_COMMITTEES_PER_EPOCH - get_index_delta(state, check_epoch)) % MAX_COMMITTEES_PER_SLOT
return index
```
#### `get_index_delta`
```python
def get_index_delta(state: BeaconState, epoch: Epoch) -> uint64:
"""
Return the amount to increase ``state.start_index`` at ``epoch``.
"""
return get_committees_per_slot(state, compute_start_slot_of_epoch(epoch)) * SLOTS_PER_EPOCH
```
#### `get_beacon_proposer_index`
```python
@ -1417,8 +1373,6 @@ def process_final_updates(state: BeaconState) -> None:
if next_epoch % (SLOTS_PER_HISTORICAL_ROOT // SLOTS_PER_EPOCH) == 0:
historical_batch = HistoricalBatch(block_roots=state.block_roots, state_roots=state.state_roots)
state.historical_roots.append(hash_tree_root(historical_batch))
# Update start shard
state.start_index = (state.start_index + get_index_delta(state, current_epoch)) % MAX_COMMITTEES_PER_SLOT
# Rotate current/previous epoch attestations
state.previous_epoch_attestations = state.current_epoch_attestations
state.current_epoch_attestations = []
@ -1545,15 +1499,8 @@ def process_attester_slashing(state: BeaconState, attester_slashing: AttesterSla
```python
def process_attestation(state: BeaconState, attestation: Attestation) -> None:
data = attestation.data
assert data.index < MAX_COMMITTEES_PER_SLOT
slot_start_index = get_slot_start_index(state, data.slot)
if data.index < slot_start_index:
test_index = data.index + MAX_COMMITTEES_PER_SLOT
else:
test_index = data.index
assert slot_start_index <= test_index < slot_start_index + get_committees_per_slot(state, data.slot)
assert data.index < get_committees_per_slot(state, data.slot)
assert data.target.epoch in (get_previous_epoch(state), get_current_epoch(state))
assert data.slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot <= data.slot + SLOTS_PER_EPOCH
committee = get_crosslink_committee(state, data.slot, data.index)

View File

@ -149,9 +149,7 @@ def get_committee_assignment(state: BeaconState,
start_slot = compute_start_slot_of_epoch(epoch)
for slot in range(start_slot, start_slot + SLOTS_PER_EPOCH):
slot_start_index = get_slot_start_index(state, Slot(slot))
for i in range(get_committees_per_slot(state, Slot(slot))):
index = (slot_start_index + i) % MAX_COMMITTEES_PER_SLOT
for index in range(get_committees_per_slot(state, Slot(slot))):
committee = get_crosslink_committee(state, Slot(slot), index)
if validator_index in committee:
return committee, index, Slot(slot)

View File

@ -42,7 +42,7 @@ def get_valid_attestation(spec, state, slot=None, index=None, signed=False):
if slot is None:
slot = state.slot
if index is None:
index = spec.get_slot_start_index(state, slot)
index = 0
attestation_data = build_attestation_data(spec, state, slot, index)

View File

@ -54,18 +54,14 @@ def next_epoch_with_attestations(spec,
slot_to_attest = post_state.slot - spec.MIN_ATTESTATION_INCLUSION_DELAY + 1
committees_per_slot = spec.get_committees_per_slot(state, slot_to_attest)
if slot_to_attest >= spec.compute_start_slot_of_epoch(spec.get_current_epoch(post_state)):
slot_start_index = spec.get_slot_start_index(state, slot_to_attest)
for i in range(committees_per_slot):
index = (slot_start_index + i) % spec.MAX_COMMITTEES_PER_SLOT
for index in range(committees_per_slot):
cur_attestation = get_valid_attestation(spec, post_state, slot_to_attest, index=index)
block.body.attestations.append(cur_attestation)
if fill_prev_epoch:
slot_to_attest = post_state.slot - spec.SLOTS_PER_EPOCH + 1
committees_per_slot = spec.get_committees_per_slot(state, slot_to_attest)
slot_start_index = spec.get_slot_start_index(state, slot_to_attest)
for i in range(committees_per_slot):
index = (slot_start_index + i) % spec.MAX_COMMITTEES_PER_SLOT
for index in range(committees_per_slot):
prev_attestation = get_valid_attestation(spec, post_state, slot_to_attest, index=index)
block.body.attestations.append(prev_attestation)

View File

@ -140,8 +140,7 @@ def test_wrong_index_for_committee_signature(spec, state):
def test_wrong_index_for_slot(spec, state):
committees_per_slot = spec.get_committees_per_slot(state, state.slot)
assert committees_per_slot < spec.MAX_COMMITTEES_PER_SLOT
slot_start_index = spec.get_slot_start_index(state, state.slot)
index = slot_start_index + committees_per_slot
index = committees_per_slot
attestation = get_valid_attestation(spec, state)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY

View File

@ -28,9 +28,7 @@ def add_mock_attestations(spec, state, epoch, source, target, sufficient_support
start_slot = spec.compute_start_slot_of_epoch(epoch)
for slot in range(start_slot, start_slot + spec.SLOTS_PER_EPOCH):
committees_per_slot = spec.get_committees_per_slot(state, slot)
slot_start_index = spec.get_slot_start_index(state, slot)
for i in range(committees_per_slot):
index = (slot_start_index + i) % spec.MAX_COMMITTEES_PER_SLOT
for index in range(committees_per_slot):
# Check if we already have had sufficient balance. (and undone if we don't want it).
# If so, do not create more attestations. (we do not have empty pending attestations normally anyway)
if remaining_balance < 0: