2020-06-12 18:47:18 +00:00
|
|
|
package slotutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-09-22 11:49:58 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/timeutils"
|
2020-06-12 18:47:18 +00:00
|
|
|
"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.
|
2020-12-01 00:06:16 +00:00
|
|
|
func CountdownToGenesis(ctx context.Context, genesisTime time.Time, genesisValidatorCount uint64, genesisStateRoot [32]byte) {
|
2020-06-12 18:47:18 +00:00
|
|
|
ticker := time.NewTicker(params.BeaconConfig().GenesisCountdownInterval)
|
2020-09-04 18:07:50 +00:00
|
|
|
defer func() {
|
|
|
|
// Used in anonymous function to make sure that updated (per second) ticker is stopped.
|
|
|
|
ticker.Stop()
|
|
|
|
}()
|
2020-06-12 18:47:18 +00:00
|
|
|
logFields := logrus.Fields{
|
|
|
|
"genesisValidators": fmt.Sprintf("%d", genesisValidatorCount),
|
|
|
|
"genesisTime": fmt.Sprintf("%v", genesisTime),
|
2020-12-01 00:06:16 +00:00
|
|
|
"genesisStateRoot": fmt.Sprintf("%x", genesisStateRoot),
|
2020-06-12 18:47:18 +00:00
|
|
|
}
|
2020-09-04 18:07:50 +00:00
|
|
|
secondTimerActivated := false
|
2020-06-12 18:47:18 +00:00
|
|
|
for {
|
2020-09-22 11:49:58 +00:00
|
|
|
currentTime := timeutils.Now()
|
2020-09-04 18:07:50 +00:00
|
|
|
if currentTime.After(genesisTime) {
|
2020-06-12 18:47:18 +00:00
|
|
|
log.WithFields(logFields).Info("Chain genesis time reached")
|
|
|
|
return
|
2020-09-04 18:07:50 +00:00
|
|
|
}
|
|
|
|
timeRemaining := genesisTime.Sub(currentTime)
|
|
|
|
if !secondTimerActivated && timeRemaining <= 2*time.Minute {
|
|
|
|
ticker.Stop()
|
|
|
|
// Replace ticker with a one having higher granularity.
|
|
|
|
ticker = time.NewTicker(time.Second)
|
|
|
|
secondTimerActivated = true
|
|
|
|
}
|
|
|
|
select {
|
2020-06-12 18:47:18 +00:00
|
|
|
case <-ticker.C:
|
|
|
|
if timeRemaining >= time.Second {
|
|
|
|
log.WithFields(logFields).Infof(
|
|
|
|
"%s until chain genesis",
|
|
|
|
timeRemaining.Truncate(time.Second),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
2020-09-04 18:07:50 +00:00
|
|
|
log.Debug("Context closed, exiting routine")
|
2020-06-12 18:47:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|