get crosslink committees by slot

This commit is contained in:
Danny Ryan 2019-10-13 21:52:58 +09:00
parent d8431f8476
commit 6208e74d3f
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
7 changed files with 48 additions and 23 deletions

View File

@ -5,7 +5,7 @@
# --------------------------------------------------------------- # ---------------------------------------------------------------
# [customized] Just 2 committees for slot for testing purposes # [customized] Just 2 committees for slot for testing purposes
MAX_COMMITTEES_PER_SLOT: 2 MAX_COMMITTEES_PER_SLOT: 4
# [customized] unsecure, but fast # [customized] unsecure, but fast
TARGET_COMMITTEE_SIZE: 4 TARGET_COMMITTEE_SIZE: 4
# 2**12 (= 4,096) # 2**12 (= 4,096)

View File

@ -858,19 +858,18 @@ def get_seed(state: BeaconState, epoch: Epoch, domain_type: DomainType) -> Hash:
return hash(domain_type + int_to_bytes(epoch, length=8) + mix) return hash(domain_type + int_to_bytes(epoch, length=8) + mix)
``` ```
#### `get_committees_per_slot`
#### `get_committee_count`
```python ```python
def get_committee_count(state: BeaconState, epoch: Epoch) -> uint64: def get_committees_per_slot(state: BeaconState, slot: Slot) -> uint64:
""" """
Return the number of committees at ``epoch``. Return the number of committees at ``slot``.
""" """
committees_per_slot = max(1, min( epoch = compute_epoch_of_slot(slot)
return max(1, min(
MAX_COMMITTEES_PER_SLOT, MAX_COMMITTEES_PER_SLOT,
len(get_active_validator_indices(state, epoch)) // SLOTS_PER_EPOCH // TARGET_COMMITTEE_SIZE, len(get_active_validator_indices(state, epoch)) // SLOTS_PER_EPOCH // TARGET_COMMITTEE_SIZE,
)) ))
return committees_per_slot * SLOTS_PER_EPOCH
``` ```
#### `get_crosslink_committee` #### `get_crosslink_committee`
@ -878,20 +877,19 @@ def get_committee_count(state: BeaconState, epoch: Epoch) -> uint64:
```python ```python
def get_crosslink_committee(state: BeaconState, slot: Slot, index: uint64) -> Sequence[ValidatorIndex]: def get_crosslink_committee(state: BeaconState, slot: Slot, index: uint64) -> Sequence[ValidatorIndex]:
""" """
Return the crosslink committee at ``epoch`` for ``index``. Return the crosslink committee at ``slot`` for ``index``.
""" """
epoch = compute_epoch_of_slot(slot) epoch = compute_epoch_of_slot(slot)
committees_per_slot = get_committee_count(state, epoch) // SLOTS_PER_EPOCH committees_per_slot = get_committees_per_slot(state, slot)
slot_start_index = get_slot_start_index(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 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 = slot_offset + (slot % SLOTS_PER_EPOCH) * committees_per_slot
print(epoch_offset)
return compute_committee( return compute_committee(
indices=get_active_validator_indices(state, epoch), indices=get_active_validator_indices(state, epoch),
seed=get_seed(state, epoch, DOMAIN_BEACON_ATTESTER), seed=get_seed(state, epoch, DOMAIN_BEACON_ATTESTER),
index=epoch_offset, index=epoch_offset,
count=get_committee_count(state, epoch), count=committees_per_slot * SLOTS_PER_EPOCH,
) )
``` ```
@ -903,8 +901,9 @@ def get_slot_start_index(state: BeaconState, slot: Slot) -> uint64:
Return the start index of the 0th committee at ``slot``. Return the start index of the 0th committee at ``slot``.
""" """
epoch = compute_epoch_of_slot(slot) epoch = compute_epoch_of_slot(slot)
committees_per_slot = get_committee_count(state, epoch) // SLOTS_PER_EPOCH committees_per_slot = get_committees_per_slot(state, slot)
slot_start_index = ((slot % SLOTS_PER_EPOCH) * committees_per_slot + get_start_index(state, epoch)) % MAX_COMMITTEES_PER_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 return slot_start_index
``` ```
@ -932,7 +931,7 @@ def get_index_delta(state: BeaconState, epoch: Epoch) -> uint64:
""" """
Return the amount to increase ``state.start_index`` at ``epoch``. Return the amount to increase ``state.start_index`` at ``epoch``.
""" """
return get_committee_count(state, epoch) return get_committees_per_slot(state, compute_start_slot_of_epoch(epoch)) * SLOTS_PER_EPOCH
``` ```
#### `get_beacon_proposer_index` #### `get_beacon_proposer_index`
@ -1547,6 +1546,12 @@ def process_attester_slashing(state: BeaconState, attester_slashing: AttesterSla
def process_attestation(state: BeaconState, attestation: Attestation) -> None: def process_attestation(state: BeaconState, attestation: Attestation) -> None:
data = attestation.data data = attestation.data
assert data.index < MAX_COMMITTEES_PER_SLOT 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.target.epoch in (get_previous_epoch(state), get_current_epoch(state)) 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 assert data.slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot <= data.slot + SLOTS_PER_EPOCH

View File

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

View File

@ -43,9 +43,6 @@ def get_valid_attestation(spec, state, slot=None, index=None, signed=False):
slot = state.slot slot = state.slot
if index is None: if index is None:
index = spec.get_slot_start_index(state, slot) index = spec.get_slot_start_index(state, slot)
print(slot)
print(index)
print(spec.get_committee_count(state, spec.compute_epoch_of_slot(slot)))
attestation_data = build_attestation_data(spec, state, slot, index) attestation_data = build_attestation_data(spec, state, slot, index)

View File

@ -51,8 +51,8 @@ def next_epoch_with_attestations(spec,
for _ in range(spec.SLOTS_PER_EPOCH): for _ in range(spec.SLOTS_PER_EPOCH):
block = build_empty_block_for_next_slot(spec, post_state) block = build_empty_block_for_next_slot(spec, post_state)
if fill_cur_epoch and post_state.slot >= spec.MIN_ATTESTATION_INCLUSION_DELAY: if fill_cur_epoch and post_state.slot >= spec.MIN_ATTESTATION_INCLUSION_DELAY:
committees_per_slot = spec.get_committee_count(state, spec.get_current_epoch(state)) // spec.SLOTS_PER_EPOCH
slot_to_attest = post_state.slot - spec.MIN_ATTESTATION_INCLUSION_DELAY + 1 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)): 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) slot_start_index = spec.get_slot_start_index(state, slot_to_attest)
for i in range(committees_per_slot): for i in range(committees_per_slot):
@ -61,8 +61,8 @@ def next_epoch_with_attestations(spec,
block.body.attestations.append(cur_attestation) block.body.attestations.append(cur_attestation)
if fill_prev_epoch: if fill_prev_epoch:
committees_per_slot = spec.get_committee_count(state, spec.get_previous_epoch(state)) // spec.SLOTS_PER_EPOCH
slot_to_attest = post_state.slot - spec.SLOTS_PER_EPOCH + 1 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) slot_start_index = spec.get_slot_start_index(state, slot_to_attest)
for i in range(committees_per_slot): for i in range(committees_per_slot):
index = (slot_start_index + i) % spec.MAX_COMMITTEES_PER_SLOT index = (slot_start_index + i) % spec.MAX_COMMITTEES_PER_SLOT

View File

@ -124,8 +124,31 @@ def test_old_source_epoch(spec, state):
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_wrong_index(spec, state): @always_bls
pass def test_wrong_index_for_committee_signature(spec, state):
attestation = get_valid_attestation(spec, state)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
attestation.data.index += 1
yield from run_attestation_processing(spec, state, attestation, False)
@with_all_phases
@spec_state_test
@never_bls
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
attestation = get_valid_attestation(spec, state)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
attestation.data.index = index
yield from run_attestation_processing(spec, state, attestation, False)
@with_all_phases @with_all_phases

View File

@ -26,8 +26,8 @@ def add_mock_attestations(spec, state, epoch, source, target, sufficient_support
remaining_balance = total_balance * 2 // 3 remaining_balance = total_balance * 2 // 3
start_slot = spec.compute_start_slot_of_epoch(epoch) start_slot = spec.compute_start_slot_of_epoch(epoch)
committees_per_slot = spec.get_committee_count(state, epoch) // spec.SLOTS_PER_EPOCH
for slot in range(start_slot, start_slot + spec.SLOTS_PER_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) slot_start_index = spec.get_slot_start_index(state, slot)
for i in range(committees_per_slot): for i in range(committees_per_slot):
index = (slot_start_index + i) % spec.MAX_COMMITTEES_PER_SLOT index = (slot_start_index + i) % spec.MAX_COMMITTEES_PER_SLOT