Minor edits + PEP8

This commit is contained in:
Carl Beekhuizen 2019-05-21 12:41:24 +02:00
parent 6b062405c9
commit 24c4d21d5e
No known key found for this signature in database
GPG Key ID: D05CA176D0020646
15 changed files with 97 additions and 52 deletions

View File

@ -52,11 +52,11 @@ citest: $(PY_SPEC_ALL_TARGETS)
python -m pytest --junitxml=test-reports/eth2spec/test_results_phase0.xml tests/phase1
install_lint:
cd $(PY_SPEC_DIR); python3 -m venv venv; . venv/bin/activate; pip3 install flake8==3.5.0
cd $(PY_SPEC_DIR); python3 -m venv venv; . venv/bin/activate; pip3 install flake8==3.7.0
lint: $(PY_SPEC_ALL_TARGETS)
cd $(PY_SPEC_DIR); . venv/bin/activate; \
flake8 --max-line-length=120 ./eth2spec;
flake8 --max-line-length=120 --per-file-ignores='./tests/*:F821,F403,F401,F405' ./eth2spec ./tests;
# "make pyspec" to create the pyspec for all phases.
pyspec: $(PY_SPEC_ALL_TARGETS)

View File

@ -50,7 +50,6 @@ from eth2spec.utils.bls_stub import (
)
from eth2spec.utils.hash_function import hash
import math
'''
NEW_TYPES = {
'Slot': 'int',

View File

@ -962,7 +962,7 @@ def get_total_balance(state: BeaconState, indices: List[ValidatorIndex]) -> Gwei
```python
def get_domain(state: BeaconState,
domain_type: int,
message_epoch: int=None) -> int:
message_epoch: int = None) -> int:
"""
Return the signature domain (fork version concatenated with domain type) of a message.
"""
@ -1150,7 +1150,7 @@ def initiate_validator_exit(state: BeaconState, index: ValidatorIndex) -> None:
```python
def slash_validator(state: BeaconState,
slashed_index: ValidatorIndex,
whistleblower_index: ValidatorIndex=None) -> None:
whistleblower_index: ValidatorIndex = None) -> None:
"""
Slash the validator with index ``slashed_index``.
"""
@ -1223,7 +1223,7 @@ Let `genesis_block = BeaconBlock(state_root=hash_tree_root(genesis_state))`.
The post-state corresponding to a pre-state `state` and a block `block` is defined as `state_transition(state, block)`. State transitions that trigger an unhandled excpetion (e.g. a failed `assert` or an out-of-range list access) are considered invalid.
```python
def state_transition(state: BeaconState, block: BeaconBlock, validate_state_root: bool=False) -> BeaconState:
def state_transition(state: BeaconState, block: BeaconBlock, validate_state_root: bool = False) -> BeaconState:
# Process slots (including those with no blocks) since block
process_slots(state, block.slot)
# Process block

View File

@ -33,6 +33,7 @@
- [Helpers](#helpers)
- [`type_of`](#type_of)
- [`empty`](#empty)
- [`ceillog2`](#ceillog2)
- [`get_crosslink_chunk_count`](#get_crosslink_chunk_count)
- [`get_custody_chunk_bit`](#get_custody_chunk_bit)
- [`get_chunk_bits_root`](#get_chunk_bits_root)
@ -126,7 +127,7 @@ This document details the beacon chain additions and changes in Phase 1 of Ether
'attestation': Attestation,
'challenger_index': ValidatorIndex,
'responder_key': BLSSignature,
'chunk_bits': "bytes",
'chunk_bits': 'bytes',
'signature': BLSSignature,
}
```
@ -257,6 +258,13 @@ The `type_of` function accepts an SSZ object as a single input and returns the c
The `empty` function accepts an SSZ type as input and returns an object of that type with all fields initialized to default values.
### `ceillog2`
```python
def ceillog2(x):
return x.bit_length()
```
### `get_crosslink_chunk_count`
```python
@ -298,7 +306,7 @@ def get_randao_epoch_for_custody_period(period: int, validator_index: ValidatorI
```python
def get_validators_custody_reveal_period(state: BeaconState,
validator_index: ValidatorIndex,
epoch: Epoch=None) -> int:
epoch: Epoch = None) -> int:
'''
This function returns the reveal period for a given validator.
If no epoch is supplied, the current epoch is assumed.
@ -479,7 +487,7 @@ def process_chunk_challenge(state: BeaconState,
record.chunk_index != challenge.chunk_index
)
# Verify depth
depth = math.ceil(math.log2(get_custody_chunk_count(challenge.attestation.data.crosslink)))
depth = ceillog2(get_custody_chunk_count(challenge.attestation.data.crosslink))
assert challenge.chunk_index < 2**depth
# Add new chunk challenge record
new_record = CustodyChunkChallengeRecord(
@ -637,7 +645,7 @@ def process_bit_challenge_response(state: BeaconState,
assert verify_merkle_branch(
leaf=hash_tree_root(response.chunk),
branch=response.data_branch,
depth=math.ceil(math.log2(challenge.chunk_count)),
depth=ceillog2(challenge.chunk_count),
index=response.chunk_index,
root=challenge.data_root,
)
@ -645,7 +653,7 @@ def process_bit_challenge_response(state: BeaconState,
assert verify_merkle_branch(
leaf=response.chunk_bits_leaf,
branch=response.chunk_bits_branch,
depth=math.ceil(math.log2(challenge.chunk_count)) >> 8,
depth=ceillog2(challenge.chunk_count) >> 8,
index=response.chunk_index // 256,
root=challenge.chunk_bits_merkle_root
)
@ -668,10 +676,9 @@ Run `process_reveal_deadlines(state)` immediately after `process_registry_update
```python
def process_reveal_deadlines(state: BeaconState) -> None:
for index, validator in enumerate(state.validator_registry):
if (validator.next_custody_reveal_period +
(CUSTODY_RESPONSE_DEADLINE // EPOCHS_PER_CUSTODY_PERIOD) <
get_validators_custody_reveal_period(state, index)):
slash_validator(state, index)
if (validator.next_custody_reveal_period + (CUSTODY_RESPONSE_DEADLINE // EPOCHS_PER_CUSTODY_PERIOD)
< get_validators_custody_reveal_period(state, index)):
slash_validator(state, index)
```
Run `process_challenge_deadlines(state)` immediately after `process_reveal_deadlines(state)`:

View File

@ -236,7 +236,7 @@ def verify_shard_attestation_signature(state: BeaconState,
### `compute_crosslink_data_root`
```python
def compute_crosslink_data_root(blocks: List[ShardBlock]) -> 'bytes32':
def compute_crosslink_data_root(blocks: List[ShardBlock]) -> Bytes32:
def is_power_of_two(value: int) -> bool:
return (value > 0) and (value & (value - 1) == 0)

View File

@ -32,7 +32,7 @@ def process_operations(state: BeaconState, block: BeaconBlock) -> None:
def process_block(state: BeaconState,
block: BeaconBlock,
verify_state_root: bool=False) -> None:
verify_state_root: bool = False) -> None:
spec.process_block_header(state, block)
spec.process_randao(state, block)
spec.process_eth1_data(state, block)
@ -65,6 +65,6 @@ def state_transition_to(state: BeaconState, up_to: Slot) -> BeaconState:
def state_transition(state: BeaconState,
block: BeaconBlock,
verify_state_root: bool=False) -> BeaconState:
verify_state_root: bool = False) -> BeaconState:
state_transition_to(state, block.slot)
process_block(state, block, verify_state_root)

View File

@ -6,8 +6,6 @@ BYTES_PER_CHUNK = 32
BYTES_PER_LENGTH_OFFSET = 4
ZERO_CHUNK = b'\x00' * BYTES_PER_CHUNK
cached_typedefs = {}
def SSZType(fields):
class SSZObject():

View File

@ -18,7 +18,6 @@ def run_transfer_processing(state, transfer, valid=True):
spec.process_transfer(post_state, transfer)
return state, None
spec.process_transfer(post_state, transfer)
proposer_index = spec.get_beacon_proposer_index(state)
@ -106,7 +105,12 @@ def test_insufficient_balance(state):
def test_no_dust(state):
sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1]
balance = state.balances[sender_index]
transfer = helpers.get_valid_transfer(state, sender_index=sender_index, amount=balance - spec.MIN_DEPOSIT_AMOUNT + 1, fee=0)
transfer = helpers.get_valid_transfer(
state,
sender_index=sender_index,
amount=balance - spec.MIN_DEPOSIT_AMOUNT + 1,
fee=0
)
# un-activate so validator can transfer
state.validator_registry[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH

View File

@ -4,13 +4,14 @@ from eth2spec.phase0 import spec
from preset_loader import loader
from tests.phase0 import helpers
from tests.phase0 import test_finality
def pytest_addoption(parser):
parser.addoption(
"--config", action="store", default="minimal", help="config: make the pyspec use the specified configuration"
)
@pytest.fixture(autouse=True)
def config(request):
config_name = request.config.getoption("--config")
@ -20,14 +21,17 @@ def config(request):
request.function.__globals__['spec'] = spec
request.function.__globals__['helpers'] = helpers
@pytest.fixture
def num_validators(config):
return spec.SLOTS_PER_EPOCH * 8
@pytest.fixture
def deposit_data_leaves():
return list()
@pytest.fixture
def state(num_validators, deposit_data_leaves):
return helpers.create_genesis_state(num_validators, deposit_data_leaves)

View File

@ -108,7 +108,10 @@ def test_double_late_crosslink(state):
# ensure that the current crosslinks were not updated by the second attestation
assert post_state.previous_crosslinks[shard] == post_state.current_crosslinks[shard]
# ensure no reward, only penalties for the failed crosslink
for index in spec.get_crosslink_committee(state, attestation_2.data.target_epoch, attestation_2.data.crosslink.shard):
for index in spec.get_crosslink_committee(
state,
attestation_2.data.target_epoch,
attestation_2.data.crosslink.shard):
assert crosslink_deltas[0][index] == 0
assert crosslink_deltas[1][index] > 0

View File

@ -14,6 +14,7 @@ privkeys = [i + 1 for i in range(1024)]
pubkeys = [bls.privtopub(privkey) for privkey in privkeys]
pubkey_to_privkey = {pubkey: privkey for privkey, pubkey in zip(privkeys, pubkeys)}
def advance_slot(state) -> None:
state.slot += 1
@ -137,7 +138,10 @@ def build_attestation_data(state, slot, shard):
justified_epoch = state.current_justified_epoch
justified_block_root = state.current_justified_root
crosslinks = state.current_crosslinks if spec.slot_to_epoch(slot) == spec.get_current_epoch(state) else state.previous_crosslinks
crosslinks = (
state.current_crosslinks if spec.slot_to_epoch(slot) == spec.get_current_epoch(state)
else state.previous_crosslinks
)
parent_crosslink = crosslinks[shard]
return spec.AttestationData(
beacon_block_root=block_root,
@ -258,7 +262,11 @@ def get_valid_attestation(state, slot=None):
attestation_data = build_attestation_data(state, slot, shard)
crosslink_committee = spec.get_crosslink_committee(state, attestation_data.target_epoch, attestation_data.crosslink.shard)
crosslink_committee = spec.get_crosslink_committee(
state,
attestation_data.target_epoch,
attestation_data.crosslink.shard
)
committee_size = len(crosslink_committee)
bitfield_length = (committee_size + 7) // 8
@ -351,7 +359,11 @@ def get_attestation_signature(state, attestation_data, privkey, custody_bit=0b0)
def fill_aggregate_attestation(state, attestation):
crosslink_committee = spec.get_crosslink_committee(state, attestation.data.target_epoch, attestation.data.crosslink.shard)
crosslink_committee = spec.get_crosslink_committee(
state,
attestation.data.target_epoch,
attestation.data.crosslink.shard
)
for i in range(len(crosslink_committee)):
attestation.aggregation_bitfield = set_bitfield_bit(attestation.aggregation_bitfield, i)
@ -390,6 +402,3 @@ def get_state_root(state, slot) -> bytes:
"""
assert slot < state.slot <= slot + spec.SLOTS_PER_HISTORICAL_ROOT
return state.latest_state_roots[slot % spec.SLOTS_PER_HISTORICAL_ROOT]
# Stub to be overwritten by config
import_spec = None

View File

@ -227,7 +227,6 @@ def test_attestation(state):
assert len(test_state.current_epoch_attestations) == len(state.current_epoch_attestations) + 1
#
# Epoch transition should move to previous_epoch_attestations
#

View File

@ -1,10 +1,11 @@
from copy import deepcopy
import pytest
#mark entire file as 'randao_key_reveals'
# mark entire file as 'randao_key_reveals'
pytestmark = pytest.mark.randao_key_reveals
def terun_early_derived_secret_reveal_processing(state, randao_key_reveal, valid=True):
def run_early_derived_secret_reveal_processing(state, randao_key_reveal, valid=True):
"""
Run ``process_randao_key_reveal`` returning the pre and post state.
If ``valid == False``, run expecting ``AssertionError``
@ -37,7 +38,7 @@ def terun_early_derived_secret_reveal_processing(state, randao_key_reveal, valid
def test_success(state):
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state)
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal)
pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal)
return pre_state, randao_key_reveal, post_state
@ -45,52 +46,71 @@ def test_success(state):
def test_reveal_from_current_epoch(state):
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state))
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal, False)
pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal, False)
return pre_state, randao_key_reveal, post_state
@pytest.mark.skip(reason="Not currently possible as we are testing at epoch 0")
def test_reveal_from_past_epoch(state):
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state) - 1)
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state) - 1)
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal, False)
pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal, False)
return pre_state, randao_key_reveal, post_state
return pre_state, randao_key_reveal, post_state
def test_reveal_with_custody_padding(state):
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state) + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING)
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal, True)
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(
state,
spec.get_current_epoch(state) + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING
)
pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal, True)
return pre_state, randao_key_reveal, post_state
def test_reveal_with_custody_padding_minus_one(state):
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state) + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING - 1)
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal, True)
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(
state,
spec.get_current_epoch(state) + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING - 1
)
pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal, True)
return pre_state, randao_key_reveal, post_state
def test_double_reveal(state):
randao_key_reveal1 = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state) + spec.RANDAO_PENALTY_EPOCHS + 1)
pre_state, intermediate_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal1)
randao_key_reveal2 = helpers.get_valid_early_derived_secret_reveal(intermediate_state, spec.get_current_epoch(pre_state) + spec.RANDAO_PENALTY_EPOCHS + 1)
_, post_state = terun_early_derived_secret_reveal_processing(intermediate_state, randao_key_reveal2, False)
randao_key_reveal1 = helpers.get_valid_early_derived_secret_reveal(
state,
spec.get_current_epoch(state) + spec.RANDAO_PENALTY_EPOCHS + 1
)
pre_state, intermediate_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal1)
randao_key_reveal2 = helpers.get_valid_early_derived_secret_reveal(
intermediate_state,
spec.get_current_epoch(pre_state) + spec.RANDAO_PENALTY_EPOCHS + 1
)
_, post_state = run_early_derived_secret_reveal_processing(intermediate_state, randao_key_reveal2, False)
return pre_state, [randao_key_reveal1, randao_key_reveal2], post_state
def test_revealer_is_slashed(state):
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state))
state.validator_registry[randao_key_reveal.revealed_index].slashed = True
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal, False)
pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal, False)
return pre_state, randao_key_reveal, post_state
def test_far_future_epoch(state):
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state) + spec.EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS)
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(
state,
spec.get_current_epoch(state) + spec.EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS
)
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal, False)
pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal, False)
return pre_state, randao_key_reveal, post_state

View File

@ -10,6 +10,7 @@ from tests.phase0.conftest import (
deposit_data_leaves,
)
def pytest_addoption(parser):
parser.addoption(
"--config", action="store", default="minimal", help="config: make the pyspec use the specified configuration"
@ -33,5 +34,5 @@ def num_validators(config):
@pytest.fixture
def state(num_validators, deposit_data_leaves):
def state(num_validators, deposit_data_leaves): # noqa: F811
return helpers.create_genesis_state(num_validators, deposit_data_leaves)

View File

@ -2,6 +2,7 @@ from py_ecc import bls
from tests.phase0.helpers import *
def get_valid_early_derived_secret_reveal(state, epoch=None):
current_epoch = spec.get_current_epoch(state)
revealed_index = spec.get_active_validator_indices(state, current_epoch)[-1]