mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-05-02 03:02:54 -04:00
Countdown Until Genesis (#6231)
* added in logic * latest countdown * fixed up formatting and add tests * ready for review * Merge branch 'master' into countdown-genesis * no log if after genesis * Merge branch 'countdown-genesis' of github.com:prysmaticlabs/prysm into countdown-genesis * countdown * smarter logging * added buffer period for countdown * Merge refs/heads/master into countdown-genesis
This commit is contained in:
@@ -37,6 +37,7 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
"github.com/prysmaticlabs/prysm/shared/slotutil"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
@@ -167,6 +168,10 @@ func (s *Service) Start() {
|
||||
if err := s.initializeChainInfo(ctx); err != nil {
|
||||
log.Fatalf("Could not set up chain info: %v", err)
|
||||
}
|
||||
|
||||
// We start a counter to genesis, if needed.
|
||||
go slotutil.CountdownToGenesis(ctx, s.genesisTime, uint64(beaconState.NumValidators()))
|
||||
|
||||
justifiedCheckpoint, err := s.beaconDB.JustifiedCheckpoint(ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not get justified checkpoint: %v", err)
|
||||
@@ -245,6 +250,11 @@ func (s *Service) processChainStartTime(ctx context.Context, genesisTime time.Ti
|
||||
if err != nil {
|
||||
log.Fatalf("Could not initialize beacon chain: %v", err)
|
||||
}
|
||||
// We start a counter to genesis, if needed.
|
||||
go slotutil.CountdownToGenesis(ctx, genesisTime, uint64(initializedState.NumValidators()))
|
||||
|
||||
// We send out a state initialized event to the rest of the services
|
||||
// running in the beacon node.
|
||||
s.stateNotifier.StateFeed().Send(&feed.Event{
|
||||
Type: statefeed.Initialized,
|
||||
Data: &statefeed.InitializedData{
|
||||
|
||||
@@ -18,6 +18,7 @@ go_library(
|
||||
"//proto/beacon/p2p/v1:go_default_library",
|
||||
"//shared:go_default_library",
|
||||
"//shared/interop:go_default_library",
|
||||
"//shared/slotutil:go_default_library",
|
||||
"@com_github_pkg_errors//:go_default_library",
|
||||
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
|
||||
"@com_github_sirupsen_logrus//:go_default_library",
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared"
|
||||
"github.com/prysmaticlabs/prysm/shared/interop"
|
||||
"github.com/prysmaticlabs/prysm/shared/slotutil"
|
||||
)
|
||||
|
||||
var _ = shared.Service(&Service{})
|
||||
@@ -97,6 +99,7 @@ func NewColdStartService(ctx context.Context, cfg *Config) *Service {
|
||||
// Generated genesis time; fetch it
|
||||
s.genesisTime = genesisTrie.GenesisTime()
|
||||
}
|
||||
go slotutil.CountdownToGenesis(ctx, time.Unix(int64(s.genesisTime), 0), s.numValidators)
|
||||
|
||||
if err := s.saveGenesisState(ctx, genesisTrie); err != nil {
|
||||
log.Fatalf("Could not save interop genesis state %v", err)
|
||||
|
||||
@@ -100,6 +100,7 @@ type BeaconChainConfig struct {
|
||||
DefaultPageSize int // DefaultPageSize defines the default page size for RPC server request.
|
||||
MaxPeersToSync int // MaxPeersToSync describes the limit for number of peers in round robin sync.
|
||||
SlotsPerArchivedPoint uint64 // SlotsPerArchivedPoint defines the number of slots per one archived point.
|
||||
GenesisCountdownInterval time.Duration // How often to log the countdown until the genesis time is reached.
|
||||
|
||||
// Slasher constants.
|
||||
WeakSubjectivityPeriod uint64 // WeakSubjectivityPeriod defines the time period expressed in number of epochs were proof of stake network should validate block headers and attestations for slashable events.
|
||||
@@ -200,6 +201,7 @@ var defaultBeaconConfig = &BeaconChainConfig{
|
||||
DefaultPageSize: 250,
|
||||
MaxPeersToSync: 15,
|
||||
SlotsPerArchivedPoint: 2048,
|
||||
GenesisCountdownInterval: time.Minute,
|
||||
|
||||
// Slasher related values.
|
||||
WeakSubjectivityPeriod: 54000,
|
||||
|
||||
@@ -4,6 +4,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test")
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"countdown.go",
|
||||
"slotticker.go",
|
||||
"slottime.go",
|
||||
],
|
||||
@@ -12,12 +13,22 @@ go_library(
|
||||
deps = [
|
||||
"//shared/params:go_default_library",
|
||||
"//shared/roughtime:go_default_library",
|
||||
"@com_github_sirupsen_logrus//:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
size = "small",
|
||||
srcs = ["slotticker_test.go"],
|
||||
srcs = [
|
||||
"countdown_test.go",
|
||||
"slotticker_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//shared/params:go_default_library",
|
||||
"//shared/roughtime:go_default_library",
|
||||
"//shared/testutil:go_default_library",
|
||||
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
51
shared/slotutil/countdown.go
Normal file
51
shared/slotutil/countdown.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package slotutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
"github.com/prysmaticlabs/prysm/shared/roughtime"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var log = logrus.WithField("prefix", "slotutil")
|
||||
|
||||
// CountdownToGenesis starts a ticker at the specified duration
|
||||
// logging the remaining minutes until the genesis chainstart event
|
||||
// along with important genesis state metadata such as number
|
||||
// of genesis validators.
|
||||
func CountdownToGenesis(ctx context.Context, genesisTime time.Time, genesisValidatorCount uint64) {
|
||||
ticker := time.NewTicker(params.BeaconConfig().GenesisCountdownInterval)
|
||||
timeTillGenesis := genesisTime.Sub(roughtime.Now())
|
||||
logFields := logrus.Fields{
|
||||
"genesisValidators": fmt.Sprintf("%d", genesisValidatorCount),
|
||||
"genesisTime": fmt.Sprintf("%v", genesisTime),
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-time.After(timeTillGenesis):
|
||||
log.WithFields(logFields).Info("Chain genesis time reached")
|
||||
return
|
||||
case <-ticker.C:
|
||||
currentTime := roughtime.Now()
|
||||
if currentTime.After(genesisTime) {
|
||||
log.WithFields(logFields).Info("Chain genesis time reached")
|
||||
return
|
||||
}
|
||||
timeRemaining := genesisTime.Sub(currentTime)
|
||||
if timeRemaining <= 2*time.Minute {
|
||||
ticker = time.NewTicker(time.Second)
|
||||
}
|
||||
if timeRemaining >= time.Second {
|
||||
log.WithFields(logFields).Infof(
|
||||
"%s until chain genesis",
|
||||
timeRemaining.Truncate(time.Second),
|
||||
)
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
30
shared/slotutil/countdown_test.go
Normal file
30
shared/slotutil/countdown_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package slotutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
"github.com/prysmaticlabs/prysm/shared/roughtime"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil"
|
||||
logTest "github.com/sirupsen/logrus/hooks/test"
|
||||
)
|
||||
|
||||
func TestCountdownToGenesis(t *testing.T) {
|
||||
hook := logTest.NewGlobal()
|
||||
params.SetupTestConfigCleanup(t)
|
||||
config := params.BeaconConfig()
|
||||
config.GenesisCountdownInterval = time.Millisecond * 500
|
||||
params.OverrideBeaconConfig(config)
|
||||
|
||||
firstStringResult := "1s until chain genesis"
|
||||
genesisReached := "Chain genesis time reached"
|
||||
CountdownToGenesis(
|
||||
context.Background(),
|
||||
roughtime.Now().Add(2*time.Second),
|
||||
params.BeaconConfig().MinGenesisActiveValidatorCount,
|
||||
)
|
||||
testutil.AssertLogsContain(t, hook, firstStringResult)
|
||||
testutil.AssertLogsContain(t, hook, genesisReached)
|
||||
}
|
||||
Reference in New Issue
Block a user