2018-09-17 20:02:15 -07:00
|
|
|
package distribution
|
|
|
|
|
|
|
|
import (
|
2019-06-28 13:11:27 -07:00
|
|
|
"fmt"
|
|
|
|
|
2018-09-17 20:02:15 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-09-19 20:01:55 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
2018-09-17 20:02:15 -07:00
|
|
|
)
|
|
|
|
|
2018-09-18 21:42:05 -07:00
|
|
|
// InitGenesis sets distribution information for genesis
|
2020-04-20 08:22:12 -07:00
|
|
|
func InitGenesis(ctx sdk.Context, bk types.BankKeeper, keeper Keeper, data types.GenesisState) {
|
2019-06-28 13:11:27 -07:00
|
|
|
var moduleHoldings sdk.DecCoins
|
|
|
|
|
2018-09-17 20:02:15 -07:00
|
|
|
keeper.SetFeePool(ctx, data.FeePool)
|
2020-01-10 12:08:14 -08:00
|
|
|
keeper.SetParams(ctx, data.Params)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
for _, dwi := range data.DelegatorWithdrawInfos {
|
2019-02-25 07:16:52 -08:00
|
|
|
keeper.SetDelegatorWithdrawAddr(ctx, dwi.DelegatorAddress, dwi.WithdrawAddress)
|
2018-09-17 20:02:15 -07:00
|
|
|
}
|
2019-01-16 13:38:05 -08:00
|
|
|
keeper.SetPreviousProposerConsAddr(ctx, data.PreviousProposer)
|
2019-03-06 10:54:12 -08:00
|
|
|
for _, rew := range data.OutstandingRewards {
|
2020-02-11 06:58:37 -08:00
|
|
|
keeper.SetValidatorOutstandingRewards(ctx, rew.ValidatorAddress, types.ValidatorOutstandingRewards{Rewards: rew.OutstandingRewards})
|
2020-01-03 12:44:53 -08:00
|
|
|
moduleHoldings = moduleHoldings.Add(rew.OutstandingRewards...)
|
2019-03-06 10:54:12 -08:00
|
|
|
}
|
2019-01-16 13:38:05 -08:00
|
|
|
for _, acc := range data.ValidatorAccumulatedCommissions {
|
2019-02-25 07:16:52 -08:00
|
|
|
keeper.SetValidatorAccumulatedCommission(ctx, acc.ValidatorAddress, acc.Accumulated)
|
2018-09-17 20:02:15 -07:00
|
|
|
}
|
2019-01-16 13:38:05 -08:00
|
|
|
for _, his := range data.ValidatorHistoricalRewards {
|
2019-02-25 07:16:52 -08:00
|
|
|
keeper.SetValidatorHistoricalRewards(ctx, his.ValidatorAddress, his.Period, his.Rewards)
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
for _, cur := range data.ValidatorCurrentRewards {
|
2019-02-25 07:16:52 -08:00
|
|
|
keeper.SetValidatorCurrentRewards(ctx, cur.ValidatorAddress, cur.Rewards)
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
for _, del := range data.DelegatorStartingInfos {
|
2019-02-25 07:16:52 -08:00
|
|
|
keeper.SetDelegatorStartingInfo(ctx, del.ValidatorAddress, del.DelegatorAddress, del.StartingInfo)
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
for _, evt := range data.ValidatorSlashEvents {
|
2019-07-01 09:03:34 -07:00
|
|
|
keeper.SetValidatorSlashEvent(ctx, evt.ValidatorAddress, evt.Height, evt.Period, evt.Event)
|
2018-09-17 20:02:15 -07:00
|
|
|
}
|
2019-06-28 13:11:27 -07:00
|
|
|
|
2020-01-03 12:44:53 -08:00
|
|
|
moduleHoldings = moduleHoldings.Add(data.FeePool.CommunityPool...)
|
2019-06-28 13:11:27 -07:00
|
|
|
moduleHoldingsInt, _ := moduleHoldings.TruncateDecimal()
|
|
|
|
|
|
|
|
// check if the module account exists
|
|
|
|
moduleAcc := keeper.GetDistributionAccount(ctx)
|
|
|
|
if moduleAcc == nil {
|
|
|
|
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
|
|
|
|
}
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
if bk.GetAllBalances(ctx, moduleAcc.GetAddress()).IsZero() {
|
|
|
|
if err := bk.SetBalances(ctx, moduleAcc.GetAddress(), moduleHoldingsInt); err != nil {
|
2019-06-28 13:11:27 -07:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2020-04-20 08:22:12 -07:00
|
|
|
bk.SetModuleAccount(ctx, moduleAcc)
|
2019-06-28 13:11:27 -07:00
|
|
|
}
|
2018-09-17 20:02:15 -07:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// ExportGenesis returns a GenesisState for a given context and keeper.
|
2018-11-08 16:28:28 -08:00
|
|
|
func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
|
2018-09-17 20:02:15 -07:00
|
|
|
feePool := keeper.GetFeePool(ctx)
|
2020-01-10 12:08:14 -08:00
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
dwi := make([]types.DelegatorWithdrawInfo, 0)
|
|
|
|
keeper.IterateDelegatorWithdrawAddrs(ctx, func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool) {
|
|
|
|
dwi = append(dwi, types.DelegatorWithdrawInfo{
|
2019-02-25 07:16:52 -08:00
|
|
|
DelegatorAddress: del,
|
|
|
|
WithdrawAddress: addr,
|
2019-01-16 13:38:05 -08:00
|
|
|
})
|
|
|
|
return false
|
|
|
|
})
|
2020-01-10 12:08:14 -08:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
pp := keeper.GetPreviousProposerConsAddr(ctx)
|
2019-03-06 10:54:12 -08:00
|
|
|
outstanding := make([]types.ValidatorOutstandingRewardsRecord, 0)
|
|
|
|
keeper.IterateValidatorOutstandingRewards(ctx,
|
|
|
|
func(addr sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool) {
|
|
|
|
outstanding = append(outstanding, types.ValidatorOutstandingRewardsRecord{
|
|
|
|
ValidatorAddress: addr,
|
2020-02-11 06:58:37 -08:00
|
|
|
OutstandingRewards: rewards.Rewards,
|
2019-03-06 10:54:12 -08:00
|
|
|
})
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2020-01-10 12:08:14 -08:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
acc := make([]types.ValidatorAccumulatedCommissionRecord, 0)
|
|
|
|
keeper.IterateValidatorAccumulatedCommissions(ctx,
|
|
|
|
func(addr sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool) {
|
|
|
|
acc = append(acc, types.ValidatorAccumulatedCommissionRecord{
|
2019-02-25 07:16:52 -08:00
|
|
|
ValidatorAddress: addr,
|
|
|
|
Accumulated: commission,
|
2019-01-16 13:38:05 -08:00
|
|
|
})
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2020-01-10 12:08:14 -08:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
his := make([]types.ValidatorHistoricalRewardsRecord, 0)
|
|
|
|
keeper.IterateValidatorHistoricalRewards(ctx,
|
|
|
|
func(val sdk.ValAddress, period uint64, rewards types.ValidatorHistoricalRewards) (stop bool) {
|
|
|
|
his = append(his, types.ValidatorHistoricalRewardsRecord{
|
2019-02-25 07:16:52 -08:00
|
|
|
ValidatorAddress: val,
|
|
|
|
Period: period,
|
|
|
|
Rewards: rewards,
|
2019-01-16 13:38:05 -08:00
|
|
|
})
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2020-01-10 12:08:14 -08:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
cur := make([]types.ValidatorCurrentRewardsRecord, 0)
|
|
|
|
keeper.IterateValidatorCurrentRewards(ctx,
|
|
|
|
func(val sdk.ValAddress, rewards types.ValidatorCurrentRewards) (stop bool) {
|
|
|
|
cur = append(cur, types.ValidatorCurrentRewardsRecord{
|
2019-02-25 07:16:52 -08:00
|
|
|
ValidatorAddress: val,
|
|
|
|
Rewards: rewards,
|
2019-01-16 13:38:05 -08:00
|
|
|
})
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
|
|
|
dels := make([]types.DelegatorStartingInfoRecord, 0)
|
|
|
|
keeper.IterateDelegatorStartingInfos(ctx,
|
|
|
|
func(val sdk.ValAddress, del sdk.AccAddress, info types.DelegatorStartingInfo) (stop bool) {
|
|
|
|
dels = append(dels, types.DelegatorStartingInfoRecord{
|
2019-02-25 07:16:52 -08:00
|
|
|
ValidatorAddress: val,
|
|
|
|
DelegatorAddress: del,
|
|
|
|
StartingInfo: info,
|
2019-01-16 13:38:05 -08:00
|
|
|
})
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2020-01-10 12:08:14 -08:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
slashes := make([]types.ValidatorSlashEventRecord, 0)
|
|
|
|
keeper.IterateValidatorSlashEvents(ctx,
|
|
|
|
func(val sdk.ValAddress, height uint64, event types.ValidatorSlashEvent) (stop bool) {
|
|
|
|
slashes = append(slashes, types.ValidatorSlashEventRecord{
|
2019-02-25 07:16:52 -08:00
|
|
|
ValidatorAddress: val,
|
|
|
|
Height: height,
|
2019-07-01 09:03:34 -07:00
|
|
|
Period: event.ValidatorPeriod,
|
2019-02-25 07:16:52 -08:00
|
|
|
Event: event,
|
2019-01-16 13:38:05 -08:00
|
|
|
})
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2020-01-10 12:08:14 -08:00
|
|
|
|
|
|
|
return types.NewGenesisState(params, feePool, dwi, pp, outstanding, acc, his, cur, dels, slashes)
|
2018-09-17 20:02:15 -07:00
|
|
|
}
|