123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com> *)(* Copyright (c) 2022 Oxhead Alpha <info@oxheadalpha.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. *)(* *)(*****************************************************************************)openTx_rollup_errors_repropenAlpha_context(* {{Note}} This model should be part of [Tx_rollup_gas].
Unfortunately, this is not possible, because this module is defined
on top of [Alpha_context], while [Tx_rollup_gas] is defined on top
of [Raw_context]. *)letverify_proof_modelmessage_sizeproof_size=letopenSaturation_reprin(* The cost of verifiying the proof depends bilinearly on the size
of the message (that is expected to capture the algoritmic
complexity of computation to make) and the size of the proof
(that is expected to capture the overhead of the storage). *)letproof_size_coeff=safe_int124inletmessage_size_coeff=safe_int8_416inlet(*)=mulinlet(+)=addin(proof_size_coeff*safe_intproof_size)+(message_size_coeff*safe_intmessage_size)letconsume_verify_proof_costctxt~message_size~proof_size=letmax_proof_size=Alpha_context.Constants.tx_rollup_rejection_max_proof_sizectxtin(* We are interested in having a safe over-approximation of the
overhead of the proof interpretation. We have trained the model
on data coming from contexts of various “size” (i.e., number of
leafs), but there is an edge case when it comes to proof
verification that is hard to consider correctly: when the context
is empty, the size is ridiculously small, no matter how many
transactions are executed.
As a safety net, we systematically compute a gas cost as if the
proof is at least the big enough to declare the message as
invalid (using the [tx_rollup_rejection_max_proof_size]
parametric constant). *)Gas.consumectxt@@verify_proof_modelmessage_size(Compare.Int.maxproof_sizemax_proof_size)moduleVerifier_storage:Tx_rollup_l2_storage_sig.STORAGEwithtypet=Context.treeandtype'am=('a,error)resultLwt.t=structtypet=Context.treetype'am=('a,error)resultLwt.tmoduleSyntax=structlet(let*)=(>>=?)let(let+)=(>|=?)letreturn=returnletfaile=Lwt.return(Errore)letcatch(m:'am)kh=m>>=functionOkx->kx|Errore->heletlist_fold_left_m=List.fold_left_esendletpathk=[Bytes.to_stringk]letgetstorekey=Context.Tree.findstore(pathkey)>>=returnletsetstorekeyvalue=Context.Tree.addstore(pathkey)value>>=returnletremovestorekey=Context.Tree.removestore(pathkey)>>=returnendmoduleVerifier_context=Tx_rollup_l2_context.Make(Verifier_storage)moduleVerifier_apply=Tx_rollup_l2_apply.Make(Verifier_context)lethash_message_resultctxtafterwithdraw=Tx_rollup_hash.message_resultctxt{context_hash=after;withdraw_list_hash=withdraw}(** [after_hash_when_proof_failed before] produces the
{!Alpha_context.Tx_rollup_message_result_hash} expected if a proof failed.
That is, the after hash is the same as [before] and it produced zero
withdrawals. *)letafter_hash_when_proof_failedctxtbefore=hash_message_resultctxtbeforeTx_rollup_withdraw_list_hash.emptyletverify_l2_proofproofparametersmessage=Context.verify_stream_proofproof(funtree->Verifier_apply.apply_messagetreeparametersmessage>>=function|Ok(tree,(_,withdrawals))->Lwt.return(tree,withdrawals)|Error_->Lwt.return(tree,[]))(** [compute_proof_after_hash ~max_proof_size agreed proof message] computes the
after hash expected while verifying [proof] on [message] starting from
[agreed].
Note that if the proof is incorrect this function fails and the commit
can not be rejected. *)letcompute_proof_after_hash~proof_length~max_proof_sizectxtparametersagreed(proof:Tx_rollup_l2_proof.t)message=letmessage_length=Data_encoding.Binary.lengthTx_rollup_message.encodingmessagein(* When considering “proof large enough to make a batch invalid,
even if truncated”, we actually need to take into consideration
the size of the message.
[max_proof_size] is the upper bound, but we need to make room for
the message itself. So the real limit for the proof size is
reduced to that end. This way, we save a bit of TPS compared to
just having a lower [max_proof_size] constant. *)letmax_proof_size=max_proof_size-message_lengthinletproof_is_too_long=Compare.Int.(proof_length>max_proof_size)inletbefore=matchproof.beforewith`Nodex->x|`Valuex->xinletagreed_is_correct=Context_hash.(before=agreed)infail_unlessagreed_is_correct(Proof_invalid_before{provided=before;agreed})>>=?fun()->consume_verify_proof_costctxt~message_size:message_length~proof_size:proof_length>>?=functxt->verify_l2_proofproofparametersmessage>>=funres->matchreswith|(Ok_|Error(`Stream_too_short_))whenproof_is_too_long->(* If the proof is larger than [max_proof_size] we care about 2 cases:
- The proof verification succedeed but should not be considered valid
since it is larger than the size limit
- The proof verification failed because it was truncated but was
already larger than the size limit
In those two cases, the expected after hash is
[after_hash_when_proof_failed] because the correct commitment is
"we were not able to apply this message, so after is the same
as before"
*)after_hash_when_proof_failedctxtagreed>>?=funres->returnres|Ok(tree,withdrawals)->(* The proof is small enough, we compare the computed hash with the
committed one *)lettree_hash=Context.Tree.hashtreeinTx_rollup_hash.withdraw_listctxtwithdrawals>>?=fun(ctxt,withdrawals)->hash_message_resultctxttree_hashwithdrawals>>?=funres->returnres|Error_->(* Finally, the proof verification leads to an internal Irmin error *)tzfailProof_failed_to_rejectletverify_proofctxtparametersmessageproof~proof_length~(agreed:Tx_rollup_message_result.t)~rejected~max_proof_size=compute_proof_after_hashctxtparametersagreed.context_hash~proof_length~max_proof_sizeproofmessage>>=?fun(ctxt,computed_result)->ifAlpha_context.Tx_rollup_message_result_hash.(computed_result<>rejected)thenreturnctxtelsetzfailProof_produced_rejected_statemoduleInternal_for_tests=structletverify_l2_proof=verify_l2_proofend