123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)(* `Permanent *)typeerror+=Malformed_periodofint64|Invalid_arg|Period_overflowlet()=letopenData_encodingin(* Malformed period *)register_error_kind`Permanent~id:"malformed_period"~title:"Malformed period"~description:"Period is negative."~pp:(funppfperiod->Format.fprintfppf"The given period '%Ld' is negative "period)(obj1(req"malformed_period"int64))(functionMalformed_periodn->Somen|_->None)(funn->Malformed_periodn);(* Invalid arg *)register_error_kind`Permanent~id:"invalid_arg"~title:"Invalid arg"~description:"Negative multiple of periods are not allowed."~pp:(funppf()->Format.fprintfppf"Invalid arg")empty(functionInvalid_arg->Some()|_->None)(fun()->Invalid_arg);lettitle="Period overflow"inregister_error_kind`Permanent~id:"period_overflow"~title~description:"Last operation generated an integer overflow."~pp:(funppf()->Format.fprintfppf"%s"title)empty(functionPeriod_overflow->Some()|_->None)(fun()->Period_overflow)moduletypeINTERNAL=sigtypet=privateint64valcreate:int64->toptionvalzero:tvalone:tvalmult_:t->t->toptionvaladd_:t->t->toptionvalencoding:tData_encoding.tvalrpc_arg:tRPC_arg.argvalpp:Format.formatter->t->unitincludeCompare.Swithtypet:=tend(* Internal module implementing natural numbers using int64. These are different
from usual (wrapping up) unsigned integers in that if one overflows the
representation bounds for int64 through [add] or [mul], a [None] value is
returned *)moduleInternal:INTERNAL=structtypet=Int64.tletencoding=Data_encoding.(with_decoding_guard(funt->ifCompare.Int64.(t>=0L)thenOk()elseError"Positive int64 required")int64)letrpc_arg=RPC_arg.uint63letppppfv=Format.fprintfppf"%Ld"vinclude(Compare.Int64:Compare.Swithtypet:=t)letzero=0Lletone=1Lletcreatet=ift>=zerothenSometelseNone(* The create function is not used in the [mul_] and [add_] below to not add
extra Some | None pattern matching to handle since the overflow checks are
generic and apply as well to negative as positive integers .
To handle overflows, both [add_] and [mult_] return option types. [None] is
returned on detected overflow, [Some value] when everything went well. *)letmult_ab=ifa<>zerothenletres=Int64.mulabinifInt64.divresa<>bthenNoneelseSomereselseSomezeroletadd_ab=letres=Int64.addabinifres<a||res<bthenNoneelseSomeresendincludeInternaltypeperiod=Internal.tletto_seconds(t:Internal.t)=(t:>int64)letof_secondssecs=matchInternal.createsecswith|Somev->okv|None->error(Malformed_periodsecs)letof_seconds_exnt=matchInternal.createtwith|Somet->t|None->invalid_arg"Period.of_seconds_exn"letmultip=matchInternal.create(Int64.of_int32i)with|None->errorInvalid_arg|Someiper->(matchInternal.mult_iperpwith|None->errorPeriod_overflow|Someres->okres)letaddp1p2=matchInternal.add_p1p2with|None->errorPeriod_overflow|Someres->okreslet(+?)=addletone_second=Internal.oneletone_minute=of_seconds_exn60Lletone_hour=of_seconds_exn3600L