123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.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. *)(* *)(*****************************************************************************)typet=int32typeraw_level=tinclude(Compare.Int32:Compare.Swithtypet:=t)moduleSet=Set.Make(Compare.Int32)moduleMap=Map.Make(Compare.Int32)letppppflevel=Format.fprintfppf"%ld"levelletrpc_arg=letconstructraw_level=Int32.to_stringraw_levelinletdestructstr=Int32.of_string_optstr|>Option.to_result~none:"Cannot parse level"inRPC_arg.make~descr:"A level integer"~name:"block_level"~construct~destruct()letroot=0lletsucc=Int32.succletaddli=assert(Compare.Int.(i>=0));Int32.addl(Int32.of_inti)letsubli=assert(Compare.Int.(i>=0));letres=Int32.subl(Int32.of_inti)inifCompare.Int32.(res>=0l)thenSomereselseNoneletpredl=ifl=0lthenNoneelseSome(Int32.predl)letpred_dontreturnzerol=ifl<=1lthenNoneelseSome(Int32.predl)letdiff=Int32.subletto_int32l=lletto_int32_non_negativel=matchBounded.Non_negative_int32.of_valuelwith|Somex->x|_->assertfalse(* invariant: raw_levels are non-negative *)typeerror+=Unexpected_levelofInt32.t(* `Permanent *)let()=register_error_kind`Permanent~id:"unexpected_level"~title:"Unexpected level"~description:"Level must be non-negative."~pp:(funppfl->Format.fprintfppf"The level is %s but should be non-negative."(Int32.to_stringl))Data_encoding.(obj1(req"level"int32))(functionUnexpected_levell->Somel|_->None)(funl->Unexpected_levell)letof_int32l=ifCompare.Int32.(l>=0l)thenoklelseerror(Unexpected_levell)letof_int32_exnl=matchof_int32lwith|Okl->l|Error_->invalid_arg"Level_repr.of_int32"letof_int32_non_negativel=matchof_int32(Bounded.Non_negative_int32.to_valuel)with|Okl->l|Error_->assertfalse(* invariant: raw_levels are non-negative *)letencoding=Data_encoding.conv_with_guard~schema:Data_encoding.positive_int32_schemato_int32(funl->matchof_int32lwith|Okl->Okl|Error_->Error"Level_repr.of_int32")Data_encoding.int32moduleIndex=structtypet=raw_levelletpath_length=1letto_pathlevell=Int32.to_stringlevel::lletof_path=function[s]->Int32.of_string_opts|_->Noneletrpc_arg=rpc_argletencoding=encodingletcompare=compareend