123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2023 Nomadic Labs <contact@nomadic-labs.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. *)(* *)(*****************************************************************************)(* Associative list acting like a map.
Invariants:
- the list is sorted by keys,
- not duplicated keys. *)typet=(Cycle_repr.t*Deposits_repr.t)listletreccheck_well_formed=function|[]->Result_syntax.return_unit|(c1,_)::(c2,_)::_whenCycle_repr.(c2<=c1)->Error"Malformed unstaked frozen deposits"|_::tl->check_well_formedtlletid_check_well_formedl=letopenResult_syntaxinlet+()=check_well_formedlinl(* A version of {!t} in which all cycles older than [unslashable_cycle] are
squashed together using {!Deposits_repr.(++?)}. *)typesquashed={unslashable_cycle:Cycle_repr.toption;t:t}letempty~unslashable_cycle={unslashable_cycle;t=[]}letencoding=letopenData_encodinginconv_with_guard(funl->l)id_check_well_formed(list(tup2Cycle_repr.encodingDeposits_repr.encoding))letsquash_unslashable~unslashable_cyclet=letopenResult_syntaxinmatch(unslashable_cycle,t)with|Someunslashable_cycle',(c,unslashable)::tlwhenCycle_repr.(c<=unslashable_cycle')->letrecauxunslashable=function|(c,d)::tlwhenCycle_repr.(c<=unslashable_cycle')->let*unslashable=Deposits_repr.(unslashable++?d)inauxunslashabletl|slashable->return{unslashable_cycle;t=(unslashable_cycle',unslashable)::slashable;}inauxunslashabletl|_->return{unslashable_cycle;t}letnormalize_cyclecycle~unslashable_cycle=matchunslashable_cyclewith|None->cycle|Someunslashable_cycle->Cycle_repr.maxcycleunslashable_cycleletgetcycle{unslashable_cycle;t}=letnormalized_cycle=normalize_cyclecycle~unslashable_cycleinList.assoc~equal:Cycle_repr.(=)normalized_cyclet|>Option.value~default:Deposits_repr.zero(* not tail-rec *)letrecupdate_t~f~normalized_cyclel=letopenResult_syntaxinmatchlwith|(c,d)::tlwhenCycle_repr.(c=normalized_cycle)->let+d=fdin(c,d)::tl|((c,_)ashd)::tlwhenCycle_repr.(c<normalized_cycle)->let+tl=update_t~f~normalized_cycletlinhd::tl|_->let+d=fDeposits_repr.zeroin(normalized_cycle,d)::lletupdate~fcycle{unslashable_cycle;t}=letopenResult_syntaxinletnormalized_cycle=normalize_cyclecycle~unslashable_cycleinlet+t=update_t~f~normalized_cycletin{unslashable_cycle;t}