123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Nomadic Development. <contact@tezcore.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. *)(* *)(*****************************************************************************)openProtocolopenAlpha_contextmoduleView_helpers=structopenTezos_michelinetypeEnvironment.Error_monad.error+=Viewed_contract_has_no_scripttypeEnvironment.Error_monad.error+=View_callback_origination_failedtypeEnvironment.Error_monad.error+=|Illformed_view_typeofstring*Script.exprtypeEnvironment.Error_monad.error+=|View_never_returnsofstring*Contract.ttypeEnvironment.Error_monad.error+=|View_unexpected_returnofstring*Contract.tlet()=Environment.Error_monad.register_error_kind`Permanent~id:"viewedContractHasNoScript"~title:"Viewed contract has no script"~description:"A view was called on a contract with no script."~pp:(funppf()->Format.fprintfppf"A view was called on a contract with no script.")Data_encoding.(unit)(functionViewed_contract_has_no_script->Some()|_->None)(fun()->Viewed_contract_has_no_script);Environment.Error_monad.register_error_kind`Permanent~id:"viewCallbackOriginationFailed"~title:"View callback origination failed"~description:"View callback origination failed"~pp:(funppf()->Format.fprintfppf"Error during origination of view callback contract.")Data_encoding.(unit)(functionView_callback_origination_failed->Some()|_->None)(fun()->View_callback_origination_failed);Environment.Error_monad.register_error_kind`Permanent~id:"illformedViewType"~title:"An entrypoint type is incompatible with TZIP-4 view type."~description:"An entrypoint type is incompatible with TZIP-4 view type."~pp:(funppf(entrypoint,typ)->Format.fprintfppf"The view %s has type %a, it is not compatible with a TZIP-4 view \
type."entrypointMicheline_printer.print_expr(Micheline_printer.printable(funx->x)(Michelson_v1_primitives.strings_of_primstyp)))Data_encoding.(obj2(req"entrypoint"string)(req"type"Script.expr_encoding))(functionIllformed_view_type(etp,exp)->Some(etp,exp)|_->None)(fun(etp,exp)->Illformed_view_type(etp,exp));Environment.Error_monad.register_error_kind`Permanent~id:"viewNeverReturns"~title:"A view never returned a transaction to the given callback contract"~description:"A view never initiated a transaction to the given callback contract."~pp:(funppf(entrypoint,callback)->Format.fprintfppf"The view %s never initiated a transaction to the given callback \
contract %a."entrypointContract.ppcallback)Data_encoding.(obj2(req"entrypoint"string)(req"callback"Contract.encoding))(functionView_never_returns(e,c)->Some(e,c)|_->None)(fun(e,c)->View_never_returns(e,c));Environment.Error_monad.register_error_kind`Permanent~id:"viewUnexpectedReturn"~title:"A view returned an unexpected list of operations"~description:"A view initiated a list of operations while the TZIP-4 standard \
expects only a transaction to the given callback contract."~pp:(funppf(entrypoint,callback)->Format.fprintfppf"The view %s initiated a list of operations while the TZIP-4 \
standard expects only a transaction to the given callback contract \
%a."entrypointContract.ppcallback)Data_encoding.(obj2(req"entrypoint"string)(req"callback"Contract.encoding))(functionView_never_returns(e,c)->Some(e,c)|_->None)(fun(e,c)->View_never_returns(e,c))(* This script is actually never run, its usage is to ensure a
contract that has the type `contract <ty>` is originated, which
will be required as callback of the view. *)letmake_viewer_scriptty:Script.t=letloc=0inletty=Micheline.roottyinletcode=Micheline.strip_locations@@Micheline.Seq(loc,[Micheline.Prim(loc,Script.K_parameter,[ty],[]);Micheline.Prim(loc,Script.K_storage,[Micheline.Prim(loc,Script.T_unit,[],[])],[]);Micheline.Prim(loc,Script.K_code,[Micheline.Prim(loc,Script.I_FAILWITH,[],[])],[]);])inletstorage=Micheline.strip_locations(Micheline.Prim(loc,Script.D_Unit,[],[]))in{code=Script.lazy_exprcode;storage=Script.lazy_exprstorage}letmake_view_parameterinputcallback=letloc=0inMicheline.strip_locations(Micheline.Prim(loc,Script.D_Pair,[input;Micheline.Bytes(loc,Data_encoding.Binary.to_bytes_exnContract.encodingcallback);],[]))letextract_view_output_typeentrypointty=matchMicheline.roottywith|Micheline.Prim(_,Script.T_pair,[_;Micheline.Prim(_,Script.T_contract,[ty],_)],_)->ok(Micheline.strip_locationsty)|_->Environment.Error_monad.error(Illformed_view_type(entrypoint,ty))(* 'view' entrypoints returns their value by calling a callback contract, thus
the expected result is a unique internal transaction to this callback. *)letextract_parameter_from_operationsentrypointoperationscallback=letunexpected_return=Environment.Error_monad.error@@View_unexpected_return(entrypoint,callback)inmatchoperationswith|[Internal_operation{operation=Transaction{destination;parameters;_};_};]whenContract.equaldestinationcallback->okparameters|[]->Environment.Error_monad.error(View_never_returns(entrypoint,callback))|_->unexpected_returnendmoduleRPC=structopenEnvironmenttypeEnvironment.Error_monad.error+=Cannot_serialize_log_normalizedlet()=(* Cannot serialize log *)Environment.Error_monad.register_error_kind`Temporary~id:"michelson_v1.cannot_serialize_log_normalized"~title:"Not enough gas to serialize normalized execution trace"~description:"Execution trace with normalized stacks was to big to be serialized \
with the provided gas"Data_encoding.empty(functionCannot_serialize_log_normalized->Some()|_->None)(fun()->Cannot_serialize_log_normalized)moduleUnparse_types=struct(* Same as the unparsing functions for types in Script_ir_translator but
does not consume gas and never folds (pair a (pair b c)) *)openScript_ir_translatoropenMichelineopenMichelson_v1_primitivesopenScript_ir_annotopenScript_typed_irletrecunparse_comparable_ty:typea.acomparable_ty->Script.node=function|Unit_keytname->Prim(-1,T_unit,[],unparse_type_annottname)|Never_keytname->Prim(-1,T_never,[],unparse_type_annottname)|Int_keytname->Prim(-1,T_int,[],unparse_type_annottname)|Nat_keytname->Prim(-1,T_nat,[],unparse_type_annottname)|Signature_keytname->Prim(-1,T_signature,[],unparse_type_annottname)|String_keytname->Prim(-1,T_string,[],unparse_type_annottname)|Bytes_keytname->Prim(-1,T_bytes,[],unparse_type_annottname)|Mutez_keytname->Prim(-1,T_mutez,[],unparse_type_annottname)|Bool_keytname->Prim(-1,T_bool,[],unparse_type_annottname)|Key_hash_keytname->Prim(-1,T_key_hash,[],unparse_type_annottname)|Key_keytname->Prim(-1,T_key,[],unparse_type_annottname)|Timestamp_keytname->Prim(-1,T_timestamp,[],unparse_type_annottname)|Address_keytname->Prim(-1,T_address,[],unparse_type_annottname)|Chain_id_keytname->Prim(-1,T_chain_id,[],unparse_type_annottname)|Pair_key((l,al),(r,ar),pname)->lettl=add_field_annotalNone(unparse_comparable_tyl)inlettr=add_field_annotarNone(unparse_comparable_tyr)inPrim(-1,T_pair,[tl;tr],unparse_type_annotpname)|Union_key((l,al),(r,ar),tname)->lettl=add_field_annotalNone(unparse_comparable_tyl)inlettr=add_field_annotarNone(unparse_comparable_tyr)inPrim(-1,T_or,[tl;tr],unparse_type_annottname)|Option_key(t,tname)->Prim(-1,T_option,[unparse_comparable_tyt],unparse_type_annottname)letunparse_memo_sizememo_size=letz=Alpha_context.Sapling.Memo_size.unparse_to_zmemo_sizeinInt(-1,z)letrecunparse_ty:typea.aty->Script.node=funty->letreturn(name,args,annot)=Prim(-1,name,args,annot)inmatchtywith|Unit_ttname->return(T_unit,[],unparse_type_annottname)|Int_ttname->return(T_int,[],unparse_type_annottname)|Nat_ttname->return(T_nat,[],unparse_type_annottname)|Signature_ttname->return(T_signature,[],unparse_type_annottname)|String_ttname->return(T_string,[],unparse_type_annottname)|Bytes_ttname->return(T_bytes,[],unparse_type_annottname)|Mutez_ttname->return(T_mutez,[],unparse_type_annottname)|Bool_ttname->return(T_bool,[],unparse_type_annottname)|Key_hash_ttname->return(T_key_hash,[],unparse_type_annottname)|Key_ttname->return(T_key,[],unparse_type_annottname)|Timestamp_ttname->return(T_timestamp,[],unparse_type_annottname)|Address_ttname->return(T_address,[],unparse_type_annottname)|Operation_ttname->return(T_operation,[],unparse_type_annottname)|Chain_id_ttname->return(T_chain_id,[],unparse_type_annottname)|Never_ttname->return(T_never,[],unparse_type_annottname)|Bls12_381_g1_ttname->return(T_bls12_381_g1,[],unparse_type_annottname)|Bls12_381_g2_ttname->return(T_bls12_381_g2,[],unparse_type_annottname)|Bls12_381_fr_ttname->return(T_bls12_381_fr,[],unparse_type_annottname)|Contract_t(ut,tname)->lett=unparse_tyutinreturn(T_contract,[t],unparse_type_annottname)|Pair_t((utl,l_field,l_var),(utr,r_field,r_var),tname)->letannot=unparse_type_annottnameinletutl=unparse_tyutlinlettl=add_field_annotl_fieldl_varutlinletutr=unparse_tyutrinlettr=add_field_annotr_fieldr_varutrinreturn(T_pair,[tl;tr],annot)|Union_t((utl,l_field),(utr,r_field),tname)->letannot=unparse_type_annottnameinletutl=unparse_tyutlinlettl=add_field_annotl_fieldNoneutlinletutr=unparse_tyutrinlettr=add_field_annotr_fieldNoneutrinreturn(T_or,[tl;tr],annot)|Lambda_t(uta,utr,tname)->letta=unparse_tyutainlettr=unparse_tyutrinreturn(T_lambda,[ta;tr],unparse_type_annottname)|Option_t(ut,tname)->letannot=unparse_type_annottnameinletut=unparse_tyutinreturn(T_option,[ut],annot)|List_t(ut,tname)->lett=unparse_tyutinreturn(T_list,[t],unparse_type_annottname)|Ticket_t(ut,tname)->lett=unparse_comparable_tyutinreturn(T_ticket,[t],unparse_type_annottname)|Set_t(ut,tname)->lett=unparse_comparable_tyutinreturn(T_set,[t],unparse_type_annottname)|Map_t(uta,utr,tname)->letta=unparse_comparable_tyutainlettr=unparse_tyutrinreturn(T_map,[ta;tr],unparse_type_annottname)|Big_map_t(uta,utr,tname)->letta=unparse_comparable_tyutainlettr=unparse_tyutrinreturn(T_big_map,[ta;tr],unparse_type_annottname)|Sapling_transaction_t(memo_size,tname)->return(T_sapling_transaction,[unparse_memo_sizememo_size],unparse_type_annottname)|Sapling_state_t(memo_size,tname)->return(T_sapling_state,[unparse_memo_sizememo_size],unparse_type_annottname)endlethelpers_path=RPC_path.(open_root/"helpers"/"scripts")letcontract_root=(RPC_path.(open_root/"context"/"contracts"):RPC_context.tRPC_path.context)letbig_map_root=(RPC_path.(open_root/"context"/"big_maps"):RPC_context.tRPC_path.context)letunparsing_mode_encoding=letopenData_encodinginunion~tag_size:`Uint8[case(Tag0)~title:"Readable"(constant"Readable")(function|Script_ir_translator.Readable->Some()|Script_ir_translator.Optimized|Script_ir_translator.Optimized_legacy->None)(fun()->Script_ir_translator.Readable);case(Tag1)~title:"Optimized"(constant"Optimized")(function|Script_ir_translator.Optimized->Some()|Script_ir_translator.Readable|Script_ir_translator.Optimized_legacy->None)(fun()->Script_ir_translator.Optimized);case(Tag2)~title:"Optimized_legacy"(constant"Optimized_legacy")(function|Script_ir_translator.Optimized_legacy->Some()|Script_ir_translator.Readable|Script_ir_translator.Optimized->None)(fun()->Script_ir_translator.Optimized_legacy);]letrun_code_input_encoding=letopenData_encodinginmerge_objs(obj10(req"script"Script.expr_encoding)(req"storage"Script.expr_encoding)(req"input"Script.expr_encoding)(req"amount"Tez.encoding)(req"balance"Tez.encoding)(req"chain_id"Chain_id.encoding)(opt"source"Contract.encoding)(opt"payer"Contract.encoding)(opt"gas"Gas.Arith.z_integral_encoding)(dft"entrypoint"string"default"))(obj1(req"unparsing_mode"unparsing_mode_encoding))letrun_view_encoding=letopenData_encodinginobj8(req"contract"Contract.encoding)(req"entrypoint"string)(req"input"Script.expr_encoding)(req"chain_id"Chain_id.encoding)(opt"source"Contract.encoding)(opt"payer"Contract.encoding)(opt"gas"Gas.Arith.z_integral_encoding)(req"unparsing_mode"unparsing_mode_encoding)letrun_view=letopenData_encodinginRPC_service.post_service~description:"Simulate a call to a view following the TZIP-4 standard. See \
https://gitlab.com/tzip/tzip/-/blob/master/proposals/tzip-4/tzip-4.md#view-entrypoints."~input:run_view_encoding~output:(obj1(req"data"Script.expr_encoding))~query:RPC_query.emptyRPC_path.(helpers_path/"run_view")letnormalize_data=letopenData_encodinginRPC_service.post_service~description:"Normalizes some data expression using the requested unparsing mode"~input:(obj4(req"data"Script.expr_encoding)(req"type"Script.expr_encoding)(req"unparsing_mode"unparsing_mode_encoding)(opt"legacy"bool))~output:(obj1(req"normalized"Script.expr_encoding))~query:RPC_query.emptyRPC_path.(helpers_path/"normalize_data")letnormalize_script=letopenData_encodinginRPC_service.post_service~description:"Normalizes a Michelson script using the requested unparsing mode"~input:(obj2(req"script"Script.expr_encoding)(req"unparsing_mode"unparsing_mode_encoding))~output:(obj1(req"normalized"Script.expr_encoding))~query:RPC_query.emptyRPC_path.(helpers_path/"normalize_script")letnormalize_type=letopenData_encodinginRPC_service.post_service~description:"Normalizes some Michelson type by expanding `pair a b c` as `pair a \
(pair b c)"~input:(obj1(req"type"Script.expr_encoding))~output:(obj1(req"normalized"Script.expr_encoding))~query:RPC_query.emptyRPC_path.(helpers_path/"normalize_type")letget_storage_normalized=letopenData_encodinginRPC_service.post_service~description:"Access the data of the contract and normalize it using the requested \
unparsing mode."~input:(obj1(req"unparsing_mode"unparsing_mode_encoding))~query:RPC_query.empty~output:(optionScript.expr_encoding)RPC_path.(contract_root/:Contract.rpc_arg/"storage"/"normalized")letget_script_normalized=letopenData_encodinginRPC_service.post_service~description:"Access the script of the contract and normalize it using the \
requested unparsing mode."~input:(obj1(req"unparsing_mode"unparsing_mode_encoding))~query:RPC_query.empty~output:(optionScript.encoding)RPC_path.(contract_root/:Contract.rpc_arg/"script"/"normalized")letrun_code_normalized=letopenData_encodinginRPC_service.post_service~description:"Run a piece of code in the current context, normalize the output \
using the requested unparsing mode."~query:RPC_query.empty~input:run_code_input_encoding~output:(conv(fun(storage,operations,lazy_storage_diff)->(storage,operations,lazy_storage_diff,lazy_storage_diff))(fun(storage,operations,legacy_lazy_storage_diff,lazy_storage_diff)->letlazy_storage_diff=Option.first_somelazy_storage_difflegacy_lazy_storage_diffin(storage,operations,lazy_storage_diff))(obj4(req"storage"Script.expr_encoding)(req"operations"(listAlpha_context.Operation.internal_operation_encoding))(opt"big_map_diff"Lazy_storage.legacy_big_map_diff_encoding)(opt"lazy_storage_diff"Lazy_storage.encoding)))RPC_path.(helpers_path/"run_code"/"normalized")lettrace_encoding=letopenData_encodingindef"scripted.trace"@@list@@obj3(req"location"Script.location_encoding)(req"gas"Gas.encoding)(req"stack"(list(obj2(req"item"Script.expr_encoding)(opt"annot"string))))lettrace_code_normalized=letopenData_encodinginRPC_service.post_service~description:"Run a piece of code in the current context, keeping a trace, \
normalize the output using the requested unparsing mode."~query:RPC_query.empty~input:run_code_input_encoding~output:(conv(fun(storage,operations,trace,lazy_storage_diff)->(storage,operations,trace,lazy_storage_diff,lazy_storage_diff))(fun(storage,operations,trace,legacy_lazy_storage_diff,lazy_storage_diff)->letlazy_storage_diff=Option.first_somelazy_storage_difflegacy_lazy_storage_diffin(storage,operations,trace,lazy_storage_diff))(obj5(req"storage"Script.expr_encoding)(req"operations"(listAlpha_context.Operation.internal_operation_encoding))(req"trace"trace_encoding)(opt"big_map_diff"Lazy_storage.legacy_big_map_diff_encoding)(opt"lazy_storage_diff"Lazy_storage.encoding)))RPC_path.(helpers_path/"trace_code"/"normalized")letbig_map_get_normalized=letopenData_encodinginRPC_service.post_service~description:"Access the value associated with a key in a big map, normalize the \
output using the requested unparsing mode."~query:RPC_query.empty~input:(obj1(req"unparsing_mode"unparsing_mode_encoding))~output:Script.expr_encodingRPC_path.(big_map_root/:Big_map.Id.rpc_arg/:Script_expr_hash.rpc_arg/"normalized")letrpc_services=letpatched_services=ref(RPC_directory.empty:Updater.rpc_contextRPC_directory.t)inletregister0_fullctxtsf=patched_services:=RPC_directory.register!patched_servicess(functxtqi->Services_registration.rpc_initctxt>>=?functxt->fctxtqi)inletregister0sf=register0_fullctxts(fun{context;_}->fcontext)inletregister1_fullctxtsf=patched_services:=RPC_directory.register!patched_servicess(fun(ctxt,arg)qi->Services_registration.rpc_initctxt>>=?functxt->fctxtargqi)inletregister1sf=register1_fullctxts(fun{context;_}x->fcontextx)inlet_register1_noctxtsf=patched_services:=RPC_directory.register!patched_servicess(fun(_,arg)qi->fargqi)inletregister2_fullctxtsf=patched_services:=RPC_directory.register!patched_servicess(fun((ctxt,arg1),arg2)qi->Services_registration.rpc_initctxt>>=?functxt->fctxtarg1arg2qi)inletregister2sf=register2_fullctxts(fun{context;_}a1a2qi->fcontexta1a2qi)inletregister_fieldsf=register1s(functxtcontract()()->Contract.existsctxtcontract>>=?function|true->fctxtcontract|false->raiseNot_found)inlet_register_opt_fieldsf=register_fields(functxta1->fctxta1>|=?functionNone->raiseNot_found|Somev->v)inletoriginate_dummy_contractctxtscriptbalance=letctxt=Contract.init_origination_noncectxtOperation_hash.zeroinLwt.return(Contract.fresh_contract_from_current_noncectxt)>>=?fun(ctxt,dummy_contract)->Contract.originatectxtdummy_contract~balance~delegate:None~script:(script,None)>>=?functxt->return(ctxt,dummy_contract)in(* Extracted and adapted from Contract_services: this function is
not exported and cannot be refactored since it is in the
protocol, and its associated service needs a RPC_context, while
we use an Alpha_context.t *)letscript_entrypoint_typectxtexprentrypoint=letctxt=Gas.set_unlimitedctxtinletlegacy=trueinletopenScript_ir_translatorinLwt.return(Script.force_decode_in_contextctxtexpr>>?fun(expr,_)->(parse_toplevel~legacyexpr>>?fun(arg_type,_,_,root_name)->parse_parameter_tyctxt~legacyarg_type>>?fun(Ex_tyarg_type,_)->Script_ir_translator.find_entrypoint~root_namearg_typeentrypoint)>>?fun(_f,Ex_tyty)->unparse_tyctxtty>|?fun(ty_node,_)->Micheline.strip_locationsty_node)inregister0normalize_data(functxt()(expr,typ,unparsing_mode,legacy)->letopenScript_ir_translatorinletlegacy=Option.value~default:falselegacyinletctxt=Gas.set_unlimitedctxtin(* Unfortunately, Script_ir_translator.parse_any_ty is not exported *)Script_ir_translator.parse_tyctxt~legacy~allow_lazy_storage:true~allow_operation:true~allow_contract:true~allow_ticket:true(Micheline.roottyp)>>?=fun(Ex_tytyp,ctxt)->parse_datactxt~legacy~allow_forged:truetyp(Micheline.rootexpr)>>=?fun(data,ctxt)->Script_ir_translator.unparse_datactxtunparsing_modetypdata>|=?fun(normalized,_ctxt)->Micheline.strip_locationsnormalized);register0normalize_script(functxt()(script,unparsing_mode)->letctxt=Gas.set_unlimitedctxtinScript_ir_translator.unparse_codectxtunparsing_mode(Micheline.rootscript)>|=?fun(normalized,_ctxt)->Micheline.strip_locationsnormalized);register0normalize_type(functxt()typ->letopenScript_ir_translatorinletctxt=Gas.set_unlimitedctxtin(* Unfortunately, Script_ir_translator.parse_any_ty is not exported *)Script_ir_translator.parse_tyctxt~legacy:true~allow_lazy_storage:true~allow_operation:true~allow_contract:true~allow_ticket:true(Micheline.roottyp)>>?=fun(Ex_tytyp,_ctxt)->letnormalized=Unparse_types.unparse_tytypinreturn@@Micheline.strip_locationsnormalized);(* Patched RPC: get_storage *)register1get_storage_normalized(functxtcontract()unparsing_mode->Contract.get_scriptctxtcontract>>=?fun(ctxt,script)->matchscriptwith|None->return_none|Somescript->letctxt=Gas.set_unlimitedctxtinletopenScript_ir_translatorinparse_scriptctxt~legacy:true~allow_forged_in_storage:truescript>>=?fun(Ex_scriptscript,ctxt)->unparse_scriptctxtunparsing_modescript>>=?fun(script,ctxt)->Script.force_decode_in_contextctxtscript.storage>>?=fun(storage,_ctxt)->return_somestorage);(* Patched RPC: get_script *)register1get_script_normalized(functxtcontract()unparsing_mode->Contract.get_scriptctxtcontract>>=?fun(ctxt,script)->matchscriptwith|None->return_none|Somescript->letctxt=Gas.set_unlimitedctxtinletopenScript_ir_translatorinparse_scriptctxt~legacy:true~allow_forged_in_storage:truescript>>=?fun(Ex_scriptscript,ctxt)->unparse_scriptctxtunparsing_modescript>>=?fun(script,_ctxt)->return_somescript);register0run_code_normalized(functxt()((code,storage,parameter,amount,balance,chain_id,source,payer,gas,entrypoint),unparsing_mode)->letstorage=Script.lazy_exprstorageinletcode=Script.lazy_exprcodeinoriginate_dummy_contractctxt{storage;code}balance>>=?fun(ctxt,dummy_contract)->letsource,payer=match(source,payer)with|Somesource,Somepayer->(source,payer)|Somesource,None->(source,source)|None,Somepayer->(payer,payer)|None,None->(dummy_contract,dummy_contract)inletgas=matchgaswith|Somegas->gas|None->Constants.hard_gas_limit_per_operationctxtinletctxt=Gas.set_limitctxtgasinletstep_constants=letopenScript_interpreterin{source;payer;self=dummy_contract;amount;chain_id}inScript_interpreter.executectxtunparsing_modestep_constants~script:{storage;code}~entrypoint~parameter~internal:true>|=?fun{Script_interpreter.storage;operations;lazy_storage_diff;_}->(storage,operations,lazy_storage_diff));register0run_view(functxt()(contract,entrypoint,input,chain_id,source,payer,gas,unparsing_mode)->Contract.get_scriptctxtcontract>>=?fun(ctxt,script_opt)->Option.fold~some:ok~none:(Error_monad.errorView_helpers.Viewed_contract_has_no_script)script_opt>>?=funscript->script_entrypoint_typectxtscript.Script.codeentrypoint>>=?funview_ty->View_helpers.extract_view_output_typeentrypointview_ty>>?=funty->Error_monad.traceView_helpers.View_callback_origination_failed@@originate_dummy_contractctxt(View_helpers.make_viewer_scriptty)Tez.zero>>=?fun(ctxt,viewer_contract)->letsource,payer=match(source,payer)with|Somesource,Somepayer->(source,payer)|Somesource,None->(source,source)|None,Somepayer->(payer,payer)|None,None->(contract,contract)inletgas=Option.value~default:(Constants.hard_gas_limit_per_operationctxt)gasinletctxt=Gas.set_limitctxtgasinletstep_constants=letopenScript_interpreterin{source;payer;self=contract;amount=Tez.zero;chain_id}inletparameter=View_helpers.make_view_parameter(Micheline.rootinput)viewer_contractinScript_interpreter.executectxtunparsing_modestep_constants~script~entrypoint~parameter~internal:true>>=?fun{Script_interpreter.operations;_}->View_helpers.extract_parameter_from_operationsentrypointoperationsviewer_contract>>?=funparameter->Lwt.return(Script_repr.force_decodeparameter>|?fst));register0trace_code_normalized(functxt()((code,storage,parameter,amount,balance,chain_id,source,payer,gas,entrypoint),unparsing_mode)->letmoduleTraced_interpreter=structtypelog_element=|Log:context*Script.location*'a*'aScript_typed_ir.stack_ty->log_elementletunparse_stackctxt(stack,stack_ty)=(* We drop the gas limit as this function is only used for debugging/errors. *)letctxt=Gas.set_unlimitedctxtinletrecunparse_stack:typea.aScript_typed_ir.stack_ty*a->(Script.expr*stringoption)listEnvironment.Error_monad.tzresultLwt.t=function|Empty_t,()->return_nil|Item_t(ty,rest_ty,annot),(v,rest)->Script_ir_translator.unparse_datactxtunparsing_modetyv>>=?fun(data,_ctxt)->unparse_stack(rest_ty,rest)>|=?funrest->letannot=matchScript_ir_annot.unparse_var_annotannotwith|[]->None|[a]->Somea|_->assertfalseinletdata=Micheline.strip_locationsdatain(data,annot)::restinunparse_stack(stack_ty,stack)moduleTrace_logger():Script_interpreter.STEP_LOGGER=structletlog:log_elementlistref=ref[]letlog_interpctxt(descr:(_,_)Script_typed_ir.descr)stack=log:=Log(ctxt,descr.loc,stack,descr.bef)::!logletlog_entry_ctxt_descr_stack=()letlog_exitctxt(descr:(_,_)Script_typed_ir.descr)stack=log:=Log(ctxt,descr.loc,stack,descr.aft)::!logletget_log()=Environment.Error_monad.map_s(fun(Log(ctxt,loc,stack,stack_ty))->Environment.Error_monad.traceCannot_serialize_log_normalized(unparse_stackctxt(stack,stack_ty))>>=?funstack->return(loc,Gas.levelctxt,stack))!log>>=?funres->return(Some(List.revres))endendinletstorage=Script.lazy_exprstorageinletcode=Script.lazy_exprcodeinoriginate_dummy_contractctxt{storage;code}balance>>=?fun(ctxt,dummy_contract)->letsource,payer=match(source,payer)with|Somesource,Somepayer->(source,payer)|Somesource,None->(source,source)|None,Somepayer->(payer,payer)|None,None->(dummy_contract,dummy_contract)inletgas=matchgaswith|Somegas->gas|None->Constants.hard_gas_limit_per_operationctxtinletctxt=Gas.set_limitctxtgasinletstep_constants=letopenScript_interpreterin{source;payer;self=dummy_contract;amount;chain_id}inletmoduleLogger=Traced_interpreter.Trace_logger()inletlogger=(moduleLogger:Script_interpreter.STEP_LOGGER)inScript_interpreter.execute~loggerctxtunparsing_modestep_constants~script:{storage;code}~entrypoint~parameter~internal:true>>=?fun{storage;lazy_storage_diff;operations;_}->Logger.get_log()>|=?funtrace->lettrace=Option.value~default:[]tracein(storage,operations,trace,lazy_storage_diff));register2big_map_get_normalized(functxtidkey()unparsing_mode->letopenScript_ir_translatorinletctxt=Gas.set_unlimitedctxtinBig_map.existsctxtid>>=?fun(ctxt,types)->matchtypeswith|None->raiseNot_found|Some(_,value_type)->(parse_big_map_value_tyctxt~legacy:true(Micheline.rootvalue_type)>>?=fun(Ex_tyvalue_type,ctxt)->Big_map.get_optctxtidkey>>=?fun(_ctxt,value)->matchvaluewith|None->raiseNot_found|Somevalue->parse_datactxt~legacy:true~allow_forged:truevalue_type(Micheline.rootvalue)>>=?fun(value,ctxt)->unparse_datactxtunparsing_modevalue_typevalue>|=?fun(value,_ctxt)->Micheline.strip_locationsvalue));RPC_directory.mergerpc_services!patched_servicesletnormalize_data?legacy~data~ty~unparsing_modectxtblock=RPC_context.make_call0normalize_datactxtblock()(data,ty,unparsing_mode,legacy)letnormalize_scriptctxtblock~script~unparsing_mode=RPC_context.make_call0normalize_scriptctxtblock()(script,unparsing_mode)letnormalize_typectxtblock~ty=RPC_context.make_call0normalize_typectxtblock()tyletget_storage_normalizedctxtblock~contract~unparsing_mode=RPC_context.make_call1get_storage_normalizedctxtblockcontract()unparsing_modeletget_script_normalizedctxtblock~contract~unparsing_mode=RPC_context.make_call1get_script_normalizedctxtblockcontract()unparsing_modeletrun_code_normalized?gas?(entrypoint="default")~script~storage~input~amount~balance~chain_id~source~payer~unparsing_modectxtblock=RPC_context.make_call0run_code_normalizedctxtblock()((script,storage,input,amount,balance,chain_id,source,payer,gas,entrypoint),unparsing_mode)letrun_view?gas~contract~entrypoint~input~chain_id~source~payer~unparsing_modectxtblock=RPC_context.make_call0run_viewctxtblock()(contract,entrypoint,input,chain_id,source,payer,gas,unparsing_mode)lettrace_code_normalized?gas?(entrypoint="default")~script~storage~input~amount~balance~chain_id~source~payer~unparsing_modectxtblock=RPC_context.make_call0trace_code_normalizedctxtblock()((script,storage,input,amount,balance,chain_id,source,payer,gas,entrypoint),unparsing_mode)letbig_map_get_normalizedctxtblockidkey~unparsing_mode=RPC_context.make_call2big_map_get_normalizedctxtblockidkey()unparsing_modeend