-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add PTC lookbehind minimal state change #5020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
tests/core/pyspec/eth_consensus_specs/test/gloas/epoch_processing/test_process_ptc_update.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| from eth_consensus_specs.test.context import ( | ||
| expect_assertion_error, | ||
| single_phase, | ||
| spec_state_test, | ||
| with_phases, | ||
| ) | ||
| from eth_consensus_specs.test.helpers.constants import GLOAS | ||
| from eth_consensus_specs.test.helpers.epoch_processing import run_epoch_processing_with | ||
|
|
||
|
|
||
| @with_phases([GLOAS]) | ||
| @spec_state_test | ||
| @single_phase | ||
| def test_process_ptc_update_caches_last_slot_ptc(spec, state): | ||
| """ | ||
| Test that process_ptc_update caches the PTC for the current slot | ||
| (last slot of the epoch) into state.previous_epoch_last_ptc. | ||
| """ | ||
| # Advance to last slot of the epoch | ||
| spec.process_slots(state, state.slot + spec.SLOTS_PER_EPOCH - 1) | ||
|
|
||
| # Compute expected PTC for this slot (last slot of epoch, before balance updates) | ||
| expected_ptc = spec.compute_ptc(state, spec.Slot(state.slot)) | ||
|
|
||
| yield from run_epoch_processing_with(spec, state, "process_ptc_update") | ||
|
|
||
| assert list(state.previous_epoch_last_ptc) == list(expected_ptc) | ||
|
|
||
|
|
||
| @with_phases([GLOAS]) | ||
| @spec_state_test | ||
| @single_phase | ||
| def test_get_ptc_returns_cached_previous_for_epoch_boundary(spec, state): | ||
| """ | ||
| Test that after crossing an epoch boundary, get_ptc returns the cached | ||
| previous_epoch_last_ptc for the last slot of the previous epoch. | ||
| """ | ||
| # Advance to first slot of next epoch | ||
| target_slot = spec.SLOTS_PER_EPOCH | ||
| spec.process_slots(state, target_slot) | ||
|
|
||
| # Now state.slot = SLOTS_PER_EPOCH (first slot of epoch 1) | ||
| # Query PTC for slot SLOTS_PER_EPOCH - 1 (last slot of epoch 0) | ||
| last_slot_prev_epoch = spec.Slot(spec.SLOTS_PER_EPOCH - 1) | ||
| ptc = spec.get_ptc(state, last_slot_prev_epoch) | ||
|
|
||
| # Should match previous_epoch_last_ptc cached during epoch processing | ||
| assert list(ptc) == list(state.previous_epoch_last_ptc) | ||
| # Should not be all zeros (real PTC was cached) | ||
| assert any(v != 0 for v in ptc) | ||
|
|
||
|
|
||
| @with_phases([GLOAS]) | ||
| @spec_state_test | ||
| @single_phase | ||
| def test_get_ptc_computes_current_epoch_on_demand(spec, state): | ||
| """ | ||
| Test that get_ptc computes current epoch PTCs on demand via compute_ptc. | ||
| """ | ||
| ptc_slot_0 = spec.get_ptc(state, spec.Slot(0)) | ||
| computed_ptc_slot_0 = spec.compute_ptc(state, spec.Slot(0)) | ||
|
|
||
| assert list(ptc_slot_0) == list(computed_ptc_slot_0) | ||
|
|
||
|
|
||
| @with_phases([GLOAS]) | ||
| @spec_state_test | ||
| @single_phase | ||
| def test_compute_ptc_next_epoch_asserts(spec, state): | ||
| """ | ||
| Test that compute_ptc does not allow next-epoch computation. | ||
| """ | ||
| next_epoch_slot = spec.Slot(spec.SLOTS_PER_EPOCH) | ||
| expect_assertion_error(lambda: spec.compute_ptc(state, next_epoch_slot)) | ||
|
|
||
|
|
||
| @with_phases([GLOAS]) | ||
| @spec_state_test | ||
| @single_phase | ||
| def test_get_ptc_next_epoch_asserts(spec, state): | ||
| """ | ||
| Test that get_ptc does not allow next-epoch lookups. | ||
| """ | ||
| next_epoch_slot = spec.Slot(spec.SLOTS_PER_EPOCH) | ||
| expect_assertion_error(lambda: spec.get_ptc(state, next_epoch_slot)) |
Empty file.
62 changes: 62 additions & 0 deletions
62
tests/core/pyspec/eth_consensus_specs/test/gloas/unittests/validator/test_validator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| from eth_consensus_specs.test.context import ( | ||
| expect_assertion_error, | ||
| single_phase, | ||
| spec_test, | ||
| with_phases, | ||
| with_state, | ||
| ) | ||
| from eth_consensus_specs.test.helpers.constants import GLOAS | ||
| from eth_consensus_specs.test.helpers.state import next_epoch | ||
|
|
||
|
|
||
| def _compute_first_ptc_assignments(spec, state, epoch): | ||
| assignments = {} | ||
| start_slot = spec.compute_start_slot_at_epoch(epoch) | ||
| for slot in range(start_slot, start_slot + spec.SLOTS_PER_EPOCH): | ||
| for validator_index in spec.compute_ptc(state, spec.Slot(slot)): | ||
| assignments.setdefault(validator_index, spec.Slot(slot)) | ||
| return assignments | ||
|
|
||
|
|
||
| def _assert_get_ptc_assignments(spec, state, epoch, assignments): | ||
| assert len(assignments) > 0 | ||
|
|
||
| for validator_index, expected_slot in assignments.items(): | ||
| assert spec.get_ptc_assignment(state, epoch, validator_index) == expected_slot | ||
|
|
||
| unassigned_validator = next( | ||
| (spec.ValidatorIndex(i) for i in range(len(state.validators)) if i not in assignments), | ||
| None, | ||
| ) | ||
| if unassigned_validator is not None: | ||
| assert spec.get_ptc_assignment(state, epoch, unassigned_validator) is None | ||
|
|
||
|
|
||
| @with_phases([GLOAS]) | ||
| @spec_test | ||
| @with_state | ||
| @single_phase | ||
| def test_get_ptc_assignment__previous_epoch(spec, state): | ||
| next_epoch(spec, state) | ||
|
|
||
| epoch = spec.Epoch(spec.get_current_epoch(state) - 1) | ||
| expect_assertion_error(lambda: spec.get_ptc_assignment(state, epoch, spec.ValidatorIndex(0))) | ||
|
|
||
|
|
||
| @with_phases([GLOAS]) | ||
| @spec_test | ||
| @with_state | ||
| @single_phase | ||
| def test_get_ptc_assignment__current_epoch(spec, state): | ||
| epoch = spec.get_current_epoch(state) | ||
| assignments = _compute_first_ptc_assignments(spec, state, epoch) | ||
| _assert_get_ptc_assignments(spec, state, epoch, assignments) | ||
|
|
||
|
|
||
| @with_phases([GLOAS]) | ||
| @spec_test | ||
| @with_state | ||
| @single_phase | ||
| def test_get_ptc_assignment__next_epoch(spec, state): | ||
| epoch = spec.Epoch(spec.get_current_epoch(state) + 1) | ||
| expect_assertion_error(lambda: spec.get_ptc_assignment(state, epoch, spec.ValidatorIndex(0))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.