123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2020 Metastate AG <hello@metastate.dev> *)(* Copyright (c) 2018-2022 Nomadic Labs, <contact@nomadic-labs.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. *)(* *)(*****************************************************************************)(* FIXME: https://gitlab.com/tezos/tezos/-/issues/4113
This file is part of the implementation of the legacy mempool,
which is compatible with Kathmandu and therefore usable on Mainnet.
This file should be removed once Lima has been activated on Mainnet.
When you modify this file, consider whether you should also change
the files that implement the more recent mempool for Lima and newer
protocols. *)openValidation_errorstype'protocol_operationoperation={hash:Operation_hash.t;raw:Operation.t;protocol:'protocol_operation;count_successful_prechecks:int;}typeerror+=Endorsement_branch_not_livelet()=register_error_kind`Permanent~id:"legacy_prevalidation.endorsement_branch_not_live"~title:"Endorsement branch not live"~description:"Endorsement's branch is not in the live blocks"~pp:(funppf()->Format.fprintfppf"Endorsement's branch is not in the live blocks")Data_encoding.(empty)(functionEndorsement_branch_not_live->Some()|_->None)(fun()->Endorsement_branch_not_live)moduletypeCHAIN_STORE=sigtypechain_storevalcontext:chain_store->Store.Block.t->Tezos_protocol_environment.Context.ttzresultLwt.tvalchain_id:chain_store->Chain_id.tendmoduletypeT=sigtypeprotocol_operationtypeoperation_receipttypevalidation_statetypechain_storetypetvalparse:Operation_hash.t->Operation.t->protocol_operationoperationtzresultvalincrement_successful_precheck:protocol_operationoperation->protocol_operationoperationvalcreate:chain_store->predecessor:Store.Block.t->live_operations:Operation_hash.Set.t->timestamp:Time.Protocol.t->unit->ttzresultLwt.ttyperesult=|Appliedoft*operation_receipt|Branch_delayedoftztrace|Branch_refusedoftztrace|Refusedoftztrace|Outdatedoftztracevalapply_operation:t->protocol_operationoperation->resultLwt.tvalvalidation_state:t->validation_statevalset_validation_state:t->validation_state->tvalpp_result:Format.formatter->result->unitmoduleInternal_for_tests:sigvalto_applied:t->(protocol_operationoperation*operation_receipt)listendend(** Doesn't depend on heavy [Registered_protocol.T] for testability. *)letsafe_binary_of_bytes(encoding:'aData_encoding.t)(bytes:bytes):'atzresult=letopenResult_syntaxinmatchData_encoding.Binary.of_bytes_optencodingbyteswith|None->tzfailParse_error|Someprotocol_data->returnprotocol_datamoduleMakeAbstract(Chain_store:CHAIN_STORE)(Proto:Tezos_protocol_environment.PROTOCOL):Twithtypeprotocol_operation=Proto.operationandtypeoperation_receipt=Proto.operation_receiptandtypevalidation_state=Proto.validation_stateandtypechain_store=Chain_store.chain_store=structtypeprotocol_operation=Proto.operationtypeoperation_receipt=Proto.operation_receipttypevalidation_state=Proto.validation_statetypechain_store=Chain_store.chain_storetypet={validation_state:validation_state;application_state:Proto.application_state;applied:(protocol_operationoperation*Proto.operation_receipt)list;live_operations:Operation_hash.Set.t;}typeresult=|Appliedoft*Proto.operation_receipt|Branch_delayedoftztrace|Branch_refusedoftztrace|Refusedoftztrace|Outdatedoftztraceletparse_unsafe(proto:bytes):Proto.operation_datatzresult=safe_binary_of_bytesProto.operation_data_encodingprotoletparsehash(raw:Operation.t)=letopenResult_syntaxinletsize=Data_encoding.Binary.lengthOperation.encodingrawinifsize>Proto.max_operation_data_lengththentzfail(Oversized_operation{size;max=Proto.max_operation_data_length})elselet+protocol_data=parse_unsaferaw.protoin{hash;raw;protocol={Proto.shell=raw.Operation.shell;protocol_data};(* When an operation is parsed, we assume that it has never been
successfully prechecked. *)count_successful_prechecks=0;}letincrement_successful_precheckop=(* We avoid {op with ...} to get feedback from the compiler if the record
type is extended/modified in the future. *){hash=op.hash;raw=op.raw;protocol=op.protocol;count_successful_prechecks=op.count_successful_prechecks+1;}letcreatechain_store~predecessor~live_operations~timestamp()=(* The prevalidation module receives input from the system byt handles
protocol values. It translates timestamps here. *)letopenLwt_result_syntaxinlet*predecessor_context=Chain_store.contextchain_storepredecessorinletpredecessor_hash=Store.Block.hashpredecessorinlet*!predecessor_context=Block_validation.update_testchain_statuspredecessor_context~predecessor_hashtimestampinletchain_id=Chain_store.chain_idchain_storeinletmode=Proto.Partial_construction{predecessor_hash;timestamp}inletpredecessor=(Store.Block.headerpredecessor).shellinlet*validation_state=Proto.begin_validationpredecessor_contextchain_idmode~predecessor~cache:`Lazyinlet*application_state=Proto.begin_applicationpredecessor_contextchain_idmode~predecessor~cache:`Lazyinreturn{validation_state;application_state;applied=[];live_operations}letapply_operationpvop=letopenLwt_syntaxinifOperation_hash.Set.memop.hashpv.live_operationsthen(* As of November 2021, it is dubious that this case can happen.
If it can, it is more likely to be because of a consensus operation;
hence the returned error. *)Lwt.return(Outdated[Endorsement_branch_not_live])elselet+r=protect(fun()->letopenLwt_result_syntaxinlet*validation_state=Proto.validate_operationpv.validation_stateop.hashop.protocolinlet*application_state,receipt=Proto.apply_operationpv.application_stateop.hashop.protocolinreturn(validation_state,application_state,receipt))inmatchrwith|Ok(validation_state,application_state,receipt)->(letpv={validation_state;application_state;applied=(op,receipt)::pv.applied;live_operations=Operation_hash.Set.addop.hashpv.live_operations;}inmatchData_encoding.Binary.(of_bytes_exnProto.operation_receipt_encoding(to_bytes_exnProto.operation_receipt_encodingreceipt))with|receipt->Applied(pv,receipt)|exceptionexn->Refused[Validation_errors.Cannot_serialize_operation_metadata;Exnexn])|Errortrace->(matchclassify_tracetracewith|Branch->Branch_refusedtrace|Permanent->Refusedtrace|Temporary->Branch_delayedtrace|Outdated->Outdatedtrace)letvalidation_state{validation_state;_}=validation_stateletset_validation_statetvalidation_state={twithvalidation_state}letpp_resultppf=letopenFormatinfunction|Applied_->pp_print_stringppf"applied"|Branch_delayederr->fprintfppf"branch delayed (%a)"pp_print_traceerr|Branch_refusederr->fprintfppf"branch refused (%a)"pp_print_traceerr|Refusederr->fprintfppf"refused (%a)"pp_print_traceerr|Outdatederr->fprintfppf"outdated (%a)"pp_print_traceerrmoduleInternal_for_tests=structletto_applied{applied;_}=appliedendendmoduleProduction_chain_store:CHAIN_STOREwithtypechain_store=Store.chain_store=structtypechain_store=Store.chain_storeletcontext=Store.Block.contextletchain_id=Store.Chain.chain_idendmoduleMake(Proto:Tezos_protocol_environment.PROTOCOL):Twithtypeprotocol_operation=Proto.operationandtypeoperation_receipt=Proto.operation_receiptandtypevalidation_state=Proto.validation_stateandtypechain_store=Production_chain_store.chain_store=MakeAbstract(Production_chain_store)(Proto)moduleInternal_for_tests=structletto_raw{raw;_}=rawlethash_of{hash;_}=hashletmake_operationopophdata=(* When we build an operation, we assume that it has never been
successfully prechecked. *){hash=oph;raw=op;protocol=data;count_successful_prechecks=0}letsafe_binary_of_bytes=safe_binary_of_bytesmoduletypeCHAIN_STORE=CHAIN_STOREmoduleMake=MakeAbstractend