Alpha/Baker: add a context's consistency check before starting the baker

This commit is contained in:
Vincent Botbol 2018-10-08 15:54:01 +02:00 committed by Grégoire Henry
parent a475ef701a
commit 8d2e302f1a
No known key found for this signature in database
GPG Key ID: 50D984F20BD445D2
3 changed files with 32 additions and 6 deletions

View File

@ -991,6 +991,7 @@ let create
let constants =
tzlazy (fun () -> Alpha_services.Constants.all cctxt (`Main, `Hash (bi.Client_baking_blocks.hash, 0))) in
Client_baking_simulator.load_context ~context_path >>= fun index ->
Client_baking_simulator.check_context_consistency index bi.context >>=? fun () ->
let state = create_state ?fee_threshold ~max_waiting_time genesis_hash context_path index delegates constants in
return state
in

View File

@ -26,21 +26,33 @@
open Proto_alpha
open Alpha_context
type error +=
| Failed_to_checkout_context
type error += Failed_to_checkout_context
type error += Invalid_context
let () =
register_error_kind
`Permanent
~id:"Client_baking_simulator.failed_to_checkout_context"
~title: "Fail during checkout context"
~description: ""
~pp:(fun ppf () -> Format.fprintf ppf "@[Failed to checkout the context@]")
~title: "Failed to checkout context"
~description: "The given context hash does not exists in the context."
~pp:(fun ppf () -> Format.fprintf ppf "Failed to checkout the context")
Data_encoding.unit
(function
| Failed_to_checkout_context -> Some ()
| _ -> None)
(fun () -> Failed_to_checkout_context)
(fun () -> Failed_to_checkout_context) ;
register_error_kind
`Permanent
~id:"Client_baking_simulator.invalid_context"
~title: "Invalid context"
~description: "Occurs when the context is inconsistent."
~pp:(fun ppf () ->
Format.fprintf ppf "The given context is invalid.")
Data_encoding.unit
(function
| Invalid_context -> Some ()
| _ -> None)
(fun () -> Invalid_context)
type incremental = {
predecessor: Client_baking_blocks.block_info ;
@ -53,6 +65,16 @@ type incremental = {
let load_context ~context_path =
Context.init ~readonly:true context_path
let check_context_consistency index context_hash =
(* Hypothesis : the version key exists *)
let version_key = ["version"] in
Context.checkout index context_hash >>= function
| None -> fail Failed_to_checkout_context
| Some context ->
Context.mem context version_key >>= function
| true -> return_unit
| false -> fail Invalid_context
let begin_construction ~timestamp ?protocol_data index predecessor =
let { Client_baking_blocks.context } = predecessor in
Context.checkout index context >>= function

View File

@ -36,6 +36,9 @@ type incremental = {
val load_context : context_path:string -> Context.index Lwt.t
(** Make sure that the given context is consistent by trying to read in it *)
val check_context_consistency : Context.index -> Context_hash.t -> unit tzresult Lwt.t
val begin_construction : timestamp:Time.t -> ?protocol_data: block_header_data -> Context.index -> Client_baking_blocks.block_info -> incremental tzresult Lwt.t
val add_operation : incremental -> Operation.packed -> incremental tzresult Lwt.t