Skip to content

Commit

Permalink
Replace validator wait for activation stream with polling (#14514)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update CHANGELOG.md

Co-authored-by: Sammy Rosso <[email protected]>

* Update CHANGELOG.md

Co-authored-by: Radosław Kapka <[email protected]>

* Update validator/client/wait_for_activation.go

Co-authored-by: Radosław Kapka <[email protected]>

* 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 <[email protected]>

* Update validator/client/wait_for_activation.go

Co-authored-by: Radosław Kapka <[email protected]>

* fixing incorect log

* refactoring based on feedback

---------

Co-authored-by: Sammy Rosso <[email protected]>
Co-authored-by: Radosław Kapka <[email protected]>
  • Loading branch information
3 people authored Oct 10, 2024
1 parent 57cc495 commit 6c22ede
Show file tree
Hide file tree
Showing 19 changed files with 544 additions and 1,263 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
### Deprecated
- `--disable-grpc-gateway` flag is deprecated due to grpc gateway removal.
- `--enable-experimental-state` flag is deprecated. This feature is now on by default. Opt-out with `--disable-experimental-state`.
- `/eth/v1alpha1/validator/activation/stream` grpc wait for activation stream is deprecated. [pr](https://github.com/prysmaticlabs/prysm/pull/14514)

### Removed

Expand Down
1 change: 1 addition & 0 deletions beacon-chain/rpc/prysm/v1alpha1/validator/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type Server struct {
// WaitForActivation checks if a validator public key exists in the active validator registry of the current
// beacon state, if not, then it creates a stream which listens for canonical states which contain
// the validator with the public key as an active validator record.
// Deprecated: do not use, just poll validator status every epoch.
func (vs *Server) WaitForActivation(req *ethpb.ValidatorActivationRequest, stream ethpb.BeaconNodeValidator_WaitForActivationServer) error {
activeValidatorExists, validatorStatuses, err := vs.activationStatus(stream.Context(), req.PublicKeys)
if err != nil {
Expand Down
567 changes: 285 additions & 282 deletions proto/prysm/v1alpha1/validator.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions proto/prysm/v1alpha1/validator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ service BeaconNodeValidator {
option (google.api.http) = {
get: "/eth/v1alpha1/validator/activation/stream"
};
option deprecated = true;
}

// ValidatorIndex retrieves a validator's index location in the beacon state's
Expand Down
123 changes: 0 additions & 123 deletions testing/mock/beacon_validator_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions time/slots/slottime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
25 changes: 25 additions & 0 deletions time/slots/slottime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

}
2 changes: 0 additions & 2 deletions validator/client/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ go_test(
"//runtime:go_default_library",
"//runtime/version:go_default_library",
"//testing/assert:go_default_library",
"//testing/mock:go_default_library",
"//testing/require:go_default_library",
"//testing/util:go_default_library",
"//testing/validator-mock:go_default_library",
Expand All @@ -169,7 +168,6 @@ go_test(
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
"@com_github_stretchr_testify//mock:go_default_library",
"@com_github_tyler_smith_go_bip39//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@com_github_wealdtech_go_eth2_util//:go_default_library",
Expand Down
2 changes: 0 additions & 2 deletions validator/client/beacon-api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ load("@prysm//tools/go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"activation.go",
"attestation_data.go",
"beacon_api_beacon_chain_client.go",
"beacon_api_helpers.go",
Expand Down Expand Up @@ -75,7 +74,6 @@ go_test(
name = "go_default_test",
size = "small",
srcs = [
"activation_test.go",
"attestation_data_test.go",
"beacon_api_beacon_chain_client_test.go",
"beacon_api_helpers_test.go",
Expand Down
121 changes: 0 additions & 121 deletions validator/client/beacon-api/activation.go

This file was deleted.

Loading

0 comments on commit 6c22ede

Please sign in to comment.