eth2.0-specs/tests/generators/shuffling/main.py

69 lines
2.1 KiB
Python
Raw Normal View History

2019-07-27 00:22:19 +00:00
from typing import Iterable
2023-05-24 12:54:22 +00:00
import random
from eth2spec.gen_helpers.gen_base import gen_runner, gen_typing
2021-05-18 18:53:04 +00:00
from eth2spec.test.helpers.typing import PresetBaseName
2021-05-18 16:29:24 +00:00
from eth2spec.phase0 import mainnet as spec_mainnet, minimal as spec_minimal
from eth2spec.test.helpers.constants import PHASE0, MINIMAL, MAINNET
2019-07-27 00:22:19 +00:00
2023-05-24 12:54:22 +00:00
def generate_random_bytes(rng=random.Random(5566)):
random_bytes = bytes(rng.randint(0, 255) for _ in range(32))
return random_bytes
# NOTE: somehow the random.Random generated seeds do not have pickle issue.
rng = random.Random(1234)
seeds = [generate_random_bytes(rng) for i in range(30)]
2021-05-18 16:29:24 +00:00
def shuffling_case_fn(spec, seed, count):
2019-07-27 00:22:19 +00:00
yield 'mapping', 'data', {
'seed': '0x' + seed.hex(),
'count': count,
'mapping': [int(spec.compute_shuffled_index(i, count, seed)) for i in range(count)]
}
2019-04-10 15:52:51 +00:00
2021-05-18 16:29:24 +00:00
def shuffling_case(spec, seed, count):
return f'shuffle_0x{seed.hex()}_{count}', lambda: shuffling_case_fn(spec, seed, count)
2019-04-10 15:52:51 +00:00
2021-05-18 16:29:24 +00:00
def shuffling_test_cases(spec):
2023-05-24 12:54:22 +00:00
for seed in seeds:
2019-07-27 00:22:19 +00:00
for count in [0, 1, 2, 3, 5, 10, 33, 100, 1000, 9999]:
2021-05-18 16:29:24 +00:00
yield shuffling_case(spec, seed, count)
2019-04-10 15:52:51 +00:00
2021-05-18 16:29:24 +00:00
def create_provider(preset_name: PresetBaseName) -> gen_typing.TestProvider:
2019-04-10 15:52:51 +00:00
2021-05-18 16:29:24 +00:00
def prepare_fn() -> None:
return
2019-04-10 15:52:51 +00:00
2019-07-27 00:22:19 +00:00
def cases_fn() -> Iterable[gen_typing.TestCase]:
2021-05-18 16:29:24 +00:00
if preset_name == MAINNET:
spec = spec_mainnet
elif preset_name == MINIMAL:
spec = spec_minimal
else:
raise Exception(f"unrecognized preset: {preset_name}")
2021-05-18 18:13:19 +00:00
for (case_name, case_fn) in shuffling_test_cases(spec):
2019-07-27 00:22:19 +00:00
yield gen_typing.TestCase(
fork_name=PHASE0,
2021-05-18 16:29:24 +00:00
preset_name=preset_name,
2019-07-27 00:22:19 +00:00
runner_name='shuffling',
handler_name='core',
suite_name='shuffle',
case_name=case_name,
2023-05-24 12:54:22 +00:00
case_fn=case_fn,
2019-07-27 00:22:19 +00:00
)
2019-04-10 15:52:51 +00:00
2019-07-27 00:22:19 +00:00
return gen_typing.TestProvider(prepare=prepare_fn, make_cases=cases_fn)
2019-04-10 15:52:51 +00:00
if __name__ == "__main__":
2023-05-24 12:54:22 +00:00
gen_runner.run_generator("shuffling", [
create_provider(MINIMAL), create_provider(MAINNET)]
)