From b352a6fa8bb7d7bd836d8020feee3e548f7933b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Santos=20Reis?= Date: Tue, 28 Nov 2023 16:07:35 +0000 Subject: [PATCH] Adjustments to block production and validation. Add notifications --- src/lib/block_producer/block_producer.ml | 71 +++++++++++++++--------- src/lib/ledger_catchup/normal_catchup.ml | 2 + src/lib/ledger_catchup/super_catchup.ml | 7 ++- src/lib/transition_handler/validator.ml | 30 +++++++++- 4 files changed, 81 insertions(+), 29 deletions(-) diff --git a/src/lib/block_producer/block_producer.ml b/src/lib/block_producer/block_producer.ml index 3a0fae54cf9..4211b23852d 100644 --- a/src/lib/block_producer/block_producer.ml +++ b/src/lib/block_producer/block_producer.ml @@ -113,17 +113,34 @@ let generate_next_state ~constraint_constants ~previous_protocol_state ~(block_data : Consensus.Data.Block_data.t) ~winner_pk ~scheduled_time ~log_block_creation ~block_reward_threshold ~consensus_constants = let open Interruptible.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_slot = + Consensus.Data.Block_data.global_slot_since_genesis block_data in match Mina_compile_config.slot_chain_end with | Some slot_chain_end - when Mina_numbers.Global_slot.(current_global_slot >= slot_chain_end) -> + when Mina_numbers.Global_slot.(global_slot >= slot_chain_end) -> + [%log info] "Reached slot_chain_end $slot_chain_end, not producing blocks" + ~metadata: + [ ("slot_chain_end", Mina_numbers.Global_slot.to_yojson slot_chain_end) + ] ; Interruptible.return None | None | Some _ -> ( + let current_global_slot = + Consensus.Data.Consensus_time.( + to_global_slot + (of_time_exn ~constants:consensus_constants + (Block_time.now time_controller) )) + in + let slot_diff slot = + let open Mina_numbers.Global_slot in + Option.map ~f:to_int @@ sub slot current_global_slot + in + Option.iter (Option.bind Mina_compile_config.slot_chain_end ~f:slot_diff) + ~f:(fun slot_diff' -> + if slot_diff' <= 480 && slot_diff' mod 60 = 0 then + [%log info] + "Block producer will stop producing blocks after $slot_diff slots" + ~metadata:[ ("slot_diff", `Int slot_diff') ] ) ; let previous_protocol_state_body_hash = Protocol_state.body previous_protocol_state |> Protocol_state.Body.hash in @@ -138,9 +155,6 @@ let generate_next_state ~constraint_constants ~previous_protocol_state in let supercharge_coinbase = let epoch_ledger = Consensus.Data.Block_data.epoch_ledger block_data in - let global_slot = - Consensus.Data.Block_data.global_slot_since_genesis block_data - in Staged_ledger.can_apply_supercharged_coinbase_exn ~winner:winner_pk ~epoch_ledger ~global_slot in @@ -150,23 +164,28 @@ let generate_next_state ~constraint_constants ~previous_protocol_state let coinbase_receiver = Consensus.Data.Block_data.coinbase_receiver block_data in - let diff = - O1trace.sync_thread "create_staged_ledger_diff" (fun () -> - let current_global_slot = - Consensus.Data.Consensus_time.( - to_global_slot - (of_time_exn ~constants:consensus_constants - (Block_time.now time_controller) )) - in - match Mina_compile_config.slot_tx_end with - | Some slot_tx_end - when Mina_numbers.Global_slot.( - current_global_slot >= slot_tx_end) -> - Ok - Staged_ledger_diff.With_valid_signatures_and_proofs - .empty_diff - | None | Some _ -> ( + match Mina_compile_config.slot_tx_end with + | Some slot_tx_end + when Mina_numbers.Global_slot.(global_slot >= slot_tx_end) -> + [%log info] + "Reached slot_tx_end $slot_tx_end, producing empty block" + ~metadata: + [ ( "slot_tx_end" + , Mina_numbers.Global_slot.to_yojson slot_tx_end ) + ] ; + Result.return + Staged_ledger_diff.With_valid_signatures_and_proofs.empty_diff + | Some _ | None -> + Option.iter + (Option.bind Mina_compile_config.slot_tx_end ~f:slot_diff) + ~f:(fun slot_diff' -> + if slot_diff' <= 480 && slot_diff' mod 60 = 0 then + [%log info] + "Block producer will begin producing only empty blocks \ + after $slot_diff slots" + ~metadata:[ ("slot_diff", `Int slot_diff') ] ) ; + O1trace.sync_thread "create_staged_ledger_diff" (fun () -> let diff = Staged_ledger.create_diff ~constraint_constants staged_ledger ~coinbase_receiver ~logger @@ -198,7 +217,7 @@ let generate_next_state ~constraint_constants ~previous_protocol_state Staged_ledger_diff.With_valid_signatures_and_proofs .empty_diff ) | _ -> - diff ) ) + diff ) in match%map let%bind.Deferred.Result diff = return diff in diff --git a/src/lib/ledger_catchup/normal_catchup.ml b/src/lib/ledger_catchup/normal_catchup.ml index 28725c12847..b074d363aa0 100644 --- a/src/lib/ledger_catchup/normal_catchup.ml +++ b/src/lib/ledger_catchup/normal_catchup.ml @@ -157,6 +157,8 @@ let verify_transition ~logger ~consensus_constants ~trust_system ~frontier Deferred.Or_error.fail @@ Error.of_string "disconnected chain" | Error `Non_empty_staged_ledger_diff_after_stop_slot -> Deferred.Or_error.fail @@ Error.of_string "non empty staged ledger diff" + | Error `Block_after_after_stop_slot -> + Deferred.Or_error.fail @@ Error.of_string "block after stop slot" let rec fold_until ~(init : 'accum) ~(f : diff --git a/src/lib/ledger_catchup/super_catchup.ml b/src/lib/ledger_catchup/super_catchup.ml index cde407fea75..26dc34417b9 100644 --- a/src/lib/ledger_catchup/super_catchup.ml +++ b/src/lib/ledger_catchup/super_catchup.ml @@ -269,8 +269,13 @@ let verify_transition ~logger ~consensus_constants ~trust_system ~frontier [%log warn] ~metadata:[ ("state_hash", state_hash) ] "initial_validate: transition with non empty staged ledger diff after \ - stop slot" ; + slot_tx_end" ; Deferred.Or_error.fail @@ Error.of_string "non empty staged ledger diff" + | Error `Block_after_after_stop_slot -> + [%log warn] + ~metadata:[ ("state_hash", state_hash) ] + "initial_validate: block after slot_chain_end" ; + Deferred.Or_error.fail @@ Error.of_string "block after stop slot" let find_map_ok ?how xs ~f = let res = Ivar.create () in diff --git a/src/lib/transition_handler/validator.ml b/src/lib/transition_handler/validator.ml index c2d4cf27577..69600f012a9 100644 --- a/src/lib/transition_handler/validator.ml +++ b/src/lib/transition_handler/validator.ml @@ -22,9 +22,20 @@ let validate_transition ~consensus_constants ~logger ~frontier @@ Protocol_state.consensus_state @@ Header.protocol_state @@ Mina_block.header transition_data in + let%bind () = + match Mina_compile_config.slot_chain_end with + | Some slot_chain_end + when Mina_numbers.Global_slot.(block_slot >= slot_chain_end) -> + [%log info] "Block after slot_chain_end, rejecting" ; + Result.fail `Block_after_after_stop_slot + | None | Some _ -> + Result.return () + in let%bind () = match Mina_compile_config.slot_tx_end with - | Some slot when Mina_numbers.Global_slot.(block_slot >= slot) -> + | Some slot_tx_end when Mina_numbers.Global_slot.(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 in @@ -149,7 +160,22 @@ let run ~logger ~consensus_constants ~trust_system ~time_controller ~frontier ~metadata: [ ("state_hash", State_hash.to_yojson transition_hash) ; ( "reason" - , `String "not empty staged ledger diff after stop slot" ) + , `String "not empty staged ledger diff after slot_tx_end" + ) + ; ( "block_slot" + , Mina_numbers.Global_slot.to_yojson + @@ Consensus.Data.Consensus_state.curr_global_slot + @@ Protocol_state.consensus_state @@ Header.protocol_state + @@ Mina_block.header @@ transition ) + ] + "Validation error: external transition with state hash \ + $state_hash was rejected for reason $reason" ; + Deferred.unit + | Error `Block_after_after_stop_slot -> + [%log error] + ~metadata: + [ ("state_hash", State_hash.to_yojson transition_hash) + ; ("reason", `String "block after slot_chain_end") ; ( "block_slot" , Mina_numbers.Global_slot.to_yojson @@ Consensus.Data.Consensus_state.curr_global_slot