123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215(*****************************************************************************)(* *)(* Open Source License *)(* 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. *)(* *)(*****************************************************************************)openProtocol_client_contextmoduleEvents=Baking_events.Liquidity_bakingletdefault_vote_json_filename="per_block_votes.json"letvote_file_content_encoding=letopenData_encodingindef(String.concat"."[Protocol.name;"vote_file_content"])(obj1(req"liquidity_baking_toggle_vote"Protocol.Alpha_context.Liquidity_baking.liquidity_baking_toggle_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:"liquidity_baking_vote.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:"liquidity_baking_vote.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:"liquidity_baking_vote.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 either \
'{\"liquidity_baking_toggle_vote\": \"on\"}', or \
'{\"liquidity_baking_toggle_vote\": \"off\"}', or \
'{\"liquidity_baking_toggle_vote\": \"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:"liquidity_baking_vote.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\" boolean field is missing. Expecting \
a JSON file containing either '{\"liquidity_baking_toggle_vote\": \
\"on\"}', or '{\"liquidity_baking_toggle_vote\": \"off\"}', or \
'{\"liquidity_baking_toggle_vote\": \"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:"liquidity_baking_vote.missing_vote_on_startup"~title:"Missing vote on startup"~description:"No CLI flag, file path, or vote file in default location provided on \
startup"~pp:(funfmt()->Format.fprintffmt"Missing liquidity baking toggle vote, please use either the \
--liquidity-baking-toggle-vote or --votefile option or a vote 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_liquidity_baking_toggle_vote~per_block_vote_file:'atzresultLwt.t=letopenLwt_result_syntaxinlet*!()=Events.(emitreading_per_block)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*liquidity_baking_toggle_vote=trace(Block_vote_file_wrong_contentper_block_vote_file)(protect(fun()->return(Data_encoding.Json.destructvote_file_content_encodingvotes_json)))inreturnliquidity_baking_toggle_voteletread_liquidity_baking_toggle_vote_no_fail~default_liquidity_baking_vote~per_block_vote_file=read_liquidity_baking_toggle_vote~per_block_vote_file>>=function|Okvote->Lwt.returnvote|Errorerrs->Events.(emitper_block_vote_file_fail)errs>>=fun()->Lwt.returndefault_liquidity_baking_voteletload_liquidity_baking_config~per_block_vote_file_arg~(toggle_vote_arg:Protocol.Alpha_context.Liquidity_baking.liquidity_baking_toggle_voteoption):Baking_configuration.liquidity_baking_configtzresultLwt.t=letopenLwt_result_syntaxin(* If a vote file is given, it takes priority. Otherwise, we expect
a toggle vote argument to be passed. *)let*config=match(per_block_vote_file_arg,toggle_vote_arg)with|None,None->tzfailMissing_vote_on_startup|None,Somevote->return{Baking_configuration.vote_file=None;liquidity_baking_vote=vote}|Someper_block_vote_file,_->(let*!(res:_tzresult)=read_liquidity_baking_toggle_vote~per_block_vote_fileinmatchreswith|Okvote->return{Baking_configuration.vote_file=Someper_block_vote_file;liquidity_baking_vote=vote;}|Errorerrs->Events.(emitper_block_vote_file_fail)errs>>=fun()->tzfailMissing_vote_on_startup)inlet*!()=Events.(emitliquidity_baking_toggle_vote)config.liquidity_baking_voteinreturnconfig