From 3d24a7e2d6924e4d009346da355599cde1b9c69e Mon Sep 17 00:00:00 2001 From: vbot Date: Thu, 26 Jan 2023 17:09:39 +0100 Subject: [PATCH 1/3] Alpha/Baker: discard corrupted/legacy state files --- src/proto_alpha/lib_delegate/baking_state.ml | 168 ++----------------- 1 file changed, 17 insertions(+), 151 deletions(-) diff --git a/src/proto_alpha/lib_delegate/baking_state.ml b/src/proto_alpha/lib_delegate/baking_state.ml index 848b7686c787..2626d60cb325 100644 --- a/src/proto_alpha/lib_delegate/baking_state.ml +++ b/src/proto_alpha/lib_delegate/baking_state.ml @@ -437,138 +437,21 @@ let event_encoding = (fun tk -> Timeout tk); ] -(* Legacy disk state support: this will only be used when migrating - from a baker using the previous format to the new one and will - happen once. *) - -type legacy_block_info = { - legacy_hash : Block_hash.t; - legacy_shell : Block_header.shell_header; - legacy_payload_hash : Block_payload_hash.t; - legacy_payload_round : Round.t; - legacy_round : Round.t; - legacy_protocol : Protocol_hash.t; - legacy_next_protocol : Protocol_hash.t; - legacy_prequorum : prequorum option; - legacy_quorum : Kind.endorsement operation list; - legacy_payload : Operation_pool.payload; - legacy_live_blocks : Block_hash.Set.t; -} - -let legacy_block_info_encoding : legacy_block_info Data_encoding.t = - let open Data_encoding in - conv - (fun ({ - legacy_hash; - legacy_shell; - legacy_payload_hash; - legacy_payload_round; - legacy_round; - legacy_protocol; - legacy_next_protocol; - legacy_prequorum; - legacy_quorum; - legacy_payload; - legacy_live_blocks; - } : - legacy_block_info) -> - ( ( legacy_hash, - legacy_shell, - legacy_payload_hash, - legacy_payload_round, - legacy_round, - legacy_protocol, - legacy_next_protocol, - legacy_prequorum, - List.map Operation.pack legacy_quorum, - legacy_payload ), - legacy_live_blocks )) - (fun ( ( legacy_hash, - legacy_shell, - legacy_payload_hash, - legacy_payload_round, - legacy_round, - legacy_protocol, - legacy_next_protocol, - legacy_prequorum, - legacy_quorum, - legacy_payload ), - legacy_live_blocks ) -> - { - legacy_hash; - legacy_shell; - legacy_payload_hash; - legacy_payload_round; - legacy_round; - legacy_protocol; - legacy_next_protocol; - legacy_prequorum; - legacy_quorum = - List.filter_map Operation_pool.unpack_endorsement legacy_quorum; - legacy_payload; - legacy_live_blocks; - }) - (merge_objs - (obj10 - (req "hash" Block_hash.encoding) - (req "shell" Block_header.shell_header_encoding) - (req "payload_hash" Block_payload_hash.encoding) - (req "payload_round" Round.encoding) - (req "round" Round.encoding) - (req "protocol" Protocol_hash.encoding) - (req "next_protocol" Protocol_hash.encoding) - (req "prequorum" (option prequorum_encoding)) - (req "quorum" (list (dynamic_size Operation.encoding))) - (req "payload" Operation_pool.payload_encoding)) - (obj1 (req "live_blocks" Block_hash.Set.encoding))) - -type legacy_proposal = { - legacy_block : legacy_block_info; - legacy_predecessor : legacy_block_info; -} - -let legacy_proposal_encoding = - let open Data_encoding in - conv - (fun {legacy_block; legacy_predecessor} -> - (legacy_block, legacy_predecessor)) - (fun (legacy_block, legacy_predecessor) -> - {legacy_block; legacy_predecessor}) - (obj2 - (req "block" legacy_block_info_encoding) - (req "predecessor" legacy_block_info_encoding)) - -type legacy_endorsable_payload = { - legacy_proposal : legacy_proposal; - prequorum : prequorum; -} +(* Disk state *) -let legacy_endorsable_payload_encoding = - let open Data_encoding in - conv - (fun {legacy_proposal; prequorum} -> (legacy_proposal, prequorum)) - (fun (legacy_proposal, prequorum) -> {legacy_proposal; prequorum}) - (obj2 - (req "proposal" legacy_proposal_encoding) - (req "prequorum" prequorum_encoding)) +module Events = struct + include Internal_event.Simple -type legacy_state_data = { - level_data : int32; - locked_round_data : locked_round option; - legacy_endorsable_payload_data : legacy_endorsable_payload option; -} + let section = [Protocol.name; "baker"; "disk"] -let legacy_state_data_encoding = - let open Data_encoding in - conv - (fun {level_data; locked_round_data; legacy_endorsable_payload_data} -> - (level_data, locked_round_data, legacy_endorsable_payload_data)) - (fun (level_data, locked_round_data, legacy_endorsable_payload_data) -> - {level_data; locked_round_data; legacy_endorsable_payload_data}) - (obj3 - (req "level" int32) - (req "locked_round" (option locked_round_encoding)) - (req "endorsable_payload" (option legacy_endorsable_payload_encoding))) + let incompatible_stored_state = + declare_0 + ~section + ~name:"incompatible_stored_state" + ~level:Warning + ~msg:"found an outdated or corrupted baking state: discarding it" + () +end type state_data = { level_data : int32; @@ -576,25 +459,6 @@ type state_data = { endorsable_payload_data : endorsable_payload option; } -let update_legacy_state_data raw_data = - (* "Upgrade" the file format by dumping the legacy - endorsable data. It is sound as the new baker - cannot start (i.e. load the preexisting state) - until the new protocol activates. - - Note: the new encoding is not backward compatible - (unless the endorsable_payload_data is [None]). *) - let legacy_state_data = - Data_encoding.Binary.of_string_exn legacy_state_data_encoding raw_data - in - { - level_data = legacy_state_data.level_data; - locked_round_data = None; - endorsable_payload_data = None; - } - -(* Disk state *) - let state_data_encoding = let open Data_encoding in conv @@ -724,9 +588,11 @@ let load_endorsable_data cctxt location = match Data_encoding.Binary.of_string_opt state_data_encoding str with - | Some state_data -> Lwt.return state_data - | None -> Lwt.return (update_legacy_state_data str)) - >>= return_some) + | Some state_data -> return_some state_data + | None -> + (* The stored state format is incompatible: discard it. *) + Events.(emit incompatible_stored_state ()) >>= fun () -> + return_none)) let may_load_endorsable_data state = let cctxt = state.global_state.cctxt in -- GitLab From 54730790e7fe91ef0c0f394484f0a618b91cdd13 Mon Sep 17 00:00:00 2001 From: vbot Date: Thu, 9 Feb 2023 14:31:50 +0100 Subject: [PATCH 2/3] Mumbai/Baker: backport changes --- .../lib_delegate/baking_state.ml | 168 ++---------------- 1 file changed, 17 insertions(+), 151 deletions(-) diff --git a/src/proto_016_PtMumbai/lib_delegate/baking_state.ml b/src/proto_016_PtMumbai/lib_delegate/baking_state.ml index 475306cac7d8..8d7b97748316 100644 --- a/src/proto_016_PtMumbai/lib_delegate/baking_state.ml +++ b/src/proto_016_PtMumbai/lib_delegate/baking_state.ml @@ -436,138 +436,21 @@ let event_encoding = (fun tk -> Timeout tk); ] -(* Legacy disk state support: this will only be used when migrating - from a baker using the previous format to the new one and will - happen once. *) - -type legacy_block_info = { - legacy_hash : Block_hash.t; - legacy_shell : Block_header.shell_header; - legacy_payload_hash : Block_payload_hash.t; - legacy_payload_round : Round.t; - legacy_round : Round.t; - legacy_protocol : Protocol_hash.t; - legacy_next_protocol : Protocol_hash.t; - legacy_prequorum : prequorum option; - legacy_quorum : Kind.endorsement operation list; - legacy_payload : Operation_pool.payload; - legacy_live_blocks : Block_hash.Set.t; -} - -let legacy_block_info_encoding : legacy_block_info Data_encoding.t = - let open Data_encoding in - conv - (fun ({ - legacy_hash; - legacy_shell; - legacy_payload_hash; - legacy_payload_round; - legacy_round; - legacy_protocol; - legacy_next_protocol; - legacy_prequorum; - legacy_quorum; - legacy_payload; - legacy_live_blocks; - } : - legacy_block_info) -> - ( ( legacy_hash, - legacy_shell, - legacy_payload_hash, - legacy_payload_round, - legacy_round, - legacy_protocol, - legacy_next_protocol, - legacy_prequorum, - List.map Operation.pack legacy_quorum, - legacy_payload ), - legacy_live_blocks )) - (fun ( ( legacy_hash, - legacy_shell, - legacy_payload_hash, - legacy_payload_round, - legacy_round, - legacy_protocol, - legacy_next_protocol, - legacy_prequorum, - legacy_quorum, - legacy_payload ), - legacy_live_blocks ) -> - { - legacy_hash; - legacy_shell; - legacy_payload_hash; - legacy_payload_round; - legacy_round; - legacy_protocol; - legacy_next_protocol; - legacy_prequorum; - legacy_quorum = - List.filter_map Operation_pool.unpack_endorsement legacy_quorum; - legacy_payload; - legacy_live_blocks; - }) - (merge_objs - (obj10 - (req "hash" Block_hash.encoding) - (req "shell" Block_header.shell_header_encoding) - (req "payload_hash" Block_payload_hash.encoding) - (req "payload_round" Round.encoding) - (req "round" Round.encoding) - (req "protocol" Protocol_hash.encoding) - (req "next_protocol" Protocol_hash.encoding) - (req "prequorum" (option prequorum_encoding)) - (req "quorum" (list (dynamic_size Operation.encoding))) - (req "payload" Operation_pool.payload_encoding)) - (obj1 (req "live_blocks" Block_hash.Set.encoding))) - -type legacy_proposal = { - legacy_block : legacy_block_info; - legacy_predecessor : legacy_block_info; -} - -let legacy_proposal_encoding = - let open Data_encoding in - conv - (fun {legacy_block; legacy_predecessor} -> - (legacy_block, legacy_predecessor)) - (fun (legacy_block, legacy_predecessor) -> - {legacy_block; legacy_predecessor}) - (obj2 - (req "block" legacy_block_info_encoding) - (req "predecessor" legacy_block_info_encoding)) - -type legacy_endorsable_payload = { - legacy_proposal : legacy_proposal; - prequorum : prequorum; -} +(* Disk state *) -let legacy_endorsable_payload_encoding = - let open Data_encoding in - conv - (fun {legacy_proposal; prequorum} -> (legacy_proposal, prequorum)) - (fun (legacy_proposal, prequorum) -> {legacy_proposal; prequorum}) - (obj2 - (req "proposal" legacy_proposal_encoding) - (req "prequorum" prequorum_encoding)) +module Events = struct + include Internal_event.Simple -type legacy_state_data = { - level_data : int32; - locked_round_data : locked_round option; - legacy_endorsable_payload_data : legacy_endorsable_payload option; -} + let section = [Protocol.name; "baker"; "disk"] -let legacy_state_data_encoding = - let open Data_encoding in - conv - (fun {level_data; locked_round_data; legacy_endorsable_payload_data} -> - (level_data, locked_round_data, legacy_endorsable_payload_data)) - (fun (level_data, locked_round_data, legacy_endorsable_payload_data) -> - {level_data; locked_round_data; legacy_endorsable_payload_data}) - (obj3 - (req "level" int32) - (req "locked_round" (option locked_round_encoding)) - (req "endorsable_payload" (option legacy_endorsable_payload_encoding))) + let incompatible_stored_state = + declare_0 + ~section + ~name:"incompatible_stored_state" + ~level:Warning + ~msg:"found an outdated or corrupted baking state: discarding it" + () +end type state_data = { level_data : int32; @@ -575,25 +458,6 @@ type state_data = { endorsable_payload_data : endorsable_payload option; } -let update_legacy_state_data raw_data = - (* "Upgrade" the file format by dumping the legacy - endorsable data. It is sound as the new baker - cannot start (i.e. load the preexisting state) - until the new protocol activates. - - Note: the new encoding is not backward compatible - (unless the endorsable_payload_data is [None]). *) - let legacy_state_data = - Data_encoding.Binary.of_string_exn legacy_state_data_encoding raw_data - in - { - level_data = legacy_state_data.level_data; - locked_round_data = None; - endorsable_payload_data = None; - } - -(* Disk state *) - let state_data_encoding = let open Data_encoding in conv @@ -723,9 +587,11 @@ let load_endorsable_data cctxt location = match Data_encoding.Binary.of_string_opt state_data_encoding str with - | Some state_data -> Lwt.return state_data - | None -> Lwt.return (update_legacy_state_data str)) - >>= return_some) + | Some state_data -> return_some state_data + | None -> + (* The stored state format is incompatible: discard it. *) + Events.(emit incompatible_stored_state ()) >>= fun () -> + return_none)) let may_load_endorsable_data state = let cctxt = state.global_state.cctxt in -- GitLab From 34e4d24b5cdf9273a0a465b535d33544e997f660 Mon Sep 17 00:00:00 2001 From: vbot Date: Thu, 9 Feb 2023 14:34:49 +0100 Subject: [PATCH 3/3] Changes: added entry for baker corrupted/legacy states discarding --- CHANGES.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 6fbfabecdc30..049a1870ca77 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -74,6 +74,10 @@ Baker should make sure that their operations' signatures are correct. (MR :gl:`!7490`) +- Made the baker discard legacy or corrupted Tenderbake's saved + states in order to avoid unexpected crashes when the baker gets + updated, or when a new protocol's baker starts. (MR :gl:`!7640`) + Accuser ------- -- GitLab