cosmos-sdk/x/distribution/keeper/genesis.go
Jonathan Gimeno abb3dfefa0
[Bank] Remove the unsafe balance changing API (#8473)
* temp commit

* setbalance now is internal

* remove set balances in genesis

* feedback test commit

* update tests

* fix: genesis panic message

* fix not bonded pool

* fix(staking): genesis test

* fix(simapp): rollback state fix change

* fix(staking): genesis large val set test

* [Bank Refactor] Frojdi jonathan/remove setsupply (#8491)

* init supply in a different way

* remove external usage of set supply

* change(staking): replace SetSupply with MintCoins in tests

* change(evidence): replace SetSupply with MintCoins in tests

* change(crisis): remove SetSupply in tests

* change(bank): remove set supply from genesis tests

* change(bank): remove set supply from keeper tests

* change(bank): remove remaining set supply usage from keeper tests

* change(bank): remove set supply usage from grpc query and querier tests

* change(bank): remove SetSupply from keeper interface

Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com>

* remove setbalances from genesis in gov

* remove keyring

* add init genesis state

* change(staking): make genesis checks coherent and add tests

* remove setbalances on distribution

* fix(staking): genesis tests

* [Bank Refactor]: Remove SetBalances usage from the code and tests (#8509)

* change(distribution): remove SetBalances usage from keeper tests

* add(simapp): FundAccount utility function

* chore(staking): use FundAccount in keeper tests

* change(staking): remove usage of SetBalance in allocation tests

* change(staking): remove usage of SetBalance in delegation tests

* change(staking): remove usage of SetBalance in proposal handler tests

* change(staking): remove usage of SetBalances in grpc query tests

* change(staking): remove usage of SetBalances in operations tests

* change(distribution): remove usage of SetBalances in genesis

* change(authz): remove usage of SetBalances keeper and operations test

* fix(authz): TestKeeperFees failing test

* change(slashing): remove SetBalances from expected BankKeeper

* change(slashing): remove usage of SetBalances in tests

* change(distribution): remove SetBalances from expected BankKeeper

* change(genutil): remove usage of SetBalances from tests

* change(gov): remove SetBalances from expected BankKeeper

* change(gov): remove usage of SetBalances from tests

* change(staking): remove usage of SetBalances from slash tests

* change(staking): remove SetBalances from expected BankKeeper

* change(staking): remove usage of SetBalances from delegation tests

* change(staking): remove usage of SetBalances from operations tests

* change(staking): remove usage of SetBalances from validator tests

* change(bank): remove usage of SetBalances from app tests

* change(bank): remove usage of SetBalances from bench tests

* change(bank): remove usage of SetBalances from querier tests

* change(bank): remove usage of SetBalances from grpc query tests

* change(bank): remove usage of SetBalances from operations tests

* change(bank): partially remove usage of SetBalances from keeper tests

* change(bank): finalize removal of usage of SetBalances from keeper tests

* change(auth): remove usage of SetBalances from verify tests

* change(auth): partially remove usage of SetBalances from tests

* [Bank refactor]: finalize removal of setbalances from auth (#8527)

* add tests with is check tx

* temp commit

* fix test

* fix other test and remove setbalances

* change(auth): remove usage of SetBalances is vesting tests

Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com>

* change(types): remove usage of SetBalances in queries

* fix(types): pagination tests

* [Bank refactor] fix pagination tests (#8550)

* fix tests

* lint

* change(bank): remove SetBalances from keeper public API

Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com>
Co-authored-by: SaReN <sahithnarahari@gmail.com>

* change(bank): remove SubtractCoins from keeper public API

* change(ibc/transfer): remove AddCoins from relay tests

* change(bank): remove AddCoins from public keeper API

* fix imports

* remove set balances

* fix fee test

* remove set balances

* fix(staking): remove dependency on minter authorization for staking pools

* chore: update CHANGELOG.md

* update: x/distribution/keeper/keeper_test.go

Co-authored-by: Robert Zaremba <robert@zaremba.ch>

* Update simapp/test_helpers.go

Co-authored-by: Robert Zaremba <robert@zaremba.ch>

* Update x/staking/genesis_test.go

Co-authored-by: Robert Zaremba <robert@zaremba.ch>

* fix(simapp): FundAccount amount variable name

* fix some PR issues

Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com>
Co-authored-by: Frojdi Dymylja <33157909+fdymylja@users.noreply.github.com>
Co-authored-by: SaReN <sahithnarahari@gmail.com>
Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Robert Zaremba <robert@zaremba.ch>
2021-02-17 18:20:33 +00:00

195 lines
6.0 KiB
Go

package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)
// InitGenesis sets distribution information for genesis
func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
var moduleHoldings sdk.DecCoins
k.SetFeePool(ctx, data.FeePool)
k.SetParams(ctx, data.Params)
for _, dwi := range data.DelegatorWithdrawInfos {
delegatorAddress, err := sdk.AccAddressFromBech32(dwi.DelegatorAddress)
if err != nil {
panic(err)
}
withdrawAddress, err := sdk.AccAddressFromBech32(dwi.WithdrawAddress)
if err != nil {
panic(err)
}
k.SetDelegatorWithdrawAddr(ctx, delegatorAddress, withdrawAddress)
}
var previousProposer sdk.ConsAddress
if data.PreviousProposer != "" {
var err error
previousProposer, err = sdk.ConsAddressFromBech32(data.PreviousProposer)
if err != nil {
panic(err)
}
}
k.SetPreviousProposerConsAddr(ctx, previousProposer)
for _, rew := range data.OutstandingRewards {
valAddr, err := sdk.ValAddressFromBech32(rew.ValidatorAddress)
if err != nil {
panic(err)
}
k.SetValidatorOutstandingRewards(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: rew.OutstandingRewards})
moduleHoldings = moduleHoldings.Add(rew.OutstandingRewards...)
}
for _, acc := range data.ValidatorAccumulatedCommissions {
valAddr, err := sdk.ValAddressFromBech32(acc.ValidatorAddress)
if err != nil {
panic(err)
}
k.SetValidatorAccumulatedCommission(ctx, valAddr, acc.Accumulated)
}
for _, his := range data.ValidatorHistoricalRewards {
valAddr, err := sdk.ValAddressFromBech32(his.ValidatorAddress)
if err != nil {
panic(err)
}
k.SetValidatorHistoricalRewards(ctx, valAddr, his.Period, his.Rewards)
}
for _, cur := range data.ValidatorCurrentRewards {
valAddr, err := sdk.ValAddressFromBech32(cur.ValidatorAddress)
if err != nil {
panic(err)
}
k.SetValidatorCurrentRewards(ctx, valAddr, cur.Rewards)
}
for _, del := range data.DelegatorStartingInfos {
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
if err != nil {
panic(err)
}
delegatorAddress, err := sdk.AccAddressFromBech32(del.DelegatorAddress)
if err != nil {
panic(err)
}
k.SetDelegatorStartingInfo(ctx, valAddr, delegatorAddress, del.StartingInfo)
}
for _, evt := range data.ValidatorSlashEvents {
valAddr, err := sdk.ValAddressFromBech32(evt.ValidatorAddress)
if err != nil {
panic(err)
}
k.SetValidatorSlashEvent(ctx, valAddr, evt.Height, evt.Period, evt.ValidatorSlashEvent)
}
moduleHoldings = moduleHoldings.Add(data.FeePool.CommunityPool...)
moduleHoldingsInt, _ := moduleHoldings.TruncateDecimal()
// check if the module account exists
moduleAcc := k.GetDistributionAccount(ctx)
if moduleAcc == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
balances := k.bankKeeper.GetAllBalances(ctx, moduleAcc.GetAddress())
if balances.IsZero() {
k.authKeeper.SetModuleAccount(ctx, moduleAcc)
}
if !balances.IsEqual(moduleHoldingsInt) {
panic(fmt.Sprintf("distribution module balance does not match the module holdings: %s <-> %s", balances, moduleHoldingsInt))
}
}
// ExportGenesis returns a GenesisState for a given context and keeper.
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
feePool := k.GetFeePool(ctx)
params := k.GetParams(ctx)
dwi := make([]types.DelegatorWithdrawInfo, 0)
k.IterateDelegatorWithdrawAddrs(ctx, func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool) {
dwi = append(dwi, types.DelegatorWithdrawInfo{
DelegatorAddress: del.String(),
WithdrawAddress: addr.String(),
})
return false
})
pp := k.GetPreviousProposerConsAddr(ctx)
outstanding := make([]types.ValidatorOutstandingRewardsRecord, 0)
k.IterateValidatorOutstandingRewards(ctx,
func(addr sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool) {
outstanding = append(outstanding, types.ValidatorOutstandingRewardsRecord{
ValidatorAddress: addr.String(),
OutstandingRewards: rewards.Rewards,
})
return false
},
)
acc := make([]types.ValidatorAccumulatedCommissionRecord, 0)
k.IterateValidatorAccumulatedCommissions(ctx,
func(addr sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool) {
acc = append(acc, types.ValidatorAccumulatedCommissionRecord{
ValidatorAddress: addr.String(),
Accumulated: commission,
})
return false
},
)
his := make([]types.ValidatorHistoricalRewardsRecord, 0)
k.IterateValidatorHistoricalRewards(ctx,
func(val sdk.ValAddress, period uint64, rewards types.ValidatorHistoricalRewards) (stop bool) {
his = append(his, types.ValidatorHistoricalRewardsRecord{
ValidatorAddress: val.String(),
Period: period,
Rewards: rewards,
})
return false
},
)
cur := make([]types.ValidatorCurrentRewardsRecord, 0)
k.IterateValidatorCurrentRewards(ctx,
func(val sdk.ValAddress, rewards types.ValidatorCurrentRewards) (stop bool) {
cur = append(cur, types.ValidatorCurrentRewardsRecord{
ValidatorAddress: val.String(),
Rewards: rewards,
})
return false
},
)
dels := make([]types.DelegatorStartingInfoRecord, 0)
k.IterateDelegatorStartingInfos(ctx,
func(val sdk.ValAddress, del sdk.AccAddress, info types.DelegatorStartingInfo) (stop bool) {
dels = append(dels, types.DelegatorStartingInfoRecord{
ValidatorAddress: val.String(),
DelegatorAddress: del.String(),
StartingInfo: info,
})
return false
},
)
slashes := make([]types.ValidatorSlashEventRecord, 0)
k.IterateValidatorSlashEvents(ctx,
func(val sdk.ValAddress, height uint64, event types.ValidatorSlashEvent) (stop bool) {
slashes = append(slashes, types.ValidatorSlashEventRecord{
ValidatorAddress: val.String(),
Height: height,
Period: event.ValidatorPeriod,
ValidatorSlashEvent: event,
})
return false
},
)
return types.NewGenesisState(params, feePool, dwi, pp, outstanding, acc, his, cur, dels, slashes)
}