Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file sc_rollup_wasm.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478(*****************************************************************************)(* *)(* 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=struct(*
This is the state hash of reference that both the prover of the
node and the verifier of the protocol {!Protocol_implementation}
have to agree on (if they do, it means they are using the same
tree structure).
We have to hard-code this value because the Wasm PVM uses Irmin as
its Merkle proof verification backend, and the economic protocol
cannot create an empty Irmin context. Such a context is required to
create an empty tree, itself required to create the initial state of
the Wasm PVM.
Utlimately, the value of this constant is decided by the prover of
reference (the only need is for it to be compatible with
{!Protocol_implementation}.)
Its value is the result of the following snippet
{|
let*! state = Prover.initial_state context in
Prover.state_hash state
|}
*)letreference_initial_state_hash=Sc_rollup_repr.State_hash.of_b58check_exn"scs11pDQTn37TBnWgQAiCPdMAcQPiXARjg9ZZVmLx26sZwxeSxovE5"openSc_rollup_reprmodulePS=Sc_rollup_PVM_sigmoduletypeTreeS=Context.TREEwithtypekey=stringlistandtypevalue=bytesmoduletypeMake_wasm=moduletypeofWasm_2_0_0.MakemoduletypeP=sigmoduleTree:TreeStypetree=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_sig.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|Waiting_for_input_message(** [get_status state] gives you the current execution status for the PVM. *)valget_status:state->statusLwt.tvalget_outbox:state->Sc_rollup_PVM_sig.outputlistLwt.tend(* [Make (Make_backend) (Context)] creates a PVM.
The Make_backend is a functor that creates the backend of the PVM.
The Conext provides the tree and the proof types.
*)moduleMake(Make_backend:Make_wasm)(Context:P):Swithtypecontext=Context.Tree.tandtypestate=Context.treeandtypeproof=Context.proof=structmoduleTree=Context.Treetypecontext=Context.Tree.ttypehash=State_hash.ttypeproof=Context.proofletproof_encoding=Context.proof_encodingletproof_start_stateproof=Context.proof_beforeproofletproof_stop_stateproof=Context.proof_afterproofletname="wasm_2_0_0"letparse_boot_sectors=Hex.to_string@@`Hexsletpp_boot_sectorfmts=Format.fprintffmt"%s"stypetree=Tree.treetypestatus=Computing|Waiting_for_input_messagemoduleState=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))mendendtypestate=State.statemoduleWASM_machine=Make_backend(Tree)openStateletpp_state=Lwt.return@@funfmt()->Format.pp_print_stringfmt"<wasm-state>"openMonadletinitial_statectxt=letopenLwt_syntaxinletstate=Tree.emptyctxtinlet*state=Tree.addstate["wasm-version"](Bytes.of_string"2.0.0")inLwt.returnstateletinstall_boot_sectorstateboot_sector=Tree.addstate["boot-sector"]Data_encoding.(Binary.to_bytes_exnstringboot_sector)letstate_hashstate=letcontext_hash=Tree.hashstateinLwt.return@@State_hash.context_hash_to_state_hashcontext_hashletresult_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->Waiting_for_input_messageletget_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|Waiting_for_input_message->(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_statusletget_outbox_state=(* FIXME: https://gitlab.com/tezos/tezos/-/issues/3790 *)letopenLwt_syntaxinreturn[]letset_input_stateinput=matchinputwith|PS.Inbox_messageinput->letopenPSinletopenMonad.Syntaxinlet{inbox_level;message_counter;payload}=inputinlet*s=getinlet*s=lift(WASM_machine.set_input_step{inbox_level=Raw_level_repr.to_int32_non_negativeinbox_level;message_counter;}(payload:>string)s)insets|PS.Reveal_->(* TODO: https://gitlab.com/tezos/tezos/-/issues/3754
The WASM PVM does not produce [Needs_reveal] input
requests. Thus, no [set_input_state] should transmit a
[Reveal_revelation].
*)assertfalseletset_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)typeerror+=WASM_proof_verification_failedletverify_proofinput_givenproof=letopenLwt_tzresult_syntaxinlet*!result=Context.verify_proofproof(step_transitioninput_given)inmatchresultwith|None->failWASM_proof_verification_failed|Some(_state,request)->returnrequesttypeerror+=WASM_proof_production_failedletproduce_proofcontextinput_givenstate=letopenLwt_tzresult_syntaxinlet*!result=Context.produce_proofcontextstate(step_transitioninput_given)inmatchresultwith|Some(tree_proof,_requested)->returntree_proof|None->failWASM_proof_production_failedletverify_origination_proofproofboot_sector=letopenLwt_syntaxinletbefore=Context.proof_beforeproofinifState_hash.(before<>reference_initial_state_hash)thenreturnfalseelselet*result=Context.verify_proofproof(funstate->let*state=install_boot_sectorstateboot_sectorinreturn(state,()))inmatchresultwithNone->returnfalse|Some(_,())->returntrueletproduce_origination_proofcontextboot_sector=letopenLwt_tzresult_syntaxinlet*!state=initial_statecontextinlet*!result=Context.produce_proofcontextstate(funstate->letopenLwt_syntaxinlet*state=install_boot_sectorstateboot_sectorinreturn(state,()))inmatchresultwith|Some(tree_proof,())->returntree_proof|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_failedmoduleInternal_for_tests=structletinsert_failurestate=letaddn=Tree.addstate["failures";string_of_intn]Bytes.emptyinletopenLwt_syntaxinlet*n=Tree.lengthstate["failures"]inaddnendendmoduleProtocol_implementation=Make(Wasm_2_0_0.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.context_hash_to_state_hashhashletproof_beforeproof=kinded_hash_to_state_hashproof.Context.Proof.beforeletproof_afterproof=kinded_hash_to_state_hashproof.Context.Proof.afterletproof_encoding=Context.Proof_encoding.V2.Tree32.tree_proof_encodingend)end