123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127(*****************************************************************************)(* *)(* 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=Seed_repr.noncetypenonce=tletencoding=Seed_repr.nonce_encodingtypeerror+=|Too_late_revelation|Too_early_revelation|Previously_revealed_nonce|Inconsistent_noncelet()=register_error_kind`Branch~id:"nonce.too_late_revelation"~title:"Too late nonce revelation"~description:"Nonce revelation happens too late"~pp:(funppf()->Format.fprintfppf"This nonce cannot be revealed anymore.")Data_encoding.unit(functionToo_late_revelation->Some()|_->None)(fun()->Too_late_revelation);register_error_kind`Temporary~id:"nonce.too_early_revelation"~title:"Too early nonce revelation"~description:"Nonce revelation happens before cycle end"~pp:(funppf()->Format.fprintfppf"This nonce should not yet be revealed")Data_encoding.unit(functionToo_early_revelation->Some()|_->None)(fun()->Too_early_revelation);register_error_kind`Branch~id:"nonce.previously_revealed"~title:"Previously revealed nonce"~description:"Duplicated revelation for a nonce."~pp:(funppf()->Format.fprintfppf"This nonce was previously revealed")Data_encoding.unit(functionPreviously_revealed_nonce->Some()|_->None)(fun()->Previously_revealed_nonce);register_error_kind`Branch~id:"nonce.inconsistent"~title:"Inconsistent nonce"~description:"The provided nonce is inconsistent with the committed nonce hash."~pp:(funppf()->Format.fprintfppf"This nonce revelation is invalid (inconsistent with the committed \
hash)")Data_encoding.unit(functionInconsistent_nonce->Some()|_->None)(fun()->Inconsistent_nonce)(* checks that the level of a revelation is not too early or too late wrt to the
current context and that a nonce has not been already revealed for that level *)letget_unrevealedctxt(level:Level_repr.t)=letcur_level=Level_storage.currentctxtinmatchCycle_repr.predcur_level.cyclewith|None->failToo_early_revelation(* no revelations during cycle 0 *)|Somerevealed_cycle->(ifCycle_repr.(revealed_cycle<level.Level_repr.cycle)thenfailToo_early_revelationelseifCycle_repr.(level.Level_repr.cycle<revealed_cycle)thenfailToo_late_revelationelseStorage.Seed.Nonce.getctxtlevel>>=?function|Revealed_->failPreviously_revealed_nonce|Unrevealedstatus->returnstatus)letrecord_hashctxtunrevealed=letlevel=Level_storage.currentctxtinStorage.Seed.Nonce.initctxtlevel(Unrevealedunrevealed)letrevealctxtlevelnonce=get_unrevealedctxtlevel>>=?fununrevealed->error_unless(Seed_repr.check_hashnonceunrevealed.nonce_hash)Inconsistent_nonce>>?=fun()->Storage.Seed.Nonce.updatectxtlevel(Revealednonce)typeunrevealed=Storage.Seed.unrevealed_nonce={nonce_hash:Nonce_hash.t;delegate:Signature.Public_key_hash.t;rewards:Tez_repr.t;fees:Tez_repr.t;}typestatus=Storage.Seed.nonce_status=|Unrevealedofunrevealed|RevealedofSeed_repr.nonceletget=Storage.Seed.Nonce.getletof_bytes=Seed_repr.make_noncelethash=Seed_repr.hashletcheck_hash=Seed_repr.check_hash