github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/distribution/keeper/validator_distr_proposal.go (about) 1 package keeper 2 3 import ( 4 "fmt" 5 6 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 7 "github.com/fibonacci-chain/fbc/x/distribution/types" 8 "github.com/fibonacci-chain/fbc/x/staking/exported" 9 ) 10 11 func (k Keeper) initializeValidatorDistrProposal(ctx sdk.Context, val exported.ValidatorI) { 12 // set initial historical rewards (period 0) with reference count of 1 13 k.SetValidatorHistoricalRewards(ctx, val.GetOperator(), 0, types.NewValidatorHistoricalRewards(sdk.SysCoins{}, 1)) 14 15 // set current rewards (starting at period 1) 16 k.SetValidatorCurrentRewards(ctx, val.GetOperator(), types.NewValidatorCurrentRewards(sdk.SysCoins{}, 1)) 17 18 // set accumulated commissions 19 k.SetValidatorAccumulatedCommission(ctx, val.GetOperator(), types.InitialValidatorAccumulatedCommission()) 20 21 // set outstanding rewards 22 k.SetValidatorOutstandingRewards(ctx, val.GetOperator(), sdk.SysCoins{}) 23 } 24 25 func (k Keeper) initExistedValidatorForDistrProposal(ctx sdk.Context, val exported.ValidatorI) { 26 logger := k.Logger(ctx) 27 if k.HasValidatorOutstandingRewards(ctx, val.GetOperator()) { 28 logger.Debug(fmt.Sprintf("has validator, %s", val.GetOperator().String())) 29 return 30 } 31 32 // set initial historical rewards (period 0) with reference count of 2, for all old delegator count repetition 33 k.SetValidatorHistoricalRewards(ctx, val.GetOperator(), 0, types.NewValidatorHistoricalRewards(sdk.SysCoins{}, 2)) 34 35 // set current rewards (starting at period 1) 36 k.SetValidatorCurrentRewards(ctx, val.GetOperator(), types.NewValidatorCurrentRewards(sdk.SysCoins{}, 1)) 37 38 // get accumulated commissions 39 commission := k.GetValidatorAccumulatedCommission(ctx, val.GetOperator()) 40 41 // set outstanding rewards with commission 42 k.SetValidatorOutstandingRewards(ctx, val.GetOperator(), commission) 43 44 logger.Debug("initExistedValidatorForDistrProposal", "Validator", val.GetOperator(), "Commission", commission) 45 } 46 47 // increment validator period, returning the period just ended 48 func (k Keeper) incrementValidatorPeriod(ctx sdk.Context, val exported.ValidatorI) uint64 { 49 if !k.CheckDistributionProposalValid(ctx) { 50 return 0 51 } 52 53 logger := k.Logger(ctx) 54 // fetch current rewards 55 rewards := k.GetValidatorCurrentRewards(ctx, val.GetOperator()) 56 57 // calculate current ratio 58 var current sdk.SysCoins 59 if val.GetDelegatorShares().IsZero() { 60 // can't calculate ratio for zero-shares validators 61 // ergo we instead add to the community pool 62 feePool := k.GetFeePool(ctx) 63 outstanding := k.GetValidatorOutstandingRewards(ctx, val.GetOperator()) 64 feePool.CommunityPool = feePool.CommunityPool.Add(rewards.Rewards...) 65 outstanding = outstanding.Sub(rewards.Rewards) 66 k.SetFeePool(ctx, feePool) 67 k.SetValidatorOutstandingRewards(ctx, val.GetOperator(), outstanding) 68 69 current = sdk.SysCoins{} 70 logger.Debug(fmt.Sprintf("delegator shares is zero, add to the community pool, val:%s", val.GetOperator().String())) 71 } else { 72 // note: necessary to truncate so we don't allow withdrawing more rewards than owed 73 current = rewards.Rewards.QuoDecTruncate(val.GetDelegatorShares()) 74 } 75 76 // fetch historical rewards for last period 77 historical := k.GetValidatorHistoricalRewards(ctx, val.GetOperator(), rewards.Period-1).CumulativeRewardRatio 78 79 // decrement reference count 80 k.decrementReferenceCount(ctx, val.GetOperator(), rewards.Period-1) 81 82 // set new historical rewards with reference count of 1 83 k.SetValidatorHistoricalRewards(ctx, val.GetOperator(), rewards.Period, types.NewValidatorHistoricalRewards(historical.Add(current...), 1)) 84 85 // set current rewards, incrementing period by 1 86 k.SetValidatorCurrentRewards(ctx, val.GetOperator(), types.NewValidatorCurrentRewards(sdk.SysCoins{}, rewards.Period+1)) 87 88 logger.Debug("incrementValidatorPeriod", "Validator", val.GetOperator(), 89 "Period", rewards.Period, "Historical", historical, "Shares", val.GetDelegatorShares()) 90 return rewards.Period 91 } 92 93 // increment the reference count for a historical rewards value 94 func (k Keeper) incrementReferenceCount(ctx sdk.Context, valAddr sdk.ValAddress, period uint64) { 95 logger := k.Logger(ctx) 96 historical := k.GetValidatorHistoricalRewards(ctx, valAddr, period) 97 if historical.ReferenceCount > 2 { 98 panic("reference count should never exceed 2") 99 } 100 historical.ReferenceCount++ 101 logger.Debug("incrementReferenceCount", "Validator", valAddr, "Period", 102 period, "ReferenceCount", historical.ReferenceCount) 103 k.SetValidatorHistoricalRewards(ctx, valAddr, period, historical) 104 } 105 106 // decrement the reference count for a historical rewards value, and delete if zero references remain 107 func (k Keeper) decrementReferenceCount(ctx sdk.Context, valAddr sdk.ValAddress, period uint64) { 108 logger := k.Logger(ctx) 109 historical := k.GetValidatorHistoricalRewards(ctx, valAddr, period) 110 if historical.ReferenceCount == 0 { 111 panic("cannot set negative reference count") 112 } 113 historical.ReferenceCount-- 114 115 if historical.ReferenceCount == 0 { 116 k.DeleteValidatorHistoricalReward(ctx, valAddr, period) 117 } else { 118 k.SetValidatorHistoricalRewards(ctx, valAddr, period, historical) 119 } 120 121 logger.Debug("decrementReferenceCount", "Validator", valAddr, "Period", 122 period, "ReferenceCount", historical.ReferenceCount) 123 }