PR feedback
This commit is contained in:
parent
0be2b8e620
commit
6d5e4da3e0
|
@ -70,7 +70,7 @@
|
|||
- [`compute_committee`](#compute_committee)
|
||||
- [`validate_indexed_attestation`](#validate_indexed_attestation)
|
||||
- [`slot_to_epoch`](#slot_to_epoch)
|
||||
- [`epoch_first_slot`](#epoch_first_slot)
|
||||
- [`epoch_start_slot`](#epoch_start_slot)
|
||||
- [`delayed_activation_exit_epoch`](#delayed_activation_exit_epoch)
|
||||
- [`bls_domain`](#bls_domain)
|
||||
- [Beacon state getters](#beacon-state-getters)
|
||||
|
@ -86,7 +86,7 @@
|
|||
- [`get_crosslink_committee`](#get_crosslink_committee)
|
||||
- [`get_start_shard`](#get_start_shard)
|
||||
- [`get_shard_delta`](#get_shard_delta)
|
||||
- [`get_proposer_index`](#get_proposer_index)
|
||||
- [`get_beacon_proposer_index`](#get_beacon_proposer_index)
|
||||
- [`get_attestation_data_slot`](#get_attestation_data_slot)
|
||||
- [`get_compact_committees_root`](#get_compact_committees_root)
|
||||
- [`get_total_balance`](#get_total_balance)
|
||||
|
@ -541,7 +541,7 @@ class BeaconState(Container):
|
|||
#### `integer_squareroot`
|
||||
|
||||
```python
|
||||
def integer_squareroot(n: uint64) -> uint64:
|
||||
def integer_squareroot(n: int) -> int:
|
||||
"""
|
||||
Return the largest integer ``x`` such that ``x**2 <= n``.
|
||||
"""
|
||||
|
@ -558,7 +558,7 @@ def integer_squareroot(n: uint64) -> uint64:
|
|||
```python
|
||||
def xor(bytes1: Bytes32, bytes2: Bytes32) -> Bytes32:
|
||||
"""
|
||||
Return the exclusive or of two 32-byte strings.
|
||||
Return the exclusive-or of two 32-byte strings.
|
||||
"""
|
||||
return Bytes32(a ^ b for a, b in zip(bytes1, bytes2))
|
||||
```
|
||||
|
@ -744,12 +744,12 @@ def slot_to_epoch(slot: Slot) -> Epoch:
|
|||
return Epoch(slot // SLOTS_PER_EPOCH)
|
||||
```
|
||||
|
||||
#### `epoch_first_slot`
|
||||
#### `epoch_start_slot`
|
||||
|
||||
```python
|
||||
def epoch_first_slot(epoch: Epoch) -> Slot:
|
||||
def epoch_start_slot(epoch: Epoch) -> Slot:
|
||||
"""
|
||||
Return the first slot of ``epoch``.
|
||||
Return the start slot of ``epoch``.
|
||||
"""
|
||||
return Slot(epoch * SLOTS_PER_EPOCH)
|
||||
```
|
||||
|
@ -759,7 +759,7 @@ def epoch_first_slot(epoch: Epoch) -> Slot:
|
|||
```python
|
||||
def delayed_activation_exit_epoch(epoch: Epoch) -> Epoch:
|
||||
"""
|
||||
Return the epoch where validator activations and exits initiated in ``epoch`` take effect.
|
||||
Return the epoch during which validator activations and exits initiated in ``epoch`` take effect.
|
||||
"""
|
||||
return Epoch(epoch + 1 + ACTIVATION_EXIT_DELAY)
|
||||
```
|
||||
|
@ -802,9 +802,9 @@ def get_previous_epoch(state: BeaconState) -> Epoch:
|
|||
```python
|
||||
def get_block_root(state: BeaconState, epoch: Epoch) -> Hash:
|
||||
"""
|
||||
Return the block root at a recent ``epoch``.
|
||||
Return the block root at the start of a recent ``epoch``.
|
||||
"""
|
||||
return get_block_root_at_slot(state, epoch_first_slot(epoch))
|
||||
return get_block_root_at_slot(state, epoch_start_slot(epoch))
|
||||
```
|
||||
|
||||
#### `get_block_root_at_slot`
|
||||
|
@ -869,10 +869,11 @@ def get_committee_count(state: BeaconState, epoch: Epoch) -> int:
|
|||
"""
|
||||
Return the number of committees at ``epoch``.
|
||||
"""
|
||||
return max(1, min(
|
||||
committees_per_slot = max(1, min(
|
||||
SHARD_COUNT // SLOTS_PER_EPOCH,
|
||||
len(get_active_validator_indices(state, epoch)) // SLOTS_PER_EPOCH // TARGET_COMMITTEE_SIZE,
|
||||
)) * SLOTS_PER_EPOCH
|
||||
))
|
||||
return committees_per_slot * SLOTS_PER_EPOCH
|
||||
```
|
||||
|
||||
#### `get_crosslink_committee`
|
||||
|
@ -909,19 +910,19 @@ def get_start_shard(state: BeaconState, epoch: Epoch) -> Shard:
|
|||
#### `get_shard_delta`
|
||||
|
||||
```python
|
||||
def get_shard_delta(state: BeaconState, epoch: Epoch) -> Shard:
|
||||
def get_shard_delta(state: BeaconState, epoch: Epoch) -> int:
|
||||
"""
|
||||
Return the number of shards to increment ``state.start_shard`` at ``epoch``.
|
||||
"""
|
||||
return Shard(min(get_committee_count(state, epoch), SHARD_COUNT - SHARD_COUNT // SLOTS_PER_EPOCH))
|
||||
return min(get_committee_count(state, epoch), SHARD_COUNT - SHARD_COUNT // SLOTS_PER_EPOCH)
|
||||
```
|
||||
|
||||
#### `get_proposer_index`
|
||||
#### `get_beacon_proposer_index`
|
||||
|
||||
```python
|
||||
def get_proposer_index(state: BeaconState) -> ValidatorIndex:
|
||||
def get_beacon_proposer_index(state: BeaconState) -> ValidatorIndex:
|
||||
"""
|
||||
Return the beacon proposer index at the current epoch.
|
||||
Return the beacon proposer index at the current slot.
|
||||
"""
|
||||
epoch = get_current_epoch(state)
|
||||
committees_per_slot = get_committee_count(state, epoch) // SLOTS_PER_EPOCH
|
||||
|
@ -949,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(epoch_first_slot(data.target.epoch) + offset // (committee_count // SLOTS_PER_EPOCH))
|
||||
return Slot(epoch_start_slot(data.target.epoch) + offset // (committee_count // SLOTS_PER_EPOCH))
|
||||
```
|
||||
|
||||
#### `get_compact_committees_root`
|
||||
|
@ -1102,7 +1103,7 @@ def slash_validator(state: BeaconState,
|
|||
decrease_balance(state, slashed_index, validator.effective_balance // MIN_SLASHING_PENALTY_QUOTIENT)
|
||||
|
||||
# Apply proposer and whistleblower rewards
|
||||
proposer_index = get_proposer_index(state)
|
||||
proposer_index = get_beacon_proposer_index(state)
|
||||
if whistleblower_index is None:
|
||||
whistleblower_index = proposer_index
|
||||
whistleblower_reward = Gwei(validator.effective_balance // WHISTLEBLOWER_REWARD_QUOTIENT)
|
||||
|
@ -1190,7 +1191,7 @@ def process_slots(state: BeaconState, slot: Slot) -> None:
|
|||
assert state.slot <= slot
|
||||
while state.slot < slot:
|
||||
process_slot(state)
|
||||
# Process epoch on the first slot of the next epoch
|
||||
# Process epoch on the start slot of the next epoch
|
||||
if (state.slot + 1) % SLOTS_PER_EPOCH == 0:
|
||||
process_epoch(state)
|
||||
state.slot += Slot(1)
|
||||
|
@ -1529,7 +1530,7 @@ def process_block_header(state: BeaconState, block: BeaconBlock) -> None:
|
|||
body_root=hash_tree_root(block.body),
|
||||
)
|
||||
# Verify proposer is not slashed
|
||||
proposer = state.validators[get_proposer_index(state)]
|
||||
proposer = state.validators[get_beacon_proposer_index(state)]
|
||||
assert not proposer.slashed
|
||||
# Verify proposer signature
|
||||
assert bls_verify(proposer.pubkey, signing_root(block), block.signature, get_domain(state, DOMAIN_BEACON_PROPOSER))
|
||||
|
@ -1541,7 +1542,7 @@ def process_block_header(state: BeaconState, block: BeaconBlock) -> None:
|
|||
def process_randao(state: BeaconState, body: BeaconBlockBody) -> None:
|
||||
epoch = get_current_epoch(state)
|
||||
# Verify RANDAO reveal
|
||||
proposer = state.validators[get_proposer_index(state)]
|
||||
proposer = state.validators[get_beacon_proposer_index(state)]
|
||||
assert bls_verify(proposer.pubkey, hash_tree_root(epoch), body.randao_reveal, get_domain(state, DOMAIN_RANDAO))
|
||||
# Mix in RANDAO reveal
|
||||
mix = xor(get_randao_mix(state, epoch), hash(body.randao_reveal))
|
||||
|
@ -1632,7 +1633,7 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None:
|
|||
data=data,
|
||||
aggregation_bits=attestation.aggregation_bits,
|
||||
inclusion_delay=state.slot - attestation_slot,
|
||||
proposer_index=get_proposer_index(state),
|
||||
proposer_index=get_beacon_proposer_index(state),
|
||||
)
|
||||
|
||||
if data.target.epoch == get_current_epoch(state):
|
||||
|
@ -1744,7 +1745,7 @@ def process_transfer(state: BeaconState, transfer: Transfer) -> None:
|
|||
# Process the transfer
|
||||
decrease_balance(state, transfer.sender, transfer.amount + transfer.fee)
|
||||
increase_balance(state, transfer.recipient, transfer.amount)
|
||||
increase_balance(state, get_proposer_index(state), transfer.fee)
|
||||
increase_balance(state, get_beacon_proposer_index(state), transfer.fee)
|
||||
# Verify balances are not dust
|
||||
assert not (0 < state.balances[transfer.sender] < MIN_DEPOSIT_AMOUNT)
|
||||
assert not (0 < state.balances[transfer.recipient] < MIN_DEPOSIT_AMOUNT)
|
||||
|
|
|
@ -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 = epoch_first_slot(store.justified_checkpoint.epoch)
|
||||
justified_slot = epoch_start_slot(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 > epoch_first_slot(store.finalized_checkpoint.epoch)
|
||||
assert block.slot > epoch_start_slot(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 arrivesr
|
||||
base_state = store.block_states[target.root].copy()
|
||||
assert store.time >= base_state.genesis_time + epoch_first_slot(target.epoch) * SECONDS_PER_SLOT
|
||||
assert store.time >= base_state.genesis_time + epoch_start_slot(target.epoch) * SECONDS_PER_SLOT
|
||||
|
||||
# Store target checkpoint state if not yet seen
|
||||
if target not in store.checkpoint_states:
|
||||
process_slots(base_state, epoch_first_slot(target.epoch))
|
||||
process_slots(base_state, epoch_start_slot(target.epoch))
|
||||
store.checkpoint_states[target] = base_state
|
||||
target_state = store.checkpoint_states[target]
|
||||
|
||||
|
|
|
@ -382,7 +382,7 @@ def process_custody_key_reveal(state: BeaconState, reveal: CustodyKeyReveal) ->
|
|||
revealer.next_custody_reveal_period += 1
|
||||
|
||||
# Reward Block Preposer
|
||||
proposer_index = get_proposer_index(state)
|
||||
proposer_index = get_beacon_proposer_index(state)
|
||||
increase_balance(
|
||||
state,
|
||||
proposer_index,
|
||||
|
@ -452,7 +452,7 @@ def process_early_derived_secret_reveal(state: BeaconState, reveal: EarlyDerived
|
|||
)
|
||||
|
||||
# Apply penalty
|
||||
proposer_index = get_proposer_index(state)
|
||||
proposer_index = get_beacon_proposer_index(state)
|
||||
whistleblower_index = reveal.masker_index
|
||||
whistleblowing_reward = Gwei(penalty // WHISTLEBLOWER_REWARD_QUOTIENT)
|
||||
proposer_reward = Gwei(whistleblowing_reward // PROPOSER_REWARD_QUOTIENT)
|
||||
|
@ -493,7 +493,7 @@ def process_chunk_challenge(state: BeaconState, challenge: CustodyChunkChallenge
|
|||
# Add new chunk challenge record
|
||||
new_record = CustodyChunkChallengeRecord(
|
||||
challenge_index=state.custody_challenge_index,
|
||||
challenger_index=get_proposer_index(state),
|
||||
challenger_index=get_beacon_proposer_index(state),
|
||||
responder_index=challenge.responder_index,
|
||||
inclusion_epoch=get_current_epoch(state),
|
||||
data_root=challenge.attestation.data.crosslink.data_root,
|
||||
|
@ -610,7 +610,7 @@ def process_chunk_challenge_response(state: BeaconState,
|
|||
records = state.custody_chunk_challenge_records
|
||||
records[records.index(challenge)] = CustodyChunkChallengeRecord()
|
||||
# Reward the proposer
|
||||
proposer_index = get_proposer_index(state)
|
||||
proposer_index = get_beacon_proposer_index(state)
|
||||
increase_balance(state, proposer_index, Gwei(get_base_reward(state, proposer_index) // MINOR_REWARD_QUOTIENT))
|
||||
```
|
||||
|
||||
|
|
|
@ -146,7 +146,7 @@ def get_committee_assignment(
|
|||
assert epoch <= next_epoch
|
||||
|
||||
committees_per_slot = get_committee_count(state, epoch) // SLOTS_PER_EPOCH
|
||||
start_slot = epoch_first_slot(epoch)
|
||||
start_slot = epoch_start_slot(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
|
||||
|
@ -162,7 +162,7 @@ A validator can use the following function to see if they are supposed to propos
|
|||
```python
|
||||
def is_proposer(state: BeaconState,
|
||||
validator_index: ValidatorIndex) -> bool:
|
||||
return get_proposer_index(state) == validator_index
|
||||
return get_beacon_proposer_index(state) == validator_index
|
||||
```
|
||||
|
||||
*Note*: To see if a validator is assigned to propose during the slot, the beacon state must be in the epoch in question. At the epoch boundaries, the validator must run an epoch transition into the epoch to successfully check the proposal assignment of the first slot.
|
||||
|
@ -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.
|
||||
|
||||
*Note*: `epoch_boundary_block_root` can be looked up in the state using:
|
||||
* Let `start_slot = epoch_first_slot(get_current_epoch(head_state))`.
|
||||
* Let `start_slot = 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)`.
|
||||
|
||||
##### Crosslink vote
|
||||
|
|
|
@ -15,15 +15,15 @@ def build_attestation_data(spec, state, slot, shard):
|
|||
else:
|
||||
block_root = spec.get_block_root_at_slot(state, slot)
|
||||
|
||||
current_epoch_first_slot = spec.epoch_first_slot(spec.get_current_epoch(state))
|
||||
if slot < current_epoch_first_slot:
|
||||
current_epoch_start_slot = spec.epoch_start_slot(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_first_slot:
|
||||
elif slot == current_epoch_start_slot:
|
||||
epoch_boundary_root = block_root
|
||||
else:
|
||||
epoch_boundary_root = spec.get_block_root(state, spec.get_current_epoch(state))
|
||||
|
||||
if slot < current_epoch_first_slot:
|
||||
if slot < current_epoch_start_slot:
|
||||
source_epoch = state.previous_justified_checkpoint.epoch
|
||||
source_root = state.previous_justified_checkpoint.root
|
||||
else:
|
||||
|
|
|
@ -12,7 +12,7 @@ def sign_block(spec, state, block, proposer_index=None):
|
|||
|
||||
if proposer_index is None:
|
||||
if block.slot == state.slot:
|
||||
proposer_index = spec.get_proposer_index(state)
|
||||
proposer_index = spec.get_beacon_proposer_index(state)
|
||||
else:
|
||||
if spec.slot_to_epoch(state.slot) + 1 > spec.slot_to_epoch(block.slot):
|
||||
print("warning: block slot far away, and no proposer index manually given."
|
||||
|
@ -20,7 +20,7 @@ def sign_block(spec, state, block, proposer_index=None):
|
|||
# use stub state to get proposer index of future slot
|
||||
stub_state = deepcopy(state)
|
||||
spec.process_slots(stub_state, block.slot)
|
||||
proposer_index = spec.get_proposer_index(stub_state)
|
||||
proposer_index = spec.get_beacon_proposer_index(stub_state)
|
||||
|
||||
privkey = privkeys[proposer_index]
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ def run_attester_slashing_processing(spec, state, attester_slashing, valid=True)
|
|||
+ attester_slashing.attestation_1.custody_bit_1_indices
|
||||
)
|
||||
|
||||
proposer_index = spec.get_proposer_index(state)
|
||||
proposer_index = spec.get_beacon_proposer_index(state)
|
||||
pre_proposer_balance = get_balance(state, proposer_index)
|
||||
pre_slashings = {slashed_index: get_balance(state, slashed_index) for slashed_index in slashed_indices}
|
||||
pre_withdrawalable_epochs = {
|
||||
|
|
|
@ -75,7 +75,7 @@ def test_proposer_slashed(spec, state):
|
|||
# use stub state to get proposer index of next slot
|
||||
stub_state = deepcopy(state)
|
||||
next_slot(spec, stub_state)
|
||||
proposer_index = spec.get_proposer_index(stub_state)
|
||||
proposer_index = spec.get_beacon_proposer_index(stub_state)
|
||||
|
||||
# set proposer to slashed
|
||||
state.validators[proposer_index].slashed = True
|
||||
|
|
|
@ -21,7 +21,7 @@ def run_transfer_processing(spec, state, transfer, valid=True):
|
|||
yield 'post', None
|
||||
return
|
||||
|
||||
proposer_index = spec.get_proposer_index(state)
|
||||
proposer_index = spec.get_beacon_proposer_index(state)
|
||||
pre_transfer_sender_balance = state.balances[transfer.sender]
|
||||
pre_transfer_recipient_balance = state.balances[transfer.recipient]
|
||||
pre_transfer_proposer_balance = state.balances[proposer_index]
|
||||
|
|
|
@ -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.epoch_first_slot(epoch)
|
||||
start_slot = spec.epoch_start_slot(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).
|
||||
|
@ -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.epoch_first_slot(c.epoch) % spec.SLOTS_PER_HISTORICAL_ROOT] = c.root
|
||||
state.block_roots[spec.epoch_start_slot(c.epoch) % spec.SLOTS_PER_HISTORICAL_ROOT] = c.root
|
||||
|
||||
|
||||
def finalize_on_234(spec, state, epoch, sufficient_support):
|
||||
|
|
|
@ -214,7 +214,7 @@ def test_attester_slashing(spec, state):
|
|||
# lost whistleblower reward
|
||||
assert get_balance(state, validator_index) < get_balance(pre_state, validator_index)
|
||||
|
||||
proposer_index = spec.get_proposer_index(state)
|
||||
proposer_index = spec.get_beacon_proposer_index(state)
|
||||
# gained whistleblower reward
|
||||
assert (
|
||||
get_balance(state, proposer_index) >
|
||||
|
|
|
@ -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.epoch_first_slot(spec.get_current_epoch(post_state)):
|
||||
if slot_to_attest >= spec.epoch_start_slot(spec.get_current_epoch(post_state)):
|
||||
cur_attestation = get_valid_attestation(spec, post_state, slot_to_attest)
|
||||
block.body.attestations.append(cur_attestation)
|
||||
|
||||
|
|
Loading…
Reference in New Issue