Merge pull request #997 from ethereum/hwwhww/clean_up

PEP8-ish cleanup
This commit is contained in:
Danny Ryan 2019-04-26 08:42:36 -06:00 committed by GitHub
commit a146f257b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 22 deletions

View File

@ -5,10 +5,8 @@ import function_puller
def build_phase0_spec(sourcefile, outfile): def build_phase0_spec(sourcefile, outfile):
code_lines = [] code_lines = []
code_lines.append(""" code_lines.append("""
from typing import ( from typing import (
Any, Any,
Callable,
Dict, Dict,
List, List,
NewType, NewType,
@ -17,19 +15,16 @@ from typing import (
from eth2spec.utils.minimal_ssz import * from eth2spec.utils.minimal_ssz import *
from eth2spec.utils.bls_stub import * from eth2spec.utils.bls_stub import *
""") """)
for i in (1, 2, 3, 4, 8, 32, 48, 96): for i in (1, 2, 3, 4, 8, 32, 48, 96):
code_lines.append("def int_to_bytes%d(x): return x.to_bytes(%d, 'little')" % (i, i)) code_lines.append("def int_to_bytes%d(x): return x.to_bytes(%d, 'little')" % (i, i))
code_lines.append(""" code_lines.append("""
# stub, will get overwritten by real var # stub, will get overwritten by real var
SLOTS_PER_EPOCH = 64 SLOTS_PER_EPOCH = 64
def slot_to_epoch(x): return x // SLOTS_PER_EPOCH
Slot = NewType('Slot', int) # uint64 Slot = NewType('Slot', int) # uint64
Epoch = NewType('Epoch', int) # uint64 Epoch = NewType('Epoch', int) # uint64
Shard = NewType('Shard', int) # uint64 Shard = NewType('Shard', int) # uint64
@ -38,7 +33,6 @@ Gwei = NewType('Gwei', int) # uint64
Bytes32 = NewType('Bytes32', bytes) # bytes32 Bytes32 = NewType('Bytes32', bytes) # bytes32
BLSPubkey = NewType('BLSPubkey', bytes) # bytes48 BLSPubkey = NewType('BLSPubkey', bytes) # bytes48
BLSSignature = NewType('BLSSignature', bytes) # bytes96 BLSSignature = NewType('BLSSignature', bytes) # bytes96
Any = None
Store = None Store = None
""") """)

View File

@ -62,9 +62,10 @@ def get_spec(file_name: str) -> List[str]:
code_lines.append('') code_lines.append('')
for type_line in ssz_type: for type_line in ssz_type:
code_lines.append(' ' + type_line) code_lines.append(' ' + type_line)
code_lines.append('') code_lines.append('\n')
code_lines.append('ssz_types = [' + ', '.join([f'\'{ssz_type_name}\'' for (ssz_type_name, _) in type_defs]) + ']') code_lines.append('ssz_types = [' + ', '.join([f'\'{ssz_type_name}\'' for (ssz_type_name, _) in type_defs]) + ']')
code_lines.append('') code_lines.append('\n')
code_lines.append('def get_ssz_type_by_name(name: str) -> SSZType: return globals()[name]') code_lines.append('def get_ssz_type_by_name(name: str) -> SSZType:')
code_lines.append(' return globals()[name]')
code_lines.append('') code_lines.append('')
return code_lines return code_lines

View File

@ -687,7 +687,6 @@ def is_slashable_validator(validator: Validator, epoch: Epoch) -> bool:
Check if ``validator`` is slashable. Check if ``validator`` is slashable.
""" """
return validator.slashed is False and (validator.activation_epoch <= epoch < validator.withdrawable_epoch) return validator.slashed is False and (validator.activation_epoch <= epoch < validator.withdrawable_epoch)
``` ```
### `get_active_validator_indices` ### `get_active_validator_indices`
@ -1250,7 +1249,7 @@ def get_genesis_beacon_state(genesis_validator_deposits: List[Deposit],
process_deposit(state, deposit) process_deposit(state, deposit)
# Process genesis activations # Process genesis activations
for index, validator in enumerate(state.validator_registry): for validator in state.validator_registry:
if validator.effective_balance >= MAX_EFFECTIVE_BALANCE: if validator.effective_balance >= MAX_EFFECTIVE_BALANCE:
validator.activation_eligibility_epoch = GENESIS_EPOCH validator.activation_eligibility_epoch = GENESIS_EPOCH
validator.activation_epoch = GENESIS_EPOCH validator.activation_epoch = GENESIS_EPOCH
@ -1366,7 +1365,7 @@ def get_winning_crosslink_and_attesting_indices(state: BeaconState, shard: Shard
if hash_tree_root(state.current_crosslinks[shard]) in (c.previous_crosslink_root, hash_tree_root(c)) if hash_tree_root(state.current_crosslinks[shard]) in (c.previous_crosslink_root, hash_tree_root(c))
] ]
if len(candidate_crosslinks) == 0: if len(candidate_crosslinks) == 0:
return Crosslink(epoch=GENESIS_EPOCH, previous_crosslink_root=ZERO_HASH, crosslink_data_root=ZERO_HASH), [] return Crosslink(epoch=GENESIS_EPOCH), []
def get_attestations_for(crosslink: Crosslink) -> List[PendingAttestation]: def get_attestations_for(crosslink: Crosslink) -> List[PendingAttestation]:
return [a for a in shard_attestations if get_crosslink_from_attestation_data(state, a.data) == crosslink] return [a for a in shard_attestations if get_crosslink_from_attestation_data(state, a.data) == crosslink]
@ -1461,15 +1460,14 @@ def get_base_reward(state: BeaconState, index: ValidatorIndex) -> Gwei:
if adjusted_quotient == 0: if adjusted_quotient == 0:
return 0 return 0
return state.validator_registry[index].effective_balance // adjusted_quotient // BASE_REWARDS_PER_EPOCH return state.validator_registry[index].effective_balance // adjusted_quotient // BASE_REWARDS_PER_EPOCH
``` ```
```python ```python
def get_attestation_deltas(state: BeaconState) -> Tuple[List[Gwei], List[Gwei]]: def get_attestation_deltas(state: BeaconState) -> Tuple[List[Gwei], List[Gwei]]:
previous_epoch = get_previous_epoch(state) previous_epoch = get_previous_epoch(state)
total_balance = get_total_active_balance(state) total_balance = get_total_active_balance(state)
rewards = [0 for index in range(len(state.validator_registry))] rewards = [0 for _ in range(len(state.validator_registry))]
penalties = [0 for index in range(len(state.validator_registry))] penalties = [0 for _ in range(len(state.validator_registry))]
eligible_validator_indices = [ eligible_validator_indices = [
index for index, v in enumerate(state.validator_registry) index for index, v in enumerate(state.validator_registry)
if is_active_validator(v, previous_epoch) or (v.slashed and previous_epoch + 1 < v.withdrawable_epoch) if is_active_validator(v, previous_epoch) or (v.slashed and previous_epoch + 1 < v.withdrawable_epoch)
@ -1509,8 +1507,8 @@ def get_attestation_deltas(state: BeaconState) -> Tuple[List[Gwei], List[Gwei]]:
```python ```python
def get_crosslink_deltas(state: BeaconState) -> Tuple[List[Gwei], List[Gwei]]: def get_crosslink_deltas(state: BeaconState) -> Tuple[List[Gwei], List[Gwei]]:
rewards = [0 for index in range(len(state.validator_registry))] rewards = [0 for _ in range(len(state.validator_registry))]
penalties = [0 for index in range(len(state.validator_registry))] penalties = [0 for _ in range(len(state.validator_registry))]
for slot in range(get_epoch_start_slot(get_previous_epoch(state)), get_epoch_start_slot(get_current_epoch(state))): for slot in range(get_epoch_start_slot(get_previous_epoch(state)), get_epoch_start_slot(get_current_epoch(state))):
epoch = slot_to_epoch(slot) epoch = slot_to_epoch(slot)
for crosslink_committee, shard in get_crosslink_committees_at_slot(state, slot): for crosslink_committee, shard in get_crosslink_committees_at_slot(state, slot):

View File

@ -1,4 +1,10 @@
from typing import Callable, Dict, Tuple, Any from typing import (
Any,
Callable,
Dict,
Tuple,
)
TestCase = Dict[str, Any] TestCase = Dict[str, Any]
TestSuite = Dict[str, Any] TestSuite = Dict[str, Any]