Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Global_slot_since_genesis for stop slots #15513

Open
wants to merge 2 commits into
base: berkeley
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/best_tip_merger/best_tip_merger.ml
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ module Compact_display = struct
; global_slot =
Mina_state.Protocol_state.consensus_state
t.state.protocol_state
|> Consensus.Data.Consensus_state.curr_global_slot
|> Consensus.Data.Consensus_state
.global_slot_since_genesis
}
in
{ state; peers = Set.length t.peer_ids } ) )
Expand Down
23 changes: 12 additions & 11 deletions src/app/test_executive/slot_end_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ module Make (Inputs : Intf.Test.Inputs_intf) = struct
(Time.Span.of_ms @@ float_of_int (num_slots * window_ms))
in
let slot_tx_end =
Mina_numbers.Global_slot_since_hard_fork.of_int slot_tx_end
Mina_numbers.Global_slot_since_genesis.of_int slot_tx_end
in
let slot_chain_end =
Mina_numbers.Global_slot_since_hard_fork.of_int slot_chain_end
Mina_numbers.Global_slot_since_genesis.of_int slot_chain_end
in
let%bind () =
section_hard "spawn transaction sending"
Expand Down Expand Up @@ -160,8 +160,8 @@ module Make (Inputs : Intf.Test.Inputs_intf) = struct
section "blocks produced before slot_tx_end"
( ok_if_true "only empty blocks were produced before slot_tx_end"
@@ List.exists blocks ~f:(fun block ->
Mina_numbers.Global_slot_since_hard_fork.(
block.slot < slot_tx_end)
Mina_numbers.Global_slot_since_genesis.(
block.slot_since_genesis < slot_tx_end)
&& ( block.command_transaction_count <> 0
|| block.snark_work_count <> 0
|| Currency.Amount.(block.coinbase <> zero) ) ) )
Expand All @@ -173,13 +173,14 @@ module Make (Inputs : Intf.Test.Inputs_intf) = struct
Printf.sprintf
"non-empty block after slot_tx_end. block slot since genesis: \
%s, txn count: %d, snark work count: %d, coinbase: %s"
(Mina_numbers.Global_slot_since_hard_fork.to_string block.slot)
(Mina_numbers.Global_slot_since_genesis.to_string
block.slot_since_genesis )
block.command_transaction_count block.snark_work_count
(Currency.Amount.to_string block.coinbase)
in
ok_if_true msg
( Mina_numbers.Global_slot_since_hard_fork.(
block.slot < slot_tx_end)
( Mina_numbers.Global_slot_since_genesis.(
block.slot_since_genesis < slot_tx_end)
|| block.command_transaction_count = 0
&& block.snark_work_count = 0
&& Currency.Amount.(block.coinbase = zero) ) ) )
Expand All @@ -188,13 +189,13 @@ module Make (Inputs : Intf.Test.Inputs_intf) = struct
section "blocks produced before slot_chain_end"
( ok_if_true "no block produced before slot_chain_end"
@@ List.exists blocks ~f:(fun block ->
Mina_numbers.Global_slot_since_hard_fork.(
block.slot < slot_chain_end) ) )
Mina_numbers.Global_slot_since_genesis.(
block.slot_since_genesis < slot_chain_end) ) )
in
section "no blocks produced after slot_chain_end"
( ok_if_true "blocks produced after slot_chain_end"
@@ not
@@ List.exists blocks ~f:(fun block ->
Mina_numbers.Global_slot_since_hard_fork.(
block.slot >= slot_chain_end) ) )
Mina_numbers.Global_slot_since_genesis.(
block.slot_since_genesis >= slot_chain_end) ) )
end
44 changes: 29 additions & 15 deletions src/lib/block_producer/block_producer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,17 @@ let generate_next_state ~zkapp_cmd_limit ~constraint_constants
~winner_pk ~scheduled_time ~log_block_creation ~block_reward_threshold
~zkapp_cmd_limit_hardcap ~slot_tx_end ~slot_chain_end =
let open Interruptible.Let_syntax in
let global_slot_since_hard_fork =
Consensus.Data.Block_data.global_slot block_data
let global_slot_since_genesis =
Consensus.Data.Block_data.global_slot_since_genesis block_data
in
match slot_chain_end with
| Some slot_chain_end
when Mina_numbers.Global_slot_since_hard_fork.(
global_slot_since_hard_fork >= slot_chain_end) ->
when Mina_numbers.Global_slot_since_genesis.(
global_slot_since_genesis >= slot_chain_end) ->
[%log info] "Reached slot_chain_end $slot_chain_end, not producing blocks"
~metadata:
[ ( "slot_chain_end"
, Mina_numbers.Global_slot_since_hard_fork.to_yojson slot_chain_end
)
, Mina_numbers.Global_slot_since_genesis.to_yojson slot_chain_end )
] ;
Interruptible.return None
| None | Some _ -> (
Expand Down Expand Up @@ -212,13 +211,13 @@ let generate_next_state ~zkapp_cmd_limit ~constraint_constants
let diff =
match slot_tx_end with
| Some slot_tx_end
when Mina_numbers.Global_slot_since_hard_fork.(
global_slot_since_hard_fork >= slot_tx_end) ->
when Mina_numbers.Global_slot_since_genesis.(
global_slot_since_genesis >= slot_tx_end) ->
[%log info]
"Reached slot_tx_end $slot_tx_end, producing empty block"
~metadata:
[ ( "slot_tx_end"
, Mina_numbers.Global_slot_since_hard_fork.to_yojson
, Mina_numbers.Global_slot_since_genesis.to_yojson
slot_tx_end )
] ;
Result.return
Expand Down Expand Up @@ -1097,10 +1096,26 @@ let run ~context:(module Context : CONTEXT) ~vrf_evaluator ~prover ~verifier
let new_global_slot = epoch_data_for_vrf.global_slot in
let log_if_slot_diff_is_less_than =
let current_global_slot =
Consensus.Data.Consensus_time.(
to_global_slot
(of_time_exn ~constants:consensus_constants
(Block_time.now time_controller) ))
let open Mina_numbers in
let global_slots_since_hard_fork =
let global_slot_since_hard_fork =
Consensus.Data.Consensus_time.(
to_global_slot
(of_time_exn ~constants:consensus_constants
(Block_time.now time_controller) ))
in
Option.value_exn
@@ Global_slot_since_hard_fork.(
diff global_slot_since_hard_fork zero)
in
let open Mina_numbers in
Global_slot_since_genesis.add
( match constraint_constants.fork with
| None ->
Global_slot_since_genesis.zero
| Some { global_slot_since_genesis; _ } ->
global_slot_since_genesis )
global_slots_since_hard_fork
Comment on lines +1099 to +1118
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to be doing this sort of computation in mutliple places in this PR. Can we turn this into a utility function at the global slot module layer? I don't want us getting this conversion wrong in some contexts.

in
fun ~diff_limit ~every ~message -> function
| None ->
Expand All @@ -1109,8 +1124,7 @@ let run ~context:(module Context : CONTEXT) ~vrf_evaluator ~prover ~verifier
let slot_diff =
let open Mina_numbers in
Option.map ~f:Global_slot_span.to_int
@@ Global_slot_since_hard_fork.diff slot
current_global_slot
@@ Global_slot_since_genesis.diff slot current_global_slot
in
Option.iter slot_diff ~f:(fun slot_diff' ->
if slot_diff' <= diff_limit && slot_diff' mod every = 0
Expand Down
5 changes: 3 additions & 2 deletions src/lib/mina_graphql/mina_graphql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2396,10 +2396,11 @@ module Queries = struct
let global_slot =
Mina_state.Protocol_state.consensus_state
protocol_state
|> Consensus.Data.Consensus_state.curr_global_slot
|> Consensus.Data.Consensus_state
.global_slot_since_genesis
in
if
Mina_numbers.Global_slot_since_hard_fork.( < )
Mina_numbers.Global_slot_since_genesis.( < )
global_slot stop_slot
then return breadcrumb
else
Expand Down
48 changes: 37 additions & 11 deletions src/lib/network_pool/indexed_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Config = struct
{ constraint_constants : Genesis_constants.Constraint_constants.t
; consensus_constants : Consensus.Constants.t
; time_controller : Block_time.Controller.t
; slot_tx_end : Mina_numbers.Global_slot_since_hard_fork.t option
; slot_tx_end : Mina_numbers.Global_slot_since_genesis.t option
}
[@@deriving sexp_of, equal, compare]
end
Expand Down Expand Up @@ -906,16 +906,29 @@ module Add_from_gossip_exn (M : Writer_result.S) = struct
let open Command_error in
let open M.Let_syntax in
let current_global_slot =
Consensus.Data.Consensus_time.(
to_global_slot
(of_time_exn ~constants:consensus_constants
(Block_time.now time_controller) ))
let global_slots_since_hard_fork =
let global_slot_since_hard_fork =
Consensus.Data.Consensus_time.(
to_global_slot
(of_time_exn ~constants:consensus_constants
(Block_time.now time_controller) ))
in
Option.value_exn
@@ Global_slot_since_hard_fork.(diff global_slot_since_hard_fork zero)
in
Global_slot_since_genesis.add
( match constraint_constants.fork with
| None ->
Global_slot_since_genesis.zero
| Some { global_slot_since_genesis; _ } ->
global_slot_since_genesis )
global_slots_since_hard_fork
in
let%bind () =
M.of_result
@@ Result.ok_if_true ~error:After_slot_tx_end
@@ Option.value_map slot_tx_end ~default:true ~f:(fun slot_tx_end' ->
Global_slot_since_hard_fork.(current_global_slot < slot_tx_end') )
Global_slot_since_genesis.(current_global_slot < slot_tx_end') )
in
let unchecked_cmd = Transaction_hash.User_command.of_checked cmd in
let unchecked = Transaction_hash.User_command.data unchecked_cmd in
Expand Down Expand Up @@ -1169,15 +1182,28 @@ let add_from_backtrack :
} as t ) cmd ->
let open Result.Let_syntax in
let current_global_slot =
Consensus.Data.Consensus_time.(
to_global_slot
(of_time_exn ~constants:consensus_constants
(Block_time.now time_controller) ))
let global_slots_since_hard_fork =
let global_slot_since_hard_fork =
Consensus.Data.Consensus_time.(
to_global_slot
(of_time_exn ~constants:consensus_constants
(Block_time.now time_controller) ))
in
Option.value_exn
@@ Global_slot_since_hard_fork.(diff global_slot_since_hard_fork zero)
in
Global_slot_since_genesis.add
( match constraint_constants.fork with
| None ->
Global_slot_since_genesis.zero
| Some { global_slot_since_genesis; _ } ->
global_slot_since_genesis )
global_slots_since_hard_fork
in
let%bind () =
Result.ok_if_true ~error:Command_error.After_slot_tx_end
@@ Option.value_map slot_tx_end ~default:true ~f:(fun slot_tx_end' ->
Global_slot_since_hard_fork.(current_global_slot < slot_tx_end') )
Global_slot_since_genesis.(current_global_slot < slot_tx_end') )
in
let unchecked =
Transaction_hash.User_command_with_valid_signature.command cmd
Expand Down
2 changes: 1 addition & 1 deletion src/lib/network_pool/indexed_pool.mli
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ val empty :
constraint_constants:Genesis_constants.Constraint_constants.t
-> consensus_constants:Consensus.Constants.t
-> time_controller:Block_time.Controller.t
-> slot_tx_end:Mina_numbers.Global_slot_since_hard_fork.t option
-> slot_tx_end:Mina_numbers.Global_slot_since_genesis.t option
-> t

(** How many transactions are currently in the pool *)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/network_pool/intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ module type Transaction_resource_pool_intf = sig
-> pool_max_size:int
-> verifier:Verifier.t
-> genesis_constants:Genesis_constants.t
-> slot_tx_end:Mina_numbers.Global_slot_since_hard_fork.t option
-> slot_tx_end:Mina_numbers.Global_slot_since_genesis.t option
-> Config.t

val member : t -> Transaction_hash.User_command_with_valid_signature.t -> bool
Expand Down
24 changes: 6 additions & 18 deletions src/lib/network_pool/transaction_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ struct
*)
; verifier : (Verifier.t[@sexp.opaque])
; genesis_constants : Genesis_constants.t
; slot_tx_end : Mina_numbers.Global_slot_since_hard_fork.t option
; slot_tx_end : Mina_numbers.Global_slot_since_genesis.t option
}
[@@deriving sexp_of]

Expand Down Expand Up @@ -3127,40 +3127,28 @@ let%test_module _ =

let%test_unit "transactions added before slot_tx_end are accepted" =
Thread_safe.block_on_async_exn (fun () ->
let curr_slot =
Mina_numbers.(
Global_slot_since_hard_fork.of_uint32
@@ Global_slot_since_genesis.to_uint32 @@ current_global_slot ())
in
let curr_slot = current_global_slot () in
let slot_tx_end =
Mina_numbers.Global_slot_since_hard_fork.(succ @@ succ curr_slot)
Mina_numbers.Global_slot_since_genesis.(succ @@ succ curr_slot)
in
let%bind t = setup_test ~slot_tx_end () in
assert_pool_txs t [] ;
add_commands t independent_cmds >>| assert_pool_apply independent_cmds )

let%test_unit "transactions added at slot_tx_end are rejected" =
Thread_safe.block_on_async_exn (fun () ->
let curr_slot =
Mina_numbers.(
Global_slot_since_hard_fork.of_uint32
@@ Global_slot_since_genesis.to_uint32 @@ current_global_slot ())
in
let curr_slot = current_global_slot () in
let%bind t = setup_test ~slot_tx_end:curr_slot () in
assert_pool_txs t [] ;
add_commands t independent_cmds >>| assert_pool_apply [] )

let%test_unit "transactions added after slot_tx_end are rejected" =
Thread_safe.block_on_async_exn (fun () ->
let curr_slot =
Mina_numbers.(
Global_slot_since_hard_fork.of_uint32
@@ Global_slot_since_genesis.to_uint32 @@ current_global_slot ())
in
let curr_slot = current_global_slot () in
let slot_tx_end =
Option.value_exn
@@ Mina_numbers.(
Global_slot_since_hard_fork.(
Global_slot_since_genesis.(
sub curr_slot @@ Global_slot_span.of_int 1))
in
let%bind t = setup_test ~slot_tx_end () in
Expand Down
2 changes: 1 addition & 1 deletion src/lib/runtime_config/runtime_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ let make_fork_config ~staged_ledger ~global_slot ~state_hash ~blockchain_length

let slot_tx_end_or_default, slot_chain_end_or_default =
let f compile get_runtime t =
Option.map ~f:Mina_numbers.Global_slot_since_hard_fork.of_int
Option.map ~f:Mina_numbers.Global_slot_since_genesis.of_int
@@ Option.value_map t.daemon ~default:compile ~f:(fun daemon ->
Option.merge compile ~f:(fun _c r -> r) @@ get_runtime daemon )
in
Expand Down
19 changes: 10 additions & 9 deletions src/lib/transition_handler/validator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ let validate_transition ~context:(module Context : CONTEXT) ~frontier
let root_breadcrumb = Transition_frontier.root frontier in
let transition_data = With_hash.data transition in
let block_slot =
Consensus.Data.Consensus_state.curr_global_slot
Consensus.Data.Consensus_state.global_slot_since_genesis
@@ Protocol_state.consensus_state @@ Header.protocol_state
@@ Mina_block.header transition_data
in
let%bind () =
match slot_chain_end with
| Some slot_chain_end
when Mina_numbers.Global_slot_since_hard_fork.(
block_slot >= slot_chain_end) ->
when Mina_numbers.Global_slot_since_genesis.(block_slot >= slot_chain_end)
->
[%log info] "Block after slot_chain_end, rejecting" ;
Result.fail `Block_after_after_stop_slot
| None | Some _ ->
Expand All @@ -48,8 +48,7 @@ let validate_transition ~context:(module Context : CONTEXT) ~frontier
let%bind () =
match slot_tx_end with
| Some slot_tx_end
when Mina_numbers.Global_slot_since_hard_fork.(block_slot >= slot_tx_end)
->
when Mina_numbers.Global_slot_since_genesis.(block_slot >= slot_tx_end) ->
[%log info] "Block after slot_tx_end, validating it is empty" ;
let staged_ledger_diff =
Body.staged_ledger_diff @@ body transition_data
Expand Down Expand Up @@ -242,8 +241,9 @@ let run ~context:(module Context : CONTEXT) ~trust_system ~time_controller
, `String "not empty staged ledger diff after slot_tx_end"
)
; ( "block_slot"
, Mina_numbers.Global_slot_since_hard_fork.to_yojson
@@ Consensus.Data.Consensus_state.curr_global_slot
, Mina_numbers.Global_slot_since_genesis.to_yojson
@@ Consensus.Data.Consensus_state
.global_slot_since_genesis
@@ Protocol_state.consensus_state @@ Header.protocol_state
@@ Mina_block.header @@ transition )
]
Expand All @@ -256,8 +256,9 @@ let run ~context:(module Context : CONTEXT) ~trust_system ~time_controller
[ ("state_hash", State_hash.to_yojson transition_hash)
; ("reason", `String "block after slot_chain_end")
; ( "block_slot"
, Mina_numbers.Global_slot_since_hard_fork.to_yojson
@@ Consensus.Data.Consensus_state.curr_global_slot
, Mina_numbers.Global_slot_since_genesis.to_yojson
@@ Consensus.Data.Consensus_state
.global_slot_since_genesis
@@ Protocol_state.consensus_state @@ Header.protocol_state
@@ Mina_block.header @@ transition )
]
Expand Down