2020-05-11 17:18:49 +00:00
|
|
|
from eth2spec.test import context
|
2021-10-18 09:42:47 +00:00
|
|
|
from eth2spec.test.helpers.constants import (
|
|
|
|
ALL_PHASES,
|
|
|
|
)
|
2020-05-11 17:18:49 +00:00
|
|
|
from eth2spec.utils import bls as bls_utils
|
2020-01-24 23:43:43 +00:00
|
|
|
|
2019-05-08 16:14:47 +00:00
|
|
|
# We import pytest only when it's present, i.e. when we are running tests.
|
|
|
|
# The test-cases themselves can be generated without installing pytest.
|
|
|
|
|
2019-05-30 01:53:46 +00:00
|
|
|
|
2019-05-08 16:14:47 +00:00
|
|
|
def module_exists(module_name):
|
|
|
|
try:
|
|
|
|
__import__(module_name)
|
|
|
|
except ImportError:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def fixture(*args, **kwargs):
|
|
|
|
if module_exists("pytest"):
|
|
|
|
import pytest
|
|
|
|
return pytest.fixture(*args, **kwargs)
|
|
|
|
else:
|
|
|
|
def ignore():
|
|
|
|
pass
|
|
|
|
return ignore
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption(
|
2021-05-18 11:59:26 +00:00
|
|
|
"--preset", action="store", type=str, default="minimal",
|
|
|
|
help="preset: make the pyspec use the specified preset"
|
2020-05-11 17:18:49 +00:00
|
|
|
)
|
2021-10-18 09:42:47 +00:00
|
|
|
parser.addoption(
|
|
|
|
"--fork", action="append", type=str,
|
|
|
|
help=(
|
|
|
|
"fork: make the pyspec only run with the specified phase."
|
|
|
|
" To run multiple phases, e.g., --fork=phase0 --fork=altair"
|
|
|
|
)
|
|
|
|
)
|
2020-05-11 17:18:49 +00:00
|
|
|
parser.addoption(
|
2020-08-10 18:58:57 +00:00
|
|
|
"--disable-bls", action="store_true", default=False,
|
2020-05-11 17:18:49 +00:00
|
|
|
help="bls-default: make tests that are not dependent on BLS run without BLS"
|
|
|
|
)
|
|
|
|
parser.addoption(
|
|
|
|
"--bls-type", action="store", type=str, default="py_ecc", choices=["py_ecc", "milagro"],
|
|
|
|
help="bls-type: use 'pyecc' or 'milagro' implementation for BLS"
|
2019-05-08 16:14:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-10-21 03:37:02 +00:00
|
|
|
def _validate_fork_name(forks):
|
|
|
|
for fork in forks:
|
|
|
|
if fork not in ALL_PHASES:
|
|
|
|
raise ValueError(
|
|
|
|
f'The given --fork argument "{fork}" is not an available fork.'
|
|
|
|
f' The available forks: {ALL_PHASES}'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-05-08 16:14:47 +00:00
|
|
|
@fixture(autouse=True)
|
2021-05-18 11:59:26 +00:00
|
|
|
def preset(request):
|
2021-05-18 14:17:02 +00:00
|
|
|
context.DEFAULT_TEST_PRESET = request.config.getoption("--preset")
|
2020-05-11 17:18:49 +00:00
|
|
|
|
|
|
|
|
2021-10-18 09:42:47 +00:00
|
|
|
@fixture(autouse=True)
|
|
|
|
def run_phases(request):
|
2021-10-21 03:37:02 +00:00
|
|
|
forks = request.config.getoption("--fork", default=None)
|
|
|
|
if forks:
|
2021-10-21 15:21:56 +00:00
|
|
|
forks = [fork.lower() for fork in forks]
|
2021-10-21 03:37:02 +00:00
|
|
|
_validate_fork_name(forks)
|
|
|
|
context.DEFAULT_PYTEST_FORKS = set(forks)
|
2021-10-18 09:42:47 +00:00
|
|
|
else:
|
|
|
|
context.DEFAULT_PYTEST_FORKS = ALL_PHASES
|
|
|
|
|
|
|
|
|
2020-05-11 17:18:49 +00:00
|
|
|
@fixture(autouse=True)
|
|
|
|
def bls_default(request):
|
2020-08-10 18:58:57 +00:00
|
|
|
disable_bls = request.config.getoption("--disable-bls")
|
2020-05-11 17:18:49 +00:00
|
|
|
if disable_bls:
|
|
|
|
context.DEFAULT_BLS_ACTIVE = False
|
|
|
|
|
|
|
|
|
|
|
|
@fixture(autouse=True)
|
|
|
|
def bls_type(request):
|
|
|
|
bls_type = request.config.getoption("--bls-type")
|
|
|
|
if bls_type == "py_ecc":
|
2020-07-23 23:19:14 +00:00
|
|
|
bls_utils.use_py_ecc()
|
2020-05-11 17:18:49 +00:00
|
|
|
elif bls_type == "milagro":
|
2020-07-23 23:19:14 +00:00
|
|
|
bls_utils.use_milagro()
|
2020-05-11 17:18:49 +00:00
|
|
|
else:
|
|
|
|
raise Exception(f"unrecognized bls type: {bls_type}")
|