123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 Trili Tech, <contact@trili.tech> *)(* *)(* 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. *)(* *)(*****************************************************************************)openAlpha_contexttypeerror+=(* Permanent *)Sc_rollup_invalid_destinationlet()=letopenData_encodinginletmsg="Invalid destination"inregister_error_kind`Permanent~id:"sc_rollup_management_protocol.sc_rollup_invalid_destination"~title:msg~pp:(funfmt()->Format.fprintffmt"%s"msg)~description:msgunit(functionSc_rollup_invalid_destination->Some()|_->None)(fun()->Sc_rollup_invalid_destination)typetransaction=|Transaction:{destination:Contract_hash.t;entrypoint:Entrypoint.t;parameters_ty:('a,_)Script_typed_ir.ty;parameters:'a;unparsed_parameters:Script.expr;}->transactiontypeatomic_transaction_batch={transactions:transactionlist}typeoutbox_message=Atomic_transaction_batchofatomic_transaction_batchletmake_internal_inbox_messagectxtty~payload~sender~source=letopenLwt_tzresult_syntaxinlet+payload,ctxt=Script_ir_translator.unparse_datactxtScript_ir_unparser.Optimizedtypayloadin(Sc_rollup.Inbox_message.Internal{payload;sender;source},ctxt)lettransactions_batch_of_internalctxttransactions=letopenLwt_tzresult_syntaxinletor_internal_transactionctxt{Sc_rollup.Outbox.Message.unparsed_parameters;destination;entrypoint}=(* Lookup the contract-hash. *)(* Load the type and entrypoints of the script. *)let*(Script_ir_translator.Ex_script(Script{arg_type;entrypoints;_}),ctxt)=let*ctxt,_cache_key,cached=Script_cache.findctxtdestinationinmatchcachedwith|Some(_script,ex_script)->return(ex_script,ctxt)|None->failSc_rollup_invalid_destinationin(* Find the entrypoint type for the given entrypoint. *)let*?res,ctxt=Gas_monad.runctxt(Script_ir_translator.find_entrypoint~error_details:(Informative())arg_typeentrypointsentrypoint)inlet*?(Ex_ty_cstr{ty=parameters_ty;_})=resin(* Parse the parameters according to the entrypoint type. *)let*parameters,ctxt=Script_ir_translator.parse_datactxt~elab_conf:Script_ir_translator_config.(make~legacy:false())~allow_forged:trueparameters_ty(Micheline.rootunparsed_parameters)inreturn(Transaction{destination;entrypoint;parameters_ty;parameters;unparsed_parameters;},ctxt)inlet+ctxt,transactions=List.fold_left_map_es(functxtmsg->let+t,ctxt=or_internal_transactionctxtmsgin(ctxt,t))ctxttransactionsin({transactions},ctxt)letoutbox_message_of_outbox_message_reprctxt(Sc_rollup.Outbox.Message.Atomic_transaction_batch{transactions})=letopenLwt_tzresult_syntaxinlet+ts,ctxt=transactions_batch_of_internalctxttransactionsin(Atomic_transaction_batchts,ctxt)moduleInternal_for_tests=structletmake_transactionctxtparameters_ty~parameters~destination~entrypoint=letopenLwt_tzresult_syntaxinlet*unparsed_parameters,ctxt=Script_ir_translator.unparse_datactxtOptimizedparameters_typarametersinreturn(Transaction{destination;entrypoint;parameters_ty;parameters;unparsed_parameters;},ctxt)letmake_atomic_batchtransactions=Atomic_transaction_batch{transactions}letserialize_outbox_message(Atomic_transaction_batch{transactions})=letopenTzresult_syntaxinletto_internal_transaction(Transaction{destination;entrypoint;parameters_ty=_;parameters=_;unparsed_parameters;})=return{Sc_rollup.Outbox.Message.unparsed_parameters;destination;entrypoint}inlet*transactions=List.map_eto_internal_transactiontransactionsinletoutput_message_internal=Sc_rollup.Outbox.Message.Atomic_transaction_batch{transactions}inSc_rollup.Outbox.Message.serializeoutput_message_internalletdeserialize_inbox_message=Sc_rollup.Inbox_message.deserializeend