startup

package
v4.2.1-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2024 License: GPL-3.0 Imports: 5 Imported by: 21

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock struct {
	// contains filtered or unexported fields
}

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.

func NewClock

func NewClock(t time.Time, vr [32]byte, opts ...ClockOpt) *Clock

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 (*Clock) CurrentSlot

func (g *Clock) CurrentSlot() types.Slot

CurrentSlot returns the current slot relative to the time.Time value that Clock embeds.

func (*Clock) GenesisTime

func (g *Clock) GenesisTime() time.Time

GenesisTime returns the genesis timestamp.

func (*Clock) GenesisValidatorsRoot

func (g *Clock) GenesisValidatorsRoot() [32]byte

GenesisValidatorsRoot returns the genesis state validator root

func (*Clock) Now

func (g *Clock) Now() time.Time

Now provides a value for time.Now() that can be overridden in tests.

func (*Clock) SlotStart added in v4.2.0

func (g *Clock) SlotStart(slot types.Slot) time.Time

SlotStart computes the time the given slot begins.

type ClockOpt

type ClockOpt func(*Clock)

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.

func WithNower

func WithNower(n Nower) ClockOpt

WithNower allows tests in particular to inject an alternate implementation of time.Now (vs using system time)

type ClockSetter

type ClockSetter interface {
	SetClock(c *Clock) error
}

ClockSetter specifies the SetClock method. ClockSynchronizer works in a 1:N pattern, so in a given graph of services, only one service should be given the ClockSetter, and all others relying on the service's activation should use ClockWaiter.

type ClockSynchronizer

type ClockSynchronizer struct {
	// contains filtered or unexported fields
}

ClockSynchronizer provides a synchronization mechanism for services that rely on the genesis time and validator root being known before getting to work.

func NewClockSynchronizer

func NewClockSynchronizer() *ClockSynchronizer

NewClockSynchronizer initializes a single instance of ClockSynchronizer that must be used by all ClockWaiters that need to be synchronized to a ClockSetter (ie blockchain service).

func (*ClockSynchronizer) SetClock

func (w *ClockSynchronizer) SetClock(c *Clock) error

SetClock sets the Clock value `c` and unblocks all threads waiting for `c` via WaitForClock. Calling SetClock more than once will return an error, as calling this function is meant to be a signal that the system is ready to start.

func (*ClockSynchronizer) WaitForClock

func (w *ClockSynchronizer) WaitForClock(ctx context.Context) (*Clock, error)

WaitForClock will block the caller until the *Clock value is available. If the provided context is canceled (eg via a deadline set upstream), the function will return the error given by ctx.Err().

type ClockWaiter

type ClockWaiter interface {
	WaitForClock(context.Context) (*Clock, error)
}

ClockWaiter specifies the WaitForClock method. ClockSynchronizer works in a 1:N pattern, with 1 thread calling SetClock, and the others blocking on a call to WaitForClock until the expected *Clock value is set.

type Nower

type Nower func() time.Time

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL