mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 21:08:10 -05:00
Replace validator wait for activation stream with polling (#14514)
* wip, waitForNextEpoch Broken * fixing wait for activation and timings * updating tests wip * fixing tests * deprecating wait for activation stream * removing duplicate test * Update validator/client/wait_for_activation.go Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com> * Update CHANGELOG.md Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com> * Update CHANGELOG.md Co-authored-by: Radosław Kapka <rkapka@wp.pl> * Update validator/client/wait_for_activation.go Co-authored-by: Radosław Kapka <rkapka@wp.pl> * moving seconds until next epoch start to slottime and adding unit test * removing seconds into slot buffer, will need to test * fixing waittime bug * adding pr to changelog * Update validator/client/wait_for_activation.go Co-authored-by: Radosław Kapka <rkapka@wp.pl> * Update validator/client/wait_for_activation.go Co-authored-by: Radosław Kapka <rkapka@wp.pl> * fixing incorect log * refactoring based on feedback --------- Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com> Co-authored-by: Radosław Kapka <rkapka@wp.pl>
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
|
||||
mathutil "github.com/prysmaticlabs/prysm/v5/math"
|
||||
prysmTime "github.com/prysmaticlabs/prysm/v5/time"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// MaxSlotBuffer specifies the max buffer given to slots from
|
||||
@@ -286,3 +287,25 @@ func WithinVotingWindow(genesisTime uint64, slot primitives.Slot) bool {
|
||||
func MaxSafeEpoch() primitives.Epoch {
|
||||
return primitives.Epoch(math.MaxUint64 / uint64(params.BeaconConfig().SlotsPerEpoch))
|
||||
}
|
||||
|
||||
// SecondsUntilNextEpochStart returns how many seconds until the next Epoch start from the current time and slot
|
||||
func SecondsUntilNextEpochStart(genesisTimeSec uint64) (uint64, error) {
|
||||
currentSlot := CurrentSlot(genesisTimeSec)
|
||||
firstSlotOfNextEpoch, err := EpochStart(ToEpoch(currentSlot) + 1)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
nextEpochStartTime, err := ToTime(genesisTimeSec, firstSlotOfNextEpoch)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
es := nextEpochStartTime.Unix()
|
||||
n := time.Now().Unix()
|
||||
waitTime := uint64(es - n)
|
||||
log.WithFields(logrus.Fields{
|
||||
"current_slot": currentSlot,
|
||||
"next_epoch_start_slot": firstSlotOfNextEpoch,
|
||||
"is_epoch_start": IsEpochStart(currentSlot),
|
||||
}).Debugf("%d seconds until next epoch", waitTime)
|
||||
return waitTime, nil
|
||||
}
|
||||
|
||||
@@ -607,3 +607,28 @@ func TestWithinVotingWindow(t *testing.T) {
|
||||
genesisTime = uint64(time.Now().Add(-40 * time.Second).Unix())
|
||||
require.Equal(t, false, WithinVotingWindow(genesisTime, 3))
|
||||
}
|
||||
|
||||
func TestSecondsUntilNextEpochStart(t *testing.T) {
|
||||
secondsInEpoch := uint64(params.BeaconConfig().SlotsPerEpoch) * params.BeaconConfig().SecondsPerSlot
|
||||
// try slot 3
|
||||
genesisTime := uint64(time.Now().Add(-39 * time.Second).Unix())
|
||||
waitTime, err := SecondsUntilNextEpochStart(genesisTime)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, secondsInEpoch-(params.BeaconConfig().SecondsPerSlot*3)-3, waitTime)
|
||||
// try slot 34
|
||||
genesisTime = uint64(time.Now().Add(time.Duration(-1*int(secondsInEpoch)-int(params.BeaconConfig().SecondsPerSlot*2)-5) * time.Second).Unix())
|
||||
waitTime, err = SecondsUntilNextEpochStart(genesisTime)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, secondsInEpoch-(params.BeaconConfig().SecondsPerSlot*2)-5, waitTime)
|
||||
|
||||
// check if waitTime is correctly EpochStart
|
||||
n := time.Now().Add(-39 * time.Second)
|
||||
genesisTime = uint64(n.Unix())
|
||||
waitTime, err = SecondsUntilNextEpochStart(genesisTime)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, secondsInEpoch-39, waitTime)
|
||||
newGenesisTime := uint64(n.Add(time.Duration(-1*int(waitTime)) * time.Second).Unix())
|
||||
currentSlot := CurrentSlot(newGenesisTime)
|
||||
require.Equal(t, true, IsEpochStart(currentSlot))
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user