-
Notifications
You must be signed in to change notification settings - Fork 155
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(state-transition): make validators epochs handling close to Eth2.0 specs #2226
Changes from 15 commits
7409de1
51a12a0
763ed85
23ccf36
0c03846
574099e
1cd77bf
712c4bf
ee2611b
7f8df44
cda83f6
9f30dd8
a720a15
2772911
c69dea0
69c753e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,14 +23,12 @@ package core | |
import ( | ||
"bytes" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/berachain/beacon-kit/config/spec" | ||
"github.com/berachain/beacon-kit/consensus-types/types" | ||
"github.com/berachain/beacon-kit/errors" | ||
"github.com/berachain/beacon-kit/log" | ||
"github.com/berachain/beacon-kit/primitives/common" | ||
"github.com/berachain/beacon-kit/primitives/constants" | ||
"github.com/berachain/beacon-kit/primitives/crypto" | ||
"github.com/berachain/beacon-kit/primitives/math" | ||
"github.com/berachain/beacon-kit/primitives/transition" | ||
|
@@ -100,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. | ||
|
@@ -188,7 +173,6 @@ func NewStateProcessor[ | |
fGetAddressFromPubKey: fGetAddressFromPubKey, | ||
ds: ds, | ||
metrics: newStateProcessorMetrics(telemetrySink), | ||
valSetByEpoch: make(map[math.Epoch][]ValidatorT, 0), | ||
} | ||
} | ||
|
||
|
@@ -380,24 +364,20 @@ func (sp *StateProcessor[ | |
return nil, err | ||
} | ||
|
||
switch { | ||
case sp.cs.DepositEth1ChainID() == spec.BartioChainID: | ||
if err = sp.hollowProcessRewardsAndPenalties(st); err != nil { | ||
return nil, err | ||
} | ||
case sp.cs.DepositEth1ChainID() == spec.BoonetEth1ChainID && | ||
slot < math.U64(spec.BoonetFork3Height): | ||
// We cannot simply drop hollowProcessRewardsAndPenalties because | ||
// appHash accounts for the list of operations carried out | ||
// over the state even if the operations does not affect the state | ||
// (rewards and penalties are always zero at this stage of beaconKit) | ||
if err = sp.hollowProcessRewardsAndPenalties(st); err != nil { | ||
return nil, err | ||
} | ||
default: | ||
// no real need to perform hollowProcessRewardsAndPenalties | ||
Comment on lines
-383
to
-398
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved into processRewardsAndPenalties to have fork switches less in the way of mostly read code |
||
// track validators set before updating it, to be able to | ||
// inform consensus of the validators set changes | ||
currentEpoch := sp.cs.SlotToEpoch(slot) | ||
currentActiveVals, err := sp.getActiveVals(st, currentEpoch) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = sp.processRewardsAndPenalties(st); err != nil { | ||
return nil, err | ||
} | ||
if err = sp.processRegistryUpdates(st); err != nil { | ||
return nil, err | ||
} | ||
if err = sp.processEffectiveBalanceUpdates(st); err != nil { | ||
return nil, err | ||
} | ||
|
@@ -407,7 +387,21 @@ func (sp *StateProcessor[ | |
if err = sp.processRandaoMixesReset(st); err != nil { | ||
return nil, err | ||
} | ||
return sp.processValidatorsSetUpdates(st) | ||
|
||
// only after we have fully updated validators, we enforce | ||
// a cap on the validators set | ||
if err = sp.processValidatorSetCap(st); err != nil { | ||
return nil, err | ||
} | ||
|
||
// finally compute diffs in validator set to duly update consensus | ||
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 | ||
|
@@ -492,40 +486,6 @@ func (sp *StateProcessor[ | |
return st.SetLatestBlockHeader(lbh) | ||
} | ||
|
||
func (sp *StateProcessor[ | ||
_, _, _, BeaconStateT, _, _, _, _, _, _, _, _, _, _, _, _, _, | ||
]) hollowProcessRewardsAndPenalties(st BeaconStateT) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved to its own file to be out of the way |
||
slot, err := st.GetSlot() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if sp.cs.SlotToEpoch(slot) == math.U64(constants.GenesisEpoch) { | ||
return nil | ||
} | ||
|
||
// this has been simplified to make clear that | ||
// we are not really doing anything here | ||
valCount, err := st.GetTotalValidators() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for i := range valCount { | ||
// Increase the balance of the validator. | ||
if err = st.IncreaseBalance(math.ValidatorIndex(i), 0); err != nil { | ||
return err | ||
} | ||
|
||
// Decrease the balance of the validator. | ||
if err = st.DecreaseBalance(math.ValidatorIndex(i), 0); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// processEffectiveBalanceUpdates as defined in the Ethereum 2.0 specification. | ||
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#effective-balances-updates | ||
// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
package core | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/berachain/beacon-kit/config/spec" | ||
"github.com/berachain/beacon-kit/primitives/common" | ||
"github.com/berachain/beacon-kit/primitives/constants" | ||
|
@@ -32,7 +34,7 @@ import ( | |
|
||
// InitializePreminedBeaconStateFromEth1 initializes the beacon state. | ||
// | ||
//nolint:gocognit // todo fix. | ||
//nolint:gocognit,funlen // todo fix. | ||
func (sp *StateProcessor[ | ||
_, BeaconBlockBodyT, BeaconBlockHeaderT, BeaconStateT, _, DepositT, | ||
Eth1DataT, _, ExecutionPayloadHeaderT, ForkT, _, _, ValidatorT, _, _, _, _, | ||
|
@@ -103,6 +105,11 @@ func (sp *StateProcessor[ | |
} | ||
} | ||
|
||
// process activations | ||
if err := sp.processGenesisActivation(st); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Handle special case bartio genesis. | ||
validatorsRoot := common.Root(hex.MustToBytes(spec.BartioValRoot)) | ||
if sp.cs.DepositEth1ChainID() != spec.BartioChainID { | ||
|
@@ -143,5 +150,48 @@ 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( | ||
st BeaconStateT, | ||
) error { | ||
switch { | ||
case sp.cs.DepositEth1ChainID() == spec.BartioChainID: | ||
// nothing to do | ||
return nil | ||
case sp.cs.DepositEth1ChainID() == spec.BoonetEth1ChainID: | ||
// nothing to do | ||
return nil | ||
default: | ||
vals, err := st.GetValidators() | ||
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 | ||
} | ||
Comment on lines
+182
to
+184
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a sanity check for myself: this case should only happen if we include a genesis deposit under min stake? In which case we don't set the activation epoch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct. I synced the first 2000 blocks and had no issue, so we should be good |
||
val.SetActivationEligibilityEpoch(0) | ||
val.SetActivationEpoch(0) | ||
idx, err = st.ValidatorIndexByPubkey(val.GetPubkey()) | ||
if err != nil { | ||
return err | ||
} | ||
if err = st.UpdateValidatorAtIndex(idx, val); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed anymore, once validators epochs are duly handled