Skip to content

Commit

Permalink
Update 3068 (#11656)
Browse files Browse the repository at this point in the history
* rename last_withdrawal_validator_index

* Update Capella methods to Specs #3068

* Add missing renames

* rename minimal state

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
potuz and prylabs-bulldozer[bot] authored Nov 12, 2022
1 parent d4a3b74 commit de73baa
Show file tree
Hide file tree
Showing 22 changed files with 90 additions and 82 deletions.
7 changes: 6 additions & 1 deletion beacon-chain/core/blocks/withdrawals.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/prysmaticlabs/prysm/v3/beacon-chain/core/signing"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
"github.com/prysmaticlabs/prysm/v3/config/params"
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v3/crypto/hash/htr"
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
enginev1 "github.com/prysmaticlabs/prysm/v3/proto/engine/v1"
Expand Down Expand Up @@ -110,7 +111,11 @@ func ProcessWithdrawals(st state.BeaconState, withdrawals []*enginev1.Withdrawal
if err := st.SetNextWithdrawalIndex(withdrawals[len(withdrawals)-1].WithdrawalIndex + 1); err != nil {
return nil, errors.Wrap(err, "could not set next withdrawal index")
}
if err := st.SetLastWithdrawalValidatorIndex(withdrawals[len(withdrawals)-1].ValidatorIndex); err != nil {
nextValidatorIndex := withdrawals[len(withdrawals)-1].ValidatorIndex + 1
if nextValidatorIndex == types.ValidatorIndex(st.NumValidators()) {
nextValidatorIndex = 0
}
if err := st.SetNextWithdrawalValidatorIndex(nextValidatorIndex); err != nil {
return nil, errors.Wrap(err, "could not set latest withdrawal validator index")
}
}
Expand Down
52 changes: 26 additions & 26 deletions beacon-chain/core/blocks/withdrawals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ func TestProcessWithdrawals(t *testing.T) {

type args struct {
Name string
LastWithdrawalValidatorIndex types.ValidatorIndex
NextWithdrawalValidatorIndex types.ValidatorIndex
NextWithdrawalIndex uint64
FullWithdrawalIndices []types.ValidatorIndex
PartialWithdrawalIndices []types.ValidatorIndex
Withdrawals []*enginev1.Withdrawal
}
type control struct {
LastWithdrawalValidatorIndex types.ValidatorIndex
NextWithdrawalValidatorIndex types.ValidatorIndex
NextWithdrawalIndex uint64
ExpectedError bool
Balances map[uint64]uint64
Expand Down Expand Up @@ -254,26 +254,26 @@ func TestProcessWithdrawals(t *testing.T) {
{
Args: args{
Name: "success no withdrawals",
LastWithdrawalValidatorIndex: 10,
NextWithdrawalValidatorIndex: 10,
NextWithdrawalIndex: 3,
},
Control: control{
LastWithdrawalValidatorIndex: 10,
NextWithdrawalValidatorIndex: 10,
NextWithdrawalIndex: 3,
},
},
{
Args: args{
Name: "success one full withdrawal",
NextWithdrawalIndex: 3,
LastWithdrawalValidatorIndex: 5,
NextWithdrawalValidatorIndex: 5,
FullWithdrawalIndices: []types.ValidatorIndex{1},
Withdrawals: []*enginev1.Withdrawal{
fullWithdrawal(1, 3),
},
},
Control: control{
LastWithdrawalValidatorIndex: 1,
NextWithdrawalValidatorIndex: 2,
NextWithdrawalIndex: 4,
Balances: map[uint64]uint64{1: 0},
},
Expand All @@ -282,14 +282,14 @@ func TestProcessWithdrawals(t *testing.T) {
Args: args{
Name: "success one partial withdrawal",
NextWithdrawalIndex: 21,
LastWithdrawalValidatorIndex: 37,
NextWithdrawalValidatorIndex: 37,
PartialWithdrawalIndices: []types.ValidatorIndex{7},
Withdrawals: []*enginev1.Withdrawal{
partialWithdrawal(7, 21),
},
},
Control: control{
LastWithdrawalValidatorIndex: 7,
NextWithdrawalValidatorIndex: 8,
NextWithdrawalIndex: 22,
Balances: map[uint64]uint64{7: maxEffectiveBalance},
},
Expand All @@ -298,15 +298,15 @@ func TestProcessWithdrawals(t *testing.T) {
Args: args{
Name: "success many full withdrawals",
NextWithdrawalIndex: 22,
LastWithdrawalValidatorIndex: 4,
NextWithdrawalValidatorIndex: 4,
FullWithdrawalIndices: []types.ValidatorIndex{7, 19, 28, 1},
Withdrawals: []*enginev1.Withdrawal{
fullWithdrawal(7, 22), fullWithdrawal(19, 23), fullWithdrawal(28, 24),
fullWithdrawal(1, 25),
},
},
Control: control{
LastWithdrawalValidatorIndex: 1,
NextWithdrawalValidatorIndex: 2,
NextWithdrawalIndex: 26,
Balances: map[uint64]uint64{7: 0, 19: 0, 28: 0, 1: 0},
},
Expand All @@ -315,15 +315,15 @@ func TestProcessWithdrawals(t *testing.T) {
Args: args{
Name: "success many partial withdrawals",
NextWithdrawalIndex: 22,
LastWithdrawalValidatorIndex: 4,
NextWithdrawalValidatorIndex: 4,
PartialWithdrawalIndices: []types.ValidatorIndex{7, 19, 28, 1},
Withdrawals: []*enginev1.Withdrawal{
partialWithdrawal(7, 22), partialWithdrawal(19, 23), partialWithdrawal(28, 24),
partialWithdrawal(1, 25),
},
},
Control: control{
LastWithdrawalValidatorIndex: 1,
NextWithdrawalValidatorIndex: 2,
NextWithdrawalIndex: 26,
Balances: map[uint64]uint64{
7: maxEffectiveBalance,
Expand All @@ -337,7 +337,7 @@ func TestProcessWithdrawals(t *testing.T) {
Args: args{
Name: "success many withdrawals",
NextWithdrawalIndex: 22,
LastWithdrawalValidatorIndex: 12,
NextWithdrawalValidatorIndex: 12,
FullWithdrawalIndices: []types.ValidatorIndex{7, 19, 28},
PartialWithdrawalIndices: []types.ValidatorIndex{2, 1, 89, 15},
Withdrawals: []*enginev1.Withdrawal{
Expand All @@ -347,7 +347,7 @@ func TestProcessWithdrawals(t *testing.T) {
},
},
Control: control{
LastWithdrawalValidatorIndex: 7,
NextWithdrawalValidatorIndex: 8,
NextWithdrawalIndex: 29,
Balances: map[uint64]uint64{
7: 0, 19: 0, 28: 0,
Expand All @@ -360,7 +360,7 @@ func TestProcessWithdrawals(t *testing.T) {
Args: args{
Name: "success more than max fully withdrawals",
NextWithdrawalIndex: 22,
LastWithdrawalValidatorIndex: 0,
NextWithdrawalValidatorIndex: 0,
FullWithdrawalIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6, 7, 8, 9, 21, 22, 23, 24, 25, 26, 27, 29, 35, 89},
Withdrawals: []*enginev1.Withdrawal{
fullWithdrawal(1, 22), fullWithdrawal(2, 23), fullWithdrawal(3, 24),
Expand All @@ -372,7 +372,7 @@ func TestProcessWithdrawals(t *testing.T) {
},
},
Control: control{
LastWithdrawalValidatorIndex: 27,
NextWithdrawalValidatorIndex: 28,
NextWithdrawalIndex: 38,
Balances: map[uint64]uint64{
1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0,
Expand All @@ -384,7 +384,7 @@ func TestProcessWithdrawals(t *testing.T) {
Args: args{
Name: "success more than max partially withdrawals",
NextWithdrawalIndex: 22,
LastWithdrawalValidatorIndex: 0,
NextWithdrawalValidatorIndex: 0,
PartialWithdrawalIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6, 7, 8, 9, 21, 22, 23, 24, 25, 26, 27, 29, 35, 89},
Withdrawals: []*enginev1.Withdrawal{
partialWithdrawal(1, 22), partialWithdrawal(2, 23), partialWithdrawal(3, 24),
Expand All @@ -396,7 +396,7 @@ func TestProcessWithdrawals(t *testing.T) {
},
},
Control: control{
LastWithdrawalValidatorIndex: 27,
NextWithdrawalValidatorIndex: 28,
NextWithdrawalIndex: 38,
Balances: map[uint64]uint64{
1: maxEffectiveBalance,
Expand All @@ -422,7 +422,7 @@ func TestProcessWithdrawals(t *testing.T) {
Args: args{
Name: "failure wrong number of partial withdrawal",
NextWithdrawalIndex: 21,
LastWithdrawalValidatorIndex: 37,
NextWithdrawalValidatorIndex: 37,
PartialWithdrawalIndices: []types.ValidatorIndex{7},
Withdrawals: []*enginev1.Withdrawal{
partialWithdrawal(7, 21), partialWithdrawal(9, 22),
Expand All @@ -436,7 +436,7 @@ func TestProcessWithdrawals(t *testing.T) {
Args: args{
Name: "failure invalid withdrawal index",
NextWithdrawalIndex: 22,
LastWithdrawalValidatorIndex: 4,
NextWithdrawalValidatorIndex: 4,
FullWithdrawalIndices: []types.ValidatorIndex{7, 19, 28, 1},
Withdrawals: []*enginev1.Withdrawal{
fullWithdrawal(7, 22), fullWithdrawal(19, 23), fullWithdrawal(28, 25),
Expand All @@ -451,7 +451,7 @@ func TestProcessWithdrawals(t *testing.T) {
Args: args{
Name: "failure invalid validator index",
NextWithdrawalIndex: 22,
LastWithdrawalValidatorIndex: 4,
NextWithdrawalValidatorIndex: 4,
FullWithdrawalIndices: []types.ValidatorIndex{7, 19, 28, 1},
Withdrawals: []*enginev1.Withdrawal{
fullWithdrawal(7, 22), fullWithdrawal(19, 23), fullWithdrawal(27, 24),
Expand All @@ -466,7 +466,7 @@ func TestProcessWithdrawals(t *testing.T) {
Args: args{
Name: "failure invalid withdrawal amount",
NextWithdrawalIndex: 22,
LastWithdrawalValidatorIndex: 4,
NextWithdrawalValidatorIndex: 4,
FullWithdrawalIndices: []types.ValidatorIndex{7, 19, 28, 1},
Withdrawals: []*enginev1.Withdrawal{
fullWithdrawal(7, 22), fullWithdrawal(19, 23), partialWithdrawal(28, 24),
Expand All @@ -481,7 +481,7 @@ func TestProcessWithdrawals(t *testing.T) {
Args: args{
Name: "failure validator not fully withdrawable",
NextWithdrawalIndex: 22,
LastWithdrawalValidatorIndex: 4,
NextWithdrawalValidatorIndex: 4,
FullWithdrawalIndices: []types.ValidatorIndex{notWithdrawableIndex},
Withdrawals: []*enginev1.Withdrawal{
fullWithdrawal(notWithdrawableIndex, 22),
Expand All @@ -495,7 +495,7 @@ func TestProcessWithdrawals(t *testing.T) {
Args: args{
Name: "failure validator not partially withdrawable",
NextWithdrawalIndex: 22,
LastWithdrawalValidatorIndex: 4,
NextWithdrawalValidatorIndex: 4,
PartialWithdrawalIndices: []types.ValidatorIndex{notPartiallyWithdrawable},
Withdrawals: []*enginev1.Withdrawal{
fullWithdrawal(notPartiallyWithdrawable, 22),
Expand All @@ -510,7 +510,7 @@ func TestProcessWithdrawals(t *testing.T) {
checkPostState := func(t *testing.T, expected control, st state.BeaconState) {
l, err := st.LastWithdrawalValidatorIndex()
require.NoError(t, err)
require.Equal(t, expected.LastWithdrawalValidatorIndex, l)
require.Equal(t, expected.NextWithdrawalValidatorIndex, l)

n, err := st.NextWithdrawalIndex()
require.NoError(t, err)
Expand Down Expand Up @@ -563,7 +563,7 @@ func TestProcessWithdrawals(t *testing.T) {
require.NoError(t, err)
spb := &ethpb.BeaconStateCapella{
Slot: slot,
LastWithdrawalValidatorIndex: test.Args.LastWithdrawalValidatorIndex,
NextWithdrawalValidatorIndex: test.Args.NextWithdrawalValidatorIndex,
NextWithdrawalIndex: test.Args.NextWithdrawalIndex,
}
st, err := prepareValidators(spb, test.Args)
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/capella/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func UpgradeToCapella(state state.BeaconState) (state.BeaconState, error) {
WithdrawalsRoot: make([]byte, 32),
},
NextWithdrawalIndex: 0,
LastWithdrawalValidatorIndex: 0,
NextWithdrawalValidatorIndex: 0,
}

return state_native.InitializeFromProtoUnsafeCapella(s)
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type WriteOnlyBeaconState interface {
AppendHistoricalRoots(root [32]byte) error
SetLatestExecutionPayloadHeader(payload interfaces.ExecutionData) error
SetNextWithdrawalIndex(i uint64) error
SetLastWithdrawalValidatorIndex(i types.ValidatorIndex) error
SetNextWithdrawalValidatorIndex(i types.ValidatorIndex) error
}

// ReadOnlyValidator defines a struct which only has read access to validator methods.
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/state/state-native/beacon_state_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type BeaconState struct {
latestExecutionPayloadHeader *enginev1.ExecutionPayloadHeader
latestExecutionPayloadHeaderCapella *enginev1.ExecutionPayloadHeaderCapella
nextWithdrawalIndex uint64
lastWithdrawalValidatorIndex eth2types.ValidatorIndex
nextWithdrawalValidatorIndex eth2types.ValidatorIndex

lock sync.RWMutex
dirtyFields map[nativetypes.FieldIndex]bool
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/state/state-native/beacon_state_minimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type BeaconState struct {
latestExecutionPayloadHeader *enginev1.ExecutionPayloadHeader
latestExecutionPayloadHeaderCapella *enginev1.ExecutionPayloadHeaderCapella
nextWithdrawalIndex uint64
lastWithdrawalValidatorIndex eth2types.ValidatorIndex
nextWithdrawalValidatorIndex eth2types.ValidatorIndex

lock sync.RWMutex
dirtyFields map[nativetypes.FieldIndex]bool
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/state/state-native/getters_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (b *BeaconState) ToProtoUnsafe() interface{} {
NextSyncCommittee: b.nextSyncCommittee,
LatestExecutionPayloadHeader: b.latestExecutionPayloadHeaderCapella,
NextWithdrawalIndex: b.nextWithdrawalIndex,
LastWithdrawalValidatorIndex: b.lastWithdrawalValidatorIndex,
NextWithdrawalValidatorIndex: b.nextWithdrawalValidatorIndex,
}
default:
return nil
Expand Down Expand Up @@ -251,7 +251,7 @@ func (b *BeaconState) ToProto() interface{} {
NextSyncCommittee: b.nextSyncCommitteeVal(),
LatestExecutionPayloadHeader: b.latestExecutionPayloadHeaderCapellaVal(),
NextWithdrawalIndex: b.nextWithdrawalIndex,
LastWithdrawalValidatorIndex: b.lastWithdrawalValidatorIndex,
NextWithdrawalValidatorIndex: b.nextWithdrawalValidatorIndex,
}
default:
return nil
Expand Down
15 changes: 9 additions & 6 deletions beacon-chain/state/state-native/getters_withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (b *BeaconState) LastWithdrawalValidatorIndex() (types.ValidatorIndex, erro
b.lock.RLock()
defer b.lock.RUnlock()

return b.lastWithdrawalValidatorIndex, nil
return b.nextWithdrawalValidatorIndex, nil
}

// ExpectedWithdrawals returns the withdrawals that a proposer will need to pack in the next block
Expand All @@ -49,14 +49,13 @@ func (b *BeaconState) ExpectedWithdrawals() ([]*enginev1.Withdrawal, error) {
defer b.lock.RUnlock()

withdrawals := make([]*enginev1.Withdrawal, 0, params.BeaconConfig().MaxWithdrawalsPerPayload)
validatorIndex := b.lastWithdrawalValidatorIndex
validatorIndex := b.nextWithdrawalValidatorIndex + 1
if uint64(validatorIndex) == uint64(len(b.validators)) {
validatorIndex = 0
}
withdrawalIndex := b.nextWithdrawalIndex
epoch := slots.ToEpoch(b.slot)
for range b.validators {
validatorIndex += 1
if uint64(validatorIndex) == uint64(len(b.validators)) {
validatorIndex = types.ValidatorIndex(0)
}
val := b.validators[validatorIndex]
balance := b.balances[validatorIndex]
if isFullyWithdrawableValidator(val, epoch) {
Expand All @@ -79,6 +78,10 @@ func (b *BeaconState) ExpectedWithdrawals() ([]*enginev1.Withdrawal, error) {
if uint64(len(withdrawals)) == params.BeaconConfig().MaxWithdrawalsPerPayload {
break
}
validatorIndex += 1
if uint64(validatorIndex) == uint64(len(b.validators)) {
validatorIndex = 0
}
}
return withdrawals, nil
}
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/state/state-native/getters_withdrawal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestNextWithdrawalIndex(t *testing.T) {

func TestLastWithdrawalValidatorIndex(t *testing.T) {
t.Run("ok", func(t *testing.T) {
s := BeaconState{version: version.Capella, lastWithdrawalValidatorIndex: 123}
s := BeaconState{version: version.Capella, nextWithdrawalValidatorIndex: 123}
i, err := s.LastWithdrawalValidatorIndex()
require.NoError(t, err)
assert.Equal(t, types.ValidatorIndex(123), i)
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestExpectedWithdrawals(t *testing.T) {
version: version.Capella,
validators: make([]*ethpb.Validator, 100),
balances: make([]uint64, 100),
lastWithdrawalValidatorIndex: 20,
nextWithdrawalValidatorIndex: 20,
}
for i := range s.validators {
s.balances[i] = params.BeaconConfig().MaxEffectiveBalance
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/state/state-native/hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ func ComputeFieldRootsWithHasher(ctx context.Context, state *BeaconState) ([][]b

// Next partial withdrawal validator index root.
nextPartialWithdrawalValidatorIndexRoot := make([]byte, 32)
binary.LittleEndian.PutUint64(nextPartialWithdrawalValidatorIndexRoot, uint64(state.lastWithdrawalValidatorIndex))
fieldRoots[nativetypes.LastWithdrawalValidatorIndex.RealPosition()] = nextPartialWithdrawalValidatorIndexRoot
binary.LittleEndian.PutUint64(nextPartialWithdrawalValidatorIndexRoot, uint64(state.nextWithdrawalValidatorIndex))
fieldRoots[nativetypes.NextWithdrawalValidatorIndex.RealPosition()] = nextPartialWithdrawalValidatorIndexRoot
}

return fieldRoots, nil
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/state/state-native/hasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func TestComputeFieldRootsWithHasher_Capella(t *testing.T) {
require.NoError(t, err)
require.NoError(t, beaconState.SetLatestExecutionPayloadHeader(wrappedHeader))
require.NoError(t, beaconState.SetNextWithdrawalIndex(123))
require.NoError(t, beaconState.SetLastWithdrawalValidatorIndex(123))
require.NoError(t, beaconState.SetNextWithdrawalValidatorIndex(123))

nativeState, ok := beaconState.(*statenative.BeaconState)
require.Equal(t, true, ok)
Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/state/state-native/setters_withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ func (b *BeaconState) SetNextWithdrawalIndex(i uint64) error {

// SetLastWithdrawalValidatorIndex sets the index of the validator which is
// next in line for a partial withdrawal.
func (b *BeaconState) SetLastWithdrawalValidatorIndex(i types.ValidatorIndex) error {
func (b *BeaconState) SetNextWithdrawalValidatorIndex(i types.ValidatorIndex) error {
if b.version < version.Capella {
return errNotSupported("SetNextPartialWithdrawalValidatorIndex", b.version)
return errNotSupported("SetNextWithdrawalValidatorIndex", b.version)
}

b.lock.Lock()
defer b.lock.Unlock()

b.lastWithdrawalValidatorIndex = i
b.nextWithdrawalValidatorIndex = i
return nil
}
Loading

0 comments on commit de73baa

Please sign in to comment.