2019-04-17 03:49:29 +00:00
|
|
|
from random import Random
|
2019-07-27 11:34:19 +00:00
|
|
|
from typing import Iterable
|
2019-06-11 16:30:50 +00:00
|
|
|
from inspect import getmembers, isclass
|
|
|
|
|
2021-02-18 07:17:47 +00:00
|
|
|
from eth2spec.gen_helpers.gen_base import gen_runner, gen_typing
|
2020-01-25 21:10:03 +00:00
|
|
|
|
2019-04-17 03:49:29 +00:00
|
|
|
from eth2spec.debug import random_value, encode
|
2021-05-18 18:53:04 +00:00
|
|
|
from eth2spec.test.helpers.constants import TESTGEN_FORKS, MINIMAL, MAINNET
|
2021-05-18 16:29:24 +00:00
|
|
|
from eth2spec.test.context import spec_targets
|
2019-06-11 16:30:50 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_typing import Container
|
2019-05-27 21:41:48 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_impl import (
|
2019-04-19 15:43:26 +00:00
|
|
|
hash_tree_root,
|
|
|
|
serialize,
|
|
|
|
)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
2019-04-16 10:50:13 +00:00
|
|
|
|
2020-07-07 19:21:43 +00:00
|
|
|
MAX_BYTES_LENGTH = 1000
|
2019-04-17 03:49:29 +00:00
|
|
|
MAX_LIST_LENGTH = 10
|
2019-04-16 10:50:13 +00:00
|
|
|
|
|
|
|
|
2020-09-15 03:19:50 +00:00
|
|
|
def create_test_case(rng: Random, typ,
|
|
|
|
mode: random_value.RandomizationMode, chaos: bool) -> Iterable[gen_typing.TestCasePart]:
|
2019-05-28 16:28:37 +00:00
|
|
|
value = random_value.get_random_ssz_object(rng, typ, MAX_BYTES_LENGTH, MAX_LIST_LENGTH, mode, chaos)
|
2019-07-27 11:34:19 +00:00
|
|
|
yield "value", "data", encode.encode(value)
|
|
|
|
yield "serialized", "ssz", serialize(value)
|
2019-08-01 20:03:40 +00:00
|
|
|
roots_data = {
|
|
|
|
"root": '0x' + hash_tree_root(value).hex()
|
|
|
|
}
|
|
|
|
yield "roots", "data", roots_data
|
2019-05-28 16:28:37 +00:00
|
|
|
|
|
|
|
|
2020-07-07 19:21:43 +00:00
|
|
|
def get_spec_ssz_types(spec):
|
2019-06-11 16:30:50 +00:00
|
|
|
return [
|
|
|
|
(name, value) for (name, value) in getmembers(spec, isclass)
|
|
|
|
if issubclass(value, Container) and value != Container # only the subclasses, not the imported base class
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-05-18 16:29:24 +00:00
|
|
|
def ssz_static_cases(fork_name: str, preset_name: str, seed: int, name, ssz_type,
|
2020-09-15 03:19:50 +00:00
|
|
|
mode: random_value.RandomizationMode, chaos: bool, count: int):
|
2019-07-27 11:34:19 +00:00
|
|
|
random_mode_name = mode.to_name()
|
2019-04-17 03:49:29 +00:00
|
|
|
|
2019-07-27 11:34:19 +00:00
|
|
|
# Reproducible RNG
|
|
|
|
rng = Random(seed)
|
2019-04-16 10:50:13 +00:00
|
|
|
|
2019-07-27 11:34:19 +00:00
|
|
|
for i in range(count):
|
|
|
|
yield gen_typing.TestCase(
|
2020-07-07 19:21:43 +00:00
|
|
|
fork_name=fork_name,
|
2021-05-18 16:29:24 +00:00
|
|
|
preset_name=preset_name,
|
2019-07-27 11:34:19 +00:00
|
|
|
runner_name='ssz_static',
|
|
|
|
handler_name=name,
|
|
|
|
suite_name=f"ssz_{random_mode_name}{'_chaos' if chaos else ''}",
|
|
|
|
case_name=f"case_{i}",
|
|
|
|
case_fn=lambda: create_test_case(rng, ssz_type, mode, chaos)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-05-18 16:29:24 +00:00
|
|
|
def create_provider(fork_name, preset_name: str, seed: int, mode: random_value.RandomizationMode, chaos: bool,
|
2019-07-27 11:34:19 +00:00
|
|
|
cases_if_random: int) -> gen_typing.TestProvider:
|
2021-05-18 16:29:24 +00:00
|
|
|
def prepare_fn() -> None:
|
|
|
|
return
|
2019-04-16 10:50:13 +00:00
|
|
|
|
2019-07-27 11:34:19 +00:00
|
|
|
def cases_fn() -> Iterable[gen_typing.TestCase]:
|
2019-04-17 03:49:29 +00:00
|
|
|
count = cases_if_random if chaos or mode.is_changing() else 1
|
2021-05-18 16:29:24 +00:00
|
|
|
spec = spec_targets[preset_name][fork_name]
|
2019-04-17 03:49:29 +00:00
|
|
|
|
2020-07-07 19:21:43 +00:00
|
|
|
for (i, (name, ssz_type)) in enumerate(get_spec_ssz_types(spec)):
|
2021-05-18 16:29:24 +00:00
|
|
|
yield from ssz_static_cases(fork_name, preset_name, seed * 1000 + i, name, ssz_type, mode, chaos, count)
|
2019-04-17 03:49:29 +00:00
|
|
|
|
2019-07-27 11:34:19 +00:00
|
|
|
return gen_typing.TestProvider(prepare=prepare_fn, make_cases=cases_fn)
|
2019-04-16 10:50:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-04-17 03:49:29 +00:00
|
|
|
# [(seed, config name, randomization mode, chaos on/off, cases_if_random)]
|
|
|
|
settings = []
|
|
|
|
seed = 1
|
|
|
|
for mode in random_value.RandomizationMode:
|
2021-02-23 13:17:12 +00:00
|
|
|
settings.append((seed, MINIMAL, mode, False, 30))
|
2019-04-17 03:49:29 +00:00
|
|
|
seed += 1
|
2021-02-23 13:17:12 +00:00
|
|
|
settings.append((seed, MINIMAL, random_value.RandomizationMode.mode_random, True, 30))
|
2019-04-17 03:49:29 +00:00
|
|
|
seed += 1
|
2021-02-23 13:17:12 +00:00
|
|
|
settings.append((seed, MAINNET, random_value.RandomizationMode.mode_random, False, 5))
|
2020-09-21 19:40:14 +00:00
|
|
|
seed += 1
|
2021-05-04 22:15:15 +00:00
|
|
|
for fork in TESTGEN_FORKS:
|
2020-07-07 19:21:43 +00:00
|
|
|
gen_runner.run_generator("ssz_static", [
|
2021-05-18 16:29:24 +00:00
|
|
|
create_provider(fork, preset_name, seed, mode, chaos, cases_if_random)
|
|
|
|
for (seed, preset_name, mode, chaos, cases_if_random) in settings
|
2020-07-07 19:21:43 +00:00
|
|
|
])
|