Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Add Missing genesis checks in Gaia #3010

Merged
merged 8 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ BREAKING CHANGES
* Gaia
- [#128](https://github.com/tendermint/devops/issues/128) Updated CircleCI job to trigger website build on every push to master/develop.
- [\#2994](https://github.com/cosmos/cosmos-sdk/pull/2994) Change wrong-password error message.
- \#3009 added missing gaia genesis verification
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved

* SDK
- [auth] \#2952 Signatures are no longer serialized on chain with the account number and sequence number
Expand Down
34 changes: 28 additions & 6 deletions cmd/gaia/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,42 @@ func NewDefaultGenesisState() GenesisState {
// TODO: No validators are both bonded and jailed (#2088)
// TODO: Error if there is a duplicate validator (#1708)
// TODO: Ensure all state machine parameters are in genesis (#1704)
func GaiaValidateGenesisState(genesisState GenesisState) (err error) {
err = validateGenesisStateAccounts(genesisState.Accounts)
func GaiaValidateGenesisState(genesisState GenesisState) error {
err := validateGenesisStateAccounts(genesisState.Accounts)
if err != nil {
return
return err
}
// skip stakeData validation as genesis is created from txs
if len(genesisState.GenTxs) > 0 {
return nil
}
return stake.ValidateGenesis(genesisState.StakeData)

stake.ValidateGenesis(genesisState.StakeData)
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
mint.ValidateGenesis(genesisState.MintData)
if err != nil {
return err
}
distr.ValidateGenesis(genesisState.DistrData)
if err != nil {
return err
}
gov.ValidateGenesis(genesisState.GovData)
if err != nil {
return err
}
slashing.ValidateGenesis(genesisState.SlashingData)
if err != nil {
return err
}

return nil
}

// Ensures that there are no duplicate accounts in the genesis state,
func validateGenesisStateAccounts(accs []GenesisAccount) (err error) {
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
func validateGenesisStateAccounts(accs []GenesisAccount) error {
addrMap := make(map[string]bool, len(accs))
for i := 0; i < len(accs); i++ {
acc := accs[i]
Expand All @@ -178,7 +200,7 @@ func validateGenesisStateAccounts(accs []GenesisAccount) (err error) {
}
addrMap[strAddr] = true
}
return
return nil
}

// GaiaAppGenState but with JSON
Expand Down
1 change: 1 addition & 0 deletions x/distribution/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var (
InitialFeePool = types.InitialFeePool

NewGenesisState = types.NewGenesisState
ValidateGenesis = types.ValidateGenesis
DefaultGenesisState = types.DefaultGenesisState
DefaultGenesisWithValidators = types.DefaultGenesisWithValidators

Expand Down
24 changes: 24 additions & 0 deletions x/distribution/types/fee_pool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -31,3 +33,25 @@ func InitialFeePool() FeePool {
CommunityPool: DecCoins{},
}
}

// ValidateGenesis validates the fee pool for a genesis state
func (f FeePool) ValidateGenesis() error {
if f.TotalValAccum.Accum.IsNegative() {
return fmt.Errorf("negative accum in distribution fee pool, is %v",
f.TotalValAccum.Accum.String())
}
if f.TotalValAccum.UpdateHeight < 0 {
return fmt.Errorf("negative update height in distribution fee pool, is %v",
f.TotalValAccum.UpdateHeight)
}
if f.ValPool.HasNegative() {
return fmt.Errorf("negative ValPool in distribution fee pool, is %v",
f.ValPool)
}
if f.CommunityPool.HasNegative() {
return fmt.Errorf("negative CommunityPool in distribution fee pool, is %v",
f.CommunityPool)
}

return nil
}
33 changes: 32 additions & 1 deletion x/distribution/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package types

import sdk "github.com/cosmos/cosmos-sdk/types"
import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// the address for where distributions rewards are withdrawn to by default
// this struct is only used at genesis to feed in default withdraw addresses
Expand Down Expand Up @@ -67,3 +71,30 @@ func DefaultGenesisWithValidators(valAddrs []sdk.ValAddress) GenesisState {
DelegationDistInfos: ddis,
}
}

// ValidateGenesis validates the genesis state of distribution genesis input
func ValidateGenesis(data GenesisState) error {
if data.CommunityTax.IsNegative() {
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("mint parameter CommunityTax should be positive, is %s",
data.CommunityTax.String())
}
if data.CommunityTax.GT(sdk.OneDec()) {
return fmt.Errorf("mint parameter CommunityTax should be less than one, is %s",
data.CommunityTax.String())
}
if data.BaseProposerReward.IsNegative() {
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("mint parameter BaseProposerReward should be positive, is %s",
data.BaseProposerReward.String())
}
if data.BonusProposerReward.IsNegative() {
return fmt.Errorf("mint parameter BonusProposerReward should be positive, is %s",
data.BonusProposerReward.String())
}
if (data.BaseProposerReward.Add(data.BonusProposerReward)).
GT(sdk.OneDec()) {
return fmt.Errorf("mint parameters BaseProposerReward and "+
"BonusProposerReward cannot add to be greater than one be less than one, "+
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
"adds to %s", data.BaseProposerReward.Add(data.BonusProposerReward).String())
}
return data.FeePool.ValidateGenesis()
}
5 changes: 5 additions & 0 deletions x/gov/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func DefaultGenesisState() GenesisState {
}
}

// ValidateGenesis TODO https://github.com/cosmos/cosmos-sdk/issues/3007
func ValidateGenesis(data GenesisState) error {
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

// InitGenesis - store genesis parameters
func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) {
err := k.setInitialProposalID(ctx, data.StartingProposalID)
Expand Down
5 changes: 5 additions & 0 deletions x/slashing/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func DefaultGenesisState() GenesisState {
}
}

// ValidateGenesis TODO https://github.com/cosmos/cosmos-sdk/issues/3008
func ValidateGenesis(data GenesisState) error {
return nil
}

// InitGenesis initialize default parameters
// and the keeper's address to pubkey map
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState, sdata types.GenesisState) {
Expand Down