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:
@@ -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