2019-04-17 03:49:29 +00:00
|
|
|
from random import Random
|
|
|
|
|
2019-06-11 16:30:50 +00:00
|
|
|
from inspect import getmembers, isclass
|
|
|
|
|
2019-04-17 03:49:29 +00:00
|
|
|
from eth2spec.debug import random_value, encode
|
|
|
|
from eth2spec.phase0 import spec
|
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,
|
|
|
|
signing_root,
|
|
|
|
serialize,
|
|
|
|
)
|
2019-04-16 10:50:13 +00:00
|
|
|
from eth_utils import (
|
|
|
|
to_tuple, to_dict
|
|
|
|
)
|
2019-04-17 03:49:29 +00:00
|
|
|
from gen_base import gen_runner, gen_suite, gen_typing
|
2019-04-16 10:50:13 +00:00
|
|
|
from preset_loader import loader
|
|
|
|
|
2019-04-17 03:49:29 +00:00
|
|
|
MAX_BYTES_LENGTH = 100
|
|
|
|
MAX_LIST_LENGTH = 10
|
2019-04-16 10:50:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@to_dict
|
2019-06-22 20:15:42 +00:00
|
|
|
def create_test_case_contents(value):
|
|
|
|
yield "value", encode.encode(value)
|
2019-04-17 09:31:06 +00:00
|
|
|
yield "serialized", '0x' + serialize(value).hex()
|
2019-04-16 10:50:13 +00:00
|
|
|
yield "root", '0x' + hash_tree_root(value).hex()
|
2019-04-19 15:43:26 +00:00
|
|
|
if hasattr(value, "signature"):
|
|
|
|
yield "signing_root", '0x' + signing_root(value).hex()
|
2019-04-16 10:50:13 +00:00
|
|
|
|
|
|
|
|
2019-05-28 16:28:37 +00:00
|
|
|
@to_dict
|
2019-06-11 16:30:50 +00:00
|
|
|
def create_test_case(rng: Random, name: str, typ, mode: random_value.RandomizationMode, chaos: bool):
|
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-06-22 20:15:42 +00:00
|
|
|
yield name, create_test_case_contents(value)
|
2019-05-28 16:28:37 +00:00
|
|
|
|
|
|
|
|
2019-06-11 16:30:50 +00:00
|
|
|
def get_spec_ssz_types():
|
|
|
|
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
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-04-16 10:50:13 +00:00
|
|
|
@to_tuple
|
2019-04-17 03:49:29 +00:00
|
|
|
def ssz_static_cases(rng: Random, mode: random_value.RandomizationMode, chaos: bool, count: int):
|
2019-06-11 16:30:50 +00:00
|
|
|
for (name, ssz_type) in get_spec_ssz_types():
|
2019-04-17 03:49:29 +00:00
|
|
|
for i in range(count):
|
2019-06-11 16:30:50 +00:00
|
|
|
yield create_test_case(rng, name, ssz_type, mode, chaos)
|
2019-04-17 03:49:29 +00:00
|
|
|
|
2019-04-16 10:50:13 +00:00
|
|
|
|
2019-04-17 03:49:29 +00:00
|
|
|
def get_ssz_suite(seed: int, config_name: str, mode: random_value.RandomizationMode, chaos: bool, cases_if_random: int):
|
|
|
|
def ssz_suite(configs_path: str) -> gen_typing.TestSuiteOutput:
|
|
|
|
# Apply changes to presets, this affects some of the vector types.
|
|
|
|
presets = loader.load_presets(configs_path, config_name)
|
|
|
|
spec.apply_constants_preset(presets)
|
2019-04-16 10:50:13 +00:00
|
|
|
|
2019-04-17 03:49:29 +00:00
|
|
|
# Reproducible RNG
|
|
|
|
rng = Random(seed)
|
2019-04-16 10:50:13 +00:00
|
|
|
|
2019-04-17 03:49:29 +00:00
|
|
|
random_mode_name = mode.to_name()
|
2019-04-16 10:50:13 +00:00
|
|
|
|
2019-04-17 03:49:29 +00:00
|
|
|
suite_name = f"ssz_{config_name}_{random_mode_name}{'_chaos' if chaos else ''}"
|
2019-04-16 10:50:13 +00:00
|
|
|
|
2019-04-17 03:49:29 +00:00
|
|
|
count = cases_if_random if chaos or mode.is_changing() else 1
|
|
|
|
print(f"generating SSZ-static suite ({count} cases per ssz type): {suite_name}")
|
|
|
|
|
|
|
|
return (suite_name, "core", gen_suite.render_suite(
|
|
|
|
title=f"ssz testing, with {config_name} config, randomized with mode {random_mode_name}{' and with chaos applied' if chaos else ''}",
|
|
|
|
summary="Test suite for ssz serialization and hash-tree-root",
|
|
|
|
forks_timeline="testing",
|
|
|
|
forks=["phase0"],
|
2019-04-18 01:37:02 +00:00
|
|
|
config=config_name,
|
2019-04-17 03:49:29 +00:00
|
|
|
runner="ssz",
|
|
|
|
handler="static",
|
|
|
|
test_cases=ssz_static_cases(rng, mode, chaos, count)))
|
|
|
|
|
|
|
|
return ssz_suite
|
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:
|
|
|
|
settings.append((seed, "minimal", mode, False, 30))
|
|
|
|
seed += 1
|
|
|
|
settings.append((seed, "minimal", random_value.RandomizationMode.mode_random, True, 30))
|
|
|
|
seed += 1
|
|
|
|
settings.append((seed, "mainnet", random_value.RandomizationMode.mode_random, False, 5))
|
|
|
|
seed += 1
|
|
|
|
|
2019-04-16 10:50:13 +00:00
|
|
|
gen_runner.run_generator("ssz_static", [
|
2019-04-17 03:49:29 +00:00
|
|
|
get_ssz_suite(seed, config_name, mode, chaos, cases_if_random)
|
|
|
|
for (seed, config_name, mode, chaos, cases_if_random) in settings
|
2019-04-16 10:50:13 +00:00
|
|
|
])
|