Skip to content

Commit

Permalink
Merge pull request #17 from ericsson49/fc-compliance2
Browse files Browse the repository at this point in the history
Some fixes
  • Loading branch information
mkalinin authored Jul 1, 2024
2 parents f184254 + 5a14ef6 commit 8545060
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 16 deletions.
4 changes: 2 additions & 2 deletions tests/generators/fork_choice_generated/instance_generator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from eth2spec.test.helpers.constants import ALTAIR
from eth2spec.test.helpers.constants import ALTAIR, DENEB
from eth2spec.gen_helpers.gen_base import gen_runner
from eth2spec.test.helpers.constants import MINIMAL, MAINNET
from itertools import product
Expand All @@ -9,7 +9,7 @@
from test_provider import GENERATOR_NAME, create_providers


forks = [ALTAIR]
forks = [DENEB]
presets = [MINIMAL]


Expand Down
10 changes: 5 additions & 5 deletions tests/generators/fork_choice_generated/instantiators/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,14 @@ def try_add_mesage(runner, message):
return False

# record initial tick
on_tick_and_append_step(spec, store, store.time, test_steps, checks_with_viable_for_head_weights=True)
on_tick_and_append_step(spec, store, store.time, test_steps)

for event in test_events:
event_kind = event[0]
if event_kind == 'tick':
_, time, _ = event
if time > store.time:
on_tick_and_append_step(spec, store, time, test_steps, checks_with_viable_for_head_weights=True)
on_tick_and_append_step(spec, store, time, test_steps)
assert store.time == time
elif event_kind == 'block':
_, signed_block, valid = event
Expand All @@ -391,19 +391,19 @@ def try_add_mesage(runner, message):
assert store.blocks[block_root] == signed_block.message
else:
assert block_root not in store.blocks.values()
output_store_checks(spec, store, test_steps, with_viable_for_head_weights=True)
output_store_checks(spec, store, test_steps)
elif event_kind == 'attestation':
_, attestation, valid = event
if valid is None:
valid = try_add_mesage(run_on_attestation, attestation)
yield from add_attestation(spec, store, attestation, test_steps, valid=valid)
output_store_checks(spec, store, test_steps, with_viable_for_head_weights=True)
output_store_checks(spec, store, test_steps)
elif event_kind == 'attester_slashing':
_, attester_slashing, valid = event
if valid is None:
valid = try_add_mesage(run_on_attester_slashing, attester_slashing)
yield from add_attester_slashing(spec, store, attester_slashing, test_steps, valid=valid)
output_store_checks(spec, store, test_steps, with_viable_for_head_weights=True)
output_store_checks(spec, store, test_steps)
else:
raise ValueError('Unknown event ' + str(event_kind))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def mut_shift_(tv, idx, delta):
new_time = int(time) + delta
if new_time >= 0:
return sorted(tv[:idx] + [(new_time, event)] + tv[idx+1:], key=lambda x: x[0])
else:
return idx


def mut_shift(tv, rnd: random.Random):
Expand Down Expand Up @@ -71,3 +73,59 @@ def mutate_test_vector(rnd, initial_tv, cnt, debug=False):
else:
assert False
yield tv_


class MutationOps:
def __init__(self, start_time, seconds_per_slot, shift_bounds=(-2,4)):
self.start_time = int(start_time)
self.seconds_per_slot = int(seconds_per_slot)
self.shift_bounds = shift_bounds

def apply_shift(self, tv, idx, delta):
return mut_shift_(tv, idx, delta)

def apply_drop(self, tv, idx):
return mut_drop_(tv, idx)

def apply_dup_shift(self, tv, idx, delta):
return mut_dup_(tv, idx, delta)

def apply_mutation(self, tv, op_kind, *params):
if op_kind == 'shift':
return self.apply_shift(tv, *params)
elif op_kind == 'dup_shift':
return self.apply_dup_shift(tv, *params)
elif op_kind == 'drop':
return self.apply_drop(tv, *params)
else:
assert False

def rand_shift(self, time: int, rnd: random.Random) -> int:
assert time >= self.start_time
neg_shift, pos_shift = self.shift_bounds
min_shift = max(self.start_time - time, neg_shift * self.seconds_per_slot)
max_shift = pos_shift * self.seconds_per_slot
if rnd.randint(0, 1) == 0:
return rnd.randint(min_shift, 0)
else:
return rnd.randint(1, max_shift)

def rand_mutation(self, tv, rnd: random.Random):
idx = rnd.choice(range(len(tv)))
op_kind = rnd.choice(['shift', 'drop', 'dup_shift'])
if op_kind == 'shift' or op_kind == 'dup_shift':
evt_time = int(tv[idx][0])
params = idx, self.rand_shift(evt_time, rnd)
else:
params = idx,
return op_kind, *params

def rand_mutations(self, tv, num, rnd: random.Random):
mutations = []
for _ in range(num):
if len(tv) == 0:
break
mut_op = self.rand_mutation(tv, rnd)
mutations.append(mut_op)
tv = self.apply_mutation(tv, *mut_op)
return tv, mutations
15 changes: 6 additions & 9 deletions tests/generators/fork_choice_generated/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,13 @@ def yield_test_parts(spec, store, test_data: FCTestData, events):
elif event_kind == 'block':
assert recovery
_block_id = get_block_file_name(event_data)
print('recovered block', _block_id)
test_steps.append({'block': _block_id, 'valid': True, 'recovery': True})
test_steps.append({'block': _block_id, 'valid': True})
elif event_kind == 'attestation':
assert recovery
_attestation_id = get_attestation_file_name(event_data)
if _attestation_id not in test_data.atts:
yield _attestation_id, event_data
print('recovered attestation', _attestation_id)
test_steps.append({'attestation': _attestation_id, 'valid': True, 'recovery': True})
test_steps.append({'attestation': _attestation_id, 'valid': True})
else:
assert False
else:
Expand All @@ -201,17 +199,15 @@ def yield_test_parts(spec, store, test_data: FCTestData, events):
if event_kind == 'block':
_block_id = get_block_file_name(event_data)
if recovery:
print('recovered block', _block_id)
test_steps.append({'block': _block_id, 'valid': True, 'recovery': True})
test_steps.append({'block': _block_id, 'valid': True})
else:
test_steps.append({'block': _block_id, 'valid': True})
elif event_kind == 'attestation':
_attestation_id = get_attestation_file_name(event_data)
if recovery:
print('recovered attestation', _attestation_id)
if _attestation_id not in test_data.atts:
yield _attestation_id, event_data
test_steps.append({'attestation': _attestation_id, 'valid': True, 'recovery': True})
test_steps.append({'attestation': _attestation_id, 'valid': True})
else:
assert False
test_steps.append({'attestation': _attestation_id, 'valid': True})
Expand Down Expand Up @@ -242,7 +238,8 @@ def yield_test_parts(spec, store, test_data: FCTestData, events):
else:
raise ValueError(f'not implemented {kind}')
next_slot_time = store.genesis_time + (spec.get_current_slot(store) + 1) * spec.config.SECONDS_PER_SLOT
on_tick_and_append_step(spec, store, next_slot_time, test_steps, checks_with_viable_for_head_weights=True)
on_tick_and_append_step(spec, store, next_slot_time, test_steps)
output_store_checks(spec, store, test_steps, with_viable_for_head_weights=True)

yield 'steps', test_steps

Expand Down

0 comments on commit 8545060

Please sign in to comment.