github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/distribution/genesis.go (about)

     1  package distribution
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/distribution/types"
     8  )
     9  
    10  // InitGenesis sets distribution information for genesis
    11  func InitGenesis(ctx sdk.Context, keeper Keeper, supplyKeeper types.SupplyKeeper, data types.GenesisState) {
    12  	var moduleHoldings sdk.DecCoins
    13  
    14  	keeper.SetFeePool(ctx, data.FeePool)
    15  	keeper.SetParams(ctx, data.Params)
    16  
    17  	for _, dwi := range data.DelegatorWithdrawInfos {
    18  		keeper.SetDelegatorWithdrawAddr(ctx, dwi.DelegatorAddress, dwi.WithdrawAddress)
    19  	}
    20  	keeper.SetPreviousProposerConsAddr(ctx, data.PreviousProposer)
    21  	for _, rew := range data.OutstandingRewards {
    22  		keeper.SetValidatorOutstandingRewards(ctx, rew.ValidatorAddress, rew.OutstandingRewards)
    23  		moduleHoldings = moduleHoldings.Add(rew.OutstandingRewards...)
    24  	}
    25  	for _, acc := range data.ValidatorAccumulatedCommissions {
    26  		keeper.SetValidatorAccumulatedCommission(ctx, acc.ValidatorAddress, acc.Accumulated)
    27  	}
    28  	for _, his := range data.ValidatorHistoricalRewards {
    29  		keeper.SetValidatorHistoricalRewards(ctx, his.ValidatorAddress, his.Period, his.Rewards)
    30  	}
    31  	for _, cur := range data.ValidatorCurrentRewards {
    32  		keeper.SetValidatorCurrentRewards(ctx, cur.ValidatorAddress, cur.Rewards)
    33  	}
    34  	for _, del := range data.DelegatorStartingInfos {
    35  		keeper.SetDelegatorStartingInfo(ctx, del.ValidatorAddress, del.DelegatorAddress, del.StartingInfo)
    36  	}
    37  	for _, evt := range data.ValidatorSlashEvents {
    38  		keeper.SetValidatorSlashEvent(ctx, evt.ValidatorAddress, evt.Height, evt.Period, evt.Event)
    39  	}
    40  
    41  	moduleHoldings = moduleHoldings.Add(data.FeePool.CommunityPool...)
    42  	moduleHoldingsInt, _ := moduleHoldings.TruncateDecimal()
    43  
    44  	// check if the module account exists
    45  	moduleAcc := keeper.GetDistributionAccount(ctx)
    46  	if moduleAcc == nil {
    47  		panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
    48  	}
    49  
    50  	if moduleAcc.GetCoins().IsZero() {
    51  		if err := moduleAcc.SetCoins(moduleHoldingsInt); err != nil {
    52  			panic(err)
    53  		}
    54  		supplyKeeper.SetModuleAccount(ctx, moduleAcc)
    55  	}
    56  }
    57  
    58  // ExportGenesis returns a GenesisState for a given context and keeper.
    59  func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
    60  	feePool := keeper.GetFeePool(ctx)
    61  	params := keeper.GetParams(ctx)
    62  
    63  	dwi := make([]types.DelegatorWithdrawInfo, 0)
    64  	keeper.IterateDelegatorWithdrawAddrs(ctx, func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool) {
    65  		dwi = append(dwi, types.DelegatorWithdrawInfo{
    66  			DelegatorAddress: del,
    67  			WithdrawAddress:  addr,
    68  		})
    69  		return false
    70  	})
    71  
    72  	pp := keeper.GetPreviousProposerConsAddr(ctx)
    73  	outstanding := make([]types.ValidatorOutstandingRewardsRecord, 0)
    74  	keeper.IterateValidatorOutstandingRewards(ctx,
    75  		func(addr sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool) {
    76  			outstanding = append(outstanding, types.ValidatorOutstandingRewardsRecord{
    77  				ValidatorAddress:   addr,
    78  				OutstandingRewards: rewards,
    79  			})
    80  			return false
    81  		},
    82  	)
    83  
    84  	acc := make([]types.ValidatorAccumulatedCommissionRecord, 0)
    85  	keeper.IterateValidatorAccumulatedCommissions(ctx,
    86  		func(addr sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool) {
    87  			acc = append(acc, types.ValidatorAccumulatedCommissionRecord{
    88  				ValidatorAddress: addr,
    89  				Accumulated:      commission,
    90  			})
    91  			return false
    92  		},
    93  	)
    94  
    95  	his := make([]types.ValidatorHistoricalRewardsRecord, 0)
    96  	keeper.IterateValidatorHistoricalRewards(ctx,
    97  		func(val sdk.ValAddress, period uint64, rewards types.ValidatorHistoricalRewards) (stop bool) {
    98  			his = append(his, types.ValidatorHistoricalRewardsRecord{
    99  				ValidatorAddress: val,
   100  				Period:           period,
   101  				Rewards:          rewards,
   102  			})
   103  			return false
   104  		},
   105  	)
   106  
   107  	cur := make([]types.ValidatorCurrentRewardsRecord, 0)
   108  	keeper.IterateValidatorCurrentRewards(ctx,
   109  		func(val sdk.ValAddress, rewards types.ValidatorCurrentRewards) (stop bool) {
   110  			cur = append(cur, types.ValidatorCurrentRewardsRecord{
   111  				ValidatorAddress: val,
   112  				Rewards:          rewards,
   113  			})
   114  			return false
   115  		},
   116  	)
   117  	dels := make([]types.DelegatorStartingInfoRecord, 0)
   118  	keeper.IterateDelegatorStartingInfos(ctx,
   119  		func(val sdk.ValAddress, del sdk.AccAddress, info types.DelegatorStartingInfo) (stop bool) {
   120  			dels = append(dels, types.DelegatorStartingInfoRecord{
   121  				ValidatorAddress: val,
   122  				DelegatorAddress: del,
   123  				StartingInfo:     info,
   124  			})
   125  			return false
   126  		},
   127  	)
   128  
   129  	slashes := make([]types.ValidatorSlashEventRecord, 0)
   130  	keeper.IterateValidatorSlashEvents(ctx,
   131  		func(val sdk.ValAddress, height uint64, event types.ValidatorSlashEvent) (stop bool) {
   132  			slashes = append(slashes, types.ValidatorSlashEventRecord{
   133  				ValidatorAddress: val,
   134  				Height:           height,
   135  				Period:           event.ValidatorPeriod,
   136  				Event:            event,
   137  			})
   138  			return false
   139  		},
   140  	)
   141  
   142  	return types.NewGenesisState(params, feePool, dwi, pp, outstanding, acc, his, cur, dels, slashes)
   143  }