`Misc` function rename

This commit is contained in:
Carl Beekhuizen 2019-06-30 20:58:02 +02:00
parent daa624e977
commit 0fd1d38417
No known key found for this signature in database
GPG Key ID: D05CA176D0020646
13 changed files with 97 additions and 95 deletions

View File

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

View File

@ -66,13 +66,13 @@
- [`is_slashable_attestation_data`](#is_slashable_attestation_data) - [`is_slashable_attestation_data`](#is_slashable_attestation_data)
- [`is_valid_merkle_branch`](#is_valid_merkle_branch) - [`is_valid_merkle_branch`](#is_valid_merkle_branch)
- [Misc](#misc) - [Misc](#misc)
- [`shuffle_index`](#shuffle_index) - [`compute_shuffle_index`](#compute_shuffle_index)
- [`compute_committee`](#compute_committee) - [`compute_committee`](#compute_committee)
- [`validate_indexed_attestation`](#validate_indexed_attestation) - [`compute_slot_epoch`](#compute_slot_epoch)
- [`slot_to_epoch`](#slot_to_epoch) - [`compute_epoch_start_slot`](#compute_epoch_start_slot)
- [`epoch_start_slot`](#epoch_start_slot)
- [`compute_activation_exit_epoch`](#compute_activation_exit_epoch) - [`compute_activation_exit_epoch`](#compute_activation_exit_epoch)
- [`bls_domain`](#bls_domain) - [`compute_bls_domain`](#compute_bls_domain)
- [`validate_indexed_attestation`](#validate_indexed_attestation)
- [Beacon state accessors](#beacon-state-accessors) - [Beacon state accessors](#beacon-state-accessors)
- [`get_current_epoch`](#get_current_epoch) - [`get_current_epoch`](#get_current_epoch)
- [`get_previous_epoch`](#get_previous_epoch) - [`get_previous_epoch`](#get_previous_epoch)
@ -662,10 +662,10 @@ def is_valid_merkle_branch(leaf: Hash, branch: Sequence[Hash], depth: uint64, in
### Misc ### Misc
#### `shuffle_index` #### `compute_shuffle_index`
```python ```python
def shuffle_index(index: ValidatorIndex, index_count: uint64, seed: Hash) -> ValidatorIndex: def compute_shuffle_index(index: ValidatorIndex, index_count: uint64, seed: Hash) -> ValidatorIndex:
""" """
Return the shuffled validator index corresponding to ``seed`` (and ``index_count``). Return the shuffled validator index corresponding to ``seed`` (and ``index_count``).
""" """
@ -698,7 +698,47 @@ def compute_committee(indices: Sequence[ValidatorIndex],
""" """
start = (len(indices) * index) // count start = (len(indices) * index) // count
end = (len(indices) * (index + 1)) // count end = (len(indices) * (index + 1)) // count
return [indices[shuffle_index(ValidatorIndex(i), len(indices), seed)] for i in range(start, end)] return [indices[compute_shuffle_index(ValidatorIndex(i), len(indices), seed)] for i in range(start, end)]
```
#### `compute_slot_epoch`
```python
def compute_slot_epoch(slot: Slot) -> Epoch:
"""
Return the epoch number of ``slot``.
"""
return Epoch(slot // SLOTS_PER_EPOCH)
```
#### `compute_epoch_start_slot`
```python
def compute_epoch_start_slot(epoch: Epoch) -> Slot:
"""
Return the start slot of ``epoch``.
"""
return Slot(epoch * SLOTS_PER_EPOCH)
```
#### `compute_activation_exit_epoch`
```python
def compute_activation_exit_epoch(epoch: Epoch) -> Epoch:
"""
Return the epoch during which validator activations and exits initiated in ``epoch`` take effect.
"""
return Epoch(epoch + 1 + ACTIVATION_EXIT_DELAY)
```
#### `compute_bls_domain`
```python
def compute_bls_domain(domain_type: uint64, fork_version: bytes=b'\x00' * 4) -> int:
"""
Return the BLS domain for the ``domain_type`` and ``fork_version``.
"""
return bytes_to_int(int_to_bytes(domain_type, length=4) + fork_version)
``` ```
#### `validate_indexed_attestation` #### `validate_indexed_attestation`
@ -734,46 +774,6 @@ def validate_indexed_attestation(state: BeaconState, indexed_attestation: Indexe
) )
``` ```
#### `slot_to_epoch`
```python
def slot_to_epoch(slot: Slot) -> Epoch:
"""
Return the epoch number of ``slot``.
"""
return Epoch(slot // SLOTS_PER_EPOCH)
```
#### `epoch_start_slot`
```python
def epoch_start_slot(epoch: Epoch) -> Slot:
"""
Return the start slot of ``epoch``.
"""
return Slot(epoch * SLOTS_PER_EPOCH)
```
#### `compute_activation_exit_epoch`
```python
def compute_activation_exit_epoch(epoch: Epoch) -> Epoch:
"""
Return the epoch during which validator activations and exits initiated in ``epoch`` take effect.
"""
return Epoch(epoch + 1 + ACTIVATION_EXIT_DELAY)
```
#### `bls_domain`
```python
def bls_domain(domain_type: uint64, fork_version: bytes=b'\x00' * 4) -> int:
"""
Return the BLS domain for the ``domain_type`` and ``fork_version``.
"""
return bytes_to_int(int_to_bytes(domain_type, length=4) + fork_version)
```
### Beacon state accessors ### Beacon state accessors
#### `get_current_epoch` #### `get_current_epoch`
@ -783,7 +783,7 @@ def get_current_epoch(state: BeaconState) -> Epoch:
""" """
Return the current epoch. Return the current epoch.
""" """
return slot_to_epoch(state.slot) return compute_slot_epoch(state.slot)
``` ```
#### `get_previous_epoch` #### `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 the block root at the start of a recent ``epoch``.
""" """
return get_block_root_at_slot(state, epoch_start_slot(epoch)) return get_block_root_at_slot(state, compute_epoch_start_slot(epoch))
``` ```
#### `get_block_root_at_slot` #### `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) committee_count = get_committee_count(state, data.target.epoch)
offset = (data.crosslink.shard + SHARD_COUNT - get_start_shard(state, data.target.epoch)) % SHARD_COUNT offset = (data.crosslink.shard + SHARD_COUNT - get_start_shard(state, data.target.epoch)) % SHARD_COUNT
return Slot(epoch_start_slot(data.target.epoch) + offset // (committee_count // SLOTS_PER_EPOCH)) return Slot(compute_epoch_start_slot(data.target.epoch) + offset // (committee_count // SLOTS_PER_EPOCH))
``` ```
#### `get_compact_committees_root` #### `get_compact_committees_root`
@ -1003,7 +1003,7 @@ def get_domain(state: BeaconState, domain_type: uint64, message_epoch: Epoch=Non
""" """
epoch = get_current_epoch(state) if message_epoch is None else message_epoch epoch = get_current_epoch(state) if message_epoch is None else message_epoch
fork_version = state.fork.previous_version if epoch < state.fork.epoch else state.fork.current_version fork_version = state.fork.previous_version if epoch < state.fork.epoch else state.fork.current_version
return bls_domain(domain_type, fork_version) return compute_bls_domain(domain_type, fork_version)
``` ```
#### `get_indexed_attestation` #### `get_indexed_attestation`
@ -1585,14 +1585,14 @@ def process_operations(state: BeaconState, body: BeaconBlockBody) -> None:
def process_proposer_slashing(state: BeaconState, proposer_slashing: ProposerSlashing) -> None: def process_proposer_slashing(state: BeaconState, proposer_slashing: ProposerSlashing) -> None:
proposer = state.validators[proposer_slashing.proposer_index] proposer = state.validators[proposer_slashing.proposer_index]
# Verify that the epoch is the same # Verify that the epoch is the same
assert slot_to_epoch(proposer_slashing.header_1.slot) == slot_to_epoch(proposer_slashing.header_2.slot) assert compute_slot_epoch(proposer_slashing.header_1.slot) == compute_slot_epoch(proposer_slashing.header_2.slot)
# But the headers are different # But the headers are different
assert proposer_slashing.header_1 != proposer_slashing.header_2 assert proposer_slashing.header_1 != proposer_slashing.header_2
# Check proposer is slashable # Check proposer is slashable
assert is_slashable_validator(proposer, get_current_epoch(state)) assert is_slashable_validator(proposer, get_current_epoch(state))
# Signatures are valid # Signatures are valid
for header in (proposer_slashing.header_1, proposer_slashing.header_2): for header in (proposer_slashing.header_1, proposer_slashing.header_2):
domain = get_domain(state, DOMAIN_BEACON_PROPOSER, slot_to_epoch(header.slot)) domain = get_domain(state, DOMAIN_BEACON_PROPOSER, compute_slot_epoch(header.slot))
assert bls_verify(proposer.pubkey, signing_root(header), header.signature, domain) assert bls_verify(proposer.pubkey, signing_root(header), header.signature, domain)
slash_validator(state, proposer_slashing.proposer_index) slash_validator(state, proposer_slashing.proposer_index)
@ -1677,8 +1677,9 @@ def process_deposit(state: BeaconState, deposit: Deposit) -> None:
if pubkey not in validator_pubkeys: if pubkey not in validator_pubkeys:
# Verify the deposit signature (proof of possession) for new validators. # Verify the deposit signature (proof of possession) for new validators.
# Note: The deposit contract does not check signatures. # Note: The deposit contract does not check signatures.
# Note: Deposits are valid across forks, hence the deposit domain is retrieved directly from `bls_domain` # Note: Deposits are valid across forks. thus the deposit domain is retrieved directly from `compute_bls_domain`
if not bls_verify(pubkey, signing_root(deposit.data), deposit.data.signature, bls_domain(DOMAIN_DEPOSIT)): if not bls_verify(pubkey, signing_root(deposit.data),
deposit.data.signature, compute_bls_domain(DOMAIN_DEPOSIT)):
return return
# Add validator and balance entries # Add validator and balance entries

View File

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

View File

@ -475,7 +475,7 @@ def process_chunk_challenge(state: BeaconState, challenge: CustodyChunkChallenge
# Verify the attestation # Verify the attestation
validate_indexed_attestation(state, get_indexed_attestation(state, challenge.attestation)) validate_indexed_attestation(state, get_indexed_attestation(state, challenge.attestation))
# Verify it is not too late to challenge # Verify it is not too late to challenge
assert slot_to_epoch(challenge.attestation.data.slot) >= get_current_epoch(state) - MAX_CHUNK_CHALLENGE_DELAY assert compute_slot_epoch(challenge.attestation.data.slot) >= get_current_epoch(state) - MAX_CHUNK_CHALLENGE_DELAY
responder = state.validators[challenge.responder_index] responder = state.validators[challenge.responder_index]
assert responder.exit_epoch >= get_current_epoch(state) - MAX_CHUNK_CHALLENGE_DELAY assert responder.exit_epoch >= get_current_epoch(state) - MAX_CHUNK_CHALLENGE_DELAY
# Verify the responder participated in the attestation # Verify the responder participated in the attestation
@ -516,7 +516,7 @@ For each `challenge` in `block.body.custody_bit_challenges`, run the following f
```python ```python
def process_bit_challenge(state: BeaconState, challenge: CustodyBitChallenge) -> None: def process_bit_challenge(state: BeaconState, challenge: CustodyBitChallenge) -> None:
attestation = challenge.attestation attestation = challenge.attestation
epoch = slot_to_epoch(attestation.data.slot) epoch = compute_slot_epoch(attestation.data.slot)
shard = attestation.data.crosslink.shard shard = attestation.data.crosslink.shard
# Verify challenge signature # 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``. Return the persistent committee for the given ``shard`` at the given ``slot``.
""" """
epoch = slot_to_epoch(slot) epoch = compute_slot_epoch(slot)
earlier_start_epoch = Epoch(epoch - (epoch % PERSISTENT_COMMITTEE_PERIOD) - PERSISTENT_COMMITTEE_PERIOD * 2) 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) 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), pubkey=bls_aggregate_pubkeys(pubkeys),
message_hash=data.shard_block_root, message_hash=data.shard_block_root,
signature=attestation.aggregate_signature, signature=attestation.aggregate_signature,
domain=get_domain(state, DOMAIN_SHARD_ATTESTER, slot_to_epoch(data.slot)) domain=get_domain(state, DOMAIN_SHARD_ATTESTER, compute_slot_epoch(data.slot))
) )
``` ```
@ -339,7 +339,7 @@ def is_valid_shard_block(beacon_blocks: Sequence[BeaconBlock],
pubkey=beacon_state.validators[proposer_index].pubkey, pubkey=beacon_state.validators[proposer_index].pubkey,
message_hash=signing_root(candidate), message_hash=signing_root(candidate),
signature=candidate.signature, signature=candidate.signature,
domain=get_domain(beacon_state, DOMAIN_SHARD_PROPOSER, slot_to_epoch(candidate.slot)), domain=get_domain(beacon_state, DOMAIN_SHARD_PROPOSER, compute_slot_epoch(candidate.slot)),
) )
return True return True
@ -403,11 +403,12 @@ def is_valid_beacon_attestation(shard: Shard,
None, None,
) )
assert previous_attestation is not None assert previous_attestation is not None
assert candidate.data.previous_attestation.epoch < slot_to_epoch(candidate.data.slot) assert candidate.data.previous_attestation.epoch < compute_slot_epoch(candidate.data.slot)
# Check crosslink data root # Check crosslink data root
start_epoch = beacon_state.crosslinks[shard].epoch start_epoch = beacon_state.crosslinks[shard].epoch
end_epoch = min(slot_to_epoch(candidate.data.slot) - CROSSLINK_LOOKBACK, start_epoch + MAX_EPOCHS_PER_CROSSLINK) end_epoch = min(compute_slot_epoch(candidate.data.slot) - CROSSLINK_LOOKBACK,
start_epoch + MAX_EPOCHS_PER_CROSSLINK)
blocks = [] blocks = []
for slot in range(start_epoch * SLOTS_PER_EPOCH, end_epoch * SLOTS_PER_EPOCH): for slot in range(start_epoch * SLOTS_PER_EPOCH, end_epoch * SLOTS_PER_EPOCH):
blocks.append(shard_blocks[slot]) blocks.append(shard_blocks[slot])

View File

@ -124,7 +124,7 @@ def compute_committee(header: BeaconBlockHeader,
maximal_later_committee = validator_memory.later_period_data.committee maximal_later_committee = validator_memory.later_period_data.committee
earlier_start_epoch = get_earlier_start_epoch(header.slot) earlier_start_epoch = get_earlier_start_epoch(header.slot)
later_start_epoch = get_later_start_epoch(header.slot) later_start_epoch = get_later_start_epoch(header.slot)
epoch = slot_to_epoch(header.slot) epoch = compute_slot_epoch(header.slot)
committee_count = max( committee_count = max(
earlier_validator_count // (SHARD_COUNT * TARGET_COMMITTEE_SIZE), 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, pubkey=group_public_key,
message_hash=hash_tree_root(shard_parent_block), message_hash=hash_tree_root(shard_parent_block),
signature=proof.shard_aggregate_signature, signature=proof.shard_aggregate_signature,
domain=get_domain(state, slot_to_epoch(shard_block.slot), DOMAIN_SHARD_ATTESTER), domain=get_domain(state, compute_slot_epoch(shard_block.slot), DOMAIN_SHARD_ATTESTER),
) )
``` ```

View File

@ -98,7 +98,7 @@ To submit a deposit:
* Pack the validator's [initialization parameters](#initialization) into `deposit_data`, a [`DepositData`](../core/0_beacon-chain.md#depositdata) SSZ object. * Pack the validator's [initialization parameters](#initialization) into `deposit_data`, a [`DepositData`](../core/0_beacon-chain.md#depositdata) SSZ object.
* Let `amount` be the amount in Gwei to be deposited by the validator where `MIN_DEPOSIT_AMOUNT <= amount <= MAX_EFFECTIVE_BALANCE`. * Let `amount` be the amount in Gwei to be deposited by the validator where `MIN_DEPOSIT_AMOUNT <= amount <= MAX_EFFECTIVE_BALANCE`.
* Set `deposit_data.amount = amount`. * Set `deposit_data.amount = amount`.
* Let `signature` be the result of `bls_sign` of the `signing_root(deposit_data)` with `domain=bls_domain(DOMAIN_DEPOSIT)`. (Deposits are valid regardless of fork version, `bls_domain` will default to zeroes there). * Let `signature` be the result of `bls_sign` of the `signing_root(deposit_data)` with `domain=compute_bls_domain(DOMAIN_DEPOSIT)`. (Deposits are valid regardless of fork version, `compute_bls_domain` will default to zeroes there).
* Send a transaction on the Ethereum 1.0 chain to `DEPOSIT_CONTRACT_ADDRESS` executing `def deposit(pubkey: bytes[48], withdrawal_credentials: bytes[32], signature: bytes[96])` along with a deposit of `amount` Gwei. * Send a transaction on the Ethereum 1.0 chain to `DEPOSIT_CONTRACT_ADDRESS` executing `def deposit(pubkey: bytes[48], withdrawal_credentials: bytes[32], signature: bytes[96])` along with a deposit of `amount` Gwei.
*Note*: Deposits made for the same `pubkey` are treated as for the same validator. A singular `Validator` will be added to `state.validators` with each additional deposit amount added to the validator's balance. A validator can only be activated when total deposits for the validator pubkey meet or exceed `MAX_EFFECTIVE_BALANCE`. *Note*: Deposits made for the same `pubkey` are treated as for the same validator. A singular `Validator` will be added to `state.validators` with each additional deposit amount added to the validator's balance. A validator can only be activated when total deposits for the validator pubkey meet or exceed `MAX_EFFECTIVE_BALANCE`.
@ -146,7 +146,7 @@ def get_committee_assignment(
assert epoch <= next_epoch assert epoch <= next_epoch
committees_per_slot = get_committee_count(state, epoch) // SLOTS_PER_EPOCH committees_per_slot = get_committee_count(state, epoch) // SLOTS_PER_EPOCH
start_slot = epoch_start_slot(epoch) start_slot = compute_epoch_start_slot(epoch)
for slot in range(start_slot, start_slot + SLOTS_PER_EPOCH): for slot in range(start_slot, start_slot + SLOTS_PER_EPOCH):
offset = committees_per_slot * (slot % SLOTS_PER_EPOCH) offset = committees_per_slot * (slot % SLOTS_PER_EPOCH)
slot_start_shard = (get_start_shard(state, epoch) + offset) % SHARD_COUNT slot_start_shard = (get_start_shard(state, epoch) + offset) % SHARD_COUNT
@ -210,10 +210,10 @@ Set `block.randao_reveal = epoch_signature` where `epoch_signature` is defined a
```python ```python
epoch_signature = bls_sign( epoch_signature = bls_sign(
privkey=validator.privkey, # privkey stored locally, not in state privkey=validator.privkey, # privkey stored locally, not in state
message_hash=hash_tree_root(slot_to_epoch(block.slot)), message_hash=hash_tree_root(compute_slot_epoch(block.slot)),
domain=get_domain( domain=get_domain(
fork=fork, # `fork` is the fork object at the slot `block.slot` fork=fork, # `fork` is the fork object at the slot `block.slot`
epoch=slot_to_epoch(block.slot), epoch=compute_slot_epoch(block.slot),
domain_type=DOMAIN_RANDAO, domain_type=DOMAIN_RANDAO,
) )
) )
@ -252,7 +252,7 @@ block_signature = bls_sign(
message_hash=signing_root(block), message_hash=signing_root(block),
domain=get_domain( domain=get_domain(
fork=fork, # `fork` is the fork object at the slot `block.slot` fork=fork, # `fork` is the fork object at the slot `block.slot`
epoch=slot_to_epoch(block.slot), epoch=compute_slot_epoch(block.slot),
domain_type=DOMAIN_BEACON_BLOCK, domain_type=DOMAIN_BEACON_BLOCK,
) )
) )
@ -307,7 +307,7 @@ Set `attestation_data.beacon_block_root = signing_root(head_block)`.
* Set `attestation_data.target_root = epoch_boundary_block_root` where `epoch_boundary_block_root` is the root of block at the most recent epoch boundary. * Set `attestation_data.target_root = epoch_boundary_block_root` where `epoch_boundary_block_root` is the root of block at the most recent epoch boundary.
*Note*: `epoch_boundary_block_root` can be looked up in the state using: *Note*: `epoch_boundary_block_root` can be looked up in the state using:
* Let `start_slot = epoch_start_slot(get_current_epoch(head_state))`. * Let `start_slot = compute_epoch_start_slot(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)`. * Let `epoch_boundary_block_root = signing_root(head_block) if start_slot == head_state.slot else get_block_root(state, start_slot)`.
##### Crosslink vote ##### Crosslink vote
@ -357,7 +357,7 @@ signed_attestation_data = bls_sign(
message_hash=attestation_message, message_hash=attestation_message,
domain=get_domain( domain=get_domain(
fork=fork, # `fork` is the fork object at the slot, `attestation_data.slot` fork=fork, # `fork` is the fork object at the slot, `attestation_data.slot`
epoch=slot_to_epoch(attestation_data.slot), epoch=compute_slot_epoch(attestation_data.slot),
domain_type=DOMAIN_ATTESTATION, domain_type=DOMAIN_ATTESTATION,
) )
) )
@ -376,7 +376,7 @@ To avoid "proposer slashings", a validator must not sign two conflicting [`Beaco
*In Phase 0, as long as the validator does not sign two different beacon blocks for the same epoch, the validator is safe against proposer slashings.* *In Phase 0, as long as the validator does not sign two different beacon blocks for the same epoch, the validator is safe against proposer slashings.*
Specifically, when signing a `BeaconBlock`, a validator should perform the following steps in the following order: 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=slot_to_epoch(block.slot)`. 1. Save a record to hard disk that a beacon block has been signed for the `epoch=compute_slot_epoch(block.slot)`.
2. Generate and broadcast the block. 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. 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.
@ -386,7 +386,7 @@ If the software crashes at some point within this routine, then when the validat
To avoid "attester slashings", a validator must not sign two conflicting [`AttestationData`](../core/0_beacon-chain.md#attestationdata) objects, i.e. two attestations that satisfy [`is_slashable_attestation_data`](../core/0_beacon-chain.md#is_slashable_attestation_data). To avoid "attester slashings", a validator must not sign two conflicting [`AttestationData`](../core/0_beacon-chain.md#attestationdata) objects, i.e. two attestations that satisfy [`is_slashable_attestation_data`](../core/0_beacon-chain.md#is_slashable_attestation_data).
Specifically, when signing an `Attestation`, a validator should perform the following steps in the following order: 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. `slot_to_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_slot_epoch(attestation_data.slot)`).
2. Generate and broadcast attestation. 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. 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

@ -10,7 +10,7 @@ from preset_loader import loader
def shuffling_case(seed, count): def shuffling_case(seed, count):
yield 'seed', '0x' + seed.hex() yield 'seed', '0x' + seed.hex()
yield 'count', count yield 'count', count
yield 'shuffled', [spec.shuffle_index(i, count, seed) for i in range(count)] yield 'shuffled', [spec.compute_shuffle_index(i, count, seed) for i in range(count)]
@to_tuple @to_tuple

View File

@ -15,7 +15,7 @@ def build_attestation_data(spec, state, slot, shard):
else: else:
block_root = spec.get_block_root_at_slot(state, slot) block_root = spec.get_block_root_at_slot(state, slot)
current_epoch_start_slot = spec.epoch_start_slot(spec.get_current_epoch(state)) current_epoch_start_slot = spec.compute_epoch_start_slot(spec.get_current_epoch(state))
if slot < current_epoch_start_slot: if slot < current_epoch_start_slot:
epoch_boundary_root = spec.get_block_root(state, spec.get_previous_epoch(state)) epoch_boundary_root = spec.get_block_root(state, spec.get_previous_epoch(state))
elif slot == current_epoch_start_slot: 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_epoch = state.current_justified_checkpoint.epoch
source_root = state.current_justified_checkpoint.root source_root = state.current_justified_checkpoint.root
if spec.slot_to_epoch(slot) == spec.get_current_epoch(state): if spec.compute_slot_epoch(slot) == spec.get_current_epoch(state):
parent_crosslink = state.current_crosslinks[shard] parent_crosslink = state.current_crosslinks[shard]
else: else:
parent_crosslink = state.previous_crosslinks[shard] parent_crosslink = state.previous_crosslinks[shard]
@ -38,11 +38,11 @@ def build_attestation_data(spec, state, slot, shard):
return spec.AttestationData( return spec.AttestationData(
beacon_block_root=block_root, beacon_block_root=block_root,
source=spec.Checkpoint(epoch=source_epoch, root=source_root), source=spec.Checkpoint(epoch=source_epoch, root=source_root),
target=spec.Checkpoint(epoch=spec.slot_to_epoch(slot), root=epoch_boundary_root), target=spec.Checkpoint(epoch=spec.compute_slot_epoch(slot), root=epoch_boundary_root),
crosslink=spec.Crosslink( crosslink=spec.Crosslink(
shard=shard, shard=shard,
start_epoch=parent_crosslink.end_epoch, start_epoch=parent_crosslink.end_epoch,
end_epoch=min(spec.slot_to_epoch(slot), parent_crosslink.end_epoch + spec.MAX_EPOCHS_PER_CROSSLINK), end_epoch=min(spec.compute_slot_epoch(slot), parent_crosslink.end_epoch + spec.MAX_EPOCHS_PER_CROSSLINK),
data_root=spec.Hash(), data_root=spec.Hash(),
parent_root=hash_tree_root(parent_crosslink), 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: if slot is None:
slot = state.slot slot = state.slot
epoch = spec.slot_to_epoch(slot) epoch = spec.compute_slot_epoch(slot)
epoch_start_shard = spec.get_start_shard(state, epoch) epoch_start_shard = spec.get_start_shard(state, epoch)
committees_per_slot = spec.get_committee_count(state, epoch) // spec.SLOTS_PER_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 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: if block.slot == state.slot:
proposer_index = spec.get_beacon_proposer_index(state) proposer_index = spec.get_beacon_proposer_index(state)
else: else:
if spec.slot_to_epoch(state.slot) + 1 > spec.slot_to_epoch(block.slot): if spec.compute_slot_epoch(state.slot) + 1 > spec.compute_slot_epoch(block.slot):
print("warning: block slot far away, and no proposer index manually given." print("warning: block slot far away, and no proposer index manually given."
" Signing block is slow due to transition for proposer index calculation.") " Signing block is slow due to transition for proposer index calculation.")
# use stub state to get proposer index of future slot # 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( block.body.randao_reveal = bls_sign(
privkey=privkey, privkey=privkey,
message_hash=hash_tree_root(spec.slot_to_epoch(block.slot)), message_hash=hash_tree_root(spec.compute_slot_epoch(block.slot)),
domain=spec.get_domain( domain=spec.get_domain(
state, state,
message_epoch=spec.slot_to_epoch(block.slot), message_epoch=spec.compute_slot_epoch(block.slot),
domain_type=spec.DOMAIN_RANDAO, domain_type=spec.DOMAIN_RANDAO,
) )
) )
@ -39,7 +39,7 @@ def sign_block(spec, state, block, proposer_index=None):
domain=spec.get_domain( domain=spec.get_domain(
state, state,
spec.DOMAIN_BEACON_PROPOSER, spec.DOMAIN_BEACON_PROPOSER,
spec.slot_to_epoch(block.slot))) spec.compute_slot_epoch(block.slot)))
def apply_empty_block(spec, state): def apply_empty_block(spec, state):

View File

@ -19,7 +19,7 @@ def build_deposit_data(spec, pubkey, privkey, amount, withdrawal_credentials, st
def sign_deposit_data(spec, deposit_data, privkey, state=None): def sign_deposit_data(spec, deposit_data, privkey, state=None):
if state is None: if state is None:
# Genesis # Genesis
domain = spec.bls_domain(spec.DOMAIN_DEPOSIT) domain = spec.compute_bls_domain(spec.DOMAIN_DEPOSIT)
else: else:
domain = spec.get_domain( domain = spec.get_domain(
state, state,

View File

@ -9,7 +9,7 @@ def run_process_just_and_fin(spec, state):
def get_shards_for_slot(spec, state, slot): def get_shards_for_slot(spec, state, slot):
epoch = spec.slot_to_epoch(slot) epoch = spec.compute_slot_epoch(slot)
epoch_start_shard = spec.get_start_shard(state, epoch) epoch_start_shard = spec.get_start_shard(state, epoch)
committees_per_slot = spec.get_committee_count(state, epoch) // spec.SLOTS_PER_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 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) total_balance = spec.get_total_active_balance(state)
remaining_balance = total_balance * 2 // 3 remaining_balance = total_balance * 2 // 3
start_slot = spec.epoch_start_slot(epoch) start_slot = spec.compute_epoch_start_slot(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):
for shard in get_shards_for_slot(spec, state, slot): 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). # 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: if remaining_balance < 0:
return return
committee = spec.get_crosslink_committee(state, spec.slot_to_epoch(slot), shard) committee = spec.get_crosslink_committee(state, spec.compute_slot_epoch(slot), shard)
# Create a bitfield filled with the given count per attestation, # Create a bitfield filled with the given count per attestation,
# exactly on the right-most part of the committee field. # 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): def put_checkpoints_in_block_roots(spec, state, checkpoints):
for c in checkpoints: for c in checkpoints:
state.block_roots[spec.epoch_start_slot(c.epoch) % spec.SLOTS_PER_HISTORICAL_ROOT] = c.root state.block_roots[spec.compute_epoch_start_slot(c.epoch) % spec.SLOTS_PER_HISTORICAL_ROOT] = c.root
def finalize_on_234(spec, state, epoch, sufficient_support): 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) 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:
slot_to_attest = post_state.slot - spec.MIN_ATTESTATION_INCLUSION_DELAY + 1 slot_to_attest = post_state.slot - spec.MIN_ATTESTATION_INCLUSION_DELAY + 1
if slot_to_attest >= spec.epoch_start_slot(spec.get_current_epoch(post_state)): if slot_to_attest >= spec.compute_epoch_start_slot(spec.get_current_epoch(post_state)):
cur_attestation = get_valid_attestation(spec, post_state, slot_to_attest) cur_attestation = get_valid_attestation(spec, post_state, slot_to_attest)
block.body.attestations.append(cur_attestation) block.body.attestations.append(cur_attestation)