Skip to content

Commit

Permalink
Merge pull request #14 from ericsson49/fc-compliance
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
mkalinin authored Jun 16, 2024
2 parents 4d7dbed + 8a67fe7 commit 2b29480
Show file tree
Hide file tree
Showing 19 changed files with 1,076 additions and 904 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def generate_block_cover(params):
'out_path': 'standard/block_cover.yaml',
'models': ['block_cover'],
'params': [
({'anchor_epoch': 0, 'number_of_solutions': 2},),
({'anchor_epoch': 0, 'number_of_solutions': 5},),
({'anchor_epoch': 2, 'number_of_solutions': 5},),
]
},
Expand Down
144 changes: 14 additions & 130 deletions tests/generators/fork_choice_generated/instance_generator.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,18 @@
from eth2spec.test.helpers.constants import ALTAIR
from eth2spec.gen_helpers.gen_base import gen_runner
from eth2spec.test.helpers.constants import MINIMAL, MAINNET
from eth2spec.test.helpers.specs import spec_targets
from eth2spec.gen_helpers.gen_base.gen_typing import TestCase, TestProvider
from itertools import product
from toolz.dicttoolz import merge
from typing import Iterable, Callable
from importlib import import_module
from eth2spec.utils import bls
from eth2spec.test.helpers.typing import SpecForkName, PresetBaseName
from minizinc import Instance, Model, Solver
from ruamel.yaml import YAML
from mutation_operators import mk_mutations, MutatorsGenerator
import random
from instantiators.block_tree import yield_block_tree_test_case
from instantiators.block_cover import yield_block_cover_test_case


BLS_ACTIVE = False
GENERATOR_NAME = 'fork_choice_generated'
from test_provider import GENERATOR_NAME, create_providers


forks = [ALTAIR]
presets = [MINIMAL]


def _create_providers(test_name: str, /,
forks: Iterable[SpecForkName],
presets: Iterable[PresetBaseName],
debug: bool,
initial_seed: int,
solutions,
number_of_variations: int,
number_of_mutations: int,
test_fn: Callable,
) -> Iterable[TestProvider]:
def prepare_fn() -> None:
bls.use_milagro()
return

seeds = [initial_seed]
if number_of_variations > 1:
rnd = random.Random(initial_seed)
seeds = [rnd.randint(1, 10000) for _ in range(number_of_variations)]
seeds[0] = initial_seed

for fork_name in forks:
for preset_name in presets:
spec = spec_targets[preset_name][fork_name]

for i, solution in enumerate(solutions):
def make_cases_fn() -> Iterable[TestCase]:
for seed in seeds:
mutation_generator = MutatorsGenerator(
spec, seed, number_of_mutations,
lambda: test_fn(fork_name, preset_name, seed, solution),
debug=debug)
for j in range(1 + number_of_mutations):
yield TestCase(fork_name=fork_name,
preset_name=preset_name,
runner_name=GENERATOR_NAME,
handler_name=test_name,
suite_name='pyspec_tests',
case_name=test_name + '_' + str(i) + '_' + str(seed) + '_' + str(j),
case_fn=mutation_generator.next_test_case)

yield TestProvider(prepare=prepare_fn, make_cases=make_cases_fn)


def _find_sm_link_solutions(anchor_epoch: int,
number_of_epochs: int,
number_of_links: int) -> Iterable[Iterable[tuple]]:
Expand Down Expand Up @@ -109,32 +54,6 @@ def _load_block_tree_instances(instance_path: str) -> Iterable[dict]:
return solutions


def _create_block_tree_providers(test_name: str, /,
forks: Iterable[SpecForkName],
presets: Iterable[PresetBaseName],
debug: bool,
initial_seed: int,
solutions: Iterable,
number_of_variations: int,
number_of_mutations: int,
with_attester_slashings: bool,
with_invalid_messages: bool) -> Iterable[TestProvider]:
def test_fn(phase: str, preset: str, seed: int, solution):
return yield_block_tree_test_case(generator_mode=True,
phase=phase,
preset=preset,
bls_active=BLS_ACTIVE,
debug=debug,
seed=seed,
sm_links=solution['sm_links'],
block_parents=solution['block_parents'],
with_attester_slashings=with_attester_slashings,
with_invalid_messages=with_invalid_messages)

yield from _create_providers(
test_name, forks, presets, debug, initial_seed, solutions, number_of_variations, number_of_mutations, test_fn)


def _find_block_cover_model_solutions(anchor_epoch: int,
store_justified_epoch_equal_zero: bool,
block_voting_source_epoch_equal_store_justified_epoch: bool,
Expand Down Expand Up @@ -210,27 +129,6 @@ def _load_block_cover_instances(instance_path: str):
return solutions


def _create_block_cover_providers(test_name: str, /,
forks: Iterable[SpecForkName],
presets: Iterable[PresetBaseName],
debug: bool,
initial_seed: int,
solutions,
number_of_variations: int,
number_of_mutations: int) -> Iterable[TestProvider]:
def test_fn(phase: str, preset: str, seed: int, solution):
return yield_block_cover_test_case(generator_mode=True,
phase=phase,
preset=preset,
bls_active=BLS_ACTIVE,
debug=debug,
seed=seed,
model_params=solution)

yield from _create_providers(
test_name, forks, presets, debug, initial_seed, solutions, number_of_variations, number_of_mutations, test_fn)


if __name__ == "__main__":
arg_parser = gen_runner.create_arg_parser(GENERATOR_NAME)

Expand Down Expand Up @@ -340,44 +238,30 @@ def test_fn(phase: str, preset: str, seed: int, solution):
block_tree_solutions = _find_block_tree_solutions(16, 3, 3)
solutions = [merge(*sols) for sols in product(sm_link_solutions, block_tree_solutions)]

if not args.fc_gen_attester_slashings and not args.fc_gen_invalid_messages:
with_attester_slashings, with_invalid_messages = args.fc_gen_attester_slashings, args.fc_gen_invalid_messages
if not with_attester_slashings and not with_invalid_messages:
test_name = 'block_tree'
elif args.fc_gen_attester_slashings and not args.fc_gen_invalid_messages:
test_kind = 'block_tree_test'
elif with_attester_slashings and not with_invalid_messages:
test_name = 'attester_slashings'
elif args.fc_gen_invalid_messages and not args.fc_gen_attester_slashings:
test_kind = 'attester_slashing_test'
elif with_attester_slashings and with_invalid_messages:
test_name = 'invalid_messages'
test_kind = 'invalid_message_test'
else:
test_name = 'attester_slashings_and_invalid_messages'

gen_runner.run_generator(GENERATOR_NAME,
_create_block_tree_providers(test_name,
forks=forks,
presets=presets,
debug=args.fc_gen_debug,
initial_seed=args.fc_gen_seed,
solutions=solutions,
number_of_variations=args.fc_gen_variations,
number_of_mutations=args.fc_gen_mutations,
with_attester_slashings=args.fc_gen_attester_slashings,
with_invalid_messages=args.fc_gen_invalid_messages),
arg_parser)
test_kind = 'attestet_slashing_and_invalid_message_test'
elif args.fc_gen_test_kind == 'block_cover':
if args.fc_gen_instances_path is not None:
solutions = _load_block_cover_instances(args.fc_gen_instances_path)
else:
solutions = _generate_block_cover_model_solutions(args.fc_gen_anchor_epoch, args.fc_gen_nr_solutions, args.fc_gen_debug)

test_name = 'block_cover'
gen_runner.run_generator(GENERATOR_NAME,
_create_block_cover_providers(
test_name,
forks=forks,
presets=presets,
debug=args.fc_gen_debug,
initial_seed=args.fc_gen_seed,
solutions=solutions,
number_of_variations=args.fc_gen_variations,
number_of_mutations=args.fc_gen_mutations),
arg_parser)
test_kind = 'block_cover_test'
else:
raise ValueError(f'Unsupported test kind: {args.fc_gen_test_kind}')

providers = create_providers(test_name, forks, presets, args.fc_gen_debug, args.fc_gen_seed,
solutions, args.fc_gen_variations, args.fc_gen_mutations, test_kind)
gen_runner.run_generator(GENERATOR_NAME, providers, arg_parser)
Loading

0 comments on commit 2b29480

Please sign in to comment.