prysm-pulse/shared/slotticker/simulatorticker.go
Nishant Das 16b04699d0 Allow Initial Sync to Work with Simulator (#669)
* polling interval

* adding proto message

* changing proto messages

* changing naming

* adding slot functionality

* initial sync working

* new changes

* more sync fixes

* its working now

* finally working

* add tests

* fix tests

* tests

* adding tests

* lint

* log checks

* making changes to simulator

* update logs

* fix tests

* get sync to work with crystallized state

* fixing race

* making requested changes

* unexport

* documentation

* gazelle and fix merge conflicts

* adding repeated requests

* fix lint

* adding new clock , db methods, and util func

* revert change to test

* gazelle

* add in test

* gazelle

* finally working

* save slot

* fix lint and constant
2018-11-21 10:00:36 -08:00

92 lines
2.1 KiB
Go

package slotticker
import (
"time"
)
// SimulatorTicker is modified version of the SlotTicker so that the
// simulator is able to generate blocks correctly even if a node is
// shutdown and started up again.
type SimulatorTicker struct {
c chan uint64
done chan struct{}
}
// C returns the ticker channel. Call Cancel afterwards to ensure
// that the goroutine exits cleanly.
func (s *SimulatorTicker) C() <-chan uint64 {
return s.c
}
// Done should be called to clean up the ticker.
func (s *SimulatorTicker) Done() {
go func() {
s.done <- struct{}{}
}()
}
// GetSimulatorTicker is the constructor for SimulatorTicker.
func GetSimulatorTicker(genesisTime time.Time, slotDuration uint64, currentSlot uint64) SimulatorTicker {
ticker := SimulatorTicker{
c: make(chan uint64),
done: make(chan struct{}),
}
ticker.start(genesisTime, slotDuration, currentSlot, time.Since, time.Until, time.After)
return ticker
}
// CurrentSimulatorSlot accepts the genesis time and returns the current time's slot.
func CurrentSimulatorSlot(
genesisTime time.Time,
slotDuration uint64,
since func(time.Time) time.Duration) uint64 {
sinceGenesis := since(genesisTime)
if sinceGenesis < 0 {
return 0
}
durationInSeconds := time.Duration(slotDuration) * time.Second
return uint64(sinceGenesis / durationInSeconds)
}
func (s *SimulatorTicker) start(
genesisTime time.Time,
slotDuration uint64,
currentSlot uint64,
since func(time.Time) time.Duration,
until func(time.Time) time.Duration,
after func(time.Duration) <-chan time.Time) {
d := time.Duration(slotDuration) * time.Second
go func() {
sinceGenesis := since(genesisTime)
var nextTickTime time.Time
var slot uint64
if sinceGenesis < 0 {
// Handle when the current time is before the genesis time
nextTickTime = genesisTime
slot = 0
} else {
nextTick := sinceGenesis.Truncate(d) + d
nextTickTime = genesisTime.Add(nextTick)
slot = currentSlot + 1
}
for {
waitTime := until(nextTickTime)
select {
case <-after(waitTime):
s.c <- slot
slot++
nextTickTime = nextTickTime.Add(d)
case <-s.done:
return
}
}
}()
}