2022-10-12 16:39:19 +00:00
|
|
|
package state_native
|
|
|
|
|
|
|
|
import (
|
2023-10-06 01:10:05 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
|
|
mathutil "github.com/prysmaticlabs/prysm/v4/math"
|
|
|
|
enginev1 "github.com/prysmaticlabs/prysm/v4/proto/engine/v1"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/runtime/version"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/time/slots"
|
2022-10-12 16:39:19 +00:00
|
|
|
)
|
|
|
|
|
2022-11-07 14:11:16 +00:00
|
|
|
const ETH1AddressOffset = 12
|
|
|
|
|
2022-10-12 16:39:19 +00:00
|
|
|
// NextWithdrawalIndex returns the index that will be assigned to the next withdrawal.
|
|
|
|
func (b *BeaconState) NextWithdrawalIndex() (uint64, error) {
|
|
|
|
if b.version < version.Capella {
|
|
|
|
return 0, errNotSupported("NextWithdrawalIndex", b.version)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
return b.nextWithdrawalIndex, nil
|
|
|
|
}
|
|
|
|
|
2022-12-06 00:36:23 +00:00
|
|
|
// NextWithdrawalValidatorIndex returns the index of the validator which is
|
|
|
|
// next in line for a withdrawal.
|
2023-01-26 14:40:12 +00:00
|
|
|
func (b *BeaconState) NextWithdrawalValidatorIndex() (primitives.ValidatorIndex, error) {
|
2022-10-12 16:39:19 +00:00
|
|
|
if b.version < version.Capella {
|
2022-12-06 00:36:23 +00:00
|
|
|
return 0, errNotSupported("NextWithdrawalValidatorIndex", b.version)
|
2022-10-12 16:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
2022-11-12 18:38:21 +00:00
|
|
|
return b.nextWithdrawalValidatorIndex, nil
|
2022-10-12 16:39:19 +00:00
|
|
|
}
|
2022-11-07 14:11:16 +00:00
|
|
|
|
|
|
|
// ExpectedWithdrawals returns the withdrawals that a proposer will need to pack in the next block
|
|
|
|
// applied to the current state. It is also used by validators to check that the execution payload carried
|
|
|
|
// the right number of withdrawals
|
|
|
|
func (b *BeaconState) ExpectedWithdrawals() ([]*enginev1.Withdrawal, error) {
|
|
|
|
if b.version < version.Capella {
|
|
|
|
return nil, errNotSupported("ExpectedWithdrawals", b.version)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
withdrawals := make([]*enginev1.Withdrawal, 0, params.BeaconConfig().MaxWithdrawalsPerPayload)
|
2022-11-15 02:33:40 +00:00
|
|
|
validatorIndex := b.nextWithdrawalValidatorIndex
|
2022-11-07 14:11:16 +00:00
|
|
|
withdrawalIndex := b.nextWithdrawalIndex
|
|
|
|
epoch := slots.ToEpoch(b.slot)
|
2022-12-16 06:45:26 +00:00
|
|
|
|
2023-10-06 01:10:05 +00:00
|
|
|
validatorsLen := b.validatorsLen()
|
|
|
|
bound := mathutil.Min(uint64(validatorsLen), params.BeaconConfig().MaxValidatorsPerWithdrawalsSweep)
|
2022-12-16 06:45:26 +00:00
|
|
|
for i := uint64(0); i < bound; i++ {
|
2023-10-06 01:10:05 +00:00
|
|
|
val, err := b.validatorAtIndex(validatorIndex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not retrieve validator at index %d", validatorIndex)
|
|
|
|
}
|
|
|
|
balance, err := b.balanceAtIndex(validatorIndex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not retrieve balance at index %d", validatorIndex)
|
|
|
|
}
|
2022-11-15 02:33:40 +00:00
|
|
|
if balance > 0 && isFullyWithdrawableValidator(val, epoch) {
|
2022-11-07 14:11:16 +00:00
|
|
|
withdrawals = append(withdrawals, &enginev1.Withdrawal{
|
2022-12-15 23:42:07 +00:00
|
|
|
Index: withdrawalIndex,
|
|
|
|
ValidatorIndex: validatorIndex,
|
|
|
|
Address: bytesutil.SafeCopyBytes(val.WithdrawalCredentials[ETH1AddressOffset:]),
|
|
|
|
Amount: balance,
|
2022-11-07 14:11:16 +00:00
|
|
|
})
|
|
|
|
withdrawalIndex++
|
|
|
|
} else if isPartiallyWithdrawableValidator(val, balance) {
|
|
|
|
withdrawals = append(withdrawals, &enginev1.Withdrawal{
|
2022-12-15 23:42:07 +00:00
|
|
|
Index: withdrawalIndex,
|
|
|
|
ValidatorIndex: validatorIndex,
|
|
|
|
Address: bytesutil.SafeCopyBytes(val.WithdrawalCredentials[ETH1AddressOffset:]),
|
|
|
|
Amount: balance - params.BeaconConfig().MaxEffectiveBalance,
|
2022-11-07 14:11:16 +00:00
|
|
|
})
|
|
|
|
withdrawalIndex++
|
|
|
|
}
|
|
|
|
if uint64(len(withdrawals)) == params.BeaconConfig().MaxWithdrawalsPerPayload {
|
|
|
|
break
|
|
|
|
}
|
2022-11-12 18:38:21 +00:00
|
|
|
validatorIndex += 1
|
2023-10-06 01:10:05 +00:00
|
|
|
if uint64(validatorIndex) == uint64(validatorsLen) {
|
2022-11-12 18:38:21 +00:00
|
|
|
validatorIndex = 0
|
|
|
|
}
|
2022-11-07 14:11:16 +00:00
|
|
|
}
|
|
|
|
return withdrawals, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// hasETH1WithdrawalCredential returns whether the validator has an ETH1
|
|
|
|
// Withdrawal prefix. It assumes that the caller has a lock on the state
|
|
|
|
func hasETH1WithdrawalCredential(val *ethpb.Validator) bool {
|
|
|
|
if val == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
cred := val.WithdrawalCredentials
|
|
|
|
return len(cred) > 0 && cred[0] == params.BeaconConfig().ETH1AddressWithdrawalPrefixByte
|
|
|
|
}
|
|
|
|
|
|
|
|
// isFullyWithdrawableValidator returns whether the validator is able to perform a full
|
|
|
|
// withdrawal. This differ from the spec helper in that the balance > 0 is not
|
|
|
|
// checked. This function assumes that the caller holds a lock on the state
|
2023-01-26 14:40:12 +00:00
|
|
|
func isFullyWithdrawableValidator(val *ethpb.Validator, epoch primitives.Epoch) bool {
|
2022-11-07 14:11:16 +00:00
|
|
|
if val == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return hasETH1WithdrawalCredential(val) && val.WithdrawableEpoch <= epoch
|
|
|
|
}
|
|
|
|
|
|
|
|
// isPartiallyWithdrawable returns whether the validator is able to perform a
|
|
|
|
// partial withdrawal. This function assumes that the caller has a lock on the state
|
|
|
|
func isPartiallyWithdrawableValidator(val *ethpb.Validator, balance uint64) bool {
|
|
|
|
if val == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
hasMaxBalance := val.EffectiveBalance == params.BeaconConfig().MaxEffectiveBalance
|
|
|
|
hasExcessBalance := balance > params.BeaconConfig().MaxEffectiveBalance
|
|
|
|
return hasETH1WithdrawalCredential(val) && hasExcessBalance && hasMaxBalance
|
|
|
|
}
|