Merge pull request #1241 from ethereum/rename_misc

Renaming of functions in the `Misc` section
This commit is contained in:
Danny Ryan 2019-06-30 19:21:10 -06:00 committed by GitHub
commit 155158f461
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 118 additions and 108 deletions

View File

@ -97,7 +97,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_epoch_of_slot(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

@ -64,15 +64,15 @@
- [`is_active_validator`](#is_active_validator) - [`is_active_validator`](#is_active_validator)
- [`is_slashable_validator`](#is_slashable_validator) - [`is_slashable_validator`](#is_slashable_validator)
- [`is_slashable_attestation_data`](#is_slashable_attestation_data) - [`is_slashable_attestation_data`](#is_slashable_attestation_data)
- [`is_valid_indexed_attestation`](#is_valid_indexed_attestation)
- [`is_valid_merkle_branch`](#is_valid_merkle_branch) - [`is_valid_merkle_branch`](#is_valid_merkle_branch)
- [Misc](#misc-1) - [Misc](#misc-1)
- [`shuffle_index`](#shuffle_index) - [`compute_shuffled_index`](#compute_shuffled_index)
- [`compute_committee`](#compute_committee) - [`compute_committee`](#compute_committee)
- [`validate_indexed_attestation`](#validate_indexed_attestation) - [`compute_epoch_of_slot`](#compute_epoch_of_slot)
- [`slot_to_epoch`](#slot_to_epoch) - [`compute_start_slot_of_epoch`](#compute_start_slot_of_epoch)
- [`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_domain`](#compute_domain)
- [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)
@ -645,6 +645,45 @@ def is_slashable_attestation_data(data_1: AttestationData, data_2: AttestationDa
) )
``` ```
#### `is_valid_indexed_attestation`
```python
def is_valid_indexed_attestation(state: BeaconState, indexed_attestation: IndexedAttestation) -> bool:
"""
Verify validity of ``indexed_attestation``.
"""
bit_0_indices = indexed_attestation.custody_bit_0_indices
bit_1_indices = indexed_attestation.custody_bit_1_indices
# Verify no index has custody bit equal to 1 [to be removed in phase 1]
if not len(bit_1_indices) == 0:
return False
# Verify max number of indices
if not len(bit_0_indices) + len(bit_1_indices) <= MAX_VALIDATORS_PER_COMMITTEE:
return False
# Verify index sets are disjoint
if not len(set(bit_0_indices).intersection(bit_1_indices)) == 0:
return False
# Verify indices are sorted
if not (bit_0_indices == sorted(bit_0_indices) and bit_1_indices == sorted(bit_1_indices)):
return False
# Verify aggregate signature
if not bls_verify_multiple(
pubkeys=[
bls_aggregate_pubkeys([state.validators[i].pubkey for i in bit_0_indices]),
bls_aggregate_pubkeys([state.validators[i].pubkey for i in bit_1_indices]),
],
message_hashes=[
hash_tree_root(AttestationDataAndCustodyBit(data=indexed_attestation.data, custody_bit=0b0)),
hash_tree_root(AttestationDataAndCustodyBit(data=indexed_attestation.data, custody_bit=0b1)),
],
signature=indexed_attestation.signature,
domain=get_domain(state, DOMAIN_ATTESTATION, indexed_attestation.data.target.epoch),
):
return False
return True
```
#### `is_valid_merkle_branch` #### `is_valid_merkle_branch`
```python ```python
@ -663,10 +702,10 @@ def is_valid_merkle_branch(leaf: Hash, branch: Sequence[Hash], depth: uint64, in
### Misc ### Misc
#### `shuffle_index` #### `compute_shuffled_index`
```python ```python
def shuffle_index(index: ValidatorIndex, index_count: uint64, seed: Hash) -> ValidatorIndex: def compute_shuffled_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``).
""" """
@ -699,56 +738,23 @@ 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_shuffled_index(ValidatorIndex(i), len(indices), seed)] for i in range(start, end)]
``` ```
#### `validate_indexed_attestation` #### `compute_epoch_of_slot`
```python ```python
def validate_indexed_attestation(state: BeaconState, indexed_attestation: IndexedAttestation) -> None: def compute_epoch_of_slot(slot: Slot) -> Epoch:
"""
Verify validity of ``indexed_attestation``.
"""
bit_0_indices = indexed_attestation.custody_bit_0_indices
bit_1_indices = indexed_attestation.custody_bit_1_indices
# Verify no index has custody bit equal to 1 [to be removed in phase 1]
assert len(bit_1_indices) == 0
# Verify max number of indices
assert len(bit_0_indices) + len(bit_1_indices) <= MAX_VALIDATORS_PER_COMMITTEE
# Verify index sets are disjoint
assert len(set(bit_0_indices).intersection(bit_1_indices)) == 0
# Verify indices are sorted
assert bit_0_indices == sorted(bit_0_indices) and bit_1_indices == sorted(bit_1_indices)
# Verify aggregate signature
assert bls_verify_multiple(
pubkeys=[
bls_aggregate_pubkeys([state.validators[i].pubkey for i in bit_0_indices]),
bls_aggregate_pubkeys([state.validators[i].pubkey for i in bit_1_indices]),
],
message_hashes=[
hash_tree_root(AttestationDataAndCustodyBit(data=indexed_attestation.data, custody_bit=0b0)),
hash_tree_root(AttestationDataAndCustodyBit(data=indexed_attestation.data, custody_bit=0b1)),
],
signature=indexed_attestation.signature,
domain=get_domain(state, DOMAIN_ATTESTATION, indexed_attestation.data.target.epoch),
)
```
#### `slot_to_epoch`
```python
def slot_to_epoch(slot: Slot) -> Epoch:
""" """
Return the epoch number of ``slot``. Return the epoch number of ``slot``.
""" """
return Epoch(slot // SLOTS_PER_EPOCH) return Epoch(slot // SLOTS_PER_EPOCH)
``` ```
#### `epoch_start_slot` #### `compute_start_slot_of_epoch`
```python ```python
def epoch_start_slot(epoch: Epoch) -> Slot: def compute_start_slot_of_epoch(epoch: Epoch) -> Slot:
""" """
Return the start slot of ``epoch``. Return the start slot of ``epoch``.
""" """
@ -765,12 +771,12 @@ def compute_activation_exit_epoch(epoch: Epoch) -> Epoch:
return Epoch(epoch + 1 + ACTIVATION_EXIT_DELAY) return Epoch(epoch + 1 + ACTIVATION_EXIT_DELAY)
``` ```
#### `bls_domain` #### `compute_domain`
```python ```python
def bls_domain(domain_type: uint64, fork_version: bytes=b'\x00' * 4) -> int: def compute_domain(domain_type: uint64, fork_version: bytes=b'\x00' * 4) -> int:
""" """
Return the BLS domain for the ``domain_type`` and ``fork_version``. Return the domain for the ``domain_type`` and ``fork_version``.
""" """
return bytes_to_int(int_to_bytes(domain_type, length=4) + fork_version) return bytes_to_int(int_to_bytes(domain_type, length=4) + fork_version)
``` ```
@ -784,7 +790,7 @@ def get_current_epoch(state: BeaconState) -> Epoch:
""" """
Return the current epoch. Return the current epoch.
""" """
return slot_to_epoch(state.slot) return compute_epoch_of_slot(state.slot)
``` ```
#### `get_previous_epoch` #### `get_previous_epoch`
@ -805,7 +811,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_start_slot_of_epoch(epoch))
``` ```
#### `get_block_root_at_slot` #### `get_block_root_at_slot`
@ -951,7 +957,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_start_slot_of_epoch(data.target.epoch) + offset // (committee_count // SLOTS_PER_EPOCH))
``` ```
#### `get_compact_committees_root` #### `get_compact_committees_root`
@ -1004,7 +1010,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_domain(domain_type, fork_version)
``` ```
#### `get_indexed_attestation` #### `get_indexed_attestation`
@ -1588,14 +1594,15 @@ 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_epoch_of_slot(proposer_slashing.header_1.slot)
== compute_epoch_of_slot(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_epoch_of_slot(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)
@ -1608,8 +1615,8 @@ def process_attester_slashing(state: BeaconState, attester_slashing: AttesterSla
attestation_1 = attester_slashing.attestation_1 attestation_1 = attester_slashing.attestation_1
attestation_2 = attester_slashing.attestation_2 attestation_2 = attester_slashing.attestation_2
assert is_slashable_attestation_data(attestation_1.data, attestation_2.data) assert is_slashable_attestation_data(attestation_1.data, attestation_2.data)
validate_indexed_attestation(state, attestation_1) assert is_valid_indexed_attestation(state, attestation_1)
validate_indexed_attestation(state, attestation_2) assert is_valid_indexed_attestation(state, attestation_2)
slashed_any = False slashed_any = False
attesting_indices_1 = attestation_1.custody_bit_0_indices + attestation_1.custody_bit_1_indices attesting_indices_1 = attestation_1.custody_bit_0_indices + attestation_1.custody_bit_1_indices
@ -1655,7 +1662,7 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None:
assert data.crosslink.data_root == Hash() # [to be removed in phase 1] assert data.crosslink.data_root == Hash() # [to be removed in phase 1]
# Check signature # Check signature
validate_indexed_attestation(state, get_indexed_attestation(state, attestation)) assert is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))
``` ```
##### Deposits ##### Deposits
@ -1680,8 +1687,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_domain`
if not bls_verify(pubkey, signing_root(deposit.data), deposit.data.signature, bls_domain(DOMAIN_DEPOSIT)): domain = compute_domain(DOMAIN_DEPOSIT)
if not bls_verify(pubkey, signing_root(deposit.data), deposit.data.signature, domain):
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_start_slot_of_epoch(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_start_slot_of_epoch(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 arrives # Attestations cannot be from future epochs. If they are, delay consideration until the epoch arrives
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_start_slot_of_epoch(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_start_slot_of_epoch(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]
@ -205,7 +205,7 @@ def on_attestation(store: Store, attestation: Attestation) -> None:
# Get state at the `target` to validate attestation and calculate the committees # Get state at the `target` to validate attestation and calculate the committees
indexed_attestation = get_indexed_attestation(target_state, attestation) indexed_attestation = get_indexed_attestation(target_state, attestation)
validate_indexed_attestation(target_state, indexed_attestation) assert is_valid_indexed_attestation(target_state, indexed_attestation)
# Update latest messages # Update latest messages
for i in indexed_attestation.custody_bit_0_indices + indexed_attestation.custody_bit_1_indices: for i in indexed_attestation.custody_bit_0_indices + indexed_attestation.custody_bit_1_indices:

View File

@ -474,9 +474,10 @@ For each `challenge` in `block.body.custody_chunk_challenges`, run the following
```python ```python
def process_chunk_challenge(state: BeaconState, challenge: CustodyChunkChallenge) -> None: def process_chunk_challenge(state: BeaconState, challenge: CustodyChunkChallenge) -> None:
# Verify the attestation # Verify the attestation
validate_indexed_attestation(state, get_indexed_attestation(state, challenge.attestation)) assert is_valid_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_epoch_of_slot(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
@ -517,7 +518,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_epoch_of_slot(attestation.data.slot)
shard = attestation.data.crosslink.shard shard = attestation.data.crosslink.shard
# Verify challenge signature # Verify challenge signature
@ -527,7 +528,7 @@ def process_bit_challenge(state: BeaconState, challenge: CustodyBitChallenge) ->
# Verify challenger is slashable # Verify challenger is slashable
assert is_slashable_validator(challenger, get_current_epoch(state)) assert is_slashable_validator(challenger, get_current_epoch(state))
# Verify attestation # Verify attestation
validate_indexed_attestation(state, get_indexed_attestation(state, attestation)) assert is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))
# Verify attestation is eligible for challenging # Verify attestation is eligible for challenging
responder = state.validators[challenge.responder_index] responder = state.validators[challenge.responder_index]
assert epoch + responder.max_reveal_lateness <= get_reveal_period(state, challenge.responder_index) assert epoch + responder.max_reveal_lateness <= get_reveal_period(state, challenge.responder_index)

View File

@ -164,7 +164,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_epoch_of_slot(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)
@ -241,7 +241,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_epoch_of_slot(data.slot))
) )
``` ```
@ -340,7 +340,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_epoch_of_slot(candidate.slot)),
) )
return True return True
@ -404,11 +404,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_epoch_of_slot(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_epoch_of_slot(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_epoch_of_slot(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_epoch_of_slot(shard_block.slot), DOMAIN_SHARD_ATTESTER),
) )
``` ```

View File

@ -7,7 +7,7 @@ A BLS compressed-hash to G2.
```yaml ```yaml
input: input:
message: bytes32, message: bytes32,
domain: uint64 -- the BLS domain domain: uint64 -- the domain
output: List[bytes48] -- length of two output: List[bytes48] -- length of two
``` ```

View File

@ -7,7 +7,7 @@ A BLS uncompressed-hash to G2.
```yaml ```yaml
input: input:
message: bytes32 message: bytes32
domain: uint64 -- the BLS domain domain: uint64 -- the domain
output: List[List[bytes48]] -- 3 lists, each a length of two output: List[List[bytes48]] -- 3 lists, each a length of two
``` ```

View File

@ -8,7 +8,7 @@ Message signing with BLS should produce a signature.
input: input:
privkey: bytes32 -- the private key used for signing privkey: bytes32 -- the private key used for signing
message: bytes32 -- input message to sign (a hash) message: bytes32 -- input message to sign (a hash)
domain: uint64 -- the BLS domain domain: uint64 -- the domain
output: bytes96 -- expected signature output: bytes96 -- expected signature
``` ```

View File

@ -99,7 +99,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_domain(DOMAIN_DEPOSIT)`. (Deposits are valid regardless of fork version, `compute_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(state: BeaconState,
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_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):
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_epoch_of_slot(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_epoch_of_slot(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_epoch_of_slot(block.slot),
domain_type=DOMAIN_BEACON_BLOCK, domain_type=DOMAIN_BEACON_BLOCK,
) )
) )
@ -308,7 +308,7 @@ Set `attestation_data.beacon_block_root = signing_root(head_block)`.
*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_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)`. - 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
@ -358,7 +358,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_epoch_of_slot(attestation_data.slot),
domain_type=DOMAIN_ATTESTATION, domain_type=DOMAIN_ATTESTATION,
) )
) )
@ -378,7 +378,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: 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_epoch_of_slot(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.
@ -389,7 +389,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: 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_epoch_of_slot(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', [int(spec.shuffled_index(i, count, seed)) for i in range(count)] yield 'shuffled', [int(spec.compute_shuffled_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_start_slot_of_epoch(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_epoch_of_slot(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_epoch_of_slot(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_epoch_of_slot(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_epoch_of_slot(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_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." 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_epoch_of_slot(block.slot)),
domain=spec.get_domain( domain=spec.get_domain(
state, state,
message_epoch=spec.slot_to_epoch(block.slot), message_epoch=spec.compute_epoch_of_slot(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_epoch_of_slot(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_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_epoch_of_slot(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_start_slot_of_epoch(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_epoch_of_slot(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_start_slot_of_epoch(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_start_slot_of_epoch(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)