Skip to content
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

Switch to compounding when consolidating with source==target #14511

Merged
merged 2 commits into from
Oct 15, 2024

Conversation

terencechain
Copy link
Member

@terencechain terencechain commented Oct 7, 2024

This PR modifies the handling of switching validators to compounding credentials by consolidating the logic into the process_consolidation_request flow. Key code changes include:

  • Removed switch_to_compounding_validator from process_pending_consolidations.
  • Removed switch_to_compounding_validator from apply_deposit.
  • Added a helper function is_valid_switch_to_compounding_request.
  • Updated process_consolidation_request to check is_valid_switch_to_compounding_request, and if valid, switch the source index validator to compounding.
  • Updated process_consolidation_request to automatically switch the target validator to compounding if it has an eth1 credential.

Note to the reviewers: In process_consolidation_request, we used to return errors in many places, but in the spec, it's a clean return. Since we process requests in one function, whereas the spec processes individual requests, returning errors is dangerous and could lead to a consensus fault. I've adjusted some returns to log and continue. I didn't modify the ones where it says an error can't happen. Please review carefully

Reference: ethereum/consensus-specs#3918

@terencechain terencechain force-pushed the consolidation-switch-to-comp1 branch 3 times, most recently from a4b49a9 to 177febd Compare October 14, 2024 20:31
@terencechain terencechain marked this pull request as ready for review October 14, 2024 20:32
@terencechain terencechain requested a review from a team as a code owner October 14, 2024 20:32
@terencechain terencechain added the Ready For Review A pull request ready for code review label Oct 14, 2024
if err != nil {
return err
}
if err := helpers.DecreaseBalance(st, pc.SourceIndex, activeBalance); err != nil {
if sourceEffectiveBalance > validatorBalance {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: instead of this if statement what about using the min function for 1 to 1 readability

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


sourcePubKey := bytesutil.ToBytes48(req.SourcePubkey)
targetPubKey := bytesutil.ToBytes48(req.TargetPubkey)
if sourcePubKey != targetPubKey {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to do !bytes.Equals check than doing !=

}
sourcePubkey := bytesutil.ToBytes48(cr.SourcePubkey)
targetPubkey := bytesutil.ToBytes48(cr.TargetPubkey)
if sourcePubkey == targetPubkey {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another check for bytes.Equal over ==

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +203 to +205
sourcePubkey := bytesutil.ToBytes48(cr.SourcePubkey)
targetPubkey := bytesutil.ToBytes48(cr.TargetPubkey)
if sourcePubkey == targetPubkey {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, bytes.Equal does not alloc, but converting to [48]byte is a 48 byte alloc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would still need [48]byte when calling ValidatorIndexByPubkey in L224

srcIdx, ok := st.ValidatorIndexByPubkey(sourcePubkey)
tgtIdx, ok := st.ValidatorIndexByPubkey(targetPubkey)

// active_balance = get_active_balance(state, pending_consolidation.source_index)
// decrease_balance(state, pending_consolidation.source_index, active_balance)
// increase_balance(state, pending_consolidation.target_index, active_balance)
// continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// increase_balance(state, pending_consolidation.target_index, active_balance)
// continue
//
// def process_pending_consolidations(state: BeaconState) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double checked this against spec, looks good.

@@ -101,6 +107,16 @@ func ProcessPendingConsolidations(ctx context.Context, st state.BeaconState) err
// state: BeaconState,
// consolidation_request: ConsolidationRequest
// ) -> None:
// if is_valid_switch_to_compounding_request(state, consolidation_request):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double checked this comment block against spec and LGTM.

// if not is_active_validator(source_validator, current_epoch):
// return False
//
// # Verify exit for source have not been initiated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// # Verify exit for source have not been initiated
// # Verify exit for source has not been initiated

The spec has this typo corrected

// if has_eth1_withdrawal_credential(validator):
// validator.withdrawal_credentials = COMPOUNDING_WITHDRAWAL_PREFIX + validator.withdrawal_credentials[1:]
// queue_excess_active_balance(state, index)
// validator.withdrawal_credentials = COMPOUNDING_WITHDRAWAL_PREFIX + validator.withdrawal_credentials[1:]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review this against spec and LGTM.

}
// As per the consensus specification, this error is not considered an assertion.
// Therefore, if the source_pubkey is not found in validator_pubkeys, we simply return false.
srcV, err := st.ValidatorAtIndex(srcIdx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use ValidatorAtIndexReadOnly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@terencechain terencechain added this pull request to the merge queue Oct 15, 2024
Merged via the queue into develop with commit 0a4ed82 Oct 15, 2024
17 of 18 checks passed
@terencechain terencechain deleted the consolidation-switch-to-comp1 branch October 15, 2024 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready For Review A pull request ready for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants