123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149(*****************************************************************************)(* *)(* 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(rewards:Constants_parametric_repr.issuance_weights)=letr=rewards.baking_reward_fixed_portion_weightinletr=rewards.baking_reward_bonus_weight+rinletr=rewards.attesting_reward_weight+rinletr=rewards.liquidity_baking_subsidy_weight+rinletr=rewards.seed_nonce_revelation_tip_weight+rinletr=rewards.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 amouht 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_seconds|>Int64.to_intinletweighted_rewards_per_minute=Tez_repr.mul_exnissuance_weights.base_total_issued_per_minuteweightinletweighted_rewards_per_block=Tez_repr.(div_exn(mul_exnweighted_rewards_per_minuteblock_delay)60)inletnormalized_rewards_per_block=Tez_repr.div_exnweighted_rewards_per_blocksum_weightsinnormalized_rewards_per_block(* 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|Liquidity_baking_subsidy|Seed_nonce_revelation_tip|Vdf_revelation_tipletreward_from_constants~(csts:Constants_parametric_repr.t)~reward_kind~(coeff:Q.t)=letissuance_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|Liquidity_baking_subsidy->issuance_weights.liquidity_baking_subsidy_weight|Seed_nonce_revelation_tip->(* Seed nonce revelation rewards are given every [blocks_per_commitment](=128)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](=128)th block *)letblocks_per_commitment=Int32.to_intcsts.blocks_per_commitmentinissuance_weights.vdf_revelation_tip_weight*blocks_per_commitmentinletminimal_block_delay=csts.minimal_block_delayinletrewards=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|_->rewardsinletmutez_base_rewards=Tez_repr.to_mutezbase_rewards|>Z.of_int64inletmutez_rewards=Z.(div(mulmutez_base_rewardscoeff.num)coeff.den)inifZ.fits_int64mutez_rewardsthenTez_repr.of_mutez_exn(Z.to_int64mutez_rewards)elseTez_repr.max_mutezendopenMletreward_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=reward_from_context~ctxt~reward_kind:Liquidity_baking_subsidyletseed_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