Skip to content

Commit 0e3b551

Browse files
committed
Set auto fork config slot and timestamp properly
The hard fork global slot since genesis and the hard fork timestamp are now set according to the stop slots and the hard fork genesis slot delta, if those components of the daemon config have been set. If they have not, then the slot and timestamp of the hard fork block itself are used as a fallback.
1 parent fe8d307 commit 0e3b551

File tree

4 files changed

+88
-22
lines changed

4 files changed

+88
-22
lines changed

src/lib/mina_graphql/mina_graphql.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2614,11 +2614,11 @@ module Queries = struct
26142614
in
26152615
let%bind { source_ledgers
26162616
; global_slot_since_genesis
2617+
; genesis_state_timestamp = _
26172618
; state_hash
26182619
; staking_epoch_seed
26192620
; next_epoch_seed
26202621
; blockchain_length
2621-
; block_timestamp = _
26222622
} =
26232623
Mina_lib.Hardfork_config.prepare_inputs ~breadcrumb_spec mina
26242624
in

src/lib/mina_lib/mina_lib.ml

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,21 @@ let get_node_state t =
453453
; uptime_of_node
454454
}
455455

456+
(** Compute the hard fork genesis slot from the runtime config, if all the stop
457+
slots and the genesis slot delta have been set *)
458+
let scheduled_hard_fork_genesis_slot t :
459+
Mina_numbers.Global_slot_since_hard_fork.t option =
460+
let open Option.Let_syntax in
461+
let runtime_config = t.config.precomputed_values.runtime_config in
462+
let%bind slot_chain_end_since_hard_fork =
463+
Runtime_config.slot_chain_end runtime_config
464+
in
465+
let%map hard_fork_genesis_slot_delta =
466+
Runtime_config.hard_fork_genesis_slot_delta runtime_config
467+
in
468+
Mina_numbers.Global_slot_since_hard_fork.add slot_chain_end_since_hard_fork
469+
hard_fork_genesis_slot_delta
470+
456471
(* This is a hack put in place to deal with nodes getting stuck
457472
in Offline states, that is, not receiving blocks for an extended period,
458473
or stuck in Bootstrap for too long
@@ -2829,24 +2844,83 @@ module Hardfork_config = struct
28292844
type inputs =
28302845
{ source_ledgers : genesis_source_ledgers
28312846
; global_slot_since_genesis : Mina_numbers.Global_slot_since_genesis.t
2847+
; genesis_state_timestamp : string
28322848
; state_hash : State_hash.t
28332849
; staking_epoch_seed : Epoch_seed.t
28342850
; next_epoch_seed : Epoch_seed.t
28352851
; blockchain_length : Mina_numbers.Length.t
2836-
; block_timestamp : Block_time.t
28372852
}
28382853

2854+
(** The genesis state timestamp string is the timestamp of the start of the
2855+
[global_slot] of the hard fork, relative to the current chain *)
2856+
let genesis_timestamp_str ~consensus_constants global_slot =
2857+
Consensus.Data.Consensus_time.(
2858+
start_time ~constants:consensus_constants
2859+
(of_global_slot ~constants:consensus_constants global_slot))
2860+
|> Block_time.to_time_exn
2861+
|> Time.to_string_iso8601_basic ~zone:Time.Zone.utc
2862+
2863+
(** Compute the hard fork slot. This will be derived from the stop slots and
2864+
hard fork genesis slot delta in the runtime config, if those have been set
2865+
and the [breadcrum_spec] was [`Stop_slot]. Otherwise, it will be the
2866+
global slot since genesis of the hard fork block. *)
2867+
let hard_fork_global_slot ~breadcrumb_spec ~block mina :
2868+
Mina_numbers.Global_slot_since_hard_fork.t =
2869+
let block_global_slot =
2870+
Mina_block.consensus_state block
2871+
|> Consensus.Data.Consensus_state.curr_global_slot
2872+
in
2873+
let configured_slot =
2874+
match breadcrumb_spec with
2875+
| `Stop_slot ->
2876+
scheduled_hard_fork_genesis_slot mina
2877+
| `State_hash _state_hash_base58 ->
2878+
None
2879+
| `Block_height _block_height ->
2880+
None
2881+
in
2882+
Option.value ~default:block_global_slot configured_slot
2883+
2884+
(** Helper to convert a global slot since hard fork to a global slot since genesis *)
2885+
let global_slot_since_hard_fork_to_genesis
2886+
~(constraint_constants : Genesis_constants.Constraint_constants.t)
2887+
global_slot =
2888+
(* Convert the global slot to a span of slots since the current hard fork *)
2889+
let global_slot_span =
2890+
global_slot |> Mina_numbers.Global_slot_since_hard_fork.to_uint32
2891+
|> Mina_numbers.Global_slot_span.of_uint32
2892+
in
2893+
(* Retrieve the global slot since genesis of the genesis of the current
2894+
chain *)
2895+
let current_genesis_global_slot =
2896+
constraint_constants.fork
2897+
|> Option.value_map ~default:Mina_numbers.Global_slot_since_genesis.zero
2898+
~f:(fun fork -> fork.global_slot_since_genesis)
2899+
in
2900+
(* Add the slot span to the current chain's genesis slot to get the desired quantity *)
2901+
Mina_numbers.Global_slot_since_genesis.add current_genesis_global_slot
2902+
global_slot_span
2903+
28392904
let prepare_inputs ~breadcrumb_spec mina =
28402905
let open Deferred.Result.Let_syntax in
28412906
let%bind breadcrumb = breadcrumb ~breadcrumb_spec mina in
28422907
let block = Transition_frontier.Breadcrumb.block breadcrumb in
28432908
let blockchain_length = Mina_block.blockchain_length block in
2909+
let global_slot_since_hard_fork =
2910+
hard_fork_global_slot ~breadcrumb_spec ~block mina
2911+
in
28442912
let global_slot_since_genesis =
2845-
Mina_block.consensus_state block
2846-
|> Consensus.Data.Consensus_state.global_slot_since_genesis
2913+
global_slot_since_hard_fork_to_genesis
2914+
~constraint_constants:
2915+
mina.config.precomputed_values.constraint_constants
2916+
global_slot_since_hard_fork
2917+
in
2918+
let genesis_state_timestamp =
2919+
genesis_timestamp_str
2920+
~consensus_constants:mina.config.precomputed_values.consensus_constants
2921+
global_slot_since_hard_fork
28472922
in
28482923
let state_hash = Transition_frontier.Breadcrumb.state_hash breadcrumb in
2849-
let block_timestamp = block |> Mina_block.timestamp in
28502924
let protocol_state =
28512925
Transition_frontier.Breadcrumb.protocol_state breadcrumb
28522926
in
@@ -2862,11 +2936,11 @@ module Hardfork_config = struct
28622936
let%map source_ledgers = source_ledgers ~breadcrumb mina in
28632937
{ source_ledgers
28642938
; global_slot_since_genesis
2939+
; genesis_state_timestamp
28652940
; state_hash
28662941
; staking_epoch_seed
28672942
; next_epoch_seed
28682943
; blockchain_length
2869-
; block_timestamp
28702944
}
28712945

28722946
(** Copy the roots of the [source_ledgers] and gather the stable ledger
@@ -3041,20 +3115,15 @@ module Hardfork_config = struct
30413115
~state_hash ~blockchain_length ~staking_epoch_seed ~next_epoch_seed
30423116
genesis_config )
30433117

3044-
let genesis_timestamp_str ~hardfork_genesis_timestamp_offset block_timestamp =
3045-
block_timestamp |> Block_time.to_time_exn
3046-
|> Fn.flip Time.add hardfork_genesis_timestamp_offset
3047-
|> Time.to_string_iso8601_basic ~zone:Time.Zone.utc
3048-
30493118
let generate_hardfork_configs ~logger
30503119
~inputs:
30513120
{ source_ledgers
30523121
; global_slot_since_genesis
3122+
; genesis_state_timestamp
30533123
; state_hash
30543124
; staking_epoch_seed
30553125
; next_epoch_seed
30563126
; blockchain_length
3057-
; block_timestamp
30583127
} ~build_dir directory_name =
30593128
let open Deferred.Or_error.Let_syntax in
30603129
let migrate_and_apply (root, diff) =
@@ -3094,12 +3163,6 @@ module Hardfork_config = struct
30943163
, genesis_next_epoch_ledger_migrated ) =
30953164
migrate_and_apply genesis_next_epoch_ledger_data
30963165
in
3097-
(* TODO: the correct timestamp is actually the timestamp of the slot_tx_end plus the hardfork genesis offset *)
3098-
let genesis_state_timestamp =
3099-
genesis_timestamp_str
3100-
~hardfork_genesis_timestamp_offset:(Time.Span.of_int_sec 0)
3101-
block_timestamp
3102-
in
31033166
[%log debug] "Writing hard fork config directories" ;
31043167
let%bind () =
31053168
write_stable_config_directory ~logger ~genesis_state_timestamp

src/lib/mina_lib/mina_lib.mli

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,6 @@ val best_chain_block_by_height :
278278
val best_chain_block_by_state_hash :
279279
t -> State_hash.t -> Transition_frontier.Breadcrumb.t Or_error.t
280280

281-
val best_chain_block_before_stop_slot :
282-
t -> Transition_frontier.Breadcrumb.t Deferred.Or_error.t
283-
284281
module Hardfork_config : sig
285282
type mina_lib = t
286283

@@ -325,11 +322,11 @@ module Hardfork_config : sig
325322
type inputs =
326323
{ source_ledgers : genesis_source_ledgers
327324
; global_slot_since_genesis : Mina_numbers.Global_slot_since_genesis.t
325+
; genesis_state_timestamp : string
328326
; state_hash : State_hash.t
329327
; staking_epoch_seed : Epoch_seed.t
330328
; next_epoch_seed : Epoch_seed.t
331329
; blockchain_length : Mina_numbers.Length.t
332-
; block_timestamp : Block_time.t
333330
}
334331

335332
val prepare_inputs :

src/lib/runtime_config/runtime_config.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,12 @@ let slot_tx_end, slot_chain_end =
16861686
in
16871687
(f (fun d -> d.slot_tx_end), f (fun d -> d.slot_chain_end))
16881688

1689+
let hard_fork_genesis_slot_delta t =
1690+
let open Option.Let_syntax in
1691+
let%bind daemon = t.daemon in
1692+
let%map delta = daemon.hard_fork_genesis_slot_delta in
1693+
Mina_numbers.Global_slot_span.of_int delta
1694+
16891695
(** This method creates a runtime daemon config for a hard fork, containing the
16901696
data that can't necessarily be computed in advance of the hard fork. This
16911697
ends up being data that's computed based on the last block produced before

0 commit comments

Comments
 (0)