github.com/Finschia/finschia-sdk@v0.49.1/x/distribution/keeper/genesis.go (about) 1 package keeper 2 3 import ( 4 "fmt" 5 6 sdk "github.com/Finschia/finschia-sdk/types" 7 "github.com/Finschia/finschia-sdk/x/distribution/types" 8 ) 9 10 // InitGenesis sets distribution information for genesis 11 func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { 12 var moduleHoldings sdk.DecCoins 13 14 k.SetFeePool(ctx, data.FeePool) 15 k.SetParams(ctx, data.Params) 16 17 for _, dwi := range data.DelegatorWithdrawInfos { 18 delegatorAddress := sdk.MustAccAddressFromBech32(dwi.DelegatorAddress) 19 withdrawAddress := sdk.MustAccAddressFromBech32(dwi.WithdrawAddress) 20 k.SetDelegatorWithdrawAddr(ctx, delegatorAddress, withdrawAddress) 21 } 22 23 var previousProposer sdk.ConsAddress 24 if data.PreviousProposer != "" { 25 var err error 26 previousProposer, err = sdk.ConsAddressFromBech32(data.PreviousProposer) 27 if err != nil { 28 panic(err) 29 } 30 } 31 32 k.SetPreviousProposerConsAddr(ctx, previousProposer) 33 34 for _, rew := range data.OutstandingRewards { 35 valAddr, err := sdk.ValAddressFromBech32(rew.ValidatorAddress) 36 if err != nil { 37 panic(err) 38 } 39 k.SetValidatorOutstandingRewards(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: rew.OutstandingRewards}) 40 moduleHoldings = moduleHoldings.Add(rew.OutstandingRewards...) 41 } 42 for _, acc := range data.ValidatorAccumulatedCommissions { 43 valAddr, err := sdk.ValAddressFromBech32(acc.ValidatorAddress) 44 if err != nil { 45 panic(err) 46 } 47 k.SetValidatorAccumulatedCommission(ctx, valAddr, acc.Accumulated) 48 } 49 for _, his := range data.ValidatorHistoricalRewards { 50 valAddr, err := sdk.ValAddressFromBech32(his.ValidatorAddress) 51 if err != nil { 52 panic(err) 53 } 54 k.SetValidatorHistoricalRewards(ctx, valAddr, his.Period, his.Rewards) 55 } 56 for _, cur := range data.ValidatorCurrentRewards { 57 valAddr, err := sdk.ValAddressFromBech32(cur.ValidatorAddress) 58 if err != nil { 59 panic(err) 60 } 61 k.SetValidatorCurrentRewards(ctx, valAddr, cur.Rewards) 62 } 63 for _, del := range data.DelegatorStartingInfos { 64 valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress) 65 if err != nil { 66 panic(err) 67 } 68 delegatorAddress := sdk.MustAccAddressFromBech32(del.DelegatorAddress) 69 70 k.SetDelegatorStartingInfo(ctx, valAddr, delegatorAddress, del.StartingInfo) 71 } 72 for _, evt := range data.ValidatorSlashEvents { 73 valAddr, err := sdk.ValAddressFromBech32(evt.ValidatorAddress) 74 if err != nil { 75 panic(err) 76 } 77 k.SetValidatorSlashEvent(ctx, valAddr, evt.Height, evt.Period, evt.ValidatorSlashEvent) 78 } 79 80 moduleHoldings = moduleHoldings.Add(data.FeePool.CommunityPool...) 81 moduleHoldingsInt, _ := moduleHoldings.TruncateDecimal() 82 83 // check if the module account exists 84 moduleAcc := k.GetDistributionAccount(ctx) 85 if moduleAcc == nil { 86 panic(fmt.Sprintf("%s module account has not been set", types.ModuleName)) 87 } 88 89 balances := k.bankKeeper.GetAllBalances(ctx, moduleAcc.GetAddress()) 90 if balances.IsZero() { 91 k.authKeeper.SetModuleAccount(ctx, moduleAcc) 92 } 93 if !balances.Equal(moduleHoldingsInt) { 94 panic(fmt.Sprintf("distribution module balance does not match the module holdings: %s <-> %s", balances, moduleHoldingsInt)) 95 } 96 } 97 98 // ExportGenesis returns a GenesisState for a given context and keeper. 99 func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { 100 feePool := k.GetFeePool(ctx) 101 params := k.GetParams(ctx) 102 103 dwi := make([]types.DelegatorWithdrawInfo, 0) 104 k.IterateDelegatorWithdrawAddrs(ctx, func(del, addr sdk.AccAddress) (stop bool) { 105 dwi = append(dwi, types.DelegatorWithdrawInfo{ 106 DelegatorAddress: del.String(), 107 WithdrawAddress: addr.String(), 108 }) 109 return false 110 }) 111 112 pp := k.GetPreviousProposerConsAddr(ctx) 113 outstanding := make([]types.ValidatorOutstandingRewardsRecord, 0) 114 115 k.IterateValidatorOutstandingRewards(ctx, 116 func(addr sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool) { 117 outstanding = append(outstanding, types.ValidatorOutstandingRewardsRecord{ 118 ValidatorAddress: addr.String(), 119 OutstandingRewards: rewards.Rewards, 120 }) 121 return false 122 }, 123 ) 124 125 acc := make([]types.ValidatorAccumulatedCommissionRecord, 0) 126 k.IterateValidatorAccumulatedCommissions(ctx, 127 func(addr sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool) { 128 acc = append(acc, types.ValidatorAccumulatedCommissionRecord{ 129 ValidatorAddress: addr.String(), 130 Accumulated: commission, 131 }) 132 return false 133 }, 134 ) 135 136 his := make([]types.ValidatorHistoricalRewardsRecord, 0) 137 k.IterateValidatorHistoricalRewards(ctx, 138 func(val sdk.ValAddress, period uint64, rewards types.ValidatorHistoricalRewards) (stop bool) { 139 his = append(his, types.ValidatorHistoricalRewardsRecord{ 140 ValidatorAddress: val.String(), 141 Period: period, 142 Rewards: rewards, 143 }) 144 return false 145 }, 146 ) 147 148 cur := make([]types.ValidatorCurrentRewardsRecord, 0) 149 k.IterateValidatorCurrentRewards(ctx, 150 func(val sdk.ValAddress, rewards types.ValidatorCurrentRewards) (stop bool) { 151 cur = append(cur, types.ValidatorCurrentRewardsRecord{ 152 ValidatorAddress: val.String(), 153 Rewards: rewards, 154 }) 155 return false 156 }, 157 ) 158 159 dels := make([]types.DelegatorStartingInfoRecord, 0) 160 k.IterateDelegatorStartingInfos(ctx, 161 func(val sdk.ValAddress, del sdk.AccAddress, info types.DelegatorStartingInfo) (stop bool) { 162 dels = append(dels, types.DelegatorStartingInfoRecord{ 163 ValidatorAddress: val.String(), 164 DelegatorAddress: del.String(), 165 StartingInfo: info, 166 }) 167 return false 168 }, 169 ) 170 171 slashes := make([]types.ValidatorSlashEventRecord, 0) 172 k.IterateValidatorSlashEvents(ctx, 173 func(val sdk.ValAddress, height uint64, event types.ValidatorSlashEvent) (stop bool) { 174 slashes = append(slashes, types.ValidatorSlashEventRecord{ 175 ValidatorAddress: val.String(), 176 Height: height, 177 Period: event.ValidatorPeriod, 178 ValidatorSlashEvent: event, 179 }) 180 return false 181 }, 182 ) 183 184 return types.NewGenesisState(params, feePool, dwi, pp, outstanding, acc, his, cur, dels, slashes) 185 }