Skip to content

Commit

Permalink
wip: fixing returned diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Dec 8, 2024
1 parent ee2611b commit 7f8df44
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 98 deletions.
48 changes: 27 additions & 21 deletions state-transition/core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package core
import (
"bytes"
"fmt"
"sync"

"github.com/berachain/beacon-kit/config/spec"
"github.com/berachain/beacon-kit/consensus-types/types"
Expand Down Expand Up @@ -99,19 +98,6 @@ type StateProcessor[
ds DepositStore[DepositT]
// metrics is the metrics for the service.
metrics *stateProcessorMetrics

// valSetMu protects valSetByEpoch from concurrent accesses
valSetMu sync.RWMutex

// valSetByEpoch tracks the set of validators active at the latest epochs.
// This is useful to optimize validators set updates.
// Note: Transition may be called multiple times on different,
// non/finalized blocks, so at some point valSetByEpoch may contain
// informations from blocks not finalized. This should be fine as long
// as a block is finalized eventually, and its changes will be the last
// ones.
// We prune the map to preserve only current and previous epoch
valSetByEpoch map[math.Epoch][]ValidatorT
}

// NewStateProcessor creates a new state processor.
Expand Down Expand Up @@ -187,7 +173,6 @@ func NewStateProcessor[
fGetAddressFromPubKey: fGetAddressFromPubKey,
ds: ds,
metrics: newStateProcessorMetrics(telemetrySink),
valSetByEpoch: make(map[math.Epoch][]ValidatorT, 0),
}
}

Expand Down Expand Up @@ -374,22 +359,43 @@ func (sp *StateProcessor[
]) processEpoch(
st BeaconStateT,
) (transition.ValidatorUpdates, error) {
if err := sp.processRewardsAndPenalties(st); err != nil {
slot, err := st.GetSlot()
if err != nil {
return nil, err
}
if err := sp.processRegistryUpdates(st); err != nil {

currentEpoch := sp.cs.SlotToEpoch(slot)
currentActiveVals, err := sp.getActiveVals(st, currentEpoch)
if err != nil {
return nil, err
}
if err := sp.processEffectiveBalanceUpdates(st); err != nil {

if err = sp.processRewardsAndPenalties(st); err != nil {
return nil, err
}
if err = sp.processRegistryUpdates(st); err != nil {
return nil, err
}
if err := sp.processSlashingsReset(st); err != nil {
if err = sp.processEffectiveBalanceUpdates(st); err != nil {
return nil, err
}
if err := sp.processRandaoMixesReset(st); err != nil {
if err = sp.processSlashingsReset(st); err != nil {
return nil, err
}
return sp.processValidatorsSetUpdates(st)
if err = sp.processRandaoMixesReset(st); err != nil {
return nil, err
}
if err = sp.processValidatorSetCap(st); err != nil {
return nil, err
}

nextEpoch := currentEpoch + 1
nextActiveVals, err := sp.getActiveVals(st, nextEpoch)
if err != nil {
return nil, err
}

return sp.validatorSetsDiffs(currentActiveVals, nextActiveVals), nil
}

// processBlockHeader processes the header and ensures it matches the local
Expand Down
11 changes: 10 additions & 1 deletion state-transition/core/state_processor_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,14 @@ func (sp *StateProcessor[
return nil, err
}

return sp.processValidatorsSetUpdates(st)
activeVals, err := sp.getActiveVals(st, 0)
if err != nil {
return nil, err
}
return sp.validatorSetsDiffs(nil, activeVals), nil
}

//nolint:lll // let it be.
func (sp *StateProcessor[
_, _, _, BeaconStateT, _, _, _, _, _, _, _, _, ValidatorT, _, _, _, _,
]) processGenesisActivation(
Expand All @@ -170,9 +175,13 @@ func (sp *StateProcessor[
if err != nil {
return fmt.Errorf("genesis activation, failed listing validators: %w", err)
}
minEffectiveBalance := math.Gwei(sp.cs.EjectionBalance() + sp.cs.EffectiveBalanceIncrement())

var idx math.ValidatorIndex
for _, val := range vals {
if val.GetEffectiveBalance() < minEffectiveBalance {
continue
}
val.SetActivationEligibilityEpoch(0)
val.SetActivationEpoch(0)
idx, err = st.ValidatorIndexByPubkey(val.GetPubkey())
Expand Down
67 changes: 52 additions & 15 deletions state-transition/core/state_processor_staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ func TestTransitionUpdateValidators(t *testing.T) {
genPayloadHeader = new(types.ExecutionPayloadHeader).Empty()
genVersion = version.FromUint32[common.Version](version.Deneb)
)
genVals, err := sp.InitializePreminedBeaconStateFromEth1(
valDiff, err := sp.InitializePreminedBeaconStateFromEth1(
st,
genDeposits,
genPayloadHeader,
genVersion,
)
require.NoError(t, err)
require.Len(t, genVals, len(genDeposits))
require.Len(t, valDiff, len(genDeposits))

// STEP 1: top up a genesis validator balance
blkDeposit := &types.Deposit{
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestTransitionUpdateValidators(t *testing.T) {
require.NoError(t, ds.EnqueueDeposits(blk1.Body.Deposits))

// run the test
valDiff, err := sp.Transition(ctx, st, blk1)
valDiff, err = sp.Transition(ctx, st, blk1)
require.NoError(t, err)
require.Empty(t, valDiff) // validators set updates only at epoch turn

Expand Down Expand Up @@ -167,8 +167,14 @@ func TestTransitionUpdateValidators(t *testing.T) {
valDiff, err = sp.Transition(ctx, st, blk)
require.NoError(t, err)
require.Len(t, valDiff, 1) // just topped up one validator

expectedBalance = genDeposits[2].Amount + blkDeposit.Amount
require.Equal(
t,
&transition.ValidatorUpdate{
Pubkey: blkDeposit.Pubkey,
EffectiveBalance: expectedBalance,
},
valDiff[0],
)
expectedEffectiveBalance = expectedBalance

balance, err = st.GetBalance(idx)
Expand Down Expand Up @@ -252,9 +258,9 @@ func TestTransitionCreateValidator(t *testing.T) {
require.NoError(t, ds.EnqueueDeposits(blk1.Body.Deposits))

// run the test
updatedVals, err := sp.Transition(ctx, st, blk1)
valDiff, err := sp.Transition(ctx, st, blk1)
require.NoError(t, err)
require.Empty(t, updatedVals) // validators set updates only at epoch turn
require.Empty(t, valDiff) // validators set updates only at epoch turn

// check validator balances are duly updated
var (
Expand Down Expand Up @@ -300,7 +306,7 @@ func TestTransitionCreateValidator(t *testing.T) {
},
)

valDiff, err := sp.Transition(ctx, st, blk)
valDiff, err = sp.Transition(ctx, st, blk)
require.NoError(t, err)
require.Empty(t, valDiff) // new validator is only eligible for activation

Expand Down Expand Up @@ -337,8 +343,17 @@ func TestTransitionCreateValidator(t *testing.T) {
)

// run the test
_, err = sp.Transition(ctx, st, blk)
valDiff, err = sp.Transition(ctx, st, blk)
require.NoError(t, err)
require.Len(t, valDiff, 1)
require.Equal(
t,
&transition.ValidatorUpdate{
Pubkey: blkDeposit.Pubkey,
EffectiveBalance: expectedBalance,
},
valDiff[0],
)

extraVal, err = st.ValidatorByIndex(extraValIdx)
require.NoError(t, err)
Expand Down Expand Up @@ -672,8 +687,9 @@ func TestTransitionHittingValidatorsCap_ExtraSmall(t *testing.T) {
require.NoError(t, ds.EnqueueDeposits(blk1.Body.Deposits))

// run the test
_, err = sp.Transition(ctx, st, blk1)
valDiff, err := sp.Transition(ctx, st, blk1)
require.NoError(t, err)
require.Empty(t, valDiff)

extraValIdx, err := st.ValidatorIndexByPubkey(extraValDeposit.Pubkey)
require.NoError(t, err)
Expand Down Expand Up @@ -708,8 +724,9 @@ func TestTransitionHittingValidatorsCap_ExtraSmall(t *testing.T) {
)

// run the test
_, err = sp.Transition(ctx, st, blk)
valDiff, err = sp.Transition(ctx, st, blk)
require.NoError(t, err)
require.Empty(t, valDiff)

// check extra validator is added with Withdraw epoch duly set
extraVal, err = st.ValidatorByIndex(extraValIdx)
Expand Down Expand Up @@ -743,8 +760,9 @@ func TestTransitionHittingValidatorsCap_ExtraSmall(t *testing.T) {
)

// run the test
_, err = sp.Transition(ctx, st, blk)
valDiff, err = sp.Transition(ctx, st, blk)
require.NoError(t, err)
require.Empty(t, valDiff)

extraVal, err = st.ValidatorByIndex(extraValIdx)
require.NoError(t, err)
Expand Down Expand Up @@ -878,8 +896,9 @@ func TestTransitionHittingValidatorsCap_ExtraBig(t *testing.T) {
require.NoError(t, ds.EnqueueDeposits(blk1.Body.Deposits))

// run the test
_, err = sp.Transition(ctx, st, blk1)
valDiff, err := sp.Transition(ctx, st, blk1)
require.NoError(t, err)
require.Empty(t, valDiff)

extraValIdx, err := st.ValidatorIndexByPubkey(extraValDeposit.Pubkey)
require.NoError(t, err)
Expand Down Expand Up @@ -923,8 +942,9 @@ func TestTransitionHittingValidatorsCap_ExtraBig(t *testing.T) {
)

// run the test
_, err = sp.Transition(ctx, st, blk)
valDiff, err = sp.Transition(ctx, st, blk)
require.NoError(t, err)
require.Empty(t, valDiff)

// check extra validator is added with Withdraw epoch duly set
extraVal, err = st.ValidatorByIndex(extraValIdx)
Expand Down Expand Up @@ -965,8 +985,25 @@ func TestTransitionHittingValidatorsCap_ExtraBig(t *testing.T) {
)

// run the test
_, err = sp.Transition(ctx, st, blk)
valDiff, err = sp.Transition(ctx, st, blk)
require.NoError(t, err)
require.Len(t, valDiff, 2)
require.Equal(
t,
&transition.ValidatorUpdate{
Pubkey: extraVal.Pubkey,
EffectiveBalance: extraVal.EffectiveBalance,
},
valDiff[0],
)
require.Equal(
t,
&transition.ValidatorUpdate{
Pubkey: smallestVal.Pubkey,
EffectiveBalance: 0,
},
valDiff[1],
)

extraVal, err = st.ValidatorByIndex(extraValIdx)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 7f8df44

Please sign in to comment.