test tagging pattern

This commit is contained in:
protolambda
2019-05-21 21:51:28 +02:00
parent 4d08e9d7d6
commit ee9c1d911f
2 changed files with 28 additions and 7 deletions

View File

@@ -3,12 +3,22 @@ from eth2spec.utils import bls
from .helpers.genesis import create_genesis_state
from .utils import spectest, with_args
from .utils import spectest, with_args, with_tags
# Provides a genesis state as first argument to the function decorated with this
with_state = with_args(lambda: [create_genesis_state(spec.SLOTS_PER_EPOCH * 8)])
# BLS is turned off by default *for performance purposes during TESTING*.
# The runner of the test can indicate the preferred setting (test generators prefer BLS to be ON).
# - Some tests are marked as BLS-requiring, and ignore this setting.
# (tests that express differences caused by BLS, e.g. invalid signatures being rejected)
# - Some other tests are marked as BLS-ignoring, and ignore this setting.
# (tests that are heavily performance impacted / require unsigned state transitions)
# - Most tests respect the BLS setting.
DEFAULT_BLS_ACTIVE = False
# shorthand for decorating @with_state @spectest()
def spec_state_test(fn):
return with_state(bls_switch(spectest()(fn)))
@@ -28,6 +38,10 @@ def expect_assertion_error(fn):
raise AssertionError('expected an assertion error, but got none.')
# Tags a test to be ignoring BLS for it to pass.
bls_ignored = with_tags({'bls_ignored': True})
def never_bls(fn):
"""
Decorator to apply on ``bls_switch`` decorator to force BLS de-activation. Useful to mark tests as BLS-ignorant.
@@ -36,7 +50,11 @@ def never_bls(fn):
# override bls setting
kw['bls_active'] = False
fn(*args, **kw)
return entry
return bls_ignored(entry)
# Tags a test to be requiring BLS for it to pass.
bls_required = with_tags({'bls_required': True})
def always_bls(fn):
@@ -47,7 +65,7 @@ def always_bls(fn):
# override bls setting
kw['bls_active'] = True
fn(*args, **kw)
return entry
return bls_required(entry)
def bls_switch(fn):
@@ -57,7 +75,7 @@ def bls_switch(fn):
"""
def entry(*args, **kw):
old_state = bls.bls_active
bls.bls_active = kw.pop('bls_active', True)
bls.bls_active = kw.pop('bls_active', DEFAULT_BLS_ACTIVE)
fn(*args, **kw)
bls.bls_active = old_state
return entry

View File

@@ -4,13 +4,16 @@ from eth2spec.phase0 import spec
from eth2spec.phase0.spec import get_beacon_proposer_index, slot_to_epoch, get_domain, BeaconBlock
from eth2spec.phase0.state_transition import state_transition, state_transition_to
from eth2spec.test.helpers.keys import privkeys
from eth2spec.utils.bls import bls_sign
from eth2spec.utils import bls
from eth2spec.utils.minimal_ssz import signing_root, hash_tree_root
def sign_block(state, block, proposer_index=None):
assert state.slot <= block.slot
if not bls.bls_active:
return
if proposer_index is None:
if block.slot == state.slot:
proposer_index = get_beacon_proposer_index(state)
@@ -25,7 +28,7 @@ def sign_block(state, block, proposer_index=None):
privkey = privkeys[proposer_index]
block.body.randao_reveal = bls_sign(
block.body.randao_reveal = bls.bls_sign(
privkey=privkey,
message_hash=hash_tree_root(slot_to_epoch(block.slot)),
domain=get_domain(
@@ -34,7 +37,7 @@ def sign_block(state, block, proposer_index=None):
domain_type=spec.DOMAIN_RANDAO,
)
)
block.signature = bls_sign(
block.signature = bls.bls_sign(
message_hash=signing_root(block),
privkey=privkey,
domain=get_domain(