-
Notifications
You must be signed in to change notification settings - Fork 1k
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
electra: Add pending deposit and consolidation tests #4024
Merged
Merged
Changes from 1 commit
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
125 changes: 125 additions & 0 deletions
125
tests/core/pyspec/eth2spec/test/electra/sanity/test_slots.py
This file contains 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,125 @@ | ||||||
from eth2spec.test.context import ( | ||||||
spec_state_test, | ||||||
with_electra_and_later, | ||||||
) | ||||||
from eth2spec.test.helpers.deposits import prepare_pending_deposit | ||||||
from eth2spec.test.helpers.state import transition_to | ||||||
|
||||||
|
||||||
def run_epoch_processing(spec, state, pending_deposits=[], pending_consolidations=[]): | ||||||
hwwhww marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
# Transition to the last slot of the epoch | ||||||
slot = state.slot + spec.SLOTS_PER_EPOCH - (state.slot % spec.SLOTS_PER_EPOCH) - 1 | ||||||
transition_to(spec, state, slot) | ||||||
state.pending_deposits = pending_deposits | ||||||
state.pending_consolidations = pending_consolidations | ||||||
yield 'pre', state | ||||||
yield 'slots', 1 | ||||||
spec.process_slots(state, state.slot + 1) | ||||||
yield 'post', state | ||||||
|
||||||
assert state.pending_deposits == [] | ||||||
assert state.pending_consolidations == [] | ||||||
|
||||||
|
||||||
@with_electra_and_later | ||||||
@spec_state_test | ||||||
def test_multiple_pending_deposits_same_pubkey(spec, state): | ||||||
# Create multiple deposits with the same pubkey | ||||||
index = len(state.validators) | ||||||
deposit = prepare_pending_deposit(spec, validator_index=index, amount=spec.MIN_ACTIVATION_BALANCE, signed=True) | ||||||
pending_deposits = [deposit, deposit] | ||||||
|
||||||
yield from run_epoch_processing(spec, state, pending_deposits) | ||||||
hwwhww marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
# Check deposit balance is applied correctly | ||||||
assert state.balances[index] == sum(d.amount for d in pending_deposits) | ||||||
assert state.validators[index].effective_balance == spec.MIN_ACTIVATION_BALANCE | ||||||
|
||||||
|
||||||
@with_electra_and_later | ||||||
@spec_state_test | ||||||
def test_multiple_pending_deposits_same_pubkey_compounding(spec, state): | ||||||
# Create multiple deposits with the same pubkey and compounding creds | ||||||
index = len(state.validators) | ||||||
deposit = prepare_pending_deposit( | ||||||
spec, validator_index=index, amount=spec.MIN_ACTIVATION_BALANCE, signed=True, | ||||||
withdrawal_credentials=(spec.COMPOUNDING_WITHDRAWAL_PREFIX + b'\x00' * 11 + b'\x11' * 20) | ||||||
) | ||||||
pending_deposits = [deposit, deposit] | ||||||
|
||||||
yield from run_epoch_processing(spec, state, pending_deposits) | ||||||
hwwhww marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
# Check deposit balance is applied correctly | ||||||
assert state.balances[index] == sum(d.amount for d in pending_deposits) | ||||||
assert state.validators[index].effective_balance == state.balances[index] | ||||||
|
||||||
|
||||||
@with_electra_and_later | ||||||
@spec_state_test | ||||||
def test_multiple_pending_deposits_same_pubkey_below_upward_threshold(spec, state): | ||||||
# Create multiple deposits with top up lower than the upward threshold | ||||||
index = len(state.validators) | ||||||
deposit_0 = prepare_pending_deposit( | ||||||
spec, validator_index=index, | ||||||
amount=(spec.MIN_ACTIVATION_BALANCE - spec.EFFECTIVE_BALANCE_INCREMENT), signed=True | ||||||
) | ||||||
deposit_1 = prepare_pending_deposit( | ||||||
spec, validator_index=index, | ||||||
amount=spec.EFFECTIVE_BALANCE_INCREMENT, signed=True | ||||||
) | ||||||
pending_deposits = [deposit_0, deposit_1] | ||||||
|
||||||
yield from run_epoch_processing(spec, state, pending_deposits) | ||||||
hwwhww marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
# Check deposit balance is applied correctly | ||||||
assert state.balances[index] == sum(d.amount for d in pending_deposits) | ||||||
assert state.validators[index].effective_balance == deposit_0.amount | ||||||
|
||||||
|
||||||
@with_electra_and_later | ||||||
@spec_state_test | ||||||
def test_multiple_pending_deposits_same_pubkey_above_upward_threshold(spec, state): | ||||||
# Create multiple deposits with top up greater than the upward threshold | ||||||
index = len(state.validators) | ||||||
deposit_0 = prepare_pending_deposit( | ||||||
spec, validator_index=index, | ||||||
amount=(spec.MIN_ACTIVATION_BALANCE - spec.EFFECTIVE_BALANCE_INCREMENT), signed=True | ||||||
) | ||||||
amount = spec.EFFECTIVE_BALANCE_INCREMENT // spec.HYSTERESIS_QUOTIENT * spec.HYSTERESIS_UPWARD_MULTIPLIER + 1 | ||||||
deposit_1 = prepare_pending_deposit(spec, validator_index=index, amount=amount, signed=True) | ||||||
pending_deposits = [deposit_0, deposit_1] | ||||||
|
||||||
yield from run_epoch_processing(spec, state, pending_deposits) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
# Check deposit balance is applied correctly | ||||||
balance = state.balances[index] | ||||||
assert balance == sum(d.amount for d in pending_deposits) | ||||||
assert state.validators[index].effective_balance == balance - balance % spec.EFFECTIVE_BALANCE_INCREMENT | ||||||
|
||||||
|
||||||
@with_electra_and_later | ||||||
@spec_state_test | ||||||
def test_pending_consolidation(spec, state): | ||||||
# Create pending consolidation | ||||||
current_epoch = spec.get_current_epoch(state) | ||||||
source_index = spec.get_active_validator_indices(state, current_epoch)[0] | ||||||
target_index = spec.get_active_validator_indices(state, current_epoch)[1] | ||||||
# Set withdrawable epoch to current epoch to allow processing | ||||||
state.validators[source_index].withdrawable_epoch = current_epoch | ||||||
# Set the source withdrawal credential to eth1 | ||||||
state.validators[target_index].withdrawal_credentials = ( | ||||||
spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b"\x00" * 11 + b"\x11" * 20 | ||||||
) | ||||||
# Set the target withdrawal credential to compounding | ||||||
state.validators[target_index].withdrawal_credentials = ( | ||||||
spec.COMPOUNDING_WITHDRAWAL_PREFIX + b"\x00" * 11 + b"\x11" * 20 | ||||||
) | ||||||
pending_consolidations = [spec.PendingConsolidation(source_index=source_index, target_index=target_index)] | ||||||
|
||||||
hwwhww marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
yield from run_epoch_processing(spec, state, pending_consolidations=pending_consolidations) | ||||||
|
||||||
# Check the consolidation is processed correctly | ||||||
assert state.balances[source_index] == 0 | ||||||
assert state.validators[source_index].effective_balance == 0 | ||||||
assert state.balances[target_index] == spec.MIN_ACTIVATION_BALANCE * 2 | ||||||
assert state.validators[target_index].effective_balance == spec.MIN_ACTIVATION_BALANCE * 2 |
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had issues generating spec tests with the changes in this PR
when running
make gen_sanity
I get the following errorwhich seems to happen because the module name is
eth2spec.test.electra.sanity.slots
as per change below