Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file sc_rollup_wasm.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 Nomadic Labs <contact@nomadic-labs.com> *)(* 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. *)(* *)(*****************************************************************************)moduleV2_0_0=structopenSc_rollup_reprmodulePS=Sc_rollup_PVM_semmoduletypeP=sigmoduleTree:Context.TREEwithtypekey=stringlistandtypevalue=bytestypetree=Tree.treetypeproofvalproof_encoding:proofData_encoding.tvalproof_before:proof->State_hash.tvalproof_after:proof->State_hash.tvalverify_proof:proof->(tree->(tree*'a)Lwt.t)->(tree*'a)optionLwt.tvalproduce_proof:Tree.t->tree->(tree->(tree*'a)Lwt.t)->(proof*'a)optionLwt.tendmoduletypeS=sigincludeSc_rollup_PVM_sem.Svalname:stringvalparse_boot_sector:string->stringoptionvalpp_boot_sector:Format.formatter->string->unit(** [get_tick state] gets the total tick counter for the given PVM state. *)valget_tick:state->Sc_rollup_tick_repr.tLwt.t(** PVM status *)typestatus=Computing|WaitingForInputMessage(** [get_status state] gives you the current execution status for the PVM. *)valget_status:state->statusLwt.tend(* TODO: https://gitlab.com/tezos/tezos/-/issues/3091
The tree proof contains enough information to derive given and requested.
Get rid of the duplication by writing the projection functions and
removing the [given] and [requested] fields.
*)type'aproof={tree_proof:'a;given:PS.inputoption;requested:PS.input_request;}letproof_encodinge=letopenData_encodinginconv(fun{tree_proof;given;requested}->(tree_proof,given,requested))(fun(tree_proof,given,requested)->{tree_proof;given;requested})(obj3(req"tree_proof"e)(req"given"(optionPS.input_encoding))(req"requested"PS.input_request_encoding))moduleMake(Context:P):Swithtypecontext=Context.Tree.tandtypestate=Context.treeandtypeproof=Context.proofproof=structmoduleTree=Context.Treetypecontext=Context.Tree.ttypehash=State_hash.ttypenonrecproof=Context.proofproofletproof_input_givenp=p.givenletproof_input_requestedp=p.requestedletproof_encoding=proof_encodingContext.proof_encodingletproof_start_statep=Context.proof_beforep.tree_proofletproof_stop_statep=match(p.given,p.requested)with|None,PS.No_input_required->Some(Context.proof_afterp.tree_proof)|None,_->None|_->Some(Context.proof_afterp.tree_proof)letname="wasm_2_0_0"letparse_boot_sectors=Somesletpp_boot_sectorfmts=Format.fprintffmt"%s"stypetree=Tree.treetypestatus=Computing|WaitingForInputMessagemoduleState=structtypestate=treemoduleMonad:sigtype'atvalrun:'at->state->(state*'a)Lwt.tvalreturn:'a->'atmoduleSyntax:sigval(let*):'at->('a->'bt)->'btendvalget:treetvalset:tree->unittvallift:'aLwt.t->'atend=structtype'at=state->(state*'a)Lwt.tletreturnxstate=Lwt.return(state,x)letbindmfstate=letopenLwt_syntaxinlet*state,res=mstateinfresstatemoduleSyntax=structlet(let*)=bindendletrunmstate=mstateletgets=Lwt.return(s,s)letsets_=Lwt.return(s,())letliftms=Lwt.map(funr->(s,r))mendendmoduleWASM_machine=Wasm_2_0_0.Make(Tree)openStatetypestate=State.stateopenMonadletinitial_statectxt_boot_sector=letopenLwt_syntaxinletstate=Tree.emptyctxtinlet*state=Tree.addstate["wasm-version"](Bytes.of_string"2.0.0")inLwt.returnstateletstate_hashstate=letm=Context_hash.to_bytes@@Tree.hashstate|>funh->return@@State_hash.hash_bytes[h]inletopenLwt_syntaxinlet*state=Monad.runmstateinmatchstatewith_,hash->returnhashletresult_ofmstate=letopenLwt_syntaxinlet*_,v=runmstateinreturnvletstate_ofmstate=letopenLwt_syntaxinlet*s,_=runmstateinreturnsletget_tick:Sc_rollup_tick_repr.tMonad.t=letopenMonad.Syntaxinlet*s=getinlet*info=lift(WASM_machine.get_infos)inreturn@@Sc_rollup_tick_repr.of_zinfo.current_tickletget_tick:state->Sc_rollup_tick_repr.tLwt.t=result_ofget_tickletget_status:statusMonad.t=letopenMonad.Syntaxinlet*s=getinlet*info=lift(WASM_machine.get_infos)inreturn@@matchinfo.input_requestwith|No_input_required->Computing|Input_required->WaitingForInputMessageletget_last_message_read:_Monad.t=letopenMonad.Syntaxinlet*s=getinlet*info=lift(WASM_machine.get_infos)inreturn@@matchinfo.last_input_readwith|Some{inbox_level;message_counter}->letinbox_level=Raw_level_repr.of_int32_non_negativeinbox_levelinSome(inbox_level,message_counter)|_->Noneletis_input_state=letopenMonad.Syntaxinlet*status=get_statusinmatchstatuswith|WaitingForInputMessage->(let*last_read=get_last_message_readinmatchlast_readwith|Some(level,n)->return(PS.First_after(level,n))|None->returnPS.Initial)|Computing->returnPS.No_input_requiredletis_input_state=result_ofis_input_stateletget_status:state->statusLwt.t=result_ofget_statusletset_input_stateinput=letopenPSinlet{inbox_level;message_counter;payload}=inputinletopenMonad.Syntaxinlet*s=getinlet*s=lift(WASM_machine.set_input_step{inbox_level=Raw_level_repr.to_int32_non_negativeinbox_level;message_counter;}payloads)insetsletset_inputinput=state_of@@set_input_stateinputleteval_step=letopenMonad.Syntaxinlet*s=getinlet*s=lift(WASM_machine.compute_steps)insetsletevalstate=state_ofeval_stepstateletstep_transitioninput_givenstate=letopenLwt_syntaxinlet*request=is_input_statestateinlet*state=matchrequestwith|PS.No_input_required->evalstate|_->(matchinput_givenwith|Someinput->set_inputinputstate|None->returnstate)inreturn(state,request)letverify_proofproof=letopenLwt_syntaxinlet*result=Context.verify_proofproof.tree_proof(step_transitionproof.given)inmatchresultwith|None->returnfalse|Some(_,request)->return(PS.input_request_equalrequestproof.requested)typeerror+=WASM_proof_production_failedletproduce_proofcontextinput_givenstate=letopenLwt_result_syntaxinlet*!result=Context.produce_proofcontextstate(step_transitioninput_given)inmatchresultwith|Some(tree_proof,requested)->return{tree_proof;given=input_given;requested}|None->failWASM_proof_production_failedtypeoutput_proof={output_proof:Context.proof;output_proof_state:hash;output_proof_output:PS.output;}letoutput_proof_encoding=letopenData_encodinginconv(fun{output_proof;output_proof_state;output_proof_output}->(output_proof,output_proof_state,output_proof_output))(fun(output_proof,output_proof_state,output_proof_output)->{output_proof;output_proof_state;output_proof_output})(obj3(req"output_proof"Context.proof_encoding)(req"output_proof_state"State_hash.encoding)(req"output_proof_output"PS.output_encoding))letoutput_of_output_proofs=s.output_proof_outputletstate_of_output_proofs=s.output_proof_statelethas_output:PS.output->boolMonad.t=function|{outbox_level;message_index;message}->letopenMonad.Syntaxinlet*s=getinlet*result=lift(WASM_machine.get_output{outbox_level=Raw_level_repr.to_int32_non_negativeoutbox_level;message_index;}s)inletmessage_encoded=Data_encoding.Binary.to_string_exnSc_rollup_outbox_message_repr.encodingmessageinreturn@@Compare.String.(result=message_encoded)letverify_output_proofp=letopenLwt_syntaxinlettransition=run@@has_outputp.output_proof_outputinlet*result=Context.verify_proofp.output_prooftransitioninmatchresultwithNone->returnfalse|Some_->returntruetypeerror+=Wasm_output_proof_production_failedtypeerror+=Wasm_invalid_claim_about_outboxletproduce_output_proofcontextstateoutput_proof_output=letopenLwt_result_syntaxinlet*!output_proof_state=state_hashstateinlet*!result=Context.produce_proofcontextstate@@run@@has_outputoutput_proof_outputinmatchresultwith|Some(output_proof,true)->return{output_proof;output_proof_state;output_proof_output}|Some(_,false)->failWasm_invalid_claim_about_outbox|None->failWasm_output_proof_production_failedendmoduleProtocolImplementation=Make(structmoduleTree=structincludeContext.Treetypetree=Context.treetypet=Context.ttypekey=stringlisttypevalue=bytesendtypetree=Context.treetypeproof=Context.Proof.treeContext.Proof.tletverify_proofpf=Lwt.mapResult.to_option(Context.verify_tree_proofpf)letproduce_proof_context_state_f=(* Can't produce proof without full context*)Lwt.returnNoneletkinded_hash_to_state_hash=function|`Valuehash|`Nodehash->State_hash.hash_bytes[Context_hash.to_byteshash]letproof_beforeproof=kinded_hash_to_state_hashproof.Context.Proof.beforeletproof_afterproof=kinded_hash_to_state_hashproof.Context.Proof.afterletproof_encoding=Context.Proof_encoding.V1.Tree32.tree_proof_encodingend)end