123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)openAlpha_context(** Returns the proposal submitted by the most delegates.
Returns None in case of a tie, if proposal quorum is below required
minimum or if there are no proposals. *)letselect_winning_proposalctxt=Vote.get_proposalsctxt>>=?funproposals->letmergeproposalvotewinners=matchwinnerswith|None->Some([proposal],vote)|Some(winners,winners_vote)asprevious->ifCompare.Int32.(vote=winners_vote)thenSome(proposal::winners,winners_vote)elseifCompare.Int32.(vote>winners_vote)thenSome([proposal],vote)elsepreviousinmatchProtocol_hash.Map.foldmergeproposalsNonewith|Some([proposal],vote)->Vote.listing_sizectxt>>=?funmax_vote->letmin_proposal_quorum=Constants.min_proposal_quorumctxtinletmin_vote_to_pass=Int32.div(Int32.mulmin_proposal_quorummax_vote)100_00linifCompare.Int32.(vote>=min_vote_to_pass)thenreturn_someproposalelsereturn_none|_->return_none(* in case of a tie, let's do nothing. *)(** A proposal is approved if it has supermajority and the participation reaches
the current quorum.
Supermajority means the yays are more 8/10 of casted votes.
The participation is the ratio of all received votes, including passes, with
respect to the number of possible votes.
The participation EMA (exponential moving average) uses the last
participation EMA and the current participation./
The expected quorum is calculated using the last participation EMA, capped
by the min/max quorum protocol constants. *)letapproval_and_participation_ema(ballots:Vote.ballots)~maximum_vote~participation_ema~expected_quorum=(* Note overflows: considering a maximum of 8e8 tokens, with roll size as
small as 1e3, there is a maximum of 8e5 rolls and thus votes.
In 'participation' an Int64 is used because in the worst case 'all_votes is
8e5 and after the multiplication is 8e9, making it potentially overflow a
signed Int32 which is 2e9. *)letcasted_votes=Int32.addballots.yayballots.nayinletall_votes=Int32.addcasted_votesballots.passinletsupermajority=Int32.div(Int32.mul8lcasted_votes)10linletparticipation=(* in centile of percentage *)Int64.(to_int32(div(mul(of_int32all_votes)100_00L)(of_int32maximum_vote)))inletapproval=Compare.Int32.(participation>=expected_quorum&&ballots.yay>=supermajority)inletnew_participation_ema=Int32.(div(add(mul8lparticipation_ema)(mul2lparticipation))10l)in(approval,new_participation_ema)letget_approval_and_update_participation_emactxt=Vote.get_ballotsctxt>>=?funballots->Vote.listing_sizectxt>>=?funmaximum_vote->Vote.get_participation_emactxt>>=?funparticipation_ema->Vote.get_current_quorumctxt>>=?funexpected_quorum->Vote.clear_ballotsctxt>>=functxt->let(approval,new_participation_ema)=approval_and_participation_emaballots~maximum_vote~participation_ema~expected_quoruminVote.set_participation_emactxtnew_participation_ema>|=?functxt->(ctxt,approval)(** Implements the state machine of the amendment procedure. Note that
[update_listings], that computes the vote weight of each delegate, is run at
the end of each voting period. This state-machine prepare the voting_period
for the next block. *)letstart_new_voting_periodctxt=Voting_period.get_current_kindctxt>>=?funkind->(matchkindwith|Proposal->(select_winning_proposalctxt>>=?funproposal->Vote.clear_proposalsctxt>>=functxt->matchproposalwith|None->Voting_period.resetctxt|Someproposal->Vote.init_current_proposalctxtproposal>>=?Voting_period.succ)|Exploration->get_approval_and_update_participation_emactxt>>=?fun(ctxt,approved)->ifapprovedthenVoting_period.succctxtelseVote.clear_current_proposalctxt>>=?functxt->Voting_period.resetctxt|Cooldown->Voting_period.succctxt|Promotion->get_approval_and_update_participation_emactxt>>=?fun(ctxt,approved)->ifapprovedthenVoting_period.succctxtelseVote.clear_current_proposalctxt>>=?Voting_period.reset|Adoption->Vote.get_current_proposalctxt>>=?funproposal->activatectxtproposal>>=functxt->Vote.clear_current_proposalctxt>>=?Voting_period.reset)>>=?functxt->Vote.update_listingsctxttypeerror+=|(* `Branch *)Invalid_proposal|Unexpected_proposal|Unauthorized_proposal|Too_many_proposals|Empty_proposal|Unexpected_ballot|Unauthorized_ballotlet()=letopenData_encodingin(* Invalid proposal *)register_error_kind`Branch~id:"invalid_proposal"~title:"Invalid proposal"~description:"Ballot provided for a proposal that is not the current one."~pp:(funppf()->Format.fprintfppf"Invalid proposal")empty(functionInvalid_proposal->Some()|_->None)(fun()->Invalid_proposal);(* Unexpected proposal *)register_error_kind`Branch~id:"unexpected_proposal"~title:"Unexpected proposal"~description:"Proposal recorded outside of a proposal period."~pp:(funppf()->Format.fprintfppf"Unexpected proposal")empty(functionUnexpected_proposal->Some()|_->None)(fun()->Unexpected_proposal);(* Unauthorized proposal *)register_error_kind`Branch~id:"unauthorized_proposal"~title:"Unauthorized proposal"~description:"The delegate provided for the proposal is not in the voting listings."~pp:(funppf()->Format.fprintfppf"Unauthorized proposal")empty(functionUnauthorized_proposal->Some()|_->None)(fun()->Unauthorized_proposal);(* Unexpected ballot *)register_error_kind`Branch~id:"unexpected_ballot"~title:"Unexpected ballot"~description:"Ballot recorded outside of a voting period."~pp:(funppf()->Format.fprintfppf"Unexpected ballot")empty(functionUnexpected_ballot->Some()|_->None)(fun()->Unexpected_ballot);(* Unauthorized ballot *)register_error_kind`Branch~id:"unauthorized_ballot"~title:"Unauthorized ballot"~description:"The delegate provided for the ballot is not in the voting listings."~pp:(funppf()->Format.fprintfppf"Unauthorized ballot")empty(functionUnauthorized_ballot->Some()|_->None)(fun()->Unauthorized_ballot);(* Too many proposals *)register_error_kind`Branch~id:"too_many_proposals"~title:"Too many proposals"~description:"The delegate reached the maximum number of allowed proposals."~pp:(funppf()->Format.fprintfppf"Too many proposals")empty(functionToo_many_proposals->Some()|_->None)(fun()->Too_many_proposals);(* Empty proposal *)register_error_kind`Branch~id:"empty_proposal"~title:"Empty proposal"~description:"Proposal lists cannot be empty."~pp:(funppf()->Format.fprintfppf"Empty proposal")empty(functionEmpty_proposal->Some()|_->None)(fun()->Empty_proposal)letrecord_proposalsctxtdelegateproposals=(matchproposalswith[]->errorEmpty_proposal|_::_->ok_unit)>>?=fun()->Voting_period.get_current_kindctxt>>=?function|Proposal->Vote.in_listingsctxtdelegate>>=funin_listings->ifin_listingsthen(Vote.recorded_proposal_count_for_delegatectxtdelegate>>=?funcount->assert(Compare.Int.(Constants.max_proposals_per_delegate>=count));error_whenCompare.Int.(List.compare_length_withproposals(Constants.max_proposals_per_delegate-count)>0)Too_many_proposals>>?=fun()->List.fold_left_es(functxtproposal->Vote.record_proposalctxtproposaldelegate)ctxtproposals)elsefailUnauthorized_proposal|Exploration|Cooldown|Promotion|Adoption->failUnexpected_proposalletrecord_ballotctxtdelegateproposalballot=Voting_period.get_current_kindctxt>>=?function|Exploration|Promotion->Vote.get_current_proposalctxt>>=?funcurrent_proposal->error_unless(Protocol_hash.equalproposalcurrent_proposal)Invalid_proposal>>?=fun()->Vote.has_recorded_ballotctxtdelegate>>=funhas_ballot->error_whenhas_ballotUnauthorized_ballot>>?=fun()->Vote.in_listingsctxtdelegate>>=funin_listings->ifin_listingsthenVote.record_ballotctxtdelegateballotelsefailUnauthorized_ballot|Cooldown|Proposal|Adoption->failUnexpected_ballotletmay_start_new_voting_periodctxt=Voting_period.is_last_blockctxt>>=?funis_last->ifis_lastthenstart_new_voting_periodctxtelsereturnctxt