`compute_slot_epoch`->`compute_epoch_of_slot`

This commit is contained in:
Carl Beekhuizen 2019-06-30 23:35:07 +02:00
parent cb71409114
commit 918192cdab
No known key found for this signature in database
GPG Key ID: D05CA176D0020646
11 changed files with 49 additions and 47 deletions

View File

@ -97,7 +97,7 @@ def apply_constants_preset(preset: Dict[str, Any]) -> None:
global_vars[k] = v
# Deal with derived constants
global_vars['GENESIS_EPOCH'] = compute_slot_epoch(GENESIS_SLOT)
global_vars['GENESIS_EPOCH'] = compute_epoch_of_slot(GENESIS_SLOT)
# Initialize SSZ types again, to account for changed lengths
init_SSZ_types()

View File

@ -68,8 +68,8 @@
- [Misc](#misc)
- [`compute_shuffled_index`](#compute_shuffled_index)
- [`compute_committee`](#compute_committee)
- [`compute_slot_epoch`](#compute_slot_epoch)
- [`compute_epoch_start_slot`](#compute_epoch_start_slot)
- [`compute_epoch_of_slot`](#compute_epoch_of_slot)
- [`compute_start_slot_of_epoch`](#compute_start_slot_of_epoch)
- [`compute_activation_exit_epoch`](#compute_activation_exit_epoch)
- [`compute_bls_domain`](#compute_bls_domain)
- [`validate_indexed_attestation`](#validate_indexed_attestation)
@ -701,20 +701,20 @@ def compute_committee(indices: Sequence[ValidatorIndex],
return [indices[compute_shuffled_index(ValidatorIndex(i), len(indices), seed)] for i in range(start, end)]
```
#### `compute_slot_epoch`
#### `compute_epoch_of_slot`
```python
def compute_slot_epoch(slot: Slot) -> Epoch:
def compute_epoch_of_slot(slot: Slot) -> Epoch:
"""
Return the epoch number of ``slot``.
"""
return Epoch(slot // SLOTS_PER_EPOCH)
```
#### `compute_epoch_start_slot`
#### `compute_start_slot_of_epoch`
```python
def compute_epoch_start_slot(epoch: Epoch) -> Slot:
def compute_start_slot_of_epoch(epoch: Epoch) -> Slot:
"""
Return the start slot of ``epoch``.
"""
@ -783,7 +783,7 @@ def get_current_epoch(state: BeaconState) -> Epoch:
"""
Return the current epoch.
"""
return compute_slot_epoch(state.slot)
return compute_epoch_of_slot(state.slot)
```
#### `get_previous_epoch`
@ -804,7 +804,7 @@ def get_block_root(state: BeaconState, epoch: Epoch) -> Hash:
"""
Return the block root at the start of a recent ``epoch``.
"""
return get_block_root_at_slot(state, compute_epoch_start_slot(epoch))
return get_block_root_at_slot(state, compute_start_slot_of_epoch(epoch))
```
#### `get_block_root_at_slot`
@ -950,7 +950,7 @@ def get_attestation_data_slot(state: BeaconState, data: AttestationData) -> Slot
"""
committee_count = get_committee_count(state, data.target.epoch)
offset = (data.crosslink.shard + SHARD_COUNT - get_start_shard(state, data.target.epoch)) % SHARD_COUNT
return Slot(compute_epoch_start_slot(data.target.epoch) + offset // (committee_count // SLOTS_PER_EPOCH))
return Slot(compute_start_slot_of_epoch(data.target.epoch) + offset // (committee_count // SLOTS_PER_EPOCH))
```
#### `get_compact_committees_root`
@ -1585,14 +1585,15 @@ def process_operations(state: BeaconState, body: BeaconBlockBody) -> None:
def process_proposer_slashing(state: BeaconState, proposer_slashing: ProposerSlashing) -> None:
proposer = state.validators[proposer_slashing.proposer_index]
# Verify that the epoch is the same
assert compute_slot_epoch(proposer_slashing.header_1.slot) == compute_slot_epoch(proposer_slashing.header_2.slot)
assert (compute_epoch_of_slot(proposer_slashing.header_1.slot)
== compute_epoch_of_slot(proposer_slashing.header_2.slot))
# But the headers are different
assert proposer_slashing.header_1 != proposer_slashing.header_2
# Check proposer is slashable
assert is_slashable_validator(proposer, get_current_epoch(state))
# Signatures are valid
for header in (proposer_slashing.header_1, proposer_slashing.header_2):
domain = get_domain(state, DOMAIN_BEACON_PROPOSER, compute_slot_epoch(header.slot))
domain = get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_of_slot(header.slot))
assert bls_verify(proposer.pubkey, signing_root(header), header.signature, domain)
slash_validator(state, proposer_slashing.proposer_index)

View File

@ -124,7 +124,7 @@ def get_latest_attesting_balance(store: Store, root: Hash) -> Gwei:
def get_head(store: Store) -> Hash:
# Execute the LMD-GHOST fork choice
head = store.justified_checkpoint.root
justified_slot = compute_epoch_start_slot(store.justified_checkpoint.epoch)
justified_slot = compute_start_slot_of_epoch(store.justified_checkpoint.epoch)
while True:
children = [
root for root in store.blocks.keys()
@ -162,7 +162,7 @@ def on_block(store: Store, block: BeaconBlock) -> None:
store.finalized_checkpoint.root
)
# Check that block is later than the finalized epoch slot
assert block.slot > compute_epoch_start_slot(store.finalized_checkpoint.epoch)
assert block.slot > compute_start_slot_of_epoch(store.finalized_checkpoint.epoch)
# Check the block is valid and compute the post-state
state = state_transition(pre_state, block)
# Add new state for this block to the store
@ -190,11 +190,11 @@ def on_attestation(store: Store, attestation: Attestation) -> None:
# Attestations cannot be from future epochs. If they are, delay consideration until the epoch arrives
base_state = store.block_states[target.root].copy()
assert store.time >= base_state.genesis_time + compute_epoch_start_slot(target.epoch) * SECONDS_PER_SLOT
assert store.time >= base_state.genesis_time + compute_start_slot_of_epoch(target.epoch) * SECONDS_PER_SLOT
# Store target checkpoint state if not yet seen
if target not in store.checkpoint_states:
process_slots(base_state, compute_epoch_start_slot(target.epoch))
process_slots(base_state, compute_start_slot_of_epoch(target.epoch))
store.checkpoint_states[target] = base_state
target_state = store.checkpoint_states[target]

View File

@ -474,7 +474,8 @@ def process_chunk_challenge(state: BeaconState, challenge: CustodyChunkChallenge
# Verify the attestation
validate_indexed_attestation(state, get_indexed_attestation(state, challenge.attestation))
# Verify it is not too late to challenge
assert compute_slot_epoch(challenge.attestation.data.slot) >= get_current_epoch(state) - MAX_CHUNK_CHALLENGE_DELAY
assert (compute_epoch_of_slot(challenge.attestation.data.slot)
>= get_current_epoch(state) - MAX_CHUNK_CHALLENGE_DELAY)
responder = state.validators[challenge.responder_index]
assert responder.exit_epoch >= get_current_epoch(state) - MAX_CHUNK_CHALLENGE_DELAY
# Verify the responder participated in the attestation
@ -515,7 +516,7 @@ For each `challenge` in `block.body.custody_bit_challenges`, run the following f
```python
def process_bit_challenge(state: BeaconState, challenge: CustodyBitChallenge) -> None:
attestation = challenge.attestation
epoch = compute_slot_epoch(attestation.data.slot)
epoch = compute_epoch_of_slot(attestation.data.slot)
shard = attestation.data.crosslink.shard
# Verify challenge signature

View File

@ -163,7 +163,7 @@ def get_persistent_committee(state: BeaconState,
"""
Return the persistent committee for the given ``shard`` at the given ``slot``.
"""
epoch = compute_slot_epoch(slot)
epoch = compute_epoch_of_slot(slot)
earlier_start_epoch = Epoch(epoch - (epoch % PERSISTENT_COMMITTEE_PERIOD) - PERSISTENT_COMMITTEE_PERIOD * 2)
later_start_epoch = Epoch(epoch - (epoch % PERSISTENT_COMMITTEE_PERIOD) - PERSISTENT_COMMITTEE_PERIOD)
@ -240,7 +240,7 @@ def verify_shard_attestation_signature(state: BeaconState,
pubkey=bls_aggregate_pubkeys(pubkeys),
message_hash=data.shard_block_root,
signature=attestation.aggregate_signature,
domain=get_domain(state, DOMAIN_SHARD_ATTESTER, compute_slot_epoch(data.slot))
domain=get_domain(state, DOMAIN_SHARD_ATTESTER, compute_epoch_of_slot(data.slot))
)
```
@ -339,7 +339,7 @@ def is_valid_shard_block(beacon_blocks: Sequence[BeaconBlock],
pubkey=beacon_state.validators[proposer_index].pubkey,
message_hash=signing_root(candidate),
signature=candidate.signature,
domain=get_domain(beacon_state, DOMAIN_SHARD_PROPOSER, compute_slot_epoch(candidate.slot)),
domain=get_domain(beacon_state, DOMAIN_SHARD_PROPOSER, compute_epoch_of_slot(candidate.slot)),
)
return True
@ -403,11 +403,11 @@ def is_valid_beacon_attestation(shard: Shard,
None,
)
assert previous_attestation is not None
assert candidate.data.previous_attestation.epoch < compute_slot_epoch(candidate.data.slot)
assert candidate.data.previous_attestation.epoch < compute_epoch_of_slot(candidate.data.slot)
# Check crosslink data root
start_epoch = beacon_state.crosslinks[shard].epoch
end_epoch = min(compute_slot_epoch(candidate.data.slot) - CROSSLINK_LOOKBACK,
end_epoch = min(compute_epoch_of_slot(candidate.data.slot) - CROSSLINK_LOOKBACK,
start_epoch + MAX_EPOCHS_PER_CROSSLINK)
blocks = []
for slot in range(start_epoch * SLOTS_PER_EPOCH, end_epoch * SLOTS_PER_EPOCH):

View File

@ -124,7 +124,7 @@ def compute_committee(header: BeaconBlockHeader,
maximal_later_committee = validator_memory.later_period_data.committee
earlier_start_epoch = get_earlier_start_epoch(header.slot)
later_start_epoch = get_later_start_epoch(header.slot)
epoch = compute_slot_epoch(header.slot)
epoch = compute_epoch_of_slot(header.slot)
committee_count = max(
earlier_validator_count // (SHARD_COUNT * TARGET_COMMITTEE_SIZE),
@ -192,7 +192,7 @@ def verify_block_validity_proof(proof: BlockValidityProof, validator_memory: Val
pubkey=group_public_key,
message_hash=hash_tree_root(shard_parent_block),
signature=proof.shard_aggregate_signature,
domain=get_domain(state, compute_slot_epoch(shard_block.slot), DOMAIN_SHARD_ATTESTER),
domain=get_domain(state, compute_epoch_of_slot(shard_block.slot), DOMAIN_SHARD_ATTESTER),
)
```

View File

@ -147,7 +147,7 @@ def get_committee_assignment(
assert epoch <= next_epoch
committees_per_slot = get_committee_count(state, epoch) // SLOTS_PER_EPOCH
start_slot = compute_epoch_start_slot(epoch)
start_slot = compute_start_slot_of_epoch(epoch)
for slot in range(start_slot, start_slot + SLOTS_PER_EPOCH):
offset = committees_per_slot * (slot % SLOTS_PER_EPOCH)
slot_start_shard = (get_start_shard(state, epoch) + offset) % SHARD_COUNT
@ -211,10 +211,10 @@ Set `block.randao_reveal = epoch_signature` where `epoch_signature` is defined a
```python
epoch_signature = bls_sign(
privkey=validator.privkey, # privkey stored locally, not in state
message_hash=hash_tree_root(compute_slot_epoch(block.slot)),
message_hash=hash_tree_root(compute_epoch_of_slot(block.slot)),
domain=get_domain(
fork=fork, # `fork` is the fork object at the slot `block.slot`
epoch=compute_slot_epoch(block.slot),
epoch=compute_epoch_of_slot(block.slot),
domain_type=DOMAIN_RANDAO,
)
)
@ -253,7 +253,7 @@ block_signature = bls_sign(
message_hash=signing_root(block),
domain=get_domain(
fork=fork, # `fork` is the fork object at the slot `block.slot`
epoch=compute_slot_epoch(block.slot),
epoch=compute_epoch_of_slot(block.slot),
domain_type=DOMAIN_BEACON_BLOCK,
)
)
@ -309,7 +309,7 @@ Set `attestation_data.beacon_block_root = signing_root(head_block)`.
*Note*: `epoch_boundary_block_root` can be looked up in the state using:
- Let `start_slot = compute_epoch_start_slot(get_current_epoch(head_state))`.
- Let `start_slot = compute_start_slot_of_epoch(get_current_epoch(head_state))`.
- Let `epoch_boundary_block_root = signing_root(head_block) if start_slot == head_state.slot else get_block_root(state, start_slot)`.
##### Crosslink vote
@ -359,7 +359,7 @@ signed_attestation_data = bls_sign(
message_hash=attestation_message,
domain=get_domain(
fork=fork, # `fork` is the fork object at the slot, `attestation_data.slot`
epoch=compute_slot_epoch(attestation_data.slot),
epoch=compute_epoch_of_slot(attestation_data.slot),
domain_type=DOMAIN_ATTESTATION,
)
)
@ -379,7 +379,7 @@ To avoid "proposer slashings", a validator must not sign two conflicting [`Beaco
Specifically, when signing a `BeaconBlock`, a validator should perform the following steps in the following order:
1. Save a record to hard disk that a beacon block has been signed for the `epoch=compute_slot_epoch(block.slot)`.
1. Save a record to hard disk that a beacon block has been signed for the `epoch=compute_epoch_of_slot(block.slot)`.
2. Generate and broadcast the block.
If the software crashes at some point within this routine, then when the validator comes back online, the hard disk has the record of the *potentially* signed/broadcast block and can effectively avoid slashing.
@ -390,7 +390,7 @@ To avoid "attester slashings", a validator must not sign two conflicting [`Attes
Specifically, when signing an `Attestation`, a validator should perform the following steps in the following order:
1. Save a record to hard disk that an attestation has been signed for source (i.e. `attestation_data.source_epoch`) and target (i.e. `compute_slot_epoch(attestation_data.slot)`).
1. Save a record to hard disk that an attestation has been signed for source (i.e. `attestation_data.source_epoch`) and target (i.e. `compute_epoch_of_slot(attestation_data.slot)`).
2. Generate and broadcast attestation.
If the software crashes at some point within this routine, then when the validator comes back online, the hard disk has the record of the *potentially* signed/broadcast attestation and can effectively avoid slashing.

View File

@ -15,7 +15,7 @@ def build_attestation_data(spec, state, slot, shard):
else:
block_root = spec.get_block_root_at_slot(state, slot)
current_epoch_start_slot = spec.compute_epoch_start_slot(spec.get_current_epoch(state))
current_epoch_start_slot = spec.compute_start_slot_of_epoch(spec.get_current_epoch(state))
if slot < current_epoch_start_slot:
epoch_boundary_root = spec.get_block_root(state, spec.get_previous_epoch(state))
elif slot == current_epoch_start_slot:
@ -30,7 +30,7 @@ def build_attestation_data(spec, state, slot, shard):
source_epoch = state.current_justified_checkpoint.epoch
source_root = state.current_justified_checkpoint.root
if spec.compute_slot_epoch(slot) == spec.get_current_epoch(state):
if spec.compute_epoch_of_slot(slot) == spec.get_current_epoch(state):
parent_crosslink = state.current_crosslinks[shard]
else:
parent_crosslink = state.previous_crosslinks[shard]
@ -38,11 +38,11 @@ def build_attestation_data(spec, state, slot, shard):
return spec.AttestationData(
beacon_block_root=block_root,
source=spec.Checkpoint(epoch=source_epoch, root=source_root),
target=spec.Checkpoint(epoch=spec.compute_slot_epoch(slot), root=epoch_boundary_root),
target=spec.Checkpoint(epoch=spec.compute_epoch_of_slot(slot), root=epoch_boundary_root),
crosslink=spec.Crosslink(
shard=shard,
start_epoch=parent_crosslink.end_epoch,
end_epoch=min(spec.compute_slot_epoch(slot), parent_crosslink.end_epoch + spec.MAX_EPOCHS_PER_CROSSLINK),
end_epoch=min(spec.compute_epoch_of_slot(slot), parent_crosslink.end_epoch + spec.MAX_EPOCHS_PER_CROSSLINK),
data_root=spec.Hash(),
parent_root=hash_tree_root(parent_crosslink),
),
@ -53,7 +53,7 @@ def get_valid_attestation(spec, state, slot=None, signed=False):
if slot is None:
slot = state.slot
epoch = spec.compute_slot_epoch(slot)
epoch = spec.compute_epoch_of_slot(slot)
epoch_start_shard = spec.get_start_shard(state, epoch)
committees_per_slot = spec.get_committee_count(state, epoch) // spec.SLOTS_PER_EPOCH
shard = (epoch_start_shard + committees_per_slot * (slot % spec.SLOTS_PER_EPOCH)) % spec.SHARD_COUNT

View File

@ -14,7 +14,7 @@ def sign_block(spec, state, block, proposer_index=None):
if block.slot == state.slot:
proposer_index = spec.get_beacon_proposer_index(state)
else:
if spec.compute_slot_epoch(state.slot) + 1 > spec.compute_slot_epoch(block.slot):
if spec.compute_epoch_of_slot(state.slot) + 1 > spec.compute_epoch_of_slot(block.slot):
print("warning: block slot far away, and no proposer index manually given."
" Signing block is slow due to transition for proposer index calculation.")
# use stub state to get proposer index of future slot
@ -26,10 +26,10 @@ def sign_block(spec, state, block, proposer_index=None):
block.body.randao_reveal = bls_sign(
privkey=privkey,
message_hash=hash_tree_root(spec.compute_slot_epoch(block.slot)),
message_hash=hash_tree_root(spec.compute_epoch_of_slot(block.slot)),
domain=spec.get_domain(
state,
message_epoch=spec.compute_slot_epoch(block.slot),
message_epoch=spec.compute_epoch_of_slot(block.slot),
domain_type=spec.DOMAIN_RANDAO,
)
)
@ -39,7 +39,7 @@ def sign_block(spec, state, block, proposer_index=None):
domain=spec.get_domain(
state,
spec.DOMAIN_BEACON_PROPOSER,
spec.compute_slot_epoch(block.slot)))
spec.compute_epoch_of_slot(block.slot)))
def apply_empty_block(spec, state):

View File

@ -9,7 +9,7 @@ def run_process_just_and_fin(spec, state):
def get_shards_for_slot(spec, state, slot):
epoch = spec.compute_slot_epoch(slot)
epoch = spec.compute_epoch_of_slot(slot)
epoch_start_shard = spec.get_start_shard(state, epoch)
committees_per_slot = spec.get_committee_count(state, epoch) // spec.SLOTS_PER_EPOCH
shard = (epoch_start_shard + committees_per_slot * (slot % spec.SLOTS_PER_EPOCH)) % spec.SHARD_COUNT
@ -33,7 +33,7 @@ def add_mock_attestations(spec, state, epoch, source, target, sufficient_support
total_balance = spec.get_total_active_balance(state)
remaining_balance = total_balance * 2 // 3
start_slot = spec.compute_epoch_start_slot(epoch)
start_slot = spec.compute_start_slot_of_epoch(epoch)
for slot in range(start_slot, start_slot + spec.SLOTS_PER_EPOCH):
for shard in get_shards_for_slot(spec, state, slot):
# Check if we already have had sufficient balance. (and undone if we don't want it).
@ -41,7 +41,7 @@ def add_mock_attestations(spec, state, epoch, source, target, sufficient_support
if remaining_balance < 0:
return
committee = spec.get_crosslink_committee(state, spec.compute_slot_epoch(slot), shard)
committee = spec.get_crosslink_committee(state, spec.compute_epoch_of_slot(slot), shard)
# Create a bitfield filled with the given count per attestation,
# exactly on the right-most part of the committee field.
@ -80,7 +80,7 @@ def get_checkpoints(spec, epoch):
def put_checkpoints_in_block_roots(spec, state, checkpoints):
for c in checkpoints:
state.block_roots[spec.compute_epoch_start_slot(c.epoch) % spec.SLOTS_PER_HISTORICAL_ROOT] = c.root
state.block_roots[spec.compute_start_slot_of_epoch(c.epoch) % spec.SLOTS_PER_HISTORICAL_ROOT] = c.root
def finalize_on_234(spec, state, epoch, sufficient_support):

View File

@ -43,7 +43,7 @@ def next_epoch_with_attestations(spec,
block = build_empty_block_for_next_slot(spec, post_state)
if fill_cur_epoch and post_state.slot >= spec.MIN_ATTESTATION_INCLUSION_DELAY:
slot_to_attest = post_state.slot - spec.MIN_ATTESTATION_INCLUSION_DELAY + 1
if slot_to_attest >= spec.compute_epoch_start_slot(spec.get_current_epoch(post_state)):
if slot_to_attest >= spec.compute_start_slot_of_epoch(spec.get_current_epoch(post_state)):
cur_attestation = get_valid_attestation(spec, post_state, slot_to_attest)
block.body.attestations.append(cur_attestation)