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

feat(withdrawal): forced withdrawal #1897

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/berad/pkg/state-transition/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ type ReadOnlyBeaconState[
ValidatorIndexByCometBFTAddress(
cometBFTAddress []byte,
) (math.ValidatorIndex, error)
ValidatorIndexByPubkey(
crypto.BLSPubkey,
) (math.ValidatorIndex, error)
}

// WriteOnlyBeaconState is the interface for a write-only beacon state.
Expand All @@ -106,6 +109,7 @@ type WriteOnlyBeaconState[

DecreaseBalance(math.ValidatorIndex, math.Gwei) error
IncreaseBalance(math.ValidatorIndex, math.Gwei) error
SetWithdrawalValidator(math.ValidatorIndex) error
SetEth1DepositIndex(uint64) error
SetFork(ForkT) error
SetGenesisValidatorsRoot(root common.Root) error
Expand Down
17 changes: 14 additions & 3 deletions examples/berad/pkg/state-transition/state_processor_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,21 @@ func (sp *StateProcessor[
func (sp *StateProcessor[
_, _, _, BeaconStateT, _, _, _, _, _, _, _, _, _, _, _, _,
]) processForcedWithdrawals(
_ BeaconStateT,
_ transition.ValidatorUpdates,
st BeaconStateT,
valUpdates transition.ValidatorUpdates,
) error {
// TODO: Implement this function.
// Process the forced withdrawals.
for _, valUpdate := range valUpdates {
idx, err := st.ValidatorIndexByPubkey(valUpdate.Pubkey)
if err != nil {
return err
}

// Set the validator's effective balance to 0.
if err = st.SetWithdrawalValidator(idx); err != nil {
return err
}
}
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions mod/storage/pkg/beacondb/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
GenesisValidatorsRootPrefix
NextWithdrawalIndexPrefix
NextWithdrawalValidatorIndexPrefix
WithdrawalValidatorsPrefix
ForkPrefix
)

Expand Down Expand Up @@ -70,5 +71,6 @@ const (
GenesisValidatorsRootPrefixHumanReadable = "GenesisValidatorsRootPrefix"
NextWithdrawalIndexPrefixHumanReadable = "NextWithdrawalIndexPrefix"
NextWithdrawalValidatorIndexPrefixHumanReadable = "NextWithdrawalValidatorIndexPrefix"
WithdrawalValidatorsPrefixHumanReadable = "WithdrawalValidatorsPrefix"
ForkPrefixHumanReadable = "ForkPrefix"
)
13 changes: 13 additions & 0 deletions mod/storage/pkg/beacondb/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ type KVStore[
// nextWithdrawalValidatorIndex stores the next withdrawal validator index
// for each validator.
nextWithdrawalValidatorIndex sdkcollections.Item[uint64]
// withdrawalValidators stores the list of validators that need to be
// withdrawn.
withdrawalValidators *sdkcollections.IndexedMap[
uint64, ValidatorT, index.ValidatorsIndex[ValidatorT],
]
// Randomness
// randaoMix stores the randao mix for the current epoch.
randaoMix sdkcollections.Map[uint64, []byte]
Expand Down Expand Up @@ -253,6 +258,14 @@ func New[
keys.NextWithdrawalValidatorIndexPrefixHumanReadable,
sdkcollections.Uint64Value,
),
withdrawalValidators: sdkcollections.NewIndexedMap(
schemaBuilder,
sdkcollections.NewPrefix([]byte{keys.WithdrawalValidatorsPrefix}),
keys.WithdrawalValidatorsPrefixHumanReadable,
sdkcollections.Uint64Key,
encoding.SSZValueCodec[ValidatorT]{},
index.NewValidatorsIndex[ValidatorT](schemaBuilder),
),
totalSlashing: sdkcollections.NewItem(
schemaBuilder,
sdkcollections.NewPrefix([]byte{keys.TotalSlashingPrefix}),
Expand Down
37 changes: 36 additions & 1 deletion mod/storage/pkg/beacondb/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (kv *KVStore[
BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT,
ForkT, ValidatorT, ValidatorsT,
]) AddValidatorBartio(val ValidatorT) error {
// Get the ne
// Get the next validator index from the sequence.
idx, err := kv.validatorIndex.Next(kv.ctx)
if err != nil {
return err
Expand Down Expand Up @@ -285,3 +285,38 @@ func (kv *KVStore[
},
)
}

func (kv *KVStore[
BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT,
ForkT, ValidatorT, ValidatorsT,
]) SetWithdrawalValidator(
idx math.ValidatorIndex,
) error {
val, err := kv.ValidatorByIndex(idx)
if err != nil {
return err
}

// Set the withdrawal validator.
if err = kv.withdrawalValidators.Set(kv.ctx, idx.Unwrap(), val); err != nil {
return err
}

// Get the next withdrawal index.
withdrawalIdx, err := kv.nextWithdrawalIndex.Get(kv.ctx)
if err != nil {
return err
}

// Increment the withdrawal index.
if err = kv.nextWithdrawalIndex.Set(kv.ctx, withdrawalIdx+1); err != nil {
return err
}

// Set the validator's balance to 0.
val.SetEffectiveBalance(0)
if err = kv.validators.Set(kv.ctx, idx.Unwrap(), val); err != nil {
return err
}
return kv.SetBalance(idx, 0)
}
2 changes: 2 additions & 0 deletions mod/storage/pkg/beacondb/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ type Validator[SelfT any] interface {
GetEffectiveBalance() math.Gwei
// IsActive checks if the validator is active at the given epoch.
IsActive(epoch math.Epoch) bool
// SetEffectiveBalance sets the effective balance of the validator.
SetEffectiveBalance(math.Gwei)
}
Loading