Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file block_header_repr.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)(** Block header *)typecontents={payload_hash:Block_payload_hash.t;payload_round:Round_repr.t;seed_nonce_hash:Nonce_hash.toption;proof_of_work_nonce:bytes;liquidity_baking_toggle_vote:Liquidity_baking_repr.liquidity_baking_toggle_vote;}typeprotocol_data={contents:contents;signature:Signature.t}typet={shell:Block_header.shell_header;protocol_data:protocol_data}typeblock_header=ttyperaw=Block_header.ttypeshell_header=Block_header.shell_headerletraw_encoding=Block_header.encodingletshell_header_encoding=Block_header.shell_header_encodingtypeblock_watermark=Block_headerofChain_id.tletbytes_of_block_watermark=function|Block_headerchain_id->Bytes.cat(Bytes.of_string"\x11")(Chain_id.to_byteschain_id)letto_watermarkb=Signature.Custom(bytes_of_block_watermarkb)letof_watermark=function|Signature.Customb->ifCompare.Int.(Bytes.lengthb>0)thenmatchBytes.getb0with|'\x11'->Option.map(funchain_id->Block_headerchain_id)(Chain_id.of_bytes_opt(Bytes.subb1(Bytes.lengthb-1)))|_->NoneelseNone|_->Noneletcontents_encoding=letopenData_encodingindef"block_header.alpha.unsigned_contents"@@conv(fun{payload_hash;payload_round;seed_nonce_hash;proof_of_work_nonce;liquidity_baking_toggle_vote;}->(payload_hash,payload_round,proof_of_work_nonce,seed_nonce_hash,liquidity_baking_toggle_vote))(fun(payload_hash,payload_round,proof_of_work_nonce,seed_nonce_hash,liquidity_baking_toggle_vote)->{payload_hash;payload_round;seed_nonce_hash;proof_of_work_nonce;liquidity_baking_toggle_vote;})(obj5(req"payload_hash"Block_payload_hash.encoding)(req"payload_round"Round_repr.encoding)(req"proof_of_work_nonce"(Fixed.bytesHexConstants_repr.proof_of_work_nonce_size))(opt"seed_nonce_hash"Nonce_hash.encoding)(req"liquidity_baking_toggle_vote"Liquidity_baking_repr.liquidity_baking_toggle_vote_encoding))letprotocol_data_encoding=letopenData_encodingindef"block_header.alpha.signed_contents"@@conv(fun{contents;signature}->(contents,signature))(fun(contents,signature)->{contents;signature})(merge_objscontents_encoding(obj1(req"signature"Signature.encoding)))letraw{shell;protocol_data}=letprotocol_data=Data_encoding.Binary.to_bytes_exnprotocol_data_encodingprotocol_datain{Block_header.shell;protocol_data}letunsigned_encoding=letopenData_encodinginmerge_objsBlock_header.shell_header_encodingcontents_encodingletencoding=letopenData_encodingindef"block_header.alpha.full_header"@@conv(fun{shell;protocol_data}->(shell,protocol_data))(fun(shell,protocol_data)->{shell;protocol_data})(merge_objsBlock_header.shell_header_encodingprotocol_data_encoding)(** Constants *)letmax_header_length=letfake_level=Raw_level_repr.rootinletfake_round=Round_repr.zeroinletfake_fitness=Fitness_repr.create_without_locked_round~level:fake_level~predecessor_round:fake_round~round:fake_roundinletfake_shell={Block_header.level=0l;proto_level=0;predecessor=Block_hash.zero;timestamp=Time.of_seconds0L;validation_passes=0;operations_hash=Operation_list_list_hash.zero;fitness=Fitness_repr.to_rawfake_fitness;context=Context_hash.zero;}andfake_contents={payload_hash=Block_payload_hash.zero;payload_round=Round_repr.zero;proof_of_work_nonce=Bytes.makeConstants_repr.proof_of_work_nonce_size'0';seed_nonce_hash=SomeNonce_hash.zero;liquidity_baking_toggle_vote=LB_pass;}inData_encoding.Binary.lengthencoding{shell=fake_shell;protocol_data={contents=fake_contents;signature=Signature.zero};}(** Header parsing entry point *)lethash_raw=Block_header.hashlethash{shell;protocol_data}=Block_header.hash{shell;protocol_data=Data_encoding.Binary.to_bytes_exnprotocol_data_encodingprotocol_data;}typelocked_round_evidence={preendorsement_round:Round_repr.t;preendorsement_count:int;}typeerror+=|(* Permanent *)Invalid_block_signatureofBlock_hash.t*Signature.Public_key_hash.t|(* Permanent *)Invalid_stamp|(* Permanent *)Invalid_payload_hashof{expected:Block_payload_hash.t;provided:Block_payload_hash.t;}|(* Permanent *)Locked_round_after_block_roundof{locked_round:Round_repr.t;round:Round_repr.t;}|(* Permanent *)Invalid_payload_roundof{payload_round:Round_repr.t;round:Round_repr.t;}|(* Permanent *)Insufficient_locked_round_evidenceof{voting_power:int;consensus_threshold:int;}|(* Permanent *)Invalid_commitmentof{expected:bool}|(* Permanent *)Wrong_timestampofTime.t*Time.tlet()=register_error_kind`Permanent~id:"block_header.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))(function|Invalid_block_signature(block,pkh)->Some(block,pkh)|_->None)(fun(block,pkh)->Invalid_block_signature(block,pkh));register_error_kind`Permanent~id:"block_header.invalid_stamp"~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:"block_header.invalid_payload_hash"~title:"Invalid payload hash"~description:"Invalid payload hash."~pp:(funppf(expected,provided)->Format.fprintfppf"Invalid payload hash (expected: %a, provided: %a)."Block_payload_hash.pp_shortexpectedBlock_payload_hash.pp_shortprovided)Data_encoding.(obj2(req"expected"Block_payload_hash.encoding)(req"provided"Block_payload_hash.encoding))(function|Invalid_payload_hash{expected;provided}->Some(expected,provided)|_->None)(fun(expected,provided)->Invalid_payload_hash{expected;provided});();register_error_kind`Permanent~id:"block_header.locked_round_after_block_round"~title:"Locked round after block round"~description:"Locked round after block round."~pp:(funppf(locked_round,round)->Format.fprintfppf"Locked round (%a) is after the block round (%a)."Round_repr.pplocked_roundRound_repr.ppround)Data_encoding.(obj2(req"locked_round"Round_repr.encoding)(req"round"Round_repr.encoding))(function|Locked_round_after_block_round{locked_round;round}->Some(locked_round,round)|_->None)(fun(locked_round,round)->Locked_round_after_block_round{locked_round;round});();register_error_kind`Permanent~id:"block_header.invalid_payload_round"~title:"Invalid payload round"~description:"The given payload round is invalid."~pp:(funppf(payload_round,round)->Format.fprintfppf"The provided payload round (%a) is after the block round (%a)."Round_repr.pppayload_roundRound_repr.ppround)Data_encoding.(obj2(req"payload_round"Round_repr.encoding)(req"round"Round_repr.encoding))(function|Invalid_payload_round{payload_round;round}->Some(payload_round,round)|_->None)(fun(payload_round,round)->Invalid_payload_round{payload_round;round});register_error_kind`Permanent~id:"block_header.insufficient_locked_round_evidence"~title:"Insufficient locked round evidence"~description:"Insufficient locked round evidence."~pp:(funppf(voting_power,consensus_threshold)->Format.fprintfppf"The provided locked round evidence is not sufficient: provided %d \
voting power but was expecting at least %d."voting_powerconsensus_threshold)Data_encoding.(obj2(req"voting_power"int31)(req"consensus_threshold"int31))(function|Insufficient_locked_round_evidence{voting_power;consensus_threshold}->Some(voting_power,consensus_threshold)|_->None)(fun(voting_power,consensus_threshold)->Insufficient_locked_round_evidence{voting_power;consensus_threshold});register_error_kind`Permanent~id:"block_header.invalid_commitment"~title:"Invalid commitment in block header"~description:"The block header has invalid commitment."~pp:(funppfexpected->ifexpectedthenFormat.fprintfppf"Missing seed's nonce commitment in block header."elseFormat.fprintfppf"Unexpected seed's nonce commitment in block header.")Data_encoding.(obj1(req"expected"bool))(functionInvalid_commitment{expected}->Someexpected|_->None)(funexpected->Invalid_commitment{expected});register_error_kind`Permanent~id:"block_header.wrong_timestamp"~title:"Wrong timestamp"~description:"Block timestamp not the expected one."~pp:(funppf(block_ts,expected_ts)->Format.fprintfppf"Wrong timestamp: block timestamp (%a) not the expected one (%a)"Time.pp_humblock_tsTime.pp_humexpected_ts)Data_encoding.(obj2(req"block_timestamp"Time.encoding)(req"expected_timestamp"Time.encoding))(functionWrong_timestamp(t1,t2)->Some(t1,t2)|_->None)(fun(t1,t2)->Wrong_timestamp(t1,t2))letcheck_signature(block:t)(chain_id:Chain_id.t)(key:Signature.Public_key.t)=letcheck_signaturekey({shell;protocol_data={contents;signature}}:t)=letunsigned_header=Data_encoding.Binary.to_bytes_exnunsigned_encoding(shell,contents)inSignature.check~watermark:(to_watermark(Block_headerchain_id))keysignatureunsigned_headerinifcheck_signaturekeyblockthenok()elseerror(Invalid_block_signature(hashblock,Signature.Public_key.hashkey))letcheck_payload_round~round~payload_round=error_whenRound_repr.(payload_round>round)(Invalid_payload_round{payload_round;round})letcheck_timestampround_durations~timestamp~round~predecessor_timestamp~predecessor_round=Round_repr.timestamp_of_roundround_durations~predecessor_timestamp~predecessor_round~round>>?funexpected_timestamp->ifTime_repr.(expected_timestamp=timestamp)thenError_monad.ok()elseerror(Wrong_timestamp(timestamp,expected_timestamp))moduleProof_of_work=structletcheck_hashhashstamp_threshold=letbytes=Block_hash.to_byteshashinletword=TzEndian.get_int64bytes0inCompare.Uint64.(word<=stamp_threshold)letcheck_header_proof_of_work_stampshellcontentsstamp_threshold=lethash=hash{shell;protocol_data={contents;signature=Signature.zero}}incheck_hashhashstamp_thresholdletcheck_proof_of_work_stamp~proof_of_work_thresholdblock=ifcheck_header_proof_of_work_stampblock.shellblock.protocol_data.contentsproof_of_work_thresholdthenok()elseerrorInvalid_stampendletbegin_validate_block_header~(block_header:t)~(chain_id:Chain_id.t)~(predecessor_timestamp:Time.t)~(predecessor_round:Round_repr.t)~(fitness:Fitness_repr.t)~(timestamp:Time.t)~(delegate_pk:Signature.Public_key.t)~(round_durations:Round_repr.Durations.t)~(proof_of_work_threshold:int64)~(expected_commitment:bool)=(* Level relationship between current node and the predecessor is
done by the shell. We know that level is predecessor level + 1.
The predecessor block hash is guaranteed by the shell to be the
one in the shell header. The operations are guaranteed to
correspond to the shell_header.operations_hash by the shell *)let{payload_round;seed_nonce_hash;_}=block_header.protocol_data.contentsinletraw_level=block_header.shell.levelinProof_of_work.check_proof_of_work_stamp~proof_of_work_thresholdblock_header>>?fun()->Raw_level_repr.of_int32raw_level>>?funlevel->check_signatureblock_headerchain_iddelegate_pk>>?fun()->letround=Fitness_repr.roundfitnessincheck_payload_round~round~payload_round>>?fun()->check_timestampround_durations~predecessor_timestamp~predecessor_round~timestamp~round>>?fun()->Fitness_repr.check_except_locked_roundfitness~level~predecessor_round>>?fun()->lethas_commitment=matchseed_nonce_hashwithNone->false|Some_->trueinerror_unlessCompare.Bool.(has_commitment=expected_commitment)(Invalid_commitment{expected=expected_commitment})typecheckable_payload_hash=|No_check|Expected_payload_hashofBlock_payload_hash.tletfinalize_validate_block_header~(block_header_contents:contents)~(round:Round_repr.t)~(* We have to check the round because in the construction case it was
deduced from the time *)(fitness:Fitness_repr.t)~(checkable_payload_hash:checkable_payload_hash)~(locked_round_evidence:locked_round_evidenceoption)~(consensus_threshold:int)=let{payload_hash=actual_payload_hash;seed_nonce_hash=_;proof_of_work_nonce=_;_;}=block_header_contentsin(matchcheckable_payload_hashwith|No_check->Result.return_unit|Expected_payload_hashbph->error_unless(Block_payload_hash.equalactual_payload_hashbph)(Invalid_payload_hash{expected=bph;provided=actual_payload_hash}))>>?fun()->(matchlocked_round_evidencewith|None->okNone|Some{preendorsement_count;preendorsement_round}->error_whenRound_repr.(preendorsement_round>=round)(Locked_round_after_block_round{locked_round=preendorsement_round;round})>>?fun()->error_whenCompare.Int.(preendorsement_count<consensus_threshold)(Insufficient_locked_round_evidence{voting_power=preendorsement_count;consensus_threshold})>>?fun()->ok(Somepreendorsement_round))>>?funlocked_round->Fitness_repr.check_locked_roundfitness~locked_round