123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2023 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. *)(* *)(*****************************************************************************)openProtocol_client_contextmoduleEvents=Baking_events.Per_block_votesletdefault_vote_json_filename="per_block_votes.json"typeper_block_votes={liquidity_baking_toggle_vote:Protocol.Alpha_context.Per_block_votes.per_block_vote;adaptive_issuance_vote_opt:Protocol.Alpha_context.Per_block_votes.per_block_voteoption;}letvote_file_content_encoding=letopenData_encodingindef(String.concat"."[Protocol.name;"vote_file_content"])@@conv(fun{liquidity_baking_toggle_vote;adaptive_issuance_vote_opt}->(liquidity_baking_toggle_vote,adaptive_issuance_vote_opt))(fun(liquidity_baking_toggle_vote,adaptive_issuance_vote_opt)->{liquidity_baking_toggle_vote;adaptive_issuance_vote_opt})(obj2(req"liquidity_baking_toggle_vote"Protocol.Alpha_context.Per_block_votes.liquidity_baking_vote_encoding)(opt"adaptive_issuance_vote"Protocol.Alpha_context.Per_block_votes.adaptive_issuance_vote_encoding))typeerror+=Block_vote_file_not_foundofstringtypeerror+=Block_vote_file_invalidofstringtypeerror+=Block_vote_file_wrong_contentofstringtypeerror+=Block_vote_file_missing_liquidity_baking_toggle_voteofstringtypeerror+=Missing_vote_on_startuplet()=register_error_kind`Permanent~id:"per_block_vote_file.block_vote_file_not_found"~title:"The provided block vote file path does not point to an existing file."~description:"A block vote file path was provided on the command line but the path \
does not point to an existing file."~pp:(funppffile_path->Format.fprintfppf"@[The provided block vote file path \"%s\" does not point to an \
existing file.@]"file_path)Data_encoding.(obj1(req"file_path"string))(function|Block_vote_file_not_foundfile_path->Somefile_path|_->None)(funfile_path->Block_vote_file_not_foundfile_path);register_error_kind`Permanent~id:"per_block_vote_file.block_vote_file_invalid"~title:"The provided block vote file path does not point to a valid JSON file."~description:"A block vote file path was provided on the command line but the path \
does not point to a valid JSON file."~pp:(funppffile_path->Format.fprintfppf"@[The provided block vote file path \"%s\" does not point to a valid \
JSON file. The file exists but its content is not valid JSON.@]"file_path)Data_encoding.(obj1(req"file_path"string))(functionBlock_vote_file_invalidfile_path->Somefile_path|_->None)(funfile_path->Block_vote_file_invalidfile_path);register_error_kind`Permanent~id:"per_block_vote_file.block_vote_file_wrong_content"~title:"The content of the provided block vote file is unexpected."~description:"The block vote file is valid JSON but its content is not the expected \
one."~pp:(funppffile_path->Format.fprintfppf"@[The provided block vote file \"%s\" is a valid JSON file but its \
content is unexpected. Expecting a JSON file containing \
'{\"liquidity_baking_toggle_vote\": value1, \
\"adaptive_issuance_vote\": value2}' or '{\"adaptive_issuance_vote\": \
value1, \"liquidity_baking_toggle_vote\": value2}', where value1 is \
one of \"on\", \"off\", or \"pass\" and value2 is one of \"on\", \
\"off\", or \"pass\", or '{\"liquidity_baking_toggle_vote\": value}' \
where value is one of \"on\", \"off\", or \"pass\".@]"file_path)Data_encoding.(obj1(req"file_path"string))(function|Block_vote_file_wrong_contentfile_path->Somefile_path|_->None)(funfile_path->Block_vote_file_wrong_contentfile_path);register_error_kind`Permanent~id:"per_block_vote_file.block_vote_file_missing_liquidity_baking_toggle_vote"~title:"In the provided block vote file, no entry for liquidity baking toggle \
vote was found"~description:"In the provided block vote file, no entry for liquidity baking toggle \
vote was found."~pp:(funppffile_path->Format.fprintfppf"@[In the provided block vote file \"%s\", the \
\"liquidity_baking_toggle_vote\" field is missing. Expecting a JSON \
file containing '{\"liquidity_baking_toggle_vote\": value1, \
\"adaptive_issuance_vote\": value2}' or '{\"adaptive_issuance_vote\": \
value1, \"liquidity_baking_toggle_vote\": value2}', where value1 is \
one of \"on\", \"off\", or \"pass\" and value2 is one of \"on\", \
\"off\", or \"pass\", or '{\"liquidity_baking_toggle_vote\": value}' \
where value is one of \"on\", \"off\", or \"pass\".@]"file_path)Data_encoding.(obj1(req"file_path"string))(function|Block_vote_file_missing_liquidity_baking_toggle_votefile_path->Somefile_path|_->None)(funfile_path->Block_vote_file_missing_liquidity_baking_toggle_votefile_path);register_error_kind`Permanent~id:"per_block_vote_file.missing_vote_on_startup"~title:"Missing vote on startup"~description:"No CLI flag, file path, or votes file in default location provided on \
startup"~pp:(funfmt()->Format.fprintffmt"Missing liquidity baking toggle vote, please use either the \
--liquidity-baking-toggle-vote option, or the --votefile option or a \
votes file in the default location: per_block_votes.json in the \
current working directory or in the baker directory.")Data_encoding.empty(functionMissing_vote_on_startup->Some()|_->None)(fun()->Missing_vote_on_startup)letcheck_file_existsfile=letopenLwt_result_syntaxinlet*!file_exists=Lwt.catch(fun()->Lwt_unix.file_existsfile)(fun_->Lwt.return_false)iniffile_existsthenreturn_unitelsetzfail(Block_vote_file_not_foundfile)letread_per_block_votes~per_block_vote_file:'atzresultLwt.t=letopenLwt_result_syntaxinlet*!()=Events.(emitreading_per_block_votes)per_block_vote_fileinlet*()=check_file_existsper_block_vote_fileinlet*votes_json=trace(Block_vote_file_invalidper_block_vote_file)(Lwt_utils_unix.Json.read_fileper_block_vote_file)inlet*votes=trace(Block_vote_file_wrong_contentper_block_vote_file)(protect(fun()->return(Data_encoding.Json.destructvote_file_content_encodingvotes_json)))inreturnvotesletread_per_block_votes_no_fail~default~per_block_vote_file=letopenLwt_syntaxinlet*result=read_per_block_votes~per_block_vote_fileinmatchresultwith|Errorerrs->let*()=Events.(emitper_block_vote_file_fail)errsinreturndefault|Ok{liquidity_baking_toggle_vote;adaptive_issuance_vote_opt=Someadaptive_issuance_vote;}->returnProtocol.Alpha_context.Per_block_votes.{liquidity_baking_vote=liquidity_baking_toggle_vote;adaptive_issuance_vote;}|Ok{liquidity_baking_toggle_vote;adaptive_issuance_vote_opt=None}->return{defaultwithliquidity_baking_vote=liquidity_baking_toggle_vote}letload_per_block_votes_config~default_liquidity_baking_vote~default_adaptive_issuance_vote~per_block_vote_file:Baking_configuration.per_block_votes_configtzresultLwt.t=letopenLwt_result_syntaxin(* If a vote file is given, it takes priority. Otherwise, we expect
per-block vote arguments to be passed. *)letdefault_adaptive_issuance_vote=(* Unlike the vote for liquidity baking, the vote for adaptive
issuance is not mandatory. *)matchdefault_adaptive_issuance_votewith|None->Protocol.Alpha_context.Per_block_votes.Per_block_vote_pass|Somedefault_adaptive_issuance_vote->default_adaptive_issuance_voteinlet*config=match(per_block_vote_file,default_liquidity_baking_vote)with|None,None->tzfailMissing_vote_on_startup|None,Someliquidity_baking_vote->return{Baking_configuration.vote_file=None;liquidity_baking_vote;adaptive_issuance_vote=default_adaptive_issuance_vote;}|Someper_block_vote_file,_->(let*!(res:_tzresult)=read_per_block_votes~per_block_vote_fileinmatchreswith|Ok{liquidity_baking_toggle_vote=liquidity_baking_vote;adaptive_issuance_vote_opt;}->letadaptive_issuance_vote=Option.value~default:default_adaptive_issuance_voteadaptive_issuance_vote_optinreturn{Baking_configuration.vote_file=Someper_block_vote_file;liquidity_baking_vote;adaptive_issuance_vote;}|Errorerrs->let*!()=Events.(emitper_block_vote_file_fail)errsintzfailMissing_vote_on_startup)inlet*!()=Events.(emitliquidity_baking_toggle_vote)config.liquidity_baking_voteinlet*!()=Events.(emitadaptive_issuance_vote)config.adaptive_issuance_voteinreturnconfig