mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 21:07:18 +00:00
918129cf36
* refactor initialization to blocking startup method * require genesisSetter in blockchain, fix tests * work-around gazelle weirdness * fix dep gazelle ignores * only call SetGenesis once * fix typo * validator test setup and fix to return right error * move waitForChainStart to Start * wire up sync Service.genesisWaiter * fix p2p genesisWaiter plumbing * remove extra clock type, integrate into genesis and rename * use time.Now when no Nower is specified * remove unused ClockSetter * simplify rpc context checking * fix typo * use clock everywhere in sync; [32]byte val root * don't use DeepEqual to compare [32]byte and []byte * don't use clock in init sync, not wired up yet * use clock waiter in blockchain as well * use cancelable contexts in tests with goroutines * missed a reference to WithClockSetter * Update beacon-chain/startup/genesis.go Co-authored-by: Radosław Kapka <rkapka@wp.pl> * Update beacon-chain/blockchain/service_test.go Co-authored-by: Radosław Kapka <rkapka@wp.pl> * more clear docs * doc for NewClock * move clock typedef to more logical file name * adding documentation * gaz * fixes for capella * reducing test raciness * fix races in committee cache tests * lint * add tests on Duration slot math helper * startup package test coverage * fix bad merge * set non-zero genesis time in tests that call Start * happy deepsource, happy me-epsource * replace Synced event with channel * remove unused error * remove accidental wip commit * gaz! * remove unused event constants * remove sync statefeed subscription to fix deadlock * remove state notifier * fix build --------- Co-authored-by: Kasey Kirkham <kasey@users.noreply.github.com> Co-authored-by: Radosław Kapka <rkapka@wp.pl> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: nisdas <nishdas93@gmail.com>
76 lines
2.4 KiB
Go
76 lines
2.4 KiB
Go
package startup
|
|
|
|
import (
|
|
"time"
|
|
|
|
types "github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
|
"github.com/prysmaticlabs/prysm/v4/time/slots"
|
|
)
|
|
|
|
// Nower is a function that can return the current time.
|
|
// In Clock, Now() will use time.Now by default, but a Nower can be set using WithNower in NewClock
|
|
// to customize the return value for Now() in tests.
|
|
type Nower func() time.Time
|
|
|
|
// Clock abstracts important time-related concerns in the beacon chain:
|
|
// - provides a time.Now() construct that can be overridden in tests
|
|
// - GenesisTime() to know the genesis time or use genesis time determination as a synchronization point.
|
|
// - CurrentSlot: convenience conversion for current time -> slot
|
|
// (support backwards compatibility with the TimeFetcher interface)
|
|
// - GenesisValidatorsRoot: is determined at the same point as genesis time and is needed by some of the same code,
|
|
// so it is also bundled for convenience.
|
|
type Clock struct {
|
|
t time.Time
|
|
vr [32]byte
|
|
now Nower
|
|
}
|
|
|
|
// GenesisTime returns the genesis timestamp.
|
|
func (g *Clock) GenesisTime() time.Time {
|
|
return g.t
|
|
}
|
|
|
|
// GenesisValidatorsRoot returns the genesis state validator root
|
|
func (g *Clock) GenesisValidatorsRoot() [32]byte {
|
|
return g.vr
|
|
}
|
|
|
|
// CurrentSlot returns the current slot relative to the time.Time value that Clock embeds.
|
|
func (g *Clock) CurrentSlot() types.Slot {
|
|
now := g.now()
|
|
return slots.Duration(g.t, now)
|
|
}
|
|
|
|
// Now provides a value for time.Now() that can be overridden in tests.
|
|
func (g *Clock) Now() time.Time {
|
|
return g.now()
|
|
}
|
|
|
|
// ClockOpt is a functional option to change the behavior of a clock value made by NewClock.
|
|
// It is primarily intended as a way to inject an alternate time.Now() callback (WithNower) for testing.
|
|
type ClockOpt func(*Clock)
|
|
|
|
// WithNower allows tests in particular to inject an alternate implementation of time.Now (vs using system time)
|
|
func WithNower(n Nower) ClockOpt {
|
|
return func(g *Clock) {
|
|
g.now = n
|
|
}
|
|
}
|
|
|
|
// NewClock constructs a Clock value from a genesis timestamp (t) and a Genesis Validator Root (vr).
|
|
// The WithNower ClockOpt can be used in tests to specify an alternate `time.Now` implementation,
|
|
// for instance to return a value for `Now` spanning a certain number of slots from genesis time, to control the current slot.
|
|
func NewClock(t time.Time, vr [32]byte, opts ...ClockOpt) *Clock {
|
|
c := &Clock{
|
|
t: t,
|
|
vr: vr,
|
|
}
|
|
for _, o := range opts {
|
|
o(c)
|
|
}
|
|
if c.now == nil {
|
|
c.now = time.Now
|
|
}
|
|
return c
|
|
}
|