forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Registry updates processing. (erigontech#6820)
- Loading branch information
1 parent
8e6c1ee
commit 07af929
Showing
8 changed files
with
141 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package transition_test | ||
|
||
import ( | ||
_ "embed" | ||
"testing" | ||
|
||
"github.com/ledgerwatch/erigon/cl/clparams" | ||
"github.com/ledgerwatch/erigon/cl/utils" | ||
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/state" | ||
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/transition" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
//go:embed test_data/rewards_penalty_test_expected.ssz_snappy | ||
var expectedRewardsPenaltyState []byte | ||
|
||
//go:embed test_data/rewards_penalty_test_state.ssz_snappy | ||
var startingRewardsPenaltyState []byte | ||
|
||
//go:embed test_data/registry_updates_test_expected.ssz_snappy | ||
var expectedRegistryUpdatesState []byte | ||
|
||
//go:embed test_data/registry_updates_test_state.ssz_snappy | ||
var startingRegistryUpdatesState []byte | ||
|
||
func TestProcessRewardsAndPenalties(t *testing.T) { | ||
// Load test states. | ||
testState := state.New(&clparams.MainnetBeaconConfig) | ||
require.NoError(t, utils.DecodeSSZSnappyWithVersion(testState, startingRewardsPenaltyState, int(clparams.BellatrixVersion))) | ||
expected := state.New(&clparams.MainnetBeaconConfig) | ||
require.NoError(t, utils.DecodeSSZSnappyWithVersion(expected, expectedRewardsPenaltyState, int(clparams.BellatrixVersion))) | ||
// Make up state transistor | ||
s := transition.New(testState, &clparams.MainnetBeaconConfig, nil, false) | ||
// Do processing | ||
require.NoError(t, s.ProcessRewardsAndPenalties()) | ||
// Now compare if the two states are the same by taking their root and comparing. | ||
haveRoot, err := testState.HashSSZ() | ||
require.NoError(t, err) | ||
expectedRoot, err := testState.HashSSZ() | ||
require.NoError(t, err) | ||
// Lastly compare | ||
require.Equal(t, expectedRoot, haveRoot) | ||
} | ||
|
||
func TestProcessRegistryUpdates(t *testing.T) { | ||
// Load test states. | ||
testState := state.New(&clparams.MainnetBeaconConfig) | ||
require.NoError(t, utils.DecodeSSZSnappyWithVersion(testState, startingRegistryUpdatesState, int(clparams.BellatrixVersion))) | ||
expected := state.New(&clparams.MainnetBeaconConfig) | ||
require.NoError(t, utils.DecodeSSZSnappyWithVersion(expected, expectedRegistryUpdatesState, int(clparams.BellatrixVersion))) | ||
// Make up state transistor | ||
s := transition.New(testState, &clparams.MainnetBeaconConfig, nil, false) | ||
// Do processing | ||
require.NoError(t, s.ProcessRegistryUpdates()) | ||
// Now compare if the two states are the same by taking their root and comparing. | ||
haveRoot, err := testState.HashSSZ() | ||
require.NoError(t, err) | ||
expectedRoot, err := testState.HashSSZ() | ||
require.NoError(t, err) | ||
// Lastly compare | ||
require.Equal(t, expectedRoot, haveRoot) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package transition | ||
|
||
import "sort" | ||
|
||
const preAllocatedSizeActivationQueue = 8192 | ||
|
||
// computeActivationExitEpoch is Implementation of compute_activation_exit_epoch. Defined in https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#compute_activation_exit_epoch. | ||
func (s *StateTransistor) computeActivationExitEpoch(epoch uint64) uint64 { | ||
return epoch + 1 + s.beaconConfig.MaxSeedLookahead | ||
} | ||
|
||
// ProcessRegistyUpdates updates every epoch the activation status of validators. Specs at: https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#registry-updates. | ||
func (s *StateTransistor) ProcessRegistryUpdates() error { | ||
currentEpoch := s.state.Epoch() | ||
// start also initializing the activation queue. | ||
activationQueue := make([]uint64, 0, preAllocatedSizeActivationQueue) | ||
validators := s.state.Validators() | ||
// Process activation eligibility and ejections. | ||
for validatorIndex, validator := range validators { | ||
if s.state.IsValidatorEligibleForActivationQueue(validator) { | ||
validator.ActivationEligibilityEpoch = currentEpoch + 1 | ||
if err := s.state.SetValidatorAt(validatorIndex, validator); err != nil { | ||
return err | ||
} | ||
} | ||
if validator.Active(currentEpoch) && validator.EffectiveBalance <= s.beaconConfig.EjectionBalance { | ||
if err := s.state.InitiateValidatorExit(uint64(validatorIndex)); err != nil { | ||
return err | ||
} | ||
} | ||
// Insert in the activation queue in case. | ||
if s.state.IsValidatorEligibleForActivation(validator) { | ||
activationQueue = append(activationQueue, uint64(validatorIndex)) | ||
} | ||
} | ||
// order the queue accordingly. | ||
sort.Slice(activationQueue, func(i, j int) bool { | ||
// Order by the sequence of activation_eligibility_epoch setting and then index. | ||
if validators[i].ActivationEligibilityEpoch != validators[j].ActivationEligibilityEpoch { | ||
return validators[i].ActivationEligibilityEpoch < validators[j].ActivationEligibilityEpoch | ||
} | ||
return activationQueue[i] < activationQueue[j] | ||
}) | ||
// Only process up to epoch limit. | ||
for _, validatorIndex := range activationQueue[:s.state.GetValidatorChurnLimit()] { | ||
validator, err := s.state.ValidatorAt(int(validatorIndex)) | ||
if err != nil { | ||
return err | ||
} | ||
validator.ActivationEpoch = s.computeActivationExitEpoch(currentEpoch) | ||
if err := s.state.SetValidatorAt(int(validatorIndex), &validator); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
37 changes: 0 additions & 37 deletions
37
cmd/erigon-cl/core/transition/process_rewards_and_penalties_test.go
This file was deleted.
Oops, something went wrong.
Binary file added
BIN
+172 KB
cmd/erigon-cl/core/transition/test_data/registry_updates_test_expected.ssz_snappy
Binary file not shown.
Binary file added
BIN
+172 KB
cmd/erigon-cl/core/transition/test_data/registry_updates_test_state.ssz_snappy
Binary file not shown.
File renamed without changes.
File renamed without changes.