123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018-2021 Tarides <contact@tarides.com> *)(* Copyright (c) 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. *)(* *)(*****************************************************************************)openTezos_context_encoding.ContextmoduleStore=Irmin_pack_mem.Make(Node)(Commit)(Conf)(Metadata)(Contents)(Path)(Branch)(Hash)typeindex=Store.Repo.ttypecontext={repo:index;parents:Store.Commit.tlist;tree:Store.tree}typet=contexttypetree=Store.treetypekey=stringlisttypevalue=bytesmoduleTree=Tezos_context_helpers.Context.Make_tree(Store)includeTreeletindex{repo;_}=repoletexistsindexkey=Store.Commit.of_hashindex(Hash.of_context_hashkey)>|=function|None->false|Some_->trueletcheckoutindexkey=Store.Commit.of_hashindex(Hash.of_context_hashkey)>>=function|None->Lwt.return_none|Somecommit->lettree=Store.Commit.treecommitinletctxt={repo=index;tree;parents=[commit]}inLwt.return_somectxtletcheckout_exnindexkey=checkoutindexkey>>=function|None->Lwt.failNot_found|Somep->Lwt.returnp(* unshallow possible 1-st level objects from previous partial
checkouts ; might be better to pass directly the list of shallow
objects. *)letunshallowcontext=Store.Tree.listcontext.tree[]>>=funchildren->Store.Private.Repo.batchcontext.repo(funxy_->List.iter_s(fun(s,k)->matchStore.Tree.destructkwith|`Contents_->Lwt.return()|`Node_->Store.Tree.get_treecontext.tree[s]>>=funtree->Store.save_tree~clear:truecontext.repoxytree>|=fun_->())children)letraw_commit~time?(message="")context=letinfo=Irmin.Info.v~date:(Time.Protocol.to_secondstime)~author:"Tezos"messageinletparents=List.mapStore.Commit.hashcontext.parentsinunshallowcontext>>=fun()->Store.Commit.vcontext.repo~info~parentscontext.tree>|=funh->Store.Tree.clearcontext.tree;hlethash~time?(message="")context=letinfo=Irmin.Info.v~date:(Time.Protocol.to_secondstime)~author:"Tezos"messageinletparents=List.map(func->Store.Commit.hashc)context.parentsinletnode=Store.Tree.hashcontext.treeinletcommit=Store.Private.Commit.Val.v~parents~node~infoinletx=Store.Private.Commit.Key.hashcommitinHash.to_context_hashxletcommit~time?messagecontext=raw_commit~time?messagecontext>|=funcommit->Hash.to_context_hash(Store.Commit.hashcommit)(*-- Generic Store Primitives ------------------------------------------------*)letdata_keykey="data"::keyletmemctxtkey=Tree.memctxt.tree(data_keykey)letmem_treectxtkey=Tree.mem_treectxt.tree(data_keykey)letlistctxt?offset?lengthkey=Tree.listctxt.tree?offset?length(data_keykey)letfindctxtkey=Tree.findctxt.tree(data_keykey)letraw_addctxtkeydata=Tree.addctxt.treekeydata>|=funtree->{ctxtwithtree}letaddctxtkeydata=raw_addctxt(data_keykey)dataletraw_removectxtk=Tree.removectxt.treek>|=funtree->{ctxtwithtree}letremovectxtkey=raw_removectxt(data_keykey)letfind_treectxtkey=Tree.find_treectxt.tree(data_keykey)letadd_treectxtkeytree=Tree.add_treectxt.tree(data_keykey)tree>|=funtree->{ctxtwithtree}letfold?depthctxtkey~order~init~f=Tree.fold?depthctxt.tree(data_keykey)~order~init~fletcurrent_protocol_key=["protocol"]letcurrent_predecessor_block_metadata_hash_key=["predecessor_block_metadata_hash"]letcurrent_predecessor_ops_metadata_hash_key=["predecessor_ops_metadata_hash"]letget_protocolctxt=Tree.findctxt.treecurrent_protocol_key>>=function|None->assertfalse|Somedata->Lwt.return(Protocol_hash.of_bytes_exndata)letadd_protocolctxtkey=letkey=Protocol_hash.to_byteskeyinraw_addctxtcurrent_protocol_keykeyletget_hash_version_c=Context_hash.Version.of_int0letset_hash_versioncv=ifContext_hash.Version.(of_int0=v)thenreturncelsefail(Tezos_context_helpers.Context.Unsupported_context_hash_versionv)letadd_predecessor_block_metadata_hashvhash=letdata=Data_encoding.Binary.to_bytes_exnBlock_metadata_hash.encodinghashinraw_addvcurrent_predecessor_block_metadata_hash_keydataletadd_predecessor_ops_metadata_hashvhash=letdata=Data_encoding.Binary.to_bytes_exnOperation_metadata_list_list_hash.encodinghashinraw_addvcurrent_predecessor_ops_metadata_hash_keydataletcreate()=letcfg=Irmin_pack.config"/tmp"inletpromise=Store.Repo.vcfg>>=funrepo->Lwt.return{repo;parents=[];tree=Store.Tree.empty}inmatchLwt.statepromisewith|Lwt.Returnresult->result|Lwt.Failexn->raiseexn|Lwt.Sleep->(* The in-memory context should never block *)assertfalseletempty=create()letconcrete_encoding:Store.Tree.concreteData_encoding.t=letopenData_encodinginmu"memory_context"(funencoding->letmap_encoding=list(tup2stringencoding)inunion[case~title:"tree"(Tag0)map_encoding(function`Treemap->Somemap|`Contents_->None)(funmap->`Treemap);case~title:"value"(Tag1)bytes(function`Contents(v,_)->Somev|`Tree_->None)(funv->`Contents(v,()));])letencoding:tData_encoding.t=Data_encoding.conv(funt->lettree=Store.Tree.to_concretet.treeinlettree=(* This is safe as store.Tree will never call any blocking
functions. *)matchLwt.statetreewithReturnt->t|_->assertfalseintree)(funt->lettree=Store.Tree.of_concretetinletctxt=create()in{ctxtwithtree})concrete_encodingletcurrent_test_chain_key=["test_chain"]letget_test_chainv=Tree.findv.treecurrent_test_chain_key>>=function|None->Lwt.fail(Failure"Unexpected error (Context.get_test_chain)")|Somedata->(matchData_encoding.Binary.of_bytesTest_chain_status.encodingdatawith|Errorre->Format.kasprintf(funs->Lwt.fail(Failures))"Error in Context.get_test_chain: %a"Data_encoding.Binary.pp_read_errorre|Okr->Lwt.returnr)letadd_test_chainvid=letid=Data_encoding.Binary.to_bytes_exnTest_chain_status.encodingidinraw_addvcurrent_test_chain_keyid