Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file baking.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)openAlpha_contextopenMisctypeerror+=Invalid_fitness_gapofint64*int64(* `Permanent *)typeerror+=Timestamp_too_earlyofTimestamp.t*Timestamp.t(* `Permanent *)typeerror+=Unexpected_endorsement(* `Permanent *)typeerror+=Invalid_block_signatureofBlock_hash.t*Signature.Public_key_hash.t(* `Permanent *)typeerror+=Invalid_signature(* `Permanent *)typeerror+=Invalid_stamp(* `Permanent *)let()=register_error_kind`Permanent~id:"baking.timestamp_too_early"~title:"Block forged too early"~description:"The block timestamp is before the first slot \
for this baker at this level"~pp:(funppf(r,p)->Format.fprintfppf"Block forged too early (%a is before %a)"Time.pp_humpTime.pp_humr)Data_encoding.(obj2(req"minimum"Time.encoding)(req"provided"Time.encoding))(functionTimestamp_too_early(r,p)->Some(r,p)|_->None)(fun(r,p)->Timestamp_too_early(r,p));register_error_kind`Permanent~id:"baking.invalid_fitness_gap"~title:"Invalid fitness gap"~description:"The gap of fitness is out of bounds"~pp:(funppf(m,g)->Format.fprintfppf"The gap of fitness %Ld is not between 0 and %Ld"gm)Data_encoding.(obj2(req"maximum"int64)(req"provided"int64))(functionInvalid_fitness_gap(m,g)->Some(m,g)|_->None)(fun(m,g)->Invalid_fitness_gap(m,g));register_error_kind`Permanent~id:"baking.invalid_block_signature"~title:"Invalid block signature"~description:"A block was not signed with the expected private key."~pp:(funppf(block,pkh)->Format.fprintfppf"Invalid signature for block %a. Expected: %a."Block_hash.pp_shortblockSignature.Public_key_hash.pp_shortpkh)Data_encoding.(obj2(req"block"Block_hash.encoding)(req"expected"Signature.Public_key_hash.encoding))(functionInvalid_block_signature(block,pkh)->Some(block,pkh)|_->None)(fun(block,pkh)->Invalid_block_signature(block,pkh));register_error_kind`Permanent~id:"baking.invalid_signature"~title:"Invalid block signature"~description:"The block's signature is invalid"~pp:(funppf()->Format.fprintfppf"Invalid block signature")Data_encoding.empty(functionInvalid_signature->Some()|_->None)(fun()->Invalid_signature);register_error_kind`Permanent~id:"baking.insufficient_proof_of_work"~title:"Insufficient block proof-of-work stamp"~description:"The block's proof-of-work stamp is insufficient"~pp:(funppf()->Format.fprintfppf"Insufficient proof-of-work stamp")Data_encoding.empty(functionInvalid_stamp->Some()|_->None)(fun()->Invalid_stamp);register_error_kind`Permanent~id:"baking.unexpected_endorsement"~title:"Endorsement from unexpected delegate"~description:"The operation is signed by a delegate without endorsement rights."~pp:(funppf()->Format.fprintfppf"The endorsement is signed by a delegate without endorsement rights.")Data_encoding.unit(functionUnexpected_endorsement->Some()|_->None)(fun()->Unexpected_endorsement)letminimal_timecprioritypred_timestamp=letpriority=Int32.of_intpriorityinletreccumsum_time_between_blocksaccdurationsp=ifCompare.Int32.(<=)p0lthenokaccelsematchdurationswith|[]->cumsum_time_between_blocksacc[Period.one_minute]p|[last]->Period.multplast>>?funperiod->Timestamp.(acc+?period)|first::durations->Timestamp.(acc+?first)>>?funacc->letp=Int32.predpincumsum_time_between_blocksaccdurationspinLwt.return(cumsum_time_between_blockspred_timestamp(Constants.time_between_blocksc)(Int32.succpriority))letearlier_predecessor_timestampctxtlevel=letcurrent=Level.currentctxtinletcurrent_timestamp=Timestamp.currentctxtinletgap=Level.difflevelcurrentinletstep=List.hd(Constants.time_between_blocksctxt)inifCompare.Int32.(gap<1l)thenfailwith"Baking.earlier_block_timestamp: past block."elseLwt.return(Period.mult(Int32.predgap)step)>>=?fundelay->Lwt.returnTimestamp.(current_timestamp+?delay)>>=?funresult->returnresultletcheck_timestampcprioritypred_timestamp=minimal_timecprioritypred_timestamp>>=?funminimal_time->lettimestamp=Alpha_context.Timestamp.currentcinfail_unlessTimestamp.(minimal_time<=timestamp)(Timestamp_too_early(minimal_time,timestamp))letcheck_baking_rightsc{Block_header.priority;_}pred_timestamp=letlevel=Level.currentcinRoll.baking_rights_ownerclevel~priority>>=?fundelegate->check_timestampcprioritypred_timestamp>>=?fun()->returndelegatetypeerror+=Incorrect_priority(* `Permanent *)let()=register_error_kind`Permanent~id:"incorrect_priority"~title:"Incorrect priority"~description:"Block priority must be non-negative."~pp:(funppf()->Format.fprintfppf"The block priority must be non-negative.")Data_encoding.unit(functionIncorrect_priority->Some()|_->None)(fun()->Incorrect_priority)letendorsement_rewardctxt~block_priority:prion=ifCompare.Int.(prio>=0)thenLwt.returnTez.(Constants.endorsement_rewardctxt/?(Int64.(succ(of_intprio))))>>=?funtez->Lwt.returnTez.(tez*?Int64.of_intn)elsefailIncorrect_priorityletbaking_prioritiesclevel=letrecfpriority=Roll.baking_rights_ownerclevel~priority>>=?fundelegate->return(LCons(delegate,(fun()->f(succpriority))))inf0letendorsement_rightsclevel=fold_left_s(funaccslot->Roll.endorsement_rights_ownerclevel~slot>>=?funpk->letpkh=Signature.Public_key.hashpkinletright=matchSignature.Public_key_hash.Map.find_optpkhaccwith|None->(pk,[slot],false)|Some(pk,slots,used)->(pk,slot::slots,used)inreturn(Signature.Public_key_hash.Map.addpkhrightacc))Signature.Public_key_hash.Map.empty(0-->(Constants.endorsers_per_blockc-1))letcheck_endorsement_rightsctxtchain_id(op:Kind.endorsementOperation.t)=letcurrent_level=Level.currentctxtinletSingle(Endorsement{level;_})=op.protocol_data.contentsinbeginifRaw_level.(succlevel=current_level.level)thenreturn(Alpha_context.allowed_endorsementsctxt)elseendorsement_rightsctxt(Level.from_rawctxtlevel)end>>=?funendorsements->matchSignature.Public_key_hash.Map.fold(* no find_first *)(funpkh(pk,slots,used)acc->matchOperation.check_signature_syncpkchain_idopwith|Error_->acc|Ok()->Some(pkh,slots,used))endorsementsNonewith|None->failUnexpected_endorsement|Somev->returnvletselect_delegatedelegatedelegate_listmax_priority=letrecloopaccln=ifCompare.Int.(n>=max_priority)thenreturn(List.revacc)elseletLCons(pk,t)=linletacc=ifSignature.Public_key_hash.equaldelegate(Signature.Public_key.hashpk)thenn::accelseaccint()>>=?funt->loopacct(succn)inloop[]delegate_list0letfirst_baking_prioritiesctxt?(max_priority=32)delegatelevel=baking_prioritiesctxtlevel>>=?fundelegate_list->select_delegatedelegatedelegate_listmax_priorityletcheck_hashhashstamp_threshold=letbytes=Block_hash.to_byteshashinletword=MBytes.get_int64bytes0inCompare.Uint64.(word<=stamp_threshold)letcheck_header_proof_of_work_stampshellcontentsstamp_threshold=lethash=Block_header.hash{shell;protocol_data={contents;signature=Signature.zero}}incheck_hashhashstamp_thresholdletcheck_proof_of_work_stampctxtblock=letproof_of_work_threshold=Constants.proof_of_work_thresholdctxtinifcheck_header_proof_of_work_stampblock.Block_header.shellblock.protocol_data.contentsproof_of_work_thresholdthenreturn_unitelsefailInvalid_stampletcheck_signatureblockchain_idkey=letcheck_signaturekey{Block_header.shell;protocol_data={contents;signature}}=letunsigned_header=Data_encoding.Binary.to_bytes_exnBlock_header.unsigned_encoding(shell,contents)inSignature.check~watermark:(Block_headerchain_id)keysignatureunsigned_headerinifcheck_signaturekeyblockthenreturn_unitelsefail(Invalid_block_signature(Block_header.hashblock,Signature.Public_key.hashkey))letmax_fitness_gapctxt=letslots=Int64.of_int(Constants.endorsers_per_blockctxt+1)inInt64.addslots1Lletcheck_fitness_gapctxt(block:Block_header.t)=letcurrent_fitness=Fitness.currentctxtinLwt.return(Fitness.to_int64block.shell.fitness)>>=?funannounced_fitness->letgap=Int64.subannounced_fitnesscurrent_fitnessinifCompare.Int64.(gap<=0L||max_fitness_gapctxt<gap)thenfail(Invalid_fitness_gap(max_fitness_gapctxt,gap))elsereturn_unitletlast_of_a_cyclectxtl=Compare.Int32.(Int32.succl.Level.cycle_position=Constants.blocks_per_cyclectxt)letdawn_of_a_new_cyclectxt=letlevel=Level.currentctxtiniflast_of_a_cyclectxtlevelthenreturn_somelevel.cycleelsereturn_none