Skip to content

Commit

Permalink
Adjustments to block production and validation. Add notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
joaosreis committed Nov 28, 2023
1 parent bde4359 commit b352a6f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 29 deletions.
71 changes: 45 additions & 26 deletions src/lib/block_producer/block_producer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/lib/ledger_catchup/normal_catchup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
7 changes: 6 additions & 1 deletion src/lib/ledger_catchup/super_catchup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 28 additions & 2 deletions src/lib/transition_handler/validator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b352a6f

Please sign in to comment.