123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 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. *)(* *)(*****************************************************************************)typet={pvm_step:Sc_rollups.wrapped_proof;inbox:Sc_rollup_inbox_repr.Proof.toption;}letencoding=letopenData_encodinginconv(fun{pvm_step;inbox}->(pvm_step,inbox))(fun(pvm_step,inbox)->{pvm_step;inbox})(obj2(req"pvm_step"Sc_rollups.wrapped_proof_encoding)(req"inbox"(optionSc_rollup_inbox_repr.Proof.encoding)))letppppf_=Format.fprintfppf"Refutation game proof"letstartproof=let(moduleP)=Sc_rollups.wrapped_proof_moduleproof.pvm_stepinP.proof_start_stateP.proofletstopproof=let(moduleP)=Sc_rollups.wrapped_proof_moduleproof.pvm_stepinP.proof_stop_stateP.proof(* This takes an [input] and checks if it is at or above the given level.
It returns [None] if this is the case.
We use this to check that the PVM proof is obeying [commit_level]
correctly---if the message obtained from the inbox proof is at or
above [commit_level] the [input_given] in the PVM proof should be
[None]. *)letcut_at_levellevelinput=letinput_level=Sc_rollup_PVM_sem.(input.inbox_level)inifRaw_level_repr.(level<=input_level)thenNoneelseSomeinputtypeerror+=Sc_rollup_proof_checkofstringletproof_errorreason=letopenLwt_result_syntaxinfail(Sc_rollup_proof_checkreason)letcheckpreason=letopenLwt_result_syntaxinifpthenreturn()elseproof_errorreasonletvalidsnapshotcommit_level~pvm_nameproof=let(moduleP)=Sc_rollups.wrapped_proof_moduleproof.pvm_stepinletopenLwt_result_syntaxinlet*_=check(String.equalP.namepvm_name)"Incorrect PVM kind"inletinput_requested=P.proof_input_requestedP.proofinletinput_given=P.proof_input_givenP.proofinlet*input=match(input_requested,proof.inbox)with|Sc_rollup_PVM_sem.No_input_required,None->returnNone|Sc_rollup_PVM_sem.Initial,Someinbox_proof->Sc_rollup_inbox_repr.Proof.valid{inbox_level=Raw_level_repr.root;message_counter=Z.zero}snapshotinbox_proof|Sc_rollup_PVM_sem.First_after(level,counter),Someinbox_proof->Sc_rollup_inbox_repr.Proof.valid{inbox_level=level;message_counter=Z.succcounter}snapshotinbox_proof|_->proof_error(Format.asprintf"input_requested is %a, inbox proof is %a"Sc_rollup_PVM_sem.pp_input_requestinput_requested(Format.pp_print_optionSc_rollup_inbox_repr.Proof.pp)proof.inbox)inlet*_=check(Option.equalSc_rollup_PVM_sem.input_equal(Option.bindinput(cut_at_levelcommit_level))input_given)"Input given is not what inbox proof expects"inLwt.mapResult.ok(P.verify_proofP.proof)moduletypePVM_with_context_and_state=sigincludeSc_rollups.PVM.Svalcontext:contextvalstate:stateendtypeerror+=Proof_cannot_be_wrappedletproducepvm_and_stateinboxcommit_level=letopenLwt_result_syntaxinlet(moduleP:PVM_with_context_and_state)=pvm_and_stateinlet*!request=P.is_input_stateP.stateinlet*inbox,input_given=matchrequestwith|Sc_rollup_PVM_sem.No_input_required->return(None,None)|Sc_rollup_PVM_sem.Initial->let*p,i=Sc_rollup_inbox_repr.Proof.produce_proofinbox(Raw_level_repr.root,Z.zero)inreturn(Somep,i)|Sc_rollup_PVM_sem.First_after(l,n)->let*p,i=Sc_rollup_inbox_repr.Proof.produce_proofinbox(l,n)inreturn(Somep,i)inletinput_given=Option.bindinput_given(cut_at_levelcommit_level)inlet*pvm_step_proof=P.produce_proofP.contextinput_givenP.stateinletmoduleP_with_proof=structincludePletproof=pvm_step_proofendinmatchSc_rollups.wrap_proof(moduleP_with_proof)with|Somepvm_step->return{pvm_step;inbox}|None->failProof_cannot_be_wrapped