123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)openProtocoltypedelegate_selection=(Raw_level_repr.t*(Round_repr.t*Signature.public_key_hash)list)listmoduleLevelRoundMap=Map.Make(structtypet=Level_repr.t*Round_repr.tletcompare(l1,r1)(l2,r2)=Stdlib.compare(Raw_level_repr.to_int32l1.Level_repr.level,Round_repr.to_int32r1)(Raw_level_repr.to_int32l2.Level_repr.level,Round_repr.to_int32r2)end)let_=Client_keys.register_signer(moduleTezos_signer_backends.Unencrypted)(* Initialize a context in memory with the Mockup *)letinit_context?constants_overrides_json?bootstrap_accounts_jsonparameters=letopenLwt_result_syntaxinletparameters=Data_encoding.Binary.of_bytes_exnMockup.M.parameters_encoding@@Data_encoding.Binary.to_bytes_exnMockup.Protocol_parameters.encodingparametersinlet*mockup_init=Mockup.M.init~cctxt:Client_context.null_printer~parameters~constants_overrides_json~bootstrap_accounts_jsoninletctxt=mockup_init.rpc_context.contextinlettimestamp=Time.Protocol.of_seconds0Lin(* The timestamp is irrelevant for the rights *)let*!result=Raw_context.preparectxt~level:1l~predecessor_timestamp:timestamp~timestamp~adaptive_issuance_enable:falseinLwt.return@@Environment.wrap_tzresultresult(* Change the initial seed for the first preserved cycles. This suppose that the
seeds for these cycles are already set, which is the case because this
function is always called after {!init_context}. *)letchange_seed?initial_seedctxt=letopenLwt_result_syntaxinletpreserved=Constants_storage.preserved_cyclesctxtinlet+(_:int),ctxt=List.fold_left_es(fun(c,ctxt)seed->letcycle=Cycle_repr.of_int32_exn(Int32.of_intc)inlet*ctxt=Storage.Seed.For_cycle.remove_existingctxtcycleinlet+ctxt=Storage.Seed.For_cycle.initctxtcycleseedin(c+1,ctxt))(0,ctxt)(Seed_repr.initial_seeds?initial_seed(preserved+2))inctxtletinit?constants_overrides_json?bootstrap_accounts_jsonparameters=letopenLwt_result_syntaxinlet*ctxt=init_context?constants_overrides_json?bootstrap_accounts_jsonparametersinletblocks_per_cycle=Constants_storage.blocks_per_cyclectxtinletblocks_per_commitment=Constants_storage.blocks_per_commitmentctxtinletcycle_eras=[Level_repr.{first_level=Raw_level_repr.of_int32_exn0l;first_cycle=Cycle_repr.root;blocks_per_cycle;blocks_per_commitment;};]inlet*?cycle_eras=Level_repr.create_cycle_erascycle_eras|>Environment.wrap_tzresultinreturn(ctxt,cycle_eras)(* Check if the given selection correponds to the baking rights assigned by the
protocol (with the initial seeds registered in the context). Returns [true]
when the selection is the one of the protocol or [false] otherwise. *)letcheckctxt~selection=letopenLwt_result_syntaxinLwt.catch(fun()->let*_ctxt=LevelRoundMap.fold_es(fun(level,round)delegatectxt->let*ctxt,_,pk=let*!result=Delegate_sampler.baking_rights_ownerctxtlevel~roundinLwt.return@@Environment.wrap_tzresultresultinifnot(Signature.Public_key_hash.equaldelegatepk.delegate)thenraiseExitelsereturnctxt)selectionctxtinreturn_true)(functionExit->return_false|e->Lwt.reraisee)(* Create random 32 bytes *)letrnd_bytes32()=letb1=Random.int64Int64.max_intinletb2=Random.int64Int64.max_intinletb3=Random.int64Int64.max_intinletb4=Random.int64Int64.max_intinletb=Bytes.make32'\000'inTzEndian.set_int64b0b1;TzEndian.set_int64b8b2;TzEndian.set_int64b16b3;TzEndian.set_int64b24b4;bletmk_selection_mapcycle_erasselection=List.fold_left(funacc(level,round_delegates)->letlevel=Level_repr.level_from_raw~cycle_eraslevelinList.fold_left(funacc(round,delegate)->ifLevelRoundMap.mem(level,round)accthenStdlib.failwith"Duplicate level/round";LevelRoundMap.add(level,round)delegateacc)accround_delegates)LevelRoundMap.emptyselection(* Bruteforce an initial seed nonce for the desired delegate selection *)letbruteforce?(show_progress=false)?(random_seed=0)?max?(parameters=Mockup.Protocol_parameters.default_value)?constants_overrides_json?bootstrap_accounts_jsonselection=letopenLwt_result_syntaxinRandom.initrandom_seed;let*ctxt,cycle_eras=init?constants_overrides_json?bootstrap_accounts_jsonparametersinletselection=mk_selection_mapcycle_erasselectioninletlast_nb_chars=ref0inletframes=[|"( ● )";"( ● )";"( ● )";"( ● )";"( ●)";"( ● )";"( ● )";"( ● )";"( ● )";"(● )";|]inletnframes=Array.lengthframesinletframen=frames.(nmodnframes)inletrecloopn=ifshow_progress&&n<>0&&nmod10_000=0then(Format.eprintf"%s"(String.make!last_nb_chars'\b');lets=frame(n/10_000)^" "^string_of_intninlast_nb_chars:=String.lengths;Format.eprintf"%s%!"s);matchmaxwith|Somemaxwhenn>max->failwith"Did not find seed nonce"|_->(letinitial_seed=ifn=0thenNoneelseSome(State_hash.of_bytes_exn(rnd_bytes32()))inlet*ctxt=let*!result=change_seed?initial_seedctxtinLwt.return@@Environment.wrap_tzresultresultinlet*result=checkctxt~selectioninmatchresultwith|true->Format.eprintf"%s%!"(String.make!last_nb_chars'\b');returninitial_seed|false->loop(n+1))inloop0(* Check that an initial seed corresonds to the desired delegate selection *)letcheck_seed?(parameters=Mockup.Protocol_parameters.default_value)?constants_overrides_json?bootstrap_accounts_json~seedselection=letopenLwt_result_syntaxinlet*ctxt,cycle_eras=init?constants_overrides_json?bootstrap_accounts_jsonparametersinletselection=mk_selection_mapcycle_erasselectioninlet*ctxt=let*!result=change_seed?initial_seed:seedctxtinLwt.return@@Environment.wrap_tzresultresultincheckctxt~selection