123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)(* Sum weights for normalizing *)letsum_weights({base_total_issued_per_minute=_;baking_reward_fixed_portion_weight;baking_reward_bonus_weight;attesting_reward_weight;seed_nonce_revelation_tip_weight;vdf_revelation_tip_weight;}:Constants_parametric_repr.issuance_weights)=letr=baking_reward_fixed_portion_weightinletr=baking_reward_bonus_weight+rinletr=attesting_reward_weight+rinletr=seed_nonce_revelation_tip_weight+rinletr=vdf_revelation_tip_weight+rinassert(Compare.Int.(r>0));r(* [tez_from_weights] returns an amount of rewards in [Tez.t],
given a couple of parameters:
[rewards] describes all the possible rewards, as a record of weights
for each of them. It also gives the (maximum) amount of rewards per minute
expected on the chain
[weight] is one of those reward weights as described in [rewards]
[minimal_block_delay] is the minimum amount of time between two blocks. *)lettez_from_weights~(issuance_weights:Constants_parametric_repr.issuance_weights)~(weight:int)~(minimal_block_delay:Period_repr.t)=letsum_weights=sum_weightsissuance_weightsinletblock_delay=minimal_block_delay|>Period_repr.to_secondsin(* base_tez = issuance_weights.base_total_issued_per_minute
relative_weight = reward_weight / sum_weights
minute_per_block = block_delay (in seconds) / 60
rewarded_tez = base_tez * relative_weight * blocks_per_minute *)letnum=Int64.(mul(of_intweight)block_delay)inletden=Int64.of_int(sum_weights*60)inTez_repr.mul_ratio~rounding:`Downissuance_weights.base_total_issued_per_minute~num~den(* Bundling some functions inside a module so they can be exported as part
of `Internal_for_tests` further down. *)moduleM=structtypereward_kind=|Baking_reward_fixed_portion|Baking_reward_bonus_per_slot|Attesting_reward_per_slot|Seed_nonce_revelation_tip|Vdf_revelation_tipletreward_from_constants~(csts:Constants_parametric_repr.t)~reward_kind~(coeff:Q.t)=letopenResult_syntaxinletissuance_weights=csts.issuance_weightsinletweight=matchreward_kindwith|Baking_reward_fixed_portion->issuance_weights.baking_reward_fixed_portion_weight|Baking_reward_bonus_per_slot->issuance_weights.baking_reward_bonus_weight|Attesting_reward_per_slot->issuance_weights.attesting_reward_weight|Seed_nonce_revelation_tip->(* Seed nonce revelation rewards are given every [blocks_per_commitment](=192)th block *)letblocks_per_commitment=Int32.to_intcsts.blocks_per_commitmentinissuance_weights.seed_nonce_revelation_tip_weight*blocks_per_commitment|Vdf_revelation_tip->(* Vdf revelation rewards are given every [blocks_per_commitment](=192)th block *)letblocks_per_commitment=Int32.to_intcsts.blocks_per_commitmentinissuance_weights.vdf_revelation_tip_weight*blocks_per_commitmentinletminimal_block_delay=csts.minimal_block_delayinlet*rewards=tez_from_weights~issuance_weights~weight~minimal_block_delayinletbase_rewards=matchreward_kindwith|Baking_reward_bonus_per_slot->letbonus_committee_size=csts.consensus_committee_size-csts.consensus_thresholdinifCompare.Int.(bonus_committee_size<=0)thenTez_repr.zeroelseTez_repr.div_exnrewardsbonus_committee_size|Attesting_reward_per_slot->Tez_repr.div_exnrewardscsts.consensus_committee_size|_->rewardsinTez_repr.mul_q~rounding:`Downbase_rewardscoeffletliquidity_baking_subsidy_from_constants(constants:Constants_parametric_repr.t)=letliquidity_baking_subsidy=constants.liquidity_baking_subsidyinletminimal_block_delay=constants.minimal_block_delay|>Period_repr.to_seconds|>Int64.to_intinTez_repr.mul_q~rounding:`Downliquidity_baking_subsidyQ.(minimal_block_delay//60)endopenMletreward_from_context~ctxt~reward_kind=letcsts=Raw_context.constantsctxtinletcoeff=Raw_context.reward_coeff_for_current_cyclectxtinreward_from_constants~csts~reward_kind~coeffletbaking_reward_fixed_portionctxt=reward_from_context~ctxt~reward_kind:Baking_reward_fixed_portionletbaking_reward_bonus_per_slotctxt=reward_from_context~ctxt~reward_kind:Baking_reward_bonus_per_slotletattesting_reward_per_slotctxt=reward_from_context~ctxt~reward_kind:Attesting_reward_per_slotletliquidity_baking_subsidyctxt=letconstants=Raw_context.constantsctxtinliquidity_baking_subsidy_from_constantsconstantsletseed_nonce_revelation_tipctxt=reward_from_context~ctxt~reward_kind:Seed_nonce_revelation_tipletvdf_revelation_tipctxt=reward_from_context~ctxt~reward_kind:Vdf_revelation_tipmoduleFor_RPC=structincludeMletreward_from_constants?(coeff=Q.one)(csts:Constants_parametric_repr.t)~reward_kind=reward_from_constants~csts~reward_kind~coeffend