Skip to content

Commit

Permalink
Fix test helpers to detect ValidationError
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed May 17, 2021
1 parent 22618bb commit 0bfda33
Show file tree
Hide file tree
Showing 17 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions specs/phase0/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,13 +571,13 @@ class SignedBeaconBlockHeader(Container):
#### `require`

```python
def require(condition: bool, message: str=None) -> None:
def require(condition: bool, message: str='') -> None:
"""
Check if the given condition is satisfied.
If not, raise an exception to signal that it's INVALID.
"""
if not condition:
raise ValidationError(str(condition) if message is None else message)
raise ValidationError(message)
```

**Note**: The `message` value is just for debugging the executable spec. The error handling details are implementation specific.
Expand Down
6 changes: 1 addition & 5 deletions tests/core/pyspec/eth2spec/test/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,7 @@ def expect_assertion_error(fn):
try:
fn()
bad = True
except ValidationError:
pass
except AssertionError:
pass
except IndexError:
except (ValidationError, AssertionError, IndexError):
# Index errors are special; the spec is not explicit on bound checking, an IndexError is like a failed assert.
pass
if bad:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def run_chunk_challenge_processing(spec, state, custody_chunk_challenge, valid=T
- pre-state ('pre')
- CustodyBitChallenge ('custody_chunk_challenge')
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
If ``valid == False``, run expecting ``ValidationError``
"""
yield 'pre', state
yield 'custody_chunk_challenge', custody_chunk_challenge
Expand All @@ -53,7 +53,7 @@ def run_custody_chunk_response_processing(spec, state, custody_response, valid=T
- pre-state ('pre')
- CustodyResponse ('custody_response')
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
If ``valid == False``, run expecting ``ValidationError``
"""
yield 'pre', state
yield 'custody_response', custody_response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def run_custody_key_reveal_processing(spec, state, custody_key_reveal, valid=Tru
- pre-state ('pre')
- custody_key_reveal ('custody_key_reveal')
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
If ``valid == False``, run expecting ``ValidationError``
"""
yield 'pre', state
yield 'custody_key_reveal', custody_key_reveal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def run_custody_slashing_processing(spec, state, custody_slashing, valid=True, c
- pre-state ('pre')
- CustodySlashing ('custody_slashing')
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
If ``valid == False``, run expecting ``ValidationError``
"""
yield 'pre', state
yield 'custody_slashing', custody_slashing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal, v
- pre-state ('pre')
- randao_key_reveal ('randao_key_reveal')
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
If ``valid == False``, run expecting ``ValidationError``
"""
yield 'pre', state
yield 'randao_key_reveal', randao_key_reveal
Expand Down
2 changes: 1 addition & 1 deletion tests/core/pyspec/eth2spec/test/helpers/attestations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def run_attestation_processing(spec, state, attestation, valid=True):
- pre-state ('pre')
- attestation ('attestation')
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
If ``valid == False``, run expecting ``ValidationError``
"""
# yield pre-state
yield 'pre', state
Expand Down
5 changes: 3 additions & 2 deletions tests/core/pyspec/eth2spec/test/helpers/fork_choice.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from eth_utils import encode_hex

from eth2spec.test.exceptions import ValidationError


def get_anchor_root(spec, state):
anchor_block_header = state.latest_block_header.copy()
Expand Down Expand Up @@ -77,8 +79,7 @@ def run_on_block(spec, store, signed_block, test_steps, valid=True):
if not valid:
try:
spec.on_block(store, signed_block)

except AssertionError:
except (ValidationError, AssertionError):
return
else:
assert False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def run_execution_payload_processing(spec, state, execution_payload, valid=True,
- execution payload ('execution_payload')
- execution details, to mock EVM execution ('execution.yml', a dict with 'execution_valid' key and boolean value)
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
If ``valid == False``, run expecting ``ValidationError``
"""

yield 'pre', state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def run_attester_slashing_processing(spec, state, attester_slashing, valid=True)
- pre-state ('pre')
- attester_slashing ('attester_slashing')
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
If ``valid == False``, run expecting ``ValidationError``
"""

yield 'pre', state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def run_block_header_processing(spec, state, block, prepare_state=True, valid=Tr
- pre-state ('pre')
- block ('block')
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
If ``valid == False``, run expecting ``ValidationError``
"""
if prepare_state:
prepare_state_for_header_processing(spec, state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def run_deposit_processing(spec, state, deposit, validator_index, valid=True, ef
- pre-state ('pre')
- deposit ('deposit')
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
If ``valid == False``, run expecting ``ValidationError``
"""
pre_validator_count = len(state.validators)
pre_balance = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def run_proposer_slashing_processing(spec, state, proposer_slashing, valid=True)
- pre-state ('pre')
- proposer_slashing ('proposer_slashing')
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
If ``valid == False``, run expecting ``ValidationError``
"""

pre_state = state.copy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def run_voluntary_exit_processing(spec, state, signed_voluntary_exit, valid=True
- pre-state ('pre')
- voluntary_exit ('voluntary_exit')
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
If ``valid == False``, run expecting ``ValidationError``
"""
validator_index = signed_voluntary_exit.message.validator_index

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from eth2spec.utils.ssz.ssz_impl import hash_tree_root

from eth2spec.test.context import with_all_phases, spec_state_test
from eth2spec.test.exceptions import ValidationError
from eth2spec.test.helpers.attestations import next_epoch_with_attestations
from eth2spec.test.helpers.block import build_empty_block_for_next_slot, sign_block, transition_unsigned_block, \
build_empty_block
Expand All @@ -13,7 +14,7 @@ def run_on_block(spec, store, signed_block, valid=True):
if not valid:
try:
spec.on_block(store, signed_block)
except AssertionError:
except (ValidationError, AssertionError):
return
else:
assert False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
always_bls, with_phases, with_all_phases,
)
from eth2spec.test.helpers.constants import PHASE0
from eth2spec.test.exceptions import ValidationError
from eth2spec.test.helpers.attestations import build_attestation_data, get_valid_attestation
from eth2spec.test.helpers.block import build_empty_block
from eth2spec.test.helpers.deposits import prepare_state_and_deposit
Expand All @@ -29,7 +30,7 @@ def run_get_committee_assignment(spec, state, epoch, validator_index, valid=True
assert committee_index < spec.get_committee_count_per_slot(state, epoch)
assert validator_index in committee
assert valid
except AssertionError:
except (ValidationError, AssertionError):
assert not valid
else:
assert valid
Expand Down
4 changes: 3 additions & 1 deletion tests/core/pyspec/eth2spec/utils/test_merkle_minimal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest

from eth2spec.test.exceptions import ValidationError
from .merkle_minimal import zerohashes, merkleize_chunks, get_merkle_root
from .hash_function import hash

Expand Down Expand Up @@ -71,7 +73,7 @@ def test_merkleize_chunks_and_get_merkle_root(count, limit, value):
try:
merkleize_chunks(chunks, limit=limit)
bad = True
except AssertionError:
except (ValidationError, AssertionError):
pass
if bad:
assert False, "expected merkleization to be invalid"
Expand Down

0 comments on commit 0bfda33

Please sign in to comment.