123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openProtocol_client_contextopenProtocolopenAlpha_contexttypeerror+=Failed_to_checkout_contexttypeerror+=Invalid_contextlet()=register_error_kind`Permanent~id:"Client_baking_simulator.failed_to_checkout_context"~title:"Failed to checkout context"~description:"The given context hash does not exists in the context."~pp:(funppf()->Format.fprintfppf"Failed to checkout the context")Data_encoding.unit(functionFailed_to_checkout_context->Some()|_->None)(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:(funppf()->Format.fprintfppf"The given context is invalid.")Data_encoding.unit(functionInvalid_context->Some()|_->None)(fun()->Invalid_context)typeincremental={predecessor:Baking_state.block_info;context:Tezos_protocol_environment.Context.t;state:Protocol.validation_state*Protocol.application_state;rev_operations:Operation.packedlist;header:Tezos_base.Block_header.shell_header;}letload_context~context_path=protect(fun()->Context.init~readonly:truecontext_path>>=funindex->return(Abstract_context_index.abstractindex))letcheck_context_consistency(abstract_index:Abstract_context_index.t)context_hash=protect(fun()->(* Hypothesis : the version key exists *)letversion_key=["version"]inabstract_index.checkout_funcontext_hash>>=function|None->failFailed_to_checkout_context|Somecontext->(Context_ops.memcontextversion_key>>=function|true->return_unit|false->failInvalid_context))letbegin_construction~timestamp~protocol_data(abstract_index:Abstract_context_index.t)predecessorchain_id=protect(fun()->let{Baking_state.shell=pred_shell;hash=pred_hash;_}=predecessorinabstract_index.checkout_funpred_shell.context>>=function|None->failFailed_to_checkout_context|Somecontext->letheader:Tezos_base.Block_header.shell_header=Tezos_base.Block_header.{predecessor=pred_hash;proto_level=pred_shell.proto_level;validation_passes=0;fitness=pred_shell.fitness;timestamp;level=pred_shell.level;context=Context_hash.zero(* fake context hash *);operations_hash=Operation_list_list_hash.zero(* fake op hash *);}inletmode=Lifted_protocol.Construction{predecessor_hash=predecessor.hash;timestamp;block_header_data=protocol_data;}inLifted_protocol.begin_validationcontextchain_idmode~predecessor:pred_shell~cache:`Lazy>>=?funvalidation_state->Lifted_protocol.begin_applicationcontextchain_idmode~predecessor:pred_shell~cache:`Lazy>>=?funapplication_state->letstate=(validation_state,application_state)inreturn{predecessor;context;state;rev_operations=[];header})let(let**)xk=letopenLwt_result_syntaxinlet*!x=xinlet*?x=Environment.wrap_tzresultxinkxletadd_operationst(op:Operation.packed)=protect(fun()->letvalidation_state,application_state=st.stateinletoph=Operation.hash_packedopinlet**validation_state=Protocol.validate_operationvalidation_stateophopinlet**application_state,receipt=Protocol.apply_operationapplication_stateophopinletstate=(validation_state,application_state)inreturn({stwithstate;rev_operations=op::st.rev_operations},receipt))letfinalize_constructioninc=protect(fun()->letvalidation_state,application_state=inc.stateinlet**()=Protocol.finalize_validationvalidation_stateinlet**result=Protocol.finalize_applicationapplication_state(Someinc.header)inreturnresult)