Module Tezos_baking_alpha.Baking_scheduling

Scheduler state type

type loop_state

the automaton's state

Functions used by the baker

val retry : Tezos_client_alpha.Protocol_client_context.full -> ?max_delay:float -> delay:float -> factor:float -> tries:int -> ?msg:string -> ('a -> 'b Tezos_base.TzPervasives.tzresult Lwt.t) -> 'a -> 'b Tezos_base.TzPervasives.tzresult Lwt.t

retry ctxt ~delay ?max_delay ~factor ~tries ?msg f x retries applying f x tries until it succeeds or returns an error different from Connection_failed, at most tries number of times. After each try it waits for a number of seconds, but not more than max_delay, if given. The wait time between tries is given by the initial delay, multiplied by factor at each subsequent try. At each failure, msg together with the current delay is printed using ctxt#message.

run context ?canceler ?stop_on_event ?on_error ?constants chain baking_configuration consensus_keys is the entry point of the baker automaton. This function performs the following tasks:

  • perform a sanity check that check the location of the baking files, nonces, highwatermarks and state files, also verifying that the files are loadable
  • create the streams for valid blocks, new heads, and operations from the node's mempool's
  • register dal profiles by calling the register_dal_profiles node RPC

Functions only needed for the baking_lib

val sleep_until : Tezos_base.TzPervasives.Time.Protocol.t -> unit Lwt.t option

sleep_until time is blocking until it is time.

first_potential_round_at_next_level Returns the first round at the next level, at or after earliest_round, whose baking slot belongs to one of our own delegates; also returns the corresponding delegate. Or returns None if no such round exists.

val compute_next_potential_baking_time_at_next_level : Baking_state.state -> (Tezos_base.TzPervasives.Time.Protocol.t * Tezos_protocol_alpha.Protocol.Alpha_context.Round.t) option Lwt.t

compute_next_potential_baking_time state From the current state, the function returns an optional association pair, which consists of the next baking timestamp and its baking round. In that case, an elected block must exist.

compute_bootstrap_event state emits the first event. If the latest proposal is for the current round, then trigger the new proposal event to possibly preattest. Otherwise, trigger the end of round event (for the previous round) to check whether we need to propose at this level or not.

val create_loop_state : ?get_valid_blocks_stream:Baking_state.proposal Lwt_stream.t Lwt.t -> heads_stream:Baking_state.proposal Lwt_stream.t -> forge_event_stream:Baking_state.forge_event Lwt_stream.t -> Operation_worker.t -> loop_state

create_loop_state ?get_valid_blocks_stream heads_stream forge_event_stream operation_worker creates a loop state with the streams of valid blocks, new heads, forged events and operations from the node's mempool.

create_initial_state context ?synchronize chain baking_configuration operation_worker current_proposal ?constants consensus_keys creates an initial Baking_state.t by initializing a Baking_state.global_state, a Baking_state.level_state and a Baking_state.round_state.

  • For the global_state initialization, a validation mode is set based on the baking_configuration and a forge worker is started. If constants is not provided, an RPC is called to recover them from the context.
  • For the level_state initialization, information regarding the current level is retrieved (the current level being that of the current_proposal) and delegates slots are computed for the given consensus_keys.
  • For the round_state initialization, current round is compute by calling Baking_actions.compute_round with current_proposal information if synchronize is set to true (which is the default).
val automaton_loop : ?stop_on_event:(Baking_state.event -> bool) -> config:Baking_configuration.t -> on_error: (Tezos_base.TzPervasives.tztrace -> (unit, Tezos_base.TzPervasives.tztrace) Stdlib.result Lwt.t) -> loop_state -> Baking_state.state -> Baking_state.event -> Baking_state.event option Tezos_base.TzPervasives.tzresult Lwt.t

automaton_loop ?stop_on_event baking_configuration on_error loop_state state event:

  • calls State_transition.step with the state and event and recover a new state and an action to perform
  • computes the next timeouts from the current state using a function that returns an Lwt promise that fulfills once the nearest timeout (between those computed by wait_end_of_round and wait_baking_time_next_level) is expired (at that moment the state machine will react)
  • waits for the next event from the events streams (new valid proposal, new heads, new forged event, (pre)quorum reached), or a timeout (end of round, or a baking time)
  • stops if stop_on_event matches the given event; recursively calls itself with the new event otherwise