123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2020-2021 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. *)(* *)(*****************************************************************************)(* Invariant: a maximum of [max_predecessors] predecessors at all
time: if a block has fewer than 12 predecessors then it is padded
so its size remain constant. *)moduleBlock_info=structtypet={offset:int;predecessors:Block_hash.tlist;resulting_context_hash:Context_hash.t;}letmax_predecessors=12letencoded_list_size=letr=max_predecessors*Block_hash.size(* uint16 *)inassert(r<1lsl16);rletencoded_size=8+1+encoded_list_size+Context_hash.sizeletcontext_hash_t=letopenReprinmap(bytes_of(`FixedContext_hash.size))(funres->Context_hash.of_bytes_exnres)(funch->Context_hash.to_bytesch)(* Format:
<file_offset>(8) + <list_size>(1) + <list>(list_size * Block_hash.size) + <context_hash_size>(31)*)lett=letopenReprinmap(tripleint(list~len:`Int16Block_key.t)context_hash_t)(fun(offset,predecessors,resulting_context_hash)->{offset;predecessors;resulting_context_hash})(fun{offset;predecessors;resulting_context_hash}->(offset,predecessors,resulting_context_hash))letencodev=letbytes=Bytes.createencoded_sizeinBytes.set_int64_bebytes0(Int64.of_intv.offset);letlen=List.lengthv.predecessorsin(* Start reading after the <file_offset>(8) *)Bytes.set_int8bytes8len;List.iteri(funih->(* Start reading after the <file_offset>(8) + <list_lize>(1) *)Bytes.blit(Block_hash.to_bytesh)0bytes(8+1+(i*Block_hash.size))Block_hash.size)v.predecessors;Bytes.blit(Context_hash.to_bytesv.resulting_context_hash)0bytes(encoded_size-Context_hash.size)Context_hash.size;Bytes.unsafe_to_stringbytesletdecodestri=lettotal_size=i+encoded_sizeinletcurrent_offset=refiin(* The Int64.to_int conversion is not likely to fail as it was
written based on Int64.of_int and the encoded offset won't
reach the int64 max value. *)letoffset=TzEndian.get_int64_stringstr!current_offset|>Int64.to_intin(* Setting current_offset right after the <file_offset>(8)*)current_offset:=!current_offset+8;letlist_size=TzEndian.get_int8_stringstr!current_offsetin(* Setting current_offset right after the <list_size>(1)*)current_offset:=!current_offset+1;letpredecessors=ref[]inletpredecessors_limit=!current_offsetincurrent_offset:=predecessors_limit+((list_size-1)*Block_hash.size);while!current_offset>=predecessors_limitdopredecessors:=(String.substr!current_offsetBlock_hash.size|>Block_hash.of_string_exn)::!predecessors;current_offset:=!current_offset-Block_hash.sizedone;letresulting_context_hash=letoffset=total_size-Context_hash.sizeinString.substroffsetContext_hash.size|>Context_hash.of_string_exnin{offset;predecessors=!predecessors;resulting_context_hash}letppfmtv=letopenFormatinfprintffmt"@[offset: %d,@ predecessors: [@[<hov>%a @]],@ resulting context hash: \
%a@]"v.offset(pp_print_list~pp_sep:(funfmt()->fprintffmt" ;@,")Block_hash.pp)v.predecessorsContext_hash.ppv.resulting_context_hashend(* Hashmap from block's hashes to location *)includeIndex_unix.Make(Block_key)(Block_info)(Index.Cache.Unbounded)moduleLegacy=structmoduleLegacy_block_info=structtypet={offset:int;predecessors:Block_hash.tlist}lett=letopenReprinmap(pairint(list~len:`Int16Block_key.t))(fun(offset,predecessors)->{offset;predecessors})(fun{offset;predecessors}->(offset,predecessors))letmax_predecessors=12letencoded_list_size=letr=max_predecessors*Block_hash.size(* uint16 *)inassert(r<1lsl16);rletencoded_size=8+1+encoded_list_size(* Format:
<file_offset>(8) + <list_size>(1) + <list>(list_size * Block_hash.size) *)letencodev=letbytes=Bytes.createencoded_sizeinBytes.set_int64_bebytes0(Int64.of_intv.offset);letlen=List.lengthv.predecessorsin(* Start reading after the <file_offset>(8) *)Bytes.set_int8bytes8len;List.iteri(funih->(* Start reading after the <file_offset>(8) + <list_lize>(1) *)Bytes.blit(Block_hash.to_bytesh)0bytes(8+1+(i*Block_hash.size))Block_hash.size)v.predecessors;Bytes.unsafe_to_stringbytesletdecodestri=letcurrent_offset=refiin(* The Int64.to_int conversion is not likely to fail as it was
written based on Int64.of_int and the encoded offset won't
reach the int64 max value. *)letoffset=TzEndian.get_int64_stringstr!current_offset|>Int64.to_intin(* Setting current_offset right after the <file_offset>(8)*)current_offset:=!current_offset+8;letlist_size=TzEndian.get_int8_stringstr!current_offsetincurrent_offset:=!current_offset+1;letpredecessors=ref[]inletlimit=!current_offsetincurrent_offset:=limit+((list_size-1)*Block_hash.size);while!current_offset>=limitdopredecessors:=(String.substr!current_offsetBlock_hash.size|>Block_hash.of_string_exn)::!predecessors;current_offset:=!current_offset-Block_hash.sizedone;{offset;predecessors=!predecessors}endincludeIndex_unix.Make(Block_key)(Legacy_block_info)(Index.Cache.Unbounded)end